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