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