xref: /freebsd/contrib/llvm-project/clang/include/clang/Frontend/FrontendActions.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 PrepareToExecuteAction(CompilerInstance &CI) override;
160   bool BeginSourceFileAction(CompilerInstance &CI) override;
161 
162   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
163                                                  StringRef InFile) override;
164 
getTranslationUnitKind()165   TranslationUnitKind getTranslationUnitKind() override { return TU_Complete; }
166 
167   std::unique_ptr<raw_pwrite_stream>
168   CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
169 };
170 
171 /// Only generates the reduced BMI. This action is mainly used by tests.
172 class GenerateReducedModuleInterfaceAction
173     : public GenerateModuleInterfaceAction {
174 private:
175   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
176                                                  StringRef InFile) override;
177 };
178 
179 class GenerateHeaderUnitAction : public GenerateModuleAction {
180 
181 private:
182   bool BeginSourceFileAction(CompilerInstance &CI) override;
183 
184   std::unique_ptr<raw_pwrite_stream>
185   CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
186 };
187 
188 class SyntaxOnlyAction : public ASTFrontendAction {
189 protected:
190   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
191                                                  StringRef InFile) override;
192 
193 public:
194   ~SyntaxOnlyAction() override;
hasCodeCompletionSupport()195   bool hasCodeCompletionSupport() const override { return true; }
196 };
197 
198 /// Dump information about the given module file, to be used for
199 /// basic debugging and discovery.
200 class DumpModuleInfoAction : public ASTFrontendAction {
201   // Allow other tools (ex lldb) to direct output for their use.
202   std::shared_ptr<llvm::raw_ostream> OutputStream;
203 
204 protected:
205   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
206                                                  StringRef InFile) override;
207   bool BeginInvocation(CompilerInstance &CI) override;
208   void ExecuteAction() override;
209 
210 public:
211   DumpModuleInfoAction() = default;
DumpModuleInfoAction(std::shared_ptr<llvm::raw_ostream> Out)212   explicit DumpModuleInfoAction(std::shared_ptr<llvm::raw_ostream> Out)
213       : OutputStream(Out) {}
hasPCHSupport()214   bool hasPCHSupport() const override { return false; }
hasASTFileSupport()215   bool hasASTFileSupport() const override { return true; }
hasIRSupport()216   bool hasIRSupport() const override { return false; }
hasCodeCompletionSupport()217   bool hasCodeCompletionSupport() const override { return false; }
218 };
219 
220 class VerifyPCHAction : public ASTFrontendAction {
221 protected:
222   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
223                                                  StringRef InFile) override;
224 
225   void ExecuteAction() override;
226 
227 public:
hasCodeCompletionSupport()228   bool hasCodeCompletionSupport() const override { return false; }
229 };
230 
231 class TemplightDumpAction : public ASTFrontendAction {
232 protected:
233   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
234                                                  StringRef InFile) override;
235 
236   void ExecuteAction() override;
237 };
238 
239 /**
240  * Frontend action adaptor that merges ASTs together.
241  *
242  * This action takes an existing AST file and "merges" it into the AST
243  * context, producing a merged context. This action is an action
244  * adaptor, which forwards most of its calls to another action that
245  * will consume the merged context.
246  */
247 class ASTMergeAction : public FrontendAction {
248   /// The action that the merge action adapts.
249   std::unique_ptr<FrontendAction> AdaptedAction;
250 
251   /// The set of AST files to merge.
252   std::vector<std::string> ASTFiles;
253 
254 protected:
255   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
256                                                  StringRef InFile) override;
257 
258   bool BeginSourceFileAction(CompilerInstance &CI) override;
259 
260   void ExecuteAction() override;
261   void EndSourceFileAction() override;
262 
263 public:
264   ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
265                  ArrayRef<std::string> ASTFiles);
266   ~ASTMergeAction() override;
267 
268   bool usesPreprocessorOnly() const override;
269   TranslationUnitKind getTranslationUnitKind() override;
270   bool hasPCHSupport() const override;
271   bool hasASTFileSupport() const override;
272   bool hasCodeCompletionSupport() const override;
273 };
274 
275 class PrintPreambleAction : public FrontendAction {
276 protected:
277   void ExecuteAction() override;
CreateASTConsumer(CompilerInstance &,StringRef)278   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
279                                                  StringRef) override {
280     return nullptr;
281   }
282 
usesPreprocessorOnly()283   bool usesPreprocessorOnly() const override { return true; }
284 };
285 
286 class PrintDependencyDirectivesSourceMinimizerAction : public FrontendAction {
287 protected:
288   void ExecuteAction() override;
CreateASTConsumer(CompilerInstance &,StringRef)289   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
290                                                  StringRef) override {
291     return nullptr;
292   }
293 
usesPreprocessorOnly()294   bool usesPreprocessorOnly() const override { return true; }
295 };
296 
297 //===----------------------------------------------------------------------===//
298 // Preprocessor Actions
299 //===----------------------------------------------------------------------===//
300 
301 class DumpRawTokensAction : public PreprocessorFrontendAction {
302 protected:
303   void ExecuteAction() override;
304 };
305 
306 class DumpTokensAction : public PreprocessorFrontendAction {
307 protected:
308   void ExecuteAction() override;
309 };
310 
311 class PreprocessOnlyAction : public PreprocessorFrontendAction {
312 protected:
313   void ExecuteAction() override;
314 };
315 
316 class PrintPreprocessedAction : public PreprocessorFrontendAction {
317 protected:
318   void ExecuteAction() override;
319 
hasPCHSupport()320   bool hasPCHSupport() const override { return true; }
321 };
322 
323 class GetDependenciesByModuleNameAction : public PreprocessOnlyAction {
324   StringRef ModuleName;
325   void ExecuteAction() override;
326 
327 public:
GetDependenciesByModuleNameAction(StringRef ModuleName)328   GetDependenciesByModuleNameAction(StringRef ModuleName)
329       : ModuleName(ModuleName) {}
330 };
331 
332 }  // end namespace clang
333 
334 #endif
335