| 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 = target->getCallee(); |
| 62 assert(target && member && "Unexpected match! No Expr captured!"); | |
| 63 auto range = clang::CharSourceRange::getTokenRange( | 73 auto range = clang::CharSourceRange::getTokenRange( |
| 64 result.SourceManager->getSpellingLoc(member->getOperatorLoc()), | 74 result.SourceManager->getSpellingLoc(callee->getLocEnd()), |
| 65 result.SourceManager->getSpellingLoc(target->getLocEnd())); | 75 result.SourceManager->getSpellingLoc(callee->getLocEnd())); |
| 66 | 76 replacements_->emplace_back(*result.SourceManager, range, "BindOnce"); |
| 67 replacements_->emplace_back(*result.SourceManager, range, ""); | |
| 68 } | 77 } |
| 69 | 78 |
| 70 private: | 79 private: |
| 71 Replacements* replacements_; | 80 Replacements* replacements_; |
| 72 }; | 81 }; |
| 73 | 82 |
| 74 llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage); | 83 llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage); |
| 75 | 84 |
| 76 } // namespace. | 85 } // namespace. |
| 77 | 86 |
| 78 int main(int argc, const char* argv[]) { | 87 int main(int argc, const char* argv[]) { |
| 79 llvm::InitializeNativeTarget(); | 88 llvm::InitializeNativeTarget(); |
| 80 llvm::InitializeNativeTargetAsmParser(); | 89 llvm::InitializeNativeTargetAsmParser(); |
| 81 llvm::cl::OptionCategory category( | 90 llvm::cl::OptionCategory category( |
| 82 "Remove raw pointer on the receiver of Bind() target"); | 91 "Remove raw pointer on the receiver of Bind() target"); |
| 83 CommonOptionsParser options(argc, argv, category); | 92 CommonOptionsParser options(argc, argv, category); |
| 84 clang::tooling::ClangTool tool(options.getCompilations(), | 93 clang::tooling::ClangTool tool(options.getCompilations(), |
| 85 options.getSourcePathList()); | 94 options.getSourcePathList()); |
| 86 | 95 |
| 87 MatchFinder match_finder; | 96 MatchFinder match_finder; |
| 88 std::vector<clang::tooling::Replacement> replacements; | 97 std::vector<clang::tooling::Replacement> replacements; |
| 89 | 98 |
| 90 | 99 BindOnceRewriter bind_once_rewriter(&replacements); |
| 91 ScopedRefptrGetRewriter scoped_refptr_rewriter(&replacements); | 100 match_finder.addMatcher(bind_once_rewriter.GetMatcher(), &bind_once_rewriter); |
| 92 match_finder.addMatcher(scoped_refptr_rewriter.GetMatcher(), | |
| 93 &scoped_refptr_rewriter); | |
| 94 | 101 |
| 95 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = | 102 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = |
| 96 clang::tooling::newFrontendActionFactory(&match_finder); | 103 clang::tooling::newFrontendActionFactory(&match_finder); |
| 97 int result = tool.run(factory.get()); | 104 int result = tool.run(factory.get()); |
| 98 if (result != 0) | 105 if (result != 0) |
| 99 return result; | 106 return result; |
| 100 | 107 |
| 101 // Serialization format is documented in tools/clang/scripts/run_tool.py | 108 // Serialization format is documented in tools/clang/scripts/run_tool.py |
| 102 llvm::outs() << "==== BEGIN EDITS ====\n"; | 109 llvm::outs() << "==== BEGIN EDITS ====\n"; |
| 103 for (const auto& r : replacements) { | 110 for (const auto& r : replacements) { |
| 104 std::string replacement_text = r.getReplacementText().str(); | 111 std::string replacement_text = r.getReplacementText().str(); |
| 105 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 112 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 106 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 113 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 107 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 114 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 108 } | 115 } |
| 109 llvm::outs() << "==== END EDITS ====\n"; | 116 llvm::outs() << "==== END EDITS ====\n"; |
| 110 | 117 |
| 111 return 0; | 118 return 0; |
| 112 } | 119 } |
| OLD | NEW |