1 //===---- tools/extra/ToolTemplate.cpp - Template for refactoring tool ----===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements an empty refactoring tool using the clang tooling.
10 // The goal is to lower the "barrier to entry" for writing refactoring tools.
13 // tool-template <cmake-output-dir> <file1> <file2> ...
15 // Where <cmake-output-dir> is a CMake build directory in which a file named
16 // compile_commands.json exists (enable -DCMAKE_EXPORT_COMPILE_COMMANDS in
17 // CMake to get this output).
19 // <file1> ... specify the paths of files in the CMake source tree. This path
20 // is looked up in the compile command database. If the path of a file is
21 // absolute, it needs to point into CMake's source tree. If the path is
22 // relative, the current working directory needs to be in the CMake source
23 // tree and the file must be in a subdirectory of the current working
24 // directory. "./" prefixes in the relative files will be automatically
25 // removed, but the rest of a relative path must be a suffix of a path in
26 // the compile command line database.
28 // For example, to use tool-template on all files in a subtree of the
31 // /path/in/subtree $ find . -name '*.cpp'|
32 // xargs tool-template /path/to/build
34 //===----------------------------------------------------------------------===//
36 #include "clang/ASTMatchers/ASTMatchFinder.h"
37 #include "clang/ASTMatchers/ASTMatchers.h"
38 #include "clang/Basic/SourceManager.h"
39 #include "clang/Frontend/FrontendActions.h"
40 #include "clang/Lex/Lexer.h"
41 #include "clang/Tooling/CommonOptionsParser.h"
42 #include "clang/Tooling/Execution.h"
43 #include "clang/Tooling/Refactoring.h"
44 #include "clang/Tooling/Refactoring/AtomicChange.h"
45 #include "clang/Tooling/Tooling.h"
46 #include "llvm/Support/CommandLine.h"
47 #include "llvm/Support/MemoryBuffer.h"
48 #include "llvm/Support/Signals.h"
50 using namespace clang
;
51 using namespace clang::ast_matchers
;
52 using namespace clang::tooling
;
56 class ToolTemplateCallback
: public MatchFinder::MatchCallback
{
58 ToolTemplateCallback(ExecutionContext
&Context
) : Context(Context
) {}
60 void run(const MatchFinder::MatchResult
&Result
) override
{
61 // TODO: This routine will get called for each thing that the matchers
63 // At this point, you can examine the match, and do whatever you want,
64 // including replacing the matched text with other text
65 auto *D
= Result
.Nodes
.getNodeAs
<NamedDecl
>("decl");
67 // Use AtomicChange to get a key.
68 if (D
->getBeginLoc().isValid()) {
69 AtomicChange
Change(*Result
.SourceManager
, D
->getBeginLoc());
70 Context
.reportResult(Change
.getKey(), D
->getQualifiedNameAsString());
74 void onStartOfTranslationUnit() override
{
75 Context
.reportResult("START", "Start of TU.");
77 void onEndOfTranslationUnit() override
{
78 Context
.reportResult("END", "End of TU.");
82 ExecutionContext
&Context
;
84 } // end anonymous namespace
86 // Set up the command line options
87 static cl::extrahelp
CommonHelp(CommonOptionsParser::HelpMessage
);
88 static cl::OptionCategory
ToolTemplateCategory("tool-template options");
90 int main(int argc
, const char **argv
) {
91 llvm::sys::PrintStackTraceOnErrorSignal(argv
[0]);
93 auto Executor
= clang::tooling::createExecutorFromCommandLineArgs(
94 argc
, argv
, ToolTemplateCategory
);
97 llvm::errs() << llvm::toString(Executor
.takeError()) << "\n";
101 ast_matchers::MatchFinder Finder
;
102 ToolTemplateCallback
Callback(*Executor
->get()->getExecutionContext());
104 // TODO: Put your matchers here.
105 // Use Finder.addMatcher(...) to define the patterns in the AST that you
106 // want to match against. You are not limited to just one matcher!
108 // This is a sample matcher:
110 namedDecl(cxxRecordDecl(), isExpansionInMainFile()).bind("decl"),
113 auto Err
= Executor
->get()->execute(newFrontendActionFactory(&Finder
));
115 llvm::errs() << llvm::toString(std::move(Err
)) << "\n";
117 Executor
->get()->getToolResults()->forEachResult(
118 [](llvm::StringRef key
, llvm::StringRef value
) {
119 llvm::errs() << "----" << key
.str() << "\n" << value
.str() << "\n";