CompilerInvocation.h 7.0 KB

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