HeaderSearchOptions.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- HeaderSearchOptions.h ------------------------------------*- 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_LEX_HEADERSEARCHOPTIONS_H
  14. #define LLVM_CLANG_LEX_HEADERSEARCHOPTIONS_H
  15. #include "clang/Basic/LLVM.h"
  16. #include "llvm/ADT/CachedHashString.h"
  17. #include "llvm/ADT/Hashing.h"
  18. #include "llvm/ADT/SetVector.h"
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/Support/HashBuilder.h"
  21. #include <cstdint>
  22. #include <map>
  23. #include <string>
  24. #include <vector>
  25. namespace clang {
  26. namespace frontend {
  27. /// IncludeDirGroup - Identifies the group an include Entry belongs to,
  28. /// representing its relative positive in the search list.
  29. /// \#include directives whose paths are enclosed by string quotes ("")
  30. /// start searching at the Quoted group (specified by '-iquote'),
  31. /// then search the Angled group, then the System group, etc.
  32. enum IncludeDirGroup {
  33. /// '\#include ""' paths, added by 'gcc -iquote'.
  34. Quoted = 0,
  35. /// Paths for '\#include <>' added by '-I'.
  36. Angled,
  37. /// Like Angled, but marks header maps used when building frameworks.
  38. IndexHeaderMap,
  39. /// Like Angled, but marks system directories.
  40. System,
  41. /// Like System, but headers are implicitly wrapped in extern "C".
  42. ExternCSystem,
  43. /// Like System, but only used for C.
  44. CSystem,
  45. /// Like System, but only used for C++.
  46. CXXSystem,
  47. /// Like System, but only used for ObjC.
  48. ObjCSystem,
  49. /// Like System, but only used for ObjC++.
  50. ObjCXXSystem,
  51. /// Like System, but searched after the system directories.
  52. After
  53. };
  54. } // namespace frontend
  55. /// HeaderSearchOptions - Helper class for storing options related to the
  56. /// initialization of the HeaderSearch object.
  57. class HeaderSearchOptions {
  58. public:
  59. struct Entry {
  60. std::string Path;
  61. frontend::IncludeDirGroup Group;
  62. unsigned IsFramework : 1;
  63. /// IgnoreSysRoot - This is false if an absolute path should be treated
  64. /// relative to the sysroot, or true if it should always be the absolute
  65. /// path.
  66. unsigned IgnoreSysRoot : 1;
  67. Entry(StringRef path, frontend::IncludeDirGroup group, bool isFramework,
  68. bool ignoreSysRoot)
  69. : Path(path), Group(group), IsFramework(isFramework),
  70. IgnoreSysRoot(ignoreSysRoot) {}
  71. };
  72. struct SystemHeaderPrefix {
  73. /// A prefix to be matched against paths in \#include directives.
  74. std::string Prefix;
  75. /// True if paths beginning with this prefix should be treated as system
  76. /// headers.
  77. bool IsSystemHeader;
  78. SystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader)
  79. : Prefix(Prefix), IsSystemHeader(IsSystemHeader) {}
  80. };
  81. /// If non-empty, the directory to use as a "virtual system root" for include
  82. /// paths.
  83. std::string Sysroot;
  84. /// User specified include entries.
  85. std::vector<Entry> UserEntries;
  86. /// User-specified system header prefixes.
  87. std::vector<SystemHeaderPrefix> SystemHeaderPrefixes;
  88. /// The directory which holds the compiler resource files (builtin includes,
  89. /// etc.).
  90. std::string ResourceDir;
  91. /// The directory used for the module cache.
  92. std::string ModuleCachePath;
  93. /// The directory used for a user build.
  94. std::string ModuleUserBuildPath;
  95. /// The mapping of module names to prebuilt module files.
  96. std::map<std::string, std::string, std::less<>> PrebuiltModuleFiles;
  97. /// The directories used to load prebuilt module files.
  98. std::vector<std::string> PrebuiltModulePaths;
  99. /// The module/pch container format.
  100. std::string ModuleFormat;
  101. /// Whether we should disable the use of the hash string within the
  102. /// module cache.
  103. ///
  104. /// Note: Only used for testing!
  105. unsigned DisableModuleHash : 1;
  106. /// Implicit module maps. This option is enabld by default when
  107. /// modules is enabled.
  108. unsigned ImplicitModuleMaps : 1;
  109. /// Set the 'home directory' of a module map file to the current
  110. /// working directory (or the home directory of the module map file that
  111. /// contained the 'extern module' directive importing this module map file
  112. /// if any) rather than the directory containing the module map file.
  113. //
  114. /// The home directory is where we look for files named in the module map
  115. /// file.
  116. unsigned ModuleMapFileHomeIsCwd : 1;
  117. /// Also search for prebuilt implicit modules in the prebuilt module cache
  118. /// path.
  119. unsigned EnablePrebuiltImplicitModules : 1;
  120. /// The interval (in seconds) between pruning operations.
  121. ///
  122. /// This operation is expensive, because it requires Clang to walk through
  123. /// the directory structure of the module cache, stat()'ing and removing
  124. /// files.
  125. ///
  126. /// The default value is large, e.g., the operation runs once a week.
  127. unsigned ModuleCachePruneInterval = 7 * 24 * 60 * 60;
  128. /// The time (in seconds) after which an unused module file will be
  129. /// considered unused and will, therefore, be pruned.
  130. ///
  131. /// When the module cache is pruned, any module file that has not been
  132. /// accessed in this many seconds will be removed. The default value is
  133. /// large, e.g., a month, to avoid forcing infrequently-used modules to be
  134. /// regenerated often.
  135. unsigned ModuleCachePruneAfter = 31 * 24 * 60 * 60;
  136. /// The time in seconds when the build session started.
  137. ///
  138. /// This time is used by other optimizations in header search and module
  139. /// loading.
  140. uint64_t BuildSessionTimestamp = 0;
  141. /// The set of macro names that should be ignored for the purposes
  142. /// of computing the module hash.
  143. llvm::SmallSetVector<llvm::CachedHashString, 16> ModulesIgnoreMacros;
  144. /// The set of user-provided virtual filesystem overlay files.
  145. std::vector<std::string> VFSOverlayFiles;
  146. /// Include the compiler builtin includes.
  147. unsigned UseBuiltinIncludes : 1;
  148. /// Include the system standard include search directories.
  149. unsigned UseStandardSystemIncludes : 1;
  150. /// Include the system standard C++ library include search directories.
  151. unsigned UseStandardCXXIncludes : 1;
  152. /// Use libc++ instead of the default libstdc++.
  153. unsigned UseLibcxx : 1;
  154. /// Whether header search information should be output as for -v.
  155. unsigned Verbose : 1;
  156. /// Whether header search should be case-insensitive.
  157. unsigned CaseInsensitive : 1;
  158. /// If true, skip verifying input files used by modules if the
  159. /// module was already verified during this build session (see
  160. /// \c BuildSessionTimestamp).
  161. unsigned ModulesValidateOncePerBuildSession : 1;
  162. /// Whether to validate system input files when a module is loaded.
  163. unsigned ModulesValidateSystemHeaders : 1;
  164. // Whether the content of input files should be hashed and used to
  165. // validate consistency.
  166. unsigned ValidateASTInputFilesContent : 1;
  167. /// Whether the module includes debug information (-gmodules).
  168. unsigned UseDebugInfo : 1;
  169. unsigned ModulesValidateDiagnosticOptions : 1;
  170. unsigned ModulesHashContent : 1;
  171. /// Whether we should include all things that could impact the module in the
  172. /// hash.
  173. ///
  174. /// This includes things like the full header search path, and enabled
  175. /// diagnostics.
  176. unsigned ModulesStrictContextHash : 1;
  177. HeaderSearchOptions(StringRef _Sysroot = "/")
  178. : Sysroot(_Sysroot), ModuleFormat("raw"), DisableModuleHash(false),
  179. ImplicitModuleMaps(false), ModuleMapFileHomeIsCwd(false),
  180. EnablePrebuiltImplicitModules(false), UseBuiltinIncludes(true),
  181. UseStandardSystemIncludes(true), UseStandardCXXIncludes(true),
  182. UseLibcxx(false), Verbose(false), CaseInsensitive(false),
  183. ModulesValidateOncePerBuildSession(false),
  184. ModulesValidateSystemHeaders(false),
  185. ValidateASTInputFilesContent(false), UseDebugInfo(false),
  186. ModulesValidateDiagnosticOptions(true), ModulesHashContent(false),
  187. ModulesStrictContextHash(false) {}
  188. /// AddPath - Add the \p Path path to the specified \p Group list.
  189. void AddPath(StringRef Path, frontend::IncludeDirGroup Group,
  190. bool IsFramework, bool IgnoreSysRoot) {
  191. UserEntries.emplace_back(Path, Group, IsFramework, IgnoreSysRoot);
  192. }
  193. /// AddSystemHeaderPrefix - Override whether \#include directives naming a
  194. /// path starting with \p Prefix should be considered as naming a system
  195. /// header.
  196. void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) {
  197. SystemHeaderPrefixes.emplace_back(Prefix, IsSystemHeader);
  198. }
  199. void AddVFSOverlayFile(StringRef Name) {
  200. VFSOverlayFiles.push_back(std::string(Name));
  201. }
  202. void AddPrebuiltModulePath(StringRef Name) {
  203. PrebuiltModulePaths.push_back(std::string(Name));
  204. }
  205. };
  206. inline llvm::hash_code hash_value(const HeaderSearchOptions::Entry &E) {
  207. return llvm::hash_combine(E.Path, E.Group, E.IsFramework, E.IgnoreSysRoot);
  208. }
  209. template <typename HasherT, llvm::support::endianness Endianness>
  210. inline void addHash(llvm::HashBuilderImpl<HasherT, Endianness> &HBuilder,
  211. const HeaderSearchOptions::Entry &E) {
  212. HBuilder.add(E.Path, E.Group, E.IsFramework, E.IgnoreSysRoot);
  213. }
  214. inline llvm::hash_code
  215. hash_value(const HeaderSearchOptions::SystemHeaderPrefix &SHP) {
  216. return llvm::hash_combine(SHP.Prefix, SHP.IsSystemHeader);
  217. }
  218. template <typename HasherT, llvm::support::endianness Endianness>
  219. inline void addHash(llvm::HashBuilderImpl<HasherT, Endianness> &HBuilder,
  220. const HeaderSearchOptions::SystemHeaderPrefix &SHP) {
  221. HBuilder.add(SHP.Prefix, SHP.IsSystemHeader);
  222. }
  223. } // namespace clang
  224. #endif // LLVM_CLANG_LEX_HEADERSEARCHOPTIONS_H
  225. #ifdef __GNUC__
  226. #pragma GCC diagnostic pop
  227. #endif