Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 // This file containts a clang tool to update base::Bind() callers: | 5 // This file containts a clang tool to update base::Bind() callers: |
| 6 // * Remove unneeded scoped_refptr<>::get() on method binding. | 6 // * Remove unneeded scoped_refptr<>::get() on method binding. |
| 7 | 7 |
| 8 #include <assert.h> | 8 #include <assert.h> |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 #include "clang/Tooling/Tooling.h" | 22 #include "clang/Tooling/Tooling.h" |
| 23 #include "llvm/Support/CommandLine.h" | 23 #include "llvm/Support/CommandLine.h" |
| 24 #include "llvm/Support/TargetSelect.h" | 24 #include "llvm/Support/TargetSelect.h" |
| 25 | 25 |
| 26 using namespace clang::ast_matchers; | 26 using namespace clang::ast_matchers; |
| 27 using clang::tooling::CommonOptionsParser; | 27 using clang::tooling::CommonOptionsParser; |
| 28 using Replacements = std::vector<clang::tooling::Replacement>; | 28 using Replacements = std::vector<clang::tooling::Replacement>; |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 // Remove unneeded scoped_refptr<>::get on a receivers of method bind. | 32 // Replace base::Bind() to base::BindOnce() where resulting base::Callback is |
| 33 // implicitly converted into base::OnceCallback. | |
| 33 // Example: | 34 // Example: |
| 34 // // Before | 35 // // Before |
| 35 // scoped_refptr<Foo> foo; | 36 // base::PostTask(FROM_HERE, base::Bind(&Foo)); |
| 36 // base::Bind(&Foo::Bar, foo.get()); | 37 // base::OnceCallback<void()> cb = base::Bind(&Foo); |
| 37 // | 38 // |
| 38 // // After | 39 // // After |
| 39 // scoped_refptr<Foo> foo; | 40 // base::PostTask(FROM_HERE, base::BindOnce(&Foo)); |
| 40 // base::Bind(&Foo::Bar, foo); | 41 // base::OnceCallback<void()> cb = base::BindOnce(&Foo); |
| 41 // | 42 class BindOnceRewriter : public MatchFinder::MatchCallback { |
| 42 class ScopedRefptrGetRewriter : public MatchFinder::MatchCallback { | |
| 43 public: | 43 public: |
| 44 explicit ScopedRefptrGetRewriter(Replacements* replacements) | 44 explicit BindOnceRewriter(Replacements* replacements) |
| 45 : replacements_(replacements) {} | 45 : replacements_(replacements) {} |
| 46 | 46 |
| 47 StatementMatcher GetMatcher() { | 47 StatementMatcher GetMatcher() { |
| 48 auto is_bind_call = callee(namedDecl(hasName("::base::Bind"))); | 48 auto is_once_callback = hasType(classTemplateSpecializationDecl( |
| 49 auto is_method_bind = hasArgument(0, hasType(memberPointerType())); | 49 hasName("::base::Callback"), |
| 50 auto is_raw_pointer_receiver = hasArgument(1, hasType(pointerType())); | 50 hasTemplateArgument(1, equalsIntegralValue("0")), |
| 51 auto is_scoped_refptr_get_call = | 51 hasTemplateArgument(2, equalsIntegralValue("0")))); |
| 52 cxxMemberCallExpr(thisPointerType(namedDecl(hasName("scoped_refptr"))), | 52 auto is_repeating_callback = hasType(classTemplateSpecializationDecl( |
| 53 callee(namedDecl(hasName("get")))); | 53 hasName("::base::Callback"), |
| 54 return callExpr(is_bind_call, is_method_bind, is_raw_pointer_receiver, | 54 hasTemplateArgument(1, equalsIntegralValue("1")), |
| 55 hasArgument(1, is_scoped_refptr_get_call), | 55 hasTemplateArgument(2, equalsIntegralValue("1")))); |
| 56 hasArgument(1, stmt().bind("target"))); | 56 |
| 57 auto bind_call = | |
| 58 callExpr(callee(namedDecl(hasName("::base::Bind")))).bind("target"); | |
| 59 auto parameter_construction = | |
| 60 cxxConstructExpr(is_repeating_callback, argumentCountIs(1), | |
| 61 hasArgument(0, ignoringImplicit(bind_call))); | |
| 62 auto constructor_conversion = cxxConstructExpr( | |
| 63 is_once_callback, argumentCountIs(1), | |
| 64 hasArgument(0, ignoringImplicit(parameter_construction))); | |
| 65 auto implicit_conversion = implicitCastExpr( | |
| 66 is_once_callback, hasSourceExpression(constructor_conversion)); | |
| 67 return implicit_conversion; | |
| 57 } | 68 } |
| 58 | 69 |
| 59 void run(const MatchFinder::MatchResult& result) override { | 70 void run(const MatchFinder::MatchResult& result) override { |
| 60 auto* target = result.Nodes.getNodeAs<clang::CXXMemberCallExpr>("target"); | 71 auto* target = result.Nodes.getNodeAs<clang::CallExpr>("target"); |
| 61 auto* member = llvm::cast<clang::MemberExpr>(target->getCallee()); | 72 auto* callee = llvm::dyn_cast<clang::DeclRefExpr>( |
| 62 assert(target && member && "Unexpected match! No Expr captured!"); | 73 target->getCallee()->IgnoreImplicit()); |
|
dcheng
2017/04/05 16:57:37
Out of curiosity, why do we have to IgnoreImplicit
tzik
2017/04/11 07:35:13
Hmm, that seems unneeded. Let me remove it.
There
| |
| 63 auto range = clang::CharSourceRange::getTokenRange( | 74 auto range = clang::CharSourceRange::getTokenRange( |
| 64 result.SourceManager->getSpellingLoc(member->getOperatorLoc()), | 75 result.SourceManager->getSpellingLoc(callee->getLocEnd()), |
| 65 result.SourceManager->getSpellingLoc(target->getLocEnd())); | 76 result.SourceManager->getSpellingLoc(callee->getLocEnd())); |
| 66 | 77 replacements_->emplace_back(*result.SourceManager, range, "BindOnce"); |
| 67 replacements_->emplace_back(*result.SourceManager, range, ""); | |
| 68 } | 78 } |
| 69 | 79 |
| 70 private: | 80 private: |
| 71 Replacements* replacements_; | 81 Replacements* replacements_; |
| 72 }; | 82 }; |
| 73 | 83 |
| 74 llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage); | 84 llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage); |
| 75 | 85 |
| 76 } // namespace. | 86 } // namespace. |
| 77 | 87 |
| 78 int main(int argc, const char* argv[]) { | 88 int main(int argc, const char* argv[]) { |
| 79 llvm::InitializeNativeTarget(); | 89 llvm::InitializeNativeTarget(); |
| 80 llvm::InitializeNativeTargetAsmParser(); | 90 llvm::InitializeNativeTargetAsmParser(); |
| 81 llvm::cl::OptionCategory category( | 91 llvm::cl::OptionCategory category( |
| 82 "Remove raw pointer on the receiver of Bind() target"); | 92 "Remove raw pointer on the receiver of Bind() target"); |
| 83 CommonOptionsParser options(argc, argv, category); | 93 CommonOptionsParser options(argc, argv, category); |
| 84 clang::tooling::ClangTool tool(options.getCompilations(), | 94 clang::tooling::ClangTool tool(options.getCompilations(), |
| 85 options.getSourcePathList()); | 95 options.getSourcePathList()); |
| 86 | 96 |
| 87 MatchFinder match_finder; | 97 MatchFinder match_finder; |
| 88 std::vector<clang::tooling::Replacement> replacements; | 98 std::vector<clang::tooling::Replacement> replacements; |
| 89 | 99 |
| 90 | 100 BindOnceRewriter bind_once_rewriter(&replacements); |
| 91 ScopedRefptrGetRewriter scoped_refptr_rewriter(&replacements); | 101 match_finder.addMatcher(bind_once_rewriter.GetMatcher(), &bind_once_rewriter); |
| 92 match_finder.addMatcher(scoped_refptr_rewriter.GetMatcher(), | |
| 93 &scoped_refptr_rewriter); | |
| 94 | 102 |
| 95 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = | 103 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = |
| 96 clang::tooling::newFrontendActionFactory(&match_finder); | 104 clang::tooling::newFrontendActionFactory(&match_finder); |
| 97 int result = tool.run(factory.get()); | 105 int result = tool.run(factory.get()); |
| 98 if (result != 0) | 106 if (result != 0) |
| 99 return result; | 107 return result; |
| 100 | 108 |
| 101 // Serialization format is documented in tools/clang/scripts/run_tool.py | 109 // Serialization format is documented in tools/clang/scripts/run_tool.py |
| 102 llvm::outs() << "==== BEGIN EDITS ====\n"; | 110 llvm::outs() << "==== BEGIN EDITS ====\n"; |
| 103 for (const auto& r : replacements) { | 111 for (const auto& r : replacements) { |
| 104 std::string replacement_text = r.getReplacementText().str(); | 112 std::string replacement_text = r.getReplacementText().str(); |
| 105 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 113 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 106 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 114 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 107 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 115 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 108 } | 116 } |
| 109 llvm::outs() << "==== END EDITS ====\n"; | 117 llvm::outs() << "==== END EDITS ====\n"; |
| 110 | 118 |
| 111 return 0; | 119 return 0; |
| 112 } | 120 } |
| OLD | NEW |