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