CompilerInvocation.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #ifndef LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H_
  10. #define LLVM_CLANG_FRONTEND_COMPILERINVOCATION_H_
  11. #include "clang/Basic/DiagnosticOptions.h"
  12. #include "clang/Basic/FileSystemOptions.h"
  13. #include "clang/Basic/LangOptions.h"
  14. #include "clang/Frontend/CodeGenOptions.h"
  15. #include "clang/Frontend/DependencyOutputOptions.h"
  16. #include "clang/Frontend/FrontendOptions.h"
  17. #include "clang/Frontend/LangStandard.h"
  18. #include "clang/Frontend/MigratorOptions.h"
  19. #include "clang/Frontend/PreprocessorOutputOptions.h"
  20. #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
  21. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  22. #include <string>
  23. namespace llvm {
  24. class Triple;
  25. namespace opt {
  26. class ArgList;
  27. }
  28. }
  29. namespace clang {
  30. class PreprocessorOptions;
  31. class HeaderSearchOptions;
  32. class TargetOptions;
  33. class LangOptions;
  34. class CompilerInvocation;
  35. class DiagnosticsEngine;
  36. /// \brief Fill out Opts based on the options given in Args.
  37. ///
  38. /// Args must have been created from the OptTable returned by
  39. /// createCC1OptTable().
  40. ///
  41. /// When errors are encountered, return false and, if Diags is non-null,
  42. /// report the error(s).
  43. bool ParseDiagnosticArgs(DiagnosticOptions &Opts, llvm::opt::ArgList &Args,
  44. DiagnosticsEngine *Diags = nullptr,
  45. bool DefaultDiagColor = true,
  46. bool DefaultShowOpt = true);
  47. class CompilerInvocationBase {
  48. void operator=(const CompilerInvocationBase &) = delete;
  49. public:
  50. /// Options controlling the language variant.
  51. std::shared_ptr<LangOptions> LangOpts;
  52. /// Options controlling the target.
  53. std::shared_ptr<TargetOptions> TargetOpts;
  54. /// Options controlling the diagnostic engine.
  55. IntrusiveRefCntPtr<DiagnosticOptions> DiagnosticOpts;
  56. /// Options controlling the \#include directive.
  57. IntrusiveRefCntPtr<HeaderSearchOptions> HeaderSearchOpts;
  58. /// Options controlling the preprocessor (aside from \#include handling).
  59. std::shared_ptr<PreprocessorOptions> PreprocessorOpts;
  60. CompilerInvocationBase();
  61. ~CompilerInvocationBase();
  62. CompilerInvocationBase(const CompilerInvocationBase &X);
  63. LangOptions *getLangOpts() { return LangOpts.get(); }
  64. const LangOptions *getLangOpts() const { return LangOpts.get(); }
  65. TargetOptions &getTargetOpts() { return *TargetOpts.get(); }
  66. const TargetOptions &getTargetOpts() const {
  67. return *TargetOpts.get();
  68. }
  69. DiagnosticOptions &getDiagnosticOpts() const { return *DiagnosticOpts; }
  70. HeaderSearchOptions &getHeaderSearchOpts() { return *HeaderSearchOpts; }
  71. const HeaderSearchOptions &getHeaderSearchOpts() const {
  72. return *HeaderSearchOpts;
  73. }
  74. std::shared_ptr<PreprocessorOptions> getPreprocessorOptsPtr() {
  75. return PreprocessorOpts;
  76. }
  77. PreprocessorOptions &getPreprocessorOpts() { return *PreprocessorOpts; }
  78. const PreprocessorOptions &getPreprocessorOpts() const {
  79. return *PreprocessorOpts;
  80. }
  81. };
  82. /// \brief Helper class for holding the data necessary to invoke the compiler.
  83. ///
  84. /// This class is designed to represent an abstract "invocation" of the
  85. /// compiler, including data such as the include paths, the code generation
  86. /// options, the warning flags, and so on.
  87. class CompilerInvocation : public CompilerInvocationBase {
  88. /// Options controlling the static analyzer.
  89. AnalyzerOptionsRef AnalyzerOpts;
  90. MigratorOptions MigratorOpts;
  91. /// Options controlling IRgen and the backend.
  92. CodeGenOptions CodeGenOpts;
  93. /// Options controlling dependency output.
  94. DependencyOutputOptions DependencyOutputOpts;
  95. /// Options controlling file system operations.
  96. FileSystemOptions FileSystemOpts;
  97. /// Options controlling the frontend itself.
  98. FrontendOptions FrontendOpts;
  99. /// Options controlling preprocessed output.
  100. PreprocessorOutputOptions PreprocessorOutputOpts;
  101. public:
  102. CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
  103. /// @name Utility Methods
  104. /// @{
  105. /// \brief Create a compiler invocation from a list of input options.
  106. /// \returns true on success.
  107. ///
  108. /// \param [out] Res - The resulting invocation.
  109. /// \param ArgBegin - The first element in the argument vector.
  110. /// \param ArgEnd - The last element in the argument vector.
  111. /// \param Diags - The diagnostic engine to use for errors.
  112. static bool CreateFromArgs(CompilerInvocation &Res,
  113. const char* const *ArgBegin,
  114. const char* const *ArgEnd,
  115. DiagnosticsEngine &Diags);
  116. /// \brief Get the directory where the compiler headers
  117. /// reside, relative to the compiler binary (found by the passed in
  118. /// arguments).
  119. ///
  120. /// \param Argv0 - The program path (from argv[0]), for finding the builtin
  121. /// compiler path.
  122. /// \param MainAddr - The address of main (or some other function in the main
  123. /// executable), for finding the builtin compiler path.
  124. static std::string GetResourcesPath(const char *Argv0, void *MainAddr);
  125. /// \brief Set language defaults for the given input language and
  126. /// language standard in the given LangOptions object.
  127. ///
  128. /// \param Opts - The LangOptions object to set up.
  129. /// \param IK - The input language.
  130. /// \param T - The target triple.
  131. /// \param PPOpts - The PreprocessorOptions affected.
  132. /// \param LangStd - The input language standard.
  133. static void setLangDefaults(LangOptions &Opts, InputKind IK,
  134. const llvm::Triple &T, PreprocessorOptions &PPOpts,
  135. LangStandard::Kind LangStd = LangStandard::lang_unspecified);
  136. /// \brief Retrieve a module hash string that is suitable for uniquely
  137. /// identifying the conditions under which the module was built.
  138. std::string getModuleHash() const;
  139. /// @}
  140. /// @name Option Subgroups
  141. /// @{
  142. AnalyzerOptionsRef getAnalyzerOpts() const {
  143. return AnalyzerOpts;
  144. }
  145. MigratorOptions &getMigratorOpts() { return MigratorOpts; }
  146. const MigratorOptions &getMigratorOpts() const {
  147. return MigratorOpts;
  148. }
  149. CodeGenOptions &getCodeGenOpts() { return CodeGenOpts; }
  150. const CodeGenOptions &getCodeGenOpts() const {
  151. return CodeGenOpts;
  152. }
  153. DependencyOutputOptions &getDependencyOutputOpts() {
  154. return DependencyOutputOpts;
  155. }
  156. const DependencyOutputOptions &getDependencyOutputOpts() const {
  157. return DependencyOutputOpts;
  158. }
  159. FileSystemOptions &getFileSystemOpts() { return FileSystemOpts; }
  160. const FileSystemOptions &getFileSystemOpts() const {
  161. return FileSystemOpts;
  162. }
  163. FrontendOptions &getFrontendOpts() { return FrontendOpts; }
  164. const FrontendOptions &getFrontendOpts() const {
  165. return FrontendOpts;
  166. }
  167. PreprocessorOutputOptions &getPreprocessorOutputOpts() {
  168. return PreprocessorOutputOpts;
  169. }
  170. const PreprocessorOutputOptions &getPreprocessorOutputOpts() const {
  171. return PreprocessorOutputOpts;
  172. }
  173. /// @}
  174. };
  175. namespace vfs {
  176. class FileSystem;
  177. }
  178. IntrusiveRefCntPtr<vfs::FileSystem>
  179. createVFSFromCompilerInvocation(const CompilerInvocation &CI,
  180. DiagnosticsEngine &Diags);
  181. } // end namespace clang
  182. #endif