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