| 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 return Node.isOverloadedOperator(); | 67 return Node.isOverloadedOperator(); |
| 68 } | 68 } |
| 69 | 69 |
| 70 AST_MATCHER_P(clang::FunctionTemplateDecl, | 70 AST_MATCHER_P(clang::FunctionTemplateDecl, |
| 71 templatedDecl, | 71 templatedDecl, |
| 72 clang::ast_matchers::internal::Matcher<clang::FunctionDecl>, | 72 clang::ast_matchers::internal::Matcher<clang::FunctionDecl>, |
| 73 InnerMatcher) { | 73 InnerMatcher) { |
| 74 return InnerMatcher.matches(*Node.getTemplatedDecl(), Finder, Builder); | 74 return InnerMatcher.matches(*Node.getTemplatedDecl(), Finder, Builder); |
| 75 } | 75 } |
| 76 | 76 |
| 77 bool IsDeclContextInBlinkOrWTF(const clang::DeclContext* decl_context, |
| 78 bool blink, |
| 79 bool wtf) { |
| 80 assert(blink || wtf); // Else, what's the point? |
| 81 auto* namespace_decl = clang::dyn_cast_or_null<clang::NamespaceDecl>( |
| 82 decl_context->getEnclosingNamespaceContext()); |
| 83 return namespace_decl && namespace_decl->getParent()->isTranslationUnit() && |
| 84 ((blink && namespace_decl->getName() == "blink") || |
| 85 (wtf && namespace_decl->getName() == "WTF")); |
| 86 } |
| 87 |
| 77 // A method is from Blink if it is from the Blink namespace or overrides a | 88 // A method is from Blink if it is from the Blink namespace or overrides a |
| 78 // method from the Blink namespace. | 89 // method from the Blink namespace. |
| 79 bool IsBlinkMethod(const clang::CXXMethodDecl& decl) { | 90 bool IsBlinkOrWTFMethod(const clang::CXXMethodDecl& decl) { |
| 80 auto* namespace_decl = clang::dyn_cast_or_null<clang::NamespaceDecl>( | 91 if (IsDeclContextInBlinkOrWTF(decl.getDeclContext(), true, true)) |
| 81 decl.getParent()->getEnclosingNamespaceContext()); | |
| 82 if (namespace_decl && namespace_decl->getParent()->isTranslationUnit() && | |
| 83 (namespace_decl->getName() == "blink" || | |
| 84 namespace_decl->getName() == "WTF")) | |
| 85 return true; | 92 return true; |
| 86 | 93 |
| 87 for (auto it = decl.begin_overridden_methods(); | 94 for (auto it = decl.begin_overridden_methods(); |
| 88 it != decl.end_overridden_methods(); ++it) { | 95 it != decl.end_overridden_methods(); ++it) { |
| 89 if (IsBlinkMethod(**it)) | 96 if (IsBlinkOrWTFMethod(**it)) |
| 90 return true; | 97 return true; |
| 91 } | 98 } |
| 92 return false; | 99 return false; |
| 93 } | 100 } |
| 94 | 101 |
| 95 AST_MATCHER(clang::CXXMethodDecl, isBlinkMethod) { | 102 AST_MATCHER(clang::CXXMethodDecl, isBlinkOrWTFMethod) { |
| 96 return IsBlinkMethod(Node); | 103 return IsBlinkOrWTFMethod(Node); |
| 97 } | 104 } |
| 98 | 105 |
| 99 // Helper to convert from a camelCaseName to camel_case_name. It uses some | 106 // Helper to convert from a camelCaseName to camel_case_name. It uses some |
| 100 // heuristics to try to handle acronyms in camel case names correctly. | 107 // heuristics to try to handle acronyms in camel case names correctly. |
| 101 std::string CamelCaseToUnderscoreCase(StringRef input, | 108 std::string CamelCaseToUnderscoreCase(StringRef input, |
| 102 bool numbers_are_separated) { | 109 bool numbers_are_separated) { |
| 103 std::string output; | 110 std::string output; |
| 104 bool needs_underscore = false; | 111 bool needs_underscore = false; |
| 105 bool was_lowercase = false; | 112 bool was_lowercase = false; |
| 106 bool was_number = false; | 113 bool was_number = false; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 c = clang::toUppercase(c); | 220 c = clang::toUppercase(c); |
| 214 return true; | 221 return true; |
| 215 } | 222 } |
| 216 | 223 |
| 217 bool GetNameForDecl(const clang::CXXMethodDecl& decl, | 224 bool GetNameForDecl(const clang::CXXMethodDecl& decl, |
| 218 const clang::ASTContext& context, | 225 const clang::ASTContext& context, |
| 219 std::string& name) { | 226 std::string& name) { |
| 220 StringRef original_name = decl.getName(); | 227 StringRef original_name = decl.getName(); |
| 221 | 228 |
| 222 if (!decl.isStatic()) { | 229 if (!decl.isStatic()) { |
| 230 std::string ret_type = decl.getReturnType().getAsString(); |
| 231 if (ret_type.find("iterator") != std::string::npos || |
| 232 ret_type.find("Iterator") != std::string::npos) { |
| 233 // Iterator methods shouldn't be renamed to work with stl and range-for |
| 234 // loops. |
| 235 static const char* kIteratorBlacklist[] = {"begin", "end", "rbegin", |
| 236 "rend"}; |
| 237 for (const auto& b : kIteratorBlacklist) { |
| 238 if (original_name == b) |
| 239 return false; |
| 240 } |
| 241 } |
| 242 |
| 223 // Some methods shouldn't be renamed because reasons. | 243 // Some methods shouldn't be renamed because reasons. |
| 224 static const char* kBlacklist[] = {"begin", "end", "rbegin", "rend", | 244 static const char* kBlacklist[] = {"trace", "lock", "unlock", "try_lock"}; |
| 225 "trace", "lock", "unlock", "try_lock"}; | |
| 226 for (const auto& b : kBlacklist) { | 245 for (const auto& b : kBlacklist) { |
| 227 if (original_name == b) | 246 if (original_name == b) |
| 228 return false; | 247 return false; |
| 229 } | 248 } |
| 230 } | 249 } |
| 231 | 250 |
| 232 name = decl.getName().str(); | 251 name = decl.getName().str(); |
| 233 name[0] = clang::toUppercase(name[0]); | 252 name[0] = clang::toUppercase(name[0]); |
| 234 return true; | 253 return true; |
| 235 } | 254 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 original_name = original_name.substr(strlen(kBlinkStaticMemberPrefix)); | 288 original_name = original_name.substr(strlen(kBlinkStaticMemberPrefix)); |
| 270 else if (original_name.startswith(kBlinkFieldPrefix)) | 289 else if (original_name.startswith(kBlinkFieldPrefix)) |
| 271 original_name = original_name.substr(strlen(kBlinkFieldPrefix)); | 290 original_name = original_name.substr(strlen(kBlinkFieldPrefix)); |
| 272 | 291 |
| 273 bool is_const = IsProbablyConst(decl, context); | 292 bool is_const = IsProbablyConst(decl, context); |
| 274 if (is_const) { | 293 if (is_const) { |
| 275 // Don't try to rename constants that already conform to Chrome style. | 294 // Don't try to rename constants that already conform to Chrome style. |
| 276 if (original_name.size() >= 2 && original_name[0] == 'k' && | 295 if (original_name.size() >= 2 && original_name[0] == 'k' && |
| 277 clang::isUppercase(original_name[1])) | 296 clang::isUppercase(original_name[1])) |
| 278 return false; | 297 return false; |
| 298 |
| 299 // Struct consts in WTF do not become kFoo cuz stuff like type traits |
| 300 // should stay as lowercase. |
| 301 const clang::DeclContext* decl_context = decl.getDeclContext(); |
| 302 bool is_in_wtf = IsDeclContextInBlinkOrWTF(decl_context, false, true); |
| 303 const clang::CXXRecordDecl* parent = |
| 304 clang::dyn_cast_or_null<clang::CXXRecordDecl>(decl_context); |
| 305 if (is_in_wtf && parent && parent->isStruct()) |
| 306 return false; |
| 307 |
| 279 name = 'k'; | 308 name = 'k'; |
| 280 name.append(original_name.data(), original_name.size()); | 309 name.append(original_name.data(), original_name.size()); |
| 281 name[1] = clang::toUppercase(name[1]); | 310 name[1] = clang::toUppercase(name[1]); |
| 282 } else { | 311 } else { |
| 283 name = CamelCaseToUnderscoreCase(original_name, false); | 312 name = CamelCaseToUnderscoreCase(original_name, false); |
| 284 } | 313 } |
| 285 | 314 |
| 286 // Static members end with _ just like other members, but constants should | 315 // Static members end with _ just like other members, but constants should |
| 287 // not. | 316 // not. |
| 288 if (!is_const && decl.isStaticDataMember()) { | 317 if (!is_const && decl.isStaticDataMember()) { |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 match_finder.addMatcher(function_ref_matcher, &function_ref_rewriter); | 575 match_finder.addMatcher(function_ref_matcher, &function_ref_rewriter); |
| 547 | 576 |
| 548 // Method declarations ======== | 577 // Method declarations ======== |
| 549 // Given | 578 // Given |
| 550 // struct S { | 579 // struct S { |
| 551 // void g(); | 580 // void g(); |
| 552 // }; | 581 // }; |
| 553 // matches |g|. | 582 // matches |g|. |
| 554 auto method_decl_matcher = | 583 auto method_decl_matcher = |
| 555 id("decl", | 584 id("decl", |
| 556 cxxMethodDecl(isBlinkMethod(), | 585 cxxMethodDecl(isBlinkOrWTFMethod(), |
| 557 unless(anyOf(is_generated, | 586 unless(anyOf(is_generated, |
| 558 // Overloaded operators have special names | 587 // Overloaded operators have special names |
| 559 // and should never be renamed. | 588 // and should never be renamed. |
| 560 isOverloadedOperator(), | 589 isOverloadedOperator(), |
| 561 // Similarly, constructors, destructors, and | 590 // Similarly, constructors, destructors, and |
| 562 // conversion functions should not be | 591 // conversion functions should not be |
| 563 // considered for renaming. | 592 // considered for renaming. |
| 564 cxxConstructorDecl(), cxxDestructorDecl(), | 593 cxxConstructorDecl(), cxxDestructorDecl(), |
| 565 cxxConversionDecl())))); | 594 cxxConversionDecl())))); |
| 566 MethodDeclRewriter method_decl_rewriter(&replacements); | 595 MethodDeclRewriter method_decl_rewriter(&replacements); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 668 for (const auto& r : replacements) { | 697 for (const auto& r : replacements) { |
| 669 std::string replacement_text = r.getReplacementText().str(); | 698 std::string replacement_text = r.getReplacementText().str(); |
| 670 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 699 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 671 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 700 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 672 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 701 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 673 } | 702 } |
| 674 llvm::outs() << "==== END EDITS ====\n"; | 703 llvm::outs() << "==== END EDITS ====\n"; |
| 675 | 704 |
| 676 return 0; | 705 return 0; |
| 677 } | 706 } |
| OLD | NEW |