Index: tools/clang/base_bind_rewriters/BaseBindRewriters.cpp |
diff --git a/tools/clang/base_bind_rewriters/BaseBindRewriters.cpp b/tools/clang/base_bind_rewriters/BaseBindRewriters.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6246895b8899cf377c13fcae582b0d539aaa6de2 |
--- /dev/null |
+++ b/tools/clang/base_bind_rewriters/BaseBindRewriters.cpp |
@@ -0,0 +1,114 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// 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.
|
+ |
+#include <assert.h> |
+#include <algorithm> |
+#include <memory> |
+#include <string> |
+ |
+#include "clang/AST/ASTContext.h" |
+#include "clang/ASTMatchers/ASTMatchers.h" |
+#include "clang/ASTMatchers/ASTMatchersMacros.h" |
+#include "clang/ASTMatchers/ASTMatchFinder.h" |
+#include "clang/Basic/SourceManager.h" |
+#include "clang/Frontend/FrontendActions.h" |
+#include "clang/Lex/Lexer.h" |
+#include "clang/Tooling/CommonOptionsParser.h" |
+#include "clang/Tooling/Refactoring.h" |
+#include "clang/Tooling/Tooling.h" |
+#include "llvm/Support/CommandLine.h" |
+#include "llvm/Support/TargetSelect.h" |
+ |
+using namespace clang::ast_matchers; |
+using clang::tooling::CommonOptionsParser; |
+using Replacements = std::vector<clang::tooling::Replacement>; |
+using Rewriter = std::function<std::string(const std::string&)>; |
+ |
+namespace { |
+ |
+class RewriterCallback : public MatchFinder::MatchCallback { |
+ public: |
+ RewriterCallback(Replacements* replacements, Rewriter rewriter) |
+ : replacements_(replacements), rewriter_(std::move(rewriter)) {} |
+ |
+ void run(const MatchFinder::MatchResult& result) override { |
+ const clang::Expr* target = result.Nodes.getNodeAs<clang::Expr>("target"); |
+ assert(target && "Unexpected match! No Expr captured!"); |
+ |
+ clang::CharSourceRange range = clang::CharSourceRange::getTokenRange( |
+ result.SourceManager->getSpellingLoc(target->getLocStart()), |
+ result.SourceManager->getSpellingLoc(target->getLocEnd())); |
+ std::string text = clang::Lexer::getSourceText( |
+ 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.
|
+ replacements_->emplace_back( |
+ *result.SourceManager, range, rewriter_(text)); |
+ } |
+ |
+ private: |
+ Replacements* replacements_; |
+ Rewriter rewriter_; |
+}; |
+ |
+llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage); |
+ |
+} // namespace. |
+ |
+int main(int argc, const char* argv[]) { |
+ llvm::InitializeNativeTarget(); |
+ llvm::InitializeNativeTargetAsmParser(); |
+ llvm::cl::OptionCategory category( |
+ "Remove raw pointer on the receiver of Bind() target"); |
+ CommonOptionsParser options(argc, argv, category); |
+ clang::tooling::ClangTool tool(options.getCompilations(), |
+ options.getSourcePathList()); |
+ |
+ MatchFinder match_finder; |
+ std::vector<clang::tooling::Replacement> replacements; |
+ |
+ auto is_bind_call = callee(namedDecl(hasName("::base::Bind"))); |
+ auto is_method_bind = hasArgument(0, hasType(memberPointerType())); |
+ auto is_raw_pointer_receiver = hasArgument(1, hasType(pointerType())); |
+ auto is_scoped_refptr_get_call = |
+ cxxMemberCallExpr(thisPointerType(namedDecl(hasName("scoped_refptr"))), |
+ callee(namedDecl(hasName("get")))); |
+ |
+ // Remove unneeded scoped_refptr<>::get on a receivers of method bind. |
+ // Example: |
+ // // Before |
+ // scoped_refptr<Foo> foo; |
+ // base::Bind(&Foo::Bar, foo.get()); |
+ // |
+ // // After |
+ // scoped_refptr<Foo> foo; |
+ // base::Bind(&Foo::Bar, foo); |
+ // |
+ RewriterCallback remove_unneeded_scoped_refptr_get( |
+ &replacements, |
+ [](const std::string& text) { |
+ return text.substr(0, text.size() - sizeof(".get()") + 1); |
dcheng
2016/08/29 20:04:09
Nit: strlen() should work here and hopefully make
|
+ }); |
+ match_finder.addMatcher( |
+ callExpr(is_bind_call, is_method_bind, is_raw_pointer_receiver, |
+ hasArgument(1, is_scoped_refptr_get_call), |
+ hasArgument(1, stmt().bind("target"))), |
+ &remove_unneeded_scoped_refptr_get); |
+ |
+ std::unique_ptr<clang::tooling::FrontendActionFactory> factory = |
+ clang::tooling::newFrontendActionFactory(&match_finder); |
+ int result = tool.run(factory.get()); |
+ if (result != 0) |
+ return result; |
+ |
+ // Serialization format is documented in tools/clang/scripts/run_tool.py |
+ llvm::outs() << "==== BEGIN EDITS ====\n"; |
+ for (const auto& r : replacements) { |
+ std::string replacement_text = r.getReplacementText().str(); |
+ std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
+ llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
+ << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
+ } |
+ llvm::outs() << "==== END EDITS ====\n"; |
+ |
+ return 0; |
+} |