| OLD | NEW |
| 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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 if (Node.getNumDecls() == 0) | 86 if (Node.getNumDecls() == 0) |
| 87 return false; | 87 return false; |
| 88 | 88 |
| 89 for (clang::NamedDecl* decl : Node.decls()) { | 89 for (clang::NamedDecl* decl : Node.decls()) { |
| 90 if (!InnerMatcher.matches(*decl, Finder, Builder)) | 90 if (!InnerMatcher.matches(*decl, Finder, Builder)) |
| 91 return false; | 91 return false; |
| 92 } | 92 } |
| 93 return true; | 93 return true; |
| 94 } | 94 } |
| 95 | 95 |
| 96 bool IsDeclContextInWTF(const clang::DeclContext* decl_context) { | |
| 97 auto* namespace_decl = clang::dyn_cast_or_null<clang::NamespaceDecl>( | |
| 98 decl_context->getEnclosingNamespaceContext()); | |
| 99 if (!namespace_decl) | |
| 100 return false; | |
| 101 if (namespace_decl->getParent()->isTranslationUnit() && | |
| 102 namespace_decl->getName() == "WTF") | |
| 103 return true; | |
| 104 return IsDeclContextInWTF(namespace_decl->getParent()); | |
| 105 } | |
| 106 | |
| 107 template <typename T> | 96 template <typename T> |
| 108 bool MatchAllOverriddenMethods( | 97 bool MatchAllOverriddenMethods( |
| 109 const clang::CXXMethodDecl& decl, | 98 const clang::CXXMethodDecl& decl, |
| 110 T&& inner_matcher, | 99 T&& inner_matcher, |
| 111 clang::ast_matchers::internal::ASTMatchFinder* finder, | 100 clang::ast_matchers::internal::ASTMatchFinder* finder, |
| 112 clang::ast_matchers::internal::BoundNodesTreeBuilder* builder) { | 101 clang::ast_matchers::internal::BoundNodesTreeBuilder* builder) { |
| 113 bool override_matches = false; | 102 bool override_matches = false; |
| 114 bool override_not_matches = false; | 103 bool override_not_matches = false; |
| 115 | 104 |
| 116 for (auto it = decl.begin_overridden_methods(); | 105 for (auto it = decl.begin_overridden_methods(); |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 else if (original_name.startswith(kBlinkFieldPrefix)) | 342 else if (original_name.startswith(kBlinkFieldPrefix)) |
| 354 original_name = original_name.substr(strlen(kBlinkFieldPrefix)); | 343 original_name = original_name.substr(strlen(kBlinkFieldPrefix)); |
| 355 | 344 |
| 356 bool is_const = IsProbablyConst(decl, context); | 345 bool is_const = IsProbablyConst(decl, context); |
| 357 if (is_const) { | 346 if (is_const) { |
| 358 // Don't try to rename constants that already conform to Chrome style. | 347 // Don't try to rename constants that already conform to Chrome style. |
| 359 if (original_name.size() >= 2 && original_name[0] == 'k' && | 348 if (original_name.size() >= 2 && original_name[0] == 'k' && |
| 360 clang::isUppercase(original_name[1])) | 349 clang::isUppercase(original_name[1])) |
| 361 return false; | 350 return false; |
| 362 | 351 |
| 363 // Struct consts in WTF do not become kFoo cuz stuff like type traits | |
| 364 // should stay as lowercase. | |
| 365 const clang::DeclContext* decl_context = decl.getDeclContext(); | |
| 366 bool is_in_wtf = IsDeclContextInWTF(decl_context); | |
| 367 const clang::CXXRecordDecl* parent = | |
| 368 clang::dyn_cast_or_null<clang::CXXRecordDecl>(decl_context); | |
| 369 if (is_in_wtf && parent && parent->isStruct()) | |
| 370 return false; | |
| 371 | |
| 372 name = 'k'; | 352 name = 'k'; |
| 373 name.append(original_name.data(), original_name.size()); | 353 name.append(original_name.data(), original_name.size()); |
| 374 name[1] = clang::toUppercase(name[1]); | 354 name[1] = clang::toUppercase(name[1]); |
| 375 } else { | 355 } else { |
| 376 name = CamelCaseToUnderscoreCase(original_name); | 356 name = CamelCaseToUnderscoreCase(original_name); |
| 377 | 357 |
| 378 // Non-const variables with static storage duration at namespace scope are | 358 // Non-const variables with static storage duration at namespace scope are |
| 379 // prefixed with `g_' to reduce the likelihood of a naming collision. | 359 // prefixed with `g_' to reduce the likelihood of a naming collision. |
| 380 const clang::DeclContext* decl_context = decl.getDeclContext(); | 360 const clang::DeclContext* decl_context = decl.getDeclContext(); |
| 381 if (name.find("g_") != 0 && decl.hasGlobalStorage() && | 361 if (name.find("g_") != 0 && decl.hasGlobalStorage() && |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 | 571 |
| 592 // Field, variable, and enum declarations ======== | 572 // Field, variable, and enum declarations ======== |
| 593 // Given | 573 // Given |
| 594 // int x; | 574 // int x; |
| 595 // struct S { | 575 // struct S { |
| 596 // int y; | 576 // int y; |
| 597 // enum { VALUE }; | 577 // enum { VALUE }; |
| 598 // }; | 578 // }; |
| 599 // matches |x|, |y|, and |VALUE|. | 579 // matches |x|, |y|, and |VALUE|. |
| 600 auto field_decl_matcher = id("decl", fieldDecl(in_blink_namespace)); | 580 auto field_decl_matcher = id("decl", fieldDecl(in_blink_namespace)); |
| 601 auto var_decl_matcher = id("decl", varDecl(in_blink_namespace)); | 581 auto is_wtf_type_trait_value = |
| 582 varDecl(hasName("value"), hasStaticStorageDuration(), |
| 583 hasType(isConstQualified()), hasType(booleanType()), |
| 584 hasAncestor(recordDecl(hasAncestor(namespaceDecl( |
| 585 hasName("WTF"), hasParent(translationUnitDecl())))))); |
| 586 auto var_decl_matcher = |
| 587 id("decl", varDecl(in_blink_namespace, unless(is_wtf_type_trait_value))); |
| 602 auto enum_member_decl_matcher = | 588 auto enum_member_decl_matcher = |
| 603 id("decl", enumConstantDecl(in_blink_namespace)); | 589 id("decl", enumConstantDecl(in_blink_namespace)); |
| 604 | 590 |
| 605 FieldDeclRewriter field_decl_rewriter(&replacements); | 591 FieldDeclRewriter field_decl_rewriter(&replacements); |
| 606 match_finder.addMatcher(field_decl_matcher, &field_decl_rewriter); | 592 match_finder.addMatcher(field_decl_matcher, &field_decl_rewriter); |
| 607 | 593 |
| 608 VarDeclRewriter var_decl_rewriter(&replacements); | 594 VarDeclRewriter var_decl_rewriter(&replacements); |
| 609 match_finder.addMatcher(var_decl_matcher, &var_decl_rewriter); | 595 match_finder.addMatcher(var_decl_matcher, &var_decl_rewriter); |
| 610 | 596 |
| 611 EnumConstantDeclRewriter enum_member_decl_rewriter(&replacements); | 597 EnumConstantDeclRewriter enum_member_decl_rewriter(&replacements); |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 879 for (const auto& r : replacements) { | 865 for (const auto& r : replacements) { |
| 880 std::string replacement_text = r.getReplacementText().str(); | 866 std::string replacement_text = r.getReplacementText().str(); |
| 881 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 867 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 882 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 868 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 883 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 869 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 884 } | 870 } |
| 885 llvm::outs() << "==== END EDITS ====\n"; | 871 llvm::outs() << "==== END EDITS ====\n"; |
| 886 | 872 |
| 887 return 0; | 873 return 0; |
| 888 } | 874 } |
| OLD | NEW |