Home | History | Annotate | Download | only in Frontend
      1 //===-- CompilerInvocation.h - Compiler Invocation Helper Data --*- 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_COMPILERINVOCATION_H_
     11 #define LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H_
     12 
     13 #include "clang/Basic/DiagnosticOptions.h"
     14 #include "clang/Basic/FileSystemOptions.h"
     15 #include "clang/Basic/LangOptions.h"
     16 #include "clang/Basic/TargetOptions.h"
     17 #include "clang/Frontend/CodeGenOptions.h"
     18 #include "clang/Frontend/DependencyOutputOptions.h"
     19 #include "clang/Frontend/FrontendOptions.h"
     20 #include "clang/Frontend/LangStandard.h"
     21 #include "clang/Frontend/MigratorOptions.h"
     22 #include "clang/Frontend/PreprocessorOutputOptions.h"
     23 #include "clang/Lex/HeaderSearchOptions.h"
     24 #include "clang/Lex/PreprocessorOptions.h"
     25 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
     26 #include "llvm/ADT/IntrusiveRefCntPtr.h"
     27 #include "llvm/ADT/StringMap.h"
     28 #include "llvm/ADT/StringRef.h"
     29 #include <string>
     30 #include <vector>
     31 
     32 namespace llvm {
     33 namespace opt {
     34 class ArgList;
     35 }
     36 }
     37 
     38 namespace clang {
     39 class CompilerInvocation;
     40 class DiagnosticsEngine;
     41 
     42 /// \brief Fill out Opts based on the options given in Args.
     43 ///
     44 /// Args must have been created from the OptTable returned by
     45 /// createCC1OptTable().
     46 ///
     47 /// When errors are encountered, return false and, if Diags is non-null,
     48 /// report the error(s).
     49 bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args,
     50                          DiagnosticsEngine *Diags = nullptr);
     51 
     52 class CompilerInvocationBase : public RefCountedBase<CompilerInvocation> {
     53   void operator=(const CompilerInvocationBase &) = delete;
     54 
     55 public:
     56   /// Options controlling the language variant.
     57   std::shared_ptr<LangOptions> LangOpts;
     58 
     59   /// Options controlling the target.
     60   std::shared_ptr<TargetOptions> TargetOpts;
     61 
     62   /// Options controlling the diagnostic engine.
     63   IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
     64 
     65   /// Options controlling the \#include directive.
     66   IntrusiveRefCntPtr<HeaderSearchOptions> HeaderSearchOpts;
     67 
     68   /// Options controlling the preprocessor (aside from \#include handling).
     69   IntrusiveRefCntPtr<PreprocessorOptions> PreprocessorOpts;
     70 
     71   CompilerInvocationBase();
     72   ~CompilerInvocationBase();
     73 
     74   CompilerInvocationBase(const CompilerInvocationBase &X);
     75 
     76   LangOptions *getLangOpts() { return LangOpts.get(); }
     77   const LangOptions *getLangOpts() const { return LangOpts.get(); }
     78 
     79   TargetOptions &getTargetOpts() { return *TargetOpts.get(); }
     80   const TargetOptions &getTargetOpts() const {
     81     return *TargetOpts.get();
     82   }
     83 
     84   DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
     85 
     86   HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
     87   const HeaderSearchOptions &getHeaderSearchOpts() const {
     88     return *HeaderSearchOpts;
     89   }
     90 
     91   PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
     92   const PreprocessorOptions &getPreprocessorOpts() const {
     93     return *PreprocessorOpts;
     94   }
     95 };
     96 
     97 /// \brief Helper class for holding the data necessary to invoke the compiler.
     98 ///
     99 /// This class is designed to represent an abstract "invocation" of the
    100 /// compiler, including data such as the include paths, the code generation
    101 /// options, the warning flags, and so on.
    102 class CompilerInvocation : public CompilerInvocationBase {
    103   /// Options controlling the static analyzer.
    104   AnalyzerOptionsRef AnalyzerOpts;
    105 
    106   MigratorOptions MigratorOpts;
    107 
    108   /// Options controlling IRgen and the backend.
    109   CodeGenOptions CodeGenOpts;
    110 
    111   /// Options controlling dependency output.
    112   DependencyOutputOptions DependencyOutputOpts;
    113 
    114   /// Options controlling file system operations.
    115   FileSystemOptions FileSystemOpts;
    116 
    117   /// Options controlling the frontend itself.
    118   FrontendOptions FrontendOpts;
    119 
    120   /// Options controlling preprocessed output.
    121   PreprocessorOutputOptions PreprocessorOutputOpts;
    122 
    123 public:
    124   CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
    125 
    126   /// @name Utility Methods
    127   /// @{
    128 
    129   /// \brief Create a compiler invocation from a list of input options.
    130   /// \returns true on success.
    131   ///
    132   /// \param [out] Res - The resulting invocation.
    133   /// \param ArgBegin - The first element in the argument vector.
    134   /// \param ArgEnd - The last element in the argument vector.
    135   /// \param Diags - The diagnostic engine to use for errors.
    136   static bool CreateFromArgs(CompilerInvocation &Res,
    137                              const char* const *ArgBegin,
    138                              const char* const *ArgEnd,
    139                              DiagnosticsEngine &Diags);
    140 
    141   /// \brief Get the directory where the compiler headers
    142   /// reside, relative to the compiler binary (found by the passed in
    143   /// arguments).
    144   ///
    145   /// \param Argv0 - The program path (from argv[0]), for finding the builtin
    146   /// compiler path.
    147   /// \param MainAddr - The address of main (or some other function in the main
    148   /// executable), for finding the builtin compiler path.
    149   static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
    150 
    151   /// \brief Set language defaults for the given input language and
    152   /// language standard in the given LangOptions object.
    153   ///
    154   /// \param Opts - The LangOptions object to set up.
    155   /// \param IK - The input language.
    156   /// \param LangStd - The input language standard.
    157   static void setLangDefaults(LangOptions &Opts, InputKind IK,
    158                    LangStandard::Kind LangStd = LangStandard::lang_unspecified);
    159 
    160   /// \brief Retrieve a module hash string that is suitable for uniquely
    161   /// identifying the conditions under which the module was built.
    162   std::string getModuleHash() const;
    163 
    164   /// @}
    165   /// @name Option Subgroups
    166   /// @{
    167 
    168   AnalyzerOptionsRef getAnalyzerOpts() const {
    169     return AnalyzerOpts;
    170   }
    171 
    172   MigratorOptions &getMigratorOpts() { return MigratorOpts; }
    173   const MigratorOptions &getMigratorOpts() const {
    174     return MigratorOpts;
    175   }
    176 
    177   CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
    178   const CodeGenOptions &getCodeGenOpts() const {
    179     return CodeGenOpts;
    180   }
    181 
    182   DependencyOutputOptions &getDependencyOutputOpts() {
    183     return DependencyOutputOpts;
    184   }
    185   const DependencyOutputOptions &getDependencyOutputOpts() const {
    186     return DependencyOutputOpts;
    187   }
    188 
    189   FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
    190   const FileSystemOptions &getFileSystemOpts() const {
    191     return FileSystemOpts;
    192   }
    193 
    194   FrontendOptions &getFrontendOpts() { return FrontendOpts; }
    195   const FrontendOptions &getFrontendOpts() const {
    196     return FrontendOpts;
    197   }
    198 
    199   PreprocessorOutputOptions &getPreprocessorOutputOpts() {
    200     return PreprocessorOutputOpts;
    201   }
    202   const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
    203     return PreprocessorOutputOpts;
    204   }
    205 
    206   /// @}
    207 };
    208 
    209 namespace vfs {
    210   class FileSystem;
    211 }
    212 
    213 IntrusiveRefCntPtr<vfs::FileSystem>
    214 createVFSFromCompilerInvocation(const CompilerInvocation &CI,
    215                                 DiagnosticsEngine &Diags);
    216 
    217 } // end namespace clang
    218 
    219 #endif
    220