clang API Documentation

CompilerInvocation.h
Go to the documentation of this file.
00001 //===-- CompilerInvocation.h - Compiler Invocation Helper Data --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H_
00011 #define LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H_
00012 
00013 #include "clang/Basic/LangOptions.h"
00014 #include "clang/Basic/TargetOptions.h"
00015 #include "clang/Basic/FileSystemOptions.h"
00016 #include "clang/Frontend/AnalyzerOptions.h"
00017 #include "clang/Frontend/MigratorOptions.h"
00018 #include "clang/Frontend/CodeGenOptions.h"
00019 #include "clang/Frontend/DependencyOutputOptions.h"
00020 #include "clang/Frontend/DiagnosticOptions.h"
00021 #include "clang/Frontend/FrontendOptions.h"
00022 #include "clang/Frontend/HeaderSearchOptions.h"
00023 #include "clang/Frontend/LangStandard.h"
00024 #include "clang/Frontend/PreprocessorOptions.h"
00025 #include "clang/Frontend/PreprocessorOutputOptions.h"
00026 #include "llvm/ADT/IntrusiveRefCntPtr.h"
00027 #include "llvm/ADT/StringRef.h"
00028 #include "llvm/ADT/StringMap.h"
00029 #include <string>
00030 #include <vector>
00031 
00032 namespace clang {
00033 
00034 class CompilerInvocation;
00035 class DiagnosticsEngine;
00036 
00037 namespace driver {
00038 class ArgList;
00039 }
00040 
00041 /// CompilerInvocation - Fill out Opts based on the options given in Args.
00042 /// Args must have been created from the OptTable returned by
00043 /// createCC1OptTable(). When errors are encountered, return false and,
00044 /// if Diags is non-null, report the error(s).
00045 bool ParseDiagnosticArgs(DiagnosticOptions &Opts, driver::ArgList &Args,
00046                          DiagnosticsEngine *Diags = 0);
00047   
00048 class CompilerInvocationBase : public RefCountedBase<CompilerInvocation> {
00049 protected:
00050   /// Options controlling the language variant.
00051   IntrusiveRefCntPtr<LangOptions> LangOpts;
00052 public:
00053   CompilerInvocationBase();
00054 
00055   CompilerInvocationBase(const CompilerInvocationBase &X);
00056   
00057   LangOptions *getLangOpts() { return LangOpts.getPtr(); }
00058   const LangOptions *getLangOpts() const { return LangOpts.getPtr(); }
00059 };
00060   
00061 /// CompilerInvocation - Helper class for holding the data necessary to invoke
00062 /// the compiler.
00063 ///
00064 /// This class is designed to represent an abstract "invocation" of the
00065 /// compiler, including data such as the include paths, the code generation
00066 /// options, the warning flags, and so on.
00067 class CompilerInvocation : public CompilerInvocationBase {
00068   /// Options controlling the static analyzer.
00069   AnalyzerOptions AnalyzerOpts;
00070 
00071   MigratorOptions MigratorOpts;
00072   
00073   /// Options controlling IRgen and the backend.
00074   CodeGenOptions CodeGenOpts;
00075 
00076   /// Options controlling dependency output.
00077   DependencyOutputOptions DependencyOutputOpts;
00078 
00079   /// Options controlling the diagnostic engine.
00080   DiagnosticOptions DiagnosticOpts;
00081 
00082   /// Options controlling file system operations.
00083   FileSystemOptions FileSystemOpts;
00084 
00085   /// Options controlling the frontend itself.
00086   FrontendOptions FrontendOpts;
00087 
00088   /// Options controlling the #include directive.
00089   HeaderSearchOptions HeaderSearchOpts;
00090 
00091   /// Options controlling the preprocessor (aside from #include handling).
00092   PreprocessorOptions PreprocessorOpts;
00093 
00094   /// Options controlling preprocessed output.
00095   PreprocessorOutputOptions PreprocessorOutputOpts;
00096 
00097   /// Options controlling the target.
00098   TargetOptions TargetOpts;
00099 
00100 public:
00101   CompilerInvocation() {}
00102 
00103   /// @name Utility Methods
00104   /// @{
00105 
00106   /// CreateFromArgs - Create a compiler invocation from a list of input
00107   /// options. Returns true on success.
00108   ///
00109   /// \param Res [out] - The resulting invocation.
00110   /// \param ArgBegin - The first element in the argument vector.
00111   /// \param ArgEnd - The last element in the argument vector.
00112   /// \param Diags - The diagnostic engine to use for errors.
00113   static bool CreateFromArgs(CompilerInvocation &Res,
00114                              const char* const *ArgBegin,
00115                              const char* const *ArgEnd,
00116                              DiagnosticsEngine &Diags);
00117 
00118   /// GetBuiltinIncludePath - Get the directory where the compiler headers
00119   /// reside, relative to the compiler binary (found by the passed in
00120   /// arguments).
00121   ///
00122   /// \param Argv0 - The program path (from argv[0]), for finding the builtin
00123   /// compiler path.
00124   /// \param MainAddr - The address of main (or some other function in the main
00125   /// executable), for finding the builtin compiler path.
00126   static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
00127 
00128   /// toArgs - Convert the CompilerInvocation to a list of strings suitable for
00129   /// passing to CreateFromArgs.
00130   void toArgs(std::vector<std::string> &Res);
00131 
00132   /// setLangDefaults - Set language defaults for the given input language and
00133   /// language standard in this CompilerInvocation.
00134   ///
00135   /// \param IK - The input language.
00136   /// \param LangStd - The input language standard.
00137   void setLangDefaults(InputKind IK,
00138                   LangStandard::Kind LangStd = LangStandard::lang_unspecified) {
00139     setLangDefaults(*getLangOpts(), IK, LangStd);
00140   }
00141 
00142   /// setLangDefaults - Set language defaults for the given input language and
00143   /// language standard in the given LangOptions object.
00144   ///
00145   /// \param LangOpts - The LangOptions object to set up.
00146   /// \param IK - The input language.
00147   /// \param LangStd - The input language standard.
00148   static void setLangDefaults(LangOptions &Opts, InputKind IK,
00149                    LangStandard::Kind LangStd = LangStandard::lang_unspecified);
00150   
00151   /// \brief Retrieve a module hash string that is suitable for uniquely 
00152   /// identifying the conditions under which the module was built.
00153   std::string getModuleHash() const;
00154   
00155   /// @}
00156   /// @name Option Subgroups
00157   /// @{
00158 
00159   AnalyzerOptions &getAnalyzerOpts() { return AnalyzerOpts; }
00160   const AnalyzerOptions &getAnalyzerOpts() const {
00161     return AnalyzerOpts;
00162   }
00163 
00164   MigratorOptions &getMigratorOpts() { return MigratorOpts; }
00165   const MigratorOptions &getMigratorOpts() const {
00166     return MigratorOpts;
00167   }
00168   
00169   CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
00170   const CodeGenOptions &getCodeGenOpts() const {
00171     return CodeGenOpts;
00172   }
00173 
00174   DependencyOutputOptions &getDependencyOutputOpts() {
00175     return DependencyOutputOpts;
00176   }
00177   const DependencyOutputOptions &getDependencyOutputOpts() const {
00178     return DependencyOutputOpts;
00179   }
00180 
00181   DiagnosticOptions &getDiagnosticOpts() { return DiagnosticOpts; }
00182   const DiagnosticOptions &getDiagnosticOpts() const { return DiagnosticOpts; }
00183 
00184   FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
00185   const FileSystemOptions &getFileSystemOpts() const {
00186     return FileSystemOpts;
00187   }
00188 
00189   HeaderSearchOptions &getHeaderSearchOpts() { return HeaderSearchOpts; }
00190   const HeaderSearchOptions &getHeaderSearchOpts() const {
00191     return HeaderSearchOpts;
00192   }
00193 
00194   FrontendOptions &getFrontendOpts() { return FrontendOpts; }
00195   const FrontendOptions &getFrontendOpts() const {
00196     return FrontendOpts;
00197   }
00198 
00199   PreprocessorOptions &getPreprocessorOpts() { return PreprocessorOpts; }
00200   const PreprocessorOptions &getPreprocessorOpts() const {
00201     return PreprocessorOpts;
00202   }
00203 
00204   PreprocessorOutputOptions &getPreprocessorOutputOpts() {
00205     return PreprocessorOutputOpts;
00206   }
00207   const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
00208     return PreprocessorOutputOpts;
00209   }
00210 
00211   TargetOptions &getTargetOpts() { return TargetOpts; }
00212   const TargetOptions &getTargetOpts() const {
00213     return TargetOpts;
00214   }
00215 
00216   /// @}
00217 };
00218 
00219 } // end namespace clang
00220 
00221 #endif