]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/lib/Frontend/InitHeaderSearch.cpp
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.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 #ifdef HAVE_CLANG_CONFIG_H
15 # include "clang/Config/config.h"
16 #endif
17
18 #include "clang/Frontend/Utils.h"
19 #include "clang/Basic/FileManager.h"
20 #include "clang/Basic/LangOptions.h"
21 #include "clang/Basic/Version.h"
22 #include "clang/Frontend/HeaderSearchOptions.h"
23 #include "clang/Lex/HeaderSearch.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/ADT/Triple.h"
29 #include "llvm/ADT/Twine.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Support/Path.h"
32 #include "llvm/Config/config.h"
33 #ifdef _MSC_VER
34   #define WIN32_LEAN_AND_MEAN 1
35   #include <windows.h>
36 #endif
37 #ifndef CLANG_PREFIX
38 #define CLANG_PREFIX
39 #endif
40 using namespace clang;
41 using namespace clang::frontend;
42
43 namespace {
44
45 /// InitHeaderSearch - This class makes it easier to set the search paths of
46 ///  a HeaderSearch object. InitHeaderSearch stores several search path lists
47 ///  internally, which can be sent to a HeaderSearch object in one swoop.
48 class InitHeaderSearch {
49   std::vector<std::pair<IncludeDirGroup, DirectoryLookup> > IncludePath;
50   typedef std::vector<std::pair<IncludeDirGroup,
51                       DirectoryLookup> >::const_iterator path_iterator;
52   HeaderSearch &Headers;
53   bool Verbose;
54   std::string IncludeSysroot;
55   bool IsNotEmptyOrRoot;
56
57 public:
58
59   InitHeaderSearch(HeaderSearch &HS, bool verbose, llvm::StringRef sysroot)
60     : Headers(HS), Verbose(verbose), IncludeSysroot(sysroot),
61       IsNotEmptyOrRoot(!(sysroot.empty() || sysroot == "/")) {
62   }
63
64   /// AddPath - Add the specified path to the specified group list.
65   void AddPath(const llvm::Twine &Path, IncludeDirGroup Group,
66                bool isCXXAware, bool isUserSupplied,
67                bool isFramework, bool IgnoreSysRoot = false);
68
69   /// AddGnuCPlusPlusIncludePaths - Add the necessary paths to support a gnu
70   ///  libstdc++.
71   void AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
72                                    llvm::StringRef ArchDir,
73                                    llvm::StringRef Dir32,
74                                    llvm::StringRef Dir64,
75                                    const llvm::Triple &triple);
76
77   /// AddMinGWCPlusPlusIncludePaths - Add the necessary paths to support a MinGW
78   ///  libstdc++.
79   void AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
80                                      llvm::StringRef Arch,
81                                      llvm::StringRef Version);
82
83   /// AddMinGW64CXXPaths - Add the necessary paths to support
84   /// libstdc++ of x86_64-w64-mingw32 aka mingw-w64.
85   void AddMinGW64CXXPaths(llvm::StringRef Base,
86                           llvm::StringRef Version);
87
88   /// AddDelimitedPaths - Add a list of paths delimited by the system PATH
89   /// separator. The processing follows that of the CPATH variable for gcc.
90   void AddDelimitedPaths(llvm::StringRef String);
91
92   // AddDefaultCIncludePaths - Add paths that should always be searched.
93   void AddDefaultCIncludePaths(const llvm::Triple &triple,
94                                const HeaderSearchOptions &HSOpts);
95
96   // AddDefaultCPlusPlusIncludePaths -  Add paths that should be searched when
97   //  compiling c++.
98   void AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple,
99                                        const HeaderSearchOptions &HSOpts);
100
101   /// AddDefaultSystemIncludePaths - Adds the default system include paths so
102   ///  that e.g. stdio.h is found.
103   void AddDefaultSystemIncludePaths(const LangOptions &Lang,
104                                     const llvm::Triple &triple,
105                                     const HeaderSearchOptions &HSOpts);
106
107   /// Realize - Merges all search path lists into one list and send it to
108   /// HeaderSearch.
109   void Realize(const LangOptions &Lang);
110 };
111
112 }  // end anonymous namespace.
113
114 void InitHeaderSearch::AddPath(const llvm::Twine &Path,
115                                IncludeDirGroup Group, bool isCXXAware,
116                                bool isUserSupplied, bool isFramework,
117                                bool IgnoreSysRoot) {
118   assert(!Path.isTriviallyEmpty() && "can't handle empty path here");
119   FileManager &FM = Headers.getFileMgr();
120
121   // Compute the actual path, taking into consideration -isysroot.
122   llvm::SmallString<256> MappedPathStorage;
123   llvm::StringRef MappedPathStr = Path.toStringRef(MappedPathStorage);
124
125   // Handle isysroot.
126   if ((Group == System || Group == CXXSystem) && !IgnoreSysRoot &&
127 #if defined(_WIN32)
128       !MappedPathStr.empty() &&
129       llvm::sys::path::is_separator(MappedPathStr[0]) &&
130 #else
131       llvm::sys::path::is_absolute(MappedPathStr) &&
132 #endif
133       IsNotEmptyOrRoot) {
134     MappedPathStorage.clear();
135     MappedPathStr =
136       (IncludeSysroot + Path).toStringRef(MappedPathStorage);
137   }
138
139   // Compute the DirectoryLookup type.
140   SrcMgr::CharacteristicKind Type;
141   if (Group == Quoted || Group == Angled)
142     Type = SrcMgr::C_User;
143   else if (isCXXAware)
144     Type = SrcMgr::C_System;
145   else
146     Type = SrcMgr::C_ExternCSystem;
147
148
149   // If the directory exists, add it.
150   if (const DirectoryEntry *DE = FM.getDirectory(MappedPathStr)) {
151     IncludePath.push_back(std::make_pair(Group, DirectoryLookup(DE, Type,
152                           isUserSupplied, isFramework)));
153     return;
154   }
155
156   // Check to see if this is an apple-style headermap (which are not allowed to
157   // be frameworks).
158   if (!isFramework) {
159     if (const FileEntry *FE = FM.getFile(MappedPathStr)) {
160       if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
161         // It is a headermap, add it to the search path.
162         IncludePath.push_back(std::make_pair(Group, DirectoryLookup(HM, Type,
163                               isUserSupplied)));
164         return;
165       }
166     }
167   }
168
169   if (Verbose)
170     llvm::errs() << "ignoring nonexistent directory \""
171                  << MappedPathStr << "\"\n";
172 }
173
174
175 void InitHeaderSearch::AddDelimitedPaths(llvm::StringRef at) {
176   if (at.empty()) // Empty string should not add '.' path.
177     return;
178
179   llvm::StringRef::size_type delim;
180   while ((delim = at.find(llvm::sys::PathSeparator)) != llvm::StringRef::npos) {
181     if (delim == 0)
182       AddPath(".", Angled, false, true, false);
183     else
184       AddPath(at.substr(0, delim), Angled, false, true, false);
185     at = at.substr(delim + 1);
186   }
187
188   if (at.empty())
189     AddPath(".", Angled, false, true, false);
190   else
191     AddPath(at, Angled, false, true, false);
192 }
193
194 void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(llvm::StringRef Base,
195                                                    llvm::StringRef ArchDir,
196                                                    llvm::StringRef Dir32,
197                                                    llvm::StringRef Dir64,
198                                                    const llvm::Triple &triple) {
199   // Add the base dir
200   AddPath(Base, CXXSystem, true, false, false);
201
202   // Add the multilib dirs
203   llvm::Triple::ArchType arch = triple.getArch();
204   bool is64bit = arch == llvm::Triple::ppc64 || arch == llvm::Triple::x86_64;
205   if (is64bit)
206     AddPath(Base + "/" + ArchDir + "/" + Dir64, CXXSystem, true, false, false);
207   else
208     AddPath(Base + "/" + ArchDir + "/" + Dir32, CXXSystem, true, false, false);
209
210   // Add the backward dir
211   AddPath(Base + "/backward", CXXSystem, true, false, false);
212 }
213
214 void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(llvm::StringRef Base,
215                                                      llvm::StringRef Arch,
216                                                      llvm::StringRef Version) {
217   AddPath(Base + "/" + Arch + "/" + Version + "/include/c++",
218           CXXSystem, true, false, false);
219   AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/" + Arch,
220           CXXSystem, true, false, false);
221   AddPath(Base + "/" + Arch + "/" + Version + "/include/c++/backward",
222           CXXSystem, true, false, false);
223 }
224
225 void InitHeaderSearch::AddMinGW64CXXPaths(llvm::StringRef Base,
226                                           llvm::StringRef Version) {
227   // Assumes Base is HeaderSearchOpts' ResourceDir
228   AddPath(Base + "/../../../include/c++/" + Version,
229           CXXSystem, true, false, false);
230   AddPath(Base + "/../../../include/c++/" + Version + "/x86_64-w64-mingw32",
231           CXXSystem, true, false, false);
232   AddPath(Base + "/../../../include/c++/" + Version + "/i686-w64-mingw32",
233           CXXSystem, true, false, false);
234   AddPath(Base + "/../../../include/c++/" + Version + "/backward",
235           CXXSystem, true, false, false);
236 }
237
238   // FIXME: This probably should goto to some platform utils place.
239 #ifdef _MSC_VER
240
241   // Read registry string.
242   // This also supports a means to look for high-versioned keys by use
243   // of a $VERSION placeholder in the key path.
244   // $VERSION in the key path is a placeholder for the version number,
245   // causing the highest value path to be searched for and used.
246   // I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
247   // There can be additional characters in the component.  Only the numberic
248   // characters are compared.
249 static bool getSystemRegistryString(const char *keyPath, const char *valueName,
250                                     char *value, size_t maxLength) {
251   HKEY hRootKey = NULL;
252   HKEY hKey = NULL;
253   const char* subKey = NULL;
254   DWORD valueType;
255   DWORD valueSize = maxLength - 1;
256   long lResult;
257   bool returnValue = false;
258   
259   if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
260     hRootKey = HKEY_CLASSES_ROOT;
261     subKey = keyPath + 18;
262   } else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
263     hRootKey = HKEY_USERS;
264     subKey = keyPath + 11;
265   } else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
266     hRootKey = HKEY_LOCAL_MACHINE;
267     subKey = keyPath + 19;
268   } else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
269     hRootKey = HKEY_CURRENT_USER;
270     subKey = keyPath + 18;
271   }
272   else
273     return false;
274   
275   const char *placeHolder = strstr(subKey, "$VERSION");
276   char bestName[256];
277   bestName[0] = '\0';
278   // If we have a $VERSION placeholder, do the highest-version search.
279   if (placeHolder) {
280     const char *keyEnd = placeHolder - 1;
281     const char *nextKey = placeHolder;
282     // Find end of previous key.
283     while ((keyEnd > subKey) && (*keyEnd != '\\'))
284       keyEnd--;
285     // Find end of key containing $VERSION.
286     while (*nextKey && (*nextKey != '\\'))
287       nextKey++;
288     size_t partialKeyLength = keyEnd - subKey;
289     char partialKey[256];
290     if (partialKeyLength > sizeof(partialKey))
291       partialKeyLength = sizeof(partialKey);
292     strncpy(partialKey, subKey, partialKeyLength);
293     partialKey[partialKeyLength] = '\0';
294     HKEY hTopKey = NULL;
295     lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ, &hTopKey);
296     if (lResult == ERROR_SUCCESS) {
297       char keyName[256];
298       int bestIndex = -1;
299       double bestValue = 0.0;
300       DWORD index, size = sizeof(keyName) - 1;
301       for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
302           NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
303         const char *sp = keyName;
304         while (*sp && !isdigit(*sp))
305           sp++;
306         if (!*sp)
307           continue;
308         const char *ep = sp + 1;
309         while (*ep && (isdigit(*ep) || (*ep == '.')))
310           ep++;
311         char numBuf[32];
312         strncpy(numBuf, sp, sizeof(numBuf) - 1);
313         numBuf[sizeof(numBuf) - 1] = '\0';
314         double value = strtod(numBuf, NULL);
315         if (value > bestValue) {
316           bestIndex = (int)index;
317           bestValue = value;
318           strcpy(bestName, keyName);
319         }
320         size = sizeof(keyName) - 1;
321       }
322       // If we found the highest versioned key, open the key and get the value.
323       if (bestIndex != -1) {
324         // Append rest of key.
325         strncat(bestName, nextKey, sizeof(bestName) - 1);
326         bestName[sizeof(bestName) - 1] = '\0';
327         // Open the chosen key path remainder.
328         lResult = RegOpenKeyEx(hTopKey, bestName, 0, KEY_READ, &hKey);
329         if (lResult == ERROR_SUCCESS) {
330           lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
331             (LPBYTE)value, &valueSize);
332           if (lResult == ERROR_SUCCESS)
333             returnValue = true;
334           RegCloseKey(hKey);
335         }
336       }
337       RegCloseKey(hTopKey);
338     }
339   }
340   else {
341     lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
342     if (lResult == ERROR_SUCCESS) {
343       lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
344         (LPBYTE)value, &valueSize);
345       if (lResult == ERROR_SUCCESS)
346         returnValue = true;
347       RegCloseKey(hKey);
348     }
349   }
350   return returnValue;
351 }
352 #else // _MSC_VER
353   // Read registry string.
354 static bool getSystemRegistryString(const char*, const char*, char*, size_t) {
355   return(false);
356 }
357 #endif // _MSC_VER
358
359   // Get Visual Studio installation directory.
360 static bool getVisualStudioDir(std::string &path) {
361   // First check the environment variables that vsvars32.bat sets.
362   const char* vcinstalldir = getenv("VCINSTALLDIR");
363   if (vcinstalldir) {
364     char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
365     if (p)
366       *p = '\0';
367     path = vcinstalldir;
368     return true;
369   }
370
371   char vsIDEInstallDir[256];
372   char vsExpressIDEInstallDir[256];
373   // Then try the windows registry.
374   bool hasVCDir = getSystemRegistryString(
375     "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
376     "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
377   bool hasVCExpressDir = getSystemRegistryString(
378     "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
379     "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
380     // If we have both vc80 and vc90, pick version we were compiled with.
381   if (hasVCDir && vsIDEInstallDir[0]) {
382     char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
383     if (p)
384       *p = '\0';
385     path = vsIDEInstallDir;
386     return true;
387   }
388   
389   if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
390     char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
391     if (p)
392       *p = '\0';
393     path = vsExpressIDEInstallDir;
394     return true;
395   }
396
397   // Try the environment.
398   const char *vs100comntools = getenv("VS100COMNTOOLS");
399   const char *vs90comntools = getenv("VS90COMNTOOLS");
400   const char *vs80comntools = getenv("VS80COMNTOOLS");
401   const char *vscomntools = NULL;
402
403   // Try to find the version that we were compiled with
404   if(false) {}
405   #if (_MSC_VER >= 1600)  // VC100
406   else if(vs100comntools) {
407     vscomntools = vs100comntools;
408   }
409   #elif (_MSC_VER == 1500) // VC80
410   else if(vs90comntools) {
411     vscomntools = vs90comntools;
412   }
413   #elif (_MSC_VER == 1400) // VC80
414   else if(vs80comntools) {
415     vscomntools = vs80comntools;
416   }
417   #endif
418   // Otherwise find any version we can
419   else if (vs100comntools)
420     vscomntools = vs100comntools;
421   else if (vs90comntools)
422     vscomntools = vs90comntools;
423   else if (vs80comntools)
424     vscomntools = vs80comntools;
425
426   if (vscomntools && *vscomntools) {
427     const char *p = strstr(vscomntools, "\\Common7\\Tools");
428     path = p ? std::string(vscomntools, p) : vscomntools;
429     return true;
430   }
431   return false;
432 }
433
434   // Get Windows SDK installation directory.
435 static bool getWindowsSDKDir(std::string &path) {
436   char windowsSDKInstallDir[256];
437   // Try the Windows registry.
438   bool hasSDKDir = getSystemRegistryString(
439    "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
440                                            "InstallationFolder",
441                                            windowsSDKInstallDir,
442                                            sizeof(windowsSDKInstallDir) - 1);
443     // If we have both vc80 and vc90, pick version we were compiled with.
444   if (hasSDKDir && windowsSDKInstallDir[0]) {
445     path = windowsSDKInstallDir;
446     return(true);
447   }
448   return(false);
449 }
450
451 void InitHeaderSearch::AddDefaultCIncludePaths(const llvm::Triple &triple,
452                                             const HeaderSearchOptions &HSOpts) {
453   llvm::Triple::OSType os = triple.getOS();
454
455   switch (os) {
456   case llvm::Triple::FreeBSD:
457   case llvm::Triple::NetBSD:
458     break;
459   default:
460     // FIXME: temporary hack: hard-coded paths.
461     AddPath("/usr/local/include", System, true, false, false);
462     break;
463   }
464
465   // Builtin includes use #include_next directives and should be positioned
466   // just prior C include dirs.
467   if (HSOpts.UseBuiltinIncludes) {
468     // Ignore the sys root, we *always* look for clang headers relative to
469     // supplied path.
470     llvm::sys::Path P(HSOpts.ResourceDir);
471     P.appendComponent("include");
472     AddPath(P.str(), System, false, false, false, /*IgnoreSysRoot=*/ true);
473   }
474
475   // Add dirs specified via 'configure --with-c-include-dirs'.
476   llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
477   if (CIncludeDirs != "") {
478     llvm::SmallVector<llvm::StringRef, 5> dirs;
479     CIncludeDirs.split(dirs, ":");
480     for (llvm::SmallVectorImpl<llvm::StringRef>::iterator i = dirs.begin();
481          i != dirs.end();
482          ++i)
483       AddPath(*i, System, false, false, false);
484     return;
485   }
486
487   switch (os) {
488   case llvm::Triple::Win32: {
489     std::string VSDir;
490     std::string WindowsSDKDir;
491     if (getVisualStudioDir(VSDir)) {
492       AddPath(VSDir + "\\VC\\include", System, false, false, false);
493       if (getWindowsSDKDir(WindowsSDKDir))
494         AddPath(WindowsSDKDir + "\\include", System, false, false, false);
495       else
496         AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
497                 System, false, false, false);
498     } else {
499       // Default install paths.
500       AddPath("C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
501               System, false, false, false);
502       AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
503               System, false, false, false);
504       AddPath(
505         "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
506         System, false, false, false);
507       AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
508               System, false, false, false);
509       AddPath(
510         "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
511         System, false, false, false);
512     }
513     break;
514   }
515   case llvm::Triple::Haiku:
516     AddPath("/boot/common/include", System, true, false, false);
517     AddPath("/boot/develop/headers/os", System, true, false, false);
518     AddPath("/boot/develop/headers/os/app", System, true, false, false);
519     AddPath("/boot/develop/headers/os/arch", System, true, false, false);
520     AddPath("/boot/develop/headers/os/device", System, true, false, false);
521     AddPath("/boot/develop/headers/os/drivers", System, true, false, false);
522     AddPath("/boot/develop/headers/os/game", System, true, false, false);
523     AddPath("/boot/develop/headers/os/interface", System, true, false, false);
524     AddPath("/boot/develop/headers/os/kernel", System, true, false, false);
525     AddPath("/boot/develop/headers/os/locale", System, true, false, false);
526     AddPath("/boot/develop/headers/os/mail", System, true, false, false);
527     AddPath("/boot/develop/headers/os/media", System, true, false, false);
528     AddPath("/boot/develop/headers/os/midi", System, true, false, false);
529     AddPath("/boot/develop/headers/os/midi2", System, true, false, false);
530     AddPath("/boot/develop/headers/os/net", System, true, false, false);
531     AddPath("/boot/develop/headers/os/storage", System, true, false, false);
532     AddPath("/boot/develop/headers/os/support", System, true, false, false);
533     AddPath("/boot/develop/headers/os/translation",
534       System, true, false, false);
535     AddPath("/boot/develop/headers/os/add-ons/graphics",
536       System, true, false, false);
537     AddPath("/boot/develop/headers/os/add-ons/input_server",
538       System, true, false, false);
539     AddPath("/boot/develop/headers/os/add-ons/screen_saver",
540       System, true, false, false);
541     AddPath("/boot/develop/headers/os/add-ons/tracker",
542       System, true, false, false);
543     AddPath("/boot/develop/headers/os/be_apps/Deskbar",
544       System, true, false, false);
545     AddPath("/boot/develop/headers/os/be_apps/NetPositive",
546       System, true, false, false);
547     AddPath("/boot/develop/headers/os/be_apps/Tracker",
548       System, true, false, false);
549     AddPath("/boot/develop/headers/cpp", System, true, false, false);
550     AddPath("/boot/develop/headers/cpp/i586-pc-haiku",
551       System, true, false, false);
552     AddPath("/boot/develop/headers/3rdparty", System, true, false, false);
553     AddPath("/boot/develop/headers/bsd", System, true, false, false);
554     AddPath("/boot/develop/headers/glibc", System, true, false, false);
555     AddPath("/boot/develop/headers/posix", System, true, false, false);
556     AddPath("/boot/develop/headers",  System, true, false, false);
557     break;
558   case llvm::Triple::RTEMS:
559     break;
560   case llvm::Triple::Cygwin:
561     AddPath("/usr/include/w32api", System, true, false, false);
562     break;
563   case llvm::Triple::MinGW32: { 
564       // mingw-w64 crt include paths
565       llvm::sys::Path P(HSOpts.ResourceDir);
566       P.appendComponent("../../../i686-w64-mingw32/include"); // <sysroot>/i686-w64-mingw32/include
567       AddPath(P.str(), System, true, false, false);
568       P = llvm::sys::Path(HSOpts.ResourceDir);
569       P.appendComponent("../../../x86_64-w64-mingw32/include"); // <sysroot>/x86_64-w64-mingw32/include
570       AddPath(P.str(), System, true, false, false);
571       // mingw.org crt include paths
572       P = llvm::sys::Path(HSOpts.ResourceDir);
573       P.appendComponent("../../../include"); // <sysroot>/include
574       AddPath(P.str(), System, true, false, false);
575       AddPath("/mingw/include", System, true, false, false);
576       AddPath("c:/mingw/include", System, true, false, false); 
577     }
578     break;
579   case llvm::Triple::FreeBSD:
580     AddPath(CLANG_PREFIX "/usr/include/clang/" CLANG_VERSION_STRING,
581       System, false, false, false);
582     break;
583       
584   case llvm::Triple::Linux:
585     // Generic Debian multiarch support:
586     if (triple.getArch() == llvm::Triple::x86_64) {
587       AddPath("/usr/include/x86_64-linux-gnu", System, false, false, false);
588       AddPath("/usr/include/i686-linux-gnu/64", System, false, false, false);
589       AddPath("/usr/include/i486-linux-gnu/64", System, false, false, false);
590     } else if (triple.getArch() == llvm::Triple::x86) {
591       AddPath("/usr/include/x86_64-linux-gnu/32", System, false, false, false);
592       AddPath("/usr/include/i686-linux-gnu", System, false, false, false);
593       AddPath("/usr/include/i486-linux-gnu", System, false, false, false);
594     } else if (triple.getArch() == llvm::Triple::arm) {
595       AddPath("/usr/include/arm-linux-gnueabi", System, false, false, false);
596     }
597   default:
598     break;
599   }
600
601   if ( os != llvm::Triple::RTEMS )
602     AddPath(CLANG_PREFIX "/usr/include", System, false, false, false);
603 }
604
605 void InitHeaderSearch::
606 AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple, const HeaderSearchOptions &HSOpts) {
607   llvm::Triple::OSType os = triple.getOS();
608   llvm::StringRef CxxIncludeRoot(CXX_INCLUDE_ROOT);
609   if (CxxIncludeRoot != "") {
610     llvm::StringRef CxxIncludeArch(CXX_INCLUDE_ARCH);
611     if (CxxIncludeArch == "")
612       AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, triple.str().c_str(),
613                                   CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
614                                   triple);
615     else
616       AddGnuCPlusPlusIncludePaths(CxxIncludeRoot, CXX_INCLUDE_ARCH,
617                                   CXX_INCLUDE_32BIT_DIR, CXX_INCLUDE_64BIT_DIR,
618                                   triple);
619     return;
620   }
621   // FIXME: temporary hack: hard-coded paths.
622
623   if (triple.isOSDarwin()) {
624     switch (triple.getArch()) {
625     default: break;
626
627     case llvm::Triple::ppc:
628     case llvm::Triple::ppc64:
629       AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
630                                   "powerpc-apple-darwin10", "", "ppc64",
631                                   triple);
632       AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
633                                   "powerpc-apple-darwin10", "", "ppc64",
634                                   triple);
635       break;
636
637     case llvm::Triple::x86:
638     case llvm::Triple::x86_64:
639       AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
640                                   "i686-apple-darwin10", "", "x86_64", triple);
641       AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
642                                   "i686-apple-darwin8", "", "", triple);
643       break;
644
645     case llvm::Triple::arm:
646     case llvm::Triple::thumb:
647       AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
648                                   "arm-apple-darwin10", "v7", "", triple);
649       AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
650                                   "arm-apple-darwin10", "v6", "", triple);
651       break;
652     }
653     return;
654   }
655
656   switch (os) {
657   case llvm::Triple::Cygwin:
658     // Cygwin-1.7
659     AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.4");
660     // g++-4 / Cygwin-1.5
661     AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "4.3.2");
662     // FIXME: Do we support g++-3.4.4?
663     AddMinGWCPlusPlusIncludePaths("/usr/lib/gcc", "i686-pc-cygwin", "3.4.4");
664     break;
665   case llvm::Triple::MinGW32:
666     // mingw-w64 C++ include paths (i686-w64-mingw32 and x86_64-w64-mingw32)
667     AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.0");
668     AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.1");
669     AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.2");
670     AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.5.3");
671     AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.0");
672     AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.1");
673     AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.6.2");
674     AddMinGW64CXXPaths(HSOpts.ResourceDir, "4.7.0");
675     // mingw.org C++ include paths
676     AddMinGWCPlusPlusIncludePaths("/mingw/lib/gcc", "mingw32", "4.5.2"); //MSYS
677     AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.5.0");
678     AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
679     AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
680     break;
681   case llvm::Triple::DragonFly:
682     AddPath("/usr/include/c++/4.1", CXXSystem, true, false, false);
683     break;
684   case llvm::Triple::Linux:
685     //===------------------------------------------------------------------===//
686     // Debian based distros.
687     // Note: these distros symlink /usr/include/c++/X.Y.Z -> X.Y
688     //===------------------------------------------------------------------===//
689
690     // Ubuntu 11.11 "Oneiric Ocelot" -- gcc-4.6.0
691     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
692                                 "x86_64-linux-gnu", "32", "", triple);
693     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
694                                 "i686-linux-gnu", "", "64", triple);
695     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
696                                 "i486-linux-gnu", "", "64", triple);
697     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
698                                 "arm-linux-gnueabi", "", "", triple);
699
700     // Ubuntu 11.04 "Natty Narwhal" -- gcc-4.5.2
701     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
702                                 "x86_64-linux-gnu", "32", "", triple);
703     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
704                                 "i686-linux-gnu", "", "64", triple);
705     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
706                                 "i486-linux-gnu", "", "64", triple);
707     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
708                                 "arm-linux-gnueabi", "", "", triple);
709
710     // Ubuntu 10.10 "Maverick Meerkat" -- gcc-4.4.5
711     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
712                                 "i686-linux-gnu", "", "64", triple);
713     // The rest of 10.10 is the same as previous versions.
714
715     // Ubuntu 10.04 LTS "Lucid Lynx" -- gcc-4.4.3
716     // Ubuntu 9.10 "Karmic Koala"    -- gcc-4.4.1
717     // Debian 6.0 "squeeze"          -- gcc-4.4.2
718     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
719                                 "x86_64-linux-gnu", "32", "", triple);
720     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
721                                 "i486-linux-gnu", "", "64", triple);
722     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
723                                 "arm-linux-gnueabi", "", "", triple);
724     // Ubuntu 9.04 "Jaunty Jackalope" -- gcc-4.3.3
725     // Ubuntu 8.10 "Intrepid Ibex"    -- gcc-4.3.2
726     // Debian 5.0 "lenny"             -- gcc-4.3.2
727     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
728                                 "x86_64-linux-gnu", "32", "", triple);
729     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
730                                 "i486-linux-gnu", "", "64", triple);
731     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
732                                 "arm-linux-gnueabi", "", "", triple);
733     // Ubuntu 8.04.4 LTS "Hardy Heron"     -- gcc-4.2.4
734     // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3
735     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
736                                 "x86_64-linux-gnu", "32", "", triple);
737     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2",
738                                 "i486-linux-gnu", "", "64", triple);
739     // Ubuntu 7.10 "Gutsy Gibbon" -- gcc-4.1.3
740     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
741                                 "x86_64-linux-gnu", "32", "", triple);
742     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1",
743                                 "i486-linux-gnu", "", "64", triple);
744
745     //===------------------------------------------------------------------===//
746     // Redhat based distros.
747     //===------------------------------------------------------------------===//
748     // Fedora 15
749     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
750                                 "x86_64-redhat-linux", "32", "", triple);
751     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
752                                 "i686-redhat-linux", "", "", triple);
753     // Fedora 14
754     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
755                                 "x86_64-redhat-linux", "32", "", triple);
756     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5.1",
757                                 "i686-redhat-linux", "", "", triple);
758     // RHEL5(gcc44)
759     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
760                                 "x86_64-redhat-linux6E", "32", "", triple);
761     // Fedora 13
762     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
763                                 "x86_64-redhat-linux", "32", "", triple);
764     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4",
765                                 "i686-redhat-linux","", "", triple);
766     // Fedora 12
767     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
768                                 "x86_64-redhat-linux", "32", "", triple);
769     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
770                                 "i686-redhat-linux","", "", triple);
771     // Fedora 12 (pre-FEB-2010)
772     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
773                                 "x86_64-redhat-linux", "32", "", triple);
774     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.2",
775                                 "i686-redhat-linux","", "", triple);
776     // Fedora 11
777     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
778                                 "x86_64-redhat-linux", "32", "", triple);
779     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.1",
780                                 "i586-redhat-linux","", "", triple);
781     // Fedora 10
782     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
783                                 "x86_64-redhat-linux", "32", "", triple);
784     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
785                                 "i386-redhat-linux","", "", triple);
786     // Fedora 9
787     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
788                                 "x86_64-redhat-linux", "32", "", triple);
789     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
790                                 "i386-redhat-linux", "", "", triple);
791     // Fedora 8
792     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
793                                 "x86_64-redhat-linux", "", "", triple);
794     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
795                                 "i386-redhat-linux", "", "", triple);
796       
797     // RHEL 5
798     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.1",
799                                 "x86_64-redhat-linux", "32", "", triple);
800     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.1",
801                                 "i386-redhat-linux", "", "", triple);
802
803
804     //===------------------------------------------------------------------===//
805
806     // Exherbo (2010-01-25)
807     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
808                                 "x86_64-pc-linux-gnu", "32", "", triple);
809     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3",
810                                 "i686-pc-linux-gnu", "", "", triple);
811
812     // openSUSE 11.1 32 bit
813     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
814                                 "i586-suse-linux", "", "", triple);
815     // openSUSE 11.1 64 bit
816     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
817                                 "x86_64-suse-linux", "32", "", triple);
818     // openSUSE 11.2
819     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
820                                 "i586-suse-linux", "", "", triple);
821     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
822                                 "x86_64-suse-linux", "", "", triple);
823
824     // openSUSE 11.4
825     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
826                                 "i586-suse-linux", "", "", triple);
827     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.5",
828                                 "x86_64-suse-linux", "", "", triple);
829
830     // openSUSE 12.1
831     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
832                                 "i586-suse-linux", "", "", triple);
833     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6",
834                                 "x86_64-suse-linux", "", "", triple);
835     // Arch Linux 2008-06-24
836     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
837                                 "i686-pc-linux-gnu", "", "", triple);
838     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
839                                 "x86_64-unknown-linux-gnu", "", "", triple);
840
841     // Arch Linux gcc 4.6
842     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.1",
843                                 "i686-pc-linux-gnu", "", "", triple);
844     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.1",
845                                 "x86_64-unknown-linux-gnu", "", "", triple);
846     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
847                                 "i686-pc-linux-gnu", "", "", triple);
848     AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.6.0",
849                                 "x86_64-unknown-linux-gnu", "", "", triple);
850
851     // Gentoo x86 gcc 4.5.2
852     AddGnuCPlusPlusIncludePaths(
853       "/usr/lib/gcc/i686-pc-linux-gnu/4.5.2/include/g++-v4",
854       "i686-pc-linux-gnu", "", "", triple);
855     // Gentoo x86 gcc 4.4.5
856     AddGnuCPlusPlusIncludePaths(
857       "/usr/lib/gcc/i686-pc-linux-gnu/4.4.5/include/g++-v4",
858       "i686-pc-linux-gnu", "", "", triple);
859     // Gentoo x86 gcc 4.4.4
860     AddGnuCPlusPlusIncludePaths(
861       "/usr/lib/gcc/i686-pc-linux-gnu/4.4.4/include/g++-v4",
862       "i686-pc-linux-gnu", "", "", triple);
863    // Gentoo x86 2010.0 stable
864     AddGnuCPlusPlusIncludePaths(
865       "/usr/lib/gcc/i686-pc-linux-gnu/4.4.3/include/g++-v4",
866       "i686-pc-linux-gnu", "", "", triple);
867     // Gentoo x86 2009.1 stable
868     AddGnuCPlusPlusIncludePaths(
869       "/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4",
870       "i686-pc-linux-gnu", "", "", triple);
871     // Gentoo x86 2009.0 stable
872     AddGnuCPlusPlusIncludePaths(
873       "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
874       "i686-pc-linux-gnu", "", "", triple);
875     // Gentoo x86 2008.0 stable
876     AddGnuCPlusPlusIncludePaths(
877       "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
878       "i686-pc-linux-gnu", "", "", triple);
879     // Gentoo x86 llvm-gcc trunk
880     AddGnuCPlusPlusIncludePaths(
881         "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
882         "i686-pc-linux-gnu", "", "", triple);
883
884     // Gentoo amd64 gcc 4.5.2
885     AddGnuCPlusPlusIncludePaths(
886         "/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.2/include/g++-v4",
887         "x86_64-pc-linux-gnu", "32", "", triple);
888     // Gentoo amd64 gcc 4.4.5
889     AddGnuCPlusPlusIncludePaths(
890         "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4",
891         "x86_64-pc-linux-gnu", "32", "", triple);
892     // Gentoo amd64 gcc 4.4.4
893     AddGnuCPlusPlusIncludePaths(
894         "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.4/include/g++-v4",
895         "x86_64-pc-linux-gnu", "32", "", triple);
896     // Gentoo amd64 gcc 4.4.3
897     AddGnuCPlusPlusIncludePaths(
898         "/usr/lib/gcc/x86_64-pc-linux-gnu/4.4.3/include/g++-v4",
899         "x86_64-pc-linux-gnu", "32", "", triple);
900     // Gentoo amd64 gcc 4.3.2
901     AddGnuCPlusPlusIncludePaths(
902         "/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/include/g++-v4",
903         "x86_64-pc-linux-gnu", "", "", triple);
904     // Gentoo amd64 stable
905     AddGnuCPlusPlusIncludePaths(
906         "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
907         "x86_64-pc-linux-gnu", "", "", triple);
908
909     // Gentoo amd64 llvm-gcc trunk
910     AddGnuCPlusPlusIncludePaths(
911         "/usr/lib/llvm-gcc-4.2-9999/include/c++/4.2.1",
912         "x86_64-pc-linux-gnu", "", "", triple);
913
914     break;
915   case llvm::Triple::FreeBSD:
916     // FreeBSD 8.0
917     // FreeBSD 7.3
918     AddGnuCPlusPlusIncludePaths(CLANG_PREFIX "/usr/include/c++/4.2",
919                                 "", "", "", triple);
920     AddGnuCPlusPlusIncludePaths(CLANG_PREFIX "/usr/include/c++/4.2/backward",
921                                 "", "", "", triple);
922     break;
923   case llvm::Triple::NetBSD:
924     AddGnuCPlusPlusIncludePaths("/usr/include/g++", "", "", "", triple);
925     break;
926   case llvm::Triple::OpenBSD: {
927     std::string t = triple.getTriple();
928     if (t.substr(0, 6) == "x86_64")
929       t.replace(0, 6, "amd64");
930     AddGnuCPlusPlusIncludePaths("/usr/include/g++",
931                                 t, "", "", triple);
932     break;
933   }
934   case llvm::Triple::Minix:
935     AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3",
936                                 "", "", "", triple);
937     break;
938   case llvm::Triple::Solaris:
939     // Solaris - Fall though..
940   case llvm::Triple::AuroraUX:
941     // AuroraUX
942     AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
943                                 "i386-pc-solaris2.11", "", "", triple);
944     break;
945   default:
946     break;
947   }
948 }
949
950 void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
951                                                     const llvm::Triple &triple,
952                                             const HeaderSearchOptions &HSOpts) {
953   if (Lang.CPlusPlus && HSOpts.UseStandardCXXIncludes) {
954     if (HSOpts.UseLibcxx)
955       AddPath("/usr/include/c++/v1", CXXSystem, true, false, false);
956     else
957       AddDefaultCPlusPlusIncludePaths(triple, HSOpts);
958   }
959
960   AddDefaultCIncludePaths(triple, HSOpts);
961
962   // Add the default framework include paths on Darwin.
963   if (triple.isOSDarwin()) {
964     AddPath("/System/Library/Frameworks", System, true, false, true);
965     AddPath("/Library/Frameworks", System, true, false, true);
966   }
967 }
968
969 /// RemoveDuplicates - If there are duplicate directory entries in the specified
970 /// search list, remove the later (dead) ones.
971 static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
972                              unsigned First, bool Verbose) {
973   llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
974   llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
975   llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
976   for (unsigned i = First; i != SearchList.size(); ++i) {
977     unsigned DirToRemove = i;
978
979     const DirectoryLookup &CurEntry = SearchList[i];
980
981     if (CurEntry.isNormalDir()) {
982       // If this isn't the first time we've seen this dir, remove it.
983       if (SeenDirs.insert(CurEntry.getDir()))
984         continue;
985     } else if (CurEntry.isFramework()) {
986       // If this isn't the first time we've seen this framework dir, remove it.
987       if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
988         continue;
989     } else {
990       assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
991       // If this isn't the first time we've seen this headermap, remove it.
992       if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
993         continue;
994     }
995
996     // If we have a normal #include dir/framework/headermap that is shadowed
997     // later in the chain by a system include location, we actually want to
998     // ignore the user's request and drop the user dir... keeping the system
999     // dir.  This is weird, but required to emulate GCC's search path correctly.
1000     //
1001     // Since dupes of system dirs are rare, just rescan to find the original
1002     // that we're nuking instead of using a DenseMap.
1003     if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
1004       // Find the dir that this is the same of.
1005       unsigned FirstDir;
1006       for (FirstDir = 0; ; ++FirstDir) {
1007         assert(FirstDir != i && "Didn't find dupe?");
1008
1009         const DirectoryLookup &SearchEntry = SearchList[FirstDir];
1010
1011         // If these are different lookup types, then they can't be the dupe.
1012         if (SearchEntry.getLookupType() != CurEntry.getLookupType())
1013           continue;
1014
1015         bool isSame;
1016         if (CurEntry.isNormalDir())
1017           isSame = SearchEntry.getDir() == CurEntry.getDir();
1018         else if (CurEntry.isFramework())
1019           isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
1020         else {
1021           assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
1022           isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
1023         }
1024
1025         if (isSame)
1026           break;
1027       }
1028
1029       // If the first dir in the search path is a non-system dir, zap it
1030       // instead of the system one.
1031       if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
1032         DirToRemove = FirstDir;
1033     }
1034
1035     if (Verbose) {
1036       llvm::errs() << "ignoring duplicate directory \""
1037                    << CurEntry.getName() << "\"\n";
1038       if (DirToRemove != i)
1039         llvm::errs() << "  as it is a non-system directory that duplicates "
1040                      << "a system directory\n";
1041     }
1042
1043     // This is reached if the current entry is a duplicate.  Remove the
1044     // DirToRemove (usually the current dir).
1045     SearchList.erase(SearchList.begin()+DirToRemove);
1046     --i;
1047   }
1048 }
1049
1050
1051 void InitHeaderSearch::Realize(const LangOptions &Lang) {
1052   // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
1053   std::vector<DirectoryLookup> SearchList;
1054   SearchList.reserve(IncludePath.size());
1055
1056   // Quoted arguments go first.
1057   for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1058        it != ie; ++it) {
1059     if (it->first == Quoted)
1060       SearchList.push_back(it->second);
1061   }
1062   // Deduplicate and remember index.
1063   RemoveDuplicates(SearchList, 0, Verbose);
1064   unsigned NumQuoted = SearchList.size();
1065
1066   for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1067        it != ie; ++it) {
1068     if (it->first == Angled)
1069       SearchList.push_back(it->second);
1070   }
1071
1072   RemoveDuplicates(SearchList, NumQuoted, Verbose);
1073   unsigned NumAngled = SearchList.size();
1074
1075   for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1076        it != ie; ++it) {
1077     if (it->first == System || (Lang.CPlusPlus && it->first == CXXSystem))
1078       SearchList.push_back(it->second);
1079   }
1080
1081   for (path_iterator it = IncludePath.begin(), ie = IncludePath.end();
1082        it != ie; ++it) {
1083     if (it->first == After)
1084       SearchList.push_back(it->second);
1085   }
1086
1087   // Remove duplicates across both the Angled and System directories.  GCC does
1088   // this and failing to remove duplicates across these two groups breaks
1089   // #include_next.
1090   RemoveDuplicates(SearchList, NumQuoted, Verbose);
1091
1092   bool DontSearchCurDir = false;  // TODO: set to true if -I- is set?
1093   Headers.SetSearchPaths(SearchList, NumQuoted, NumAngled, DontSearchCurDir);
1094
1095   // If verbose, print the list of directories that will be searched.
1096   if (Verbose) {
1097     llvm::errs() << "#include \"...\" search starts here:\n";
1098     for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
1099       if (i == NumQuoted)
1100         llvm::errs() << "#include <...> search starts here:\n";
1101       const char *Name = SearchList[i].getName();
1102       const char *Suffix;
1103       if (SearchList[i].isNormalDir())
1104         Suffix = "";
1105       else if (SearchList[i].isFramework())
1106         Suffix = " (framework directory)";
1107       else {
1108         assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
1109         Suffix = " (headermap)";
1110       }
1111       llvm::errs() << " " << Name << Suffix << "\n";
1112     }
1113     llvm::errs() << "End of search list.\n";
1114   }
1115 }
1116
1117 void clang::ApplyHeaderSearchOptions(HeaderSearch &HS,
1118                                      const HeaderSearchOptions &HSOpts,
1119                                      const LangOptions &Lang,
1120                                      const llvm::Triple &Triple) {
1121   InitHeaderSearch Init(HS, HSOpts.Verbose, HSOpts.Sysroot);
1122
1123   // Add the user defined entries.
1124   for (unsigned i = 0, e = HSOpts.UserEntries.size(); i != e; ++i) {
1125     const HeaderSearchOptions::Entry &E = HSOpts.UserEntries[i];
1126     Init.AddPath(E.Path, E.Group, false, E.IsUserSupplied, E.IsFramework,
1127                  E.IgnoreSysRoot);
1128   }
1129
1130   // Add entries from CPATH and friends.
1131   Init.AddDelimitedPaths(HSOpts.EnvIncPath);
1132   if (Lang.CPlusPlus && Lang.ObjC1)
1133     Init.AddDelimitedPaths(HSOpts.ObjCXXEnvIncPath);
1134   else if (Lang.CPlusPlus)
1135     Init.AddDelimitedPaths(HSOpts.CXXEnvIncPath);
1136   else if (Lang.ObjC1)
1137     Init.AddDelimitedPaths(HSOpts.ObjCEnvIncPath);
1138   else
1139     Init.AddDelimitedPaths(HSOpts.CEnvIncPath);
1140
1141   if (HSOpts.UseStandardIncludes)
1142     Init.AddDefaultSystemIncludePaths(Lang, Triple, HSOpts);
1143
1144   Init.Realize(Lang);
1145 }