xref: /freebsd/contrib/llvm-project/clang/include/clang/Lex/HeaderSearchOptions.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 
9 #ifndef LLVM_CLANG_LEX_HEADERSEARCHOPTIONS_H
10 #define LLVM_CLANG_LEX_HEADERSEARCHOPTIONS_H
11 
12 #include "clang/Basic/LLVM.h"
13 #include "llvm/ADT/CachedHashString.h"
14 #include "llvm/ADT/SetVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/HashBuilder.h"
17 #include <cstdint>
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 namespace clang {
23 
24 namespace frontend {
25 
26 /// IncludeDirGroup - Identifies the group an include Entry belongs to,
27 /// representing its relative positive in the search list.
28 /// \#include directives whose paths are enclosed by string quotes ("")
29 /// start searching at the Quoted group (specified by '-iquote'),
30 /// then search the Angled group, then the System group, etc.
31 enum IncludeDirGroup {
32   /// '\#include ""' paths, added by 'gcc -iquote'.
33   Quoted = 0,
34 
35   /// Paths for '\#include <>' added by '-I'.
36   Angled,
37 
38   /// Like Angled, but marks header maps used when building frameworks.
39   IndexHeaderMap,
40 
41   /// Like Angled, but marks system directories.
42   System,
43 
44   /// Like System, but headers are implicitly wrapped in extern "C".
45   ExternCSystem,
46 
47   /// Like System, but only used for C.
48   CSystem,
49 
50   /// Like System, but only used for C++.
51   CXXSystem,
52 
53   /// Like System, but only used for ObjC.
54   ObjCSystem,
55 
56   /// Like System, but only used for ObjC++.
57   ObjCXXSystem,
58 
59   /// Like System, but searched after the system directories.
60   After
61 };
62 
63 } // namespace frontend
64 
65 /// HeaderSearchOptions - Helper class for storing options related to the
66 /// initialization of the HeaderSearch object.
67 class HeaderSearchOptions {
68 public:
69   struct Entry {
70     std::string Path;
71     frontend::IncludeDirGroup Group;
72     LLVM_PREFERRED_TYPE(bool)
73     unsigned IsFramework : 1;
74 
75     /// IgnoreSysRoot - This is false if an absolute path should be treated
76     /// relative to the sysroot, or true if it should always be the absolute
77     /// path.
78     LLVM_PREFERRED_TYPE(bool)
79     unsigned IgnoreSysRoot : 1;
80 
EntryEntry81     Entry(StringRef path, frontend::IncludeDirGroup group, bool isFramework,
82           bool ignoreSysRoot)
83         : Path(path), Group(group), IsFramework(isFramework),
84           IgnoreSysRoot(ignoreSysRoot) {}
85   };
86 
87   struct SystemHeaderPrefix {
88     /// A prefix to be matched against paths in \#include directives.
89     std::string Prefix;
90 
91     /// True if paths beginning with this prefix should be treated as system
92     /// headers.
93     bool IsSystemHeader;
94 
SystemHeaderPrefixSystemHeaderPrefix95     SystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader)
96         : Prefix(Prefix), IsSystemHeader(IsSystemHeader) {}
97   };
98 
99   /// If non-empty, the directory to use as a "virtual system root" for include
100   /// paths.
101   std::string Sysroot;
102 
103   /// User specified include entries.
104   std::vector<Entry> UserEntries;
105 
106   /// User-specified system header prefixes.
107   std::vector<SystemHeaderPrefix> SystemHeaderPrefixes;
108 
109   /// The directory which holds the compiler resource files (builtin includes,
110   /// etc.).
111   std::string ResourceDir;
112 
113   /// The directory used for the module cache.
114   std::string ModuleCachePath;
115 
116   /// The directory used for a user build.
117   std::string ModuleUserBuildPath;
118 
119   /// The mapping of module names to prebuilt module files.
120   std::map<std::string, std::string, std::less<>> PrebuiltModuleFiles;
121 
122   /// The directories used to load prebuilt module files.
123   std::vector<std::string> PrebuiltModulePaths;
124 
125   /// The module/pch container format.
126   std::string ModuleFormat;
127 
128   /// Whether we should disable the use of the hash string within the
129   /// module cache.
130   ///
131   /// Note: Only used for testing!
132   LLVM_PREFERRED_TYPE(bool)
133   unsigned DisableModuleHash : 1;
134 
135   /// Implicit module maps.  This option is enabld by default when
136   /// modules is enabled.
137   LLVM_PREFERRED_TYPE(bool)
138   unsigned ImplicitModuleMaps : 1;
139 
140   /// Set the 'home directory' of a module map file to the current
141   /// working directory (or the home directory of the module map file that
142   /// contained the 'extern module' directive importing this module map file
143   /// if any) rather than the directory containing the module map file.
144   //
145   /// The home directory is where we look for files named in the module map
146   /// file.
147   LLVM_PREFERRED_TYPE(bool)
148   unsigned ModuleMapFileHomeIsCwd : 1;
149 
150   /// Set the base path of a built module file to be the current working
151   /// directory. This is useful for sharing module files across machines
152   /// that build with different paths without having to rewrite all
153   /// modulemap files to have working directory relative paths.
154   LLVM_PREFERRED_TYPE(bool)
155   unsigned ModuleFileHomeIsCwd : 1;
156 
157   /// Also search for prebuilt implicit modules in the prebuilt module cache
158   /// path.
159   LLVM_PREFERRED_TYPE(bool)
160   unsigned EnablePrebuiltImplicitModules : 1;
161 
162   /// The interval (in seconds) between pruning operations.
163   ///
164   /// This operation is expensive, because it requires Clang to walk through
165   /// the directory structure of the module cache, stat()'ing and removing
166   /// files.
167   ///
168   /// The default value is large, e.g., the operation runs once a week.
169   unsigned ModuleCachePruneInterval = 7 * 24 * 60 * 60;
170 
171   /// The time (in seconds) after which an unused module file will be
172   /// considered unused and will, therefore, be pruned.
173   ///
174   /// When the module cache is pruned, any module file that has not been
175   /// accessed in this many seconds will be removed. The default value is
176   /// large, e.g., a month, to avoid forcing infrequently-used modules to be
177   /// regenerated often.
178   unsigned ModuleCachePruneAfter = 31 * 24 * 60 * 60;
179 
180   /// The time in seconds when the build session started.
181   ///
182   /// This time is used by other optimizations in header search and module
183   /// loading.
184   uint64_t BuildSessionTimestamp = 0;
185 
186   /// The set of macro names that should be ignored for the purposes
187   /// of computing the module hash.
188   llvm::SmallSetVector<llvm::CachedHashString, 16> ModulesIgnoreMacros;
189 
190   /// The set of user-provided virtual filesystem overlay files.
191   std::vector<std::string> VFSOverlayFiles;
192 
193   /// Include the compiler builtin includes.
194   LLVM_PREFERRED_TYPE(bool)
195   unsigned UseBuiltinIncludes : 1;
196 
197   /// Include the system standard include search directories.
198   LLVM_PREFERRED_TYPE(bool)
199   unsigned UseStandardSystemIncludes : 1;
200 
201   /// Include the system standard C++ library include search directories.
202   LLVM_PREFERRED_TYPE(bool)
203   unsigned UseStandardCXXIncludes : 1;
204 
205   /// Use libc++ instead of the default libstdc++.
206   LLVM_PREFERRED_TYPE(bool)
207   unsigned UseLibcxx : 1;
208 
209   /// Whether header search information should be output as for -v.
210   LLVM_PREFERRED_TYPE(bool)
211   unsigned Verbose : 1;
212 
213   /// If true, skip verifying input files used by modules if the
214   /// module was already verified during this build session (see
215   /// \c BuildSessionTimestamp).
216   LLVM_PREFERRED_TYPE(bool)
217   unsigned ModulesValidateOncePerBuildSession : 1;
218 
219   /// Whether to validate system input files when a module is loaded.
220   LLVM_PREFERRED_TYPE(bool)
221   unsigned ModulesValidateSystemHeaders : 1;
222 
223   // Whether the content of input files should be hashed and used to
224   // validate consistency.
225   LLVM_PREFERRED_TYPE(bool)
226   unsigned ValidateASTInputFilesContent : 1;
227 
228   // Whether the input files from C++20 Modules should be checked.
229   LLVM_PREFERRED_TYPE(bool)
230   unsigned ForceCheckCXX20ModulesInputFiles : 1;
231 
232   /// Whether the module includes debug information (-gmodules).
233   LLVM_PREFERRED_TYPE(bool)
234   unsigned UseDebugInfo : 1;
235 
236   LLVM_PREFERRED_TYPE(bool)
237   unsigned ModulesValidateDiagnosticOptions : 1;
238 
239   /// Whether to entirely skip writing diagnostic options.
240   /// Primarily used to speed up deserialization during dependency scanning.
241   LLVM_PREFERRED_TYPE(bool)
242   unsigned ModulesSkipDiagnosticOptions : 1;
243 
244   /// Whether to entirely skip writing header search paths.
245   /// Primarily used to speed up deserialization during dependency scanning.
246   LLVM_PREFERRED_TYPE(bool)
247   unsigned ModulesSkipHeaderSearchPaths : 1;
248 
249   /// Whether to entirely skip writing pragma diagnostic mappings.
250   /// Primarily used to speed up deserialization during dependency scanning.
251   LLVM_PREFERRED_TYPE(bool)
252   unsigned ModulesSkipPragmaDiagnosticMappings : 1;
253 
254   /// Whether to prune non-affecting module map files from PCM files.
255   LLVM_PREFERRED_TYPE(bool)
256   unsigned ModulesPruneNonAffectingModuleMaps : 1;
257 
258   LLVM_PREFERRED_TYPE(bool)
259   unsigned ModulesHashContent : 1;
260 
261   /// Whether we should include all things that could impact the module in the
262   /// hash.
263   ///
264   /// This includes things like the full header search path, and enabled
265   /// diagnostics.
266   LLVM_PREFERRED_TYPE(bool)
267   unsigned ModulesStrictContextHash : 1;
268 
269   /// Whether to include ivfsoverlay usage information in written AST files.
270   LLVM_PREFERRED_TYPE(bool)
271   unsigned ModulesIncludeVFSUsage : 1;
272 
273   HeaderSearchOptions(StringRef _Sysroot = "/")
Sysroot(_Sysroot)274       : Sysroot(_Sysroot), ModuleFormat("raw"), DisableModuleHash(false),
275         ImplicitModuleMaps(false), ModuleMapFileHomeIsCwd(false),
276         ModuleFileHomeIsCwd(false), EnablePrebuiltImplicitModules(false),
277         UseBuiltinIncludes(true), UseStandardSystemIncludes(true),
278         UseStandardCXXIncludes(true), UseLibcxx(false), Verbose(false),
279         ModulesValidateOncePerBuildSession(false),
280         ModulesValidateSystemHeaders(false),
281         ValidateASTInputFilesContent(false),
282         ForceCheckCXX20ModulesInputFiles(false), UseDebugInfo(false),
283         ModulesValidateDiagnosticOptions(true),
284         ModulesSkipDiagnosticOptions(false),
285         ModulesSkipHeaderSearchPaths(false),
286         ModulesSkipPragmaDiagnosticMappings(false),
287         ModulesPruneNonAffectingModuleMaps(true), ModulesHashContent(false),
288         ModulesStrictContextHash(false), ModulesIncludeVFSUsage(false) {}
289 
290   /// AddPath - Add the \p Path path to the specified \p Group list.
AddPath(StringRef Path,frontend::IncludeDirGroup Group,bool IsFramework,bool IgnoreSysRoot)291   void AddPath(StringRef Path, frontend::IncludeDirGroup Group,
292                bool IsFramework, bool IgnoreSysRoot) {
293     UserEntries.emplace_back(Path, Group, IsFramework, IgnoreSysRoot);
294   }
295 
296   /// AddSystemHeaderPrefix - Override whether \#include directives naming a
297   /// path starting with \p Prefix should be considered as naming a system
298   /// header.
AddSystemHeaderPrefix(StringRef Prefix,bool IsSystemHeader)299   void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) {
300     SystemHeaderPrefixes.emplace_back(Prefix, IsSystemHeader);
301   }
302 
AddVFSOverlayFile(StringRef Name)303   void AddVFSOverlayFile(StringRef Name) {
304     VFSOverlayFiles.push_back(std::string(Name));
305   }
306 
AddPrebuiltModulePath(StringRef Name)307   void AddPrebuiltModulePath(StringRef Name) {
308     PrebuiltModulePaths.push_back(std::string(Name));
309   }
310 };
311 
312 template <typename HasherT, llvm::endianness Endianness>
addHash(llvm::HashBuilder<HasherT,Endianness> & HBuilder,const HeaderSearchOptions::Entry & E)313 inline void addHash(llvm::HashBuilder<HasherT, Endianness> &HBuilder,
314                     const HeaderSearchOptions::Entry &E) {
315   HBuilder.add(E.Path, E.Group, E.IsFramework, E.IgnoreSysRoot);
316 }
317 
318 template <typename HasherT, llvm::endianness Endianness>
addHash(llvm::HashBuilder<HasherT,Endianness> & HBuilder,const HeaderSearchOptions::SystemHeaderPrefix & SHP)319 inline void addHash(llvm::HashBuilder<HasherT, Endianness> &HBuilder,
320                     const HeaderSearchOptions::SystemHeaderPrefix &SHP) {
321   HBuilder.add(SHP.Prefix, SHP.IsSystemHeader);
322 }
323 
324 } // namespace clang
325 
326 #endif // LLVM_CLANG_LEX_HEADERSEARCHOPTIONS_H
327