Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: tools/clang/rewrite_to_chrome_style/RewriteToChromeStyle.cpp

Issue 2671713005: Avoid calling getName() on declarations that don't have an associated identifier. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 } 128 }
129 129
130 class MethodBlocklist { 130 class MethodBlocklist {
131 public: 131 public:
132 explicit MethodBlocklist(const std::string& filepath) { 132 explicit MethodBlocklist(const std::string& filepath) {
133 if (!filepath.empty()) 133 if (!filepath.empty())
134 ParseInputFile(filepath); 134 ParseInputFile(filepath);
135 } 135 }
136 136
137 bool Contains(const clang::FunctionDecl& method) const { 137 bool Contains(const clang::FunctionDecl& method) const {
138 if (!method.getDeclName().isIdentifier())
139 return false;
140
138 auto it = method_to_class_to_args_.find(method.getName()); 141 auto it = method_to_class_to_args_.find(method.getName());
139 if (it == method_to_class_to_args_.end()) 142 if (it == method_to_class_to_args_.end())
140 return false; 143 return false;
141 144
142 // |method_context| is either 145 // |method_context| is either
143 // 1) a CXXRecordDecl (i.e. blink::Document) or 146 // 1) a CXXRecordDecl (i.e. blink::Document) or
144 // 2) a NamespaceDecl (i.e. blink::DOMWindowTimers). 147 // 2) a NamespaceDecl (i.e. blink::DOMWindowTimers).
145 const clang::NamedDecl* method_context = 148 const clang::NamedDecl* method_context =
146 clang::dyn_cast<clang::NamedDecl>(method.getDeclContext()); 149 clang::dyn_cast<clang::NamedDecl>(method.getDeclContext());
147 if (!method_context) 150 if (!method_context)
148 return false; 151 return false;
152 if (!method_context->getDeclName().isIdentifier())
153 return false;
149 154
150 const llvm::StringMap<std::set<unsigned>>& class_to_args = it->second; 155 const llvm::StringMap<std::set<unsigned>>& class_to_args = it->second;
151 auto it2 = class_to_args.find(method_context->getName()); 156 auto it2 = class_to_args.find(method_context->getName());
152 if (it2 == class_to_args.end()) 157 if (it2 == class_to_args.end())
153 return false; 158 return false;
154 159
155 const std::set<unsigned>& arg_counts = it2->second; 160 const std::set<unsigned>& arg_counts = it2->second;
156 unsigned method_param_count = method.param_size(); 161 unsigned method_param_count = method.param_size();
157 unsigned method_non_optional_param_count = method_param_count; 162 unsigned method_non_optional_param_count = method_param_count;
158 for (const clang::ParmVarDecl* param : method.parameters()) { 163 for (const clang::ParmVarDecl* param : method.parameters()) {
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 } 452 }
448 return false; 453 return false;
449 } 454 }
450 455
451 bool IsBlacklistedMethodName(llvm::StringRef name) { 456 bool IsBlacklistedMethodName(llvm::StringRef name) {
452 return IsBlacklistedFunctionName(name) || 457 return IsBlacklistedFunctionName(name) ||
453 IsBlacklistedInstanceMethodName(name); 458 IsBlacklistedInstanceMethodName(name);
454 } 459 }
455 460
456 bool IsBlacklistedFunction(const clang::FunctionDecl& decl) { 461 bool IsBlacklistedFunction(const clang::FunctionDecl& decl) {
462 if (!decl.getDeclName().isIdentifier())
463 return false;
464
457 clang::StringRef name = decl.getName(); 465 clang::StringRef name = decl.getName();
458 return IsBlacklistedFunctionName(name) || IsBlacklistedFreeFunctionName(name); 466 return IsBlacklistedFunctionName(name) || IsBlacklistedFreeFunctionName(name);
459 } 467 }
460 468
461 bool IsBlacklistedMethod(const clang::CXXMethodDecl& decl) { 469 bool IsBlacklistedMethod(const clang::CXXMethodDecl& decl) {
470 if (!decl.getDeclName().isIdentifier())
471 return false;
472
462 clang::StringRef name = decl.getName(); 473 clang::StringRef name = decl.getName();
463 if (IsBlacklistedFunctionName(name)) 474 if (IsBlacklistedFunctionName(name))
464 return true; 475 return true;
465 476
466 // Remaining cases are only applicable to instance methods. 477 // Remaining cases are only applicable to instance methods.
467 if (decl.isStatic()) 478 if (decl.isStatic())
468 return false; 479 return false;
469 480
470 if (IsBlacklistedInstanceMethodName(name)) 481 if (IsBlacklistedInstanceMethodName(name))
471 return true; 482 return true;
(...skipping 15 matching lines...) Expand all
487 AST_MATCHER(clang::CXXMethodDecl, isBlacklistedMethod) { 498 AST_MATCHER(clang::CXXMethodDecl, isBlacklistedMethod) {
488 return IsBlacklistedMethod(Node); 499 return IsBlacklistedMethod(Node);
489 } 500 }
490 501
491 bool IsKnownTraitName(clang::StringRef name) { 502 bool IsKnownTraitName(clang::StringRef name) {
492 // This set of names is globally a type trait throughout chromium. 503 // This set of names is globally a type trait throughout chromium.
493 return name == "safeToCompareToEmptyOrDeleted"; 504 return name == "safeToCompareToEmptyOrDeleted";
494 } 505 }
495 506
496 AST_MATCHER(clang::VarDecl, isKnownTraitName) { 507 AST_MATCHER(clang::VarDecl, isKnownTraitName) {
497 return IsKnownTraitName(Node.getName()); 508 return Node.getDeclName().isIdentifier() && IsKnownTraitName(Node.getName());
498 } 509 }
499 510
500 // Helper to convert from a camelCaseName to camel_case_name. It uses some 511 // Helper to convert from a camelCaseName to camel_case_name. It uses some
501 // heuristics to try to handle acronyms in camel case names correctly. 512 // heuristics to try to handle acronyms in camel case names correctly.
502 std::string CamelCaseToUnderscoreCase(StringRef input) { 513 std::string CamelCaseToUnderscoreCase(StringRef input) {
503 std::string output; 514 std::string output;
504 bool needs_underscore = false; 515 bool needs_underscore = false;
505 bool was_lowercase = false; 516 bool was_lowercase = false;
506 bool was_uppercase = false; 517 bool was_uppercase = false;
507 bool first_char = true; 518 bool first_char = true;
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
718 }; 729 };
719 for (const auto& conflicting_method : kConflictingMethods) { 730 for (const auto& conflicting_method : kConflictingMethods) {
720 if (old_method_name == conflicting_method) 731 if (old_method_name == conflicting_method)
721 return true; 732 return true;
722 } 733 }
723 734
724 return false; 735 return false;
725 } 736 }
726 737
727 AST_MATCHER(clang::FunctionDecl, shouldPrefixFunctionName) { 738 AST_MATCHER(clang::FunctionDecl, shouldPrefixFunctionName) {
728 return ShouldPrefixFunctionName(Node.getName().str()); 739 return Node.getDeclName().isIdentifier() &&
740 ShouldPrefixFunctionName(Node.getName().str());
729 } 741 }
730 742
731 bool GetNameForDecl(const clang::FunctionDecl& decl, 743 bool GetNameForDecl(const clang::FunctionDecl& decl,
732 clang::ASTContext& context, 744 clang::ASTContext& context,
733 std::string& name) { 745 std::string& name) {
734 name = decl.getName().str(); 746 name = decl.getName().str();
735 name[0] = clang::toUppercase(name[0]); 747 name[0] = clang::toUppercase(name[0]);
736 748
737 // Given 749 // Given
738 // class Foo {}; 750 // class Foo {};
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after
1124 template <typename DeclNode, typename TargetNode> 1136 template <typename DeclNode, typename TargetNode>
1125 class DeclRewriterBase : public RewriterBase<TargetNode> { 1137 class DeclRewriterBase : public RewriterBase<TargetNode> {
1126 public: 1138 public:
1127 using Base = RewriterBase<TargetNode>; 1139 using Base = RewriterBase<TargetNode>;
1128 1140
1129 explicit DeclRewriterBase(std::set<Replacement>* replacements) 1141 explicit DeclRewriterBase(std::set<Replacement>* replacements)
1130 : Base(replacements) {} 1142 : Base(replacements) {}
1131 1143
1132 void run(const MatchFinder::MatchResult& result) override { 1144 void run(const MatchFinder::MatchResult& result) override {
1133 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl"); 1145 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl");
1146 if (!decl->getDeclName().isIdentifier())
1147 return;
1148
1134 assert(decl); 1149 assert(decl);
1135 llvm::StringRef old_name = decl->getName(); 1150 llvm::StringRef old_name = decl->getName();
Łukasz Anforowicz 2017/02/02 22:51:42 In theory a few GetNameForDecl should also be prot
1136 1151
1137 // Return early if there's no name to be renamed. 1152 // Return early if there's no name to be renamed.
1138 if (!decl->getIdentifier()) 1153 if (!decl->getIdentifier())
1139 return; 1154 return;
1140 1155
1141 // Get the new name. 1156 // Get the new name.
1142 std::string new_name; 1157 std::string new_name;
1143 if (!GetNameForDecl(*decl, *result.Context, new_name)) 1158 if (!GetNameForDecl(*decl, *result.Context, new_name))
1144 return; // If false, the name was not suitable for renaming. 1159 return; // If false, the name was not suitable for renaming.
1145 1160
(...skipping 697 matching lines...) Expand 10 before | Expand all | Expand 10 after
1843 for (const auto& r : replacements) { 1858 for (const auto& r : replacements) {
1844 std::string replacement_text = r.getReplacementText().str(); 1859 std::string replacement_text = r.getReplacementText().str();
1845 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 1860 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
1846 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 1861 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
1847 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 1862 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
1848 } 1863 }
1849 llvm::outs() << "==== END EDITS ====\n"; 1864 llvm::outs() << "==== END EDITS ====\n";
1850 1865
1851 return 0; 1866 return 0;
1852 } 1867 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698