1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | #ifndef LLVM_CLANG_FRONTEND_FRONTENDACTION_H |
18 | #define LLVM_CLANG_FRONTEND_FRONTENDACTION_H |
19 | |
20 | #include "clang/AST/ASTConsumer.h" |
21 | #include "clang/Basic/LLVM.h" |
22 | #include "clang/Basic/LangOptions.h" |
23 | #include "clang/Frontend/ASTUnit.h" |
24 | #include "clang/Frontend/FrontendOptions.h" |
25 | #include "llvm/ADT/StringRef.h" |
26 | #include <memory> |
27 | #include <string> |
28 | #include <vector> |
29 | |
30 | namespace clang { |
31 | class ASTMergeAction; |
32 | class CompilerInstance; |
33 | |
34 | |
35 | class FrontendAction { |
36 | FrontendInputFile CurrentInput; |
37 | std::unique_ptr<ASTUnit> CurrentASTUnit; |
38 | CompilerInstance *Instance; |
39 | friend class ASTMergeAction; |
40 | friend class WrapperFrontendAction; |
41 | |
42 | private: |
43 | std::unique_ptr<ASTConsumer> CreateWrappedASTConsumer(CompilerInstance &CI, |
44 | StringRef InFile); |
45 | |
46 | protected: |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | virtual bool PrepareToExecuteAction(CompilerInstance &CI) { return true; } |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | virtual std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
70 | StringRef InFile) = 0; |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | |
77 | |
78 | virtual bool BeginInvocation(CompilerInstance &CI) { return true; } |
79 | |
80 | |
81 | |
82 | |
83 | |
84 | virtual bool BeginSourceFileAction(CompilerInstance &CI) { |
85 | return true; |
86 | } |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | virtual void ExecuteAction() = 0; |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | virtual void EndSourceFileAction() {} |
100 | |
101 | |
102 | |
103 | |
104 | |
105 | |
106 | |
107 | virtual bool shouldEraseOutputFiles(); |
108 | |
109 | |
110 | |
111 | public: |
112 | FrontendAction(); |
113 | virtual ~FrontendAction(); |
114 | |
115 | |
116 | |
117 | |
118 | CompilerInstance &getCompilerInstance() const { |
119 | (0) . __assert_fail ("Instance && \"Compiler instance not registered!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Frontend/FrontendAction.h", 119, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(Instance && "Compiler instance not registered!"); |
120 | return *Instance; |
121 | } |
122 | |
123 | void setCompilerInstance(CompilerInstance *Value) { Instance = Value; } |
124 | |
125 | |
126 | |
127 | |
128 | |
129 | bool isCurrentFileAST() const { |
130 | (0) . __assert_fail ("!CurrentInput.isEmpty() && \"No current file!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Frontend/FrontendAction.h", 130, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!CurrentInput.isEmpty() && "No current file!"); |
131 | return (bool)CurrentASTUnit; |
132 | } |
133 | |
134 | const FrontendInputFile &getCurrentInput() const { |
135 | return CurrentInput; |
136 | } |
137 | |
138 | StringRef getCurrentFile() const { |
139 | (0) . __assert_fail ("!CurrentInput.isEmpty() && \"No current file!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Frontend/FrontendAction.h", 139, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!CurrentInput.isEmpty() && "No current file!"); |
140 | return CurrentInput.getFile(); |
141 | } |
142 | |
143 | StringRef getCurrentFileOrBufferName() const { |
144 | (0) . __assert_fail ("!CurrentInput.isEmpty() && \"No current file!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Frontend/FrontendAction.h", 144, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!CurrentInput.isEmpty() && "No current file!"); |
145 | return CurrentInput.isFile() |
146 | ? CurrentInput.getFile() |
147 | : CurrentInput.getBuffer()->getBufferIdentifier(); |
148 | } |
149 | |
150 | InputKind getCurrentFileKind() const { |
151 | (0) . __assert_fail ("!CurrentInput.isEmpty() && \"No current file!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Frontend/FrontendAction.h", 151, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!CurrentInput.isEmpty() && "No current file!"); |
152 | return CurrentInput.getKind(); |
153 | } |
154 | |
155 | ASTUnit &getCurrentASTUnit() const { |
156 | (0) . __assert_fail ("CurrentASTUnit && \"No current AST unit!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/Frontend/FrontendAction.h", 156, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(CurrentASTUnit && "No current AST unit!"); |
157 | return *CurrentASTUnit; |
158 | } |
159 | |
160 | Module *getCurrentModule() const; |
161 | |
162 | std::unique_ptr<ASTUnit> takeCurrentASTUnit() { |
163 | return std::move(CurrentASTUnit); |
164 | } |
165 | |
166 | void setCurrentInput(const FrontendInputFile &CurrentInput, |
167 | std::unique_ptr<ASTUnit> AST = nullptr); |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | |
174 | |
175 | |
176 | |
177 | |
178 | virtual bool isModelParsingAction() const { return false; } |
179 | |
180 | |
181 | |
182 | |
183 | |
184 | virtual bool usesPreprocessorOnly() const = 0; |
185 | |
186 | |
187 | virtual TranslationUnitKind getTranslationUnitKind() { return TU_Complete; } |
188 | |
189 | |
190 | virtual bool hasPCHSupport() const { return true; } |
191 | |
192 | |
193 | virtual bool hasASTFileSupport() const { return true; } |
194 | |
195 | |
196 | virtual bool hasIRSupport() const { return false; } |
197 | |
198 | |
199 | virtual bool hasCodeCompletionSupport() const { return false; } |
200 | |
201 | |
202 | |
203 | |
204 | |
205 | |
206 | bool PrepareToExecute(CompilerInstance &CI) { |
207 | return PrepareToExecuteAction(CI); |
208 | } |
209 | |
210 | |
211 | |
212 | |
213 | |
214 | |
215 | |
216 | |
217 | |
218 | |
219 | |
220 | |
221 | |
222 | |
223 | |
224 | |
225 | |
226 | |
227 | |
228 | |
229 | bool BeginSourceFile(CompilerInstance &CI, const FrontendInputFile &Input); |
230 | |
231 | |
232 | bool Execute(); |
233 | |
234 | |
235 | |
236 | void EndSourceFile(); |
237 | |
238 | |
239 | }; |
240 | |
241 | |
242 | class ASTFrontendAction : public FrontendAction { |
243 | protected: |
244 | |
245 | |
246 | |
247 | |
248 | |
249 | void ExecuteAction() override; |
250 | |
251 | public: |
252 | ASTFrontendAction() {} |
253 | bool usesPreprocessorOnly() const override { return false; } |
254 | }; |
255 | |
256 | class PluginASTAction : public ASTFrontendAction { |
257 | virtual void anchor(); |
258 | public: |
259 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
260 | StringRef InFile) override = 0; |
261 | |
262 | |
263 | |
264 | |
265 | |
266 | |
267 | |
268 | virtual bool ParseArgs(const CompilerInstance &CI, |
269 | const std::vector<std::string> &arg) = 0; |
270 | |
271 | enum ActionType { |
272 | Cmdline, |
273 | ReplaceAction, |
274 | AddBeforeMainAction, |
275 | AddAfterMainAction |
276 | }; |
277 | |
278 | |
279 | |
280 | |
281 | |
282 | virtual ActionType getActionType() { return Cmdline; } |
283 | }; |
284 | |
285 | |
286 | class PreprocessorFrontendAction : public FrontendAction { |
287 | protected: |
288 | |
289 | |
290 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
291 | StringRef InFile) override; |
292 | |
293 | public: |
294 | bool usesPreprocessorOnly() const override { return true; } |
295 | }; |
296 | |
297 | |
298 | |
299 | |
300 | |
301 | |
302 | |
303 | class WrapperFrontendAction : public FrontendAction { |
304 | std::unique_ptr<FrontendAction> WrappedAction; |
305 | |
306 | protected: |
307 | bool PrepareToExecuteAction(CompilerInstance &CI) override; |
308 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
309 | StringRef InFile) override; |
310 | bool BeginInvocation(CompilerInstance &CI) override; |
311 | bool BeginSourceFileAction(CompilerInstance &CI) override; |
312 | void ExecuteAction() override; |
313 | void EndSourceFileAction() override; |
314 | |
315 | public: |
316 | |
317 | |
318 | WrapperFrontendAction(std::unique_ptr<FrontendAction> WrappedAction); |
319 | |
320 | bool usesPreprocessorOnly() const override; |
321 | TranslationUnitKind getTranslationUnitKind() override; |
322 | bool hasPCHSupport() const override; |
323 | bool hasASTFileSupport() const override; |
324 | bool hasIRSupport() const override; |
325 | bool hasCodeCompletionSupport() const override; |
326 | }; |
327 | |
328 | } |
329 | |
330 | #endif |
331 | |