CompilerInvocation.h 11 KB

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