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 // Performs simple cleanups of base::Value API usage. 6 7 #include <assert.h> 8 #include <memory> 9 #include <set> 10 #include <string> 11 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Basic/CharInfo.h" 14 #include "clang/Basic/SourceManager.h" 15 #include "clang/Frontend/FrontendActions.h" 16 #include "clang/Lex/Lexer.h" 17 #include "clang/Tooling/CommonOptionsParser.h" 18 #include "clang/Tooling/Refactoring.h" 19 #include "clang/Tooling/Tooling.h" 20 #include "llvm/Support/CommandLine.h" 21 #include "llvm/Support/TargetSelect.h" 22 23 #include "ListValueRewriter.h" 24 25 using namespace clang::ast_matchers; 26 using clang::tooling::CommonOptionsParser; 27 using clang::tooling::Replacement; 28 using llvm::StringRef; 29 30 static llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage); 31 main(int argc,const char * argv[])32int main(int argc, const char* argv[]) { 33 // TODO(dcheng): Clang tooling should do this itself. 34 // http://llvm.org/bugs/show_bug.cgi?id=21627 35 llvm::InitializeNativeTarget(); 36 llvm::InitializeNativeTargetAsmParser(); 37 llvm::cl::OptionCategory category( 38 "value_cleanup: Cleanup deprecated base::Value APIs."); 39 CommonOptionsParser options(argc, argv, category); 40 clang::tooling::ClangTool tool(options.getCompilations(), 41 options.getSourcePathList()); 42 43 MatchFinder match_finder; 44 std::set<Replacement> replacements; 45 46 ListValueRewriter list_value_rewriter(&replacements); 47 list_value_rewriter.RegisterMatchers(&match_finder); 48 49 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = 50 clang::tooling::newFrontendActionFactory(&match_finder); 51 int result = tool.run(factory.get()); 52 if (result != 0) 53 return result; 54 55 // Serialization format is documented in tools/clang/scripts/run_tool.py 56 llvm::outs() << "==== BEGIN EDITS ====\n"; 57 for (const auto& r : replacements) { 58 std::string replacement_text = r.getReplacementText().str(); 59 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 60 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 61 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 62 } 63 llvm::outs() << "==== END EDITS ====\n"; 64 65 return 0; 66 } 67