blob: 9381c67cd8a2478fc2f50b7fd55de2c164d751d5 [file] [log] [blame]
Nico Weber0fca0222008-08-22 09:25:22 +00001//===--- 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
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000014#include "clang/Frontend/InitHeaderSearch.h"
Nico Weber0fca0222008-08-22 09:25:22 +000015#include "clang/Lex/HeaderSearch.h"
16#include "clang/Basic/FileManager.h"
17#include "clang/Basic/LangOptions.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerd57a7ef2009-08-23 22:45:33 +000020#include "llvm/Support/raw_ostream.h"
Nico Weber0fca0222008-08-22 09:25:22 +000021#include "llvm/System/Path.h"
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +000022#include "llvm/Config/config.h"
Chris Lattner3daed522009-03-02 22:20:04 +000023#include <cstdio>
Mike Stump620d57a2009-10-12 20:50:45 +000024#ifdef _MSC_VER
25 #define WIN32_LEAN_AND_MEAN 1
26 #include <windows.h>
27#endif
Nico Weber0fca0222008-08-22 09:25:22 +000028using namespace clang;
29
Benjamin Kramer458fb102009-09-05 09:49:39 +000030void InitHeaderSearch::AddPath(const llvm::StringRef &Path,
31 IncludeDirGroup Group, bool isCXXAware,
32 bool isUserSupplied, bool isFramework,
33 bool IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000034 assert(!Path.empty() && "can't handle empty path here");
35 FileManager &FM = Headers.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +000036
Nico Weber0fca0222008-08-22 09:25:22 +000037 // Compute the actual path, taking into consideration -isysroot.
38 llvm::SmallString<256> MappedPath;
Mike Stump1eb44332009-09-09 15:08:12 +000039
Nico Weber0fca0222008-08-22 09:25:22 +000040 // Handle isysroot.
Chris Lattner6858dd32009-02-19 06:48:28 +000041 if (Group == System && !IgnoreSysRoot) {
Nico Weber0fca0222008-08-22 09:25:22 +000042 // FIXME: Portability. This should be a sys::Path interface, this doesn't
43 // handle things like C:\ right, nor win32 \\network\device\blah.
44 if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present.
45 MappedPath.append(isysroot.begin(), isysroot.end());
46 }
Mike Stump1eb44332009-09-09 15:08:12 +000047
Nico Weber0fca0222008-08-22 09:25:22 +000048 MappedPath.append(Path.begin(), Path.end());
49
50 // Compute the DirectoryLookup type.
Chris Lattner9d728512008-10-27 01:19:25 +000051 SrcMgr::CharacteristicKind Type;
Nico Weber0fca0222008-08-22 09:25:22 +000052 if (Group == Quoted || Group == Angled)
Chris Lattner0b9e7362008-09-26 21:18:42 +000053 Type = SrcMgr::C_User;
Nico Weber0fca0222008-08-22 09:25:22 +000054 else if (isCXXAware)
Chris Lattner0b9e7362008-09-26 21:18:42 +000055 Type = SrcMgr::C_System;
Nico Weber0fca0222008-08-22 09:25:22 +000056 else
Chris Lattner0b9e7362008-09-26 21:18:42 +000057 Type = SrcMgr::C_ExternCSystem;
Mike Stump1eb44332009-09-09 15:08:12 +000058
59
Nico Weber0fca0222008-08-22 09:25:22 +000060 // If the directory exists, add it.
Benjamin Kramer458fb102009-09-05 09:49:39 +000061 if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +000062 IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
63 isFramework));
64 return;
65 }
Mike Stump1eb44332009-09-09 15:08:12 +000066
Nico Weber0fca0222008-08-22 09:25:22 +000067 // Check to see if this is an apple-style headermap (which are not allowed to
68 // be frameworks).
69 if (!isFramework) {
Benjamin Kramer458fb102009-09-05 09:49:39 +000070 if (const FileEntry *FE = FM.getFile(MappedPath.str())) {
Nico Weber0fca0222008-08-22 09:25:22 +000071 if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) {
72 // It is a headermap, add it to the search path.
73 IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied));
74 return;
75 }
76 }
77 }
Mike Stump1eb44332009-09-09 15:08:12 +000078
Nico Weber0fca0222008-08-22 09:25:22 +000079 if (Verbose)
Daniel Dunbar77659342009-08-19 20:04:03 +000080 llvm::errs() << "ignoring nonexistent directory \""
81 << MappedPath.str() << "\"\n";
Nico Weber0fca0222008-08-22 09:25:22 +000082}
83
84
85void InitHeaderSearch::AddEnvVarPaths(const char *Name) {
86 const char* at = getenv(Name);
Daniel Dunbarbb952552008-10-04 20:58:18 +000087 if (!at || *at == 0) // Empty string should not add '.' path.
Nico Weber0fca0222008-08-22 09:25:22 +000088 return;
89
90 const char* delim = strchr(at, llvm::sys::PathSeparator);
91 while (delim != 0) {
92 if (delim-at == 0)
93 AddPath(".", Angled, false, true, false);
94 else
Benjamin Kramer458fb102009-09-05 09:49:39 +000095 AddPath(llvm::StringRef(at, delim-at), Angled, false, true, false);
Nico Weber0fca0222008-08-22 09:25:22 +000096 at = delim + 1;
97 delim = strchr(at, llvm::sys::PathSeparator);
98 }
99 if (*at == 0)
100 AddPath(".", Angled, false, true, false);
101 else
102 AddPath(at, Angled, false, true, false);
103}
104
Mike Stumpec057662009-10-09 20:16:49 +0000105void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(const std::string &Base,
106 const char *Arch) {
107 AddPath(Base, System, true, false, false);
108 AddPath(Base + "/" + Arch, System, true, false, false);
109 AddPath(Base + "/backward", System, true, false, false);
Rafael Espindola2e9f6522009-10-06 01:33:02 +0000110}
Nico Weber0fca0222008-08-22 09:25:22 +0000111
Mike Stump620d57a2009-10-12 20:50:45 +0000112void InitHeaderSearch::AddMinGWCPlusPlusIncludePaths(const std::string &Base,
113 const char *Arch,
114 const char *Version) {
115 std::string localBase = Base + "/" + Arch + "/" + Version + "/include";
116 AddPath(localBase, System, true, false, false);
117 AddPath(localBase + "/c++", System, true, false, false);
118 AddPath(localBase + "/c++/backward", System, true, false, false);
119}
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000120
Mike Stump620d57a2009-10-12 20:50:45 +0000121 // FIXME: This probably should goto to some platform utils place.
122#ifdef _MSC_VER
123 // Read registry string.
124bool getSystemRegistryString(const char *keyPath, const char *valueName,
Mike Stump43d81762009-10-08 23:29:47 +0000125 char *value, size_t maxLength) {
126 HKEY hRootKey = NULL;
127 HKEY hKey = NULL;
128 const char* subKey = NULL;
129 DWORD valueType;
130 DWORD valueSize = maxLength - 1;
131 bool returnValue = false;
132 if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
133 hRootKey = HKEY_CLASSES_ROOT;
134 subKey = keyPath + 18;
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000135 }
Mike Stump43d81762009-10-08 23:29:47 +0000136 else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
137 hRootKey = HKEY_USERS;
138 subKey = keyPath + 11;
139 }
140 else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
141 hRootKey = HKEY_LOCAL_MACHINE;
142 subKey = keyPath + 19;
143 }
144 else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
145 hRootKey = HKEY_CURRENT_USER;
146 subKey = keyPath + 18;
147 }
148 else
149 return(false);
150 long lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ, &hKey);
151 if (lResult == ERROR_SUCCESS) {
Mike Stump620d57a2009-10-12 20:50:45 +0000152 lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
153 (LPBYTE)value, &valueSize);
Mike Stump43d81762009-10-08 23:29:47 +0000154 if (lResult == ERROR_SUCCESS)
155 returnValue = true;
Mike Stump620d57a2009-10-12 20:50:45 +0000156 RegCloseKey(hKey);
Mike Stump43d81762009-10-08 23:29:47 +0000157 }
158 return(returnValue);
159}
Mike Stump620d57a2009-10-12 20:50:45 +0000160#else // _MSC_VER
161 // Read registry string.
162bool getSystemRegistryString(const char *, const char *, char *, size_t) {
163 return(false);
164}
165#endif // _MSC_VER
Argyrios Kyrtzidis121e3c22008-09-05 09:41:20 +0000166
Mike Stump43d81762009-10-08 23:29:47 +0000167 // Get Visual Studio installation directory.
168bool getVisualStudioDir(std::string &path) {
Mike Stump620d57a2009-10-12 20:50:45 +0000169 // Try the Windows registry first.
170 char vs80IDEInstallDir[256];
171 char vs90IDEInstallDir[256];
172 const char* vsIDEInstallDir = NULL;
173 bool has80 = getSystemRegistryString(
Mike Stump43d81762009-10-08 23:29:47 +0000174 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0",
Mike Stump620d57a2009-10-12 20:50:45 +0000175 "InstallDir", vs80IDEInstallDir, sizeof(vs80IDEInstallDir) - 1);
176 bool has90 = getSystemRegistryString(
Mike Stump43d81762009-10-08 23:29:47 +0000177 "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\9.0",
Mike Stump620d57a2009-10-12 20:50:45 +0000178 "InstallDir", vs90IDEInstallDir, sizeof(vs90IDEInstallDir) - 1);
Mike Stump43d81762009-10-08 23:29:47 +0000179 // If we have both vc80 and vc90, pick version we were compiled with.
180 if (has80 && has90) {
181 #ifdef _MSC_VER
182 #if (_MSC_VER >= 1500) // VC90
Mike Stump620d57a2009-10-12 20:50:45 +0000183 vsIDEInstallDir = vs90IDEInstallDir;
184 #elif (_MSC_VER == 1400) // VC80
185 vsIDEInstallDir = vs80IDEInstallDir;
186 #else
187 vsIDEInstallDir = vs90IDEInstallDir;
188 #endif
189 #else
190 vsIDEInstallDir = vs90IDEInstallDir;
191 #endif
192 }
193 else if (has90)
194 vsIDEInstallDir = vs90IDEInstallDir;
195 else if (has80)
196 vsIDEInstallDir = vs80IDEInstallDir;
197 if (vsIDEInstallDir && *vsIDEInstallDir) {
198 char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
199 if (p)
200 *p = '\0';
201 path = vsIDEInstallDir;
202 return(true);
203 }
204 else {
205 // Try the environment.
206 const char* vs90comntools = getenv("VS90COMNTOOLS");
207 const char* vs80comntools = getenv("VS80COMNTOOLS");
208 const char* vscomntools = NULL;
209 // If we have both vc80 and vc90, pick version we were compiled with.
210 if (vs90comntools && vs80comntools) {
211 #if (_MSC_VER >= 1500) // VC90
Mike Stump43d81762009-10-08 23:29:47 +0000212 vscomntools = vs90comntools;
213 #elif (_MSC_VER == 1400) // VC80
214 vscomntools = vs80comntools;
215 #else
216 vscomntools = vs90comntools;
217 #endif
Mike Stump620d57a2009-10-12 20:50:45 +0000218 }
219 else if (vs90comntools)
Mike Stump43d81762009-10-08 23:29:47 +0000220 vscomntools = vs90comntools;
Mike Stump620d57a2009-10-12 20:50:45 +0000221 else if (vs80comntools)
222 vscomntools = vs80comntools;
223 if (vscomntools && *vscomntools) {
224 char *p = (char*)strstr(vscomntools, "\\Common7\\Tools");
225 if (p)
226 *p = '\0';
227 path = vscomntools;
228 return(true);
229 }
230 else
231 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000232 }
Mike Stump620d57a2009-10-12 20:50:45 +0000233 return(false);
Mike Stump43d81762009-10-08 23:29:47 +0000234}
Mike Stump43d81762009-10-08 23:29:47 +0000235
236void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang,
Mike Stumpe85c74d2009-10-09 19:42:16 +0000237 const llvm::Triple &triple) {
Mike Stump43d81762009-10-08 23:29:47 +0000238 // FIXME: temporary hack: hard-coded paths.
239 llvm::Triple::OSType os = triple.getOS();
240
241 switch (os) {
242 case llvm::Triple::Win32:
243 {
Mike Stump620d57a2009-10-12 20:50:45 +0000244 std::string VSDir;
245 if (getVisualStudioDir(VSDir)) {
246 AddPath(VSDir + "\\VC\\include", System, false, false, false);
247 AddPath(VSDir + "\\VC\\PlatformSDK\\Include",
248 System, false, false, false);
249 }
250 else {
Mike Stump43d81762009-10-08 23:29:47 +0000251 // Default install paths.
Mike Stump620d57a2009-10-12 20:50:45 +0000252 AddPath("C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
Mike Stump43d81762009-10-08 23:29:47 +0000253 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000254 AddPath(
255 "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
Mike Stump43d81762009-10-08 23:29:47 +0000256 System, false, false, false);
Mike Stump620d57a2009-10-12 20:50:45 +0000257 AddPath("C:/Program Files/Microsoft Visual Studio 8/VC/include",
258 System, false, false, false);
259 AddPath(
260 "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include",
261 System, false, false, false);
262 // For some clang developers.
263 AddPath("G:/Program Files/Microsoft Visual Studio 9.0/VC/include",
264 System, false, false, false);
265 AddPath(
266 "G:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
267 System, false, false, false);
268 }
Mike Stump43d81762009-10-08 23:29:47 +0000269 }
270 break;
271 case llvm::Triple::Cygwin:
272 if (Lang.CPlusPlus) {
Mike Stump620d57a2009-10-12 20:50:45 +0000273 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include",
274 System, false, false, false);
275 AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++",
276 System, false, false, false);
Mike Stump43d81762009-10-08 23:29:47 +0000277 }
278 AddPath("/usr/include", System, false, false, false);
279 break;
Mike Stump43d81762009-10-08 23:29:47 +0000280 case llvm::Triple::MinGW64:
Mike Stump620d57a2009-10-12 20:50:45 +0000281 if (Lang.CPlusPlus) { // I'm guessing here.
282 // Try gcc 4.4.0
283 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.4.0");
284 // Try gcc 4.3.0
285 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw64", "4.3.0");
286 }
287 // Fall through.
288 case llvm::Triple::MinGW32:
Mike Stump43d81762009-10-08 23:29:47 +0000289 if (Lang.CPlusPlus) {
290 // Try gcc 4.4.0
Mike Stump620d57a2009-10-12 20:50:45 +0000291 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.4.0");
Mike Stump43d81762009-10-08 23:29:47 +0000292 // Try gcc 4.3.0
Mike Stump620d57a2009-10-12 20:50:45 +0000293 AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0");
Mike Stump43d81762009-10-08 23:29:47 +0000294 }
295 AddPath("c:/mingw/include", System, true, false, false);
296 break;
297 default:
298 if (Lang.CPlusPlus) {
299 switch (os) {
300 case llvm::Triple::Darwin:
301 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1",
302 "i686-apple-darwin10");
303 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0",
304 "i686-apple-darwin8");
305 break;
306 case llvm::Triple::Linux:
307 // Ubuntu 7.10 - Gutsy Gibbon
308 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3",
309 "i486-linux-gnu");
310 // Ubuntu 9.04
311 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3",
312 "x86_64-linux-gnu");
313 // Fedora 8
314 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2",
315 "i386-redhat-linux");
316 // Fedora 9
317 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0",
318 "i386-redhat-linux");
319 // Fedora 10
320 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2",
321 "i386-redhat-linux");
322 // openSUSE 11.1
323 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
324 "i586-suse-linux");
325 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
326 "x86_64-suse-linux");
327 // openSUSE 11.2
328 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
329 "i586-suse-linux");
330 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4",
331 "x86_64-suse-linux");
332 // Arch Linux 2008-06-24
333 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
334 "i686-pc-linux-gnu");
335 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1",
336 "x86_64-unknown-linux-gnu");
337 // Gentoo x86 2009.0 stable
338 AddGnuCPlusPlusIncludePaths(
339 "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4",
340 "i686-pc-linux-gnu");
341 // Gentoo x86 2008.0 stable
342 AddGnuCPlusPlusIncludePaths(
343 "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4",
344 "i686-pc-linux-gnu");
345 // Ubuntu 8.10
346 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3",
347 "i486-pc-linux-gnu");
348 // Gentoo amd64 stable
349 AddGnuCPlusPlusIncludePaths(
350 "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4",
351 "i686-pc-linux-gnu");
352 break;
353 case llvm::Triple::FreeBSD:
354 // DragonFly
355 AddPath("/usr/include/c++/4.1", System, true, false, false);
356 // FreeBSD
357 AddPath("/usr/include/c++/4.2", System, true, false, false);
358 break;
359 case llvm::Triple::Solaris:
360 // AuroraUX
Edward O'Callaghanff267202009-10-12 12:02:47 +0000361 AddGnuCPlusPlusIncludePaths("/opt/gcc4/include/c++/4.2.4",
Mike Stump43d81762009-10-08 23:29:47 +0000362 "i386-pc-solaris2.11");
363 break;
364 default:
365 break;
366 }
367 }
368
369 AddPath("/usr/local/include", System, false, false, false);
370
371 AddPath("/usr/include", System, false, false, false);
372 AddPath("/System/Library/Frameworks", System, true, false, true);
373 AddPath("/Library/Frameworks", System, true, false, true);
374 break;
375 }
Nico Weber0fca0222008-08-22 09:25:22 +0000376}
377
378void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) {
379 AddEnvVarPaths("CPATH");
380 if (Lang.CPlusPlus && Lang.ObjC1)
381 AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH");
382 else if (Lang.CPlusPlus)
383 AddEnvVarPaths("CPLUS_INCLUDE_PATH");
384 else if (Lang.ObjC1)
385 AddEnvVarPaths("OBJC_INCLUDE_PATH");
386 else
387 AddEnvVarPaths("C_INCLUDE_PATH");
388}
389
390
391/// RemoveDuplicates - If there are duplicate directory entries in the specified
392/// search list, remove the later (dead) ones.
393static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList,
394 bool Verbose) {
395 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs;
396 llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs;
397 llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps;
398 for (unsigned i = 0; i != SearchList.size(); ++i) {
Chris Lattner7a739402008-09-26 17:46:45 +0000399 unsigned DirToRemove = i;
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Chris Lattner43eee072009-02-08 01:00:10 +0000401 const DirectoryLookup &CurEntry = SearchList[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Chris Lattner43eee072009-02-08 01:00:10 +0000403 if (CurEntry.isNormalDir()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000404 // If this isn't the first time we've seen this dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000405 if (SeenDirs.insert(CurEntry.getDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000406 continue;
Chris Lattner43eee072009-02-08 01:00:10 +0000407 } else if (CurEntry.isFramework()) {
Nico Weber0fca0222008-08-22 09:25:22 +0000408 // If this isn't the first time we've seen this framework dir, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000409 if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir()))
Nico Weber0fca0222008-08-22 09:25:22 +0000410 continue;
Nico Weber0fca0222008-08-22 09:25:22 +0000411 } else {
Chris Lattner43eee072009-02-08 01:00:10 +0000412 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
Nico Weber0fca0222008-08-22 09:25:22 +0000413 // If this isn't the first time we've seen this headermap, remove it.
Chris Lattner43eee072009-02-08 01:00:10 +0000414 if (SeenHeaderMaps.insert(CurEntry.getHeaderMap()))
Nico Weber0fca0222008-08-22 09:25:22 +0000415 continue;
Chris Lattner30f05b52009-02-08 00:55:22 +0000416 }
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Chris Lattner30f05b52009-02-08 00:55:22 +0000418 // If we have a normal #include dir/framework/headermap that is shadowed
419 // later in the chain by a system include location, we actually want to
420 // ignore the user's request and drop the user dir... keeping the system
421 // dir. This is weird, but required to emulate GCC's search path correctly.
422 //
423 // Since dupes of system dirs are rare, just rescan to find the original
424 // that we're nuking instead of using a DenseMap.
Chris Lattner43eee072009-02-08 01:00:10 +0000425 if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) {
Chris Lattner30f05b52009-02-08 00:55:22 +0000426 // Find the dir that this is the same of.
427 unsigned FirstDir;
428 for (FirstDir = 0; ; ++FirstDir) {
429 assert(FirstDir != i && "Didn't find dupe?");
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Chris Lattner43eee072009-02-08 01:00:10 +0000431 const DirectoryLookup &SearchEntry = SearchList[FirstDir];
432
Chris Lattner30f05b52009-02-08 00:55:22 +0000433 // If these are different lookup types, then they can't be the dupe.
Chris Lattner43eee072009-02-08 01:00:10 +0000434 if (SearchEntry.getLookupType() != CurEntry.getLookupType())
Chris Lattner30f05b52009-02-08 00:55:22 +0000435 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Chris Lattner30f05b52009-02-08 00:55:22 +0000437 bool isSame;
Chris Lattner43eee072009-02-08 01:00:10 +0000438 if (CurEntry.isNormalDir())
439 isSame = SearchEntry.getDir() == CurEntry.getDir();
440 else if (CurEntry.isFramework())
441 isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir();
Chris Lattner30f05b52009-02-08 00:55:22 +0000442 else {
Chris Lattner43eee072009-02-08 01:00:10 +0000443 assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?");
444 isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap();
Chris Lattner30f05b52009-02-08 00:55:22 +0000445 }
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattner30f05b52009-02-08 00:55:22 +0000447 if (isSame)
448 break;
449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Chris Lattner30f05b52009-02-08 00:55:22 +0000451 // If the first dir in the search path is a non-system dir, zap it
452 // instead of the system one.
453 if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User)
454 DirToRemove = FirstDir;
455 }
456
457 if (Verbose) {
Chris Lattner43eee072009-02-08 01:00:10 +0000458 fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
459 CurEntry.getName());
Chris Lattner30f05b52009-02-08 00:55:22 +0000460 if (DirToRemove != i)
461 fprintf(stderr, " as it is a non-system directory that duplicates"
462 " a system directory\n");
Nico Weber0fca0222008-08-22 09:25:22 +0000463 }
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Chris Lattner7a739402008-09-26 17:46:45 +0000465 // This is reached if the current entry is a duplicate. Remove the
466 // DirToRemove (usually the current dir).
467 SearchList.erase(SearchList.begin()+DirToRemove);
Nico Weber0fca0222008-08-22 09:25:22 +0000468 --i;
469 }
470}
471
472
473void InitHeaderSearch::Realize() {
474 // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
475 std::vector<DirectoryLookup> SearchList;
476 SearchList = IncludeGroup[Angled];
477 SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
478 IncludeGroup[System].end());
479 SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
480 IncludeGroup[After].end());
481 RemoveDuplicates(SearchList, Verbose);
482 RemoveDuplicates(IncludeGroup[Quoted], Verbose);
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Nico Weber0fca0222008-08-22 09:25:22 +0000484 // Prepend QUOTED list on the search list.
Mike Stump1eb44332009-09-09 15:08:12 +0000485 SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
Nico Weber0fca0222008-08-22 09:25:22 +0000486 IncludeGroup[Quoted].end());
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Nico Weber0fca0222008-08-22 09:25:22 +0000488
489 bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
490 Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
491 DontSearchCurDir);
492
493 // If verbose, print the list of directories that will be searched.
494 if (Verbose) {
495 fprintf(stderr, "#include \"...\" search starts here:\n");
496 unsigned QuotedIdx = IncludeGroup[Quoted].size();
497 for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
498 if (i == QuotedIdx)
499 fprintf(stderr, "#include <...> search starts here:\n");
500 const char *Name = SearchList[i].getName();
501 const char *Suffix;
502 if (SearchList[i].isNormalDir())
503 Suffix = "";
504 else if (SearchList[i].isFramework())
505 Suffix = " (framework directory)";
506 else {
507 assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup");
508 Suffix = " (headermap)";
509 }
510 fprintf(stderr, " %s%s\n", Name, Suffix);
511 }
512 fprintf(stderr, "End of search list.\n");
513 }
514}