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 = 0);
     51 
     52 class CompilerInvocationBase : public RefCountedBase<CompilerInvocation> {
     53 protected:
     54   /// Options controlling the language variant.
     55   IntrusiveRefCntPtr<LangOptions> LangOpts;
     56 
     57   /// Options controlling the target.
     58   IntrusiveRefCntPtr<TargetOptions> TargetOpts;
     59 
     60   /// Options controlling the diagnostic engine.
     61   IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
     62 
     63   /// Options controlling the \#include directive.
     64   IntrusiveRefCntPtr<HeaderSearchOptions> HeaderSearchOpts;
     65 
     66   /// Options controlling the preprocessor (aside from \#include handling).
     67   IntrusiveRefCntPtr<PreprocessorOptions> PreprocessorOpts;
     68 
     69 public:
     70   CompilerInvocationBase();
     71 
     72   CompilerInvocationBase(const CompilerInvocationBase &X);
     73 
     74   LangOptions *getLangOpts() { return LangOpts.getPtr(); }
     75   const LangOptions *getLangOpts() const { return LangOpts.getPtr(); }
     76 
     77   TargetOptions &getTargetOpts() { return *TargetOpts.getPtr(); }
     78   const TargetOptions &getTargetOpts() const {
     79     return *TargetOpts.getPtr();
     80   }
     81 
     82   DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
     83 
     84   HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
     85   const HeaderSearchOptions &getHeaderSearchOpts() const {
     86     return *HeaderSearchOpts;
     87   }
     88 
     89   PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
     90   const PreprocessorOptions &getPreprocessorOpts() const {
     91     return *PreprocessorOpts;
     92   }
     93 };
     94 
     95 /// \brief Helper class for holding the data necessary to invoke the compiler.
     96 ///
     97 /// This class is designed to represent an abstract "invocation" of the
     98 /// compiler, including data such as the include paths, the code generation
     99 /// options, the warning flags, and so on.
    100 class CompilerInvocation : public CompilerInvocationBase {
    101   /// Options controlling the static analyzer.
    102   AnalyzerOptionsRef AnalyzerOpts;
    103 
    104   MigratorOptions MigratorOpts;
    105 
    106   /// Options controlling IRgen and the backend.
    107   CodeGenOptions CodeGenOpts;
    108 
    109   /// Options controlling dependency output.
    110   DependencyOutputOptions DependencyOutputOpts;
    111 
    112   /// Options controlling file system operations.
    113   FileSystemOptions FileSystemOpts;
    114 
    115   /// Options controlling the frontend itself.
    116   FrontendOptions FrontendOpts;
    117 
    118   /// Options controlling preprocessed output.
    119   PreprocessorOutputOptions PreprocessorOutputOpts;
    120 
    121 public:
    122   CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
    123 
    124   /// @name Utility Methods
    125   /// @{
    126 
    127   /// \brief Create a compiler invocation from a list of input options.
    128   /// \returns true on success.
    129   ///
    130   /// \param [out] Res - The resulting invocation.
    131   /// \param ArgBegin - The first element in the argument vector.
    132   /// \param ArgEnd - The last element in the argument vector.
    133   /// \param Diags - The diagnostic engine to use for errors.
    134   static bool CreateFromArgs(CompilerInvocation &Res,
    135                              const char* const *ArgBegin,
    136                              const char* const *ArgEnd,
    137                              DiagnosticsEngine &Diags);
    138 
    139   /// \brief Get the directory where the compiler headers
    140   /// reside, relative to the compiler binary (found by the passed in
    141   /// arguments).
    142   ///
    143   /// \param Argv0 - The program path (from argv[0]), for finding the builtin
    144   /// compiler path.
    145   /// \param MainAddr - The address of main (or some other function in the main
    146   /// executable), for finding the builtin compiler path.
    147   static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
    148 
    149   /// \brief Set language defaults for the given input language and
    150   /// language standard in the given LangOptions object.
    151   ///
    152   /// \param Opts - The LangOptions object to set up.
    153   /// \param IK - The input language.
    154   /// \param LangStd - The input language standard.
    155   static void setLangDefaults(LangOptions &Opts, InputKind IK,
    156                    LangStandard::Kind LangStd = LangStandard::lang_unspecified);
    157 
    158   /// \brief Retrieve a module hash string that is suitable for uniquely
    159   /// identifying the conditions under which the module was built.
    160   std::string getModuleHash() const;
    161 
    162   /// @}
    163   /// @name Option Subgroups
    164   /// @{
    165 
    166   AnalyzerOptionsRef getAnalyzerOpts() const {
    167     return AnalyzerOpts;
    168   }
    169 
    170   MigratorOptions &getMigratorOpts() { return MigratorOpts; }
    171   const MigratorOptions &getMigratorOpts() const {
    172     return MigratorOpts;
    173   }
    174 
    175   CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
    176   const CodeGenOptions &getCodeGenOpts() const {
    177     return CodeGenOpts;
    178   }
    179 
    180   DependencyOutputOptions &getDependencyOutputOpts() {
    181     return DependencyOutputOpts;
    182   }
    183   const DependencyOutputOptions &getDependencyOutputOpts() const {
    184     return DependencyOutputOpts;
    185   }
    186 
    187   FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
    188   const FileSystemOptions &getFileSystemOpts() const {
    189     return FileSystemOpts;
    190   }
    191 
    192   FrontendOptions &getFrontendOpts() { return FrontendOpts; }
    193   const FrontendOptions &getFrontendOpts() const {
    194     return FrontendOpts;
    195   }
    196 
    197   PreprocessorOutputOptions &getPreprocessorOutputOpts() {
    198     return PreprocessorOutputOpts;
    199   }
    200   const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
    201     return PreprocessorOutputOpts;
    202   }
    203 
    204   /// @}
    205 };
    206 
    207 } // end namespace clang
    208 
    209 #endif
    210