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

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

Issue 2589923002: Add a hardcoded list of methods that need a "Get" prefix. (Closed)
Patch Set: Rebasing... Created 3 years, 12 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/methods-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 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 323
324 // If the expression can be evaluated at compile time, then it should have a 324 // If the expression can be evaluated at compile time, then it should have a
325 // kFoo style name. Otherwise, not. 325 // kFoo style name. Otherwise, not.
326 return initializer->isEvaluatable(context); 326 return initializer->isEvaluatable(context);
327 } 327 }
328 328
329 AST_MATCHER_P(clang::QualType, hasString, std::string, ExpectedString) { 329 AST_MATCHER_P(clang::QualType, hasString, std::string, ExpectedString) {
330 return ExpectedString == Node.getAsString(); 330 return ExpectedString == Node.getAsString();
331 } 331 }
332 332
333 bool ShouldPrefixFunctionName(const std::string& old_method_name) {
334 // Methods that are named similarily to a type - they should be prefixed
335 // with a "get" prefix.
336 static const char* kConflictingMethods[] = {
337 "animationWorklet",
338 "audioWorklet",
339 "binaryType",
340 "blob",
341 "channelCountMode",
342 "color",
343 "counterDirectives",
344 "document",
345 "emptyChromeClient",
346 "emptyEditorClient",
347 "emptySpellCheckerClient",
348 "entryType",
349 "error",
350 "fileUtilities",
351 "font",
352 "iconURL",
353 "inputMethodController",
354 "inputType",
355 "layout",
356 "layoutBlock",
357 "layoutSize",
358 "length",
359 "lineCap",
360 "lineJoin",
361 "matchedProperties",
362 "name",
363 "navigationType",
364 "node",
365 "outcome",
366 "pagePopup",
367 "paintWorklet",
368 "path",
369 "processingInstruction",
370 "readyState",
371 "screenInfo",
372 "scrollAnimator",
373 "settings",
374 "signalingState",
375 "state",
376 "string",
377 "text",
378 "textAlign",
379 "textBaseline",
380 "theme",
381 "timing",
382 "topLevelBlameContext",
383 "widget",
384 };
385 for (const auto& conflicting_method : kConflictingMethods) {
386 if (old_method_name == conflicting_method)
387 return true;
388 }
389
390 return false;
391 }
392
393 AST_MATCHER(clang::FunctionDecl, shouldPrefixFunctionName) {
394 return ShouldPrefixFunctionName(Node.getName().str());
395 }
396
333 bool GetNameForDecl(const clang::FunctionDecl& decl, 397 bool GetNameForDecl(const clang::FunctionDecl& decl,
334 clang::ASTContext& context, 398 clang::ASTContext& context,
335 std::string& name) { 399 std::string& name) {
336 name = decl.getName().str(); 400 name = decl.getName().str();
337 name[0] = clang::toUppercase(name[0]); 401 name[0] = clang::toUppercase(name[0]);
338 402
339 // Given 403 // Given
340 // class Foo {}; 404 // class Foo {};
341 // class DerivedFoo : class Foo; 405 // class DerivedFoo : class Foo;
342 // using Bar = Foo; 406 // using Bar = Foo;
343 // Bar f1(); // <- |Bar| would be matched by hasString("Bar") below. 407 // Bar f1(); // <- |Bar| would be matched by hasString("Bar") below.
344 // Bar f2(); // <- |Bar| would be matched by hasName("Foo") below. 408 // Bar f2(); // <- |Bar| would be matched by hasName("Foo") below.
345 // DerivedFoo f3(); // <- |DerivedFoo| matched by isDerivedFrom(...) below. 409 // DerivedFoo f3(); // <- |DerivedFoo| matched by isDerivedFrom(...) below.
346 // |type_with_same_name_as_function| matcher matches Bar and Foo return types. 410 // |type_with_same_name_as_function| matcher matches Bar and Foo return types.
347 auto type_with_same_name_as_function = qualType(anyOf( 411 auto type_with_same_name_as_function = qualType(anyOf(
348 // hasString matches the type as spelled (Bar above). 412 // hasString matches the type as spelled (Bar above).
349 hasString(name), 413 hasString(name),
350 // hasDeclaration matches resolved type (Foo or DerivedFoo above). 414 // hasDeclaration matches resolved type (Foo or DerivedFoo above).
351 hasDeclaration(namedDecl(hasName(name))), 415 hasDeclaration(namedDecl(hasName(name))),
352 hasDeclaration(cxxRecordDecl(isDerivedFrom(namedDecl(hasName(name))))))); 416 hasDeclaration(
417 cxxRecordDecl(isDerivedFrom(namedDecl(hasName(name)))))));
353 418
354 // |type_containing_same_name_as_function| matcher will match all of the 419 // |type_containing_same_name_as_function| matcher will match all of the
355 // return types below: 420 // return types below:
356 // - Foo foo() // Direct application of |type_with_same_name_as_function|. 421 // - Foo foo() // Direct application of |type_with_same_name_as_function|.
357 // - Foo* foo() // |hasDescendant| traverses references/pointers. 422 // - Foo* foo() // |hasDescendant| traverses references/pointers.
358 // - RefPtr<Foo> foo() // |hasDescendant| traverses template arguments. 423 // - RefPtr<Foo> foo() // |hasDescendant| traverses template arguments.
359 auto type_containing_same_name_as_function = 424 auto type_containing_same_name_as_function =
360 qualType(anyOf(type_with_same_name_as_function, 425 qualType(anyOf(type_with_same_name_as_function,
361 hasDescendant(type_with_same_name_as_function))); 426 hasDescendant(type_with_same_name_as_function)));
362 // https://crbug.com/582312: Prepend "Get" if method name conflicts with 427 // https://crbug.com/582312: Prepend "Get" if method name conflicts with
363 // return type. 428 // return type.
364 auto conflict_matcher = 429 auto conflict_matcher = functionDecl(anyOf(
365 functionDecl(returns(type_containing_same_name_as_function)); 430 returns(type_containing_same_name_as_function),
431 shouldPrefixFunctionName()));
366 if (IsMatching(conflict_matcher, decl, context)) 432 if (IsMatching(conflict_matcher, decl, context))
367 name = "Get" + name; 433 name = "Get" + name;
368 434
369 return true; 435 return true;
370 } 436 }
371 437
372 bool GetNameForDecl(const clang::EnumConstantDecl& decl, 438 bool GetNameForDecl(const clang::EnumConstantDecl& decl,
373 clang::ASTContext& context, 439 clang::ASTContext& context,
374 std::string& name) { 440 std::string& name) {
375 StringRef original_name = decl.getName(); 441 StringRef original_name = decl.getName();
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after
812 new_name = CamelCaseToUnderscoreCase(field_name) + "_"; 878 new_name = CamelCaseToUnderscoreCase(field_name) + "_";
813 return true; 879 return true;
814 } 880 }
815 } 881 }
816 882
817 // |T::myMethod(...)| -> |T::MyMethod(...)|. 883 // |T::myMethod(...)| -> |T::MyMethod(...)|.
818 if ((old_name.find('_') == std::string::npos) && IsCallee(node, context) && 884 if ((old_name.find('_') == std::string::npos) && IsCallee(node, context) &&
819 !IsBlacklistedMethodName(old_name)) { 885 !IsBlacklistedMethodName(old_name)) {
820 new_name = old_name; 886 new_name = old_name;
821 new_name[0] = clang::toUppercase(old_name[0]); 887 new_name[0] = clang::toUppercase(old_name[0]);
888 if (ShouldPrefixFunctionName(old_name))
889 new_name = "Get" + new_name;
822 return true; 890 return true;
823 } 891 }
824 892
825 // In the future we can consider more heuristics: 893 // In the future we can consider more heuristics:
826 // - "s_" and "g_" prefixes 894 // - "s_" and "g_" prefixes
827 // - "ALL_CAPS" 895 // - "ALL_CAPS"
828 // - |T::myStaticField| -> |T::kMyStaticField| 896 // - |T::myStaticField| -> |T::kMyStaticField|
829 // (but have to be careful not to rename |value| in WTF/TypeTraits.h?) 897 // (but have to be careful not to rename |value| in WTF/TypeTraits.h?)
830 return false; 898 return false;
831 } 899 }
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after
1255 for (const auto& r : replacements) { 1323 for (const auto& r : replacements) {
1256 std::string replacement_text = r.getReplacementText().str(); 1324 std::string replacement_text = r.getReplacementText().str();
1257 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 1325 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
1258 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 1326 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
1259 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 1327 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
1260 } 1328 }
1261 llvm::outs() << "==== END EDITS ====\n"; 1329 llvm::outs() << "==== END EDITS ====\n";
1262 1330
1263 return 0; 1331 return 0;
1264 } 1332 }
OLDNEW
« no previous file with comments | « no previous file | tools/clang/rewrite_to_chrome_style/tests/methods-expected.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698