Chromium Code Reviews| Index: tools/clang/value_cleanup/ValueCleanup.cpp |
| diff --git a/tools/clang/value_cleanup/ValueCleanup.cpp b/tools/clang/value_cleanup/ValueCleanup.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ceec4e1b609ab772ff66d7569dfae7119edb23af |
| --- /dev/null |
| +++ b/tools/clang/value_cleanup/ValueCleanup.cpp |
| @@ -0,0 +1,119 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
|
danakj
2016/06/02 18:33:02
2016
dcheng
2016/06/02 19:51:33
Done
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| +// |
| +// Performs simple cleanups of base::Value API usage. |
| +// - base::ListValue::Append(new base::StringValue(...)) |
| +// => base::ListValue::AppendString(...) |
| + |
| +#include <assert.h> |
| +#include <algorithm> |
| +#include <memory> |
| +#include <string> |
| +#include <unordered_map> |
| + |
| +#include "clang/AST/ASTContext.h" |
| +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| +#include "clang/ASTMatchers/ASTMatchers.h" |
| +#include "clang/ASTMatchers/ASTMatchersMacros.h" |
| +#include "clang/Basic/CharInfo.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 clang::tooling::Replacement; |
| +using clang::tooling::Replacements; |
| +using llvm::StringRef; |
| + |
| +namespace { |
| + |
| +class AppendStringCallback : public MatchFinder::MatchCallback { |
| + public: |
| + explicit AppendStringCallback(Replacements* replacements) |
| + : replacements_(replacements) {} |
| + |
| + void run(const MatchFinder::MatchResult& result) override { |
| + // Replace 'Append' with 'AppendString'. |
| + auto* callExpr = |
| + result.Nodes.getNodeAs<clang::CXXMemberCallExpr>("callExpr"); |
| + |
| + clang::CharSourceRange call_range = |
| + clang::CharSourceRange::getTokenRange(callExpr->getExprLoc()); |
| + replacements_->emplace(*result.SourceManager, call_range, "AppendString"); |
| + |
| + // Delete `new base::StringValue(' and `)'. |
| + auto* newExpr = result.Nodes.getNodeAs<clang::CXXNewExpr>("newExpr"); |
| + auto* argExpr = result.Nodes.getNodeAs<clang::Expr>("argExpr"); |
| + |
| + clang::CharSourceRange pre_arg_range = clang::CharSourceRange::getCharRange( |
| + newExpr->getLocStart(), argExpr->getLocStart()); |
| + replacements_->emplace(*result.SourceManager, pre_arg_range, ""); |
| + |
| + clang::CharSourceRange post_arg_range = |
| + clang::CharSourceRange::getTokenRange(newExpr->getLocEnd()); |
| + replacements_->emplace(*result.SourceManager, post_arg_range, ""); |
| + } |
| + |
| + private: |
| + Replacements* const replacements_; |
| +}; |
| + |
| +} // namespace |
| + |
| +static llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage); |
| + |
| +int main(int argc, const char* argv[]) { |
| + // TODO(dcheng): Clang tooling should do this itself. |
| + // http://llvm.org/bugs/show_bug.cgi?id=21627 |
| + llvm::InitializeNativeTarget(); |
| + llvm::InitializeNativeTargetAsmParser(); |
| + llvm::cl::OptionCategory category( |
| + "value_cleanup: Cleanup deprecated base::Value APIs."); |
| + CommonOptionsParser options(argc, argv, category); |
| + clang::tooling::ClangTool tool(options.getCompilations(), |
| + options.getSourcePathList()); |
| + |
| + MatchFinder match_finder; |
| + Replacements replacements; |
| + |
| + auto append_with_raw_string_value = id( |
| + "callExpr", |
| + cxxMemberCallExpr( |
| + callee(cxxMethodDecl(hasName("::base::ListValue::Append"))), |
| + argumentCountIs(1), |
| + hasArgument(0, ignoringParenImpCasts(id( |
| + "newExpr", |
| + cxxNewExpr(has(cxxConstructExpr( |
| + hasDeclaration(cxxMethodDecl(hasName( |
| + "::base::StringValue::StringValue"))), |
| + argumentCountIs(1), |
| + hasArgument(0, id("argExpr", expr())))))))))); |
| + |
| + AppendStringCallback append_string_cb(&replacements); |
| + match_finder.addMatcher(append_with_raw_string_value, &append_string_cb); |
| + |
| + 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; |
| +} |