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