| 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: |
| 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> | 17 #include <fstream> |
| 18 #include <memory> | 18 #include <memory> |
| 19 #include <string> | 19 #include <string> |
| 20 #include <unordered_map> | 20 #include <unordered_map> |
| 21 #include <unordered_set> | 21 #include <unordered_set> |
| 22 | 22 |
| 23 #if defined(_WIN32) | |
| 24 #include <windows.h> | |
| 25 #else | |
| 26 #include <sys/file.h> | |
| 27 #include <unistd.h> | |
| 28 #endif | |
| 29 | |
| 30 #include "clang/AST/ASTContext.h" | 23 #include "clang/AST/ASTContext.h" |
| 31 #include "clang/ASTMatchers/ASTMatchFinder.h" | 24 #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 32 #include "clang/ASTMatchers/ASTMatchers.h" | 25 #include "clang/ASTMatchers/ASTMatchers.h" |
| 33 #include "clang/ASTMatchers/ASTMatchersMacros.h" | 26 #include "clang/ASTMatchers/ASTMatchersMacros.h" |
| 34 #include "clang/Basic/CharInfo.h" | 27 #include "clang/Basic/CharInfo.h" |
| 35 #include "clang/Basic/SourceManager.h" | 28 #include "clang/Basic/SourceManager.h" |
| 36 #include "clang/Frontend/FrontendActions.h" | 29 #include "clang/Frontend/FrontendActions.h" |
| 37 #include "clang/Lex/Lexer.h" | 30 #include "clang/Lex/Lexer.h" |
| 38 #include "clang/Tooling/CommonOptionsParser.h" | 31 #include "clang/Tooling/CommonOptionsParser.h" |
| 39 #include "clang/Tooling/Refactoring.h" | 32 #include "clang/Tooling/Refactoring.h" |
| 40 #include "clang/Tooling/Tooling.h" | 33 #include "clang/Tooling/Tooling.h" |
| 41 #include "llvm/Support/CommandLine.h" | 34 #include "llvm/Support/CommandLine.h" |
| 42 #include "llvm/Support/TargetSelect.h" | 35 #include "llvm/Support/TargetSelect.h" |
| 43 | 36 |
| 37 #if defined(_WIN32) |
| 38 #include <windows.h> |
| 39 #else |
| 40 #include <sys/file.h> |
| 41 #include <unistd.h> |
| 42 #endif |
| 43 |
| 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 clang::tooling::Replacements; | 47 using clang::tooling::Replacements; |
| 48 using llvm::StringRef; | 48 using llvm::StringRef; |
| 49 | 49 |
| 50 namespace { | 50 namespace { |
| 51 | 51 |
| 52 AST_MATCHER(clang::FunctionDecl, isOverloadedOperator) { | 52 AST_MATCHER(clang::FunctionDecl, isOverloadedOperator) { |
| 53 return Node.isOverloadedOperator(); | 53 return Node.isOverloadedOperator(); |
| 54 } | 54 } |
| 55 | 55 |
| 56 constexpr char kBlinkFieldPrefix[] = "m_"; | 56 const char kBlinkFieldPrefix[] = "m_"; |
| 57 constexpr char kBlinkStaticMemberPrefix[] = "s_"; | 57 const char kBlinkStaticMemberPrefix[] = "s_"; |
| 58 | 58 |
| 59 bool GetNameForDecl(const clang::FunctionDecl& decl, | 59 bool GetNameForDecl(const clang::FunctionDecl& decl, |
| 60 const clang::ASTContext& context, | 60 const clang::ASTContext& context, |
| 61 std::string& name) { | 61 std::string& name) { |
| 62 name = decl.getNameAsString(); | 62 name = decl.getNameAsString(); |
| 63 name[0] = clang::toUppercase(name[0]); | 63 name[0] = clang::toUppercase(name[0]); |
| 64 return true; | 64 return true; |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Helper to convert from a camelCaseName to camel_case_name. It uses some | 67 // Helper to convert from a camelCaseName to camel_case_name. It uses some |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 } | 173 } |
| 174 | 174 |
| 175 return true; | 175 return true; |
| 176 } | 176 } |
| 177 | 177 |
| 178 template <typename Type> | 178 template <typename Type> |
| 179 struct TargetNodeTraits; | 179 struct TargetNodeTraits; |
| 180 | 180 |
| 181 template <> | 181 template <> |
| 182 struct TargetNodeTraits<clang::NamedDecl> { | 182 struct TargetNodeTraits<clang::NamedDecl> { |
| 183 static constexpr char kName[] = "decl"; | 183 static const char kName[]; |
| 184 static clang::CharSourceRange GetRange(const clang::NamedDecl& decl) { | 184 static clang::CharSourceRange GetRange(const clang::NamedDecl& decl) { |
| 185 return clang::CharSourceRange::getTokenRange(decl.getLocation()); | 185 return clang::CharSourceRange::getTokenRange(decl.getLocation()); |
| 186 } | 186 } |
| 187 }; | 187 }; |
| 188 constexpr char TargetNodeTraits<clang::NamedDecl>::kName[]; | 188 const char TargetNodeTraits<clang::NamedDecl>::kName[] = "decl"; |
| 189 | 189 |
| 190 template <> | 190 template <> |
| 191 struct TargetNodeTraits<clang::MemberExpr> { | 191 struct TargetNodeTraits<clang::MemberExpr> { |
| 192 static constexpr char kName[] = "expr"; | 192 static const char kName[]; |
| 193 static clang::CharSourceRange GetRange(const clang::MemberExpr& expr) { | 193 static clang::CharSourceRange GetRange(const clang::MemberExpr& expr) { |
| 194 return clang::CharSourceRange::getTokenRange(expr.getMemberLoc()); | 194 return clang::CharSourceRange::getTokenRange(expr.getMemberLoc()); |
| 195 } | 195 } |
| 196 }; | 196 }; |
| 197 constexpr char TargetNodeTraits<clang::MemberExpr>::kName[]; | 197 const char TargetNodeTraits<clang::MemberExpr>::kName[] = "expr"; |
| 198 | 198 |
| 199 template <> | 199 template <> |
| 200 struct TargetNodeTraits<clang::DeclRefExpr> { | 200 struct TargetNodeTraits<clang::DeclRefExpr> { |
| 201 static constexpr char kName[] = "expr"; | 201 static const char kName[]; |
| 202 static clang::CharSourceRange GetRange(const clang::DeclRefExpr& expr) { | 202 static clang::CharSourceRange GetRange(const clang::DeclRefExpr& expr) { |
| 203 return clang::CharSourceRange::getTokenRange(expr.getLocation()); | 203 return clang::CharSourceRange::getTokenRange(expr.getLocation()); |
| 204 } | 204 } |
| 205 }; | 205 }; |
| 206 constexpr char TargetNodeTraits<clang::DeclRefExpr>::kName[]; | 206 const char TargetNodeTraits<clang::DeclRefExpr>::kName[] = "expr"; |
| 207 | 207 |
| 208 template <> | 208 template <> |
| 209 struct TargetNodeTraits<clang::CXXCtorInitializer> { | 209 struct TargetNodeTraits<clang::CXXCtorInitializer> { |
| 210 static constexpr char kName[] = "initializer"; | 210 static const char kName[]; |
| 211 static clang::CharSourceRange GetRange( | 211 static clang::CharSourceRange GetRange( |
| 212 const clang::CXXCtorInitializer& init) { | 212 const clang::CXXCtorInitializer& init) { |
| 213 return clang::CharSourceRange::getTokenRange(init.getSourceLocation()); | 213 return clang::CharSourceRange::getTokenRange(init.getSourceLocation()); |
| 214 } | 214 } |
| 215 }; | 215 }; |
| 216 constexpr char TargetNodeTraits<clang::CXXCtorInitializer>::kName[]; | 216 const char TargetNodeTraits<clang::CXXCtorInitializer>::kName[] = "initializer"; |
| 217 | 217 |
| 218 template <typename DeclNode, typename TargetNode> | 218 template <typename DeclNode, typename TargetNode> |
| 219 class RewriterBase : public MatchFinder::MatchCallback { | 219 class RewriterBase : public MatchFinder::MatchCallback { |
| 220 public: | 220 public: |
| 221 explicit RewriterBase(Replacements* replacements) | 221 explicit RewriterBase(Replacements* replacements) |
| 222 : replacements_(replacements) {} | 222 : replacements_(replacements) {} |
| 223 | 223 |
| 224 void run(const MatchFinder::MatchResult& result) override { | 224 void run(const MatchFinder::MatchResult& result) override { |
| 225 std::string name; | 225 std::string name; |
| 226 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl"); | 226 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl"); |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 match_finder.addMatcher(constructor_initializer_matcher, | 513 match_finder.addMatcher(constructor_initializer_matcher, |
| 514 &constructor_initializer_rewriter); | 514 &constructor_initializer_rewriter); |
| 515 | 515 |
| 516 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = | 516 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = |
| 517 clang::tooling::newFrontendActionFactory(&match_finder); | 517 clang::tooling::newFrontendActionFactory(&match_finder); |
| 518 int result = tool.run(factory.get()); | 518 int result = tool.run(factory.get()); |
| 519 if (result != 0) | 519 if (result != 0) |
| 520 return result; | 520 return result; |
| 521 | 521 |
| 522 #if defined(_WIN32) | 522 #if defined(_WIN32) |
| 523 HFILE lockfd = CreateFile("rewrite-sym.lock", GENERIC_READ, FILE_SHARE_READ, | 523 HANDLE lockfd = CreateFile("rewrite-sym.lock", GENERIC_READ, FILE_SHARE_READ, |
| 524 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | 524 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 525 OVERLAPPED overlapped = {}; | 525 OVERLAPPED overlapped = {}; |
| 526 LockFileEx(lockfd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &overlapped); | 526 LockFileEx(lockfd, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &overlapped); |
| 527 #else | 527 #else |
| 528 int lockfd = open("rewrite-sym.lock", O_RDWR | O_CREAT, 0666); | 528 int lockfd = open("rewrite-sym.lock", O_RDWR | O_CREAT, 0666); |
| 529 while (flock(lockfd, LOCK_EX)) { // :D | 529 while (flock(lockfd, LOCK_EX)) { // :D |
| 530 } | 530 } |
| 531 #endif | 531 #endif |
| 532 | 532 |
| 533 std::ofstream replacement_db_file("rewrite-sym.txt", | 533 std::ofstream replacement_db_file("rewrite-sym.txt", |
| 534 std::ios_base::out | std::ios_base::app); | 534 std::ios_base::out | std::ios_base::app); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 555 for (const auto& r : replacements) { | 555 for (const auto& r : replacements) { |
| 556 std::string replacement_text = r.getReplacementText().str(); | 556 std::string replacement_text = r.getReplacementText().str(); |
| 557 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 557 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 558 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 558 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 559 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 559 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 560 } | 560 } |
| 561 llvm::outs() << "==== END EDITS ====\n"; | 561 llvm::outs() << "==== END EDITS ====\n"; |
| 562 | 562 |
| 563 return 0; | 563 return 0; |
| 564 } | 564 } |
| OLD | NEW |