File indexing completed on 2026-05-10 08:36:55
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
0010 #define LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
0011
0012 #include "clang/APINotes/APINotesOptions.h"
0013 #include "clang/Basic/CodeGenOptions.h"
0014 #include "clang/Basic/DiagnosticOptions.h"
0015 #include "clang/Basic/FileSystemOptions.h"
0016 #include "clang/Basic/LLVM.h"
0017 #include "clang/Basic/LangOptions.h"
0018 #include "clang/Basic/LangStandard.h"
0019 #include "clang/Frontend/DependencyOutputOptions.h"
0020 #include "clang/Frontend/FrontendOptions.h"
0021 #include "clang/Frontend/MigratorOptions.h"
0022 #include "clang/Frontend/PreprocessorOutputOptions.h"
0023 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
0024 #include "llvm/ADT/IntrusiveRefCntPtr.h"
0025 #include "llvm/ADT/ArrayRef.h"
0026 #include <memory>
0027 #include <string>
0028
0029 namespace llvm {
0030
0031 class Triple;
0032
0033 namespace opt {
0034
0035 class ArgList;
0036
0037 }
0038
0039 namespace vfs {
0040
0041 class FileSystem;
0042
0043 }
0044
0045 }
0046
0047 namespace clang {
0048
0049 class DiagnosticsEngine;
0050 class HeaderSearchOptions;
0051 class PreprocessorOptions;
0052 class TargetOptions;
0053
0054
0055
0056 std::unique_ptr<DiagnosticOptions>
0057 CreateAndPopulateDiagOpts(ArrayRef<const char *> Argv);
0058
0059
0060
0061
0062
0063
0064
0065
0066 bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args,
0067 DiagnosticsEngine *Diags = nullptr,
0068 bool DefaultDiagColor = true);
0069
0070
0071
0072
0073
0074 class CompilerInvocationBase {
0075 protected:
0076
0077 std::shared_ptr<LangOptions> LangOpts;
0078
0079
0080 std::shared_ptr<TargetOptions> TargetOpts;
0081
0082
0083 IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
0084
0085
0086 std::shared_ptr<HeaderSearchOptions> HSOpts;
0087
0088
0089 std::shared_ptr<PreprocessorOptions> PPOpts;
0090
0091
0092 AnalyzerOptionsRef AnalyzerOpts;
0093
0094 std::shared_ptr<MigratorOptions> MigratorOpts;
0095
0096
0097 std::shared_ptr<APINotesOptions> APINotesOpts;
0098
0099
0100 std::shared_ptr<CodeGenOptions> CodeGenOpts;
0101
0102
0103 std::shared_ptr<FileSystemOptions> FSOpts;
0104
0105
0106 std::shared_ptr<FrontendOptions> FrontendOpts;
0107
0108
0109 std::shared_ptr<DependencyOutputOptions> DependencyOutputOpts;
0110
0111
0112 std::shared_ptr<PreprocessorOutputOptions> PreprocessorOutputOpts;
0113
0114
0115
0116 struct EmptyConstructor {};
0117
0118 CompilerInvocationBase();
0119 CompilerInvocationBase(EmptyConstructor) {}
0120 CompilerInvocationBase(const CompilerInvocationBase &X) = delete;
0121 CompilerInvocationBase(CompilerInvocationBase &&X) = default;
0122 CompilerInvocationBase &operator=(const CompilerInvocationBase &X) = delete;
0123 CompilerInvocationBase &deep_copy_assign(const CompilerInvocationBase &X);
0124 CompilerInvocationBase &shallow_copy_assign(const CompilerInvocationBase &X);
0125 CompilerInvocationBase &operator=(CompilerInvocationBase &&X) = default;
0126 ~CompilerInvocationBase() = default;
0127
0128 public:
0129
0130
0131 const LangOptions &getLangOpts() const { return *LangOpts; }
0132 const TargetOptions &getTargetOpts() const { return *TargetOpts; }
0133 const DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
0134 const HeaderSearchOptions &getHeaderSearchOpts() const { return *HSOpts; }
0135 const PreprocessorOptions &getPreprocessorOpts() const { return *PPOpts; }
0136 const AnalyzerOptions &getAnalyzerOpts() const { return *AnalyzerOpts; }
0137 const MigratorOptions &getMigratorOpts() const { return *MigratorOpts; }
0138 const APINotesOptions &getAPINotesOpts() const { return *APINotesOpts; }
0139 const CodeGenOptions &getCodeGenOpts() const { return *CodeGenOpts; }
0140 const FileSystemOptions &getFileSystemOpts() const { return *FSOpts; }
0141 const FrontendOptions &getFrontendOpts() const { return *FrontendOpts; }
0142 const DependencyOutputOptions &getDependencyOutputOpts() const {
0143 return *DependencyOutputOpts;
0144 }
0145 const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
0146 return *PreprocessorOutputOpts;
0147 }
0148
0149
0150
0151
0152 using StringAllocator = llvm::function_ref<const char *(const Twine &)>;
0153
0154
0155
0156
0157
0158
0159
0160
0161 void generateCC1CommandLine(llvm::SmallVectorImpl<const char *> &Args,
0162 StringAllocator SA) const {
0163 generateCC1CommandLine([&](const Twine &Arg) {
0164
0165 Args.push_back(Arg.isSingleStringLiteral()
0166 ? Arg.getSingleStringRef().data()
0167 : SA(Arg));
0168 });
0169 }
0170
0171 using ArgumentConsumer = llvm::function_ref<void(const Twine &)>;
0172
0173
0174
0175
0176 void generateCC1CommandLine(ArgumentConsumer Consumer) const;
0177
0178
0179
0180
0181
0182 std::vector<std::string> getCC1CommandLine() const;
0183
0184 private:
0185
0186 static void GenerateDiagnosticArgs(const DiagnosticOptions &Opts,
0187 ArgumentConsumer Consumer,
0188 bool DefaultDiagColor);
0189
0190
0191 static void GenerateLangArgs(const LangOptions &Opts,
0192 ArgumentConsumer Consumer, const llvm::Triple &T,
0193 InputKind IK);
0194
0195
0196 static void GenerateCodeGenArgs(const CodeGenOptions &Opts,
0197 ArgumentConsumer Consumer,
0198 const llvm::Triple &T,
0199 const std::string &OutputFile,
0200 const LangOptions *LangOpts);
0201
0202 };
0203
0204 class CowCompilerInvocation;
0205
0206
0207
0208
0209
0210
0211 class CompilerInvocation : public CompilerInvocationBase {
0212 public:
0213 CompilerInvocation() = default;
0214 CompilerInvocation(const CompilerInvocation &X)
0215 : CompilerInvocationBase(EmptyConstructor{}) {
0216 deep_copy_assign(X);
0217 }
0218 CompilerInvocation(CompilerInvocation &&) = default;
0219 CompilerInvocation &operator=(const CompilerInvocation &X) {
0220 deep_copy_assign(X);
0221 return *this;
0222 }
0223 ~CompilerInvocation() = default;
0224
0225 explicit CompilerInvocation(const CowCompilerInvocation &X);
0226 CompilerInvocation &operator=(const CowCompilerInvocation &X);
0227
0228
0229
0230
0231
0232 using CompilerInvocationBase::getLangOpts;
0233 using CompilerInvocationBase::getTargetOpts;
0234 using CompilerInvocationBase::getDiagnosticOpts;
0235 using CompilerInvocationBase::getHeaderSearchOpts;
0236 using CompilerInvocationBase::getPreprocessorOpts;
0237 using CompilerInvocationBase::getAnalyzerOpts;
0238 using CompilerInvocationBase::getMigratorOpts;
0239 using CompilerInvocationBase::getAPINotesOpts;
0240 using CompilerInvocationBase::getCodeGenOpts;
0241 using CompilerInvocationBase::getFileSystemOpts;
0242 using CompilerInvocationBase::getFrontendOpts;
0243 using CompilerInvocationBase::getDependencyOutputOpts;
0244 using CompilerInvocationBase::getPreprocessorOutputOpts;
0245
0246
0247
0248
0249 LangOptions &getLangOpts() { return *LangOpts; }
0250 TargetOptions &getTargetOpts() { return *TargetOpts; }
0251 DiagnosticOptions &getDiagnosticOpts() { return *DiagnosticOpts; }
0252 HeaderSearchOptions &getHeaderSearchOpts() { return *HSOpts; }
0253 PreprocessorOptions &getPreprocessorOpts() { return *PPOpts; }
0254 AnalyzerOptions &getAnalyzerOpts() { return *AnalyzerOpts; }
0255 MigratorOptions &getMigratorOpts() { return *MigratorOpts; }
0256 APINotesOptions &getAPINotesOpts() { return *APINotesOpts; }
0257 CodeGenOptions &getCodeGenOpts() { return *CodeGenOpts; }
0258 FileSystemOptions &getFileSystemOpts() { return *FSOpts; }
0259 FrontendOptions &getFrontendOpts() { return *FrontendOpts; }
0260 DependencyOutputOptions &getDependencyOutputOpts() {
0261 return *DependencyOutputOpts;
0262 }
0263 PreprocessorOutputOptions &getPreprocessorOutputOpts() {
0264 return *PreprocessorOutputOpts;
0265 }
0266
0267
0268
0269
0270 using CompilerInvocationBase::LangOpts;
0271 using CompilerInvocationBase::TargetOpts;
0272 using CompilerInvocationBase::DiagnosticOpts;
0273 std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() {
0274 return HSOpts;
0275 }
0276 std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
0277 return PPOpts;
0278 }
0279 std::shared_ptr<LangOptions> getLangOptsPtr() { return LangOpts; }
0280
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293 static bool CreateFromArgs(CompilerInvocation &Res,
0294 ArrayRef<const char *> CommandLineArgs,
0295 DiagnosticsEngine &Diags,
0296 const char *Argv0 = nullptr);
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306 static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
0307
0308
0309
0310
0311
0312
0313 static void setDefaultPointerAuthOptions(PointerAuthOptions &Opts,
0314 const LangOptions &LangOpts,
0315 const llvm::Triple &Triple);
0316
0317
0318
0319 std::string getModuleHash() const;
0320
0321
0322
0323
0324
0325
0326
0327
0328 static bool checkCC1RoundTrip(ArrayRef<const char *> Args,
0329 DiagnosticsEngine &Diags,
0330 const char *Argv0 = nullptr);
0331
0332
0333
0334 void resetNonModularOptions();
0335
0336
0337
0338 void clearImplicitModuleBuildOptions();
0339
0340 private:
0341 static bool CreateFromArgsImpl(CompilerInvocation &Res,
0342 ArrayRef<const char *> CommandLineArgs,
0343 DiagnosticsEngine &Diags, const char *Argv0);
0344
0345
0346 static bool ParseLangArgs(LangOptions &Opts, llvm::opt::ArgList &Args,
0347 InputKind IK, const llvm::Triple &T,
0348 std::vector<std::string> &Includes,
0349 DiagnosticsEngine &Diags);
0350
0351
0352 static bool ParseCodeGenArgs(CodeGenOptions &Opts, llvm::opt::ArgList &Args,
0353 InputKind IK, DiagnosticsEngine &Diags,
0354 const llvm::Triple &T,
0355 const std::string &OutputFile,
0356 const LangOptions &LangOptsRef);
0357 };
0358
0359
0360 class CowCompilerInvocation : public CompilerInvocationBase {
0361 public:
0362 CowCompilerInvocation() = default;
0363 CowCompilerInvocation(const CowCompilerInvocation &X)
0364 : CompilerInvocationBase(EmptyConstructor{}) {
0365 shallow_copy_assign(X);
0366 }
0367 CowCompilerInvocation(CowCompilerInvocation &&) = default;
0368 CowCompilerInvocation &operator=(const CowCompilerInvocation &X) {
0369 shallow_copy_assign(X);
0370 return *this;
0371 }
0372 ~CowCompilerInvocation() = default;
0373
0374 CowCompilerInvocation(const CompilerInvocation &X)
0375 : CompilerInvocationBase(EmptyConstructor{}) {
0376 deep_copy_assign(X);
0377 }
0378
0379 CowCompilerInvocation(CompilerInvocation &&X)
0380 : CompilerInvocationBase(std::move(X)) {}
0381
0382
0383
0384
0385
0386 LangOptions &getMutLangOpts();
0387 TargetOptions &getMutTargetOpts();
0388 DiagnosticOptions &getMutDiagnosticOpts();
0389 HeaderSearchOptions &getMutHeaderSearchOpts();
0390 PreprocessorOptions &getMutPreprocessorOpts();
0391 AnalyzerOptions &getMutAnalyzerOpts();
0392 MigratorOptions &getMutMigratorOpts();
0393 APINotesOptions &getMutAPINotesOpts();
0394 CodeGenOptions &getMutCodeGenOpts();
0395 FileSystemOptions &getMutFileSystemOpts();
0396 FrontendOptions &getMutFrontendOpts();
0397 DependencyOutputOptions &getMutDependencyOutputOpts();
0398 PreprocessorOutputOptions &getMutPreprocessorOutputOpts();
0399
0400 };
0401
0402 IntrusiveRefCntPtr<llvm::vfs::FileSystem>
0403 createVFSFromCompilerInvocation(const CompilerInvocation &CI,
0404 DiagnosticsEngine &Diags);
0405
0406 IntrusiveRefCntPtr<llvm::vfs::FileSystem> createVFSFromCompilerInvocation(
0407 const CompilerInvocation &CI, DiagnosticsEngine &Diags,
0408 IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
0409
0410 IntrusiveRefCntPtr<llvm::vfs::FileSystem>
0411 createVFSFromOverlayFiles(ArrayRef<std::string> VFSOverlayFiles,
0412 DiagnosticsEngine &Diags,
0413 IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
0414
0415 }
0416
0417 #endif