1 //===--- InitHeaderSearch.cpp - Initialize header search paths ------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the InitHeaderSearch class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Basic/FileManager.h"
15 #include "clang/Basic/LangOptions.h"
16 #include "clang/Config/config.h" // C_INCLUDE_DIRS
17 #include "clang/Frontend/FrontendDiagnostic.h"
18 #include "clang/Frontend/Utils.h"
19 #include "clang/Lex/HeaderMap.h"
20 #include "clang/Lex/HeaderSearch.h"
21 #include "clang/Lex/HeaderSearchOptions.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallString.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/ADT/Triple.h"
27 #include "llvm/ADT/Twine.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/raw_ostream.h"
31
32 using namespace clang;
33 using namespace clang::frontend;
34
35 namespace {
36
37 /// InitHeaderSearch - This class makes it easier to set the search paths of
38 /// a HeaderSearch object. InitHeaderSearch stores several search path lists
39 /// internally, which can be sent to a HeaderSearch object in one swoop.
40 class InitHeaderSearch {
41 std::vector<std::pair<IncludeDirGroup, DirectoryLookup> > IncludePath;
42 typedef std::vector<std::pair<IncludeDirGroup,
43 DirectoryLookup> >::const_iterator path_iterator;
44 std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
45 HeaderSearch &Headers;
46 bool Verbose;
47 std::string IncludeSysroot;
48 bool HasSysroot;
49
50 public:
51
InitHeaderSearch(HeaderSearch & HS,bool verbose,StringRef sysroot)52 InitHeaderSearch(HeaderSearch &HS, bool verbose, StringRef sysroot)
53 : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot),
54 HasSysroot(!(sysroot.empty() || sysroot == "/")) {
55 }
56
57 /// AddPath - Add the specified path to the specified group list, prefixing
58 /// the sysroot if used.
59 /// Returns true if the path exists, false if it was ignored.
60 bool AddPath(const Twine &Path, IncludeDirGroup Group, bool isFramework);
61
62 /// AddUnmappedPath - Add the specified path to the specified group list,
63 /// without performing any sysroot remapping.
64 /// Returns true if the path exists, false if it was ignored.
65 bool AddUnmappedPath(const Twine &Path, IncludeDirGroup Group,
66 bool isFramework);
67
68 /// AddSystemHeaderPrefix - Add the specified prefix to the system header
69 /// prefix list.
AddSystemHeaderPrefix(StringRef Prefix,bool IsSystemHeader)70 void AddSystemHeaderPrefix(StringRef Prefix, bool IsSystemHeader) {
71 SystemHeaderPrefixes.emplace_back(Prefix, IsSystemHeader);
72 }
73
74 /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
75 /// libstdc++.
76 /// Returns true if the \p Base path was found, false if it does not exist.
77 bool AddGnuCPlusPlusIncludePaths(StringRef Base, StringRef ArchDir,
78 StringRef Dir32, StringRef Dir64,
79 const llvm::Triple &triple);
80
81 /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW
82 /// libstdc++.
83 void AddMinGWCPlusPlusIncludePaths(StringRef Base,
84 StringRef Arch,
85 StringRef Version);
86
87 // AddDefaultCIncludePaths - Add paths that should always be searched.
88 void AddDefaultCIncludePaths(const llvm::Triple &triple,
89 const HeaderSearchOptions &HSOpts);
90
91 // AddDefaultCPlusPlusIncludePaths - Add paths that should be searched when
92 // compiling c++.
93 void AddDefaultCPlusPlusIncludePaths(const LangOptions &LangOpts,
94 const llvm::Triple &triple,
95 const HeaderSearchOptions &HSOpts);
96
97 /// AddDefaultSystemIncludePaths - Adds the default system include paths so
98 /// that e.g. stdio.h is found.
99 void AddDefaultIncludePaths(const LangOptions &Lang,
100 const llvm::Triple &triple,
101 const HeaderSearchOptions &HSOpts);
102
103 /// Realize - Merges all search path lists into one list and send it to
104 /// HeaderSearch.
105 void Realize(const LangOptions &Lang);
106 };
107
108 } // end anonymous namespace.
109
CanPrefixSysroot(StringRef Path)110 static bool CanPrefixSysroot(StringRef Path) {
111 #if defined(_WIN32)
112 return !Path.empty() && llvm::sys::path::is_separator(Path[0]);
113 #else
114 return llvm::sys::path::is_absolute(Path);
115 #endif
116 }
117
AddPath(const Twine & Path,IncludeDirGroup Group,bool isFramework)118 bool InitHeaderSearch::AddPath(const Twine &Path, IncludeDirGroup Group,
119 bool isFramework) {
120 // Add the path with sysroot prepended, if desired and this is a system header
121 // group.
122 if (HasSysroot) {
123 SmallString<256> MappedPathStorage;
124 StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
125 if (CanPrefixSysroot(MappedPathStr)) {
126 return AddUnmappedPath(IncludeSysroot + Path, Group, isFramework);
127 }
128 }
129
130 return AddUnmappedPath(Path, Group, isFramework);
131 }
132
AddUnmappedPath(const Twine & Path,IncludeDirGroup Group,bool isFramework)133 bool InitHeaderSearch::AddUnmappedPath(const Twine &Path, IncludeDirGroup Group,
134 bool isFramework) {
135 assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
136
137 FileManager &FM = Headers.getFileMgr();
138 SmallString<256> MappedPathStorage;
139 StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
140
141 // Compute the DirectoryLookup type.
142 SrcMgr::CharacteristicKind Type;
143 if (Group == Quoted || Group == Angled || Group == IndexHeaderMap) {
144 Type = SrcMgr::C_User;
145 } else if (Group == ExternCSystem) {
146 Type = SrcMgr::C_ExternCSystem;
147 } else {
148 Type = SrcMgr::C_System;
149 }
150
151 // If the directory exists, add it.
152 if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
153 IncludePath.push_back(
154 std::make_pair(Group, DirectoryLookup(DE, Type, isFramework)));
155 return true;
156 }
157
158 // Check to see if this is an apple-style headermap (which are not allowed to
159 // be frameworks).
160 if (!isFramework) {
161 if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
162 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
163 // It is a headermap, add it to the search path.
164 IncludePath.push_back(
165 std::make_pair(Group,
166 DirectoryLookup(HM, Type, Group == IndexHeaderMap)));
167 return true;
168 }
169 }
170 }
171
172 if (Verbose)
173 llvm::errs() << "ignoring nonexistent directory \""
174 << MappedPathStr << "\"\n";
175 return false;
176 }
177
AddGnuCPlusPlusIncludePaths(StringRef Base,StringRef ArchDir,StringRef Dir32,StringRef Dir64,const llvm::Triple & triple)178 bool InitHeaderSearch::AddGnuCPlusPlusIncludePaths(StringRef Base,
179 StringRef ArchDir,
180 StringRef Dir32,
181 StringRef Dir64,
182 const llvm::Triple &triple) {
183 // Add the base dir
184 bool IsBaseFound = AddPath(Base, CXXSystem, false);
185
186 // Add the multilib dirs
187 llvm::Triple::ArchType arch = triple.getArch();
188 bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
189 if (is64bit)
190 AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, false);
191 else
192 AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, false);
193
194 // Add the backward dir
195 AddPath(Base + "/backward", CXXSystem, false);
196 return IsBaseFound;
197 }
198
AddMinGWCPlusPlusIncludePaths(StringRef Base,StringRef Arch,StringRef Version)199 void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(StringRef Base,
200 StringRef Arch,
201 StringRef Version) {
202 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
203 CXXSystem, false);
204 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
205 CXXSystem, false);
206 AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
207 CXXSystem, false);
208 }
209
AddDefaultCIncludePaths(const llvm::Triple & triple,const HeaderSearchOptions & HSOpts)210 void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
211 const HeaderSearchOptions &HSOpts) {
212 llvm::Triple::OSType os = triple.getOS();
213
214 if (HSOpts.UseStandardSystemIncludes) {
215 switch (os) {
216 case llvm::Triple::CloudABI:
217 case llvm::Triple::FreeBSD:
218 case llvm::Triple::NetBSD:
219 case llvm::Triple::OpenBSD:
220 case llvm::Triple::NaCl:
221 case llvm::Triple::PS4:
222 case llvm::Triple::ELFIAMCU:
223 case llvm::Triple::Fuchsia:
224 break;
225 case llvm::Triple::Win32:
226 if (triple.getEnvironment() != llvm::Triple::Cygnus)
227 break;
228 LLVM_FALLTHROUGH;
229 default:
230 // FIXME: temporary hack: hard-coded paths.
231 AddPath("/usr/local/include", System, false);
232 break;
233 }
234 }
235
236 // Builtin includes use #include_next directives and should be positioned
237 // just prior C include dirs.
238 if (HSOpts.UseBuiltinIncludes) {
239 // Ignore the sys root, we *always* look for clang headers relative to
240 // supplied path.
241 SmallString<128> P = StringRef(HSOpts.ResourceDir);
242 llvm::sys::path::append(P, "include");
243 AddUnmappedPath(P, ExternCSystem, false);
244 }
245
246 // All remaining additions are for system include directories, early exit if
247 // we aren't using them.
248 if (!HSOpts.UseStandardSystemIncludes)
249 return;
250
251 // Add dirs specified via 'configure --with-c-include-dirs'.
252 StringRef CIncludeDirs(C_INCLUDE_DIRS);
253 if (CIncludeDirs != "") {
254 SmallVector<StringRef, 5> dirs;
255 CIncludeDirs.split(dirs, ":");
256 for (StringRef dir : dirs)
257 AddPath(dir, ExternCSystem, false);
258 return;
259 }
260
261 switch (os) {
262 case llvm::Triple::Linux:
263 case llvm::Triple::Hurd:
264 case llvm::Triple::Solaris:
265 llvm_unreachable("Include management is handled in the driver.");
266
267 case llvm::Triple::CloudABI: {
268 // <sysroot>/<triple>/include
269 SmallString<128> P = StringRef(HSOpts.ResourceDir);
270 llvm::sys::path::append(P, "../../..", triple.str(), "include");
271 AddPath(P, System, false);
272 break;
273 }
274
275 case llvm::Triple::Haiku:
276 AddPath("/boot/system/non-packaged/develop/headers", System, false);
277 AddPath("/boot/system/develop/headers/os", System, false);
278 AddPath("/boot/system/develop/headers/os/app", System, false);
279 AddPath("/boot/system/develop/headers/os/arch", System, false);
280 AddPath("/boot/system/develop/headers/os/device", System, false);
281 AddPath("/boot/system/develop/headers/os/drivers", System, false);
282 AddPath("/boot/system/develop/headers/os/game", System, false);
283 AddPath("/boot/system/develop/headers/os/interface", System, false);
284 AddPath("/boot/system/develop/headers/os/kernel", System, false);
285 AddPath("/boot/system/develop/headers/os/locale", System, false);
286 AddPath("/boot/system/develop/headers/os/mail", System, false);
287 AddPath("/boot/system/develop/headers/os/media", System, false);
288 AddPath("/boot/system/develop/headers/os/midi", System, false);
289 AddPath("/boot/system/develop/headers/os/midi2", System, false);
290 AddPath("/boot/system/develop/headers/os/net", System, false);
291 AddPath("/boot/system/develop/headers/os/opengl", System, false);
292 AddPath("/boot/system/develop/headers/os/storage", System, false);
293 AddPath("/boot/system/develop/headers/os/support", System, false);
294 AddPath("/boot/system/develop/headers/os/translation", System, false);
295 AddPath("/boot/system/develop/headers/os/add-ons/graphics", System, false);
296 AddPath("/boot/system/develop/headers/os/add-ons/input_server", System, false);
297 AddPath("/boot/system/develop/headers/os/add-ons/mail_daemon", System, false);
298 AddPath("/boot/system/develop/headers/os/add-ons/registrar", System, false);
299 AddPath("/boot/system/develop/headers/os/add-ons/screen_saver", System, false);
300 AddPath("/boot/system/develop/headers/os/add-ons/tracker", System, false);
301 AddPath("/boot/system/develop/headers/os/be_apps/Deskbar", System, false);
302 AddPath("/boot/system/develop/headers/os/be_apps/NetPositive", System, false);
303 AddPath("/boot/system/develop/headers/os/be_apps/Tracker", System, false);
304 AddPath("/boot/system/develop/headers/3rdparty", System, false);
305 AddPath("/boot/system/develop/headers/bsd", System, false);
306 AddPath("/boot/system/develop/headers/glibc", System, false);
307 AddPath("/boot/system/develop/headers/posix", System, false);
308 AddPath("/boot/system/develop/headers", System, false);
309 break;
310 case llvm::Triple::RTEMS:
311 break;
312 case llvm::Triple::Win32:
313 switch (triple.getEnvironment()) {
314 default: llvm_unreachable("Include management is handled in the driver.");
315 case llvm::Triple::Cygnus:
316 AddPath("/usr/include/w32api", System, false);
317 break;
318 case llvm::Triple::GNU:
319 break;
320 }
321 break;
322 default:
323 break;
324 }
325
326 switch (os) {
327 case llvm::Triple::CloudABI:
328 case llvm::Triple::RTEMS:
329 case llvm::Triple::NaCl:
330 case llvm::Triple::ELFIAMCU:
331 case llvm::Triple::Fuchsia:
332 break;
333 case llvm::Triple::PS4: {
334 // <isysroot> gets prepended later in AddPath().
335 std::string BaseSDKPath = "";
336 if (!HasSysroot) {
337 const char *envValue = getenv("SCE_ORBIS_SDK_DIR");
338 if (envValue)
339 BaseSDKPath = envValue;
340 else {
341 // HSOpts.ResourceDir variable contains the location of Clang's
342 // resource files.
343 // Assuming that Clang is configured for PS4 without
344 // --with-clang-resource-dir option, the location of Clang's resource
345 // files is <SDK_DIR>/host_tools/lib/clang
346 SmallString<128> P = StringRef(HSOpts.ResourceDir);
347 llvm::sys::path::append(P, "../../..");
348 BaseSDKPath = P.str();
349 }
350 }
351 AddPath(BaseSDKPath + "/target/include", System, false);
352 if (triple.isPS4CPU())
353 AddPath(BaseSDKPath + "/target/include_common", System, false);
354 LLVM_FALLTHROUGH;
355 }
356 default:
357 AddPath("/usr/include", ExternCSystem, false);
358 break;
359 }
360 }
361
AddDefaultCPlusPlusIncludePaths(const LangOptions & LangOpts,const llvm::Triple & triple,const HeaderSearchOptions & HSOpts)362 void InitHeaderSearch::AddDefaultCPlusPlusIncludePaths(
363 const LangOptions &LangOpts, const llvm::Triple &triple,
364 const HeaderSearchOptions &HSOpts) {
365 llvm::Triple::OSType os = triple.getOS();
366 // FIXME: temporary hack: hard-coded paths.
367
368 if (triple.isOSDarwin()) {
369 bool IsBaseFound = true;
370 switch (triple.getArch()) {
371 default: break;
372
373 case llvm::Triple::ppc:
374 case llvm::Triple::ppc64:
375 IsBaseFound = AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
376 "powerpc-apple-darwin10", "",
377 "ppc64", triple);
378 IsBaseFound |= AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
379 "powerpc-apple-darwin10", "",
380 "ppc64", triple);
381 break;
382
383 case llvm::Triple::x86:
384 case llvm::Triple::x86_64:
385 IsBaseFound = AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
386 "i686-apple-darwin10", "",
387 "x86_64", triple);
388 IsBaseFound |= AddGnuCPlusPlusIncludePaths(
389 "/usr/include/c++/4.0.0", "i686-apple-darwin8", "", "", triple);
390 break;
391
392 case llvm::Triple::arm:
393 case llvm::Triple::thumb:
394 IsBaseFound = AddGnuCPlusPlusIncludePaths(
395 "/usr/include/c++/4.2.1", "arm-apple-darwin10", "v7", "", triple);
396 IsBaseFound |= AddGnuCPlusPlusIncludePaths(
397 "/usr/include/c++/4.2.1", "arm-apple-darwin10", "v6", "", triple);
398 break;
399
400 case llvm::Triple::aarch64:
401 IsBaseFound = AddGnuCPlusPlusIncludePaths(
402 "/usr/include/c++/4.2.1", "arm64-apple-darwin10", "", "", triple);
403 break;
404 }
405 // Warn when compiling pure C++ / Objective-C++ only.
406 if (!IsBaseFound &&
407 !(LangOpts.CUDA || LangOpts.OpenCL || LangOpts.RenderScript)) {
408 Headers.getDiags().Report(SourceLocation(),
409 diag::warn_stdlibcxx_not_found);
410 }
411 return;
412 }
413
414 switch (os) {
415 case llvm::Triple::Linux:
416 case llvm::Triple::Hurd:
417 case llvm::Triple::Solaris:
418 llvm_unreachable("Include management is handled in the driver.");
419 break;
420 case llvm::Triple::Win32:
421 switch (triple.getEnvironment()) {
422 default: llvm_unreachable("Include management is handled in the driver.");
423 case llvm::Triple::Cygnus:
424 // Cygwin-1.7
425 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.7.3");
426 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.5.3");
427 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
428 // g++-4 / Cygwin-1.5
429 AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
430 break;
431 }
432 break;
433 case llvm::Triple::DragonFly:
434 AddPath("/usr/include/c++/5.0", CXXSystem, false);
435 break;
436 case llvm::Triple::Minix:
437 AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
438 "", "", "", triple);
439 break;
440 default:
441 break;
442 }
443 }
444
AddDefaultIncludePaths(const LangOptions & Lang,const llvm::Triple & triple,const HeaderSearchOptions & HSOpts)445 void InitHeaderSearch::AddDefaultIncludePaths(const LangOptions &Lang,
446 const llvm::Triple &triple,
447 const HeaderSearchOptions &HSOpts) {
448 // NB: This code path is going away. All of the logic is moving into the
449 // driver which has the information necessary to do target-specific
450 // selections of default include paths. Each target which moves there will be
451 // exempted from this logic here until we can delete the entire pile of code.
452 switch (triple.getOS()) {
453 default:
454 break; // Everything else continues to use this routine's logic.
455
456 case llvm::Triple::Linux:
457 case llvm::Triple::Hurd:
458 case llvm::Triple::Solaris:
459 return;
460
461 case llvm::Triple::Win32:
462 if (triple.getEnvironment() != llvm::Triple::Cygnus ||
463 triple.isOSBinFormatMachO())
464 return;
465 break;
466 }
467
468 if (Lang.CPlusPlus && !Lang.AsmPreprocessor &&
469 HSOpts.UseStandardCXXIncludes && HSOpts.UseStandardSystemIncludes) {
470 if (HSOpts.UseLibcxx) {
471 AddPath("/usr/include/c++/v1", CXXSystem, false);
472 } else {
473 AddDefaultCPlusPlusIncludePaths(Lang, triple, HSOpts);
474 }
475 }
476
477 AddDefaultCIncludePaths(triple, HSOpts);
478
479 // Add the default framework include paths on Darwin.
480 if (HSOpts.UseStandardSystemIncludes) {
481 if (triple.isOSDarwin()) {
482 AddPath("/System/Library/Frameworks", System, true);
483 AddPath("/Library/Frameworks", System, true);
484 }
485 }
486 }
487
488 /// RemoveDuplicates - If there are duplicate directory entries in the specified
489 /// search list, remove the later (dead) ones. Returns the number of non-system
490 /// headers removed, which is used to update NumAngled.
RemoveDuplicates(std::vector<DirectoryLookup> & SearchList,unsigned First,bool Verbose)491 static unsigned RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
492 unsigned First, bool Verbose) {
493 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
494 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
495 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
496 unsigned NonSystemRemoved = 0;
497 for (unsigned i = First; i != SearchList.size(); ++i) {
498 unsigned DirToRemove = i;
499
500 const DirectoryLookup &CurEntry = SearchList[i];
501
502 if (CurEntry.isNormalDir()) {
503 // If this isn't the first time we've seen this dir, remove it.
504 if (SeenDirs.insert(CurEntry.getDir()).second)
505 continue;
506 } else if (CurEntry.isFramework()) {
507 // If this isn't the first time we've seen this framework dir, remove it.
508 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()).second)
509 continue;
510 } else {
511 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
512 // If this isn't the first time we've seen this headermap, remove it.
513 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()).second)
514 continue;
515 }
516
517 // If we have a normal #include dir/framework/headermap that is shadowed
518 // later in the chain by a system include location, we actually want to
519 // ignore the user's request and drop the user dir... keeping the system
520 // dir. This is weird, but required to emulate GCC's search path correctly.
521 //
522 // Since dupes of system dirs are rare, just rescan to find the original
523 // that we're nuking instead of using a DenseMap.
524 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
525 // Find the dir that this is the same of.
526 unsigned FirstDir;
527 for (FirstDir = First;; ++FirstDir) {
528 assert(FirstDir != i && "Didn't find dupe?");
529
530 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
531
532 // If these are different lookup types, then they can't be the dupe.
533 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
534 continue;
535
536 bool isSame;
537 if (CurEntry.isNormalDir())
538 isSame = SearchEntry.getDir() == CurEntry.getDir();
539 else if (CurEntry.isFramework())
540 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
541 else {
542 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
543 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
544 }
545
546 if (isSame)
547 break;
548 }
549
550 // If the first dir in the search path is a non-system dir, zap it
551 // instead of the system one.
552 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
553 DirToRemove = FirstDir;
554 }
555
556 if (Verbose) {
557 llvm::errs() << "ignoring duplicate directory \""
558 << CurEntry.getName() << "\"\n";
559 if (DirToRemove != i)
560 llvm::errs() << " as it is a non-system directory that duplicates "
561 << "a system directory\n";
562 }
563 if (DirToRemove != i)
564 ++NonSystemRemoved;
565
566 // This is reached if the current entry is a duplicate. Remove the
567 // DirToRemove (usually the current dir).
568 SearchList.erase(SearchList.begin()+DirToRemove);
569 --i;
570 }
571 return NonSystemRemoved;
572 }
573
574
Realize(const LangOptions & Lang)575 void InitHeaderSearch::Realize(const LangOptions &Lang) {
576 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
577 std::vector<DirectoryLookup> SearchList;
578 SearchList.reserve(IncludePath.size());
579
580 // Quoted arguments go first.
581 for (auto &Include : IncludePath)
582 if (Include.first == Quoted)
583 SearchList.push_back(Include.second);
584
585 // Deduplicate and remember index.
586 RemoveDuplicates(SearchList, 0, Verbose);
587 unsigned NumQuoted = SearchList.size();
588
589 for (auto &Include : IncludePath)
590 if (Include.first == Angled || Include.first == IndexHeaderMap)
591 SearchList.push_back(Include.second);
592
593 RemoveDuplicates(SearchList, NumQuoted, Verbose);
594 unsigned NumAngled = SearchList.size();
595
596 for (auto &Include : IncludePath)
597 if (Include.first == System || Include.first == ExternCSystem ||
598 (!Lang.ObjC && !Lang.CPlusPlus && Include.first == CSystem) ||
599 (/*FIXME !Lang.ObjC && */ Lang.CPlusPlus &&
600 Include.first == CXXSystem) ||
601 (Lang.ObjC && !Lang.CPlusPlus && Include.first == ObjCSystem) ||
602 (Lang.ObjC && Lang.CPlusPlus && Include.first == ObjCXXSystem))
603 SearchList.push_back(Include.second);
604
605 for (auto &Include : IncludePath)
606 if (Include.first == After)
607 SearchList.push_back(Include.second);
608
609 // Remove duplicates across both the Angled and System directories. GCC does
610 // this and failing to remove duplicates across these two groups breaks
611 // #include_next.
612 unsigned NonSystemRemoved = RemoveDuplicates(SearchList, NumQuoted, Verbose);
613 NumAngled -= NonSystemRemoved;
614
615 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
616 Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
617
618 Headers.SetSystemHeaderPrefixes(SystemHeaderPrefixes);
619
620 // If verbose, print the list of directories that will be searched.
621 if (Verbose) {
622 llvm::errs() << "#include \"...\" search starts here:\n";
623 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
624 if (i == NumQuoted)
625 llvm::errs() << "#include <...> search starts here:\n";
626 StringRef Name = SearchList[i].getName();
627 const char *Suffix;
628 if (SearchList[i].isNormalDir())
629 Suffix = "";
630 else if (SearchList[i].isFramework())
631 Suffix = " (framework directory)";
632 else {
633 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
634 Suffix = " (headermap)";
635 }
636 llvm::errs() << " " << Name << Suffix << "\n";
637 }
638 llvm::errs() << "End of search list.\n";
639 }
640 }
641
ApplyHeaderSearchOptions(HeaderSearch & HS,const HeaderSearchOptions & HSOpts,const LangOptions & Lang,const llvm::Triple & Triple)642 void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
643 const HeaderSearchOptions &HSOpts,
644 const LangOptions &Lang,
645 const llvm::Triple &Triple) {
646 InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
647
648 // Add the user defined entries.
649 for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
650 const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
651 if (E.IgnoreSysRoot) {
652 Init.AddUnmappedPath(E.Path, E.Group, E.IsFramework);
653 } else {
654 Init.AddPath(E.Path, E.Group, E.IsFramework);
655 }
656 }
657
658 Init.AddDefaultIncludePaths(Lang, Triple, HSOpts);
659
660 for (unsigned i = 0, e = HSOpts.SystemHeaderPrefixes.size(); i != e; ++i)
661 Init.AddSystemHeaderPrefix(HSOpts.SystemHeaderPrefixes[i].Prefix,
662 HSOpts.SystemHeaderPrefixes[i].IsSystemHeader);
663
664 if (HSOpts.UseBuiltinIncludes) {
665 // Set up the builtin include directory in the module map.
666 SmallString<128> P = StringRef(HSOpts.ResourceDir);
667 llvm::sys::path::append(P, "include");
668 if (const DirectoryEntry *Dir = HS.getFileMgr().getDirectory(P))
669 HS.getModuleMap().setBuiltinIncludeDir(Dir);
670 }
671
672 Init.Realize(Lang);
673 }
674