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