2012-08-25 07:29:33 +08:00
|
|
|
//===---- tools/extra/ToolTemplate.cpp - Template for refactoring tool ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements an empty refactoring tool using the clang tooling.
|
|
|
|
// The goal is to lower the "barrier to entry" for writing refactoring tools.
|
|
|
|
//
|
|
|
|
// Usage:
|
|
|
|
// tool-template <cmake-output-dir> <file1> <file2> ...
|
|
|
|
//
|
|
|
|
// Where <cmake-output-dir> is a CMake build directory in which a file named
|
|
|
|
// compile_commands.json exists (enable -DCMAKE_EXPORT_COMPILE_COMMANDS in
|
|
|
|
// CMake to get this output).
|
|
|
|
//
|
|
|
|
// <file1> ... specify the paths of files in the CMake source tree. This path
|
|
|
|
// is looked up in the compile command database. If the path of a file is
|
|
|
|
// absolute, it needs to point into CMake's source tree. If the path is
|
|
|
|
// relative, the current working directory needs to be in the CMake source
|
|
|
|
// tree and the file must be in a subdirectory of the current working
|
|
|
|
// directory. "./" prefixes in the relative files will be automatically
|
|
|
|
// removed, but the rest of a relative path must be a suffix of a path in
|
|
|
|
// the compile command line database.
|
|
|
|
//
|
|
|
|
// For example, to use tool-template on all files in a subtree of the
|
|
|
|
// source tree, use:
|
|
|
|
//
|
|
|
|
// /path/in/subtree $ find . -name '*.cpp'|
|
|
|
|
// xargs tool-template /path/to/build
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
2014-08-02 16:24:10 +08:00
|
|
|
#include "clang/Tooling/CommonOptionsParser.h"
|
2012-08-25 07:29:33 +08:00
|
|
|
#include "clang/Tooling/Refactoring.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2013-01-18 22:31:00 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2012-08-25 07:29:33 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
using namespace clang::tooling;
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class ToolTemplateCallback : public MatchFinder::MatchCallback {
|
|
|
|
public:
|
|
|
|
ToolTemplateCallback(Replacements *Replace) : Replace(Replace) {}
|
|
|
|
|
2014-08-02 16:24:10 +08:00
|
|
|
void run(const MatchFinder::MatchResult &Result) override {
|
|
|
|
// TODO: This routine will get called for each thing that the matchers find.
|
|
|
|
// At this point, you can examine the match, and do whatever you want,
|
|
|
|
// including replacing the matched text with other text
|
|
|
|
(void)Replace; // This to prevent an "unused member variable" warning;
|
2012-08-25 07:29:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Replacements *Replace;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
// Set up the command line options
|
2014-08-02 16:24:10 +08:00
|
|
|
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
|
|
|
|
static cl::OptionCategory ToolTemplateCategory("tool-template options");
|
2012-08-25 07:29:33 +08:00
|
|
|
|
|
|
|
int main(int argc, const char **argv) {
|
2016-06-09 08:54:42 +08:00
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2014-08-02 16:24:10 +08:00
|
|
|
CommonOptionsParser OptionsParser(argc, argv, ToolTemplateCategory);
|
|
|
|
RefactoringTool Tool(OptionsParser.getCompilations(),
|
|
|
|
OptionsParser.getSourcePathList());
|
2012-08-25 07:29:33 +08:00
|
|
|
ast_matchers::MatchFinder Finder;
|
|
|
|
ToolTemplateCallback Callback(&Tool.getReplacements());
|
|
|
|
|
2014-08-02 16:24:10 +08:00
|
|
|
// TODO: Put your matchers here.
|
|
|
|
// Use Finder.addMatcher(...) to define the patterns in the AST that you
|
|
|
|
// want to match against. You are not limited to just one matcher!
|
2012-08-25 07:29:33 +08:00
|
|
|
|
2014-04-28 18:06:50 +08:00
|
|
|
return Tool.run(newFrontendActionFactory(&Finder).get());
|
2012-08-25 07:29:33 +08:00
|
|
|
}
|