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