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