blob: 54b4a696df98febdb3c10dd56fb3e05556576e33 [file] [log] [blame]
Sam Panzer8e0df502012-08-24 23:29:33 +00001//===---- 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 Kornienkoda2734d2014-08-02 08:24:10 +000042#include "clang/Tooling/CommonOptionsParser.h"
Sam Panzer8e0df502012-08-24 23:29:33 +000043#include "clang/Tooling/Refactoring.h"
44#include "clang/Tooling/Tooling.h"
Sam Panzer8e0df502012-08-24 23:29:33 +000045#include "llvm/Support/CommandLine.h"
46#include "llvm/Support/MemoryBuffer.h"
NAKAMURA Takumi40ac1122013-01-18 14:31:00 +000047#include "llvm/Support/Signals.h"
Sam Panzer8e0df502012-08-24 23:29:33 +000048
49using namespace clang;
50using namespace clang::ast_matchers;
51using namespace clang::tooling;
52using namespace llvm;
53
54namespace {
55class ToolTemplateCallback : public MatchFinder::MatchCallback {
Eric Liu267034c2016-08-01 10:16:39 +000056public:
57 ToolTemplateCallback(std::map<std::string, Replacements> *Replace)
58 : Replace(Replace) {}
Sam Panzer8e0df502012-08-24 23:29:33 +000059
Alexander Kornienkoda2734d2014-08-02 08:24:10 +000060 void run(const MatchFinder::MatchResult &Result) override {
Eric Liu267034c2016-08-01 10:16:39 +000061 // TODO: This routine will get called for each thing that the matchers
62 // find.
Alexander Kornienkoda2734d2014-08-02 08:24:10 +000063 // 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 Panzer8e0df502012-08-24 23:29:33 +000066 }
67
Eric Liu267034c2016-08-01 10:16:39 +000068private:
69 std::map<std::string, Replacements> *Replace;
Sam Panzer8e0df502012-08-24 23:29:33 +000070};
71} // end anonymous namespace
72
73// Set up the command line options
Alexander Kornienkoda2734d2014-08-02 08:24:10 +000074static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
75static cl::OptionCategory ToolTemplateCategory("tool-template options");
Sam Panzer8e0df502012-08-24 23:29:33 +000076
77int main(int argc, const char **argv) {
Richard Smith22252f92016-06-09 00:54:42 +000078 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
Alexander Kornienkoda2734d2014-08-02 08:24:10 +000079 CommonOptionsParser OptionsParser(argc, argv, ToolTemplateCategory);
80 RefactoringTool Tool(OptionsParser.getCompilations(),
81 OptionsParser.getSourcePathList());
Sam Panzer8e0df502012-08-24 23:29:33 +000082 ast_matchers::MatchFinder Finder;
83 ToolTemplateCallback Callback(&Tool.getReplacements());
84
Alexander Kornienkoda2734d2014-08-02 08:24:10 +000085 // 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 Panzer8e0df502012-08-24 23:29:33 +000088
Benjamin Kramerfb68e642014-04-28 10:06:50 +000089 return Tool.run(newFrontendActionFactory(&Finder).get());
Sam Panzer8e0df502012-08-24 23:29:33 +000090}