OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
dcheng
2016/08/29 20:04:09
Let's document what the tool does (similar to http
tzik
2016/08/30 07:40:50
Done.
| |
4 | |
5 #include <assert.h> | |
6 #include <algorithm> | |
7 #include <memory> | |
8 #include <string> | |
9 | |
10 #include "clang/AST/ASTContext.h" | |
11 #include "clang/ASTMatchers/ASTMatchers.h" | |
12 #include "clang/ASTMatchers/ASTMatchersMacros.h" | |
13 #include "clang/ASTMatchers/ASTMatchFinder.h" | |
14 #include "clang/Basic/SourceManager.h" | |
15 #include "clang/Frontend/FrontendActions.h" | |
16 #include "clang/Lex/Lexer.h" | |
17 #include "clang/Tooling/CommonOptionsParser.h" | |
18 #include "clang/Tooling/Refactoring.h" | |
19 #include "clang/Tooling/Tooling.h" | |
20 #include "llvm/Support/CommandLine.h" | |
21 #include "llvm/Support/TargetSelect.h" | |
22 | |
23 using namespace clang::ast_matchers; | |
24 using clang::tooling::CommonOptionsParser; | |
25 using Replacements = std::vector<clang::tooling::Replacement>; | |
26 using Rewriter = std::function<std::string(const std::string&)>; | |
27 | |
28 namespace { | |
29 | |
30 class RewriterCallback : public MatchFinder::MatchCallback { | |
31 public: | |
32 RewriterCallback(Replacements* replacements, Rewriter rewriter) | |
33 : replacements_(replacements), rewriter_(std::move(rewriter)) {} | |
34 | |
35 void run(const MatchFinder::MatchResult& result) override { | |
36 const clang::Expr* target = result.Nodes.getNodeAs<clang::Expr>("target"); | |
37 assert(target && "Unexpected match! No Expr captured!"); | |
38 | |
39 clang::CharSourceRange range = clang::CharSourceRange::getTokenRange( | |
40 result.SourceManager->getSpellingLoc(target->getLocStart()), | |
41 result.SourceManager->getSpellingLoc(target->getLocEnd())); | |
42 std::string text = clang::Lexer::getSourceText( | |
43 range, *result.SourceManager, result.Context->getLangOpts()); | |
dcheng
2016/08/29 20:04:09
Usually it's slightly safer to not grab the source
tzik
2016/08/30 07:40:50
Done.
| |
44 replacements_->emplace_back( | |
45 *result.SourceManager, range, rewriter_(text)); | |
46 } | |
47 | |
48 private: | |
49 Replacements* replacements_; | |
50 Rewriter rewriter_; | |
51 }; | |
52 | |
53 llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage); | |
54 | |
55 } // namespace. | |
56 | |
57 int main(int argc, const char* argv[]) { | |
58 llvm::InitializeNativeTarget(); | |
59 llvm::InitializeNativeTargetAsmParser(); | |
60 llvm::cl::OptionCategory category( | |
61 "Remove raw pointer on the receiver of Bind() target"); | |
62 CommonOptionsParser options(argc, argv, category); | |
63 clang::tooling::ClangTool tool(options.getCompilations(), | |
64 options.getSourcePathList()); | |
65 | |
66 MatchFinder match_finder; | |
67 std::vector<clang::tooling::Replacement> replacements; | |
68 | |
69 auto is_bind_call = callee(namedDecl(hasName("::base::Bind"))); | |
70 auto is_method_bind = hasArgument(0, hasType(memberPointerType())); | |
71 auto is_raw_pointer_receiver = hasArgument(1, hasType(pointerType())); | |
72 auto is_scoped_refptr_get_call = | |
73 cxxMemberCallExpr(thisPointerType(namedDecl(hasName("scoped_refptr"))), | |
74 callee(namedDecl(hasName("get")))); | |
75 | |
76 // Remove unneeded scoped_refptr<>::get on a receivers of method bind. | |
77 // Example: | |
78 // // Before | |
79 // scoped_refptr<Foo> foo; | |
80 // base::Bind(&Foo::Bar, foo.get()); | |
81 // | |
82 // // After | |
83 // scoped_refptr<Foo> foo; | |
84 // base::Bind(&Foo::Bar, foo); | |
85 // | |
86 RewriterCallback remove_unneeded_scoped_refptr_get( | |
87 &replacements, | |
88 [](const std::string& text) { | |
89 return text.substr(0, text.size() - sizeof(".get()") + 1); | |
dcheng
2016/08/29 20:04:09
Nit: strlen() should work here and hopefully make
| |
90 }); | |
91 match_finder.addMatcher( | |
92 callExpr(is_bind_call, is_method_bind, is_raw_pointer_receiver, | |
93 hasArgument(1, is_scoped_refptr_get_call), | |
94 hasArgument(1, stmt().bind("target"))), | |
95 &remove_unneeded_scoped_refptr_get); | |
96 | |
97 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = | |
98 clang::tooling::newFrontendActionFactory(&match_finder); | |
99 int result = tool.run(factory.get()); | |
100 if (result != 0) | |
101 return result; | |
102 | |
103 // Serialization format is documented in tools/clang/scripts/run_tool.py | |
104 llvm::outs() << "==== BEGIN EDITS ====\n"; | |
105 for (const auto& r : replacements) { | |
106 std::string replacement_text = r.getReplacementText().str(); | |
107 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | |
108 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | |
109 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | |
110 } | |
111 llvm::outs() << "==== END EDITS ====\n"; | |
112 | |
113 return 0; | |
114 } | |
OLD | NEW |