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

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: Rebasing... 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 437 matching lines...) Expand 10 before | Expand all | Expand 10 after
501 503
502 bool IsKnownTraitName(clang::StringRef name) { 504 bool IsKnownTraitName(clang::StringRef name) {
503 // This set of names is globally a type trait throughout chromium. 505 // This set of names is globally a type trait throughout chromium.
504 return name == "safeToCompareToEmptyOrDeleted"; 506 return name == "safeToCompareToEmptyOrDeleted";
505 } 507 }
506 508
507 AST_MATCHER(clang::VarDecl, isKnownTraitName) { 509 AST_MATCHER(clang::VarDecl, isKnownTraitName) {
508 return Node.getDeclName().isIdentifier() && IsKnownTraitName(Node.getName()); 510 return Node.getDeclName().isIdentifier() && IsKnownTraitName(Node.getName());
509 } 511 }
510 512
513 AST_MATCHER(clang::Decl, isDeclInGeneratedFile) {
514 // This matcher mimics the built-in isExpansionInFileMatching matcher from
515 // llvm/tools/clang/include/clang/ASTMatchers/ASTMatchers.h, except:
516 // - It special cases some files (e.g. doesn't skip renaming of identifiers
517 // from gen/blink/core/ComputedStyleBase.h)
518
519 const clang::SourceManager& source_manager =
520 Node.getASTContext().getSourceManager();
521
522 // TODO(lukasza): Consider using getSpellingLoc below.
523 // The built-in isExpansionInFileMatching matcher uses getExpansionLoc below.
524 // We could consider using getSpellingLoc (which properly handles things like
525 // SETTINGS_GETTERS_AND_SETTERS macro which is defined in generated code
526 // (gen/blink/core/SettingsMacros.h), but expanded in non-generated code
527 // (third_party/WebKit/Source/core/frame/Settings.h).
528 clang::SourceLocation loc =
529 source_manager.getExpansionLoc(Node.getLocStart());
530
531 // TODO(lukasza): jump out of scratch space if token concatenation was used.
532 if (loc.isInvalid())
533 return false;
534
535 const clang::FileEntry* file_entry =
536 source_manager.getFileEntryForID(source_manager.getFileID(loc));
537 if (!file_entry)
538 return false;
539
540 static llvm::Regex exclusion_regex(kGeneratedFileExclusionRegex);
541 if (exclusion_regex.match(file_entry->getName()))
542 return false;
543
544 static llvm::Regex generated_file_regex(kGeneratedFileRegex);
545 return generated_file_regex.match(file_entry->getName());
546 }
547
511 // Helper to convert from a camelCaseName to camel_case_name. It uses some 548 // Helper to convert from a camelCaseName to camel_case_name. It uses some
512 // heuristics to try to handle acronyms in camel case names correctly. 549 // heuristics to try to handle acronyms in camel case names correctly.
513 std::string CamelCaseToUnderscoreCase(StringRef input) { 550 std::string CamelCaseToUnderscoreCase(StringRef input) {
514 std::string output; 551 std::string output;
515 bool needs_underscore = false; 552 bool needs_underscore = false;
516 bool was_lowercase = false; 553 bool was_lowercase = false;
517 bool was_uppercase = false; 554 bool was_uppercase = false;
518 bool first_char = true; 555 bool first_char = true;
519 // Iterate in reverse to minimize the amount of backtracking. 556 // Iterate in reverse to minimize the amount of backtracking.
520 for (const unsigned char* i = input.bytes_end() - 1; i >= input.bytes_begin(); 557 for (const unsigned char* i = input.bytes_end() - 1; i >= input.bytes_begin();
(...skipping 988 matching lines...) Expand 10 before | Expand all | Expand 10 after
1509 // void WTF::function() {} 1546 // void WTF::function() {}
1510 // void WTF::Class::method() {} 1547 // void WTF::Class::method() {}
1511 // matches |WTF::function| and |WTF::Class::method| decls. 1548 // matches |WTF::function| and |WTF::Class::method| decls.
1512 auto decl_has_qualifier_to_blink_namespace = 1549 auto decl_has_qualifier_to_blink_namespace =
1513 declaratorDecl(has(nestedNameSpecifier( 1550 declaratorDecl(has(nestedNameSpecifier(
1514 hasTopLevelPrefix(specifiesNamespace(blink_namespace_decl))))); 1551 hasTopLevelPrefix(specifiesNamespace(blink_namespace_decl)))));
1515 1552
1516 auto in_blink_namespace = decl( 1553 auto in_blink_namespace = decl(
1517 anyOf(decl_under_blink_namespace, decl_has_qualifier_to_blink_namespace, 1554 anyOf(decl_under_blink_namespace, decl_has_qualifier_to_blink_namespace,
1518 hasAncestor(decl_has_qualifier_to_blink_namespace)), 1555 hasAncestor(decl_has_qualifier_to_blink_namespace)),
1519 unless(hasCanonicalDecl(isExpansionInFileMatching(kGeneratedFileRegex)))); 1556 unless(hasCanonicalDecl(isDeclInGeneratedFile())));
1520 1557
1521 // Field, variable, and enum declarations ======== 1558 // Field, variable, and enum declarations ========
1522 // Given 1559 // Given
1523 // int x; 1560 // int x;
1524 // struct S { 1561 // struct S {
1525 // int y; 1562 // int y;
1526 // enum { VALUE }; 1563 // enum { VALUE };
1527 // }; 1564 // };
1528 // matches |x|, |y|, and |VALUE|. 1565 // matches |x|, |y|, and |VALUE|.
1529 auto field_decl_matcher = id("decl", fieldDecl(in_blink_namespace)); 1566 auto field_decl_matcher = id("decl", fieldDecl(in_blink_namespace));
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
1909 for (const auto& r : replacements) { 1946 for (const auto& r : replacements) {
1910 std::string replacement_text = r.getReplacementText().str(); 1947 std::string replacement_text = r.getReplacementText().str();
1911 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 1948 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
1912 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 1949 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
1913 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 1950 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
1914 } 1951 }
1915 llvm::outs() << "==== END EDITS ====\n"; 1952 llvm::outs() << "==== END EDITS ====\n";
1916 1953
1917 return 0; 1954 return 0;
1918 } 1955 }
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