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

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

Issue 2667273003: Make the tool rename identifiers declared in (generated) ComputedStyleBase.h (Closed)
Patch Set: Created 3 years, 10 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 | no next file » | 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 using namespace clang::ast_matchers; 44 using namespace clang::ast_matchers;
45 using clang::tooling::CommonOptionsParser; 45 using clang::tooling::CommonOptionsParser;
46 using clang::tooling::Replacement; 46 using clang::tooling::Replacement;
47 using llvm::StringRef; 47 using llvm::StringRef;
48 48
49 namespace { 49 namespace {
50 50
51 const char kBlinkFieldPrefix[] = "m_"; 51 const char kBlinkFieldPrefix[] = "m_";
52 const char kBlinkStaticMemberPrefix[] = "s_"; 52 const char kBlinkStaticMemberPrefix[] = "s_";
53 const char kGeneratedFileRegex[] = "^gen/|/gen/"; 53 const char kGeneratedFileRegex[] = "^gen/|/gen/";
54 const char kGeneratedFileExclusionRegex[] =
55 "(^gen/|/gen/).*/ComputedStyleBase\\.h$";
54 const char kGMockMethodNamePrefix[] = "gmock_"; 56 const char kGMockMethodNamePrefix[] = "gmock_";
55 const char kMethodBlocklistParamName[] = "method-blocklist"; 57 const char kMethodBlocklistParamName[] = "method-blocklist";
56 58
57 template <typename MatcherType, typename NodeType> 59 template <typename MatcherType, typename NodeType>
58 bool IsMatching(const MatcherType& matcher, 60 bool IsMatching(const MatcherType& matcher,
59 const NodeType& node, 61 const NodeType& node,
60 clang::ASTContext& context) { 62 clang::ASTContext& context) {
61 return !match(matcher, node, context).empty(); 63 return !match(matcher, node, context).empty();
62 } 64 }
63 65
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 492
491 bool IsKnownTraitName(clang::StringRef name) { 493 bool IsKnownTraitName(clang::StringRef name) {
492 // This set of names is globally a type trait throughout chromium. 494 // This set of names is globally a type trait throughout chromium.
493 return name == "safeToCompareToEmptyOrDeleted"; 495 return name == "safeToCompareToEmptyOrDeleted";
494 } 496 }
495 497
496 AST_MATCHER(clang::VarDecl, isKnownTraitName) { 498 AST_MATCHER(clang::VarDecl, isKnownTraitName) {
497 return IsKnownTraitName(Node.getName()); 499 return IsKnownTraitName(Node.getName());
498 } 500 }
499 501
502 AST_MATCHER(clang::Decl, isDeclInGeneratedFile) {
503 // This matcher mimics the built-in isExpansionInFileMatching matcher from
504 // llvm/tools/clang/include/clang/ASTMatchers/ASTMatchers.h, except:
505 // - It special cases some files (e.g. doesn't skip renaming of identifiers
506 // from gen/blink/core/ComputedStyleBase.h)
507
508 const clang::SourceManager& source_manager =
509 Node.getASTContext().getSourceManager();
510
511 // TODO(lukasza): Consider using getSpellingLoc below.
512 // The built-in isExpansionInFileMatching matcher uses getExpansionLoc below.
513 // We could consider using getSpellingLoc (which properly handles things like
514 // SETTINGS_GETTERS_AND_SETTERS macro which is defined in generated code
515 // (gen/blink/core/SettingsMacros.h), but expanded in non-generated code
516 // (third_party/WebKit/Source/core/frame/Settings.h).
Łukasz Anforowicz 2017/02/02 01:06:16 This TODO is probably not that important. Things
517 clang::SourceLocation loc =
518 source_manager.getExpansionLoc(Node.getLocStart());
519
520 // TODO(lukasza): jump out of scratch space if token concatenation was used.
521 if (loc.isInvalid())
522 return false;
523
524 const clang::FileEntry* file_entry =
525 source_manager.getFileEntryForID(source_manager.getFileID(loc));
526 if (!file_entry)
527 return false;
528
529 static llvm::Regex exclusion_regex(kGeneratedFileExclusionRegex);
530 if (exclusion_regex.match(file_entry->getName()))
531 return false;
532
533 static llvm::Regex generated_file_regex(kGeneratedFileRegex);
534 return generated_file_regex.match(file_entry->getName());
535 }
536
500 // Helper to convert from a camelCaseName to camel_case_name. It uses some 537 // Helper to convert from a camelCaseName to camel_case_name. It uses some
501 // heuristics to try to handle acronyms in camel case names correctly. 538 // heuristics to try to handle acronyms in camel case names correctly.
502 std::string CamelCaseToUnderscoreCase(StringRef input) { 539 std::string CamelCaseToUnderscoreCase(StringRef input) {
503 std::string output; 540 std::string output;
504 bool needs_underscore = false; 541 bool needs_underscore = false;
505 bool was_lowercase = false; 542 bool was_lowercase = false;
506 bool was_uppercase = false; 543 bool was_uppercase = false;
507 bool first_char = true; 544 bool first_char = true;
508 // Iterate in reverse to minimize the amount of backtracking. 545 // Iterate in reverse to minimize the amount of backtracking.
509 for (const unsigned char* i = input.bytes_end() - 1; i >= input.bytes_begin(); 546 for (const unsigned char* i = input.bytes_end() - 1; i >= input.bytes_begin();
(...skipping 952 matching lines...) Expand 10 before | Expand all | Expand 10 after
1462 // void WTF::function() {} 1499 // void WTF::function() {}
1463 // void WTF::Class::method() {} 1500 // void WTF::Class::method() {}
1464 // matches |WTF::function| and |WTF::Class::method| decls. 1501 // matches |WTF::function| and |WTF::Class::method| decls.
1465 auto decl_has_qualifier_to_blink_namespace = 1502 auto decl_has_qualifier_to_blink_namespace =
1466 declaratorDecl(has(nestedNameSpecifier( 1503 declaratorDecl(has(nestedNameSpecifier(
1467 hasTopLevelPrefix(specifiesNamespace(blink_namespace_decl))))); 1504 hasTopLevelPrefix(specifiesNamespace(blink_namespace_decl)))));
1468 1505
1469 auto in_blink_namespace = decl( 1506 auto in_blink_namespace = decl(
1470 anyOf(decl_under_blink_namespace, decl_has_qualifier_to_blink_namespace, 1507 anyOf(decl_under_blink_namespace, decl_has_qualifier_to_blink_namespace,
1471 hasAncestor(decl_has_qualifier_to_blink_namespace)), 1508 hasAncestor(decl_has_qualifier_to_blink_namespace)),
1472 unless(hasCanonicalDecl(isExpansionInFileMatching(kGeneratedFileRegex)))); 1509 unless(hasCanonicalDecl(isDeclInGeneratedFile())));
1473 1510
1474 // Field, variable, and enum declarations ======== 1511 // Field, variable, and enum declarations ========
1475 // Given 1512 // Given
1476 // int x; 1513 // int x;
1477 // struct S { 1514 // struct S {
1478 // int y; 1515 // int y;
1479 // enum { VALUE }; 1516 // enum { VALUE };
1480 // }; 1517 // };
1481 // matches |x|, |y|, and |VALUE|. 1518 // matches |x|, |y|, and |VALUE|.
1482 auto field_decl_matcher = id("decl", fieldDecl(in_blink_namespace)); 1519 auto field_decl_matcher = id("decl", fieldDecl(in_blink_namespace));
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
1843 for (const auto& r : replacements) { 1880 for (const auto& r : replacements) {
1844 std::string replacement_text = r.getReplacementText().str(); 1881 std::string replacement_text = r.getReplacementText().str();
1845 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 1882 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
1846 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 1883 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
1847 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 1884 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
1848 } 1885 }
1849 llvm::outs() << "==== END EDITS ====\n"; 1886 llvm::outs() << "==== END EDITS ====\n";
1850 1887
1851 return 0; 1888 return 0;
1852 } 1889 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698