Chromium Code Reviews| 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 IsDeclInBlinkOrWtf(const clang::DeclContext* decl_context, | |
| 78 bool blink, | |
|
dcheng
2016/03/01 23:58:02
The double bools here are kind of hard to read, bu
| |
| 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, |
|
dcheng
2016/03/01 23:58:02
Wtf or WTF consistently, I think.
danakj
2016/03/02 00:32:12
Done.
| |
| 91 bool blink, | |
| 92 bool wtf) { | |
|
dcheng
2016/03/01 23:58:02
It looks like blink and wtf are both always true?
danakj
2016/03/02 00:32:12
Yeh.. I was doing something I shouldn't have with
| |
| 93 assert(blink || wtf); // Else, what's the point? | |
| 80 auto* namespace_decl = clang::dyn_cast_or_null<clang::NamespaceDecl>( | 94 auto* namespace_decl = clang::dyn_cast_or_null<clang::NamespaceDecl>( |
|
dcheng
2016/03/01 23:58:02
Maybe this can just call out to IsDeclInBlinkOrWtf
danakj
2016/03/02 00:32:12
Done.
| |
| 81 decl.getParent()->getEnclosingNamespaceContext()); | 95 decl.getParent()->getEnclosingNamespaceContext()); |
| 82 if (namespace_decl && namespace_decl->getParent()->isTranslationUnit() && | 96 if (namespace_decl && namespace_decl->getParent()->isTranslationUnit() && |
| 83 (namespace_decl->getName() == "blink" || | 97 ((blink && namespace_decl->getName() == "blink") || |
| 84 namespace_decl->getName() == "WTF")) | 98 (wtf && namespace_decl->getName() == "WTF"))) |
| 85 return true; | 99 return true; |
| 86 | 100 |
| 87 for (auto it = decl.begin_overridden_methods(); | 101 for (auto it = decl.begin_overridden_methods(); |
| 88 it != decl.end_overridden_methods(); ++it) { | 102 it != decl.end_overridden_methods(); ++it) { |
| 89 if (IsBlinkMethod(**it)) | 103 if (IsBlinkOrWtfMethod(**it, blink, wtf)) |
| 90 return true; | 104 return true; |
| 91 } | 105 } |
| 92 return false; | 106 return false; |
| 93 } | 107 } |
| 94 | 108 |
| 95 AST_MATCHER(clang::CXXMethodDecl, isBlinkMethod) { | 109 AST_MATCHER(clang::CXXMethodDecl, isBlinkOrWTFMethod) { |
| 96 return IsBlinkMethod(Node); | 110 return IsBlinkOrWtfMethod(Node, true, true); |
| 97 } | 111 } |
| 98 | 112 |
| 99 // Helper to convert from a camelCaseName to camel_case_name. It uses some | 113 // 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. | 114 // heuristics to try to handle acronyms in camel case names correctly. |
| 101 std::string CamelCaseToUnderscoreCase(StringRef input, | 115 std::string CamelCaseToUnderscoreCase(StringRef input, |
| 102 bool numbers_are_separated) { | 116 bool numbers_are_separated) { |
| 103 std::string output; | 117 std::string output; |
| 104 bool needs_underscore = false; | 118 bool needs_underscore = false; |
| 105 bool was_lowercase = false; | 119 bool was_lowercase = false; |
| 106 bool was_number = false; | 120 bool was_number = false; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 c = clang::toUppercase(c); | 227 c = clang::toUppercase(c); |
| 214 return true; | 228 return true; |
| 215 } | 229 } |
| 216 | 230 |
| 217 bool GetNameForDecl(const clang::CXXMethodDecl& decl, | 231 bool GetNameForDecl(const clang::CXXMethodDecl& decl, |
| 218 const clang::ASTContext& context, | 232 const clang::ASTContext& context, |
| 219 std::string& name) { | 233 std::string& name) { |
| 220 StringRef original_name = decl.getName(); | 234 StringRef original_name = decl.getName(); |
| 221 | 235 |
| 222 if (!decl.isStatic()) { | 236 if (!decl.isStatic()) { |
| 237 std::string ret_type = decl.getReturnType().getAsString(); | |
| 238 if (ret_type.find("iterator") != std::string::npos || | |
| 239 ret_type.find("Iterator") != std::string::npos) { | |
| 240 // Iterator methods shouldn't be renamed to work with stl and range-for | |
| 241 // loops. | |
| 242 static const char* kIteratorBlacklist[] = {"begin", "end", "rbegin", | |
| 243 "rend"}; | |
| 244 for (const auto& b : kIteratorBlacklist) { | |
| 245 if (original_name == b) | |
| 246 return false; | |
| 247 } | |
| 248 } | |
| 249 | |
| 223 // Some methods shouldn't be renamed because reasons. | 250 // Some methods shouldn't be renamed because reasons. |
| 224 static const char* kBlacklist[] = {"begin", "end", "rbegin", "rend", | 251 static const char* kBlacklist[] = {"trace", "lock", "unlock", "try_lock"}; |
| 225 "trace", "lock", "unlock", "try_lock"}; | |
| 226 for (const auto& b : kBlacklist) { | 252 for (const auto& b : kBlacklist) { |
| 227 if (original_name == b) | 253 if (original_name == b) |
| 228 return false; | 254 return false; |
| 229 } | 255 } |
| 230 } | 256 } |
| 231 | 257 |
| 232 name = decl.getName().str(); | 258 name = decl.getName().str(); |
| 233 name[0] = clang::toUppercase(name[0]); | 259 name[0] = clang::toUppercase(name[0]); |
| 234 return true; | 260 return true; |
| 235 } | 261 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 original_name = original_name.substr(strlen(kBlinkStaticMemberPrefix)); | 295 original_name = original_name.substr(strlen(kBlinkStaticMemberPrefix)); |
| 270 else if (original_name.startswith(kBlinkFieldPrefix)) | 296 else if (original_name.startswith(kBlinkFieldPrefix)) |
| 271 original_name = original_name.substr(strlen(kBlinkFieldPrefix)); | 297 original_name = original_name.substr(strlen(kBlinkFieldPrefix)); |
| 272 | 298 |
| 273 bool is_const = IsProbablyConst(decl, context); | 299 bool is_const = IsProbablyConst(decl, context); |
| 274 if (is_const) { | 300 if (is_const) { |
| 275 // Don't try to rename constants that already conform to Chrome style. | 301 // Don't try to rename constants that already conform to Chrome style. |
| 276 if (original_name.size() >= 2 && original_name[0] == 'k' && | 302 if (original_name.size() >= 2 && original_name[0] == 'k' && |
| 277 clang::isUppercase(original_name[1])) | 303 clang::isUppercase(original_name[1])) |
| 278 return false; | 304 return false; |
| 305 | |
| 306 // Struct consts in WTF do not become kFoo cuz stuff like type traits | |
| 307 // should stay as lowercase. | |
| 308 const clang::DeclContext* decl_context = decl.getDeclContext(); | |
| 309 bool is_in_wtf = IsDeclInBlinkOrWtf(decl_context, false, true); | |
| 310 const clang::CXXRecordDecl* parent = | |
| 311 clang::dyn_cast_or_null<clang::CXXRecordDecl>(decl.getDeclContext()); | |
|
dcheng
2016/03/01 23:58:02
s/decl.getDeclContext()/decl_context/
danakj
2016/03/02 00:32:12
Done.
| |
| 312 if (is_in_wtf && parent && parent->isStruct()) | |
| 313 return false; | |
| 314 | |
| 279 name = 'k'; | 315 name = 'k'; |
| 280 name.append(original_name.data(), original_name.size()); | 316 name.append(original_name.data(), original_name.size()); |
| 281 name[1] = clang::toUppercase(name[1]); | 317 name[1] = clang::toUppercase(name[1]); |
| 282 } else { | 318 } else { |
| 283 name = CamelCaseToUnderscoreCase(original_name, false); | 319 name = CamelCaseToUnderscoreCase(original_name, false); |
| 284 } | 320 } |
| 285 | 321 |
| 286 // Static members end with _ just like other members, but constants should | 322 // Static members end with _ just like other members, but constants should |
| 287 // not. | 323 // not. |
| 288 if (!is_const && decl.isStaticDataMember()) { | 324 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); | 582 match_finder.addMatcher(function_ref_matcher, &function_ref_rewriter); |
| 547 | 583 |
| 548 // Method declarations ======== | 584 // Method declarations ======== |
| 549 // Given | 585 // Given |
| 550 // struct S { | 586 // struct S { |
| 551 // void g(); | 587 // void g(); |
| 552 // }; | 588 // }; |
| 553 // matches |g|. | 589 // matches |g|. |
| 554 auto method_decl_matcher = | 590 auto method_decl_matcher = |
| 555 id("decl", | 591 id("decl", |
| 556 cxxMethodDecl(isBlinkMethod(), | 592 cxxMethodDecl(isBlinkOrWTFMethod(), |
| 557 unless(anyOf(is_generated, | 593 unless(anyOf(is_generated, |
| 558 // Overloaded operators have special names | 594 // Overloaded operators have special names |
| 559 // and should never be renamed. | 595 // and should never be renamed. |
| 560 isOverloadedOperator(), | 596 isOverloadedOperator(), |
| 561 // Similarly, constructors, destructors, and | 597 // Similarly, constructors, destructors, and |
| 562 // conversion functions should not be | 598 // conversion functions should not be |
| 563 // considered for renaming. | 599 // considered for renaming. |
| 564 cxxConstructorDecl(), cxxDestructorDecl(), | 600 cxxConstructorDecl(), cxxDestructorDecl(), |
| 565 cxxConversionDecl())))); | 601 cxxConversionDecl())))); |
| 566 MethodDeclRewriter method_decl_rewriter(&replacements); | 602 MethodDeclRewriter method_decl_rewriter(&replacements); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 668 for (const auto& r : replacements) { | 704 for (const auto& r : replacements) { |
| 669 std::string replacement_text = r.getReplacementText().str(); | 705 std::string replacement_text = r.getReplacementText().str(); |
| 670 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 706 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 671 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 707 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 672 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 708 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 673 } | 709 } |
| 674 llvm::outs() << "==== END EDITS ====\n"; | 710 llvm::outs() << "==== END EDITS ====\n"; |
| 675 | 711 |
| 676 return 0; | 712 return 0; |
| 677 } | 713 } |
| OLD | NEW |