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