Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: tools/clang/base_bind_rewriters/BaseBindRewriters.cpp

Issue 2283003002: Remove unneeded scoped_refptr<>::get on method bind (Closed)
Patch Set: update Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.
4 //
5 // This file containts a clang tool to update base::Bind() callers:
6 // * Remove unneeded scoped_refptr<>::get() on method binding.
7
8 #include <assert.h>
9 #include <algorithm>
10 #include <memory>
11 #include <string>
12
13 #include "clang/AST/ASTContext.h"
14 #include "clang/ASTMatchers/ASTMatchers.h"
15 #include "clang/ASTMatchers/ASTMatchersMacros.h"
16 #include "clang/ASTMatchers/ASTMatchFinder.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Frontend/FrontendActions.h"
19 #include "clang/Lex/Lexer.h"
20 #include "clang/Tooling/CommonOptionsParser.h"
21 #include "clang/Tooling/Refactoring.h"
22 #include "clang/Tooling/Tooling.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/TargetSelect.h"
25
26 using namespace clang::ast_matchers;
27 using clang::tooling::CommonOptionsParser;
28 using Replacements = std::vector<clang::tooling::Replacement>;
29
30 namespace {
31
32 // Remove unneeded scoped_refptr<>::get on a receivers of method bind.
33 // Example:
34 // // Before
35 // scoped_refptr<Foo> foo;
36 // base::Bind(&Foo::Bar, foo.get());
37 //
38 // // After
39 // scoped_refptr<Foo> foo;
40 // base::Bind(&Foo::Bar, foo);
41 //
42 class ScopedRefptrGetRewriter : public MatchFinder::MatchCallback {
43 public:
44 explicit ScopedRefptrGetRewriter(Replacements* replacements)
45 : replacements_(replacements) {}
46
47 StatementMatcher GetMatcher() {
48 auto is_bind_call = callee(namedDecl(hasName("::base::Bind")));
49 auto is_method_bind = hasArgument(0, hasType(memberPointerType()));
50 auto is_raw_pointer_receiver = hasArgument(1, hasType(pointerType()));
51 auto is_scoped_refptr_get_call =
52 cxxMemberCallExpr(thisPointerType(namedDecl(hasName("scoped_refptr"))),
53 callee(namedDecl(hasName("get"))));
54 return callExpr(is_bind_call, is_method_bind, is_raw_pointer_receiver,
55 hasArgument(1, is_scoped_refptr_get_call),
56 hasArgument(1, stmt().bind("target")));
57 }
58
59 void run(const MatchFinder::MatchResult& result) override {
60 auto* target = result.Nodes.getNodeAs<clang::CXXMemberCallExpr>("target");
61 assert(target && "Unexpected match! No Expr captured!");
62
63 clang::CharSourceRange range = clang::CharSourceRange::getTokenRange(
64 result.SourceManager->getSpellingLoc(target->getLocStart()),
65 result.SourceManager->getSpellingLoc(target->getLocEnd()));
66
67 auto* member = llvm::dyn_cast<clang::MemberExpr>(target->getCallee());
dcheng 2016/08/31 06:14:44 Nit: use cast<> here, since we want to assert if t
tzik 2016/08/31 09:06:43 Done.
68 clang::CharSourceRange member_range = clang::CharSourceRange::getTokenRange(
dcheng 2016/08/31 06:14:44 To clarify, I would do this: auto range = clang::
tzik 2016/08/31 09:06:43 Oh... Thanks! Updated. TIL: - SourceLocation poi
69 result.SourceManager->getSpellingLoc(member->getLocStart()),
70 result.SourceManager->getSpellingLoc(member->getOperatorLoc()));
71 std::string text = clang::Lexer::getSourceText(
72 member_range, *result.SourceManager, result.Context->getLangOpts());
73 if (member->isArrow())
74 text.erase(text.size() - 2);
75 else
76 text.erase(text.size() - 1);
77 replacements_->emplace_back(
78 *result.SourceManager, range, text);
79 }
80
81 private:
82 Replacements* replacements_;
83 };
84
85 llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage);
86
87 } // namespace.
88
89 int main(int argc, const char* argv[]) {
90 llvm::InitializeNativeTarget();
91 llvm::InitializeNativeTargetAsmParser();
92 llvm::cl::OptionCategory category(
93 "Remove raw pointer on the receiver of Bind() target");
94 CommonOptionsParser options(argc, argv, category);
95 clang::tooling::ClangTool tool(options.getCompilations(),
96 options.getSourcePathList());
97
98 MatchFinder match_finder;
99 std::vector<clang::tooling::Replacement> replacements;
100
101
102 ScopedRefptrGetRewriter scoped_refptr_rewriter(&replacements);
103 match_finder.addMatcher(scoped_refptr_rewriter.GetMatcher(),
104 &scoped_refptr_rewriter);
105
106 std::unique_ptr<clang::tooling::FrontendActionFactory> factory =
107 clang::tooling::newFrontendActionFactory(&match_finder);
108 int result = tool.run(factory.get());
109 if (result != 0)
110 return result;
111
112 // Serialization format is documented in tools/clang/scripts/run_tool.py
113 llvm::outs() << "==== BEGIN EDITS ====\n";
114 for (const auto& r : replacements) {
115 std::string replacement_text = r.getReplacementText().str();
116 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
117 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
118 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
119 }
120 llvm::outs() << "==== END EDITS ====\n";
121
122 return 0;
123 }
OLDNEW
« no previous file with comments | « base/message_loop/message_pump_glib_unittest.cc ('k') | tools/clang/base_bind_rewriters/CMakeLists.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698