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

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

Issue 1838713002: rewrite_to_chrome_style: improve template rewrite handling (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Combine template fixes Created 4 years, 8 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/template-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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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
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
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>.
danakj 2016/03/28 18:35:10 Can you say why this not using the UnresolvedMembe
dcheng 2016/03/28 21:22:44 Done.
800 allOverloadsMatch(anyOf(method_decl_matcher,
801 method_template_decl_matcher))))));
785 UnresolvedLookupRewriter unresolved_lookup_rewriter(&replacements); 802 UnresolvedLookupRewriter unresolved_lookup_rewriter(&replacements);
786 match_finder.addMatcher(unresolved_lookup_matcher, 803 match_finder.addMatcher(unresolved_lookup_matcher,
787 &unresolved_lookup_rewriter); 804 &unresolved_lookup_rewriter);
788 805
806 // Unresolved member expressions ========
807 // Similar to unresolved lookup expressions, but for methods in a member
808 // context, e.g. var_with_templated_type.Method().
809 auto unresolved_member_matcher = expr(id(
810 "expr",
811 unresolvedMemberExpr(
812 // Similar to UnresolvedLookupExprs, all the candidate methods must be
813 // Blink methods/method templates.
814 allOverloadsMatch(
815 anyOf(method_decl_matcher, method_template_decl_matcher)))));
816 UnresolvedMemberRewriter unresolved_member_rewriter(&replacements);
817 match_finder.addMatcher(unresolved_member_matcher,
818 &unresolved_member_rewriter);
819
789 // Using declarations ======== 820 // Using declarations ========
790 // Given 821 // Given
791 // using blink::X; 822 // using blink::X;
792 // matches |using blink::X|. 823 // matches |using blink::X|.
793 auto using_decl_matcher = id( 824 auto using_decl_matcher = id(
794 "decl", usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(anyOf( 825 "decl", usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(anyOf(
795 var_decl_matcher, field_decl_matcher, function_decl_matcher, 826 var_decl_matcher, field_decl_matcher, function_decl_matcher,
796 method_decl_matcher, function_template_decl_matcher, 827 method_decl_matcher, function_template_decl_matcher,
797 method_template_decl_matcher, enum_member_decl_matcher))))); 828 method_template_decl_matcher, enum_member_decl_matcher)))));
798 UsingDeclRewriter using_decl_rewriter(&replacements); 829 UsingDeclRewriter using_decl_rewriter(&replacements);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
842 for (const auto& r : replacements) { 873 for (const auto& r : replacements) {
843 std::string replacement_text = r.getReplacementText().str(); 874 std::string replacement_text = r.getReplacementText().str();
844 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 875 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
845 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 876 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
846 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 877 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
847 } 878 }
848 llvm::outs() << "==== END EDITS ====\n"; 879 llvm::outs() << "==== END EDITS ====\n";
849 880
850 return 0; 881 return 0;
851 } 882 }
OLDNEW
« no previous file with comments | « no previous file | tools/clang/rewrite_to_chrome_style/tests/template-expected.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698