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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 // getAnyMember() will return a FieldDecl which we can match against. | 80 // getAnyMember() will return a FieldDecl which we can match against. |
| 81 AST_MATCHER_P(clang::CXXCtorInitializer, | 81 AST_MATCHER_P(clang::CXXCtorInitializer, |
| 82 forAnyField, | 82 forAnyField, |
| 83 clang::ast_matchers::internal::Matcher<clang::FieldDecl>, | 83 clang::ast_matchers::internal::Matcher<clang::FieldDecl>, |
| 84 InnerMatcher) { | 84 InnerMatcher) { |
| 85 const clang::FieldDecl* NodeAsDecl = Node.getAnyMember(); | 85 const clang::FieldDecl* NodeAsDecl = Node.getAnyMember(); |
| 86 return (NodeAsDecl != nullptr && | 86 return (NodeAsDecl != nullptr && |
| 87 InnerMatcher.matches(*NodeAsDecl, Finder, Builder)); | 87 InnerMatcher.matches(*NodeAsDecl, Finder, Builder)); |
| 88 } | 88 } |
| 89 | 89 |
| 90 bool IsBlacklistedMethod(const clang::CXXMethodDecl& decl) { | |
| 91 if (decl.isStatic()) | |
| 92 return false; | |
| 93 | |
| 94 clang::StringRef name = decl.getName(); | |
| 95 | |
| 96 // These methods should never be renamed. | |
| 97 static const char* kBlacklistMethods[] = {"trace", "lock", "unlock", | |
| 98 "try_lock"}; | |
| 99 for (const auto& b : kBlacklistMethods) { | |
| 100 if (name == b) | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 // Iterator methods shouldn't be renamed to work with stl and range-for | |
| 105 // loops. | |
| 106 std::string ret_type = decl.getReturnType().getAsString(); | |
| 107 if (ret_type.find("iterator") == std::string::npos && | |
| 108 ret_type.find("Iterator") == std::string::npos) | |
| 109 return false; | |
| 110 static const char* kIteratorBlacklist[] = {"begin", "end", "rbegin", "rend"}; | |
| 111 for (const auto& b : kIteratorBlacklist) { | |
| 112 if (name == b) | |
| 113 return true; | |
| 114 } | |
| 115 | |
| 116 return false; | |
| 117 } | |
| 118 | |
| 90 bool IsDeclContextInBlinkOrWTF(const clang::DeclContext* decl_context, | 119 bool IsDeclContextInBlinkOrWTF(const clang::DeclContext* decl_context, |
| 91 const clang::SourceLocation& location, | 120 const clang::SourceLocation& location, |
| 92 clang::SourceManager& source_manager, | 121 clang::SourceManager& source_manager, |
| 93 bool blink, | 122 bool blink, |
| 94 bool wtf) { | 123 bool wtf) { |
| 95 assert(blink || wtf); // Else, what's the point? | 124 assert(blink || wtf); // Else, what's the point? |
| 96 | 125 |
| 97 // Generated stuff is not considered in blink for renaming since the tool will | 126 // Generated stuff is not considered in blink for renaming since the tool will |
| 98 // not rename them. | 127 // not rename them. |
| 99 auto expansion_loc = source_manager.getExpansionLoc(location); | 128 auto expansion_loc = source_manager.getExpansionLoc(location); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 124 return false; | 153 return false; |
| 125 } | 154 } |
| 126 | 155 |
| 127 // A method is from Blink if it is from the Blink namespace or overrides a | 156 // A method is from Blink if it is from the Blink namespace or overrides a |
| 128 // method from the Blink namespace. | 157 // method from the Blink namespace. |
| 129 bool IsBlinkOrWTFMethod(const clang::CXXMethodDecl& decl, | 158 bool IsBlinkOrWTFMethod(const clang::CXXMethodDecl& decl, |
| 130 clang::SourceManager& source_manager) { | 159 clang::SourceManager& source_manager) { |
| 131 bool overrides_blink = false; | 160 bool overrides_blink = false; |
| 132 bool overrides_non_blink = false; | 161 bool overrides_non_blink = false; |
| 133 | 162 |
| 163 // Blacklisted methods are not considered to be in blink so that they will | |
| 164 // never be rewritten. | |
| 165 if (IsBlacklistedMethod(decl)) | |
| 166 return false; | |
| 167 | |
| 134 for (auto it = decl.begin_overridden_methods(); | 168 for (auto it = decl.begin_overridden_methods(); |
| 135 it != decl.end_overridden_methods(); ++it) { | 169 it != decl.end_overridden_methods(); ++it) { |
| 136 if (IsBlinkOrWTFMethod(**it, source_manager)) | 170 if (IsBlinkOrWTFMethod(**it, source_manager)) |
| 137 overrides_blink = true; | 171 overrides_blink = true; |
| 138 else | 172 else |
| 139 overrides_non_blink = true; | 173 overrides_non_blink = true; |
| 140 } | 174 } |
| 141 | 175 |
| 142 // If this fires we have a class overriding a method from a class in blink, | 176 // If this fires we have a class overriding a method from a class in blink, |
| 143 // and also overriding a method of the same name from a class not in blink, | 177 // and also overriding a method of the same name from a class not in blink, |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 284 name += original_name; | 318 name += original_name; |
| 285 name[1] = clang::toUppercase(name[1]); | 319 name[1] = clang::toUppercase(name[1]); |
| 286 return true; | 320 return true; |
| 287 } | 321 } |
| 288 | 322 |
| 289 bool GetNameForDecl(const clang::CXXMethodDecl& decl, | 323 bool GetNameForDecl(const clang::CXXMethodDecl& decl, |
| 290 const clang::ASTContext& context, | 324 const clang::ASTContext& context, |
| 291 clang::SourceManager& source_manager, | 325 clang::SourceManager& source_manager, |
| 292 std::string& name) { | 326 std::string& name) { |
| 293 StringRef original_name = decl.getName(); | 327 StringRef original_name = decl.getName(); |
| 294 | 328 name = original_name.str(); |
|
dcheng
2016/03/10 23:57:42
Nit: get rid of |original_name| local here.
danakj
2016/03/11 01:49:33
Done.
| |
| 295 if (!decl.isStatic()) { | |
| 296 std::string ret_type = decl.getReturnType().getAsString(); | |
| 297 if (ret_type.find("iterator") != std::string::npos || | |
| 298 ret_type.find("Iterator") != std::string::npos) { | |
| 299 // Iterator methods shouldn't be renamed to work with stl and range-for | |
| 300 // loops. | |
| 301 static const char* kIteratorBlacklist[] = {"begin", "end", "rbegin", | |
| 302 "rend"}; | |
| 303 for (const auto& b : kIteratorBlacklist) { | |
| 304 if (original_name == b) | |
| 305 return false; | |
| 306 } | |
| 307 } | |
| 308 | |
| 309 // Some methods shouldn't be renamed because reasons. | |
| 310 static const char* kBlacklist[] = {"trace", "lock", "unlock", "try_lock"}; | |
| 311 for (const auto& b : kBlacklist) { | |
| 312 if (original_name == b) | |
| 313 return false; | |
| 314 } | |
| 315 } | |
| 316 | |
| 317 name = decl.getName().str(); | |
| 318 name[0] = clang::toUppercase(name[0]); | 329 name[0] = clang::toUppercase(name[0]); |
| 319 return true; | 330 return true; |
| 320 } | 331 } |
| 321 | 332 |
| 322 bool GetNameForDecl(const clang::FieldDecl& decl, | 333 bool GetNameForDecl(const clang::FieldDecl& decl, |
| 323 const clang::ASTContext& context, | 334 const clang::ASTContext& context, |
| 324 clang::SourceManager& source_manager, | 335 clang::SourceManager& source_manager, |
| 325 std::string& name) { | 336 std::string& name) { |
| 326 StringRef original_name = decl.getName(); | 337 StringRef original_name = decl.getName(); |
| 327 bool member_prefix = original_name.startswith(kBlinkFieldPrefix); | 338 bool member_prefix = original_name.startswith(kBlinkFieldPrefix); |
| (...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 791 for (const auto& r : replacements) { | 802 for (const auto& r : replacements) { |
| 792 std::string replacement_text = r.getReplacementText().str(); | 803 std::string replacement_text = r.getReplacementText().str(); |
| 793 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 804 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 794 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 805 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 795 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 806 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 796 } | 807 } |
| 797 llvm::outs() << "==== END EDITS ====\n"; | 808 llvm::outs() << "==== END EDITS ====\n"; |
| 798 | 809 |
| 799 return 0; | 810 return 0; |
| 800 } | 811 } |
| OLD | NEW |