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 const char kBlinkFieldPrefix[] = "m_"; | 62 const char kBlinkFieldPrefix[] = "m_"; |
57 const char kBlinkStaticMemberPrefix[] = "s_"; | 63 const 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 | 69 |
64 if (const auto* method = clang::dyn_cast<const clang::CXXMethodDecl>(&decl)) { | 70 if (const auto* method = clang::dyn_cast<const clang::CXXMethodDecl>(&decl)) { |
65 if (!method->isStatic()) { | 71 if (!method->isStatic()) { |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 return clang::CharSourceRange::getTokenRange(expr.getLocation()); | 228 return clang::CharSourceRange::getTokenRange(expr.getLocation()); |
223 } | 229 } |
224 }; | 230 }; |
225 const char TargetNodeTraits<clang::DeclRefExpr>::kName[] = "expr"; | 231 const char TargetNodeTraits<clang::DeclRefExpr>::kName[] = "expr"; |
226 | 232 |
227 template <> | 233 template <> |
228 struct TargetNodeTraits<clang::CXXCtorInitializer> { | 234 struct TargetNodeTraits<clang::CXXCtorInitializer> { |
229 static const char kName[]; | 235 static const char kName[]; |
230 static clang::CharSourceRange GetRange( | 236 static clang::CharSourceRange GetRange( |
231 const clang::CXXCtorInitializer& init) { | 237 const clang::CXXCtorInitializer& init) { |
| 238 assert(init.isWritten()); |
232 return clang::CharSourceRange::getTokenRange(init.getSourceLocation()); | 239 return clang::CharSourceRange::getTokenRange(init.getSourceLocation()); |
233 } | 240 } |
234 }; | 241 }; |
235 const char TargetNodeTraits<clang::CXXCtorInitializer>::kName[] = "initializer"; | 242 const char TargetNodeTraits<clang::CXXCtorInitializer>::kName[] = "initializer"; |
236 | 243 |
237 template <typename DeclNode, typename TargetNode> | 244 template <typename DeclNode, typename TargetNode> |
238 class RewriterBase : public MatchFinder::MatchCallback { | 245 class RewriterBase : public MatchFinder::MatchCallback { |
239 public: | 246 public: |
240 explicit RewriterBase(Replacements* replacements) | 247 explicit RewriterBase(Replacements* replacements) |
241 : replacements_(replacements) {} | 248 : replacements_(replacements) {} |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 VarDeclRewriter var_decl_rewriter(&replacements); | 410 VarDeclRewriter var_decl_rewriter(&replacements); |
404 match_finder.addMatcher(var_decl_matcher, &var_decl_rewriter); | 411 match_finder.addMatcher(var_decl_matcher, &var_decl_rewriter); |
405 | 412 |
406 // Field and variable references ======== | 413 // Field and variable references ======== |
407 // Given | 414 // Given |
408 // bool x = true; | 415 // bool x = true; |
409 // if (x) { | 416 // if (x) { |
410 // ... | 417 // ... |
411 // } | 418 // } |
412 // matches |x| in if (x). | 419 // matches |x| in if (x). |
413 auto member_matcher = id("expr", memberExpr(member(field_decl_matcher))); | 420 auto member_matcher = id( |
| 421 "expr", |
| 422 memberExpr( |
| 423 member(field_decl_matcher), |
| 424 // Needed to avoid matching member references in functions (which will |
| 425 // be an ancestor of the member reference) synthesized by the |
| 426 // compiler, such as a synthesized copy constructor. |
| 427 // This skips explicitly defaulted functions as well, but that's OK: |
| 428 // there's nothing interesting to rewrite in those either. |
| 429 unless(hasAncestor(functionDecl(isDefaulted()))))); |
414 auto decl_ref_matcher = id("expr", declRefExpr(to(var_decl_matcher))); | 430 auto decl_ref_matcher = id("expr", declRefExpr(to(var_decl_matcher))); |
415 | 431 |
416 MemberRewriter member_rewriter(&replacements); | 432 MemberRewriter member_rewriter(&replacements); |
417 match_finder.addMatcher(member_matcher, &member_rewriter); | 433 match_finder.addMatcher(member_matcher, &member_rewriter); |
418 | 434 |
419 DeclRefRewriter decl_ref_rewriter(&replacements); | 435 DeclRefRewriter decl_ref_rewriter(&replacements); |
420 match_finder.addMatcher(decl_ref_matcher, &decl_ref_rewriter); | 436 match_finder.addMatcher(decl_ref_matcher, &decl_ref_rewriter); |
421 | 437 |
422 // Non-method function declarations ======== | 438 // Non-method function declarations ======== |
423 // Given | 439 // Given |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 | 535 |
520 // Initializers ======== | 536 // Initializers ======== |
521 // Given | 537 // Given |
522 // struct S { | 538 // struct S { |
523 // int x; | 539 // int x; |
524 // S() : x(2) {} | 540 // S() : x(2) {} |
525 // }; | 541 // }; |
526 // matches each initializer in the constructor for S. | 542 // matches each initializer in the constructor for S. |
527 auto constructor_initializer_matcher = | 543 auto constructor_initializer_matcher = |
528 cxxConstructorDecl(forEachConstructorInitializer( | 544 cxxConstructorDecl(forEachConstructorInitializer( |
529 id("initializer", cxxCtorInitializer(forField(field_decl_matcher))))); | 545 id("initializer", |
| 546 cxxCtorInitializer(forField(field_decl_matcher), isWritten())))); |
530 | 547 |
531 ConstructorInitializerRewriter constructor_initializer_rewriter( | 548 ConstructorInitializerRewriter constructor_initializer_rewriter( |
532 &replacements); | 549 &replacements); |
533 match_finder.addMatcher(constructor_initializer_matcher, | 550 match_finder.addMatcher(constructor_initializer_matcher, |
534 &constructor_initializer_rewriter); | 551 &constructor_initializer_rewriter); |
535 | 552 |
536 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = | 553 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = |
537 clang::tooling::newFrontendActionFactory(&match_finder); | 554 clang::tooling::newFrontendActionFactory(&match_finder); |
538 int result = tool.run(factory.get()); | 555 int result = tool.run(factory.get()); |
539 if (result != 0) | 556 if (result != 0) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 for (const auto& r : replacements) { | 592 for (const auto& r : replacements) { |
576 std::string replacement_text = r.getReplacementText().str(); | 593 std::string replacement_text = r.getReplacementText().str(); |
577 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 594 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
578 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 595 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
579 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 596 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
580 } | 597 } |
581 llvm::outs() << "==== END EDITS ====\n"; | 598 llvm::outs() << "==== END EDITS ====\n"; |
582 | 599 |
583 return 0; | 600 return 0; |
584 } | 601 } |
OLD | NEW |