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

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: Moving type traits detection into |var_decl_matcher|. 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
457 // should stay as lowercase.
458 const clang::DeclContext* decl_context = decl.getDeclContext();
459 bool is_in_wtf = IsDeclContextInWTF(decl_context);
460 const clang::CXXRecordDecl* parent =
461 clang::dyn_cast_or_null<clang::CXXRecordDecl>(decl_context);
462 if (is_in_wtf && parent && parent->isStruct())
463 return false;
464
465 if (!GetNameForConstant(original_name.str(), name)) 445 if (!GetNameForConstant(original_name.str(), name))
466 return false; 446 return false;
467 } else { 447 } else {
468 name = CamelCaseToUnderscoreCase(original_name); 448 name = CamelCaseToUnderscoreCase(original_name);
469 449
470 // Non-const variables with static storage duration at namespace scope are 450 // Non-const variables with static storage duration at namespace scope are
471 // prefixed with `g_' to reduce the likelihood of a naming collision. 451 // prefixed with `g_' to reduce the likelihood of a naming collision.
472 const clang::DeclContext* decl_context = decl.getDeclContext(); 452 const clang::DeclContext* decl_context = decl.getDeclContext();
473 if (name.find("g_") != 0 && decl.hasGlobalStorage() && 453 if (name.find("g_") != 0 && decl.hasGlobalStorage() &&
474 decl_context->isNamespace()) 454 decl_context->isNamespace())
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
848 828
849 // Field, variable, and enum declarations ======== 829 // Field, variable, and enum declarations ========
850 // Given 830 // Given
851 // int x; 831 // int x;
852 // struct S { 832 // struct S {
853 // int y; 833 // int y;
854 // enum { VALUE }; 834 // enum { VALUE };
855 // }; 835 // };
856 // matches |x|, |y|, and |VALUE|. 836 // matches |x|, |y|, and |VALUE|.
857 auto field_decl_matcher = id("decl", fieldDecl(in_blink_namespace)); 837 auto field_decl_matcher = id("decl", fieldDecl(in_blink_namespace));
858 auto var_decl_matcher = id("decl", varDecl(in_blink_namespace)); 838 auto var_decl_matcher = id(
839 "decl",
840 varDecl(in_blink_namespace,
841 unless(varDecl( // Unless it is a type trait's static value.
dcheng 2016/08/25 03:05:33 I'd recommend capturing the varDecl(hasName("value
Łukasz Anforowicz 2016/08/25 21:31:41 Done.
842 hasName("value"), hasStaticStorageDuration(),
Łukasz Anforowicz 2016/08/24 22:36:20 BTW: All static fields in wtf/TypeTraits.h are nam
843 hasType(isConstQualified()), hasType(booleanType()),
844 hasAncestor(recordDecl(hasAncestor(namespaceDecl(
845 hasName("WTF"), hasParent(translationUnitDecl())))))))));
859 auto enum_member_decl_matcher = 846 auto enum_member_decl_matcher =
860 id("decl", enumConstantDecl(in_blink_namespace)); 847 id("decl", enumConstantDecl(in_blink_namespace));
861 848
862 FieldDeclRewriter field_decl_rewriter(&replacements); 849 FieldDeclRewriter field_decl_rewriter(&replacements);
863 match_finder.addMatcher(field_decl_matcher, &field_decl_rewriter); 850 match_finder.addMatcher(field_decl_matcher, &field_decl_rewriter);
864 851
865 VarDeclRewriter var_decl_rewriter(&replacements); 852 VarDeclRewriter var_decl_rewriter(&replacements);
866 match_finder.addMatcher(var_decl_matcher, &var_decl_rewriter); 853 match_finder.addMatcher(var_decl_matcher, &var_decl_rewriter);
867 854
868 EnumConstantDeclRewriter enum_member_decl_rewriter(&replacements); 855 EnumConstantDeclRewriter enum_member_decl_rewriter(&replacements);
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after
1210 for (const auto& r : replacements) { 1197 for (const auto& r : replacements) {
1211 std::string replacement_text = r.getReplacementText().str(); 1198 std::string replacement_text = r.getReplacementText().str();
1212 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 1199 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
1213 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 1200 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
1214 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 1201 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
1215 } 1202 }
1216 llvm::outs() << "==== END EDITS ====\n"; 1203 llvm::outs() << "==== END EDITS ====\n";
1217 1204
1218 return 0; 1205 return 0;
1219 } 1206 }
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