Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1038)

Side by Side Diff: tools/clang/rewrite_to_chrome_style/RewriteToChromeStyle.cpp

Issue 2276813003: Improve accuracy of detecting type trait fields. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@blink-style-definition-outside-of-namespace-node
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tools/clang/rewrite_to_chrome_style/tests/fields-expected.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // Changes Blink-style names to Chrome-style names. Currently transforms: 5 // Changes Blink-style names to Chrome-style names. Currently transforms:
6 // fields: 6 // fields:
7 // int m_operationCount => int operation_count_ 7 // int m_operationCount => int operation_count_
8 // variables (including parameters): 8 // variables (including parameters):
9 // int mySuperVariable => int my_super_variable 9 // int mySuperVariable => int my_super_variable
10 // constants: 10 // constants:
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 if (Node.getNumDecls() == 0) 94 if (Node.getNumDecls() == 0)
95 return false; 95 return false;
96 96
97 for (clang::NamedDecl* decl : Node.decls()) { 97 for (clang::NamedDecl* decl : Node.decls()) {
98 if (!InnerMatcher.matches(*decl, Finder, Builder)) 98 if (!InnerMatcher.matches(*decl, Finder, Builder))
99 return false; 99 return false;
100 } 100 }
101 return true; 101 return true;
102 } 102 }
103 103
104 bool IsDeclContextInWTF(const clang::DeclContext* decl_context) {
105 auto* namespace_decl = clang::dyn_cast_or_null<clang::NamespaceDecl>(
106 decl_context->getEnclosingNamespaceContext());
107 if (!namespace_decl)
108 return false;
109 if (namespace_decl->getParent()->isTranslationUnit() &&
110 namespace_decl->getName() == "WTF")
111 return true;
112 return IsDeclContextInWTF(namespace_decl->getParent());
113 }
114
115 template <typename T> 104 template <typename T>
116 bool MatchAllOverriddenMethods( 105 bool MatchAllOverriddenMethods(
117 const clang::CXXMethodDecl& decl, 106 const clang::CXXMethodDecl& decl,
118 T&& inner_matcher, 107 T&& inner_matcher,
119 clang::ast_matchers::internal::ASTMatchFinder* finder, 108 clang::ast_matchers::internal::ASTMatchFinder* finder,
120 clang::ast_matchers::internal::BoundNodesTreeBuilder* builder) { 109 clang::ast_matchers::internal::BoundNodesTreeBuilder* builder) {
121 bool override_matches = false; 110 bool override_matches = false;
122 bool override_not_matches = false; 111 bool override_not_matches = false;
123 112
124 for (auto it = decl.begin_overridden_methods(); 113 for (auto it = decl.begin_overridden_methods();
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 // static class members match against VarDecls. Blink style dictates that 435 // static class members match against VarDecls. Blink style dictates that
447 // these should be prefixed with `s_`, so strip that off. Also check for `m_` 436 // these should be prefixed with `s_`, so strip that off. Also check for `m_`
448 // and strip that off too, for code that accidentally uses the wrong prefix. 437 // and strip that off too, for code that accidentally uses the wrong prefix.
449 if (original_name.startswith(kBlinkStaticMemberPrefix)) 438 if (original_name.startswith(kBlinkStaticMemberPrefix))
450 original_name = original_name.substr(strlen(kBlinkStaticMemberPrefix)); 439 original_name = original_name.substr(strlen(kBlinkStaticMemberPrefix));
451 else if (original_name.startswith(kBlinkFieldPrefix)) 440 else if (original_name.startswith(kBlinkFieldPrefix))
452 original_name = original_name.substr(strlen(kBlinkFieldPrefix)); 441 original_name = original_name.substr(strlen(kBlinkFieldPrefix));
453 442
454 bool is_const = IsProbablyConst(decl, context); 443 bool is_const = IsProbablyConst(decl, context);
455 if (is_const) { 444 if (is_const) {
456 // Struct consts in WTF do not become kFoo cuz stuff like type traits 445 // Given
457 // should stay as lowercase. 446 // namespace WTF {
458 const clang::DeclContext* decl_context = decl.getDeclContext(); 447 // struct TypeTrait {
459 bool is_in_wtf = IsDeclContextInWTF(decl_context); 448 // static const bool value = ...;
460 const clang::CXXRecordDecl* parent = 449 // };
461 clang::dyn_cast_or_null<clang::CXXRecordDecl>(decl_context); 450 // }
462 if (is_in_wtf && parent && parent->isStruct()) 451 // matches |value|.
452 auto type_trait_matcher = varDecl(
453 hasName("value"),
454 hasStaticStorageDuration(),
455 hasType(isConstQualified()),
456 hasType(booleanType()),
457 hasAncestor(recordDecl(hasAncestor(namespaceDecl(
458 hasName("WTF"), hasParent(translationUnitDecl()))))));
dcheng 2016/08/24 21:42:17 Since this is expressed as a matcher now, should w
Łukasz Anforowicz 2016/08/24 21:55:09 Good point. Done. (although this has to go to |v
459
460 // Avoid renaming |value| -> |kValue| in WTF type traits.
461 if (!match(type_trait_matcher, decl, context).empty())
463 return false; 462 return false;
464 463
465 if (!GetNameForConstant(original_name.str(), name)) 464 if (!GetNameForConstant(original_name.str(), name))
466 return false; 465 return false;
467 } else { 466 } else {
468 name = CamelCaseToUnderscoreCase(original_name); 467 name = CamelCaseToUnderscoreCase(original_name);
469 468
470 // Non-const variables with static storage duration at namespace scope are 469 // Non-const variables with static storage duration at namespace scope are
471 // prefixed with `g_' to reduce the likelihood of a naming collision. 470 // prefixed with `g_' to reduce the likelihood of a naming collision.
472 const clang::DeclContext* decl_context = decl.getDeclContext(); 471 const clang::DeclContext* decl_context = decl.getDeclContext();
(...skipping 737 matching lines...) Expand 10 before | Expand all | Expand 10 after
1210 for (const auto& r : replacements) { 1209 for (const auto& r : replacements) {
1211 std::string replacement_text = r.getReplacementText().str(); 1210 std::string replacement_text = r.getReplacementText().str();
1212 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 1211 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
1213 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 1212 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
1214 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 1213 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
1215 } 1214 }
1216 llvm::outs() << "==== END EDITS ====\n"; 1215 llvm::outs() << "==== END EDITS ====\n";
1217 1216
1218 return 0; 1217 return 0;
1219 } 1218 }
OLDNEW
« no previous file with comments | « no previous file | tools/clang/rewrite_to_chrome_style/tests/fields-expected.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698