File indexing completed on 2026-05-10 08:36:55
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef LLVM_CLANG_FRONTEND_FRONTENDACTION_H
0018 #define LLVM_CLANG_FRONTEND_FRONTENDACTION_H
0019
0020 #include "clang/AST/ASTConsumer.h"
0021 #include "clang/Basic/LLVM.h"
0022 #include "clang/Basic/LangOptions.h"
0023 #include "clang/Frontend/ASTUnit.h"
0024 #include "clang/Frontend/CompilerInstance.h"
0025 #include "clang/Frontend/FrontendOptions.h"
0026 #include "llvm/ADT/StringRef.h"
0027 #include "llvm/Support/Error.h"
0028 #include <memory>
0029 #include <string>
0030 #include <vector>
0031
0032 namespace clang {
0033 class ASTMergeAction;
0034 class CompilerInstance;
0035
0036
0037 class FrontendAction {
0038 FrontendInputFile CurrentInput;
0039 std::unique_ptr<ASTUnit> CurrentASTUnit;
0040 CompilerInstance *Instance;
0041 friend class ASTMergeAction;
0042 friend class WrapperFrontendAction;
0043
0044 private:
0045 std::unique_ptr<ASTConsumer> CreateWrappedASTConsumer(CompilerInstance &CI,
0046 StringRef InFile);
0047
0048 protected:
0049
0050
0051
0052
0053
0054
0055
0056 virtual bool PrepareToExecuteAction(CompilerInstance &CI) { return true; }
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071 virtual std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0072 StringRef InFile) = 0;
0073
0074
0075
0076
0077
0078
0079
0080 virtual bool BeginInvocation(CompilerInstance &CI) { return true; }
0081
0082
0083
0084
0085
0086 virtual bool BeginSourceFileAction(CompilerInstance &CI) {
0087 return true;
0088 }
0089
0090
0091
0092
0093
0094
0095 virtual void ExecuteAction() = 0;
0096
0097
0098
0099
0100
0101 virtual void EndSourceFileAction() {}
0102
0103
0104
0105
0106
0107
0108
0109 virtual bool shouldEraseOutputFiles();
0110
0111
0112
0113 public:
0114 FrontendAction();
0115 virtual ~FrontendAction();
0116
0117
0118
0119
0120 CompilerInstance &getCompilerInstance() const {
0121 assert(Instance && "Compiler instance not registered!");
0122 return *Instance;
0123 }
0124
0125 void setCompilerInstance(CompilerInstance *Value) { Instance = Value; }
0126
0127
0128
0129
0130
0131 bool isCurrentFileAST() const {
0132 assert(!CurrentInput.isEmpty() && "No current file!");
0133 return (bool)CurrentASTUnit;
0134 }
0135
0136 const FrontendInputFile &getCurrentInput() const {
0137 return CurrentInput;
0138 }
0139
0140 StringRef getCurrentFile() const {
0141 assert(!CurrentInput.isEmpty() && "No current file!");
0142 return CurrentInput.getFile();
0143 }
0144
0145 StringRef getCurrentFileOrBufferName() const {
0146 assert(!CurrentInput.isEmpty() && "No current file!");
0147 return CurrentInput.isFile()
0148 ? CurrentInput.getFile()
0149 : CurrentInput.getBuffer().getBufferIdentifier();
0150 }
0151
0152 InputKind getCurrentFileKind() const {
0153 assert(!CurrentInput.isEmpty() && "No current file!");
0154 return CurrentInput.getKind();
0155 }
0156
0157 ASTUnit &getCurrentASTUnit() const {
0158 assert(CurrentASTUnit && "No current AST unit!");
0159 return *CurrentASTUnit;
0160 }
0161
0162 Module *getCurrentModule() const;
0163
0164 std::unique_ptr<ASTUnit> takeCurrentASTUnit() {
0165 return std::move(CurrentASTUnit);
0166 }
0167
0168 void setCurrentInput(const FrontendInputFile &CurrentInput,
0169 std::unique_ptr<ASTUnit> AST = nullptr);
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180 virtual bool isModelParsingAction() const { return false; }
0181
0182
0183
0184
0185
0186 virtual bool usesPreprocessorOnly() const = 0;
0187
0188
0189 virtual TranslationUnitKind getTranslationUnitKind() {
0190
0191 if (Instance && Instance->hasASTContext())
0192 return Instance->getASTContext().TUKind;
0193 return TU_Complete;
0194 }
0195
0196
0197 virtual bool hasPCHSupport() const { return true; }
0198
0199
0200 virtual bool hasASTFileSupport() const { return true; }
0201
0202
0203 virtual bool hasIRSupport() const { return false; }
0204
0205
0206 virtual bool hasCodeCompletionSupport() const { return false; }
0207
0208
0209
0210
0211
0212
0213 bool PrepareToExecute(CompilerInstance &CI) {
0214 return PrepareToExecuteAction(CI);
0215 }
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236 bool BeginSourceFile(CompilerInstance &CI, const FrontendInputFile &Input);
0237
0238
0239 llvm::Error Execute();
0240
0241
0242
0243 virtual void EndSourceFile();
0244
0245
0246 };
0247
0248
0249 class ASTFrontendAction : public FrontendAction {
0250 protected:
0251
0252
0253
0254
0255
0256 void ExecuteAction() override;
0257
0258 public:
0259 ASTFrontendAction() {}
0260 bool usesPreprocessorOnly() const override { return false; }
0261 };
0262
0263 class PluginASTAction : public ASTFrontendAction {
0264 virtual void anchor();
0265 public:
0266 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0267 StringRef InFile) override = 0;
0268
0269
0270
0271
0272
0273
0274
0275 virtual bool ParseArgs(const CompilerInstance &CI,
0276 const std::vector<std::string> &arg) = 0;
0277
0278 enum ActionType {
0279 CmdlineBeforeMainAction,
0280
0281 CmdlineAfterMainAction,
0282
0283 ReplaceAction,
0284 AddBeforeMainAction,
0285 AddAfterMainAction
0286 };
0287
0288
0289
0290 virtual ActionType getActionType() { return CmdlineAfterMainAction; }
0291 };
0292
0293
0294 class PreprocessorFrontendAction : public FrontendAction {
0295 protected:
0296
0297
0298 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0299 StringRef InFile) override;
0300
0301 public:
0302 bool usesPreprocessorOnly() const override { return true; }
0303 };
0304
0305
0306
0307
0308
0309
0310
0311 class WrapperFrontendAction : public FrontendAction {
0312 protected:
0313 std::unique_ptr<FrontendAction> WrappedAction;
0314
0315 bool PrepareToExecuteAction(CompilerInstance &CI) override;
0316 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
0317 StringRef InFile) override;
0318 bool BeginInvocation(CompilerInstance &CI) override;
0319 bool BeginSourceFileAction(CompilerInstance &CI) override;
0320 void ExecuteAction() override;
0321 void EndSourceFile() override;
0322 void EndSourceFileAction() override;
0323 bool shouldEraseOutputFiles() override;
0324
0325 public:
0326
0327
0328 WrapperFrontendAction(std::unique_ptr<FrontendAction> WrappedAction);
0329
0330 bool usesPreprocessorOnly() const override;
0331 TranslationUnitKind getTranslationUnitKind() override;
0332 bool hasPCHSupport() const override;
0333 bool hasASTFileSupport() const override;
0334 bool hasIRSupport() const override;
0335 bool hasCodeCompletionSupport() const override;
0336 };
0337
0338 }
0339
0340 #endif