1//===--- InitHeaderSearch.cpp - Initialize header search paths ------------===//
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// This file implements the InitHeaderSearch class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Basic/FileManager.h"
14#include "clang/Basic/LangOptions.h"
15#include "clang/Config/config.h" // C_INCLUDE_DIRS
16#include "clang/Lex/HeaderMap.h"
17#include "clang/Lex/HeaderSearch.h"
18#include "clang/Lex/HeaderSearchOptions.h"
19#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/ADT/Twine.h"
22#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/Path.h"
24#include "llvm/Support/raw_ostream.h"
25#include "llvm/TargetParser/Triple.h"
26#include <optional>
27
28using namespace clang;
29using namespace clang::frontend;
30
31namespace {
32/// Holds information about a single DirectoryLookup object.
33struct DirectoryLookupInfo {
34 IncludeDirGroup Group;
35 DirectoryLookup Lookup;
36 std::optional<unsigned> UserEntryIdx;
37
38 DirectoryLookupInfo(IncludeDirGroup Group, DirectoryLookup Lookup,
39 std::optional<unsigned> UserEntryIdx)
40 : Group(Group), Lookup(Lookup), UserEntryIdx(UserEntryIdx) {}
41};
42
43/// This class makes it easier to set the search paths of a HeaderSearch object.
44/// InitHeaderSearch stores several search path lists internally, which can be
45/// sent to a HeaderSearch object in one swoop.
46class InitHeaderSearch {
47 std::vector<DirectoryLookupInfo> IncludePath;
48 std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
49 HeaderSearch &Headers;
50 bool Verbose;
51 std::string IncludeSysroot;
52 bool HasSysroot;
53
54public:
55 InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot)
56 : Headers(HS), Verbose(verbose), IncludeSysroot(std::string(sysroot)),
57 HasSysroot(!(sysroot.empty() || sysroot == "/")) {}
58
59 /// Add the specified path to the specified group list, prefixing the sysroot
60 /// if used.
61 /// Returns true if the path exists, false if it was ignored.
62 bool AddPath(const Twine &Path, IncludeDirGroup Group, bool isFramework,
63 std::optional<unsigned> UserEntryIdx = std::nullopt);
64
65 /// Add the specified path to the specified group list, without performing any
66 /// sysroot remapping.
67 /// Returns true if the path exists, false if it was ignored.
68 bool AddUnmappedPath(const Twine &Path, IncludeDirGroup Group,
69 bool isFramework,
70 std::optional<unsigned> UserEntryIdx = std::nullopt);
71
72 /// Add the specified prefix to the system header prefix list.
73 void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) {
74 SystemHeaderPrefixes.emplace_back(args: std::string(Prefix), args&: IsSystemHeader);
75 }
76
77 /// Add paths that should always be searched.
78 void AddDefaultCIncludePaths(const llvm::Triple &triple,
79 const HeaderSearchOptions &HSOpts);
80
81 /// Returns true iff AddDefaultIncludePaths should do anything. If this
82 /// returns false, include paths should instead be handled in the driver.
83 bool ShouldAddDefaultIncludePaths(const llvm::Triple &triple);
84
85 /// Adds the default system include paths so that e.g. stdio.h is found.
86 void AddDefaultIncludePaths(const LangOptions &Lang,
87 const llvm::Triple &triple,
88 const HeaderSearchOptions &HSOpts);
89
90 /// Merges all search path lists into one list and send it to HeaderSearch.
91 void Realize(const LangOptions &Lang);
92};
93
94} // end anonymous namespace.
95
96static bool CanPrefixSysroot(StringRef Path) {
97#if defined(_WIN32)
98 return !Path.empty() && llvm::sys::path::is_separator(Path[0]);
99#else
100 return llvm::sys::path::is_absolute(path: Path);
101#endif
102}
103
104bool InitHeaderSearch::AddPath(const Twine &Path, IncludeDirGroup Group,
105 bool isFramework,
106 std::optional<unsigned> UserEntryIdx) {
107 // Add the path with sysroot prepended, if desired and this is a system header
108 // group.
109 if (HasSysroot) {
110 SmallString<256> MappedPathStorage;
111 StringRef MappedPathStr = Path.toStringRef(Out&: MappedPathStorage);
112 if (CanPrefixSysroot(Path: MappedPathStr)) {
113 return AddUnmappedPath(Path: IncludeSysroot + Path, Group, isFramework,
114 UserEntryIdx);
115 }
116 }
117
118 return AddUnmappedPath(Path, Group, isFramework, UserEntryIdx);
119}
120
121bool InitHeaderSearch::AddUnmappedPath(const Twine &Path, IncludeDirGroup Group,
122 bool isFramework,
123 std::optional<unsigned> UserEntryIdx) {
124 assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
125
126 FileManager &FM = Headers.getFileMgr();
127 SmallString<256> MappedPathStorage;
128 StringRef MappedPathStr = Path.toStringRef(Out&: MappedPathStorage);
129
130 // If use system headers while cross-compiling, emit the warning.
131 if (HasSysroot && (MappedPathStr.starts_with(Prefix: "/usr/include") ||
132 MappedPathStr.starts_with(Prefix: "/usr/local/include"))) {
133 Headers.getDiags().Report(DiagID: diag::warn_poison_system_directories)
134 << MappedPathStr;
135 }
136
137 // Compute the DirectoryLookup type.
138 SrcMgr::CharacteristicKind Type;
139 if (Group == Quoted || Group == Angled) {
140 Type = SrcMgr::C_User;
141 } else if (Group == ExternCSystem) {
142 Type = SrcMgr::C_ExternCSystem;
143 } else {
144 Type = SrcMgr::C_System;
145 }
146
147 // If the directory exists, add it.
148 if (auto DE = FM.getOptionalDirectoryRef(DirName: MappedPathStr)) {
149 IncludePath.emplace_back(args&: Group, args: DirectoryLookup(*DE, Type, isFramework),
150 args&: UserEntryIdx);
151 return true;
152 }
153
154 // Check to see if this is an apple-style headermap (which are not allowed to
155 // be frameworks).
156 if (!isFramework) {
157 if (auto FE = FM.getOptionalFileRef(Filename: MappedPathStr)) {
158 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE: *FE)) {
159 // It is a headermap, add it to the search path.
160 IncludePath.emplace_back(args&: Group, args: DirectoryLookup(HM, Type),
161 args&: UserEntryIdx);
162 return true;
163 }
164 }
165 }
166
167 if (Verbose)
168 llvm::errs() << "ignoring nonexistent directory \""
169 << MappedPathStr << "\"\n";
170 return false;
171}
172
173void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
174 const HeaderSearchOptions &HSOpts) {
175 if (!ShouldAddDefaultIncludePaths(triple))
176 llvm_unreachable("Include management is handled in the driver.");
177
178 if (HSOpts.UseStandardSystemIncludes) {
179 // FIXME: temporary hack: hard-coded paths.
180 AddPath(Path: "/usr/local/include", Group: System, isFramework: false);
181 }
182
183 // Builtin includes use #include_next directives and should be positioned
184 // just prior C include dirs.
185 if (HSOpts.UseBuiltinIncludes) {
186 // Ignore the sys root, we *always* look for clang headers relative to
187 // supplied path.
188 SmallString<128> P = StringRef(HSOpts.ResourceDir);
189 llvm::sys::path::append(path&: P, a: "include");
190 AddUnmappedPath(Path: P, Group: ExternCSystem, isFramework: false);
191 }
192
193 // All remaining additions are for system include directories, early exit if
194 // we aren't using them.
195 if (!HSOpts.UseStandardSystemIncludes)
196 return;
197
198 // Add dirs specified via 'configure --with-c-include-dirs'.
199 StringRef CIncludeDirs(C_INCLUDE_DIRS);
200 if (CIncludeDirs != "") {
201 SmallVector<StringRef, 5> dirs;
202 CIncludeDirs.split(A&: dirs, Separator: ":");
203 for (StringRef dir : dirs)
204 AddPath(Path: dir, Group: ExternCSystem, isFramework: false);
205 return;
206 }
207
208 AddPath(Path: "/usr/include", Group: ExternCSystem, isFramework: false);
209}
210
211bool InitHeaderSearch::ShouldAddDefaultIncludePaths(
212 const llvm::Triple &triple) {
213 switch (triple.getOS()) {
214 case llvm::Triple::AIX:
215 case llvm::Triple::DragonFly:
216 case llvm::Triple::ELFIAMCU:
217 case llvm::Triple::Emscripten:
218 case llvm::Triple::FreeBSD:
219 case llvm::Triple::Fuchsia:
220 case llvm::Triple::Haiku:
221 case llvm::Triple::Hurd:
222 case llvm::Triple::Linux:
223 case llvm::Triple::LiteOS:
224 case llvm::Triple::Managarm:
225 case llvm::Triple::NetBSD:
226 case llvm::Triple::OpenBSD:
227 case llvm::Triple::PS4:
228 case llvm::Triple::PS5:
229 case llvm::Triple::RTEMS:
230 case llvm::Triple::Serenity:
231 case llvm::Triple::Solaris:
232 case llvm::Triple::UEFI:
233 case llvm::Triple::WASI:
234 case llvm::Triple::WASIp1:
235 case llvm::Triple::WASIp2:
236 case llvm::Triple::WASIp3:
237 case llvm::Triple::Win32:
238 case llvm::Triple::ZOS:
239 return false;
240
241 case llvm::Triple::UnknownOS:
242 if (triple.isWasm() || triple.isAppleMachO())
243 return false;
244 break;
245
246 default:
247 break;
248 }
249
250 if (triple.isOSDarwin())
251 return false;
252
253 // On hexagon, include paths are managed by the driver.
254 if (triple.getArch() == llvm::Triple::hexagon)
255 return false;
256
257 return true; // Everything else uses AddDefaultIncludePaths().
258}
259
260void InitHeaderSearch::AddDefaultIncludePaths(
261 const LangOptions &Lang, const llvm::Triple &triple,
262 const HeaderSearchOptions &HSOpts) {
263 // NB: This code path is going away. All of the logic is moving into the
264 // driver which has the information necessary to do target-specific
265 // selections of default include paths. Each target which moves there will be
266 // exempted from this logic in ShouldAddDefaultIncludePaths() until we can
267 // delete the entire pile of code.
268 if (!ShouldAddDefaultIncludePaths(triple))
269 return;
270
271 if (Lang.CPlusPlus && !Lang.AsmPreprocessor &&
272 HSOpts.UseStandardCXXIncludes && HSOpts.UseStandardSystemIncludes) {
273 if (HSOpts.UseLibcxx) {
274 AddPath(Path: "/usr/include/c++/v1", Group: CXXSystem, isFramework: false);
275 }
276 }
277
278 AddDefaultCIncludePaths(triple, HSOpts);
279}
280
281/// If there are duplicate directory entries in the specified search list,
282/// remove the later (dead) ones. Returns the number of non-system headers
283/// removed, which is used to update NumAngled.
284static unsigned RemoveDuplicates(std::vector<DirectoryLookupInfo> &SearchList,
285 unsigned First, bool Verbose) {
286 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
287 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
288 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
289 unsigned NonSystemRemoved = 0;
290 for (unsigned i = First; i != SearchList.size(); ++i) {
291 unsigned DirToRemove = i;
292
293 const DirectoryLookup &CurEntry = SearchList[i].Lookup;
294
295 if (CurEntry.isNormalDir()) {
296 // If this isn't the first time we've seen this dir, remove it.
297 if (SeenDirs.insert(Ptr: CurEntry.getDir()).second)
298 continue;
299 } else if (CurEntry.isFramework()) {
300 // If this isn't the first time we've seen this framework dir, remove it.
301 if (SeenFrameworkDirs.insert(Ptr: CurEntry.getFrameworkDir()).second)
302 continue;
303 } else {
304 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
305 // If this isn't the first time we've seen this headermap, remove it.
306 if (SeenHeaderMaps.insert(Ptr: CurEntry.getHeaderMap()).second)
307 continue;
308 }
309
310 // If we have a normal #include dir/framework/headermap that is shadowed
311 // later in the chain by a system include location, we actually want to
312 // ignore the user's request and drop the user dir... keeping the system
313 // dir. This is weird, but required to emulate GCC's search path correctly.
314 //
315 // Since dupes of system dirs are rare, just rescan to find the original
316 // that we're nuking instead of using a DenseMap.
317 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
318 // Find the dir that this is the same of.
319 unsigned FirstDir;
320 for (FirstDir = First;; ++FirstDir) {
321 assert(FirstDir != i && "Didn't find dupe?");
322
323 const DirectoryLookup &SearchEntry = SearchList[FirstDir].Lookup;
324
325 // If these are different lookup types, then they can't be the dupe.
326 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
327 continue;
328
329 bool isSame;
330 if (CurEntry.isNormalDir())
331 isSame = SearchEntry.getDir() == CurEntry.getDir();
332 else if (CurEntry.isFramework())
333 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
334 else {
335 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
336 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
337 }
338
339 if (isSame)
340 break;
341 }
342
343 // If the first dir in the search path is a non-system dir, zap it
344 // instead of the system one.
345 if (SearchList[FirstDir].Lookup.getDirCharacteristic() == SrcMgr::C_User)
346 DirToRemove = FirstDir;
347 }
348
349 if (Verbose) {
350 llvm::errs() << "ignoring duplicate directory \""
351 << CurEntry.getName() << "\"\n";
352 if (DirToRemove != i)
353 llvm::errs() << " as it is a non-system directory that duplicates "
354 << "a system directory\n";
355 }
356 if (DirToRemove != i)
357 ++NonSystemRemoved;
358
359 // This is reached if the current entry is a duplicate. Remove the
360 // DirToRemove (usually the current dir).
361 SearchList.erase(position: SearchList.begin()+DirToRemove);
362 --i;
363 }
364 return NonSystemRemoved;
365}
366
367/// Extract DirectoryLookups from DirectoryLookupInfos.
368static std::vector<DirectoryLookup>
369extractLookups(const std::vector<DirectoryLookupInfo> &Infos) {
370 std::vector<DirectoryLookup> Lookups;
371 Lookups.reserve(n: Infos.size());
372 llvm::transform(Range: Infos, d_first: std::back_inserter(x&: Lookups),
373 F: [](const DirectoryLookupInfo &Info) { return Info.Lookup; });
374 return Lookups;
375}
376
377/// Collect the mapping between indices of DirectoryLookups and UserEntries.
378static llvm::DenseMap<unsigned, unsigned>
379mapToUserEntries(const std::vector<DirectoryLookupInfo> &Infos) {
380 llvm::DenseMap<unsigned, unsigned> LookupsToUserEntries;
381 for (unsigned I = 0, E = Infos.size(); I < E; ++I) {
382 // Check whether this DirectoryLookup maps to a HeaderSearch::UserEntry.
383 if (Infos[I].UserEntryIdx)
384 LookupsToUserEntries.insert(KV: {I, *Infos[I].UserEntryIdx});
385 }
386 return LookupsToUserEntries;
387}
388
389void InitHeaderSearch::Realize(const LangOptions &Lang) {
390 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
391 std::vector<DirectoryLookupInfo> SearchList;
392 SearchList.reserve(n: IncludePath.size());
393
394 // Quoted arguments go first.
395 for (auto &Include : IncludePath)
396 if (Include.Group == Quoted)
397 SearchList.push_back(x: Include);
398
399 // Deduplicate and remember index.
400 RemoveDuplicates(SearchList, First: 0, Verbose);
401 unsigned NumQuoted = SearchList.size();
402
403 for (auto &Include : IncludePath)
404 if (Include.Group == Angled)
405 SearchList.push_back(x: Include);
406
407 RemoveDuplicates(SearchList, First: NumQuoted, Verbose);
408 unsigned NumAngled = SearchList.size();
409
410 for (auto &Include : IncludePath)
411 if (Include.Group == System || Include.Group == ExternCSystem ||
412 (!Lang.ObjC && !Lang.CPlusPlus && Include.Group == CSystem) ||
413 (/*FIXME !Lang.ObjC && */ Lang.CPlusPlus &&
414 Include.Group == CXXSystem) ||
415 (Lang.ObjC && !Lang.CPlusPlus && Include.Group == ObjCSystem) ||
416 (Lang.ObjC && Lang.CPlusPlus && Include.Group == ObjCXXSystem))
417 SearchList.push_back(x: Include);
418
419 for (auto &Include : IncludePath)
420 if (Include.Group == After)
421 SearchList.push_back(x: Include);
422
423 // Remove duplicates across both the Angled and System directories. GCC does
424 // this and failing to remove duplicates across these two groups breaks
425 // #include_next.
426 unsigned NonSystemRemoved = RemoveDuplicates(SearchList, First: NumQuoted, Verbose);
427 NumAngled -= NonSystemRemoved;
428
429 Headers.SetSearchPaths(dirs: extractLookups(Infos: SearchList), angledDirIdx: NumQuoted, systemDirIdx: NumAngled,
430 searchDirToHSEntry: mapToUserEntries(Infos: SearchList));
431
432 Headers.SetSystemHeaderPrefixes(SystemHeaderPrefixes);
433
434 // If verbose, print the list of directories that will be searched.
435 if (Verbose) {
436 llvm::errs() << "#include \"...\" search starts here:\n";
437 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
438 if (i == NumQuoted)
439 llvm::errs() << "#include <...> search starts here:\n";
440 StringRef Name = SearchList[i].Lookup.getName();
441 const char *Suffix;
442 if (SearchList[i].Lookup.isNormalDir())
443 Suffix = "";
444 else if (SearchList[i].Lookup.isFramework())
445 Suffix = " (framework directory)";
446 else {
447 assert(SearchList[i].Lookup.isHeaderMap() && "Unknown DirectoryLookup");
448 Suffix = " (headermap)";
449 }
450 llvm::errs() << " " << Name << Suffix << "\n";
451 }
452 llvm::errs() << "End of search list.\n";
453 }
454}
455
456void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
457 const HeaderSearchOptions &HSOpts,
458 const LangOptions &Lang,
459 const llvm::Triple &Triple) {
460 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
461
462 // Add the user defined entries.
463 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
464 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
465 if (E.IgnoreSysRoot) {
466 Init.AddUnmappedPath(Path: E.Path, Group: E.Group, isFramework: E.IsFramework, UserEntryIdx: i);
467 } else {
468 Init.AddPath(Path: E.Path, Group: E.Group, isFramework: E.IsFramework, UserEntryIdx: i);
469 }
470 }
471
472 Init.AddDefaultIncludePaths(Lang, triple: Triple, HSOpts);
473
474 for (unsigned i = 0, e = HSOpts.SystemHeaderPrefixes.size(); i != e; ++i)
475 Init.AddSystemHeaderPrefix(Prefix: HSOpts.SystemHeaderPrefixes[i].Prefix,
476 IsSystemHeader: HSOpts.SystemHeaderPrefixes[i].IsSystemHeader);
477
478 if (HSOpts.UseBuiltinIncludes) {
479 // Set up the builtin include directory in the module map.
480 SmallString<128> P = StringRef(HSOpts.ResourceDir);
481 llvm::sys::path::append(path&: P, a: "include");
482 if (auto Dir = HS.getFileMgr().getOptionalDirectoryRef(DirName: P))
483 HS.getModuleMap().setBuiltinIncludeDir(*Dir);
484 }
485
486 Init.Realize(Lang);
487}
488