1 //===-- FrontendActions.h - Useful Frontend Actions -------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
10 #define LLVM_CLANG_FRONTEND_FRONTENDACTIONS_H
12 #include "clang/Frontend/FrontendAction.h"
21 //===----------------------------------------------------------------------===//
22 // Custom Consumer Actions
23 //===----------------------------------------------------------------------===//
25 class InitOnlyAction : public FrontendAction {
26 void ExecuteAction() override;
28 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
29 StringRef InFile) override;
32 // Don't claim to only use the preprocessor, we want to follow the AST path,
34 bool usesPreprocessorOnly() const override { return false; }
37 class DumpCompilerOptionsAction : public FrontendAction {
38 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
39 StringRef InFile) override {
43 void ExecuteAction() override;
46 bool usesPreprocessorOnly() const override { return true; }
49 //===----------------------------------------------------------------------===//
50 // AST Consumer Actions
51 //===----------------------------------------------------------------------===//
53 class ASTPrintAction : public ASTFrontendAction {
55 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
56 StringRef InFile) override;
59 class ASTDumpAction : public ASTFrontendAction {
61 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
62 StringRef InFile) override;
65 class ASTDeclListAction : public ASTFrontendAction {
67 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
68 StringRef InFile) override;
71 class ASTViewAction : public ASTFrontendAction {
73 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
74 StringRef InFile) override;
77 class GeneratePCHAction : public ASTFrontendAction {
79 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
80 StringRef InFile) override;
82 TranslationUnitKind getTranslationUnitKind() override {
86 bool hasASTFileSupport() const override { return false; }
88 bool shouldEraseOutputFiles() override;
91 /// Compute the AST consumer arguments that will be used to
92 /// create the PCHGenerator instance returned by CreateASTConsumer.
94 /// \returns false if an error occurred, true otherwise.
95 static bool ComputeASTConsumerArguments(CompilerInstance &CI,
96 std::string &Sysroot);
98 /// Creates file to write the PCH into and returns a stream to write it
99 /// into. On error, returns null.
100 static std::unique_ptr<llvm::raw_pwrite_stream>
101 CreateOutputFile(CompilerInstance &CI, StringRef InFile,
102 std::string &OutputFile);
104 bool BeginSourceFileAction(CompilerInstance &CI) override;
107 class GenerateModuleAction : public ASTFrontendAction {
108 virtual std::unique_ptr<raw_pwrite_stream>
109 CreateOutputFile(CompilerInstance &CI, StringRef InFile) = 0;
112 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
113 StringRef InFile) override;
115 TranslationUnitKind getTranslationUnitKind() override {
119 bool hasASTFileSupport() const override { return false; }
122 class GenerateInterfaceStubAction : public ASTFrontendAction {
124 TranslationUnitKind getTranslationUnitKind() override { return TU_Module; }
126 bool hasASTFileSupport() const override { return false; }
129 class GenerateInterfaceIfsExpV1Action : public GenerateInterfaceStubAction {
131 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
132 StringRef InFile) override;
135 class GenerateModuleFromModuleMapAction : public GenerateModuleAction {
137 bool BeginSourceFileAction(CompilerInstance &CI) override;
139 std::unique_ptr<raw_pwrite_stream>
140 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
143 class GenerateModuleInterfaceAction : public GenerateModuleAction {
145 bool BeginSourceFileAction(CompilerInstance &CI) override;
147 std::unique_ptr<raw_pwrite_stream>
148 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
151 class GenerateHeaderModuleAction : public GenerateModuleAction {
152 /// The synthesized module input buffer for the current compilation.
153 std::unique_ptr<llvm::MemoryBuffer> Buffer;
154 std::vector<std::string> ModuleHeaders;
157 bool PrepareToExecuteAction(CompilerInstance &CI) override;
158 bool BeginSourceFileAction(CompilerInstance &CI) override;
160 std::unique_ptr<raw_pwrite_stream>
161 CreateOutputFile(CompilerInstance &CI, StringRef InFile) override;
164 class SyntaxOnlyAction : public ASTFrontendAction {
166 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
167 StringRef InFile) override;
170 ~SyntaxOnlyAction() override;
171 bool hasCodeCompletionSupport() const override { return true; }
174 /// Dump information about the given module file, to be used for
175 /// basic debugging and discovery.
176 class DumpModuleInfoAction : public ASTFrontendAction {
178 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
179 StringRef InFile) override;
180 bool BeginInvocation(CompilerInstance &CI) override;
181 void ExecuteAction() override;
184 bool hasPCHSupport() const override { return false; }
185 bool hasASTFileSupport() const override { return true; }
186 bool hasIRSupport() const override { return false; }
187 bool hasCodeCompletionSupport() const override { return false; }
190 class VerifyPCHAction : public ASTFrontendAction {
192 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
193 StringRef InFile) override;
195 void ExecuteAction() override;
198 bool hasCodeCompletionSupport() const override { return false; }
201 class TemplightDumpAction : public ASTFrontendAction {
203 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
204 StringRef InFile) override;
206 void ExecuteAction() override;
210 * Frontend action adaptor that merges ASTs together.
212 * This action takes an existing AST file and "merges" it into the AST
213 * context, producing a merged context. This action is an action
214 * adaptor, which forwards most of its calls to another action that
215 * will consume the merged context.
217 class ASTMergeAction : public FrontendAction {
218 /// The action that the merge action adapts.
219 std::unique_ptr<FrontendAction> AdaptedAction;
221 /// The set of AST files to merge.
222 std::vector<std::string> ASTFiles;
225 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
226 StringRef InFile) override;
228 bool BeginSourceFileAction(CompilerInstance &CI) override;
230 void ExecuteAction() override;
231 void EndSourceFileAction() override;
234 ASTMergeAction(std::unique_ptr<FrontendAction> AdaptedAction,
235 ArrayRef<std::string> ASTFiles);
236 ~ASTMergeAction() override;
238 bool usesPreprocessorOnly() const override;
239 TranslationUnitKind getTranslationUnitKind() override;
240 bool hasPCHSupport() const override;
241 bool hasASTFileSupport() const override;
242 bool hasCodeCompletionSupport() const override;
245 class PrintPreambleAction : public FrontendAction {
247 void ExecuteAction() override;
248 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
249 StringRef) override {
253 bool usesPreprocessorOnly() const override { return true; }
256 class PrintDependencyDirectivesSourceMinimizerAction : public FrontendAction {
258 void ExecuteAction() override;
259 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &,
260 StringRef) override {
264 bool usesPreprocessorOnly() const override { return true; }
267 //===----------------------------------------------------------------------===//
268 // Preprocessor Actions
269 //===----------------------------------------------------------------------===//
271 class DumpRawTokensAction : public PreprocessorFrontendAction {
273 void ExecuteAction() override;
276 class DumpTokensAction : public PreprocessorFrontendAction {
278 void ExecuteAction() override;
281 class PreprocessOnlyAction : public PreprocessorFrontendAction {
283 void ExecuteAction() override;
286 class PrintPreprocessedAction : public PreprocessorFrontendAction {
288 void ExecuteAction() override;
290 bool hasPCHSupport() const override { return true; }
293 } // end namespace clang