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.
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:
43   bool usesPreprocessorOnly() const override { return false; }
44 };
45 
46 class 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 
54 public:
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 
91   TranslationUnitKind getTranslationUnitKind() override {
92     return TU_Prefix;
93   }
94 
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::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 
133 class GenerateInterfaceStubsAction : public ASTFrontendAction {
134 protected:
135   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
136                                                  StringRef InFile) override;
137 
138   TranslationUnitKind getTranslationUnitKind() override { return TU_Module; }
139   bool hasASTFileSupport() const override { return false; }
140 };
141 
142 class GenerateModuleFromModuleMapAction : public GenerateModuleAction {
143 private:
144   bool BeginSourceFileAction(CompilerInstance &CI) override;
145 
146   std::unique_ptr<raw_pwrite_stream>
147   CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
148 };
149 
150 class GenerateModuleInterfaceAction : public GenerateModuleAction {
151 private:
152   bool BeginSourceFileAction(CompilerInstance &CI) override;
153 
154   std::unique_ptr<raw_pwrite_stream>
155   CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
156 };
157 
158 class GenerateHeaderUnitAction : public GenerateModuleAction {
159 
160 private:
161   bool BeginSourceFileAction(CompilerInstance &CI) override;
162 
163   std::unique_ptr<raw_pwrite_stream>
164   CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
165 };
166 
167 class SyntaxOnlyAction : public ASTFrontendAction {
168 protected:
169   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
170                                                  StringRef InFile) override;
171 
172 public:
173   ~SyntaxOnlyAction() override;
174   bool hasCodeCompletionSupport() const override { return true; }
175 };
176 
177 /// Dump information about the given module file, to be used for
178 /// basic debugging and discovery.
179 class DumpModuleInfoAction : public ASTFrontendAction {
180 public:
181   // Allow other tools (ex lldb) to direct output for their use.
182   llvm::raw_ostream *OutputStream = nullptr;
183 
184 protected:
185   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
186                                                  StringRef InFile) override;
187   bool BeginInvocation(CompilerInstance &CI) override;
188   void ExecuteAction() override;
189 
190 public:
191   bool hasPCHSupport() const override { return false; }
192   bool hasASTFileSupport() const override { return true; }
193   bool hasIRSupport() const override { return false; }
194   bool hasCodeCompletionSupport() const override { return false; }
195 };
196 
197 class VerifyPCHAction : public ASTFrontendAction {
198 protected:
199   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
200                                                  StringRef InFile) override;
201 
202   void ExecuteAction() override;
203 
204 public:
205   bool hasCodeCompletionSupport() const override { return false; }
206 };
207 
208 class TemplightDumpAction : public ASTFrontendAction {
209 protected:
210   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
211                                                  StringRef InFile) override;
212 
213   void ExecuteAction() override;
214 };
215 
216 /**
217  * Frontend action adaptor that merges ASTs together.
218  *
219  * This action takes an existing AST file and "merges" it into the AST
220  * context, producing a merged context. This action is an action
221  * adaptor, which forwards most of its calls to another action that
222  * will consume the merged context.
223  */
224 class ASTMergeAction : public FrontendAction {
225   /// The action that the merge action adapts.
226   std::unique_ptr<FrontendAction> AdaptedAction;
227 
228   /// The set of AST files to merge.
229   std::vector<std::string> ASTFiles;
230 
231 protected:
232   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
233                                                  StringRef InFile) override;
234 
235   bool BeginSourceFileAction(CompilerInstance &CI) override;
236 
237   void ExecuteAction() override;
238   void EndSourceFileAction() override;
239 
240 public:
241   ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
242                  ArrayRef<std::string> ASTFiles);
243   ~ASTMergeAction() override;
244 
245   bool usesPreprocessorOnly() const override;
246   TranslationUnitKind getTranslationUnitKind() override;
247   bool hasPCHSupport() const override;
248   bool hasASTFileSupport() const override;
249   bool hasCodeCompletionSupport() const override;
250 };
251 
252 class PrintPreambleAction : public FrontendAction {
253 protected:
254   void ExecuteAction() override;
255   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
256                                                  StringRef) override {
257     return nullptr;
258   }
259 
260   bool usesPreprocessorOnly() const override { return true; }
261 };
262 
263 class PrintDependencyDirectivesSourceMinimizerAction : public FrontendAction {
264 protected:
265   void ExecuteAction() override;
266   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
267                                                  StringRef) override {
268     return nullptr;
269   }
270 
271   bool usesPreprocessorOnly() const override { return true; }
272 };
273 
274 //===----------------------------------------------------------------------===//
275 // Preprocessor Actions
276 //===----------------------------------------------------------------------===//
277 
278 class DumpRawTokensAction : public PreprocessorFrontendAction {
279 protected:
280   void ExecuteAction() override;
281 };
282 
283 class DumpTokensAction : public PreprocessorFrontendAction {
284 protected:
285   void ExecuteAction() override;
286 };
287 
288 class PreprocessOnlyAction : public PreprocessorFrontendAction {
289 protected:
290   void ExecuteAction() override;
291 };
292 
293 class PrintPreprocessedAction : public PreprocessorFrontendAction {
294 protected:
295   void ExecuteAction() override;
296 
297   bool hasPCHSupport() const override { return true; }
298 };
299 
300 class GetDependenciesByModuleNameAction : public PreprocessOnlyAction {
301   StringRef ModuleName;
302   void ExecuteAction() override;
303 
304 public:
305   GetDependenciesByModuleNameAction(StringRef ModuleName)
306       : ModuleName(ModuleName) {}
307 };
308 
309 }  // end namespace clang
310 
311 #endif
312