1 //===- CompilerInvocation.h - Compiler Invocation Helper Data ---*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H 10 #define LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H 11 12 #include "clang/Basic/CodeGenOptions.h" 13 #include "clang/Basic/DiagnosticOptions.h" 14 #include "clang/Basic/FileSystemOptions.h" 15 #include "clang/Basic/LLVM.h" 16 #include "clang/Basic/LangOptions.h" 17 #include "clang/Basic/LangStandard.h" 18 #include "clang/Frontend/DependencyOutputOptions.h" 19 #include "clang/Frontend/FrontendOptions.h" 20 #include "clang/Frontend/MigratorOptions.h" 21 #include "clang/Frontend/PreprocessorOutputOptions.h" 22 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" 23 #include "llvm/ADT/IntrusiveRefCntPtr.h" 24 #include "llvm/ADT/ArrayRef.h" 25 #include <memory> 26 #include <string> 27 28 namespace llvm { 29 30 class Triple; 31 32 namespace opt { 33 34 class ArgList; 35 36 } // namespace opt 37 38 namespace vfs { 39 40 class FileSystem; 41 42 } // namespace vfs 43 44 } // namespace llvm 45 46 namespace clang { 47 48 class DiagnosticsEngine; 49 class HeaderSearchOptions; 50 class PreprocessorOptions; 51 class TargetOptions; 52 53 /// Fill out Opts based on the options given in Args. 54 /// 55 /// Args must have been created from the OptTable returned by 56 /// createCC1OptTable(). 57 /// 58 /// When errors are encountered, return false and, if Diags is non-null, 59 /// report the error(s). 60 bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args, 61 DiagnosticsEngine *Diags = nullptr, 62 bool DefaultDiagColor = true); 63 64 class CompilerInvocationBase { 65 public: 66 /// Options controlling the language variant. 67 std::shared_ptr<LangOptions> LangOpts; 68 69 /// Options controlling the target. 70 std::shared_ptr<TargetOptions> TargetOpts; 71 72 /// Options controlling the diagnostic engine. 73 IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts; 74 75 /// Options controlling the \#include directive. 76 std::shared_ptr<HeaderSearchOptions> HeaderSearchOpts; 77 78 /// Options controlling the preprocessor (aside from \#include handling). 79 std::shared_ptr<PreprocessorOptions> PreprocessorOpts; 80 81 CompilerInvocationBase(); 82 CompilerInvocationBase(const CompilerInvocationBase &X); 83 CompilerInvocationBase &operator=(const CompilerInvocationBase &) = delete; 84 ~CompilerInvocationBase(); 85 86 LangOptions *getLangOpts() { return LangOpts.get(); } 87 const LangOptions *getLangOpts() const { return LangOpts.get(); } 88 89 TargetOptions &getTargetOpts() { return *TargetOpts.get(); } 90 const TargetOptions &getTargetOpts() const { return *TargetOpts.get(); } 91 92 DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; } 93 94 HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; } 95 96 const HeaderSearchOptions &getHeaderSearchOpts() const { 97 return *HeaderSearchOpts; 98 } 99 100 std::shared_ptr<HeaderSearchOptions> getHeaderSearchOptsPtr() const { 101 return HeaderSearchOpts; 102 } 103 104 std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() { 105 return PreprocessorOpts; 106 } 107 108 PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; } 109 110 const PreprocessorOptions &getPreprocessorOpts() const { 111 return *PreprocessorOpts; 112 } 113 }; 114 115 /// Helper class for holding the data necessary to invoke the compiler. 116 /// 117 /// This class is designed to represent an abstract "invocation" of the 118 /// compiler, including data such as the include paths, the code generation 119 /// options, the warning flags, and so on. 120 class CompilerInvocation : public CompilerInvocationBase { 121 /// Options controlling the static analyzer. 122 AnalyzerOptionsRef AnalyzerOpts; 123 124 MigratorOptions MigratorOpts; 125 126 /// Options controlling IRgen and the backend. 127 CodeGenOptions CodeGenOpts; 128 129 /// Options controlling dependency output. 130 DependencyOutputOptions DependencyOutputOpts; 131 132 /// Options controlling file system operations. 133 FileSystemOptions FileSystemOpts; 134 135 /// Options controlling the frontend itself. 136 FrontendOptions FrontendOpts; 137 138 /// Options controlling preprocessed output. 139 PreprocessorOutputOptions PreprocessorOutputOpts; 140 141 public: 142 CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {} 143 144 /// @name Utility Methods 145 /// @{ 146 147 /// Create a compiler invocation from a list of input options. 148 /// \returns true on success. 149 /// 150 /// \returns false if an error was encountered while parsing the arguments 151 /// and attempts to recover and continue parsing the rest of the arguments. 152 /// The recovery is best-effort and only guarantees that \p Res will end up in 153 /// one of the vaild-to-access (albeit arbitrary) states. 154 /// 155 /// \param [out] Res - The resulting invocation. 156 static bool CreateFromArgs(CompilerInvocation &Res, 157 ArrayRef<const char *> CommandLineArgs, 158 DiagnosticsEngine &Diags); 159 160 /// Get the directory where the compiler headers 161 /// reside, relative to the compiler binary (found by the passed in 162 /// arguments). 163 /// 164 /// \param Argv0 - The program path (from argv[0]), for finding the builtin 165 /// compiler path. 166 /// \param MainAddr - The address of main (or some other function in the main 167 /// executable), for finding the builtin compiler path. 168 static std::string GetResourcesPath(const char *Argv0, void *MainAddr); 169 170 /// Set language defaults for the given input language and 171 /// language standard in the given LangOptions object. 172 /// 173 /// \param Opts - The LangOptions object to set up. 174 /// \param IK - The input language. 175 /// \param T - The target triple. 176 /// \param PPOpts - The PreprocessorOptions affected. 177 /// \param LangStd - The input language standard. 178 static void setLangDefaults(LangOptions &Opts, InputKind IK, 179 const llvm::Triple &T, PreprocessorOptions &PPOpts, 180 LangStandard::Kind LangStd = LangStandard::lang_unspecified); 181 182 /// Retrieve a module hash string that is suitable for uniquely 183 /// identifying the conditions under which the module was built. 184 std::string getModuleHash() const; 185 186 /// @} 187 /// @name Option Subgroups 188 /// @{ 189 190 AnalyzerOptionsRef getAnalyzerOpts() const { return AnalyzerOpts; } 191 192 MigratorOptions &getMigratorOpts() { return MigratorOpts; } 193 const MigratorOptions &getMigratorOpts() const { return MigratorOpts; } 194 195 CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; } 196 const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; } 197 198 DependencyOutputOptions &getDependencyOutputOpts() { 199 return DependencyOutputOpts; 200 } 201 202 const DependencyOutputOptions &getDependencyOutputOpts() const { 203 return DependencyOutputOpts; 204 } 205 206 FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; } 207 208 const FileSystemOptions &getFileSystemOpts() const { 209 return FileSystemOpts; 210 } 211 212 FrontendOptions &getFrontendOpts() { return FrontendOpts; } 213 const FrontendOptions &getFrontendOpts() const { return FrontendOpts; } 214 215 PreprocessorOutputOptions &getPreprocessorOutputOpts() { 216 return PreprocessorOutputOpts; 217 } 218 219 const PreprocessorOutputOptions &getPreprocessorOutputOpts() const { 220 return PreprocessorOutputOpts; 221 } 222 223 /// @} 224 }; 225 226 IntrusiveRefCntPtr<llvm::vfs::FileSystem> 227 createVFSFromCompilerInvocation(const CompilerInvocation &CI, 228 DiagnosticsEngine &Diags); 229 230 IntrusiveRefCntPtr<llvm::vfs::FileSystem> createVFSFromCompilerInvocation( 231 const CompilerInvocation &CI, DiagnosticsEngine &Diags, 232 IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS); 233 234 } // namespace clang 235 236 #endif // LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H 237