1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
//===--- Tooling.h - Framework for standalone Clang tools -------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements functions to run clang tools standalone instead
// of running them as a plugin.
//
// A ClangTool is initialized with a CompilationDatabase and a set of files
// to run over. The tool will then run a user-specified FrontendAction over
// all TUs in which the given files are compiled.
//
// It is also possible to run a FrontendAction over a snippet of code by
// calling runSyntaxOnlyToolOnCode, which is useful for unit testing.
//
// Applications that need more fine grained control over how to run
// multiple FrontendActions over code can use ToolInvocation.
//
// Example tools:
// - running clang -fsyntax-only over source code from an editor to get
// fast syntax checks
// - running match/replace tools over C++ code
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLING_TOOLING_H
#define LLVM_CLANG_TOOLING_TOOLING_H
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Twine.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/LLVM.h"
#include "clang/Driver/Util.h"
#include <string>
#include <vector>
namespace clang {
namespace driver {
class Compilation;
} // end namespace driver
class CompilerInvocation;
class SourceManager;
class FrontendAction;
namespace tooling {
class CompilationDatabase;
/// \brief Interface to generate clang::FrontendActions.
class FrontendActionFactory {
public:
virtual ~FrontendActionFactory();
/// \brief Returns a new clang::FrontendAction.
///
/// The caller takes ownership of the returned action.
virtual clang::FrontendAction *create() = 0;
};
/// \brief Returns a new FrontendActionFactory for a given type.
///
/// T must extend clang::FrontendAction.
///
/// Example:
/// FrontendActionFactory *Factory =
/// newFrontendActionFactory<clang::SyntaxOnlyAction>();
template <typename T>
FrontendActionFactory *newFrontendActionFactory();
/// \brief Returns a new FrontendActionFactory for any type that provides an
/// implementation of newFrontendAction().
///
/// FactoryT must implement: FrontendAction *newFrontendAction().
///
/// Example:
/// struct ProvidesFrontendActions {
/// FrontendAction *newFrontendAction();
/// } Factory;
/// FrontendActionFactory *FactoryAdapter =
/// newFrontendActionFactory(&Factory);
template <typename FactoryT>
FrontendActionFactory *newFrontendActionFactory(FactoryT *ActionFactory);
/// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag.
///
/// \param ToolAction The action to run over the code.
/// \param Code C++ code.
/// \param FileName The file name which 'Code' will be mapped as.
///
/// \return - True if 'ToolAction' was successfully executed.
bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code,
const Twine &FileName = "input.cc");
/// \brief Utility to run a FrontendAction in a single clang invocation.
class ToolInvocation {
public:
/// \brief Create a tool invocation.
///
/// \param CommandLine The command line arguments to clang.
/// \param ToolAction The action to be executed. Class takes ownership.
/// \param Files The FileManager used for the execution. Class does not take
/// ownership.
ToolInvocation(ArrayRef<std::string> CommandLine, FrontendAction *ToolAction,
FileManager *Files);
/// \brief Map a virtual file to be used while running the tool.
///
/// \param FilePath The path at which the content will be mapped.
/// \param Content A null terminated buffer of the file's content.
void mapVirtualFile(StringRef FilePath, StringRef Content);
/// \brief Run the clang invocation.
///
/// \returns True if there were no errors during execution.
bool run();
private:
void addFileMappingsTo(SourceManager &SourceManager);
bool runInvocation(const char *BinaryName,
clang::driver::Compilation *Compilation,
clang::CompilerInvocation *Invocation,
const clang::driver::ArgStringList &CC1Args,
clang::FrontendAction *ToolAction);
std::vector<std::string> CommandLine;
llvm::OwningPtr<FrontendAction> ToolAction;
FileManager *Files;
// Maps <file name> -> <file content>.
llvm::StringMap<StringRef> MappedFileContents;
};
/// \brief Utility to run a FrontendAction over a set of files.
///
/// This class is written to be usable for command line utilities.
class ClangTool {
public:
/// \brief Constructs a clang tool to run over a list of files.
///
/// \param Compilations The CompilationDatabase which contains the compile
/// command lines for the given source paths.
/// \param SourcePaths The source files to run over. If a source files is
/// not found in Compilations, it is skipped.
ClangTool(const CompilationDatabase &Compilations,
ArrayRef<std::string> SourcePaths);
/// \brief Map a virtual file to be used while running the tool.
///
/// \param FilePath The path at which the content will be mapped.
/// \param Content A null terminated buffer of the file's content.
void mapVirtualFile(StringRef FilePath, StringRef Content);
/// Runs a frontend action over all files specified in the command line.
///
/// \param ActionFactory Factory generating the frontend actions. The function
/// takes ownership of this parameter. A new action is generated for every
/// processed translation unit.
int run(FrontendActionFactory *ActionFactory);
/// \brief Returns the file manager used in the tool.
///
/// The file manager is shared between all translation units.
FileManager &getFiles() { return Files; }
private:
// We store command lines as pair (file name, command line).
typedef std::pair< std::string, std::vector<std::string> > CommandLine;
std::vector<CommandLine> CommandLines;
FileManager Files;
// Contains a list of pairs (<file name>, <file content>).
std::vector< std::pair<StringRef, StringRef> > MappedFileContents;
};
template <typename T>
FrontendActionFactory *newFrontendActionFactory() {
class SimpleFrontendActionFactory : public FrontendActionFactory {
public:
virtual clang::FrontendAction *create() { return new T; }
};
return new SimpleFrontendActionFactory;
}
template <typename FactoryT>
FrontendActionFactory *newFrontendActionFactory(FactoryT *ActionFactory) {
class FrontendActionFactoryAdapter : public FrontendActionFactory {
public:
explicit FrontendActionFactoryAdapter(FactoryT *ActionFactory)
: ActionFactory(ActionFactory) {}
virtual clang::FrontendAction *create() {
return ActionFactory->newFrontendAction();
}
private:
FactoryT *ActionFactory;
};
return new FrontendActionFactoryAdapter(ActionFactory);
}
} // end namespace tooling
} // end namespace clang
#endif // LLVM_CLANG_TOOLING_TOOLING_H
|