1//===-- FrontendActions.h - Useful Frontend Actions -------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
10#define LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
11
12#include "clang/Frontend/FrontendAction.h"
13#include <memory>
14#include <string>
15#include <vector>
16
17namespace clang {
18
19//===----------------------------------------------------------------------===//
20// Custom Consumer Actions
21//===----------------------------------------------------------------------===//
22
23class InitOnlyAction : public FrontendAction {
24 void ExecuteAction() override;
25
26 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
27 StringRef InFile) override;
28
29public:
30 // Don't claim to only use the preprocessor, we want to follow the AST path,
31 // but do nothing.
32 bool usesPreprocessorOnly() const override { return false; }
33};
34
35/// Preprocessor-based frontend action that also loads PCH files.
36class ReadPCHAndPreprocessAction : public FrontendAction {
37 void ExecuteAction() override;
38
39 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
40 StringRef InFile) override;
41
42public:
43 bool usesPreprocessorOnly() const override { return false; }
44};
45
46class DumpCompilerOptionsAction : public FrontendAction {
47 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
48 StringRef InFile) override {
49 return nullptr;
50 }
51
52 void ExecuteAction() override;
53
54public:
55 bool usesPreprocessorOnly() const override { return true; }
56};
57
58//===----------------------------------------------------------------------===//
59// AST Consumer Actions
60//===----------------------------------------------------------------------===//
61
62class ASTPrintAction : public ASTFrontendAction {
63protected:
64 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
65 StringRef InFile) override;
66};
67
68class ASTDumpAction : public ASTFrontendAction {
69protected:
70 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
71 StringRef InFile) override;
72};
73
74class ASTDeclListAction : public ASTFrontendAction {
75protected:
76 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
77 StringRef InFile) override;
78};
79
80class ASTViewAction : public ASTFrontendAction {
81protected:
82 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
83 StringRef InFile) override;
84};
85
86class GeneratePCHAction : public ASTFrontendAction {
87protected:
88 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
89 StringRef InFile) override;
90
91 TranslationUnitKind getTranslationUnitKind() override {
92 return TU_Prefix;
93 }
94
95 bool hasASTFileSupport() const override { return false; }
96
97 bool shouldEraseOutputFiles() override;
98
99public:
100 /// Compute the AST consumer arguments that will be used to
101 /// create the PCHGenerator instance returned by CreateASTConsumer.
102 ///
103 /// \returns false if an error occurred, true otherwise.
104 static bool ComputeASTConsumerArguments(CompilerInstance &CI,
105 std::string &Sysroot);
106
107 /// Creates file to write the PCH into and returns a stream to write it
108 /// into. On error, returns null.
109 static std::unique_ptr<llvm::raw_pwrite_stream>
110 CreateOutputFile(CompilerInstance &CI, StringRef InFile,
111 std::string &OutputFile);
112
113 bool BeginSourceFileAction(CompilerInstance &CI) override;
114};
115
116class GenerateModuleAction : public ASTFrontendAction {
117 virtual std::unique_ptr<raw_pwrite_stream>
118 CreateOutputFile(CompilerInstance &CI, StringRef InFile) = 0;
119
120protected:
121 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
122 StringRef InFile) override;
123
124 TranslationUnitKind getTranslationUnitKind() override {
125 return TU_Module;
126 }
127
128 bool hasASTFileSupport() const override { return false; }
129
130 bool shouldEraseOutputFiles() override;
131};
132
133class InstallAPIAction : public ASTFrontendAction {
134protected:
135 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
136 StringRef InFile) override;
137
138public:
139 static std::unique_ptr<llvm::raw_pwrite_stream>
140 CreateOutputFile(CompilerInstance &CI, StringRef InFile);
141};
142
143class GenerateInterfaceStubsAction : public ASTFrontendAction {
144protected:
145 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
146 StringRef InFile) override;
147
148 TranslationUnitKind getTranslationUnitKind() override { return TU_Module; }
149 bool hasASTFileSupport() const override { return false; }
150};
151
152class GenerateModuleFromModuleMapAction : public GenerateModuleAction {
153private:
154 bool BeginSourceFileAction(CompilerInstance &CI) override;
155
156 std::unique_ptr<raw_pwrite_stream>
157 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
158};
159
160class GenerateModuleInterfaceAction : public GenerateModuleAction {
161private:
162 bool BeginSourceFileAction(CompilerInstance &CI) override;
163
164 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
165 StringRef InFile) override;
166
167 std::unique_ptr<raw_pwrite_stream>
168 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
169};
170
171class GenerateHeaderUnitAction : public GenerateModuleAction {
172
173private:
174 bool BeginSourceFileAction(CompilerInstance &CI) override;
175
176 std::unique_ptr<raw_pwrite_stream>
177 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
178};
179
180class SyntaxOnlyAction : public ASTFrontendAction {
181protected:
182 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
183 StringRef InFile) override;
184
185public:
186 ~SyntaxOnlyAction() override;
187 bool hasCodeCompletionSupport() const override { return true; }
188};
189
190/// Dump information about the given module file, to be used for
191/// basic debugging and discovery.
192class DumpModuleInfoAction : public ASTFrontendAction {
193 // Allow other tools (ex lldb) to direct output for their use.
194 std::shared_ptr<llvm::raw_ostream> OutputStream;
195
196protected:
197 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
198 StringRef InFile) override;
199 bool BeginInvocation(CompilerInstance &CI) override;
200 void ExecuteAction() override;
201
202public:
203 DumpModuleInfoAction() = default;
204 explicit DumpModuleInfoAction(std::shared_ptr<llvm::raw_ostream> Out)
205 : OutputStream(Out) {}
206 bool hasPCHSupport() const override { return false; }
207 bool hasASTFileSupport() const override { return true; }
208 bool hasIRSupport() const override { return false; }
209 bool hasCodeCompletionSupport() const override { return false; }
210};
211
212class VerifyPCHAction : public ASTFrontendAction {
213protected:
214 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
215 StringRef InFile) override;
216
217 void ExecuteAction() override;
218
219public:
220 bool hasCodeCompletionSupport() const override { return false; }
221};
222
223class TemplightDumpAction : public ASTFrontendAction {
224protected:
225 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
226 StringRef InFile) override;
227
228 void ExecuteAction() override;
229};
230
231/**
232 * Frontend action adaptor that merges ASTs together.
233 *
234 * This action takes an existing AST file and "merges" it into the AST
235 * context, producing a merged context. This action is an action
236 * adaptor, which forwards most of its calls to another action that
237 * will consume the merged context.
238 */
239class ASTMergeAction : public FrontendAction {
240 /// The action that the merge action adapts.
241 std::unique_ptr<FrontendAction> AdaptedAction;
242
243 /// The set of AST files to merge.
244 std::vector<std::string> ASTFiles;
245
246protected:
247 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
248 StringRef InFile) override;
249
250 bool BeginSourceFileAction(CompilerInstance &CI) override;
251
252 void ExecuteAction() override;
253 void EndSourceFileAction() override;
254
255public:
256 ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
257 ArrayRef<std::string> ASTFiles);
258 ~ASTMergeAction() override;
259
260 bool usesPreprocessorOnly() const override;
261 TranslationUnitKind getTranslationUnitKind() override;
262 bool hasPCHSupport() const override;
263 bool hasASTFileSupport() const override;
264 bool hasCodeCompletionSupport() const override;
265};
266
267class PrintPreambleAction : public FrontendAction {
268protected:
269 void ExecuteAction() override;
270 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
271 StringRef) override {
272 return nullptr;
273 }
274
275 bool usesPreprocessorOnly() const override { return true; }
276};
277
278class PrintDependencyDirectivesSourceMinimizerAction : public FrontendAction {
279protected:
280 void ExecuteAction() override;
281 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
282 StringRef) override {
283 return nullptr;
284 }
285
286 bool usesPreprocessorOnly() const override { return true; }
287};
288
289//===----------------------------------------------------------------------===//
290// Preprocessor Actions
291//===----------------------------------------------------------------------===//
292
293class DumpRawTokensAction : public PreprocessorFrontendAction {
294protected:
295 void ExecuteAction() override;
296};
297
298class DumpTokensAction : public PreprocessorFrontendAction {
299protected:
300 void ExecuteAction() override;
301};
302
303class PreprocessOnlyAction : public PreprocessorFrontendAction {
304protected:
305 void ExecuteAction() override;
306};
307
308class PrintPreprocessedAction : public PreprocessorFrontendAction {
309protected:
310 void ExecuteAction() override;
311
312 bool hasPCHSupport() const override { return true; }
313};
314
315class GetDependenciesByModuleNameAction : public PreprocessOnlyAction {
316 StringRef ModuleName;
317 void ExecuteAction() override;
318
319public:
320 GetDependenciesByModuleNameAction(StringRef ModuleName)
321 : ModuleName(ModuleName) {}
322};
323
324} // end namespace clang
325
326#endif
327

source code of clang/include/clang/Frontend/FrontendActions.h