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

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

Issue 2324643002: Prepending "Get" when type with conflicting name is hidden inside return type. (Closed)
Patch Set: Addressed CR feedback from danakj@. Created 4 years, 3 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 | tools/clang/rewrite_to_chrome_style/tests/functions-expected.cc » ('j') | 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 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 // instantiation of the template and see if they are all compile-time 283 // instantiation of the template and see if they are all compile-time
284 // isEvaluable(). 284 // isEvaluable().
285 if (initializer->isInstantiationDependent()) 285 if (initializer->isInstantiationDependent())
286 return false; 286 return false;
287 287
288 // If the expression can be evaluated at compile time, then it should have a 288 // If the expression can be evaluated at compile time, then it should have a
289 // kFoo style name. Otherwise, not. 289 // kFoo style name. Otherwise, not.
290 return initializer->isEvaluatable(context); 290 return initializer->isEvaluatable(context);
291 } 291 }
292 292
293 AST_MATCHER_P(clang::QualType, hasString, std::string, ExpectedString) {
294 return ExpectedString == Node.getAsString();
295 }
296
293 bool GetNameForDecl(const clang::FunctionDecl& decl, 297 bool GetNameForDecl(const clang::FunctionDecl& decl,
294 clang::ASTContext& context, 298 clang::ASTContext& context,
295 std::string& name) { 299 std::string& name) {
296 name = decl.getName().str(); 300 name = decl.getName().str();
297 name[0] = clang::toUppercase(name[0]); 301 name[0] = clang::toUppercase(name[0]);
298 302
299 // https://crbug.com/582312: Prepend "Get" if method name conflicts with type. 303 // Given
300 const clang::IdentifierInfo* return_type = 304 // class Foo {};
301 decl.getReturnType().getBaseTypeIdentifier(); 305 // using Bar = Foo;
302 if (return_type && return_type->getName() == name) 306 // Bar f1(); // <- |Bar| would be matched by hasString("Bar") below.
307 // Bar f2(); // <- |Bar| would be matched by hasName("Foo") below.
308 // |type_with_same_name_as_function| matcher matches Bar and Foo return types.
309 auto type_with_same_name_as_function = qualType(anyOf(
310 hasString(name), // hasString matches the type as spelled (Bar above).
311 hasDeclaration(namedDecl(hasName(name))))); // hasDeclaration matches
312 // resolved type (Foo above).
313 // |type_containing_same_name_as_function| matcher will match all of the
314 // return types below:
315 // - Foo foo() // Direct application of |type_with_same_name_as_function|.
316 // - Foo* foo() // |hasDescendant| traverses references/pointers.
317 // - RefPtr<Foo> foo() // |hasDescendant| traverses template arguments.
318 auto type_containing_same_name_as_function =
319 qualType(anyOf(type_with_same_name_as_function,
320 hasDescendant(type_with_same_name_as_function)));
321 // https://crbug.com/582312: Prepend "Get" if method name conflicts with
322 // return type.
323 auto conflict_matcher =
324 functionDecl(returns(type_containing_same_name_as_function));
325 if (!match(conflict_matcher, decl, context).empty())
303 name = "Get" + name; 326 name = "Get" + name;
304 327
305 return true; 328 return true;
306 } 329 }
307 330
308 bool GetNameForDecl(const clang::EnumConstantDecl& decl, 331 bool GetNameForDecl(const clang::EnumConstantDecl& decl,
309 clang::ASTContext& context, 332 clang::ASTContext& context,
310 std::string& name) { 333 std::string& name) {
311 StringRef original_name = decl.getName(); 334 StringRef original_name = decl.getName();
312 335
(...skipping 619 matching lines...) Expand 10 before | Expand all | Expand 10 after
932 for (const auto& r : replacements) { 955 for (const auto& r : replacements) {
933 std::string replacement_text = r.getReplacementText().str(); 956 std::string replacement_text = r.getReplacementText().str();
934 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 957 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
935 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 958 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
936 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 959 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
937 } 960 }
938 llvm::outs() << "==== END EDITS ====\n"; 961 llvm::outs() << "==== END EDITS ====\n";
939 962
940 return 0; 963 return 0;
941 } 964 }
OLDNEW
« no previous file with comments | « no previous file | tools/clang/rewrite_to_chrome_style/tests/functions-expected.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698