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 IsMethodNameLikelyToConflictWithTypeName(std::string method_name) { | |
dcheng
2016/12/19 19:26:52
Just call this ShouldPrefixMethodName ('likely' is
Łukasz Anforowicz
2016/12/19 21:58:47
Done.
| |
344 // Make sure that IsMethodNameLikelyToConflictWithTypeName works | |
345 // both when |method_name| is the old and/or the new name. | |
346 method_name[0] = clang::toLowercase(method_name[0]); | |
dcheng
2016/12/19 19:26:52
As discussed, I think we shouldn't need this:
- if
Łukasz Anforowicz
2016/12/19 21:58:47
Done. (OTOH, please note that this is why I also
| |
347 | |
348 // Methods that are named similarily to a type - they should be prefixed | |
349 // with a "get" prefix. | |
350 static const char* kConflictingMethods[] = { | |
351 "entryType", | |
352 "readyState", | |
Łukasz Anforowicz
2016/12/19 19:07:35
Nasko, can you help populate this list with more i
Łukasz Anforowicz
2016/12/20 00:32:05
I've populated this list with the errors I've seen
| |
353 }; | |
354 for (const auto& conflicting_method : kConflictingMethods) { | |
355 if (method_name == conflicting_method) | |
356 return true; | |
357 } | |
358 | |
359 return false; | |
360 } | |
361 | |
343 bool GetNameForDecl(const clang::FunctionDecl& decl, | 362 bool GetNameForDecl(const clang::FunctionDecl& decl, |
344 clang::ASTContext& context, | 363 clang::ASTContext& context, |
345 std::string& name) { | 364 std::string& name) { |
346 name = decl.getName().str(); | 365 name = decl.getName().str(); |
347 name[0] = clang::toUppercase(name[0]); | 366 name[0] = clang::toUppercase(name[0]); |
348 | 367 |
349 // Given | 368 // Given |
350 // class Foo {}; | 369 // class Foo {}; |
351 // class DerivedFoo : class Foo; | 370 // class DerivedFoo : class Foo; |
352 // using Bar = Foo; | 371 // using Bar = Foo; |
(...skipping 13 matching lines...) Expand all Loading... | |
366 // - Foo foo() // Direct application of |type_with_same_name_as_function|. | 385 // - Foo foo() // Direct application of |type_with_same_name_as_function|. |
367 // - Foo* foo() // |hasDescendant| traverses references/pointers. | 386 // - Foo* foo() // |hasDescendant| traverses references/pointers. |
368 // - RefPtr<Foo> foo() // |hasDescendant| traverses template arguments. | 387 // - RefPtr<Foo> foo() // |hasDescendant| traverses template arguments. |
369 auto type_containing_same_name_as_function = | 388 auto type_containing_same_name_as_function = |
370 qualType(anyOf(type_with_same_name_as_function, | 389 qualType(anyOf(type_with_same_name_as_function, |
371 hasDescendant(type_with_same_name_as_function))); | 390 hasDescendant(type_with_same_name_as_function))); |
372 // https://crbug.com/582312: Prepend "Get" if method name conflicts with | 391 // https://crbug.com/582312: Prepend "Get" if method name conflicts with |
373 // return type. | 392 // return type. |
374 auto conflict_matcher = | 393 auto conflict_matcher = |
375 functionDecl(returns(type_containing_same_name_as_function)); | 394 functionDecl(returns(type_containing_same_name_as_function)); |
376 if (IsMatching(conflict_matcher, decl, context)) | 395 bool return_type_conflicts_with_method_name = |
396 IsMatching(conflict_matcher, decl, context); | |
397 | |
398 if (return_type_conflicts_with_method_name || | |
399 IsMethodNameLikelyToConflictWithTypeName(name)) { | |
377 name = "Get" + name; | 400 name = "Get" + name; |
401 } | |
378 | 402 |
379 return true; | 403 return true; |
380 } | 404 } |
381 | 405 |
382 bool GetNameForDecl(const clang::EnumConstantDecl& decl, | 406 bool GetNameForDecl(const clang::EnumConstantDecl& decl, |
383 clang::ASTContext& context, | 407 clang::ASTContext& context, |
384 std::string& name) { | 408 std::string& name) { |
385 StringRef original_name = decl.getName(); | 409 StringRef original_name = decl.getName(); |
386 | 410 |
387 // If it's already correct leave it alone. | 411 // 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) + "_"; | 846 new_name = CamelCaseToUnderscoreCase(field_name) + "_"; |
823 return true; | 847 return true; |
824 } | 848 } |
825 } | 849 } |
826 | 850 |
827 // |T::myMethod(...)| -> |T::MyMethod(...)|. | 851 // |T::myMethod(...)| -> |T::MyMethod(...)|. |
828 if ((old_name.find('_') == std::string::npos) && IsCallee(node, context) && | 852 if ((old_name.find('_') == std::string::npos) && IsCallee(node, context) && |
829 !IsBlacklistedFunctionOrMethodName(old_name)) { | 853 !IsBlacklistedFunctionOrMethodName(old_name)) { |
830 new_name = old_name; | 854 new_name = old_name; |
831 new_name[0] = clang::toUppercase(old_name[0]); | 855 new_name[0] = clang::toUppercase(old_name[0]); |
856 if (IsMethodNameLikelyToConflictWithTypeName(new_name)) | |
857 new_name = "Get" + new_name; | |
832 return true; | 858 return true; |
833 } | 859 } |
834 | 860 |
835 // In the future we can consider more heuristics: | 861 // In the future we can consider more heuristics: |
836 // - "s_" and "g_" prefixes | 862 // - "s_" and "g_" prefixes |
837 // - "ALL_CAPS" | 863 // - "ALL_CAPS" |
838 // - |T::myStaticField| -> |T::kMyStaticField| | 864 // - |T::myStaticField| -> |T::kMyStaticField| |
839 // (but have to be careful not to rename |value| in WTF/TypeTraits.h?) | 865 // (but have to be careful not to rename |value| in WTF/TypeTraits.h?) |
840 return false; | 866 return false; |
841 } | 867 } |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1265 for (const auto& r : replacements) { | 1291 for (const auto& r : replacements) { |
1266 std::string replacement_text = r.getReplacementText().str(); | 1292 std::string replacement_text = r.getReplacementText().str(); |
1267 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 1293 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
1268 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 1294 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
1269 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 1295 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
1270 } | 1296 } |
1271 llvm::outs() << "==== END EDITS ====\n"; | 1297 llvm::outs() << "==== END EDITS ====\n"; |
1272 | 1298 |
1273 return 0; | 1299 return 0; |
1274 } | 1300 } |
OLD | NEW |