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