| 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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 return inner_matcher.matches(decl, finder, builder); | 141 return inner_matcher.matches(decl, finder, builder); |
| 142 } | 142 } |
| 143 | 143 |
| 144 AST_MATCHER_P(clang::CXXMethodDecl, | 144 AST_MATCHER_P(clang::CXXMethodDecl, |
| 145 includeAllOverriddenMethods, | 145 includeAllOverriddenMethods, |
| 146 clang::ast_matchers::internal::Matcher<clang::CXXMethodDecl>, | 146 clang::ast_matchers::internal::Matcher<clang::CXXMethodDecl>, |
| 147 InnerMatcher) { | 147 InnerMatcher) { |
| 148 return MatchAllOverriddenMethods(Node, InnerMatcher, Finder, Builder); | 148 return MatchAllOverriddenMethods(Node, InnerMatcher, Finder, Builder); |
| 149 } | 149 } |
| 150 | 150 |
| 151 bool IsBlacklistedMethod(const clang::CXXMethodDecl& decl) { |
| 152 if (decl.isStatic()) |
| 153 return false; |
| 154 |
| 155 clang::StringRef name = decl.getName(); |
| 156 |
| 157 // These methods should never be renamed. |
| 158 static const char* kBlacklistMethods[] = {"trace", "lock", "unlock", |
| 159 "try_lock"}; |
| 160 for (const auto& b : kBlacklistMethods) { |
| 161 if (name == b) |
| 162 return true; |
| 163 } |
| 164 |
| 165 // Iterator methods shouldn't be renamed to work with stl and range-for |
| 166 // loops. |
| 167 std::string ret_type = decl.getReturnType().getAsString(); |
| 168 if (ret_type.find("iterator") == std::string::npos && |
| 169 ret_type.find("Iterator") == std::string::npos) |
| 170 return false; |
| 171 static const char* kIteratorBlacklist[] = {"begin", "end", "rbegin", "rend"}; |
| 172 for (const auto& b : kIteratorBlacklist) { |
| 173 if (name == b) |
| 174 return true; |
| 175 } |
| 176 |
| 177 return false; |
| 178 } |
| 179 |
| 180 AST_MATCHER(clang::CXXMethodDecl, isBlacklistedMethod) { |
| 181 return IsBlacklistedMethod(Node); |
| 182 } |
| 183 |
| 151 // Helper to convert from a camelCaseName to camel_case_name. It uses some | 184 // Helper to convert from a camelCaseName to camel_case_name. It uses some |
| 152 // heuristics to try to handle acronyms in camel case names correctly. | 185 // heuristics to try to handle acronyms in camel case names correctly. |
| 153 std::string CamelCaseToUnderscoreCase(StringRef input) { | 186 std::string CamelCaseToUnderscoreCase(StringRef input) { |
| 154 std::string output; | 187 std::string output; |
| 155 bool needs_underscore = false; | 188 bool needs_underscore = false; |
| 156 bool was_lowercase = false; | 189 bool was_lowercase = false; |
| 157 bool was_number = false; | 190 bool was_number = false; |
| 158 bool was_uppercase = false; | 191 bool was_uppercase = false; |
| 159 bool first_char = true; | 192 bool first_char = true; |
| 160 // Iterate in reverse to minimize the amount of backtracking. | 193 // Iterate in reverse to minimize the amount of backtracking. |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 | 295 |
| 263 name = 'k'; // k prefix on enum values. | 296 name = 'k'; // k prefix on enum values. |
| 264 name += original_name; | 297 name += original_name; |
| 265 name[1] = clang::toUppercase(name[1]); | 298 name[1] = clang::toUppercase(name[1]); |
| 266 return true; | 299 return true; |
| 267 } | 300 } |
| 268 | 301 |
| 269 bool GetNameForDecl(const clang::CXXMethodDecl& decl, | 302 bool GetNameForDecl(const clang::CXXMethodDecl& decl, |
| 270 const clang::ASTContext& context, | 303 const clang::ASTContext& context, |
| 271 std::string& name) { | 304 std::string& name) { |
| 272 StringRef original_name = decl.getName(); | |
| 273 | |
| 274 if (!decl.isStatic()) { | |
| 275 std::string ret_type = decl.getReturnType().getAsString(); | |
| 276 if (ret_type.find("iterator") != std::string::npos || | |
| 277 ret_type.find("Iterator") != std::string::npos) { | |
| 278 // Iterator methods shouldn't be renamed to work with stl and range-for | |
| 279 // loops. | |
| 280 static const char* kIteratorBlacklist[] = {"begin", "end", "rbegin", | |
| 281 "rend"}; | |
| 282 for (const auto& b : kIteratorBlacklist) { | |
| 283 if (original_name == b) | |
| 284 return false; | |
| 285 } | |
| 286 } | |
| 287 | |
| 288 // Some methods shouldn't be renamed because reasons. | |
| 289 static const char* kBlacklist[] = {"trace", "lock", "unlock", "try_lock"}; | |
| 290 for (const auto& b : kBlacklist) { | |
| 291 if (original_name == b) | |
| 292 return false; | |
| 293 } | |
| 294 } | |
| 295 | |
| 296 name = decl.getName().str(); | 305 name = decl.getName().str(); |
| 297 name[0] = clang::toUppercase(name[0]); | 306 name[0] = clang::toUppercase(name[0]); |
| 298 return true; | 307 return true; |
| 299 } | 308 } |
| 300 | 309 |
| 301 bool GetNameForDecl(const clang::FieldDecl& decl, | 310 bool GetNameForDecl(const clang::FieldDecl& decl, |
| 302 const clang::ASTContext& context, | 311 const clang::ASTContext& context, |
| 303 std::string& name) { | 312 std::string& name) { |
| 304 StringRef original_name = decl.getName(); | 313 StringRef original_name = decl.getName(); |
| 305 bool member_prefix = original_name.startswith(kBlinkFieldPrefix); | 314 bool member_prefix = original_name.startswith(kBlinkFieldPrefix); |
| (...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 639 id("expr", declRefExpr(to(function_decl_matcher))); | 648 id("expr", declRefExpr(to(function_decl_matcher))); |
| 640 FunctionRefRewriter function_ref_rewriter(&replacements); | 649 FunctionRefRewriter function_ref_rewriter(&replacements); |
| 641 match_finder.addMatcher(function_ref_matcher, &function_ref_rewriter); | 650 match_finder.addMatcher(function_ref_matcher, &function_ref_rewriter); |
| 642 | 651 |
| 643 // Method declarations ======== | 652 // Method declarations ======== |
| 644 // Given | 653 // Given |
| 645 // struct S { | 654 // struct S { |
| 646 // void g(); | 655 // void g(); |
| 647 // }; | 656 // }; |
| 648 // matches |g|. | 657 // matches |g|. |
| 658 // For a method to be considered for rewrite, it must not override something |
| 659 // that we're not rewriting. Any methods that we would not normally consider |
| 660 // but that override something we are rewriting should also be rewritten. So |
| 661 // we use includeAllOverriddenMethods() to check these rules not just for the |
| 662 // method being matched but for the methods it overrides also. |
| 663 auto is_blink_method = includeAllOverriddenMethods( |
| 664 allOf(in_blink_namespace, unless(isBlacklistedMethod()))); |
| 649 auto method_decl_matcher = id( | 665 auto method_decl_matcher = id( |
| 650 "decl", | 666 "decl", |
| 651 cxxMethodDecl( | 667 cxxMethodDecl( |
| 652 unless(anyOf( // Overloaded operators have special names | 668 unless(anyOf( |
| 653 // and should never be renamed. | 669 // Overloaded operators have special names and should never be |
| 670 // renamed. |
| 654 isOverloadedOperator(), | 671 isOverloadedOperator(), |
| 655 // Similarly, constructors, destructors, and | 672 // Similarly, constructors, destructors, and conversion functions |
| 656 // conversion functions should not be | 673 // should not be considered for renaming. |
| 657 // considered for renaming. | |
| 658 cxxConstructorDecl(), cxxDestructorDecl(), cxxConversionDecl())), | 674 cxxConstructorDecl(), cxxDestructorDecl(), cxxConversionDecl())), |
| 659 // Check this last after excluding things, to avoid | 675 // Check this last after excluding things, to avoid |
| 660 // asserts about overriding non-blink and blink for the | 676 // asserts about overriding non-blink and blink for the |
| 661 // same method. | 677 // same method. |
| 662 includeAllOverriddenMethods(in_blink_namespace))); | 678 is_blink_method)); |
| 663 MethodDeclRewriter method_decl_rewriter(&replacements); | 679 MethodDeclRewriter method_decl_rewriter(&replacements); |
| 664 match_finder.addMatcher(method_decl_matcher, &method_decl_rewriter); | 680 match_finder.addMatcher(method_decl_matcher, &method_decl_rewriter); |
| 665 | 681 |
| 666 // Method references in a non-member context ======== | 682 // Method references in a non-member context ======== |
| 667 // Given | 683 // Given |
| 668 // S s; | 684 // S s; |
| 669 // s.g(); | 685 // s.g(); |
| 670 // void (S::*p)() = &S::g; | 686 // void (S::*p)() = &S::g; |
| 671 // matches |&S::g| but not |s.g()|. | 687 // matches |&S::g| but not |s.g()|. |
| 672 auto method_ref_matcher = id("expr", declRefExpr(to(method_decl_matcher))); | 688 auto method_ref_matcher = id("expr", declRefExpr(to(method_decl_matcher))); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 764 for (const auto& r : replacements) { | 780 for (const auto& r : replacements) { |
| 765 std::string replacement_text = r.getReplacementText().str(); | 781 std::string replacement_text = r.getReplacementText().str(); |
| 766 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 782 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 767 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 783 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 768 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 784 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 769 } | 785 } |
| 770 llvm::outs() << "==== END EDITS ====\n"; | 786 llvm::outs() << "==== END EDITS ====\n"; |
| 771 | 787 |
| 772 return 0; | 788 return 0; |
| 773 } | 789 } |
| OLD | NEW |