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/ASTMatchers.h" 38 #include "clang/ASTMatchers/ASTMatchFinder.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(Replacements *Replace) : Replace(Replace) {} 58 59 void run(const MatchFinder::MatchResult &Result) override { 60 // TODO: This routine will get called for each thing that the matchers find. 61 // At this point, you can examine the match, and do whatever you want, 62 // including replacing the matched text with other text 63 (void)Replace; // This to prevent an "unused member variable" warning; 64 } 65 66 private: 67 Replacements *Replace; 68 }; 69 } // end anonymous namespace 70 71 // Set up the command line options 72 static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); 73 static cl::OptionCategory ToolTemplateCategory("tool-template options"); 74 75 int main(int argc, const char **argv) { 76 llvm::sys::PrintStackTraceOnErrorSignal(); 77 CommonOptionsParser OptionsParser(argc, argv, ToolTemplateCategory); 78 RefactoringTool Tool(OptionsParser.getCompilations(), 79 OptionsParser.getSourcePathList()); 80 ast_matchers::MatchFinder Finder; 81 ToolTemplateCallback Callback(&Tool.getReplacements()); 82 83 // TODO: Put your matchers here. 84 // Use Finder.addMatcher(...) to define the patterns in the AST that you 85 // want to match against. You are not limited to just one matcher! 86 87 return Tool.run(newFrontendActionFactory(&Finder).get()); 88 } 89