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