Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/InitHeaderSearch.h" |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 15 | #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 Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 21 | #include "llvm/System/Path.h" |
Argyrios Kyrtzidis | 121e3c2 | 2008-09-05 09:41:20 +0000 | [diff] [blame] | 22 | #include "llvm/Config/config.h" |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 23 | #include <cstdio> |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 26 | void InitHeaderSearch::AddPath(const llvm::StringRef &Path, |
| 27 | IncludeDirGroup Group, bool isCXXAware, |
| 28 | bool isUserSupplied, bool isFramework, |
| 29 | bool IgnoreSysRoot) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 30 | assert(!Path.empty() && "can't handle empty path here"); |
| 31 | FileManager &FM = Headers.getFileMgr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 33 | // Compute the actual path, taking into consideration -isysroot. |
| 34 | llvm::SmallString<256> MappedPath; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 36 | // Handle isysroot. |
Chris Lattner | 6858dd3 | 2009-02-19 06:48:28 +0000 | [diff] [blame] | 37 | if (Group == System && !IgnoreSysRoot) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 38 | // FIXME: Portability. This should be a sys::Path interface, this doesn't |
| 39 | // handle things like C:\ right, nor win32 \\network\device\blah. |
| 40 | if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present. |
| 41 | MappedPath.append(isysroot.begin(), isysroot.end()); |
| 42 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 44 | MappedPath.append(Path.begin(), Path.end()); |
| 45 | |
| 46 | // Compute the DirectoryLookup type. |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 47 | SrcMgr::CharacteristicKind Type; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 48 | if (Group == Quoted || Group == Angled) |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 49 | Type = SrcMgr::C_User; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 50 | else if (isCXXAware) |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 51 | Type = SrcMgr::C_System; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 52 | else |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 53 | Type = SrcMgr::C_ExternCSystem; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
| 55 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 56 | // If the directory exists, add it. |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 57 | if (const DirectoryEntry *DE = FM.getDirectory(MappedPath.str())) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 58 | IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied, |
| 59 | isFramework)); |
| 60 | return; |
| 61 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 63 | // Check to see if this is an apple-style headermap (which are not allowed to |
| 64 | // be frameworks). |
| 65 | if (!isFramework) { |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 66 | if (const FileEntry *FE = FM.getFile(MappedPath.str())) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 67 | if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) { |
| 68 | // It is a headermap, add it to the search path. |
| 69 | IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied)); |
| 70 | return; |
| 71 | } |
| 72 | } |
| 73 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 75 | if (Verbose) |
Daniel Dunbar | 7765934 | 2009-08-19 20:04:03 +0000 | [diff] [blame] | 76 | llvm::errs() << "ignoring nonexistent directory \"" |
| 77 | << MappedPath.str() << "\"\n"; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | |
| 81 | void InitHeaderSearch::AddEnvVarPaths(const char *Name) { |
| 82 | const char* at = getenv(Name); |
Daniel Dunbar | bb95255 | 2008-10-04 20:58:18 +0000 | [diff] [blame] | 83 | if (!at || *at == 0) // Empty string should not add '.' path. |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 84 | return; |
| 85 | |
| 86 | const char* delim = strchr(at, llvm::sys::PathSeparator); |
| 87 | while (delim != 0) { |
| 88 | if (delim-at == 0) |
| 89 | AddPath(".", Angled, false, true, false); |
| 90 | else |
Benjamin Kramer | 458fb10 | 2009-09-05 09:49:39 +0000 | [diff] [blame] | 91 | AddPath(llvm::StringRef(at, delim-at), Angled, false, true, false); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 92 | at = delim + 1; |
| 93 | delim = strchr(at, llvm::sys::PathSeparator); |
| 94 | } |
| 95 | if (*at == 0) |
| 96 | AddPath(".", Angled, false, true, false); |
| 97 | else |
| 98 | AddPath(at, Angled, false, true, false); |
| 99 | } |
| 100 | |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 101 | void InitHeaderSearch::AddGnuCPlusPlusIncludePaths(std::string base, |
| 102 | std::string arch) { |
| 103 | AddPath(base, System, true, false, false); |
| 104 | AddPath(base + "/" + arch, System, true, false, false); |
| 105 | AddPath(base + "/backward", System, true, false, false); |
| 106 | } |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 107 | |
| 108 | void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang) { |
| 109 | // FIXME: temporary hack: hard-coded paths. |
| 110 | // FIXME: get these from the target? |
Argyrios Kyrtzidis | 121e3c2 | 2008-09-05 09:41:20 +0000 | [diff] [blame] | 111 | |
| 112 | #ifdef LLVM_ON_WIN32 |
| 113 | if (Lang.CPlusPlus) { |
| 114 | // Mingw32 GCC version 4 |
Chris Lattner | 6f54102 | 2009-02-18 00:25:15 +0000 | [diff] [blame] | 115 | AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++", |
| 116 | System, true, false, false); |
| 117 | AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/mingw32", |
| 118 | System, true, false, false); |
| 119 | AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/backward", |
| 120 | System, true, false, false); |
Argyrios Kyrtzidis | 121e3c2 | 2008-09-05 09:41:20 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // Mingw32 GCC version 4 |
Argyrios Kyrtzidis | 121e3c2 | 2008-09-05 09:41:20 +0000 | [diff] [blame] | 124 | AddPath("C:/mingw/include", System, false, false, false); |
| 125 | #else |
| 126 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 127 | if (Lang.CPlusPlus) { |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 128 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", |
| 129 | "i686-apple-darwin10"); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 130 | |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 131 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0", "i686-apple-darwin8"); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 132 | |
| 133 | // Ubuntu 7.10 - Gutsy Gibbon |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 134 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.3", "i486-linux-gnu"); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 135 | |
Douglas Gregor | 15e9232 | 2009-06-17 21:18:36 +0000 | [diff] [blame] | 136 | // Ubuntu 9.04 |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 137 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.3", "x86_64-linux-gnu"); |
Douglas Gregor | 15e9232 | 2009-06-17 21:18:36 +0000 | [diff] [blame] | 138 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 139 | // Fedora 8 |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 140 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.1.2", "i386-redhat-linux"); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 141 | |
| 142 | // Fedora 9 |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 143 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.0", "i386-redhat-linux"); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 144 | |
Zhongxing Xu | 776caef | 2008-12-25 09:28:01 +0000 | [diff] [blame] | 145 | // Fedora 10 |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 146 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.2", "i386-redhat-linux"); |
Zhongxing Xu | 776caef | 2008-12-25 09:28:01 +0000 | [diff] [blame] | 147 | |
Chris Lattner | ef888a4 | 2009-08-07 05:28:24 +0000 | [diff] [blame] | 148 | // openSUSE 11.1 |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 149 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", "i586-suse-linux"); |
| 150 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", "x86_64-suse-linux"); |
Chris Lattner | ef888a4 | 2009-08-07 05:28:24 +0000 | [diff] [blame] | 151 | |
| 152 | // openSUSE 11.2 |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 153 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", "i586-suse-linux"); |
| 154 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4", "x86_64-suse-linux"); |
Chris Lattner | ef888a4 | 2009-08-07 05:28:24 +0000 | [diff] [blame] | 155 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 156 | // Arch Linux 2008-06-24 |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 157 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1", "i686-pc-linux-gnu"); |
| 158 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3.1", |
| 159 | "x86_64-unknown-linux-gnu"); |
Chris Lattner | 5654ffd | 2008-08-23 18:25:07 +0000 | [diff] [blame] | 160 | |
Nuno Lopes | ec9fd76 | 2009-07-26 16:14:05 +0000 | [diff] [blame] | 161 | // Gentoo x86 2009.0 stable |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 162 | AddGnuCPlusPlusIncludePaths( |
| 163 | "/usr/lib/gcc/i686-pc-linux-gnu/4.3.2/include/g++-v4", |
| 164 | "i686-pc-linux-gnu"); |
Nuno Lopes | ec9fd76 | 2009-07-26 16:14:05 +0000 | [diff] [blame] | 165 | |
| 166 | // Gentoo x86 2008.0 stable |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 167 | AddGnuCPlusPlusIncludePaths( |
| 168 | "/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4", |
| 169 | "i686-pc-linux-gnu"); |
Nuno Lopes | a3d783b | 2008-12-07 12:11:37 +0000 | [diff] [blame] | 170 | |
Eli Friedman | 03d6b6e | 2009-08-15 03:45:14 +0000 | [diff] [blame] | 171 | // Ubuntu 8.10 |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 172 | AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", "i486-pc-linux-gnu"); |
Eli Friedman | 03d6b6e | 2009-08-15 03:45:14 +0000 | [diff] [blame] | 173 | |
Sebastian Redl | 4d374d4 | 2009-07-01 18:59:43 +0000 | [diff] [blame] | 174 | // Gentoo amd64 stable |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 175 | AddGnuCPlusPlusIncludePaths( |
| 176 | "/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4", |
| 177 | "i686-pc-linux-gnu"); |
Sebastian Redl | 4d374d4 | 2009-07-01 18:59:43 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 5654ffd | 2008-08-23 18:25:07 +0000 | [diff] [blame] | 179 | // DragonFly |
| 180 | AddPath("/usr/include/c++/4.1", System, true, false, false); |
Chris Lattner | 01e4b5c | 2009-02-25 18:06:37 +0000 | [diff] [blame] | 181 | |
| 182 | // FreeBSD |
| 183 | AddPath("/usr/include/c++/4.2", System, true, false, false); |
Eli Friedman | 868d016 | 2009-08-01 17:10:21 +0000 | [diff] [blame] | 184 | |
| 185 | // AuroraUX |
Rafael Espindola | 2e9f652 | 2009-10-06 01:33:02 +0000 | [diff] [blame] | 186 | AddGnuCPlusPlusIncludePaths("/Opt/gcc4/include/c++/4.2.4", |
| 187 | "i386-pc-solaris2.11"); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | AddPath("/usr/local/include", System, false, false, false); |
| 191 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 192 | AddPath("/usr/include", System, false, false, false); |
| 193 | AddPath("/System/Library/Frameworks", System, true, false, true); |
| 194 | AddPath("/Library/Frameworks", System, true, false, true); |
Argyrios Kyrtzidis | 121e3c2 | 2008-09-05 09:41:20 +0000 | [diff] [blame] | 195 | #endif |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) { |
| 199 | AddEnvVarPaths("CPATH"); |
| 200 | if (Lang.CPlusPlus && Lang.ObjC1) |
| 201 | AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH"); |
| 202 | else if (Lang.CPlusPlus) |
| 203 | AddEnvVarPaths("CPLUS_INCLUDE_PATH"); |
| 204 | else if (Lang.ObjC1) |
| 205 | AddEnvVarPaths("OBJC_INCLUDE_PATH"); |
| 206 | else |
| 207 | AddEnvVarPaths("C_INCLUDE_PATH"); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /// RemoveDuplicates - If there are duplicate directory entries in the specified |
| 212 | /// search list, remove the later (dead) ones. |
| 213 | static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList, |
| 214 | bool Verbose) { |
| 215 | llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs; |
| 216 | llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs; |
| 217 | llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps; |
| 218 | for (unsigned i = 0; i != SearchList.size(); ++i) { |
Chris Lattner | 7a73940 | 2008-09-26 17:46:45 +0000 | [diff] [blame] | 219 | unsigned DirToRemove = i; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 221 | const DirectoryLookup &CurEntry = SearchList[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 223 | if (CurEntry.isNormalDir()) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 224 | // If this isn't the first time we've seen this dir, remove it. |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 225 | if (SeenDirs.insert(CurEntry.getDir())) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 226 | continue; |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 227 | } else if (CurEntry.isFramework()) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 228 | // If this isn't the first time we've seen this framework dir, remove it. |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 229 | if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir())) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 230 | continue; |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 231 | } else { |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 232 | assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 233 | // If this isn't the first time we've seen this headermap, remove it. |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 234 | if (SeenHeaderMaps.insert(CurEntry.getHeaderMap())) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 235 | continue; |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 236 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 238 | // If we have a normal #include dir/framework/headermap that is shadowed |
| 239 | // later in the chain by a system include location, we actually want to |
| 240 | // ignore the user's request and drop the user dir... keeping the system |
| 241 | // dir. This is weird, but required to emulate GCC's search path correctly. |
| 242 | // |
| 243 | // Since dupes of system dirs are rare, just rescan to find the original |
| 244 | // that we're nuking instead of using a DenseMap. |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 245 | if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) { |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 246 | // Find the dir that this is the same of. |
| 247 | unsigned FirstDir; |
| 248 | for (FirstDir = 0; ; ++FirstDir) { |
| 249 | assert(FirstDir != i && "Didn't find dupe?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 251 | const DirectoryLookup &SearchEntry = SearchList[FirstDir]; |
| 252 | |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 253 | // If these are different lookup types, then they can't be the dupe. |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 254 | if (SearchEntry.getLookupType() != CurEntry.getLookupType()) |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 255 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 257 | bool isSame; |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 258 | if (CurEntry.isNormalDir()) |
| 259 | isSame = SearchEntry.getDir() == CurEntry.getDir(); |
| 260 | else if (CurEntry.isFramework()) |
| 261 | isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir(); |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 262 | else { |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 263 | assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); |
| 264 | isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap(); |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 265 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 266 | |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 267 | if (isSame) |
| 268 | break; |
| 269 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 271 | // If the first dir in the search path is a non-system dir, zap it |
| 272 | // instead of the system one. |
| 273 | if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User) |
| 274 | DirToRemove = FirstDir; |
| 275 | } |
| 276 | |
| 277 | if (Verbose) { |
Chris Lattner | 43eee07 | 2009-02-08 01:00:10 +0000 | [diff] [blame] | 278 | fprintf(stderr, "ignoring duplicate directory \"%s\"\n", |
| 279 | CurEntry.getName()); |
Chris Lattner | 30f05b5 | 2009-02-08 00:55:22 +0000 | [diff] [blame] | 280 | if (DirToRemove != i) |
| 281 | fprintf(stderr, " as it is a non-system directory that duplicates" |
| 282 | " a system directory\n"); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 283 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | |
Chris Lattner | 7a73940 | 2008-09-26 17:46:45 +0000 | [diff] [blame] | 285 | // This is reached if the current entry is a duplicate. Remove the |
| 286 | // DirToRemove (usually the current dir). |
| 287 | SearchList.erase(SearchList.begin()+DirToRemove); |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 288 | --i; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | |
| 293 | void InitHeaderSearch::Realize() { |
| 294 | // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList. |
| 295 | std::vector<DirectoryLookup> SearchList; |
| 296 | SearchList = IncludeGroup[Angled]; |
| 297 | SearchList.insert(SearchList.end(), IncludeGroup[System].begin(), |
| 298 | IncludeGroup[System].end()); |
| 299 | SearchList.insert(SearchList.end(), IncludeGroup[After].begin(), |
| 300 | IncludeGroup[After].end()); |
| 301 | RemoveDuplicates(SearchList, Verbose); |
| 302 | RemoveDuplicates(IncludeGroup[Quoted], Verbose); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 304 | // Prepend QUOTED list on the search list. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 305 | SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(), |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 306 | IncludeGroup[Quoted].end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 308 | |
| 309 | bool DontSearchCurDir = false; // TODO: set to true if -I- is set? |
| 310 | Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(), |
| 311 | DontSearchCurDir); |
| 312 | |
| 313 | // If verbose, print the list of directories that will be searched. |
| 314 | if (Verbose) { |
| 315 | fprintf(stderr, "#include \"...\" search starts here:\n"); |
| 316 | unsigned QuotedIdx = IncludeGroup[Quoted].size(); |
| 317 | for (unsigned i = 0, e = SearchList.size(); i != e; ++i) { |
| 318 | if (i == QuotedIdx) |
| 319 | fprintf(stderr, "#include <...> search starts here:\n"); |
| 320 | const char *Name = SearchList[i].getName(); |
| 321 | const char *Suffix; |
| 322 | if (SearchList[i].isNormalDir()) |
| 323 | Suffix = ""; |
| 324 | else if (SearchList[i].isFramework()) |
| 325 | Suffix = " (framework directory)"; |
| 326 | else { |
| 327 | assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup"); |
| 328 | Suffix = " (headermap)"; |
| 329 | } |
| 330 | fprintf(stderr, " %s%s\n", Name, Suffix); |
| 331 | } |
| 332 | fprintf(stderr, "End of search list.\n"); |
| 333 | } |
| 334 | } |