| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 namespace { | 49 namespace { |
| 50 | 50 |
| 51 const char kBlinkFieldPrefix[] = "m_"; | 51 const char kBlinkFieldPrefix[] = "m_"; |
| 52 const char kBlinkStaticMemberPrefix[] = "s_"; | 52 const char kBlinkStaticMemberPrefix[] = "s_"; |
| 53 const char kGeneratedFileRegex[] = "^gen/|/gen/"; | 53 const char kGeneratedFileRegex[] = "^gen/|/gen/"; |
| 54 | 54 |
| 55 const clang::ast_matchers::internal:: | 55 const clang::ast_matchers::internal:: |
| 56 VariadicDynCastAllOfMatcher<clang::Expr, clang::UnresolvedLookupExpr> | 56 VariadicDynCastAllOfMatcher<clang::Expr, clang::UnresolvedLookupExpr> |
| 57 unresolvedLookupExpr; | 57 unresolvedLookupExpr; |
| 58 | 58 |
| 59 const clang::ast_matchers::internal:: |
| 60 VariadicDynCastAllOfMatcher<clang::Expr, clang::UnresolvedMemberExpr> |
| 61 unresolvedMemberExpr; |
| 62 |
| 59 AST_MATCHER(clang::FunctionDecl, isOverloadedOperator) { | 63 AST_MATCHER(clang::FunctionDecl, isOverloadedOperator) { |
| 60 return Node.isOverloadedOperator(); | 64 return Node.isOverloadedOperator(); |
| 61 } | 65 } |
| 62 | 66 |
| 63 AST_MATCHER_P(clang::FunctionTemplateDecl, | 67 AST_MATCHER_P(clang::FunctionTemplateDecl, |
| 64 templatedDecl, | 68 templatedDecl, |
| 65 clang::ast_matchers::internal::Matcher<clang::FunctionDecl>, | 69 clang::ast_matchers::internal::Matcher<clang::FunctionDecl>, |
| 66 InnerMatcher) { | 70 InnerMatcher) { |
| 67 return InnerMatcher.matches(*Node.getTemplatedDecl(), Finder, Builder); | 71 return InnerMatcher.matches(*Node.getTemplatedDecl(), Finder, Builder); |
| 68 } | 72 } |
| (...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 assert(init.isWritten()); | 463 assert(init.isWritten()); |
| 460 return init.getSourceLocation(); | 464 return init.getSourceLocation(); |
| 461 } | 465 } |
| 462 static const char* GetName() { return "initializer"; } | 466 static const char* GetName() { return "initializer"; } |
| 463 static const char* GetType() { return "CXXCtorInitializer"; } | 467 static const char* GetType() { return "CXXCtorInitializer"; } |
| 464 }; | 468 }; |
| 465 | 469 |
| 466 template <> | 470 template <> |
| 467 struct TargetNodeTraits<clang::UnresolvedLookupExpr> { | 471 struct TargetNodeTraits<clang::UnresolvedLookupExpr> { |
| 468 static clang::SourceLocation GetLoc(const clang::UnresolvedLookupExpr& expr) { | 472 static clang::SourceLocation GetLoc(const clang::UnresolvedLookupExpr& expr) { |
| 469 return expr.getLocStart(); | 473 return expr.getNameLoc(); |
| 470 } | 474 } |
| 471 static const char* GetName() { return "expr"; } | 475 static const char* GetName() { return "expr"; } |
| 472 static const char* GetType() { return "UnresolvedLookupExpr"; } | 476 static const char* GetType() { return "UnresolvedLookupExpr"; } |
| 473 }; | 477 }; |
| 474 | 478 |
| 479 template <> |
| 480 struct TargetNodeTraits<clang::UnresolvedMemberExpr> { |
| 481 static clang::SourceLocation GetLoc(const clang::UnresolvedMemberExpr& expr) { |
| 482 return expr.getMemberLoc(); |
| 483 } |
| 484 static const char* GetName() { return "expr"; } |
| 485 static const char* GetType() { return "UnresolvedMemberExpr"; } |
| 486 }; |
| 487 |
| 475 template <typename DeclNode, typename TargetNode> | 488 template <typename DeclNode, typename TargetNode> |
| 476 class RewriterBase : public MatchFinder::MatchCallback { | 489 class RewriterBase : public MatchFinder::MatchCallback { |
| 477 public: | 490 public: |
| 478 explicit RewriterBase(Replacements* replacements) | 491 explicit RewriterBase(Replacements* replacements) |
| 479 : replacements_(replacements) {} | 492 : replacements_(replacements) {} |
| 480 | 493 |
| 481 void run(const MatchFinder::MatchResult& result) override { | 494 void run(const MatchFinder::MatchResult& result) override { |
| 482 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl"); | 495 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl"); |
| 483 // If false, there's no name to be renamed. | 496 // If false, there's no name to be renamed. |
| 484 if (!decl->getIdentifier()) | 497 if (!decl->getIdentifier()) |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 using MethodMemberRewriter = | 552 using MethodMemberRewriter = |
| 540 RewriterBase<clang::CXXMethodDecl, clang::MemberExpr>; | 553 RewriterBase<clang::CXXMethodDecl, clang::MemberExpr>; |
| 541 | 554 |
| 542 using EnumConstantDeclRewriter = | 555 using EnumConstantDeclRewriter = |
| 543 RewriterBase<clang::EnumConstantDecl, clang::NamedDecl>; | 556 RewriterBase<clang::EnumConstantDecl, clang::NamedDecl>; |
| 544 using EnumConstantDeclRefRewriter = | 557 using EnumConstantDeclRefRewriter = |
| 545 RewriterBase<clang::EnumConstantDecl, clang::DeclRefExpr>; | 558 RewriterBase<clang::EnumConstantDecl, clang::DeclRefExpr>; |
| 546 | 559 |
| 547 using UnresolvedLookupRewriter = | 560 using UnresolvedLookupRewriter = |
| 548 RewriterBase<clang::NamedDecl, clang::UnresolvedLookupExpr>; | 561 RewriterBase<clang::NamedDecl, clang::UnresolvedLookupExpr>; |
| 562 using UnresolvedMemberRewriter = |
| 563 RewriterBase<clang::NamedDecl, clang::UnresolvedMemberExpr>; |
| 549 | 564 |
| 550 using UsingDeclRewriter = RewriterBase<clang::UsingDecl, clang::NamedDecl>; | 565 using UsingDeclRewriter = RewriterBase<clang::UsingDecl, clang::NamedDecl>; |
| 551 | 566 |
| 552 } // namespace | 567 } // namespace |
| 553 | 568 |
| 554 static llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage); | 569 static llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage); |
| 555 | 570 |
| 556 int main(int argc, const char* argv[]) { | 571 int main(int argc, const char* argv[]) { |
| 557 // TODO(dcheng): Clang tooling should do this itself. | 572 // TODO(dcheng): Clang tooling should do this itself. |
| 558 // http://llvm.org/bugs/show_bug.cgi?id=21627 | 573 // http://llvm.org/bugs/show_bug.cgi?id=21627 |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 765 // selected until instantiation time either. | 780 // selected until instantiation time either. |
| 766 // | 781 // |
| 767 // Another instance where UnresolvedLookupExprs can appear is in a template | 782 // Another instance where UnresolvedLookupExprs can appear is in a template |
| 768 // argument list, like the provided example. | 783 // argument list, like the provided example. |
| 769 auto function_template_decl_matcher = | 784 auto function_template_decl_matcher = |
| 770 id("decl", functionTemplateDecl(templatedDecl(function_decl_matcher))); | 785 id("decl", functionTemplateDecl(templatedDecl(function_decl_matcher))); |
| 771 auto method_template_decl_matcher = | 786 auto method_template_decl_matcher = |
| 772 id("decl", functionTemplateDecl(templatedDecl(method_decl_matcher))); | 787 id("decl", functionTemplateDecl(templatedDecl(method_decl_matcher))); |
| 773 auto unresolved_lookup_matcher = expr(id( | 788 auto unresolved_lookup_matcher = expr(id( |
| 774 "expr", | 789 "expr", |
| 775 unresolvedLookupExpr(anyOf( | 790 unresolvedLookupExpr( |
| 776 // In order to automatically rename an unresolved lookup, the lookup | 791 // In order to automatically rename an unresolved lookup, the lookup |
| 777 // candidates must either all be Blink functions/function templates or | 792 // candidates must either all be Blink functions/function templates or |
| 778 // Blink methods/method templates. Otherwise, we might end up in a | 793 // all be Blink methods/method templates. Otherwise, we might end up |
| 779 // situation where the naming could change depending on the selected | 794 // in a situation where the naming could change depending on the |
| 780 // candidate. | 795 // selected candidate. |
| 781 allOverloadsMatch( | 796 anyOf(allOverloadsMatch(anyOf(function_decl_matcher, |
| 782 anyOf(function_decl_matcher, function_template_decl_matcher)), | 797 function_template_decl_matcher)), |
| 783 allOverloadsMatch( | 798 // Note: this matches references to methods in a non-member |
| 784 anyOf(method_decl_matcher, method_template_decl_matcher)))))); | 799 // context, e.g. Template<&Class::Method>. This and the |
| 800 // UnresolvedMemberExpr matcher below are analogous to how the |
| 801 // rewriter has both a MemberRefRewriter matcher to rewrite |
| 802 // &T::method and a MethodMemberRewriter matcher to rewriter |
| 803 // t.method(). |
| 804 allOverloadsMatch(anyOf(method_decl_matcher, |
| 805 method_template_decl_matcher)))))); |
| 785 UnresolvedLookupRewriter unresolved_lookup_rewriter(&replacements); | 806 UnresolvedLookupRewriter unresolved_lookup_rewriter(&replacements); |
| 786 match_finder.addMatcher(unresolved_lookup_matcher, | 807 match_finder.addMatcher(unresolved_lookup_matcher, |
| 787 &unresolved_lookup_rewriter); | 808 &unresolved_lookup_rewriter); |
| 788 | 809 |
| 810 // Unresolved member expressions ======== |
| 811 // Similar to unresolved lookup expressions, but for methods in a member |
| 812 // context, e.g. var_with_templated_type.Method(). |
| 813 auto unresolved_member_matcher = expr(id( |
| 814 "expr", |
| 815 unresolvedMemberExpr( |
| 816 // Similar to UnresolvedLookupExprs, all the candidate methods must be |
| 817 // Blink methods/method templates. |
| 818 allOverloadsMatch( |
| 819 anyOf(method_decl_matcher, method_template_decl_matcher))))); |
| 820 UnresolvedMemberRewriter unresolved_member_rewriter(&replacements); |
| 821 match_finder.addMatcher(unresolved_member_matcher, |
| 822 &unresolved_member_rewriter); |
| 823 |
| 789 // Using declarations ======== | 824 // Using declarations ======== |
| 790 // Given | 825 // Given |
| 791 // using blink::X; | 826 // using blink::X; |
| 792 // matches |using blink::X|. | 827 // matches |using blink::X|. |
| 793 auto using_decl_matcher = id( | 828 auto using_decl_matcher = id( |
| 794 "decl", usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(anyOf( | 829 "decl", usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(anyOf( |
| 795 var_decl_matcher, field_decl_matcher, function_decl_matcher, | 830 var_decl_matcher, field_decl_matcher, function_decl_matcher, |
| 796 method_decl_matcher, function_template_decl_matcher, | 831 method_decl_matcher, function_template_decl_matcher, |
| 797 method_template_decl_matcher, enum_member_decl_matcher))))); | 832 method_template_decl_matcher, enum_member_decl_matcher))))); |
| 798 UsingDeclRewriter using_decl_rewriter(&replacements); | 833 UsingDeclRewriter using_decl_rewriter(&replacements); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 842 for (const auto& r : replacements) { | 877 for (const auto& r : replacements) { |
| 843 std::string replacement_text = r.getReplacementText().str(); | 878 std::string replacement_text = r.getReplacementText().str(); |
| 844 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 879 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 845 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 880 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 846 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 881 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 847 } | 882 } |
| 848 llvm::outs() << "==== END EDITS ====\n"; | 883 llvm::outs() << "==== END EDITS ====\n"; |
| 849 | 884 |
| 850 return 0; | 885 return 0; |
| 851 } | 886 } |
| OLD | NEW |