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 = |
401 id("expr", | |
402 // Needed to avoid matching member references in functions synthesized | |
danakj
2016/01/28 23:08:20
can you put this comment on the unless part?
dcheng
2016/01/28 23:15:43
Done.
| |
403 // by the compiler, such as a synthesized copy constructor. | |
404 // This skips explicitly defaulted functions as well, but that's OK: | |
405 // there's nothing interesting to rewrite in those either. | |
406 memberExpr(member(field_decl_matcher), | |
407 unless(hasAncestor(functionDecl(isDefaulted()))))); | |
danakj
2016/01/28 23:08:20
why the ancestor part of this? (maybe add to comme
dcheng
2016/01/28 23:15:43
Hm... that's kind of implicit in "member reference
danakj
2016/01/29 00:16:55
oh the ancestor is the function, this is the membe
dcheng
2016/01/29 01:17:30
Done.
| |
394 auto decl_ref_matcher = id("expr", declRefExpr(to(var_decl_matcher))); | 408 auto decl_ref_matcher = id("expr", declRefExpr(to(var_decl_matcher))); |
395 | 409 |
396 MemberRewriter member_rewriter(&replacements); | 410 MemberRewriter member_rewriter(&replacements); |
397 match_finder.addMatcher(member_matcher, &member_rewriter); | 411 match_finder.addMatcher(member_matcher, &member_rewriter); |
398 | 412 |
399 DeclRefRewriter decl_ref_rewriter(&replacements); | 413 DeclRefRewriter decl_ref_rewriter(&replacements); |
400 match_finder.addMatcher(decl_ref_matcher, &decl_ref_rewriter); | 414 match_finder.addMatcher(decl_ref_matcher, &decl_ref_rewriter); |
401 | 415 |
402 // Non-method function declarations ======== | 416 // Non-method function declarations ======== |
403 // Given | 417 // Given |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
499 | 513 |
500 // Initializers ======== | 514 // Initializers ======== |
501 // Given | 515 // Given |
502 // struct S { | 516 // struct S { |
503 // int x; | 517 // int x; |
504 // S() : x(2) {} | 518 // S() : x(2) {} |
505 // }; | 519 // }; |
506 // matches each initializer in the constructor for S. | 520 // matches each initializer in the constructor for S. |
507 auto constructor_initializer_matcher = | 521 auto constructor_initializer_matcher = |
508 cxxConstructorDecl(forEachConstructorInitializer( | 522 cxxConstructorDecl(forEachConstructorInitializer( |
509 id("initializer", cxxCtorInitializer(forField(field_decl_matcher))))); | 523 id("initializer", |
524 cxxCtorInitializer(forField(field_decl_matcher), isWritten())))); | |
510 | 525 |
511 ConstructorInitializerRewriter constructor_initializer_rewriter( | 526 ConstructorInitializerRewriter constructor_initializer_rewriter( |
512 &replacements); | 527 &replacements); |
513 match_finder.addMatcher(constructor_initializer_matcher, | 528 match_finder.addMatcher(constructor_initializer_matcher, |
514 &constructor_initializer_rewriter); | 529 &constructor_initializer_rewriter); |
515 | 530 |
516 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = | 531 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = |
517 clang::tooling::newFrontendActionFactory(&match_finder); | 532 clang::tooling::newFrontendActionFactory(&match_finder); |
518 int result = tool.run(factory.get()); | 533 int result = tool.run(factory.get()); |
519 if (result != 0) | 534 if (result != 0) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
555 for (const auto& r : replacements) { | 570 for (const auto& r : replacements) { |
556 std::string replacement_text = r.getReplacementText().str(); | 571 std::string replacement_text = r.getReplacementText().str(); |
557 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 572 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
558 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 573 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
559 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 574 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
560 } | 575 } |
561 llvm::outs() << "==== END EDITS ====\n"; | 576 llvm::outs() << "==== END EDITS ====\n"; |
562 | 577 |
563 return 0; | 578 return 0; |
564 } | 579 } |
OLD | NEW |