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