Sam Panzer | 8e0df50 | 2012-08-24 23:29:33 +0000 | [diff] [blame] | 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" |
Alexander Kornienko | da2734d | 2014-08-02 08:24:10 +0000 | [diff] [blame] | 42 | #include "clang/Tooling/CommonOptionsParser.h" |
Sam Panzer | 8e0df50 | 2012-08-24 23:29:33 +0000 | [diff] [blame] | 43 | #include "clang/Tooling/Refactoring.h" |
| 44 | #include "clang/Tooling/Tooling.h" |
Sam Panzer | 8e0df50 | 2012-08-24 23:29:33 +0000 | [diff] [blame] | 45 | #include "llvm/Support/CommandLine.h" |
| 46 | #include "llvm/Support/MemoryBuffer.h" |
NAKAMURA Takumi | 40ac112 | 2013-01-18 14:31:00 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Signals.h" |
Sam Panzer | 8e0df50 | 2012-08-24 23:29:33 +0000 | [diff] [blame] | 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 { |
Eric Liu | 267034c | 2016-08-01 10:16:39 +0000 | [diff] [blame] | 56 | public: |
| 57 | ToolTemplateCallback(std::map<std::string, Replacements> *Replace) |
| 58 | : Replace(Replace) {} |
Sam Panzer | 8e0df50 | 2012-08-24 23:29:33 +0000 | [diff] [blame] | 59 | |
Alexander Kornienko | da2734d | 2014-08-02 08:24:10 +0000 | [diff] [blame] | 60 | void run(const MatchFinder::MatchResult &Result) override { |
Eric Liu | 267034c | 2016-08-01 10:16:39 +0000 | [diff] [blame] | 61 | // TODO: This routine will get called for each thing that the matchers |
| 62 | // find. |
Alexander Kornienko | da2734d | 2014-08-02 08:24:10 +0000 | [diff] [blame] | 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; |
Sam Panzer | 8e0df50 | 2012-08-24 23:29:33 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Eric Liu | 267034c | 2016-08-01 10:16:39 +0000 | [diff] [blame] | 68 | private: |
| 69 | std::map<std::string, Replacements> *Replace; |
Sam Panzer | 8e0df50 | 2012-08-24 23:29:33 +0000 | [diff] [blame] | 70 | }; |
| 71 | } // end anonymous namespace |
| 72 | |
| 73 | // Set up the command line options |
Alexander Kornienko | da2734d | 2014-08-02 08:24:10 +0000 | [diff] [blame] | 74 | static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); |
| 75 | static cl::OptionCategory ToolTemplateCategory("tool-template options"); |
Sam Panzer | 8e0df50 | 2012-08-24 23:29:33 +0000 | [diff] [blame] | 76 | |
| 77 | int main(int argc, const char **argv) { |
Richard Smith | 22252f9 | 2016-06-09 00:54:42 +0000 | [diff] [blame] | 78 | llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); |
Alexander Kornienko | da2734d | 2014-08-02 08:24:10 +0000 | [diff] [blame] | 79 | CommonOptionsParser OptionsParser(argc, argv, ToolTemplateCategory); |
| 80 | RefactoringTool Tool(OptionsParser.getCompilations(), |
| 81 | OptionsParser.getSourcePathList()); |
Sam Panzer | 8e0df50 | 2012-08-24 23:29:33 +0000 | [diff] [blame] | 82 | ast_matchers::MatchFinder Finder; |
| 83 | ToolTemplateCallback Callback(&Tool.getReplacements()); |
| 84 | |
Alexander Kornienko | da2734d | 2014-08-02 08:24:10 +0000 | [diff] [blame] | 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! |
Sam Panzer | 8e0df50 | 2012-08-24 23:29:33 +0000 | [diff] [blame] | 88 | |
Benjamin Kramer | fb68e64 | 2014-04-28 10:06:50 +0000 | [diff] [blame] | 89 | return Tool.run(newFrontendActionFactory(&Finder).get()); |
Sam Panzer | 8e0df50 | 2012-08-24 23:29:33 +0000 | [diff] [blame] | 90 | } |