Clang Project

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