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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/clang/rewrite_to_chrome_style/RewriteToChromeStyle.cpp
diff --git a/tools/clang/rewrite_to_chrome_style/RewriteToChromeStyle.cpp b/tools/clang/rewrite_to_chrome_style/RewriteToChromeStyle.cpp
index 600adeb807171fb69775d72155cf35aa883edac9..36a53d6ba93ed5a276ff767bb36ef0c3846a6f3d 100644
--- a/tools/clang/rewrite_to_chrome_style/RewriteToChromeStyle.cpp
+++ b/tools/clang/rewrite_to_chrome_style/RewriteToChromeStyle.cpp
@@ -290,16 +290,39 @@ bool IsProbablyConst(const clang::VarDecl& decl,
return initializer->isEvaluatable(context);
}
+AST_MATCHER_P(clang::QualType, hasString, std::string, ExpectedString) {
+ return ExpectedString == Node.getAsString();
+}
+
bool GetNameForDecl(const clang::FunctionDecl& decl,
clang::ASTContext& context,
std::string& name) {
name = decl.getName().str();
name[0] = clang::toUppercase(name[0]);
- // https://crbug.com/582312: Prepend "Get" if method name conflicts with type.
- const clang::IdentifierInfo* return_type =
- decl.getReturnType().getBaseTypeIdentifier();
- if (return_type && return_type->getName() == name)
+ // Given
+ // class Foo {};
+ // using Bar = Foo;
+ // Bar f1(); // <- |Bar| would be matched by hasString("Bar") below.
+ // Bar f2(); // <- |Bar| would be matched by hasName("Foo") below.
+ // |type_with_same_name_as_function| matcher matches Bar and Foo return types.
+ auto type_with_same_name_as_function = qualType(anyOf(
+ hasString(name), // hasString matches the type as spelled (Bar above).
+ hasDeclaration(namedDecl(hasName(name))))); // hasDeclaration matches
+ // resolved type (Foo above).
+ // |type_containing_same_name_as_function| matcher will match all of the
+ // return types below:
+ // - Foo foo() // Direct application of |type_with_same_name_as_function|.
+ // - Foo* foo() // |hasDescendant| traverses references/pointers.
+ // - RefPtr<Foo> foo() // |hasDescendant| traverses template arguments.
+ auto type_containing_same_name_as_function =
+ qualType(anyOf(type_with_same_name_as_function,
+ hasDescendant(type_with_same_name_as_function)));
+ // https://crbug.com/582312: Prepend "Get" if method name conflicts with
+ // return type.
+ auto conflict_matcher =
+ functionDecl(returns(type_containing_same_name_as_function));
+ if (!match(conflict_matcher, decl, context).empty())
name = "Get" + name;
return true;
« 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