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

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

Issue 1642323002: rewrite_to_chrome_style: Fix assert calling getName() when there's none (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rewrite-getname: rebase Created 4 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 AST_MATCHER(clang::FunctionDecl, isOverloadedOperator) { 52 AST_MATCHER(clang::FunctionDecl, isOverloadedOperator) {
53 return Node.isOverloadedOperator(); 53 return Node.isOverloadedOperator();
54 } 54 }
55 55
56 const char kBlinkFieldPrefix[] = "m_"; 56 const char kBlinkFieldPrefix[] = "m_";
57 const char kBlinkStaticMemberPrefix[] = "s_"; 57 const char kBlinkStaticMemberPrefix[] = "s_";
58 58
59 bool GetNameForDecl(const clang::FunctionDecl& decl, 59 bool GetNameForDecl(const clang::FunctionDecl& decl,
60 const clang::ASTContext& context, 60 const clang::ASTContext& context,
61 std::string& name) { 61 std::string& name) {
62 name = decl.getNameAsString(); 62 StringRef original_name = decl.getName();
63 63
64 if (const auto* method = clang::dyn_cast<const clang::CXXMethodDecl>(&decl)) { 64 if (const auto* method = clang::dyn_cast<const clang::CXXMethodDecl>(&decl)) {
65 if (!method->isStatic()) { 65 if (!method->isStatic()) {
66 // Some methods shouldn't be renamed because reasons. 66 // Some methods shouldn't be renamed because reasons.
67 static const char* kBlacklist[] = {"begin", "end", "rbegin", "rend", 67 static const char* kBlacklist[] = {"begin", "end", "rbegin", "rend",
68 "trace"}; 68 "trace"};
69 for (const auto& b : kBlacklist) { 69 for (const auto& b : kBlacklist) {
70 if (name == b) 70 if (original_name == b)
71 return false; 71 return false;
72 } 72 }
73 } 73 }
74 } 74 }
75 75
76 name = original_name.str();
76 name[0] = clang::toUppercase(name[0]); 77 name[0] = clang::toUppercase(name[0]);
77 return true; 78 return true;
78 } 79 }
79 80
80 // Helper to convert from a camelCaseName to camel_case_name. It uses some 81 // Helper to convert from a camelCaseName to camel_case_name. It uses some
81 // heuristics to try to handle acronyms in camel case names correctly. 82 // heuristics to try to handle acronyms in camel case names correctly.
82 std::string CamelCaseToUnderscoreCase(StringRef input) { 83 std::string CamelCaseToUnderscoreCase(StringRef input) {
83 std::string output; 84 std::string output;
84 bool needs_underscore = false; 85 bool needs_underscore = false;
85 bool was_lowercase = false; 86 bool was_lowercase = false;
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 template <typename DeclNode, typename TargetNode> 238 template <typename DeclNode, typename TargetNode>
238 class RewriterBase : public MatchFinder::MatchCallback { 239 class RewriterBase : public MatchFinder::MatchCallback {
239 public: 240 public:
240 explicit RewriterBase(Replacements* replacements) 241 explicit RewriterBase(Replacements* replacements)
241 : replacements_(replacements) {} 242 : replacements_(replacements) {}
242 243
243 void run(const MatchFinder::MatchResult& result) override { 244 void run(const MatchFinder::MatchResult& result) override {
244 std::string new_name; 245 std::string new_name;
245 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl"); 246 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl");
246 clang::ASTContext* context = result.Context; 247 clang::ASTContext* context = result.Context;
248 // If false, there's no name to be renamed.
249 if (!decl->getIdentifier())
250 return;
251 // If false, the name was not suitable for renaming.
247 if (!GetNameForDecl(*decl, *context, new_name)) 252 if (!GetNameForDecl(*decl, *context, new_name))
248 return; 253 return;
249 llvm::StringRef old_name = decl->getName(); 254 llvm::StringRef old_name = decl->getName();
250 if (old_name == new_name) 255 if (old_name == new_name)
251 return; 256 return;
252 clang::CharSourceRange range = TargetNodeTraits<TargetNode>::GetRange( 257 clang::CharSourceRange range = TargetNodeTraits<TargetNode>::GetRange(
253 *result.Nodes.getNodeAs<TargetNode>( 258 *result.Nodes.getNodeAs<TargetNode>(
254 TargetNodeTraits<TargetNode>::kName)); 259 TargetNodeTraits<TargetNode>::kName));
255 if (range.getBegin().isMacroID() || range.getEnd().isMacroID()) 260 if (range.getBegin().isMacroID() || range.getEnd().isMacroID())
256 return; 261 return;
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
575 for (const auto& r : replacements) { 580 for (const auto& r : replacements) {
576 std::string replacement_text = r.getReplacementText().str(); 581 std::string replacement_text = r.getReplacementText().str();
577 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 582 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
578 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 583 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
579 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 584 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
580 } 585 }
581 llvm::outs() << "==== END EDITS ====\n"; 586 llvm::outs() << "==== END EDITS ====\n";
582 587
583 return 0; 588 return 0;
584 } 589 }
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