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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 namespace { | 48 namespace { |
49 | 49 |
50 const char kBlinkFieldPrefix[] = "m_"; | 50 const char kBlinkFieldPrefix[] = "m_"; |
51 const char kBlinkStaticMemberPrefix[] = "s_"; | 51 const char kBlinkStaticMemberPrefix[] = "s_"; |
52 const char kGeneratedFileRegex[] = "^gen/|/gen/"; | 52 const char kGeneratedFileRegex[] = "^gen/|/gen/"; |
53 | 53 |
54 const clang::ast_matchers::internal:: | 54 const clang::ast_matchers::internal:: |
55 VariadicDynCastAllOfMatcher<clang::Expr, clang::UnresolvedMemberExpr> | 55 VariadicDynCastAllOfMatcher<clang::Expr, clang::UnresolvedMemberExpr> |
56 unresolvedMemberExpr; | 56 unresolvedMemberExpr; |
57 | 57 |
| 58 const clang::ast_matchers::internal:: |
| 59 VariadicDynCastAllOfMatcher<clang::Expr, clang::DependentScopeDeclRefExpr> |
| 60 dependentScopeDeclRefExpr; |
| 61 |
| 62 const clang::ast_matchers::internal:: |
| 63 VariadicDynCastAllOfMatcher<clang::Expr, clang::CXXDependentScopeMemberExpr> |
| 64 cxxDependentScopeMemberExpr; |
| 65 |
58 AST_MATCHER(clang::FunctionDecl, isOverloadedOperator) { | 66 AST_MATCHER(clang::FunctionDecl, isOverloadedOperator) { |
59 return Node.isOverloadedOperator(); | 67 return Node.isOverloadedOperator(); |
60 } | 68 } |
61 | 69 |
62 AST_MATCHER(clang::CXXMethodDecl, isInstanceMethod) { | 70 AST_MATCHER(clang::CXXMethodDecl, isInstanceMethod) { |
63 return Node.isInstance(); | 71 return Node.isInstance(); |
64 } | 72 } |
65 | 73 |
66 AST_MATCHER_P(clang::FunctionTemplateDecl, | 74 AST_MATCHER_P(clang::FunctionTemplateDecl, |
67 templatedDecl, | 75 templatedDecl, |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 return inner_matcher.matches(decl, finder, builder); | 159 return inner_matcher.matches(decl, finder, builder); |
152 } | 160 } |
153 | 161 |
154 AST_MATCHER_P(clang::CXXMethodDecl, | 162 AST_MATCHER_P(clang::CXXMethodDecl, |
155 includeAllOverriddenMethods, | 163 includeAllOverriddenMethods, |
156 clang::ast_matchers::internal::Matcher<clang::CXXMethodDecl>, | 164 clang::ast_matchers::internal::Matcher<clang::CXXMethodDecl>, |
157 InnerMatcher) { | 165 InnerMatcher) { |
158 return MatchAllOverriddenMethods(Node, InnerMatcher, Finder, Builder); | 166 return MatchAllOverriddenMethods(Node, InnerMatcher, Finder, Builder); |
159 } | 167 } |
160 | 168 |
| 169 // Matches |x.m| CXXDependentScopeMemberExpr if InnerMatcher matches |x|. |
| 170 AST_MATCHER_P(clang::CXXDependentScopeMemberExpr, |
| 171 hasBaseExprMissingOrMatching, |
| 172 clang::ast_matchers::internal::Matcher<clang::Expr>, |
| 173 InnerMatcher) { |
| 174 clang::Expr* base_expr = Node.isImplicitAccess() ? nullptr : Node.getBase(); |
| 175 return !base_expr || InnerMatcher.matches(*base_expr, Finder, Builder); |
| 176 } |
| 177 |
| 178 // Matches |T::m| CXXDependentScopeMemberExpr if InnerMatcher matches |T|. |
| 179 AST_MATCHER_P( |
| 180 clang::CXXDependentScopeMemberExpr, |
| 181 hasQualifierMissingOrMatching, |
| 182 clang::ast_matchers::internal::Matcher<clang::NestedNameSpecifier>, |
| 183 InnerMatcher) { |
| 184 clang::NestedNameSpecifier* qual = Node.getQualifier(); |
| 185 return !qual || InnerMatcher.matches(*qual, Finder, Builder); |
| 186 } |
| 187 |
| 188 // Matches |const Class<T>&| QualType if InnerMatcher matches |Class<T>|. |
| 189 AST_MATCHER_P(clang::QualType, |
| 190 hasBaseType, |
| 191 clang::ast_matchers::internal::Matcher<clang::Type>, |
| 192 InnerMatcher) { |
| 193 const clang::Type* type = Node.getTypePtrOrNull(); |
| 194 return type && InnerMatcher.matches(*type, Finder, Builder); |
| 195 } |
| 196 |
161 bool IsMethodOverrideOf(const clang::CXXMethodDecl& decl, | 197 bool IsMethodOverrideOf(const clang::CXXMethodDecl& decl, |
162 const char* class_name) { | 198 const char* class_name) { |
163 if (decl.getParent()->getQualifiedNameAsString() == class_name) | 199 if (decl.getParent()->getQualifiedNameAsString() == class_name) |
164 return true; | 200 return true; |
165 for (auto it = decl.begin_overridden_methods(); | 201 for (auto it = decl.begin_overridden_methods(); |
166 it != decl.end_overridden_methods(); ++it) { | 202 it != decl.end_overridden_methods(); ++it) { |
167 if (IsMethodOverrideOf(**it, class_name)) | 203 if (IsMethodOverrideOf(**it, class_name)) |
168 return true; | 204 return true; |
169 } | 205 } |
170 return false; | 206 return false; |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 | 520 |
485 template <> | 521 template <> |
486 struct TargetNodeTraits<clang::DeclRefExpr> { | 522 struct TargetNodeTraits<clang::DeclRefExpr> { |
487 static clang::SourceLocation GetLoc(const clang::DeclRefExpr& expr) { | 523 static clang::SourceLocation GetLoc(const clang::DeclRefExpr& expr) { |
488 return expr.getLocation(); | 524 return expr.getLocation(); |
489 } | 525 } |
490 static const char* GetName() { return "expr"; } | 526 static const char* GetName() { return "expr"; } |
491 }; | 527 }; |
492 | 528 |
493 template <> | 529 template <> |
| 530 struct TargetNodeTraits<clang::DependentScopeDeclRefExpr> { |
| 531 static clang::SourceLocation GetLoc( |
| 532 const clang::DependentScopeDeclRefExpr& expr) { |
| 533 return expr.getLocation(); |
| 534 } |
| 535 static const char* GetName() { return "expr"; } |
| 536 }; |
| 537 |
| 538 template <> |
| 539 struct TargetNodeTraits<clang::CXXDependentScopeMemberExpr> { |
| 540 static clang::SourceLocation GetLoc( |
| 541 const clang::CXXDependentScopeMemberExpr& expr) { |
| 542 return expr.getMemberLoc(); |
| 543 } |
| 544 static const char* GetName() { return "expr"; } |
| 545 }; |
| 546 |
| 547 template <> |
494 struct TargetNodeTraits<clang::CXXCtorInitializer> { | 548 struct TargetNodeTraits<clang::CXXCtorInitializer> { |
495 static clang::SourceLocation GetLoc(const clang::CXXCtorInitializer& init) { | 549 static clang::SourceLocation GetLoc(const clang::CXXCtorInitializer& init) { |
496 assert(init.isWritten()); | 550 assert(init.isWritten()); |
497 return init.getSourceLocation(); | 551 return init.getSourceLocation(); |
498 } | 552 } |
499 static const char* GetName() { return "initializer"; } | 553 static const char* GetName() { return "initializer"; } |
500 }; | 554 }; |
501 | 555 |
502 template <> | 556 template <> |
503 struct TargetNodeTraits<clang::UnresolvedLookupExpr> { | 557 struct TargetNodeTraits<clang::UnresolvedLookupExpr> { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 Base::AddReplacement(result, old_name, std::move(new_name)); | 649 Base::AddReplacement(result, old_name, std::move(new_name)); |
596 } | 650 } |
597 }; | 651 }; |
598 | 652 |
599 clang::DeclarationName GetUnresolvedName( | 653 clang::DeclarationName GetUnresolvedName( |
600 const clang::UnresolvedMemberExpr& expr) { | 654 const clang::UnresolvedMemberExpr& expr) { |
601 return expr.getMemberName(); | 655 return expr.getMemberName(); |
602 } | 656 } |
603 | 657 |
604 clang::DeclarationName GetUnresolvedName( | 658 clang::DeclarationName GetUnresolvedName( |
| 659 const clang::DependentScopeDeclRefExpr& expr) { |
| 660 return expr.getDeclName(); |
| 661 } |
| 662 |
| 663 clang::DeclarationName GetUnresolvedName( |
| 664 const clang::CXXDependentScopeMemberExpr& expr) { |
| 665 return expr.getMember(); |
| 666 } |
| 667 |
| 668 clang::DeclarationName GetUnresolvedName( |
605 const clang::UnresolvedUsingValueDecl& decl) { | 669 const clang::UnresolvedUsingValueDecl& decl) { |
606 return decl.getDeclName(); | 670 return decl.getDeclName(); |
607 } | 671 } |
608 | 672 |
609 // Returns whether |expr| is a callee of a method or function call. | 673 // Returns whether |expr| is a callee of a method or function call. |
610 bool IsCallee(const clang::Expr& expr, clang::ASTContext& context) { | 674 bool IsCallee(const clang::Expr& expr, clang::ASTContext& context) { |
611 auto parents = context.getParents(expr); | 675 auto parents = context.getParents(expr); |
612 return std::all_of( | 676 return std::all_of( |
613 parents.begin(), parents.end(), | 677 parents.begin(), parents.end(), |
614 [&expr](const clang::ast_type_traits::DynTypedNode& parent) { | 678 [&expr](const clang::ast_type_traits::DynTypedNode& parent) { |
(...skipping 11 matching lines...) Expand all Loading... |
626 template <typename TargetNode> | 690 template <typename TargetNode> |
627 class UnresolvedRewriterBase : public RewriterBase<TargetNode> { | 691 class UnresolvedRewriterBase : public RewriterBase<TargetNode> { |
628 public: | 692 public: |
629 using Base = RewriterBase<TargetNode>; | 693 using Base = RewriterBase<TargetNode>; |
630 | 694 |
631 explicit UnresolvedRewriterBase(std::set<Replacement>* replacements) | 695 explicit UnresolvedRewriterBase(std::set<Replacement>* replacements) |
632 : RewriterBase<TargetNode>(replacements) {} | 696 : RewriterBase<TargetNode>(replacements) {} |
633 | 697 |
634 void run(const MatchFinder::MatchResult& result) override { | 698 void run(const MatchFinder::MatchResult& result) override { |
635 const TargetNode& expr = Base::GetTargetNode(result); | 699 const TargetNode& expr = Base::GetTargetNode(result); |
636 llvm::StringRef old_name = GetUnresolvedName(expr).getAsString(); | 700 |
| 701 clang::DeclarationName unresolved_name = GetUnresolvedName(expr); |
| 702 switch (unresolved_name.getNameKind()) { |
| 703 // Do not rewrite this: |
| 704 // return operator T*(); |
| 705 // into this: |
| 706 // return Operator type - parameter - 0 - 0 * T * (); |
| 707 case clang::DeclarationName::NameKind::CXXConversionFunctionName: |
| 708 case clang::DeclarationName::NameKind::CXXOperatorName: |
| 709 case clang::DeclarationName::NameKind::CXXLiteralOperatorName: |
| 710 return; |
| 711 default: |
| 712 break; |
| 713 } |
| 714 |
| 715 llvm::StringRef old_name = unresolved_name.getAsString(); |
637 std::string new_name; | 716 std::string new_name; |
638 if (GuessNameForUnresolvedDependentNode(expr, *result.Context, old_name, | 717 if (GuessNameForUnresolvedDependentNode(expr, *result.Context, old_name, |
639 new_name)) { | 718 new_name)) { |
640 Base::AddReplacement(result, old_name, std::move(new_name)); | 719 Base::AddReplacement(result, old_name, std::move(new_name)); |
641 } | 720 } |
642 } | 721 } |
643 | 722 |
644 private: | 723 private: |
645 // This method calculates a new name for nodes that depend on template | 724 // This method calculates a new name for nodes that depend on template |
646 // parameters (http://en.cppreference.com/w/cpp/language/dependent_name). The | 725 // parameters (http://en.cppreference.com/w/cpp/language/dependent_name). The |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
709 DeclRewriterBase<clang::NamedDecl, clang::UnresolvedMemberExpr>; | 788 DeclRewriterBase<clang::NamedDecl, clang::UnresolvedMemberExpr>; |
710 | 789 |
711 using UnresolvedDependentMemberRewriter = | 790 using UnresolvedDependentMemberRewriter = |
712 UnresolvedRewriterBase<clang::UnresolvedMemberExpr>; | 791 UnresolvedRewriterBase<clang::UnresolvedMemberExpr>; |
713 | 792 |
714 using UnresolvedUsingValueDeclRewriter = | 793 using UnresolvedUsingValueDeclRewriter = |
715 UnresolvedRewriterBase<clang::UnresolvedUsingValueDecl>; | 794 UnresolvedRewriterBase<clang::UnresolvedUsingValueDecl>; |
716 | 795 |
717 using UsingDeclRewriter = DeclRewriterBase<clang::UsingDecl, clang::NamedDecl>; | 796 using UsingDeclRewriter = DeclRewriterBase<clang::UsingDecl, clang::NamedDecl>; |
718 | 797 |
| 798 using DependentScopeDeclRefExprRewriter = |
| 799 UnresolvedRewriterBase<clang::DependentScopeDeclRefExpr>; |
| 800 |
| 801 using CXXDependentScopeMemberExprRewriter = |
| 802 UnresolvedRewriterBase<clang::CXXDependentScopeMemberExpr>; |
| 803 |
719 } // namespace | 804 } // namespace |
720 | 805 |
721 static llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage); | 806 static llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage); |
722 | 807 |
723 int main(int argc, const char* argv[]) { | 808 int main(int argc, const char* argv[]) { |
724 // TODO(dcheng): Clang tooling should do this itself. | 809 // TODO(dcheng): Clang tooling should do this itself. |
725 // http://llvm.org/bugs/show_bug.cgi?id=21627 | 810 // http://llvm.org/bugs/show_bug.cgi?id=21627 |
726 llvm::InitializeNativeTarget(); | 811 llvm::InitializeNativeTarget(); |
727 llvm::InitializeNativeTargetAsmParser(); | 812 llvm::InitializeNativeTargetAsmParser(); |
728 llvm::cl::OptionCategory category( | 813 llvm::cl::OptionCategory category( |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1035 // using blink::X; | 1120 // using blink::X; |
1036 // matches |using blink::X|. | 1121 // matches |using blink::X|. |
1037 auto using_decl_matcher = id( | 1122 auto using_decl_matcher = id( |
1038 "decl", usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(anyOf( | 1123 "decl", usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(anyOf( |
1039 var_decl_matcher, field_decl_matcher, function_decl_matcher, | 1124 var_decl_matcher, field_decl_matcher, function_decl_matcher, |
1040 method_decl_matcher, function_template_decl_matcher, | 1125 method_decl_matcher, function_template_decl_matcher, |
1041 method_template_decl_matcher, enum_member_decl_matcher))))); | 1126 method_template_decl_matcher, enum_member_decl_matcher))))); |
1042 UsingDeclRewriter using_decl_rewriter(&replacements); | 1127 UsingDeclRewriter using_decl_rewriter(&replacements); |
1043 match_finder.addMatcher(using_decl_matcher, &using_decl_rewriter); | 1128 match_finder.addMatcher(using_decl_matcher, &using_decl_rewriter); |
1044 | 1129 |
| 1130 // Matches any QualType that refers to a blink type: |
| 1131 // - const blink::Foo& |
| 1132 // - blink::Foo* |
| 1133 // - blink::Foo<T> |
| 1134 // - ... |
| 1135 // TODO(lukasza): The matchers below can be simplified after |
| 1136 // https://llvm.org/bugs/show_bug.cgi?id=30331 is fixed. |
| 1137 // Simplified matchers: |
| 1138 // auto blink_qual_type_base_matcher = |
| 1139 // qualType(hasDeclaration(in_blink_namespace)); |
| 1140 // auto blink_qual_type_matcher = qualType(anyOf( |
| 1141 // blink_qual_type_base_matcher, |
| 1142 // pointsTo(blink_qual_type_base_matcher), |
| 1143 // references(blink_qual_type_base_matcher))); |
| 1144 auto blink_qual_type_bug_workaround_matcher1 = hasBaseType( |
| 1145 anyOf(enumType(hasDeclaration(in_blink_namespace)), |
| 1146 recordType(hasDeclaration(in_blink_namespace)), |
| 1147 templateSpecializationType(hasDeclaration(in_blink_namespace)), |
| 1148 templateTypeParmType(hasDeclaration(in_blink_namespace)), |
| 1149 typedefType(hasDeclaration(in_blink_namespace)))); |
| 1150 auto blink_qual_type_base_matcher = |
| 1151 qualType(anyOf(blink_qual_type_bug_workaround_matcher1, |
| 1152 hasBaseType(elaboratedType( |
| 1153 namesType(blink_qual_type_bug_workaround_matcher1))))); |
| 1154 auto blink_qual_type_matcher = |
| 1155 qualType(anyOf(blink_qual_type_base_matcher, pointsTo(in_blink_namespace), |
| 1156 references(in_blink_namespace))); |
| 1157 |
| 1158 // Template-dependent decl lookup ======== |
| 1159 // Given |
| 1160 // template <typename T> void f() { T::foo(); } |
| 1161 // matches |T::foo|. |
| 1162 auto dependent_scope_decl_ref_expr_matcher = |
| 1163 expr(id("expr", dependentScopeDeclRefExpr(has(nestedNameSpecifier( |
| 1164 specifiesType(blink_qual_type_matcher)))))); |
| 1165 DependentScopeDeclRefExprRewriter dependent_scope_decl_ref_expr_rewriter( |
| 1166 &replacements); |
| 1167 match_finder.addMatcher(dependent_scope_decl_ref_expr_matcher, |
| 1168 &dependent_scope_decl_ref_expr_rewriter); |
| 1169 |
| 1170 // Template-dependent member lookup ======== |
| 1171 // Given |
| 1172 // template <typename T> |
| 1173 // class Foo { |
| 1174 // void f() { T::foo(); } |
| 1175 // void g(T x) { x.bar(); } |
| 1176 // }; |
| 1177 // matches |T::foo| and |x.bar|. |
| 1178 auto cxx_dependent_scope_member_expr_matcher = expr( |
| 1179 id("expr", |
| 1180 cxxDependentScopeMemberExpr(allOf( |
| 1181 hasBaseExprMissingOrMatching(hasType(blink_qual_type_matcher)), |
| 1182 hasQualifierMissingOrMatching( |
| 1183 specifiesType(blink_qual_type_matcher)))))); |
| 1184 CXXDependentScopeMemberExprRewriter cxx_dependent_scope_member_expr_rewriter( |
| 1185 &replacements); |
| 1186 match_finder.addMatcher(cxx_dependent_scope_member_expr_matcher, |
| 1187 &cxx_dependent_scope_member_expr_rewriter); |
| 1188 |
1045 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = | 1189 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = |
1046 clang::tooling::newFrontendActionFactory(&match_finder); | 1190 clang::tooling::newFrontendActionFactory(&match_finder); |
1047 int result = tool.run(factory.get()); | 1191 int result = tool.run(factory.get()); |
1048 if (result != 0) | 1192 if (result != 0) |
1049 return result; | 1193 return result; |
1050 | 1194 |
1051 #if defined(_WIN32) | 1195 #if defined(_WIN32) |
1052 HANDLE lockfd = CreateFile("rewrite-sym.lock", GENERIC_READ, FILE_SHARE_READ, | 1196 HANDLE lockfd = CreateFile("rewrite-sym.lock", GENERIC_READ, FILE_SHARE_READ, |
1053 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | 1197 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
1054 OVERLAPPED overlapped = {}; | 1198 OVERLAPPED overlapped = {}; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1086 for (const auto& r : replacements) { | 1230 for (const auto& r : replacements) { |
1087 std::string replacement_text = r.getReplacementText().str(); | 1231 std::string replacement_text = r.getReplacementText().str(); |
1088 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 1232 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
1089 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 1233 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
1090 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 1234 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
1091 } | 1235 } |
1092 llvm::outs() << "==== END EDITS ====\n"; | 1236 llvm::outs() << "==== END EDITS ====\n"; |
1093 | 1237 |
1094 return 0; | 1238 return 0; |
1095 } | 1239 } |
OLD | NEW |