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