Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:55

0001 //===- CompilerInvocation.h - Compiler Invocation Helper Data ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
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 } // namespace opt
0038 
0039 namespace vfs {
0040 
0041 class FileSystem;
0042 
0043 } // namespace vfs
0044 
0045 } // namespace llvm
0046 
0047 namespace clang {
0048 
0049 class DiagnosticsEngine;
0050 class HeaderSearchOptions;
0051 class PreprocessorOptions;
0052 class TargetOptions;
0053 
0054 // This lets us create the DiagnosticsEngine with a properly-filled-out
0055 // DiagnosticOptions instance.
0056 std::unique_ptr<DiagnosticOptions>
0057 CreateAndPopulateDiagOpts(ArrayRef<const char *> Argv);
0058 
0059 /// Fill out Opts based on the options given in Args.
0060 ///
0061 /// Args must have been created from the OptTable returned by
0062 /// createCC1OptTable().
0063 ///
0064 /// When errors are encountered, return false and, if Diags is non-null,
0065 /// report the error(s).
0066 bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args,
0067                          DiagnosticsEngine *Diags = nullptr,
0068                          bool DefaultDiagColor = true);
0069 
0070 /// The base class of CompilerInvocation. It keeps individual option objects
0071 /// behind reference-counted pointers, which is useful for clients that want to
0072 /// keep select option objects alive (even after CompilerInvocation gets
0073 /// destroyed) without making a copy.
0074 class CompilerInvocationBase {
0075 protected:
0076   /// Options controlling the language variant.
0077   std::shared_ptr<LangOptions> LangOpts;
0078 
0079   /// Options controlling the target.
0080   std::shared_ptr<TargetOptions> TargetOpts;
0081 
0082   /// Options controlling the diagnostic engine.
0083   IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
0084 
0085   /// Options controlling the \#include directive.
0086   std::shared_ptr<HeaderSearchOptions> HSOpts;
0087 
0088   /// Options controlling the preprocessor (aside from \#include handling).
0089   std::shared_ptr<PreprocessorOptions> PPOpts;
0090 
0091   /// Options controlling the static analyzer.
0092   AnalyzerOptionsRef AnalyzerOpts;
0093 
0094   std::shared_ptr<MigratorOptions> MigratorOpts;
0095 
0096   /// Options controlling API notes.
0097   std::shared_ptr<APINotesOptions> APINotesOpts;
0098 
0099   /// Options controlling IRgen and the backend.
0100   std::shared_ptr<CodeGenOptions> CodeGenOpts;
0101 
0102   /// Options controlling file system operations.
0103   std::shared_ptr<FileSystemOptions> FSOpts;
0104 
0105   /// Options controlling the frontend itself.
0106   std::shared_ptr<FrontendOptions> FrontendOpts;
0107 
0108   /// Options controlling dependency output.
0109   std::shared_ptr<DependencyOutputOptions> DependencyOutputOpts;
0110 
0111   /// Options controlling preprocessed output.
0112   std::shared_ptr<PreprocessorOutputOptions> PreprocessorOutputOpts;
0113 
0114   /// Dummy tag type whose instance can be passed into the constructor to
0115   /// prevent creation of the reference-counted option objects.
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   /// Const getters.
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   /// Command line generation.
0151   /// @{
0152   using StringAllocator = llvm::function_ref<const char *(const Twine &)>;
0153   /// Generate cc1-compatible command line arguments from this instance.
0154   ///
0155   /// \param [out] Args - The generated arguments. Note that the caller is
0156   /// responsible for inserting the path to the clang executable and "-cc1" if
0157   /// desired.
0158   /// \param SA - A function that given a Twine can allocate storage for a given
0159   /// command line argument and return a pointer to the newly allocated string.
0160   /// The returned pointer is what gets appended to Args.
0161   void generateCC1CommandLine(llvm::SmallVectorImpl<const char *> &Args,
0162                               StringAllocator SA) const {
0163     generateCC1CommandLine([&](const Twine &Arg) {
0164       // No need to allocate static string literals.
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   /// Generate cc1-compatible command line arguments from this instance.
0173   ///
0174   /// \param Consumer - Callback that gets invoked for every single generated
0175   /// command line argument.
0176   void generateCC1CommandLine(ArgumentConsumer Consumer) const;
0177 
0178   /// Generate cc1-compatible command line arguments from this instance,
0179   /// wrapping the result as a std::vector<std::string>.
0180   ///
0181   /// This is a (less-efficient) wrapper over generateCC1CommandLine().
0182   std::vector<std::string> getCC1CommandLine() const;
0183 
0184 private:
0185   /// Generate command line options from DiagnosticOptions.
0186   static void GenerateDiagnosticArgs(const DiagnosticOptions &Opts,
0187                                      ArgumentConsumer Consumer,
0188                                      bool DefaultDiagColor);
0189 
0190   /// Generate command line options from LangOptions.
0191   static void GenerateLangArgs(const LangOptions &Opts,
0192                                ArgumentConsumer Consumer, const llvm::Triple &T,
0193                                InputKind IK);
0194 
0195   // Generate command line options from CodeGenOptions.
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 /// Helper class for holding the data necessary to invoke the compiler.
0207 ///
0208 /// This class is designed to represent an abstract "invocation" of the
0209 /// compiler, including data such as the include paths, the code generation
0210 /// options, the warning flags, and so on.
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   /// Const getters.
0229   /// @{
0230   // Note: These need to be pulled in manually. Otherwise, they get hidden by
0231   // the mutable getters with the same names.
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   /// Mutable getters.
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   /// Base class internals.
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   /// Create a compiler invocation from a list of input options.
0283   /// \returns true on success.
0284   ///
0285   /// \returns false if an error was encountered while parsing the arguments
0286   /// and attempts to recover and continue parsing the rest of the arguments.
0287   /// The recovery is best-effort and only guarantees that \p Res will end up in
0288   /// one of the vaild-to-access (albeit arbitrary) states.
0289   ///
0290   /// \param [out] Res - The resulting invocation.
0291   /// \param [in] CommandLineArgs - Array of argument strings, this must not
0292   /// contain "-cc1".
0293   static bool CreateFromArgs(CompilerInvocation &Res,
0294                              ArrayRef<const char *> CommandLineArgs,
0295                              DiagnosticsEngine &Diags,
0296                              const char *Argv0 = nullptr);
0297 
0298   /// Get the directory where the compiler headers
0299   /// reside, relative to the compiler binary (found by the passed in
0300   /// arguments).
0301   ///
0302   /// \param Argv0 - The program path (from argv[0]), for finding the builtin
0303   /// compiler path.
0304   /// \param MainAddr - The address of main (or some other function in the main
0305   /// executable), for finding the builtin compiler path.
0306   static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
0307 
0308   /// Populate \p Opts with the default set of pointer authentication-related
0309   /// options given \p LangOpts and \p Triple.
0310   ///
0311   /// Note: This is intended to be used by tools which must be aware of
0312   /// pointer authentication-related code generation, e.g. lldb.
0313   static void setDefaultPointerAuthOptions(PointerAuthOptions &Opts,
0314                                            const LangOptions &LangOpts,
0315                                            const llvm::Triple &Triple);
0316 
0317   /// Retrieve a module hash string that is suitable for uniquely
0318   /// identifying the conditions under which the module was built.
0319   std::string getModuleHash() const;
0320 
0321   /// Check that \p Args can be parsed and re-serialized without change,
0322   /// emiting diagnostics for any differences.
0323   ///
0324   /// This check is only suitable for command-lines that are expected to already
0325   /// be canonical.
0326   ///
0327   /// \return false if there are any errors.
0328   static bool checkCC1RoundTrip(ArrayRef<const char *> Args,
0329                                 DiagnosticsEngine &Diags,
0330                                 const char *Argv0 = nullptr);
0331 
0332   /// Reset all of the options that are not considered when building a
0333   /// module.
0334   void resetNonModularOptions();
0335 
0336   /// Disable implicit modules and canonicalize options that are only used by
0337   /// implicit modules.
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   /// Parse command line options that map to LangOptions.
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   /// Parse command line options that map to CodeGenOptions.
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 /// Same as \c CompilerInvocation, but with copy-on-write optimization.
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   // Const getters are inherited from the base class.
0383 
0384   /// Mutable getters.
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 } // namespace clang
0416 
0417 #endif // LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H