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