xref: /minix3/external/bsd/llvm/dist/clang/include/clang/Frontend/FrontendActions.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 //===-- FrontendActions.h - Useful Frontend Actions -------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
11 #define LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
12 
13 #include "clang/Frontend/FrontendAction.h"
14 #include <string>
15 #include <vector>
16 
17 namespace clang {
18 
19 class Module;
20 class FileEntry;
21 
22 //===----------------------------------------------------------------------===//
23 // Custom Consumer Actions
24 //===----------------------------------------------------------------------===//
25 
26 class InitOnlyAction : public FrontendAction {
27   void ExecuteAction() override;
28 
29   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
30                                                  StringRef InFile) override;
31 
32 public:
33   // Don't claim to only use the preprocessor, we want to follow the AST path,
34   // but do nothing.
usesPreprocessorOnly()35   bool usesPreprocessorOnly() const override { return false; }
36 };
37 
38 //===----------------------------------------------------------------------===//
39 // AST Consumer Actions
40 //===----------------------------------------------------------------------===//
41 
42 class ASTPrintAction : public ASTFrontendAction {
43 protected:
44   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
45                                                  StringRef InFile) override;
46 };
47 
48 class ASTDumpAction : public ASTFrontendAction {
49 protected:
50   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
51                                                  StringRef InFile) override;
52 };
53 
54 class ASTDeclListAction : public ASTFrontendAction {
55 protected:
56   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
57                                                  StringRef InFile) override;
58 };
59 
60 class ASTViewAction : public ASTFrontendAction {
61 protected:
62   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
63                                                  StringRef InFile) override;
64 };
65 
66 class DeclContextPrintAction : public ASTFrontendAction {
67 protected:
68   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
69                                                  StringRef InFile) override;
70 };
71 
72 class GeneratePCHAction : public ASTFrontendAction {
73 protected:
74   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
75                                                  StringRef InFile) override;
76 
getTranslationUnitKind()77   TranslationUnitKind getTranslationUnitKind() override {
78     return TU_Prefix;
79   }
80 
hasASTFileSupport()81   bool hasASTFileSupport() const override { return false; }
82 
83 public:
84   /// \brief Compute the AST consumer arguments that will be used to
85   /// create the PCHGenerator instance returned by CreateASTConsumer.
86   ///
87   /// \returns true if an error occurred, false otherwise.
88   static bool ComputeASTConsumerArguments(CompilerInstance &CI,
89                                           StringRef InFile,
90                                           std::string &Sysroot,
91                                           std::string &OutputFile,
92                                           raw_ostream *&OS);
93 };
94 
95 class GenerateModuleAction : public ASTFrontendAction {
96   clang::Module *Module;
97   const FileEntry *ModuleMapForUniquing;
98   bool IsSystem;
99 
100 protected:
101   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
102                                                  StringRef InFile) override;
103 
getTranslationUnitKind()104   TranslationUnitKind getTranslationUnitKind() override {
105     return TU_Module;
106   }
107 
hasASTFileSupport()108   bool hasASTFileSupport() const override { return false; }
109 
110 public:
111   GenerateModuleAction(const FileEntry *ModuleMap = nullptr,
112                        bool IsSystem = false)
ASTFrontendAction()113     : ASTFrontendAction(), ModuleMapForUniquing(ModuleMap), IsSystem(IsSystem)
114   { }
115 
116   bool BeginSourceFileAction(CompilerInstance &CI, StringRef Filename) override;
117 
118   /// \brief Compute the AST consumer arguments that will be used to
119   /// create the PCHGenerator instance returned by CreateASTConsumer.
120   ///
121   /// \returns true if an error occurred, false otherwise.
122   bool ComputeASTConsumerArguments(CompilerInstance &CI,
123                                    StringRef InFile,
124                                    std::string &Sysroot,
125                                    std::string &OutputFile,
126                                    raw_ostream *&OS);
127 };
128 
129 class SyntaxOnlyAction : public ASTFrontendAction {
130 protected:
131   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
132                                                  StringRef InFile) override;
133 
134 public:
hasCodeCompletionSupport()135   bool hasCodeCompletionSupport() const override { return true; }
136 };
137 
138 /// \brief Dump information about the given module file, to be used for
139 /// basic debugging and discovery.
140 class DumpModuleInfoAction : public ASTFrontendAction {
141 protected:
142   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
143                                                  StringRef InFile) override;
144   void ExecuteAction() override;
145 
146 public:
hasPCHSupport()147   bool hasPCHSupport() const override { return false; }
hasASTFileSupport()148   bool hasASTFileSupport() const override { return true; }
hasIRSupport()149   bool hasIRSupport() const override { return false; }
hasCodeCompletionSupport()150   bool hasCodeCompletionSupport() const override { return false; }
151 };
152 
153 class VerifyPCHAction : public ASTFrontendAction {
154 protected:
155   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
156                                                  StringRef InFile) override;
157 
158   void ExecuteAction() override;
159 
160 public:
hasCodeCompletionSupport()161   bool hasCodeCompletionSupport() const override { return false; }
162 };
163 
164 /**
165  * \brief Frontend action adaptor that merges ASTs together.
166  *
167  * This action takes an existing AST file and "merges" it into the AST
168  * context, producing a merged context. This action is an action
169  * adaptor, which forwards most of its calls to another action that
170  * will consume the merged context.
171  */
172 class ASTMergeAction : public FrontendAction {
173   /// \brief The action that the merge action adapts.
174   FrontendAction *AdaptedAction;
175 
176   /// \brief The set of AST files to merge.
177   std::vector<std::string> ASTFiles;
178 
179 protected:
180   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
181                                                  StringRef InFile) override;
182 
183   bool BeginSourceFileAction(CompilerInstance &CI,
184                              StringRef Filename) override;
185 
186   void ExecuteAction() override;
187   void EndSourceFileAction() override;
188 
189 public:
190   ASTMergeAction(FrontendAction *AdaptedAction, ArrayRef<std::string> ASTFiles);
191   virtual ~ASTMergeAction();
192 
193   bool usesPreprocessorOnly() const override;
194   TranslationUnitKind getTranslationUnitKind() override;
195   bool hasPCHSupport() const override;
196   bool hasASTFileSupport() const override;
197   bool hasCodeCompletionSupport() const override;
198 };
199 
200 class PrintPreambleAction : public FrontendAction {
201 protected:
202   void ExecuteAction() override;
CreateASTConsumer(CompilerInstance &,StringRef)203   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
204                                                  StringRef) override {
205     return nullptr;
206   }
207 
usesPreprocessorOnly()208   bool usesPreprocessorOnly() const override { return true; }
209 };
210 
211 //===----------------------------------------------------------------------===//
212 // Preprocessor Actions
213 //===----------------------------------------------------------------------===//
214 
215 class DumpRawTokensAction : public PreprocessorFrontendAction {
216 protected:
217   void ExecuteAction() override;
218 };
219 
220 class DumpTokensAction : public PreprocessorFrontendAction {
221 protected:
222   void ExecuteAction() override;
223 };
224 
225 class GeneratePTHAction : public PreprocessorFrontendAction {
226 protected:
227   void ExecuteAction() override;
228 };
229 
230 class PreprocessOnlyAction : public PreprocessorFrontendAction {
231 protected:
232   void ExecuteAction() override;
233 };
234 
235 class PrintPreprocessedAction : public PreprocessorFrontendAction {
236 protected:
237   void ExecuteAction() override;
238 
hasPCHSupport()239   bool hasPCHSupport() const override { return true; }
240 };
241 
242 }  // end namespace clang
243 
244 #endif
245