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

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

Issue 2597863002: rewrite_to_chrome_style: associate replacements with the affected file (Closed)
Patch Set: . Created 3 years, 12 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
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:
11 // const int maxThings => const int kMaxThings 11 // const int maxThings => const int kMaxThings
12 // free functions and methods: 12 // free functions and methods:
13 // void doThisThenThat() => void DoThisAndThat() 13 // void doThisThenThat() => void DoThisAndThat()
14 14
15 #include <assert.h> 15 #include <assert.h>
16 #include <algorithm> 16 #include <algorithm>
17 #include <fstream>
18 #include <memory> 17 #include <memory>
18 #include <set>
19 #include <string> 19 #include <string>
20 #include <unordered_map>
21 20
22 #include "clang/AST/ASTContext.h" 21 #include "clang/AST/ASTContext.h"
23 #include "clang/ASTMatchers/ASTMatchFinder.h" 22 #include "clang/ASTMatchers/ASTMatchFinder.h"
24 #include "clang/ASTMatchers/ASTMatchers.h" 23 #include "clang/ASTMatchers/ASTMatchers.h"
25 #include "clang/ASTMatchers/ASTMatchersMacros.h" 24 #include "clang/ASTMatchers/ASTMatchersMacros.h"
26 #include "clang/Basic/CharInfo.h" 25 #include "clang/Basic/CharInfo.h"
27 #include "clang/Basic/SourceManager.h" 26 #include "clang/Basic/SourceManager.h"
28 #include "clang/Frontend/FrontendActions.h" 27 #include "clang/Frontend/FrontendActions.h"
29 #include "clang/Lex/Lexer.h" 28 #include "clang/Lex/Lexer.h"
30 #include "clang/Tooling/CommonOptionsParser.h" 29 #include "clang/Tooling/CommonOptionsParser.h"
31 #include "clang/Tooling/Refactoring.h" 30 #include "clang/Tooling/Refactoring.h"
32 #include "clang/Tooling/Tooling.h" 31 #include "clang/Tooling/Tooling.h"
33 #include "llvm/Support/CommandLine.h" 32 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/TargetSelect.h" 33 #include "llvm/Support/TargetSelect.h"
35 34
36 #if defined(_WIN32) 35 #include "EditTracker.h"
37 #include <windows.h>
38 #else
39 #include <sys/file.h>
40 #include <unistd.h>
41 #endif
42 36
43 using namespace clang::ast_matchers; 37 using namespace clang::ast_matchers;
44 using clang::tooling::CommonOptionsParser; 38 using clang::tooling::CommonOptionsParser;
45 using clang::tooling::Replacement; 39 using clang::tooling::Replacement;
46 using llvm::StringRef; 40 using llvm::StringRef;
47 41
48 namespace { 42 namespace {
49 43
50 const char kBlinkFieldPrefix[] = "m_"; 44 const char kBlinkFieldPrefix[] = "m_";
51 const char kBlinkStaticMemberPrefix[] = "s_"; 45 const char kBlinkStaticMemberPrefix[] = "s_";
(...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after
687 } 681 }
688 682
689 void AddReplacement(const MatchFinder::MatchResult& result, 683 void AddReplacement(const MatchFinder::MatchResult& result,
690 llvm::StringRef old_name, 684 llvm::StringRef old_name,
691 std::string new_name) { 685 std::string new_name) {
692 if (old_name == new_name) 686 if (old_name == new_name)
693 return; 687 return;
694 688
695 clang::SourceLocation loc = 689 clang::SourceLocation loc =
696 TargetNodeTraits<TargetNode>::GetLoc(GetTargetNode(result)); 690 TargetNodeTraits<TargetNode>::GetLoc(GetTargetNode(result));
691 edit_tracker_.Add(*result.SourceManager, loc, old_name, new_name);
692
697 clang::CharSourceRange range = clang::CharSourceRange::getTokenRange(loc); 693 clang::CharSourceRange range = clang::CharSourceRange::getTokenRange(loc);
698 replacements_->emplace(*result.SourceManager, range, new_name); 694 replacements_->emplace(*result.SourceManager, range, new_name);
699 replacement_names_.emplace(old_name.str(), std::move(new_name));
700 } 695 }
701 696
702 const std::unordered_map<std::string, std::string>& replacement_names() 697 const EditTracker& edit_tracker() const { return edit_tracker_; }
703 const {
704 return replacement_names_;
705 }
706 698
707 private: 699 private:
708 std::set<Replacement>* const replacements_; 700 std::set<Replacement>* const replacements_;
709 std::unordered_map<std::string, std::string> replacement_names_; 701 EditTracker edit_tracker_;
710 }; 702 };
711 703
712 template <typename DeclNode, typename TargetNode> 704 template <typename DeclNode, typename TargetNode>
713 class DeclRewriterBase : public RewriterBase<TargetNode> { 705 class DeclRewriterBase : public RewriterBase<TargetNode> {
714 public: 706 public:
715 using Base = RewriterBase<TargetNode>; 707 using Base = RewriterBase<TargetNode>;
716 708
717 explicit DeclRewriterBase(std::set<Replacement>* replacements) 709 explicit DeclRewriterBase(std::set<Replacement>* replacements)
718 : Base(replacements) {} 710 : Base(replacements) {}
719 711
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after
1280 &replacements); 1272 &replacements);
1281 match_finder.addMatcher(cxx_dependent_scope_member_expr_matcher, 1273 match_finder.addMatcher(cxx_dependent_scope_member_expr_matcher,
1282 &cxx_dependent_scope_member_expr_rewriter); 1274 &cxx_dependent_scope_member_expr_rewriter);
1283 1275
1284 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = 1276 std::unique_ptr<clang::tooling::FrontendActionFactory> factory =
1285 clang::tooling::newFrontendActionFactory(&match_finder); 1277 clang::tooling::newFrontendActionFactory(&match_finder);
1286 int result = tool.run(factory.get()); 1278 int result = tool.run(factory.get());
1287 if (result != 0) 1279 if (result != 0)
1288 return result; 1280 return result;
1289 1281
1290 #if defined(_WIN32) 1282 // Supplemental data for the Blink rename rebase helper.
1291 HANDLE lockfd = CreateFile("rewrite-sym.lock", GENERIC_READ, FILE_SHARE_READ, 1283 // TODO(dcheng): There's a lot of match rewriters missing from this list.
1292 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 1284 llvm::outs() << "==== BEGIN TRACKED EDITS ====\n";
1293 OVERLAPPED overlapped = {}; 1285 field_decl_rewriter.edit_tracker().SerializeTo("var", llvm::outs());
1294 LockFileEx(lockfd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &overlapped); 1286 var_decl_rewriter.edit_tracker().SerializeTo("var", llvm::outs());
1295 #else 1287 enum_member_decl_rewriter.edit_tracker().SerializeTo("enu", llvm::outs());
1296 int lockfd = open("rewrite-sym.lock", O_RDWR | O_CREAT, 0666); 1288 function_decl_rewriter.edit_tracker().SerializeTo("fun", llvm::outs());
1297 while (flock(lockfd, LOCK_EX)) { // :D 1289 method_decl_rewriter.edit_tracker().SerializeTo("fun", llvm::outs());
1298 } 1290 llvm::outs() << "==== END TRACKED EDITS ====\n";
1299 #endif
1300
1301 std::ofstream replacement_db_file("rewrite-sym.txt",
1302 std::ios_base::out | std::ios_base::app);
1303 for (const auto& p : field_decl_rewriter.replacement_names())
1304 replacement_db_file << "var:" << p.first << ":" << p.second << "\n";
1305 for (const auto& p : var_decl_rewriter.replacement_names())
1306 replacement_db_file << "var:" << p.first << ":" << p.second << "\n";
1307 for (const auto& p : enum_member_decl_rewriter.replacement_names())
1308 replacement_db_file << "enu:" << p.first << ":" << p.second << "\n";
1309 for (const auto& p : function_decl_rewriter.replacement_names())
1310 replacement_db_file << "fun:" << p.first << ":" << p.second << "\n";
1311 for (const auto& p : method_decl_rewriter.replacement_names())
1312 replacement_db_file << "fun:" << p.first << ":" << p.second << "\n";
1313 replacement_db_file.close();
1314
1315 #if defined(_WIN32)
1316 UnlockFileEx(lockfd, 0, 1, 0, &overlapped);
1317 CloseHandle(lockfd);
1318 #else
1319 flock(lockfd, LOCK_UN);
1320 close(lockfd);
1321 #endif
1322 1291
1323 // Serialization format is documented in tools/clang/scripts/run_tool.py 1292 // Serialization format is documented in tools/clang/scripts/run_tool.py
1324 llvm::outs() << "==== BEGIN EDITS ====\n"; 1293 llvm::outs() << "==== BEGIN EDITS ====\n";
1325 for (const auto& r : replacements) { 1294 for (const auto& r : replacements) {
1326 std::string replacement_text = r.getReplacementText().str(); 1295 std::string replacement_text = r.getReplacementText().str();
1327 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 1296 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
1328 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 1297 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
1329 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 1298 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
1330 } 1299 }
1331 llvm::outs() << "==== END EDITS ====\n"; 1300 llvm::outs() << "==== END EDITS ====\n";
1332 1301
1333 return 0; 1302 return 0;
1334 } 1303 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698