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