Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 | 333 |
| 334 // If the expression can be evaluated at compile time, then it should have a | 334 // If the expression can be evaluated at compile time, then it should have a |
| 335 // kFoo style name. Otherwise, not. | 335 // kFoo style name. Otherwise, not. |
| 336 return initializer->isEvaluatable(context); | 336 return initializer->isEvaluatable(context); |
| 337 } | 337 } |
| 338 | 338 |
| 339 AST_MATCHER_P(clang::QualType, hasString, std::string, ExpectedString) { | 339 AST_MATCHER_P(clang::QualType, hasString, std::string, ExpectedString) { |
| 340 return ExpectedString == Node.getAsString(); | 340 return ExpectedString == Node.getAsString(); |
| 341 } | 341 } |
| 342 | 342 |
| 343 bool ShouldPrefixMethodName(std::string old_method_name) { | |
|
dcheng
2016/12/20 00:42:02
StringRef or const std::string&
Łukasz Anforowicz
2016/12/20 00:58:58
Done - thanks for pointing this out (I initially h
| |
| 344 // Methods that are named similarily to a type - they should be prefixed | |
| 345 // with a "get" prefix. | |
| 346 static const char* kConflictingMethods[] = { | |
| 347 "animationWorklet", | |
| 348 "audioWorklet", | |
| 349 "binaryType", | |
| 350 "blob", | |
| 351 "channelCountMode", | |
| 352 "color", | |
| 353 "counterDirectives", | |
| 354 "document", | |
| 355 "emptyChromeClient", | |
| 356 "emptyEditorClient", | |
| 357 "emptySpellCheckerClient", | |
| 358 "entryType", | |
| 359 "error", | |
| 360 "fileUtilities", | |
| 361 "font", | |
| 362 "iconURL", | |
| 363 "inputMethodController", | |
| 364 "inputType", | |
| 365 "layout", | |
| 366 "layoutBlock", | |
| 367 "layoutSize", | |
| 368 "length", | |
| 369 "lineCap", | |
| 370 "lineJoin", | |
| 371 "matchedProperties", | |
| 372 "name", | |
| 373 "navigationType", | |
| 374 "node", | |
| 375 "outcome", | |
| 376 "pagePopup", | |
| 377 "paintWorklet", | |
| 378 "path", | |
| 379 "processingInstruction", | |
| 380 "readyState", | |
| 381 "screenInfo", | |
| 382 "scrollAnimator", | |
| 383 "settings", | |
| 384 "signalingState", | |
| 385 "state", | |
| 386 "string", | |
| 387 "text", | |
| 388 "textAlign", | |
| 389 "textBaseline", | |
| 390 "theme", | |
| 391 "timing", | |
| 392 "topLevelBlameContext", | |
|
dcheng
2016/12/20 00:42:02
This one looks surprising, as I don't see a type w
Łukasz Anforowicz
2016/12/20 00:58:58
Ack. Please see the error message in my other rep
| |
| 393 "widget", | |
| 394 }; | |
| 395 for (const auto& conflicting_method : kConflictingMethods) { | |
| 396 if (old_method_name == conflicting_method) | |
| 397 return true; | |
| 398 } | |
| 399 | |
| 400 return false; | |
| 401 } | |
| 402 | |
| 343 bool GetNameForDecl(const clang::FunctionDecl& decl, | 403 bool GetNameForDecl(const clang::FunctionDecl& decl, |
| 344 clang::ASTContext& context, | 404 clang::ASTContext& context, |
| 345 std::string& name) { | 405 std::string& name) { |
| 346 name = decl.getName().str(); | 406 name = decl.getName().str(); |
| 407 | |
| 408 // Need to call ShouldPrefixMethodName on the *old* name. | |
| 409 bool should_prefix_method_name = ShouldPrefixMethodName(name); | |
| 410 | |
| 411 // Calculate the new name. | |
| 347 name[0] = clang::toUppercase(name[0]); | 412 name[0] = clang::toUppercase(name[0]); |
| 348 | 413 |
| 349 // Given | 414 // See if the new name conflicts with a type ~inside~ the return type. |
| 350 // class Foo {}; | 415 if (!should_prefix_method_name) { |
| 351 // class DerivedFoo : class Foo; | 416 // Given |
| 352 // using Bar = Foo; | 417 // class Foo {}; |
| 353 // Bar f1(); // <- |Bar| would be matched by hasString("Bar") below. | 418 // class DerivedFoo : class Foo; |
| 354 // Bar f2(); // <- |Bar| would be matched by hasName("Foo") below. | 419 // using Bar = Foo; |
| 355 // DerivedFoo f3(); // <- |DerivedFoo| matched by isDerivedFrom(...) below. | 420 // |type_with_same_name_as_function| would match: |
| 356 // |type_with_same_name_as_function| matcher matches Bar and Foo return types. | 421 // Bar bar(); // <- |Bar| would be matched by hasString("Bar") below. |
| 357 auto type_with_same_name_as_function = qualType(anyOf( | 422 // Bar foo(); // <- |Bar| would be matched by hasName("Foo") below. |
| 358 // hasString matches the type as spelled (Bar above). | 423 // DerivedFoo foo(); // <- |DerivedFoo| matched by isDerivedFrom(...) |
| 359 hasString(name), | 424 // // below. |
| 360 // hasDeclaration matches resolved type (Foo or DerivedFoo above). | 425 auto type_with_same_name_as_function = qualType(anyOf( |
| 361 hasDeclaration(namedDecl(hasName(name))), | 426 // hasString matches the type as spelled (Bar above). |
| 362 hasDeclaration(cxxRecordDecl(isDerivedFrom(namedDecl(hasName(name))))))); | 427 hasString(name), |
| 428 // hasDeclaration matches resolved type (Foo or DerivedFoo above). | |
| 429 hasDeclaration(namedDecl(hasName(name))), | |
| 430 hasDeclaration( | |
| 431 cxxRecordDecl(isDerivedFrom(namedDecl(hasName(name))))))); | |
| 363 | 432 |
| 364 // |type_containing_same_name_as_function| matcher will match all of the | 433 // |type_containing_same_name_as_function| matcher will match all of the |
| 365 // return types below: | 434 // return types below: |
| 366 // - Foo foo() // Direct application of |type_with_same_name_as_function|. | 435 // - Foo foo() // Direct application of |type_with_same_name_as_function|. |
| 367 // - Foo* foo() // |hasDescendant| traverses references/pointers. | 436 // - Foo* foo() // |hasDescendant| traverses references/pointers. |
| 368 // - RefPtr<Foo> foo() // |hasDescendant| traverses template arguments. | 437 // - RefPtr<Foo> foo() // |hasDescendant| traverses template arguments. |
| 369 auto type_containing_same_name_as_function = | 438 auto type_containing_same_name_as_function = |
| 370 qualType(anyOf(type_with_same_name_as_function, | 439 qualType(anyOf(type_with_same_name_as_function, |
| 371 hasDescendant(type_with_same_name_as_function))); | 440 hasDescendant(type_with_same_name_as_function))); |
| 372 // https://crbug.com/582312: Prepend "Get" if method name conflicts with | 441 // https://crbug.com/582312: Prepend "Get" if method name conflicts with |
| 373 // return type. | 442 // return type. |
| 374 auto conflict_matcher = | 443 auto conflict_matcher = |
| 375 functionDecl(returns(type_containing_same_name_as_function)); | 444 functionDecl(returns(type_containing_same_name_as_function)); |
| 376 if (IsMatching(conflict_matcher, decl, context)) | 445 if (IsMatching(conflict_matcher, decl, context)) |
| 446 should_prefix_method_name = true; | |
| 447 } | |
| 448 | |
| 449 if (should_prefix_method_name) { | |
| 377 name = "Get" + name; | 450 name = "Get" + name; |
| 451 } | |
| 378 | 452 |
| 379 return true; | 453 return true; |
| 380 } | 454 } |
| 381 | 455 |
| 382 bool GetNameForDecl(const clang::EnumConstantDecl& decl, | 456 bool GetNameForDecl(const clang::EnumConstantDecl& decl, |
| 383 clang::ASTContext& context, | 457 clang::ASTContext& context, |
| 384 std::string& name) { | 458 std::string& name) { |
| 385 StringRef original_name = decl.getName(); | 459 StringRef original_name = decl.getName(); |
| 386 | 460 |
| 387 // If it's already correct leave it alone. | 461 // If it's already correct leave it alone. |
| (...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 822 new_name = CamelCaseToUnderscoreCase(field_name) + "_"; | 896 new_name = CamelCaseToUnderscoreCase(field_name) + "_"; |
| 823 return true; | 897 return true; |
| 824 } | 898 } |
| 825 } | 899 } |
| 826 | 900 |
| 827 // |T::myMethod(...)| -> |T::MyMethod(...)|. | 901 // |T::myMethod(...)| -> |T::MyMethod(...)|. |
| 828 if ((old_name.find('_') == std::string::npos) && IsCallee(node, context) && | 902 if ((old_name.find('_') == std::string::npos) && IsCallee(node, context) && |
| 829 !IsBlacklistedFunctionOrMethodName(old_name)) { | 903 !IsBlacklistedFunctionOrMethodName(old_name)) { |
| 830 new_name = old_name; | 904 new_name = old_name; |
| 831 new_name[0] = clang::toUppercase(old_name[0]); | 905 new_name[0] = clang::toUppercase(old_name[0]); |
| 906 if (ShouldPrefixMethodName(old_name)) | |
| 907 new_name = "Get" + new_name; | |
| 832 return true; | 908 return true; |
| 833 } | 909 } |
| 834 | 910 |
| 835 // In the future we can consider more heuristics: | 911 // In the future we can consider more heuristics: |
| 836 // - "s_" and "g_" prefixes | 912 // - "s_" and "g_" prefixes |
| 837 // - "ALL_CAPS" | 913 // - "ALL_CAPS" |
| 838 // - |T::myStaticField| -> |T::kMyStaticField| | 914 // - |T::myStaticField| -> |T::kMyStaticField| |
| 839 // (but have to be careful not to rename |value| in WTF/TypeTraits.h?) | 915 // (but have to be careful not to rename |value| in WTF/TypeTraits.h?) |
| 840 return false; | 916 return false; |
| 841 } | 917 } |
| (...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1265 for (const auto& r : replacements) { | 1341 for (const auto& r : replacements) { |
| 1266 std::string replacement_text = r.getReplacementText().str(); | 1342 std::string replacement_text = r.getReplacementText().str(); |
| 1267 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 1343 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 1268 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 1344 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 1269 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 1345 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 1270 } | 1346 } |
| 1271 llvm::outs() << "==== END EDITS ====\n"; | 1347 llvm::outs() << "==== END EDITS ====\n"; |
| 1272 | 1348 |
| 1273 return 0; | 1349 return 0; |
| 1274 } | 1350 } |
| OLD | NEW |