CompilerInvocation.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. /// Retrieve a module hash string that is suitable for uniquely
  178. /// identifying the conditions under which the module was built.
  179. std::string getModuleHash() const;
  180. using StringAllocator = llvm::function_ref<const char *(const llvm::Twine &)>;
  181. /// Generate cc1-compatible command line arguments from this instance.
  182. ///
  183. /// \param [out] Args - The generated arguments. Note that the caller is
  184. /// responsible for inserting the path to the clang executable and "-cc1" if
  185. /// desired.
  186. /// \param SA - A function that given a Twine can allocate storage for a given
  187. /// command line argument and return a pointer to the newly allocated string.
  188. /// The returned pointer is what gets appended to Args.
  189. void generateCC1CommandLine(llvm::SmallVectorImpl<const char *> &Args,
  190. StringAllocator SA) const;
  191. /// Generate cc1-compatible command line arguments from this instance,
  192. /// wrapping the result as a std::vector<std::string>.
  193. ///
  194. /// This is a (less-efficient) wrapper over generateCC1CommandLine().
  195. std::vector<std::string> getCC1CommandLine() const;
  196. /// Reset all of the options that are not considered when building a
  197. /// module.
  198. void resetNonModularOptions();
  199. /// Disable implicit modules and canonicalize options that are only used by
  200. /// implicit modules.
  201. void clearImplicitModuleBuildOptions();
  202. private:
  203. static bool CreateFromArgsImpl(CompilerInvocation &Res,
  204. ArrayRef<const char *> CommandLineArgs,
  205. DiagnosticsEngine &Diags, const char *Argv0);
  206. /// Generate command line options from DiagnosticOptions.
  207. static void GenerateDiagnosticArgs(const DiagnosticOptions &Opts,
  208. SmallVectorImpl<const char *> &Args,
  209. StringAllocator SA, bool DefaultDiagColor);
  210. /// Parse command line options that map to LangOptions.
  211. static bool ParseLangArgs(LangOptions &Opts, llvm::opt::ArgList &Args,
  212. InputKind IK, const llvm::Triple &T,
  213. std::vector<std::string> &Includes,
  214. DiagnosticsEngine &Diags);
  215. /// Generate command line options from LangOptions.
  216. static void GenerateLangArgs(const LangOptions &Opts,
  217. SmallVectorImpl<const char *> &Args,
  218. StringAllocator SA, const llvm::Triple &T,
  219. InputKind IK);
  220. /// Parse command line options that map to CodeGenOptions.
  221. static bool ParseCodeGenArgs(CodeGenOptions &Opts, llvm::opt::ArgList &Args,
  222. InputKind IK, DiagnosticsEngine &Diags,
  223. const llvm::Triple &T,
  224. const std::string &OutputFile,
  225. const LangOptions &LangOptsRef);
  226. // Generate command line options from CodeGenOptions.
  227. static void GenerateCodeGenArgs(const CodeGenOptions &Opts,
  228. SmallVectorImpl<const char *> &Args,
  229. StringAllocator SA, const llvm::Triple &T,
  230. const std::string &OutputFile,
  231. const LangOptions *LangOpts);
  232. };
  233. IntrusiveRefCntPtr<llvm::vfs::FileSystem>
  234. createVFSFromCompilerInvocationOrig(const CompilerInvocation &CI,
  235. DiagnosticsEngine &Diags);
  236. IntrusiveRefCntPtr<llvm::vfs::FileSystem> createVFSFromCompilerInvocation(
  237. const CompilerInvocation &CI, DiagnosticsEngine &Diags,
  238. IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
  239. IntrusiveRefCntPtr<llvm::vfs::FileSystem>
  240. createVFSFromCompilerInvocation(const CompilerInvocation &CI,
  241. DiagnosticsEngine &Diags);
  242. IntrusiveRefCntPtr<llvm::vfs::FileSystem>
  243. createVFSFromOverlayFiles(ArrayRef<std::string> VFSOverlayFiles,
  244. DiagnosticsEngine &Diags,
  245. IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS);
  246. } // namespace clang
  247. #endif // LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H
  248. #ifdef __GNUC__
  249. #pragma GCC diagnostic pop
  250. #endif