| 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: |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 // This is available in newer clang revisions... but alas, Chrome has not rolled |
| 57 // that far yet. |
| 58 AST_MATCHER(clang::FunctionDecl, isDefaulted) { |
| 59 return Node.isDefaulted(); |
| 60 } |
| 61 |
| 56 constexpr char kBlinkFieldPrefix[] = "m_"; | 62 constexpr char kBlinkFieldPrefix[] = "m_"; |
| 57 constexpr char kBlinkStaticMemberPrefix[] = "s_"; | 63 constexpr char kBlinkStaticMemberPrefix[] = "s_"; |
| 58 | 64 |
| 59 bool GetNameForDecl(const clang::FunctionDecl& decl, | 65 bool GetNameForDecl(const clang::FunctionDecl& decl, |
| 60 const clang::ASTContext& context, | 66 const clang::ASTContext& context, |
| 61 std::string& name) { | 67 std::string& name) { |
| 62 name = decl.getNameAsString(); | 68 name = decl.getNameAsString(); |
| 63 name[0] = clang::toUppercase(name[0]); | 69 name[0] = clang::toUppercase(name[0]); |
| 64 return true; | 70 return true; |
| 65 } | 71 } |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 return clang::CharSourceRange::getTokenRange(expr.getLocation()); | 209 return clang::CharSourceRange::getTokenRange(expr.getLocation()); |
| 204 } | 210 } |
| 205 }; | 211 }; |
| 206 constexpr char TargetNodeTraits<clang::DeclRefExpr>::kName[]; | 212 constexpr char TargetNodeTraits<clang::DeclRefExpr>::kName[]; |
| 207 | 213 |
| 208 template <> | 214 template <> |
| 209 struct TargetNodeTraits<clang::CXXCtorInitializer> { | 215 struct TargetNodeTraits<clang::CXXCtorInitializer> { |
| 210 static constexpr char kName[] = "initializer"; | 216 static constexpr char kName[] = "initializer"; |
| 211 static clang::CharSourceRange GetRange( | 217 static clang::CharSourceRange GetRange( |
| 212 const clang::CXXCtorInitializer& init) { | 218 const clang::CXXCtorInitializer& init) { |
| 219 assert(init.isWritten()); |
| 213 return clang::CharSourceRange::getTokenRange(init.getSourceLocation()); | 220 return clang::CharSourceRange::getTokenRange(init.getSourceLocation()); |
| 214 } | 221 } |
| 215 }; | 222 }; |
| 216 constexpr char TargetNodeTraits<clang::CXXCtorInitializer>::kName[]; | 223 constexpr char TargetNodeTraits<clang::CXXCtorInitializer>::kName[]; |
| 217 | 224 |
| 218 template <typename DeclNode, typename TargetNode> | 225 template <typename DeclNode, typename TargetNode> |
| 219 class RewriterBase : public MatchFinder::MatchCallback { | 226 class RewriterBase : public MatchFinder::MatchCallback { |
| 220 public: | 227 public: |
| 221 explicit RewriterBase(Replacements* replacements) | 228 explicit RewriterBase(Replacements* replacements) |
| 222 : replacements_(replacements) {} | 229 : replacements_(replacements) {} |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 VarDeclRewriter var_decl_rewriter(&replacements); | 390 VarDeclRewriter var_decl_rewriter(&replacements); |
| 384 match_finder.addMatcher(var_decl_matcher, &var_decl_rewriter); | 391 match_finder.addMatcher(var_decl_matcher, &var_decl_rewriter); |
| 385 | 392 |
| 386 // Field and variable references ======== | 393 // Field and variable references ======== |
| 387 // Given | 394 // Given |
| 388 // bool x = true; | 395 // bool x = true; |
| 389 // if (x) { | 396 // if (x) { |
| 390 // ... | 397 // ... |
| 391 // } | 398 // } |
| 392 // matches |x| in if (x). | 399 // matches |x| in if (x). |
| 393 auto member_matcher = id("expr", memberExpr(member(field_decl_matcher))); | 400 auto member_matcher = id( |
| 401 "expr", |
| 402 memberExpr( |
| 403 member(field_decl_matcher), |
| 404 // Needed to avoid matching member references in functions synthesized |
| 405 // by the compiler, such as a synthesized copy constructor. |
| 406 // This skips explicitly defaulted functions as well, but that's OK: |
| 407 // there's nothing interesting to rewrite in those either. |
| 408 unless(hasAncestor(functionDecl(isDefaulted()))))); |
| 394 auto decl_ref_matcher = id("expr", declRefExpr(to(var_decl_matcher))); | 409 auto decl_ref_matcher = id("expr", declRefExpr(to(var_decl_matcher))); |
| 395 | 410 |
| 396 MemberRewriter member_rewriter(&replacements); | 411 MemberRewriter member_rewriter(&replacements); |
| 397 match_finder.addMatcher(member_matcher, &member_rewriter); | 412 match_finder.addMatcher(member_matcher, &member_rewriter); |
| 398 | 413 |
| 399 DeclRefRewriter decl_ref_rewriter(&replacements); | 414 DeclRefRewriter decl_ref_rewriter(&replacements); |
| 400 match_finder.addMatcher(decl_ref_matcher, &decl_ref_rewriter); | 415 match_finder.addMatcher(decl_ref_matcher, &decl_ref_rewriter); |
| 401 | 416 |
| 402 // Non-method function declarations ======== | 417 // Non-method function declarations ======== |
| 403 // Given | 418 // Given |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 | 514 |
| 500 // Initializers ======== | 515 // Initializers ======== |
| 501 // Given | 516 // Given |
| 502 // struct S { | 517 // struct S { |
| 503 // int x; | 518 // int x; |
| 504 // S() : x(2) {} | 519 // S() : x(2) {} |
| 505 // }; | 520 // }; |
| 506 // matches each initializer in the constructor for S. | 521 // matches each initializer in the constructor for S. |
| 507 auto constructor_initializer_matcher = | 522 auto constructor_initializer_matcher = |
| 508 cxxConstructorDecl(forEachConstructorInitializer( | 523 cxxConstructorDecl(forEachConstructorInitializer( |
| 509 id("initializer", cxxCtorInitializer(forField(field_decl_matcher))))); | 524 id("initializer", |
| 525 cxxCtorInitializer(forField(field_decl_matcher), isWritten())))); |
| 510 | 526 |
| 511 ConstructorInitializerRewriter constructor_initializer_rewriter( | 527 ConstructorInitializerRewriter constructor_initializer_rewriter( |
| 512 &replacements); | 528 &replacements); |
| 513 match_finder.addMatcher(constructor_initializer_matcher, | 529 match_finder.addMatcher(constructor_initializer_matcher, |
| 514 &constructor_initializer_rewriter); | 530 &constructor_initializer_rewriter); |
| 515 | 531 |
| 516 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = | 532 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = |
| 517 clang::tooling::newFrontendActionFactory(&match_finder); | 533 clang::tooling::newFrontendActionFactory(&match_finder); |
| 518 int result = tool.run(factory.get()); | 534 int result = tool.run(factory.get()); |
| 519 if (result != 0) | 535 if (result != 0) |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 for (const auto& r : replacements) { | 571 for (const auto& r : replacements) { |
| 556 std::string replacement_text = r.getReplacementText().str(); | 572 std::string replacement_text = r.getReplacementText().str(); |
| 557 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 573 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 558 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 574 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 559 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 575 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 560 } | 576 } |
| 561 llvm::outs() << "==== END EDITS ====\n"; | 577 llvm::outs() << "==== END EDITS ====\n"; |
| 562 | 578 |
| 563 return 0; | 579 return 0; |
| 564 } | 580 } |
| OLD | NEW |