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/Hashing.h"
15 #include "llvm/ADT/SetVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include <cstdint>
18 #include <string>
19 #include <vector>
20 #include <map>
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     unsigned IsFramework : 1;
73 
74     /// IgnoreSysRoot - This is false if an absolute path should be treated
75     /// relative to the sysroot, or true if it should always be the absolute
76     /// path.
77     unsigned IgnoreSysRoot : 1;
78 
79     Entry(StringRef path, frontend::IncludeDirGroup group, bool isFramework,
80           bool ignoreSysRoot)
81         : Path(path), Group(group), IsFramework(isFramework),
82           IgnoreSysRoot(ignoreSysRoot) {}
83   };
84 
85   struct SystemHeaderPrefix {
86     /// A prefix to be matched against paths in \#include directives.
87     std::string Prefix;
88 
89     /// True if paths beginning with this prefix should be treated as system
90     /// headers.
91     bool IsSystemHeader;
92 
93     SystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader)
94         : Prefix(Prefix), IsSystemHeader(IsSystemHeader) {}
95   };
96 
97   /// If non-empty, the directory to use as a "virtual system root" for include
98   /// paths.
99   std::string Sysroot;
100 
101   /// User specified include entries.
102   std::vector<Entry> UserEntries;
103 
104   /// User-specified system header prefixes.
105   std::vector<SystemHeaderPrefix> SystemHeaderPrefixes;
106 
107   /// The directory which holds the compiler resource files (builtin includes,
108   /// etc.).
109   std::string ResourceDir;
110 
111   /// The directory used for the module cache.
112   std::string ModuleCachePath;
113 
114   /// The directory used for a user build.
115   std::string ModuleUserBuildPath;
116 
117   /// The mapping of module names to prebuilt module files.
118   std::map<std::string, std::string, std::less<>> PrebuiltModuleFiles;
119 
120   /// The directories used to load prebuilt module files.
121   std::vector<std::string> PrebuiltModulePaths;
122 
123   /// The module/pch container format.
124   std::string ModuleFormat;
125 
126   /// Whether we should disable the use of the hash string within the
127   /// module cache.
128   ///
129   /// Note: Only used for testing!
130   unsigned DisableModuleHash : 1;
131 
132   /// Implicit module maps.  This option is enabld by default when
133   /// modules is enabled.
134   unsigned ImplicitModuleMaps : 1;
135 
136   /// Set the 'home directory' of a module map file to the current
137   /// working directory (or the home directory of the module map file that
138   /// contained the 'extern module' directive importing this module map file
139   /// if any) rather than the directory containing the module map file.
140   //
141   /// The home directory is where we look for files named in the module map
142   /// file.
143   unsigned ModuleMapFileHomeIsCwd : 1;
144 
145   /// Also search for prebuilt implicit modules in the prebuilt module cache
146   /// path.
147   unsigned EnablePrebuiltImplicitModules : 1;
148 
149   /// The interval (in seconds) between pruning operations.
150   ///
151   /// This operation is expensive, because it requires Clang to walk through
152   /// the directory structure of the module cache, stat()'ing and removing
153   /// files.
154   ///
155   /// The default value is large, e.g., the operation runs once a week.
156   unsigned ModuleCachePruneInterval = 7 * 24 * 60 * 60;
157 
158   /// The time (in seconds) after which an unused module file will be
159   /// considered unused and will, therefore, be pruned.
160   ///
161   /// When the module cache is pruned, any module file that has not been
162   /// accessed in this many seconds will be removed. The default value is
163   /// large, e.g., a month, to avoid forcing infrequently-used modules to be
164   /// regenerated often.
165   unsigned ModuleCachePruneAfter = 31 * 24 * 60 * 60;
166 
167   /// The time in seconds when the build session started.
168   ///
169   /// This time is used by other optimizations in header search and module
170   /// loading.
171   uint64_t BuildSessionTimestamp = 0;
172 
173   /// The set of macro names that should be ignored for the purposes
174   /// of computing the module hash.
175   llvm::SmallSetVector<llvm::CachedHashString, 16> ModulesIgnoreMacros;
176 
177   /// The set of user-provided virtual filesystem overlay files.
178   std::vector<std::string> VFSOverlayFiles;
179 
180   /// Include the compiler builtin includes.
181   unsigned UseBuiltinIncludes : 1;
182 
183   /// Include the system standard include search directories.
184   unsigned UseStandardSystemIncludes : 1;
185 
186   /// Include the system standard C++ library include search directories.
187   unsigned UseStandardCXXIncludes : 1;
188 
189   /// Use libc++ instead of the default libstdc++.
190   unsigned UseLibcxx : 1;
191 
192   /// Whether header search information should be output as for -v.
193   unsigned Verbose : 1;
194 
195   /// If true, skip verifying input files used by modules if the
196   /// module was already verified during this build session (see
197   /// \c BuildSessionTimestamp).
198   unsigned ModulesValidateOncePerBuildSession : 1;
199 
200   /// Whether to validate system input files when a module is loaded.
201   unsigned ModulesValidateSystemHeaders : 1;
202 
203   // Whether the content of input files should be hashed and used to
204   // validate consistency.
205   unsigned ValidateASTInputFilesContent : 1;
206 
207   /// Whether the module includes debug information (-gmodules).
208   unsigned UseDebugInfo : 1;
209 
210   unsigned ModulesValidateDiagnosticOptions : 1;
211 
212   unsigned ModulesHashContent : 1;
213 
214   /// Whether we should include all things that could impact the module in the
215   /// hash.
216   ///
217   /// This includes things like the full header search path, and enabled
218   /// diagnostics.
219   unsigned ModulesStrictContextHash : 1;
220 
221   HeaderSearchOptions(StringRef _Sysroot = "/")
222       : Sysroot(_Sysroot), ModuleFormat("raw"), DisableModuleHash(false),
223         ImplicitModuleMaps(false), ModuleMapFileHomeIsCwd(false),
224         EnablePrebuiltImplicitModules(false), UseBuiltinIncludes(true),
225         UseStandardSystemIncludes(true), UseStandardCXXIncludes(true),
226         UseLibcxx(false), Verbose(false),
227         ModulesValidateOncePerBuildSession(false),
228         ModulesValidateSystemHeaders(false),
229         ValidateASTInputFilesContent(false), UseDebugInfo(false),
230         ModulesValidateDiagnosticOptions(true), ModulesHashContent(false),
231         ModulesStrictContextHash(false) {}
232 
233   /// AddPath - Add the \p Path path to the specified \p Group list.
234   void AddPath(StringRef Path, frontend::IncludeDirGroup Group,
235                bool IsFramework, bool IgnoreSysRoot) {
236     UserEntries.emplace_back(Path, Group, IsFramework, IgnoreSysRoot);
237   }
238 
239   /// AddSystemHeaderPrefix - Override whether \#include directives naming a
240   /// path starting with \p Prefix should be considered as naming a system
241   /// header.
242   void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) {
243     SystemHeaderPrefixes.emplace_back(Prefix, IsSystemHeader);
244   }
245 
246   void AddVFSOverlayFile(StringRef Name) {
247     VFSOverlayFiles.push_back(std::string(Name));
248   }
249 
250   void AddPrebuiltModulePath(StringRef Name) {
251     PrebuiltModulePaths.push_back(std::string(Name));
252   }
253 };
254 
255 inline llvm::hash_code hash_value(const HeaderSearchOptions::Entry &E) {
256   return llvm::hash_combine(E.Path, E.Group, E.IsFramework, E.IgnoreSysRoot);
257 }
258 
259 inline llvm::hash_code
260 hash_value(const HeaderSearchOptions::SystemHeaderPrefix &SHP) {
261   return llvm::hash_combine(SHP.Prefix, SHP.IsSystemHeader);
262 }
263 
264 } // namespace clang
265 
266 #endif // LLVM_CLANG_LEX_HEADERSEARCHOPTIONS_H
267