1 //===-- FrontendActions.h - Useful Frontend Actions -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
11 #define LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
13 #include "clang/Frontend/FrontendAction.h"
22 //===----------------------------------------------------------------------===//
23 // Custom Consumer Actions
24 //===----------------------------------------------------------------------===//
26 class InitOnlyAction : public FrontendAction {
27 void ExecuteAction() override;
29 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
30 StringRef InFile) override;
33 // Don't claim to only use the preprocessor, we want to follow the AST path,
35 bool usesPreprocessorOnly() const override { return false; }
38 class DumpCompilerOptionsAction : public FrontendAction {
39 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
40 StringRef InFile) override {
44 void ExecuteAction() override;
47 bool usesPreprocessorOnly() const override { return true; }
50 //===----------------------------------------------------------------------===//
51 // AST Consumer Actions
52 //===----------------------------------------------------------------------===//
54 class ASTPrintAction : public ASTFrontendAction {
56 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
57 StringRef InFile) override;
60 class ASTDumpAction : public ASTFrontendAction {
62 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
63 StringRef InFile) override;
66 class ASTDeclListAction : public ASTFrontendAction {
68 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
69 StringRef InFile) override;
72 class ASTViewAction : public ASTFrontendAction {
74 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
75 StringRef InFile) override;
78 class DeclContextPrintAction : public ASTFrontendAction {
80 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
81 StringRef InFile) override;
84 class GeneratePCHAction : public ASTFrontendAction {
86 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
87 StringRef InFile) override;
89 TranslationUnitKind getTranslationUnitKind() override {
93 bool hasASTFileSupport() const override { return false; }
95 bool shouldEraseOutputFiles() override;
98 /// Compute the AST consumer arguments that will be used to
99 /// create the PCHGenerator instance returned by CreateASTConsumer.
101 /// \returns false if an error occurred, true otherwise.
102 static bool ComputeASTConsumerArguments(CompilerInstance &CI,
103 std::string &Sysroot);
105 /// Creates file to write the PCH into and returns a stream to write it
106 /// into. On error, returns null.
107 static std::unique_ptr<llvm::raw_pwrite_stream>
108 CreateOutputFile(CompilerInstance &CI, StringRef InFile,
109 std::string &OutputFile);
111 bool BeginSourceFileAction(CompilerInstance &CI) override;
114 class GenerateModuleAction : public ASTFrontendAction {
115 virtual std::unique_ptr<raw_pwrite_stream>
116 CreateOutputFile(CompilerInstance &CI, StringRef InFile) = 0;
119 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
120 StringRef InFile) override;
122 TranslationUnitKind getTranslationUnitKind() override {
126 bool hasASTFileSupport() const override { return false; }
129 class GenerateModuleFromModuleMapAction : public GenerateModuleAction {
131 bool BeginSourceFileAction(CompilerInstance &CI) override;
133 std::unique_ptr<raw_pwrite_stream>
134 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
137 class GenerateModuleInterfaceAction : public GenerateModuleAction {
139 bool BeginSourceFileAction(CompilerInstance &CI) override;
141 std::unique_ptr<raw_pwrite_stream>
142 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
145 class GenerateHeaderModuleAction : public GenerateModuleAction {
146 /// The synthesized module input buffer for the current compilation.
147 std::unique_ptr<llvm::MemoryBuffer> Buffer;
148 std::vector<std::string> ModuleHeaders;
151 bool PrepareToExecuteAction(CompilerInstance &CI) override;
152 bool BeginSourceFileAction(CompilerInstance &CI) override;
154 std::unique_ptr<raw_pwrite_stream>
155 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
158 class SyntaxOnlyAction : public ASTFrontendAction {
160 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
161 StringRef InFile) override;
164 ~SyntaxOnlyAction() override;
165 bool hasCodeCompletionSupport() const override { return true; }
168 /// Dump information about the given module file, to be used for
169 /// basic debugging and discovery.
170 class DumpModuleInfoAction : public ASTFrontendAction {
172 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
173 StringRef InFile) override;
174 bool BeginInvocation(CompilerInstance &CI) override;
175 void ExecuteAction() override;
178 bool hasPCHSupport() const override { return false; }
179 bool hasASTFileSupport() const override { return true; }
180 bool hasIRSupport() const override { return false; }
181 bool hasCodeCompletionSupport() const override { return false; }
184 class VerifyPCHAction : public ASTFrontendAction {
186 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
187 StringRef InFile) override;
189 void ExecuteAction() override;
192 bool hasCodeCompletionSupport() const override { return false; }
195 class TemplightDumpAction : public ASTFrontendAction {
197 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
198 StringRef InFile) override;
200 void ExecuteAction() override;
204 * Frontend action adaptor that merges ASTs together.
206 * This action takes an existing AST file and "merges" it into the AST
207 * context, producing a merged context. This action is an action
208 * adaptor, which forwards most of its calls to another action that
209 * will consume the merged context.
211 class ASTMergeAction : public FrontendAction {
212 /// The action that the merge action adapts.
213 std::unique_ptr<FrontendAction> AdaptedAction;
215 /// The set of AST files to merge.
216 std::vector<std::string> ASTFiles;
219 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
220 StringRef InFile) override;
222 bool BeginSourceFileAction(CompilerInstance &CI) override;
224 void ExecuteAction() override;
225 void EndSourceFileAction() override;
228 ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
229 ArrayRef<std::string> ASTFiles);
230 ~ASTMergeAction() override;
232 bool usesPreprocessorOnly() const override;
233 TranslationUnitKind getTranslationUnitKind() override;
234 bool hasPCHSupport() const override;
235 bool hasASTFileSupport() const override;
236 bool hasCodeCompletionSupport() const override;
239 class PrintPreambleAction : public FrontendAction {
241 void ExecuteAction() override;
242 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
243 StringRef) override {
247 bool usesPreprocessorOnly() const override { return true; }
250 //===----------------------------------------------------------------------===//
251 // Preprocessor Actions
252 //===----------------------------------------------------------------------===//
254 class DumpRawTokensAction : public PreprocessorFrontendAction {
256 void ExecuteAction() override;
259 class DumpTokensAction : public PreprocessorFrontendAction {
261 void ExecuteAction() override;
264 class PreprocessOnlyAction : public PreprocessorFrontendAction {
266 void ExecuteAction() override;
269 class PrintPreprocessedAction : public PreprocessorFrontendAction {
271 void ExecuteAction() override;
273 bool hasPCHSupport() const override { return true; }
276 } // end namespace clang