1 //===---- tools/extra/ToolTemplate.cpp - Template for refactoring tool ----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements an empty refactoring tool using the clang tooling. 11 // The goal is to lower the "barrier to entry" for writing refactoring tools. 12 // 13 // Usage: 14 // tool-template <cmake-output-dir> <file1> <file2> ... 15 // 16 // Where <cmake-output-dir> is a CMake build directory in which a file named 17 // compile_commands.json exists (enable -DCMAKE_EXPORT_COMPILE_COMMANDS in 18 // CMake to get this output). 19 // 20 // <file1> ... specify the paths of files in the CMake source tree. This path 21 // is looked up in the compile command database. If the path of a file is 22 // absolute, it needs to point into CMake's source tree. If the path is 23 // relative, the current working directory needs to be in the CMake source 24 // tree and the file must be in a subdirectory of the current working 25 // directory. "./" prefixes in the relative files will be automatically 26 // removed, but the rest of a relative path must be a suffix of a path in 27 // the compile command line database. 28 // 29 // For example, to use tool-template on all files in a subtree of the 30 // source tree, use: 31 // 32 // /path/in/subtree $ find . -name '*.cpp'| 33 // xargs tool-template /path/to/build 34 // 35 //===----------------------------------------------------------------------===// 36 37 #include "clang/ASTMatchers/ASTMatchFinder.h" 38 #include "clang/ASTMatchers/ASTMatchers.h" 39 #include "clang/Basic/SourceManager.h" 40 #include "clang/Frontend/FrontendActions.h" 41 #include "clang/Lex/Lexer.h" 42 #include "clang/Tooling/CommonOptionsParser.h" 43 #include "clang/Tooling/Refactoring.h" 44 #include "clang/Tooling/Tooling.h" 45 #include "llvm/Support/CommandLine.h" 46 #include "llvm/Support/MemoryBuffer.h" 47 #include "llvm/Support/Signals.h" 48 49 using namespace clang; 50 using namespace clang::ast_matchers; 51 using namespace clang::tooling; 52 using namespace llvm; 53 54 namespace { 55 class ToolTemplateCallback : public MatchFinder::MatchCallback { 56 public: 57 ToolTemplateCallback(std::map<std::string, Replacements> *Replace) 58 : Replace(Replace) {} 59 60 void run(const MatchFinder::MatchResult &Result) override { 61 // TODO: This routine will get called for each thing that the matchers 62 // find. 63 // At this point, you can examine the match, and do whatever you want, 64 // including replacing the matched text with other text 65 (void)Replace; // This to prevent an "unused member variable" warning; 66 } 67 68 private: 69 std::map<std::string, Replacements> *Replace; 70 }; 71 } // end anonymous namespace 72 73 // Set up the command line options 74 static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); 75 static cl::OptionCategory ToolTemplateCategory("tool-template options"); 76 77 int main(int argc, const char **argv) { 78 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); 79 CommonOptionsParser OptionsParser(argc, argv, ToolTemplateCategory); 80 RefactoringTool Tool(OptionsParser.getCompilations(), 81 OptionsParser.getSourcePathList()); 82 ast_matchers::MatchFinder Finder; 83 ToolTemplateCallback Callback(&Tool.getReplacements()); 84 85 // TODO: Put your matchers here. 86 // Use Finder.addMatcher(...) to define the patterns in the AST that you 87 // want to match against. You are not limited to just one matcher! 88 89 return Tool.run(newFrontendActionFactory(&Finder).get()); 90 } 91