Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- HeaderSearch.cpp - Resolve Header File Locations ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the DirectoryLookup and HeaderSearch interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 14 | #include "clang/Lex/HeaderSearch.h" |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 15 | #include "clang/Lex/HeaderMap.h" |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 17 | #include "clang/Basic/FileManager.h" |
| 18 | #include "clang/Basic/IdentifierTable.h" |
Michael J. Spencer | 32bef4e | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 19 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Path.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | eabea45 | 2011-07-27 18:41:18 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Capacity.h" |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 23 | #include <cstdio> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 26 | const IdentifierInfo * |
| 27 | HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) { |
| 28 | if (ControllingMacro) |
| 29 | return ControllingMacro; |
| 30 | |
| 31 | if (!ControllingMacroID || !External) |
| 32 | return 0; |
| 33 | |
| 34 | ControllingMacro = External->GetIdentifier(ControllingMacroID); |
| 35 | return ControllingMacro; |
| 36 | } |
| 37 | |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 38 | ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {} |
| 39 | |
Douglas Gregor | 8e23806 | 2011-11-11 00:35:06 +0000 | [diff] [blame] | 40 | HeaderSearch::HeaderSearch(FileManager &FM, DiagnosticsEngine &Diags) |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 41 | : FileMgr(FM), Diags(Diags), FrameworkMap(64), |
| 42 | ModMap(FileMgr, *Diags.getClient()) |
Douglas Gregor | 8e23806 | 2011-11-11 00:35:06 +0000 | [diff] [blame] | 43 | { |
Nico Weber | 74a5fd8 | 2011-05-24 04:31:14 +0000 | [diff] [blame] | 44 | AngledDirIdx = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | SystemDirIdx = 0; |
| 46 | NoCurDirSearch = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 48 | ExternalLookup = 0; |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 49 | ExternalSource = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | NumIncluded = 0; |
| 51 | NumMultiIncludeFileOptzn = 0; |
| 52 | NumFrameworkLookups = NumSubFrameworkLookups = 0; |
| 53 | } |
| 54 | |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 55 | HeaderSearch::~HeaderSearch() { |
| 56 | // Delete headermaps. |
| 57 | for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) |
| 58 | delete HeaderMaps[i].second; |
| 59 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | void HeaderSearch::PrintStats() { |
| 62 | fprintf(stderr, "\n*** HeaderSearch Stats:\n"); |
| 63 | fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size()); |
| 64 | unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0; |
| 65 | for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) { |
| 66 | NumOnceOnlyFiles += FileInfo[i].isImport; |
| 67 | if (MaxNumIncludes < FileInfo[i].NumIncludes) |
| 68 | MaxNumIncludes = FileInfo[i].NumIncludes; |
| 69 | NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1; |
| 70 | } |
| 71 | fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles); |
| 72 | fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles); |
| 73 | fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded); |
| 76 | fprintf(stderr, " %d #includes skipped due to" |
| 77 | " the multi-include optimization.\n", NumMultiIncludeFileOptzn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 79 | fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups); |
| 80 | fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups); |
| 81 | } |
| 82 | |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 83 | /// CreateHeaderMap - This method returns a HeaderMap for the specified |
| 84 | /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure. |
Chris Lattner | 1bfd4a6 | 2007-12-17 18:34:53 +0000 | [diff] [blame] | 85 | const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) { |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 86 | // We expect the number of headermaps to be small, and almost always empty. |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 87 | // If it ever grows, use of a linear search should be re-evaluated. |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 88 | if (!HeaderMaps.empty()) { |
| 89 | for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 90 | // Pointer equality comparison of FileEntries works because they are |
| 91 | // already uniqued by inode. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | if (HeaderMaps[i].first == FE) |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 93 | return HeaderMaps[i].second; |
| 94 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 96 | if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) { |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 97 | HeaderMaps.push_back(std::make_pair(FE, HM)); |
| 98 | return HM; |
| 99 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 104 | const FileEntry *HeaderSearch::lookupModule(StringRef ModuleName, |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 105 | std::string *ModuleFileName, |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 106 | std::string *UmbrellaHeader) { |
Douglas Gregor | 9a6da69 | 2011-09-12 20:41:59 +0000 | [diff] [blame] | 107 | // If we don't have a module cache path, we can't do anything. |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 108 | if (ModuleCachePath.empty()) { |
| 109 | if (ModuleFileName) |
| 110 | ModuleFileName->clear(); |
Douglas Gregor | 9a6da69 | 2011-09-12 20:41:59 +0000 | [diff] [blame] | 111 | return 0; |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 114 | // Try to find the module path. |
Douglas Gregor | 9a6da69 | 2011-09-12 20:41:59 +0000 | [diff] [blame] | 115 | llvm::SmallString<256> FileName(ModuleCachePath); |
| 116 | llvm::sys::path::append(FileName, ModuleName + ".pcm"); |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 117 | if (ModuleFileName) |
| 118 | *ModuleFileName = FileName.str(); |
| 119 | |
| 120 | if (const FileEntry *ModuleFile |
| 121 | = getFileMgr().getFile(FileName, /*OpenFile=*/false, |
| 122 | /*CacheFailure=*/false)) |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 123 | return ModuleFile; |
| 124 | |
| 125 | // We didn't find the module. If we're not supposed to look for an |
| 126 | // umbrella header, this is the end of the road. |
| 127 | if (!UmbrellaHeader) |
| 128 | return 0; |
| 129 | |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 130 | // Look in the module map to determine if there is a module by this name. |
| 131 | ModuleMap::Module *Module = ModMap.findModule(ModuleName); |
| 132 | if (!Module) { |
| 133 | // Look through the various header search paths to load any avaiable module |
| 134 | // maps, searching for a module map that describes this module. |
| 135 | for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { |
| 136 | // Skip non-normal include paths |
| 137 | if (!SearchDirs[Idx].isNormalDir()) |
| 138 | continue; |
| 139 | |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 140 | // Search for a module map file in this directory. |
| 141 | if (loadModuleMapFile(SearchDirs[Idx].getDir()) == LMM_NewlyLoaded) { |
| 142 | // We just loaded a module map file; check whether the module is |
| 143 | // available now. |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 144 | Module = ModMap.findModule(ModuleName); |
| 145 | if (Module) |
| 146 | break; |
| 147 | } |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 148 | |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 149 | // Search for a module map in a subdirectory with the same name as the |
| 150 | // module. |
| 151 | llvm::SmallString<128> NestedModuleMapDirName; |
| 152 | NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName(); |
| 153 | llvm::sys::path::append(NestedModuleMapDirName, ModuleName); |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 154 | if (loadModuleMapFile(NestedModuleMapDirName) == LMM_NewlyLoaded) { |
| 155 | // If we just loaded a module map file, look for the module again. |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 156 | Module = ModMap.findModule(ModuleName); |
| 157 | if (Module) |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // If we have a module with an umbrella header |
Douglas Gregor | 484535e | 2011-11-11 23:20:24 +0000 | [diff] [blame] | 164 | // FIXME: Even if it doesn't have an umbrella header, we should be able to |
| 165 | // handle the module. However, the caller isn't ready for that yet. |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 166 | if (Module && Module->UmbrellaHeader) { |
| 167 | *UmbrellaHeader = Module->UmbrellaHeader->getName(); |
| 168 | return 0; |
Douglas Gregor | 484535e | 2011-11-11 23:20:24 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 171 | // Look in each of the framework directories for an umbrella header with |
| 172 | // the same name as the module. |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 173 | llvm::SmallString<128> UmbrellaHeaderName; |
| 174 | UmbrellaHeaderName = ModuleName; |
| 175 | UmbrellaHeaderName += '/'; |
| 176 | UmbrellaHeaderName += ModuleName; |
| 177 | UmbrellaHeaderName += ".h"; |
| 178 | for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { |
| 179 | // Skip non-framework include paths |
| 180 | if (!SearchDirs[Idx].isFramework()) |
| 181 | continue; |
| 182 | |
| 183 | // Look for the umbrella header in this directory. |
| 184 | if (const FileEntry *HeaderFile |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 185 | = SearchDirs[Idx].LookupFile(UmbrellaHeaderName, *this, 0, 0, |
| 186 | StringRef(), 0)) { |
Douglas Gregor | 21cae20 | 2011-09-12 23:31:24 +0000 | [diff] [blame] | 187 | *UmbrellaHeader = HeaderFile->getName(); |
| 188 | return 0; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // We did not find an umbrella header. Clear out the UmbrellaHeader pointee |
| 193 | // so our caller knows that we failed. |
| 194 | UmbrellaHeader->clear(); |
| 195 | return 0; |
Douglas Gregor | 9a6da69 | 2011-09-12 20:41:59 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 198 | //===----------------------------------------------------------------------===// |
| 199 | // File lookup within a DirectoryLookup scope |
| 200 | //===----------------------------------------------------------------------===// |
| 201 | |
Chris Lattner | 3af66a9 | 2007-12-17 17:57:27 +0000 | [diff] [blame] | 202 | /// getName - Return the directory or filename corresponding to this lookup |
| 203 | /// object. |
| 204 | const char *DirectoryLookup::getName() const { |
| 205 | if (isNormalDir()) |
| 206 | return getDir()->getName(); |
| 207 | if (isFramework()) |
| 208 | return getFrameworkDir()->getName(); |
| 209 | assert(isHeaderMap() && "Unknown DirectoryLookup"); |
| 210 | return getHeaderMap()->getFileName(); |
| 211 | } |
| 212 | |
| 213 | |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 214 | /// LookupFile - Lookup the specified file in this search path, returning it |
| 215 | /// if it exists or returning null if not. |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 216 | const FileEntry *DirectoryLookup::LookupFile( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 217 | StringRef Filename, |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 218 | HeaderSearch &HS, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 219 | SmallVectorImpl<char> *SearchPath, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 220 | SmallVectorImpl<char> *RelativePath, |
| 221 | StringRef BuildingModule, |
| 222 | StringRef *SuggestedModule) const { |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 223 | llvm::SmallString<1024> TmpDir; |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 224 | if (isNormalDir()) { |
| 225 | // Concatenate the requested file onto the directory. |
Eli Friedman | a6e023c | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 226 | TmpDir = getDir()->getName(); |
| 227 | llvm::sys::path::append(TmpDir, Filename); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 228 | if (SearchPath != NULL) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 229 | StringRef SearchPathRef(getDir()->getName()); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 230 | SearchPath->clear(); |
| 231 | SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); |
| 232 | } |
| 233 | if (RelativePath != NULL) { |
| 234 | RelativePath->clear(); |
| 235 | RelativePath->append(Filename.begin(), Filename.end()); |
| 236 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 237 | |
| 238 | // If we have a module map that might map this header, load it and |
| 239 | // check whether we'll have a suggestion for a module. |
| 240 | if (SuggestedModule && HS.hasModuleMap(TmpDir, getDir())) { |
| 241 | const FileEntry *File = HS.getFileMgr().getFile(TmpDir.str(), |
| 242 | /*openFile=*/false); |
| 243 | if (!File) |
| 244 | return File; |
| 245 | |
| 246 | // If there is a module that corresponds to this header, |
| 247 | // suggest it. |
Douglas Gregor | 65f3b5e | 2011-11-11 22:18:48 +0000 | [diff] [blame] | 248 | StringRef Module = HS.findModuleForHeader(File); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 249 | if (!Module.empty() && Module != BuildingModule) |
| 250 | *SuggestedModule = Module; |
| 251 | |
| 252 | return File; |
| 253 | } |
| 254 | |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 255 | return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true); |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 256 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 258 | if (isFramework()) |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 259 | return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath, |
| 260 | BuildingModule, SuggestedModule); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | |
Chris Lattner | b09e71f | 2007-12-17 08:17:39 +0000 | [diff] [blame] | 262 | assert(isHeaderMap() && "Unknown directory lookup"); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 263 | const FileEntry * const Result = getHeaderMap()->LookupFile( |
| 264 | Filename, HS.getFileMgr()); |
| 265 | if (Result) { |
| 266 | if (SearchPath != NULL) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 267 | StringRef SearchPathRef(getName()); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 268 | SearchPath->clear(); |
| 269 | SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); |
| 270 | } |
| 271 | if (RelativePath != NULL) { |
| 272 | RelativePath->clear(); |
| 273 | RelativePath->append(Filename.begin(), Filename.end()); |
| 274 | } |
| 275 | } |
| 276 | return Result; |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 280 | /// DoFrameworkLookup - Do a lookup of the specified file in the current |
| 281 | /// DirectoryLookup, which is a framework directory. |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 282 | const FileEntry *DirectoryLookup::DoFrameworkLookup( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 283 | StringRef Filename, |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 284 | HeaderSearch &HS, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 285 | SmallVectorImpl<char> *SearchPath, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 286 | SmallVectorImpl<char> *RelativePath, |
| 287 | StringRef BuildingModule, |
| 288 | StringRef *SuggestedModule) const |
| 289 | { |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 290 | FileManager &FileMgr = HS.getFileMgr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 292 | // Framework names must have a '/' in the filename. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 293 | size_t SlashPos = Filename.find('/'); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 294 | if (SlashPos == StringRef::npos) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 296 | // Find out if this is the home for the specified framework, by checking |
| 297 | // HeaderSearch. Possible answer are yes/no and unknown. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | const DirectoryEntry *&FrameworkDirCache = |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 299 | HS.LookupFrameworkCache(Filename.substr(0, SlashPos)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 301 | // If it is known and in some other directory, fail. |
| 302 | if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 303 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 305 | // Otherwise, construct the path to this framework dir. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | // FrameworkName = "/System/Library/Frameworks/" |
| 308 | llvm::SmallString<1024> FrameworkName; |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 309 | FrameworkName += getFrameworkDir()->getName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 310 | if (FrameworkName.empty() || FrameworkName.back() != '/') |
| 311 | FrameworkName.push_back('/'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 313 | // FrameworkName = "/System/Library/Frameworks/Cocoa" |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 314 | FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 316 | // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/" |
| 317 | FrameworkName += ".framework/"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 319 | // If the cache entry is still unresolved, query to see if the cache entry is |
| 320 | // still unresolved. If so, check its existence now. |
| 321 | if (FrameworkDirCache == 0) { |
| 322 | HS.IncrementFrameworkLookupCount(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 324 | // If the framework dir doesn't exist, we fail. |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 325 | // FIXME: It's probably more efficient to query this with FileMgr.getDir. |
Michael J. Spencer | 32bef4e | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 326 | bool Exists; |
| 327 | if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 328 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 330 | // Otherwise, if it does, remember that this is the right direntry for this |
| 331 | // framework. |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 332 | FrameworkDirCache = getFrameworkDir(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 333 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 335 | if (RelativePath != NULL) { |
| 336 | RelativePath->clear(); |
| 337 | RelativePath->append(Filename.begin()+SlashPos+1, Filename.end()); |
| 338 | } |
| 339 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h" |
| 341 | unsigned OrigSize = FrameworkName.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 343 | FrameworkName += "Headers/"; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 344 | |
| 345 | if (SearchPath != NULL) { |
| 346 | SearchPath->clear(); |
| 347 | // Without trailing '/'. |
| 348 | SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1); |
| 349 | } |
| 350 | |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 351 | /// Determine whether this is the module we're building or not. |
| 352 | bool AutomaticImport = SuggestedModule && |
Douglas Gregor | 0fd787b | 2011-09-16 00:22:46 +0000 | [diff] [blame] | 353 | (BuildingModule != StringRef(Filename.begin(), SlashPos)) && |
| 354 | !Filename.substr(SlashPos + 1).startswith(".."); |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 355 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 356 | FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end()); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 357 | if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(), |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 358 | /*openFile=*/!AutomaticImport)) { |
| 359 | if (AutomaticImport) |
| 360 | *SuggestedModule = StringRef(Filename.begin(), SlashPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 361 | return FE; |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 362 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 364 | // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h" |
| 365 | const char *Private = "Private"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | FrameworkName.insert(FrameworkName.begin()+OrigSize, Private, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 367 | Private+strlen(Private)); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 368 | if (SearchPath != NULL) |
| 369 | SearchPath->insert(SearchPath->begin()+OrigSize, Private, |
| 370 | Private+strlen(Private)); |
| 371 | |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 372 | const FileEntry *FE = FileMgr.getFile(FrameworkName.str(), |
| 373 | /*openFile=*/!AutomaticImport); |
| 374 | if (FE && AutomaticImport) |
| 375 | *SuggestedModule = StringRef(Filename.begin(), SlashPos); |
| 376 | return FE; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 379 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 380 | //===----------------------------------------------------------------------===// |
| 381 | // Header File Location. |
| 382 | //===----------------------------------------------------------------------===// |
| 383 | |
| 384 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 385 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, |
| 386 | /// return null on failure. isAngled indicates whether the file reference is |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 387 | /// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if |
| 388 | /// non-null, indicates where the #including file is, in case a relative search |
| 389 | /// is needed. |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 390 | const FileEntry *HeaderSearch::LookupFile( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 391 | StringRef Filename, |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 392 | bool isAngled, |
| 393 | const DirectoryLookup *FromDir, |
| 394 | const DirectoryLookup *&CurDir, |
| 395 | const FileEntry *CurFileEnt, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 396 | SmallVectorImpl<char> *SearchPath, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 397 | SmallVectorImpl<char> *RelativePath, |
| 398 | StringRef *SuggestedModule) |
| 399 | { |
| 400 | if (SuggestedModule) |
| 401 | *SuggestedModule = StringRef(); |
| 402 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 403 | // If 'Filename' is absolute, check to see if it exists and no searching. |
Michael J. Spencer | 256053b | 2010-12-17 21:22:22 +0000 | [diff] [blame] | 404 | if (llvm::sys::path::is_absolute(Filename)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 405 | CurDir = 0; |
| 406 | |
| 407 | // If this was an #include_next "/absolute/file", fail. |
| 408 | if (FromDir) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 410 | if (SearchPath != NULL) |
| 411 | SearchPath->clear(); |
| 412 | if (RelativePath != NULL) { |
| 413 | RelativePath->clear(); |
| 414 | RelativePath->append(Filename.begin(), Filename.end()); |
| 415 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 416 | // Otherwise, just return the file. |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 417 | return FileMgr.getFile(Filename, /*openFile=*/true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 418 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | |
Douglas Gregor | 65e02fa | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 420 | // Unless disabled, check to see if the file is in the #includer's |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 421 | // directory. This has to be based on CurFileEnt, not CurDir, because |
| 422 | // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 423 | // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h". |
| 424 | // This search is not done for <> headers. |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 425 | if (CurFileEnt && !isAngled && !NoCurDirSearch) { |
| 426 | llvm::SmallString<1024> TmpDir; |
| 427 | // Concatenate the requested file onto the directory. |
| 428 | // FIXME: Portability. Filename concatenation should be in sys::Path. |
| 429 | TmpDir += CurFileEnt->getDir()->getName(); |
| 430 | TmpDir.push_back('/'); |
| 431 | TmpDir.append(Filename.begin(), Filename.end()); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 432 | if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 433 | // Leave CurDir unset. |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 434 | // This file is a system header or C++ unfriendly if the old file is. |
| 435 | // |
| 436 | // Note that the temporary 'DirInfo' is required here, as either call to |
| 437 | // getFileInfo could resize the vector and we don't want to rely on order |
| 438 | // of evaluation. |
| 439 | unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo; |
Chris Lattner | c9dde4f | 2008-02-25 21:38:21 +0000 | [diff] [blame] | 440 | getFileInfo(FE).DirInfo = DirInfo; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 441 | if (SearchPath != NULL) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 442 | StringRef SearchPathRef(CurFileEnt->getDir()->getName()); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 443 | SearchPath->clear(); |
| 444 | SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); |
| 445 | } |
| 446 | if (RelativePath != NULL) { |
| 447 | RelativePath->clear(); |
| 448 | RelativePath->append(Filename.begin(), Filename.end()); |
| 449 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 450 | return FE; |
| 451 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 452 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 454 | CurDir = 0; |
| 455 | |
| 456 | // If this is a system #include, ignore the user #include locs. |
Nico Weber | 74a5fd8 | 2011-05-24 04:31:14 +0000 | [diff] [blame] | 457 | unsigned i = isAngled ? AngledDirIdx : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 459 | // If this is a #include_next request, start searching after the directory the |
| 460 | // file was found in. |
| 461 | if (FromDir) |
| 462 | i = FromDir-&SearchDirs[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 464 | // Cache all of the lookups performed by this method. Many headers are |
| 465 | // multiply included, and the "pragma once" optimization prevents them from |
| 466 | // being relex/pp'd, but they would still have to search through a |
| 467 | // (potentially huge) series of SearchDirs to find it. |
| 468 | std::pair<unsigned, unsigned> &CacheLookup = |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 469 | LookupFileCache.GetOrCreateValue(Filename).getValue(); |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 470 | |
| 471 | // If the entry has been previously looked up, the first value will be |
| 472 | // non-zero. If the value is equal to i (the start point of our search), then |
| 473 | // this is a matching hit. |
| 474 | if (CacheLookup.first == i+1) { |
| 475 | // Skip querying potentially lots of directories for this lookup. |
| 476 | i = CacheLookup.second; |
| 477 | } else { |
| 478 | // Otherwise, this is the first query, or the previous query didn't match |
| 479 | // our search start. We will fill in our found location below, so prime the |
| 480 | // start point value. |
| 481 | CacheLookup.first = i+1; |
| 482 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 484 | // Check each directory in sequence to see if it contains this file. |
| 485 | for (; i != SearchDirs.size(); ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | const FileEntry *FE = |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 487 | SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath, |
| 488 | BuildingModule, SuggestedModule); |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 489 | if (!FE) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 491 | CurDir = &SearchDirs[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 493 | // This file is a system header or C++ unfriendly if the dir is. |
Douglas Gregor | 65e02fa | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 494 | HeaderFileInfo &HFI = getFileInfo(FE); |
| 495 | HFI.DirInfo = CurDir->getDirCharacteristic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | |
Douglas Gregor | 65e02fa | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 497 | // If this file is found in a header map and uses the framework style of |
| 498 | // includes, then this header is part of a framework we're building. |
| 499 | if (CurDir->isIndexHeaderMap()) { |
| 500 | size_t SlashPos = Filename.find('/'); |
| 501 | if (SlashPos != StringRef::npos) { |
| 502 | HFI.IndexHeaderMapHeader = 1; |
| 503 | HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(), |
| 504 | SlashPos)); |
| 505 | } |
| 506 | } |
| 507 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 508 | // Remember this location for the next lookup we do. |
| 509 | CacheLookup.second = i; |
| 510 | return FE; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 511 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | |
Douglas Gregor | 2c7b780 | 2011-07-30 06:28:34 +0000 | [diff] [blame] | 513 | // If we are including a file with a quoted include "foo.h" from inside |
| 514 | // a header in a framework that is currently being built, and we couldn't |
| 515 | // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where |
| 516 | // "Foo" is the name of the framework in which the including header was found. |
| 517 | if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) { |
| 518 | HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt); |
| 519 | if (IncludingHFI.IndexHeaderMapHeader) { |
| 520 | llvm::SmallString<128> ScratchFilename; |
| 521 | ScratchFilename += IncludingHFI.Framework; |
| 522 | ScratchFilename += '/'; |
| 523 | ScratchFilename += Filename; |
| 524 | |
| 525 | const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true, |
| 526 | FromDir, CurDir, CurFileEnt, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 527 | SearchPath, RelativePath, |
| 528 | SuggestedModule); |
Douglas Gregor | 2c7b780 | 2011-07-30 06:28:34 +0000 | [diff] [blame] | 529 | std::pair<unsigned, unsigned> &CacheLookup |
| 530 | = LookupFileCache.GetOrCreateValue(Filename).getValue(); |
| 531 | CacheLookup.second |
| 532 | = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second; |
| 533 | return Result; |
| 534 | } |
| 535 | } |
| 536 | |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 537 | // Otherwise, didn't find it. Remember we didn't find this. |
| 538 | CacheLookup.second = SearchDirs.size(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | /// LookupSubframeworkHeader - Look up a subframework for the specified |
| 543 | /// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from |
| 544 | /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox |
| 545 | /// is a subframework within Carbon.framework. If so, return the FileEntry |
| 546 | /// for the designated file, otherwise return null. |
| 547 | const FileEntry *HeaderSearch:: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 548 | LookupSubframeworkHeader(StringRef Filename, |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 549 | const FileEntry *ContextFileEnt, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 550 | SmallVectorImpl<char> *SearchPath, |
| 551 | SmallVectorImpl<char> *RelativePath) { |
Chris Lattner | 9415a0c | 2008-02-01 05:34:02 +0000 | [diff] [blame] | 552 | assert(ContextFileEnt && "No context file?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 554 | // Framework names must have a '/' in the filename. Find it. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 555 | size_t SlashPos = Filename.find('/'); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 556 | if (SlashPos == StringRef::npos) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 558 | // Look up the base framework name of the ContextFileEnt. |
| 559 | const char *ContextName = ContextFileEnt->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 560 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 561 | // If the context info wasn't a framework, couldn't be a subframework. |
| 562 | const char *FrameworkPos = strstr(ContextName, ".framework/"); |
| 563 | if (FrameworkPos == 0) |
| 564 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 565 | |
| 566 | llvm::SmallString<1024> FrameworkName(ContextName, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 567 | FrameworkPos+strlen(".framework/")); |
| 568 | |
| 569 | // Append Frameworks/HIToolbox.framework/ |
| 570 | FrameworkName += "Frameworks/"; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 571 | FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 572 | FrameworkName += ".framework/"; |
| 573 | |
| 574 | llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup = |
Chris Lattner | 6538227 | 2010-11-21 09:55:08 +0000 | [diff] [blame] | 575 | FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 577 | // Some other location? |
| 578 | if (CacheLookup.getValue() && |
| 579 | CacheLookup.getKeyLength() == FrameworkName.size() && |
| 580 | memcmp(CacheLookup.getKeyData(), &FrameworkName[0], |
| 581 | CacheLookup.getKeyLength()) != 0) |
| 582 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 584 | // Cache subframework. |
| 585 | if (CacheLookup.getValue() == 0) { |
| 586 | ++NumSubFrameworkLookups; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 587 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 588 | // If the framework dir doesn't exist, we fail. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 589 | const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 590 | if (Dir == 0) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 592 | // Otherwise, if it does, remember that this is the right direntry for this |
| 593 | // framework. |
| 594 | CacheLookup.setValue(Dir); |
| 595 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 597 | const FileEntry *FE = 0; |
| 598 | |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 599 | if (RelativePath != NULL) { |
| 600 | RelativePath->clear(); |
| 601 | RelativePath->append(Filename.begin()+SlashPos+1, Filename.end()); |
| 602 | } |
| 603 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 604 | // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h" |
| 605 | llvm::SmallString<1024> HeadersFilename(FrameworkName); |
| 606 | HeadersFilename += "Headers/"; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 607 | if (SearchPath != NULL) { |
| 608 | SearchPath->clear(); |
| 609 | // Without trailing '/'. |
| 610 | SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1); |
| 611 | } |
| 612 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 613 | HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 614 | if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 616 | // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h" |
| 617 | HeadersFilename = FrameworkName; |
| 618 | HeadersFilename += "PrivateHeaders/"; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 619 | if (SearchPath != NULL) { |
| 620 | SearchPath->clear(); |
| 621 | // Without trailing '/'. |
| 622 | SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1); |
| 623 | } |
| 624 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 625 | HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 626 | if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 627 | return 0; |
| 628 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 630 | // This file is a system header or C++ unfriendly if the old file is. |
Ted Kremenek | ca63fa0 | 2008-02-24 03:55:14 +0000 | [diff] [blame] | 631 | // |
Chris Lattner | c9dde4f | 2008-02-25 21:38:21 +0000 | [diff] [blame] | 632 | // Note that the temporary 'DirInfo' is required here, as either call to |
| 633 | // getFileInfo could resize the vector and we don't want to rely on order |
| 634 | // of evaluation. |
| 635 | unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo; |
| 636 | getFileInfo(FE).DirInfo = DirInfo; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 637 | return FE; |
| 638 | } |
| 639 | |
| 640 | //===----------------------------------------------------------------------===// |
| 641 | // File Info Management. |
| 642 | //===----------------------------------------------------------------------===// |
| 643 | |
Douglas Gregor | 8f8d581 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 644 | /// \brief Merge the header file info provided by \p OtherHFI into the current |
| 645 | /// header file info (\p HFI) |
| 646 | static void mergeHeaderFileInfo(HeaderFileInfo &HFI, |
| 647 | const HeaderFileInfo &OtherHFI) { |
| 648 | HFI.isImport |= OtherHFI.isImport; |
| 649 | HFI.isPragmaOnce |= OtherHFI.isPragmaOnce; |
| 650 | HFI.NumIncludes += OtherHFI.NumIncludes; |
| 651 | |
| 652 | if (!HFI.ControllingMacro && !HFI.ControllingMacroID) { |
| 653 | HFI.ControllingMacro = OtherHFI.ControllingMacro; |
| 654 | HFI.ControllingMacroID = OtherHFI.ControllingMacroID; |
| 655 | } |
| 656 | |
| 657 | if (OtherHFI.External) { |
| 658 | HFI.DirInfo = OtherHFI.DirInfo; |
| 659 | HFI.External = OtherHFI.External; |
| 660 | HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader; |
| 661 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 662 | |
Douglas Gregor | 8f8d581 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 663 | if (HFI.Framework.empty()) |
| 664 | HFI.Framework = OtherHFI.Framework; |
| 665 | |
| 666 | HFI.Resolved = true; |
| 667 | } |
| 668 | |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 669 | /// getFileInfo - Return the HeaderFileInfo structure for the specified |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 670 | /// FileEntry. |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 671 | HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 672 | if (FE->getUID() >= FileInfo.size()) |
| 673 | FileInfo.resize(FE->getUID()+1); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 674 | |
| 675 | HeaderFileInfo &HFI = FileInfo[FE->getUID()]; |
Douglas Gregor | 8f8d581 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 676 | if (ExternalSource && !HFI.Resolved) |
| 677 | mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE)); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 678 | return HFI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 680 | |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 681 | bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) { |
| 682 | // Check if we've ever seen this file as a header. |
| 683 | if (File->getUID() >= FileInfo.size()) |
| 684 | return false; |
| 685 | |
| 686 | // Resolve header file info from the external source, if needed. |
| 687 | HeaderFileInfo &HFI = FileInfo[File->getUID()]; |
Douglas Gregor | 8f8d581 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 688 | if (ExternalSource && !HFI.Resolved) |
| 689 | mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File)); |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 690 | |
| 691 | return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID; |
| 692 | } |
| 693 | |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 694 | void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) { |
| 695 | if (UID >= FileInfo.size()) |
| 696 | FileInfo.resize(UID+1); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 697 | HFI.Resolved = true; |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 698 | FileInfo[UID] = HFI; |
| 699 | } |
| 700 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 701 | /// ShouldEnterIncludeFile - Mark the specified file as a target of of a |
| 702 | /// #include, #include_next, or #import directive. Return false if #including |
| 703 | /// the file will have no effect or true if we should include it. |
| 704 | bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){ |
| 705 | ++NumIncluded; // Count # of attempted #includes. |
| 706 | |
| 707 | // Get information about this file. |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 708 | HeaderFileInfo &FileInfo = getFileInfo(File); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 709 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 710 | // If this is a #import directive, check that we have not already imported |
| 711 | // this header. |
| 712 | if (isImport) { |
| 713 | // If this has already been imported, don't import it again. |
| 714 | FileInfo.isImport = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 715 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 716 | // Has this already been #import'ed or #include'd? |
| 717 | if (FileInfo.NumIncludes) return false; |
| 718 | } else { |
| 719 | // Otherwise, if this is a #include of a file that was previously #import'd |
| 720 | // or if this is the second #include of a #pragma once file, ignore it. |
| 721 | if (FileInfo.isImport) |
| 722 | return false; |
| 723 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 724 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 725 | // Next, check to see if the file is wrapped with #ifndef guards. If so, and |
| 726 | // if the macro that guards it is defined, we know the #include has no effect. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 727 | if (const IdentifierInfo *ControllingMacro |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 728 | = FileInfo.getControllingMacro(ExternalLookup)) |
| 729 | if (ControllingMacro->hasMacroDefinition()) { |
| 730 | ++NumMultiIncludeFileOptzn; |
| 731 | return false; |
| 732 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 733 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 734 | // Increment the number of times this file has been included. |
| 735 | ++FileInfo.NumIncludes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 736 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 737 | return true; |
| 738 | } |
| 739 | |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 740 | size_t HeaderSearch::getTotalMemory() const { |
| 741 | return SearchDirs.capacity() |
Ted Kremenek | eabea45 | 2011-07-27 18:41:18 +0000 | [diff] [blame] | 742 | + llvm::capacity_in_bytes(FileInfo) |
| 743 | + llvm::capacity_in_bytes(HeaderMaps) |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 744 | + LookupFileCache.getAllocator().getTotalMemory() |
| 745 | + FrameworkMap.getAllocator().getTotalMemory(); |
| 746 | } |
Douglas Gregor | 65e02fa | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 747 | |
| 748 | StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) { |
| 749 | return FrameworkNames.GetOrCreateValue(Framework).getKey(); |
| 750 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 751 | |
| 752 | bool HeaderSearch::hasModuleMap(StringRef FileName, |
| 753 | const DirectoryEntry *Root) { |
| 754 | llvm::SmallVector<const DirectoryEntry *, 2> FixUpDirectories; |
| 755 | |
| 756 | StringRef DirName = FileName; |
| 757 | do { |
| 758 | // Get the parent directory name. |
| 759 | DirName = llvm::sys::path::parent_path(DirName); |
| 760 | if (DirName.empty()) |
| 761 | return false; |
| 762 | |
| 763 | // Determine whether this directory exists. |
| 764 | const DirectoryEntry *Dir = FileMgr.getDirectory(DirName); |
| 765 | if (!Dir) |
| 766 | return false; |
| 767 | |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 768 | // Try to load the module map file in this directory. |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 769 | switch (loadModuleMapFile(Dir)) { |
| 770 | case LMM_NewlyLoaded: |
| 771 | case LMM_AlreadyLoaded: |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 772 | // Success. All of the directories we stepped through inherit this module |
| 773 | // map file. |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 774 | for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I) |
| 775 | DirectoryHasModuleMap[FixUpDirectories[I]] = true; |
| 776 | |
| 777 | return true; |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 778 | |
| 779 | case LMM_NoDirectory: |
| 780 | case LMM_InvalidModuleMap: |
| 781 | break; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 782 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 783 | |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 784 | // If we hit the top of our search, we're done. |
| 785 | if (Dir == Root) |
| 786 | return false; |
| 787 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 788 | // Keep track of all of the directories we checked, so we can mark them as |
| 789 | // having module maps if we eventually do find a module map. |
| 790 | FixUpDirectories.push_back(Dir); |
| 791 | } while (true); |
| 792 | |
| 793 | return false; |
| 794 | } |
| 795 | |
Douglas Gregor | 65f3b5e | 2011-11-11 22:18:48 +0000 | [diff] [blame] | 796 | StringRef HeaderSearch::findModuleForHeader(const FileEntry *File) { |
| 797 | if (ModuleMap::Module *Module = ModMap.findModuleForHeader(File)) |
| 798 | return Module->getTopLevelModuleName(); |
| 799 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 800 | return StringRef(); |
| 801 | } |
| 802 | |
Douglas Gregor | db1cde7 | 2011-11-16 00:09:06 +0000 | [diff] [blame^] | 803 | bool HeaderSearch::loadModuleMapFile(const FileEntry *File) { |
| 804 | const DirectoryEntry *Dir = File->getDir(); |
| 805 | |
| 806 | llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir |
| 807 | = DirectoryHasModuleMap.find(Dir); |
| 808 | if (KnownDir != DirectoryHasModuleMap.end()) |
| 809 | return !KnownDir->second; |
| 810 | |
| 811 | bool Result = ModMap.parseModuleMapFile(File); |
| 812 | DirectoryHasModuleMap[Dir] = !Result; |
| 813 | return Result; |
| 814 | } |
| 815 | |
| 816 | ModuleMap::Module *HeaderSearch::getModule(StringRef Name, bool AllowSearch) { |
| 817 | if (ModuleMap::Module *Module = ModMap.findModule(Name)) |
| 818 | return Module; |
| 819 | |
| 820 | if (!AllowSearch) |
| 821 | return 0; |
| 822 | |
| 823 | for (unsigned I = 0, N = SearchDirs.size(); I != N; ++I) { |
| 824 | if (!SearchDirs[I].isNormalDir()) |
| 825 | continue; |
| 826 | |
| 827 | switch (loadModuleMapFile(SearchDirs[I].getDir())) { |
| 828 | case LMM_AlreadyLoaded: |
| 829 | case LMM_InvalidModuleMap: |
| 830 | case LMM_NoDirectory: |
| 831 | break; |
| 832 | |
| 833 | case LMM_NewlyLoaded: |
| 834 | if (ModuleMap::Module *Module = ModMap.findModule(Name)) |
| 835 | return Module; |
| 836 | break; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | return 0; |
| 841 | } |
| 842 | |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 843 | HeaderSearch::LoadModuleMapResult |
| 844 | HeaderSearch::loadModuleMapFile(StringRef DirName) { |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 845 | if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName)) |
| 846 | return loadModuleMapFile(Dir); |
| 847 | |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 848 | return LMM_NoDirectory; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 851 | HeaderSearch::LoadModuleMapResult |
| 852 | HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) { |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 853 | llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir |
| 854 | = DirectoryHasModuleMap.find(Dir); |
| 855 | if (KnownDir != DirectoryHasModuleMap.end()) |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 856 | return KnownDir->second? LMM_AlreadyLoaded : LMM_InvalidModuleMap; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 857 | |
| 858 | llvm::SmallString<128> ModuleMapFileName; |
| 859 | ModuleMapFileName += Dir->getName(); |
| 860 | llvm::sys::path::append(ModuleMapFileName, "module.map"); |
| 861 | if (const FileEntry *ModuleMapFile = FileMgr.getFile(ModuleMapFileName)) { |
| 862 | // We have found a module map file. Try to parse it. |
| 863 | if (!ModMap.parseModuleMapFile(ModuleMapFile)) { |
| 864 | // This directory has a module map. |
| 865 | DirectoryHasModuleMap[Dir] = true; |
| 866 | |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 867 | return LMM_NewlyLoaded; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | |
| 871 | // No suitable module map. |
| 872 | DirectoryHasModuleMap[Dir] = false; |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 873 | return LMM_InvalidModuleMap; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 874 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 875 | |