HeaderSearchOptions.h 8.6 KB

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