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

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

Issue 2252853002: Replace clang::tooling::Replacements with std::set<clang::tooling::Replacement>. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Even works with Blink code now... Created 4 years, 4 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 25 matching lines...) Expand all
36 #if defined(_WIN32) 36 #if defined(_WIN32)
37 #include <windows.h> 37 #include <windows.h>
38 #else 38 #else
39 #include <sys/file.h> 39 #include <sys/file.h>
40 #include <unistd.h> 40 #include <unistd.h>
41 #endif 41 #endif
42 42
43 using namespace clang::ast_matchers; 43 using namespace clang::ast_matchers;
44 using clang::tooling::CommonOptionsParser; 44 using clang::tooling::CommonOptionsParser;
45 using clang::tooling::Replacement; 45 using clang::tooling::Replacement;
46 using clang::tooling::Replacements;
47 using llvm::StringRef; 46 using llvm::StringRef;
48 47
49 namespace { 48 namespace {
50 49
51 const char kBlinkFieldPrefix[] = "m_"; 50 const char kBlinkFieldPrefix[] = "m_";
52 const char kBlinkStaticMemberPrefix[] = "s_"; 51 const char kBlinkStaticMemberPrefix[] = "s_";
53 const char kGeneratedFileRegex[] = "^gen/|/gen/"; 52 const char kGeneratedFileRegex[] = "^gen/|/gen/";
54 53
55 const clang::ast_matchers::internal:: 54 const clang::ast_matchers::internal::
56 VariadicDynCastAllOfMatcher<clang::Expr, clang::UnresolvedMemberExpr> 55 VariadicDynCastAllOfMatcher<clang::Expr, clang::UnresolvedMemberExpr>
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 static clang::SourceLocation GetLoc(const clang::UnresolvedMemberExpr& expr) { 483 static clang::SourceLocation GetLoc(const clang::UnresolvedMemberExpr& expr) {
485 return expr.getMemberLoc(); 484 return expr.getMemberLoc();
486 } 485 }
487 static const char* GetName() { return "expr"; } 486 static const char* GetName() { return "expr"; }
488 static const char* GetType() { return "UnresolvedMemberExpr"; } 487 static const char* GetType() { return "UnresolvedMemberExpr"; }
489 }; 488 };
490 489
491 template <typename DeclNode, typename TargetNode> 490 template <typename DeclNode, typename TargetNode>
492 class RewriterBase : public MatchFinder::MatchCallback { 491 class RewriterBase : public MatchFinder::MatchCallback {
493 public: 492 public:
494 explicit RewriterBase(Replacements* replacements) 493 explicit RewriterBase(std::set<Replacement>* replacements)
495 : replacements_(replacements) {} 494 : replacements_(replacements) {}
496 495
497 void run(const MatchFinder::MatchResult& result) override { 496 void run(const MatchFinder::MatchResult& result) override {
498 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl"); 497 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl");
499 // If false, there's no name to be renamed. 498 // If false, there's no name to be renamed.
500 if (!decl->getIdentifier()) 499 if (!decl->getIdentifier())
501 return; 500 return;
502 clang::SourceLocation decl_loc = 501 clang::SourceLocation decl_loc =
503 TargetNodeTraits<clang::NamedDecl>::GetLoc(*decl); 502 TargetNodeTraits<clang::NamedDecl>::GetLoc(*decl);
504 if (decl_loc.isMacroID()) { 503 if (decl_loc.isMacroID()) {
(...skipping 21 matching lines...) Expand all
526 replacements_->emplace(*result.SourceManager, range, new_name); 525 replacements_->emplace(*result.SourceManager, range, new_name);
527 replacement_names_.emplace(old_name.str(), std::move(new_name)); 526 replacement_names_.emplace(old_name.str(), std::move(new_name));
528 } 527 }
529 528
530 const std::unordered_map<std::string, std::string>& replacement_names() 529 const std::unordered_map<std::string, std::string>& replacement_names()
531 const { 530 const {
532 return replacement_names_; 531 return replacement_names_;
533 } 532 }
534 533
535 private: 534 private:
536 Replacements* const replacements_; 535 std::set<Replacement>* const replacements_;
537 std::unordered_map<std::string, std::string> replacement_names_; 536 std::unordered_map<std::string, std::string> replacement_names_;
538 }; 537 };
539 538
540 using FieldDeclRewriter = RewriterBase<clang::FieldDecl, clang::NamedDecl>; 539 using FieldDeclRewriter = RewriterBase<clang::FieldDecl, clang::NamedDecl>;
541 using VarDeclRewriter = RewriterBase<clang::VarDecl, clang::NamedDecl>; 540 using VarDeclRewriter = RewriterBase<clang::VarDecl, clang::NamedDecl>;
542 using MemberRewriter = RewriterBase<clang::FieldDecl, clang::MemberExpr>; 541 using MemberRewriter = RewriterBase<clang::FieldDecl, clang::MemberExpr>;
543 using DeclRefRewriter = RewriterBase<clang::VarDecl, clang::DeclRefExpr>; 542 using DeclRefRewriter = RewriterBase<clang::VarDecl, clang::DeclRefExpr>;
544 using FieldDeclRefRewriter = RewriterBase<clang::FieldDecl, clang::DeclRefExpr>; 543 using FieldDeclRefRewriter = RewriterBase<clang::FieldDecl, clang::DeclRefExpr>;
545 using FunctionDeclRewriter = 544 using FunctionDeclRewriter =
546 RewriterBase<clang::FunctionDecl, clang::NamedDecl>; 545 RewriterBase<clang::FunctionDecl, clang::NamedDecl>;
(...skipping 29 matching lines...) Expand all
576 // http://llvm.org/bugs/show_bug.cgi?id=21627 575 // http://llvm.org/bugs/show_bug.cgi?id=21627
577 llvm::InitializeNativeTarget(); 576 llvm::InitializeNativeTarget();
578 llvm::InitializeNativeTargetAsmParser(); 577 llvm::InitializeNativeTargetAsmParser();
579 llvm::cl::OptionCategory category( 578 llvm::cl::OptionCategory category(
580 "rewrite_to_chrome_style: convert Blink style to Chrome style."); 579 "rewrite_to_chrome_style: convert Blink style to Chrome style.");
581 CommonOptionsParser options(argc, argv, category); 580 CommonOptionsParser options(argc, argv, category);
582 clang::tooling::ClangTool tool(options.getCompilations(), 581 clang::tooling::ClangTool tool(options.getCompilations(),
583 options.getSourcePathList()); 582 options.getSourcePathList());
584 583
585 MatchFinder match_finder; 584 MatchFinder match_finder;
586 Replacements replacements; 585 std::set<Replacement> replacements;
587 586
588 auto in_blink_namespace = 587 auto in_blink_namespace =
589 decl(hasAncestor(namespaceDecl(anyOf(hasName("blink"), hasName("WTF")), 588 decl(hasAncestor(namespaceDecl(anyOf(hasName("blink"), hasName("WTF")),
590 hasParent(translationUnitDecl()))), 589 hasParent(translationUnitDecl()))),
591 unless(isExpansionInFileMatching(kGeneratedFileRegex))); 590 unless(isExpansionInFileMatching(kGeneratedFileRegex)));
592 591
593 // Field, variable, and enum declarations ======== 592 // Field, variable, and enum declarations ========
594 // Given 593 // Given
595 // int x; 594 // int x;
596 // struct S { 595 // struct S {
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 for (const auto& r : replacements) { 879 for (const auto& r : replacements) {
881 std::string replacement_text = r.getReplacementText().str(); 880 std::string replacement_text = r.getReplacementText().str();
882 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 881 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
883 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 882 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
884 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 883 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
885 } 884 }
886 llvm::outs() << "==== END EDITS ====\n"; 885 llvm::outs() << "==== END EDITS ====\n";
887 886
888 return 0; 887 return 0;
889 } 888 }
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