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