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" |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
| 17 | #include "clang/Basic/IdentifierTable.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Lex/HeaderMap.h" |
| 19 | #include "clang/Lex/HeaderSearchOptions.h" |
| 20 | #include "clang/Lex/Lexer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | eabea45 | 2011-07-27 18:41:18 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Capacity.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "llvm/Support/FileSystem.h" |
| 24 | #include "llvm/Support/Path.h" |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 25 | #include <cstdio> |
Douglas Gregor | 3cc6277 | 2013-01-22 23:49:45 +0000 | [diff] [blame] | 26 | #if defined(LLVM_ON_UNIX) |
Dmitri Gribenko | adeb782 | 2013-01-26 16:29:36 +0000 | [diff] [blame] | 27 | #include <limits.h> |
Douglas Gregor | 3cc6277 | 2013-01-22 23:49:45 +0000 | [diff] [blame] | 28 | #endif |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 31 | const IdentifierInfo * |
| 32 | HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) { |
| 33 | if (ControllingMacro) |
| 34 | return ControllingMacro; |
| 35 | |
| 36 | if (!ControllingMacroID || !External) |
| 37 | return 0; |
| 38 | |
| 39 | ControllingMacro = External->GetIdentifier(ControllingMacroID); |
| 40 | return ControllingMacro; |
| 41 | } |
| 42 | |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 43 | ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {} |
| 44 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 45 | HeaderSearch::HeaderSearch(IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts, |
Douglas Gregor | c042edd | 2012-10-24 16:19:39 +0000 | [diff] [blame] | 46 | FileManager &FM, DiagnosticsEngine &Diags, |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 47 | const LangOptions &LangOpts, |
| 48 | const TargetInfo *Target) |
Douglas Gregor | c042edd | 2012-10-24 16:19:39 +0000 | [diff] [blame] | 49 | : HSOpts(HSOpts), FileMgr(FM), FrameworkMap(64), |
Argyrios Kyrtzidis | 55ea75b | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 50 | ModMap(FileMgr, *Diags.getClient(), LangOpts, Target, *this) |
Douglas Gregor | 8e23806 | 2011-11-11 00:35:06 +0000 | [diff] [blame] | 51 | { |
Nico Weber | 74a5fd8 | 2011-05-24 04:31:14 +0000 | [diff] [blame] | 52 | AngledDirIdx = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | SystemDirIdx = 0; |
| 54 | NoCurDirSearch = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 55 | |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 56 | ExternalLookup = 0; |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 57 | ExternalSource = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 58 | NumIncluded = 0; |
| 59 | NumMultiIncludeFileOptzn = 0; |
| 60 | NumFrameworkLookups = NumSubFrameworkLookups = 0; |
| 61 | } |
| 62 | |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 63 | HeaderSearch::~HeaderSearch() { |
| 64 | // Delete headermaps. |
| 65 | for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) |
| 66 | delete HeaderMaps[i].second; |
| 67 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 69 | void HeaderSearch::PrintStats() { |
| 70 | fprintf(stderr, "\n*** HeaderSearch Stats:\n"); |
| 71 | fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size()); |
| 72 | unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0; |
| 73 | for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) { |
| 74 | NumOnceOnlyFiles += FileInfo[i].isImport; |
| 75 | if (MaxNumIncludes < FileInfo[i].NumIncludes) |
| 76 | MaxNumIncludes = FileInfo[i].NumIncludes; |
| 77 | NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1; |
| 78 | } |
| 79 | fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles); |
| 80 | fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles); |
| 81 | fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 83 | fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded); |
| 84 | fprintf(stderr, " %d #includes skipped due to" |
| 85 | " the multi-include optimization.\n", NumMultiIncludeFileOptzn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 86 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 87 | fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups); |
| 88 | fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups); |
| 89 | } |
| 90 | |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 91 | /// CreateHeaderMap - This method returns a HeaderMap for the specified |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 92 | /// FileEntry, uniquing them through the 'HeaderMaps' datastructure. |
Chris Lattner | 1bfd4a6 | 2007-12-17 18:34:53 +0000 | [diff] [blame] | 93 | const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) { |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 94 | // 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] | 95 | // 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] | 96 | if (!HeaderMaps.empty()) { |
| 97 | for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 98 | // Pointer equality comparison of FileEntries works because they are |
| 99 | // already uniqued by inode. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | if (HeaderMaps[i].first == FE) |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 101 | return HeaderMaps[i].second; |
| 102 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 104 | if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) { |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 105 | HeaderMaps.push_back(std::make_pair(FE, HM)); |
| 106 | return HM; |
| 107 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 109 | return 0; |
| 110 | } |
| 111 | |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 112 | std::string HeaderSearch::getModuleFileName(Module *Module) { |
Douglas Gregor | 9a6da69 | 2011-09-12 20:41:59 +0000 | [diff] [blame] | 113 | // 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] | 114 | if (ModuleCachePath.empty()) |
| 115 | return std::string(); |
| 116 | |
| 117 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 118 | SmallString<256> Result(ModuleCachePath); |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 119 | llvm::sys::path::append(Result, Module->getTopLevelModule()->Name + ".pcm"); |
| 120 | return Result.str().str(); |
| 121 | } |
| 122 | |
| 123 | std::string HeaderSearch::getModuleFileName(StringRef ModuleName) { |
| 124 | // If we don't have a module cache path, we can't do anything. |
| 125 | if (ModuleCachePath.empty()) |
| 126 | return std::string(); |
Douglas Gregor | 6e975c4 | 2011-09-13 23:15:45 +0000 | [diff] [blame] | 127 | |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 128 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 129 | SmallString<256> Result(ModuleCachePath); |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 130 | llvm::sys::path::append(Result, ModuleName + ".pcm"); |
| 131 | return Result.str().str(); |
| 132 | } |
| 133 | |
| 134 | Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) { |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 135 | // 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] | 136 | Module *Module = ModMap.findModule(ModuleName); |
| 137 | if (Module || !AllowSearch) |
| 138 | return Module; |
| 139 | |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 140 | // Look through the various header search paths to load any available module |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 141 | // maps, searching for a module map that describes this module. |
| 142 | for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { |
| 143 | if (SearchDirs[Idx].isFramework()) { |
| 144 | // Search for or infer a module map for a framework. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 145 | SmallString<128> FrameworkDirName; |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 146 | FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName(); |
| 147 | llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework"); |
| 148 | if (const DirectoryEntry *FrameworkDir |
| 149 | = FileMgr.getDirectory(FrameworkDirName)) { |
| 150 | bool IsSystem |
| 151 | = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User; |
| 152 | Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem); |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 153 | if (Module) |
| 154 | break; |
| 155 | } |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // FIXME: Figure out how header maps and module maps will work together. |
| 159 | |
| 160 | // Only deal with normal search directories. |
| 161 | if (!SearchDirs[Idx].isNormalDir()) |
| 162 | continue; |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 163 | |
| 164 | bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory(); |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 165 | // Search for a module map file in this directory. |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 166 | if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem) |
| 167 | == LMM_NewlyLoaded) { |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 168 | // We just loaded a module map file; check whether the module is |
| 169 | // available now. |
| 170 | Module = ModMap.findModule(ModuleName); |
| 171 | if (Module) |
| 172 | break; |
| 173 | } |
| 174 | |
| 175 | // Search for a module map in a subdirectory with the same name as the |
| 176 | // module. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 177 | SmallString<128> NestedModuleMapDirName; |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 178 | NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName(); |
| 179 | llvm::sys::path::append(NestedModuleMapDirName, ModuleName); |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 180 | if (loadModuleMapFile(NestedModuleMapDirName, IsSystem) == LMM_NewlyLoaded){ |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 181 | // If we just loaded a module map file, look for the module again. |
| 182 | Module = ModMap.findModule(ModuleName); |
| 183 | if (Module) |
| 184 | break; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 185 | } |
Douglas Gregor | cdf2808 | 2013-03-21 01:08:50 +0000 | [diff] [blame] | 186 | |
| 187 | // If we've already performed the exhaustive search for module maps in this |
| 188 | // search directory, don't do it again. |
| 189 | if (SearchDirs[Idx].haveSearchedAllModuleMaps()) |
| 190 | continue; |
| 191 | |
| 192 | // Load all module maps in the immediate subdirectories of this search |
| 193 | // directory. |
| 194 | loadSubdirectoryModuleMaps(SearchDirs[Idx]); |
| 195 | |
| 196 | // Look again for the module. |
| 197 | Module = ModMap.findModule(ModuleName); |
| 198 | if (Module) |
| 199 | break; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 200 | } |
Douglas Gregor | cdf2808 | 2013-03-21 01:08:50 +0000 | [diff] [blame] | 201 | |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 202 | return Module; |
Douglas Gregor | 9a6da69 | 2011-09-12 20:41:59 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 205 | //===----------------------------------------------------------------------===// |
| 206 | // File lookup within a DirectoryLookup scope |
| 207 | //===----------------------------------------------------------------------===// |
| 208 | |
Chris Lattner | 3af66a9 | 2007-12-17 17:57:27 +0000 | [diff] [blame] | 209 | /// getName - Return the directory or filename corresponding to this lookup |
| 210 | /// object. |
| 211 | const char *DirectoryLookup::getName() const { |
| 212 | if (isNormalDir()) |
| 213 | return getDir()->getName(); |
| 214 | if (isFramework()) |
| 215 | return getFrameworkDir()->getName(); |
| 216 | assert(isHeaderMap() && "Unknown DirectoryLookup"); |
| 217 | return getHeaderMap()->getFileName(); |
| 218 | } |
| 219 | |
| 220 | |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 221 | /// LookupFile - Lookup the specified file in this search path, returning it |
| 222 | /// if it exists or returning null if not. |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 223 | const FileEntry *DirectoryLookup::LookupFile( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 224 | StringRef Filename, |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 225 | HeaderSearch &HS, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 226 | SmallVectorImpl<char> *SearchPath, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 227 | SmallVectorImpl<char> *RelativePath, |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 228 | ModuleMap::KnownHeader *SuggestedModule, |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 229 | bool &InUserSpecifiedSystemFramework) const { |
| 230 | InUserSpecifiedSystemFramework = false; |
| 231 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 232 | SmallString<1024> TmpDir; |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 233 | if (isNormalDir()) { |
| 234 | // Concatenate the requested file onto the directory. |
Eli Friedman | a6e023c | 2011-07-08 20:17:28 +0000 | [diff] [blame] | 235 | TmpDir = getDir()->getName(); |
| 236 | llvm::sys::path::append(TmpDir, Filename); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 237 | if (SearchPath != NULL) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 238 | StringRef SearchPathRef(getDir()->getName()); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 239 | SearchPath->clear(); |
| 240 | SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); |
| 241 | } |
| 242 | if (RelativePath != NULL) { |
| 243 | RelativePath->clear(); |
| 244 | RelativePath->append(Filename.begin(), Filename.end()); |
| 245 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 246 | |
| 247 | // If we have a module map that might map this header, load it and |
| 248 | // check whether we'll have a suggestion for a module. |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 249 | if (SuggestedModule && |
| 250 | HS.hasModuleMap(TmpDir, getDir(), isSystemHeaderDirectory())) { |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 251 | const FileEntry *File = HS.getFileMgr().getFile(TmpDir.str(), |
| 252 | /*openFile=*/false); |
| 253 | if (!File) |
| 254 | return File; |
| 255 | |
| 256 | // If there is a module that corresponds to this header, |
| 257 | // suggest it. |
Douglas Gregor | 5e3f922 | 2011-12-08 17:01:29 +0000 | [diff] [blame] | 258 | *SuggestedModule = HS.findModuleForHeader(File); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 259 | return File; |
| 260 | } |
| 261 | |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 262 | return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true); |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 263 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 265 | if (isFramework()) |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 266 | return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath, |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 267 | SuggestedModule, InUserSpecifiedSystemFramework); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | |
Chris Lattner | b09e71f | 2007-12-17 08:17:39 +0000 | [diff] [blame] | 269 | assert(isHeaderMap() && "Unknown directory lookup"); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 270 | const FileEntry * const Result = getHeaderMap()->LookupFile( |
| 271 | Filename, HS.getFileMgr()); |
| 272 | if (Result) { |
| 273 | if (SearchPath != NULL) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 274 | StringRef SearchPathRef(getName()); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 275 | SearchPath->clear(); |
| 276 | SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); |
| 277 | } |
| 278 | if (RelativePath != NULL) { |
| 279 | RelativePath->clear(); |
| 280 | RelativePath->append(Filename.begin(), Filename.end()); |
| 281 | } |
| 282 | } |
| 283 | return Result; |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 286 | /// \brief Given a framework directory, find the top-most framework directory. |
| 287 | /// |
| 288 | /// \param FileMgr The file manager to use for directory lookups. |
| 289 | /// \param DirName The name of the framework directory. |
| 290 | /// \param SubmodulePath Will be populated with the submodule path from the |
| 291 | /// returned top-level module to the originally named framework. |
| 292 | static const DirectoryEntry * |
| 293 | getTopFrameworkDir(FileManager &FileMgr, StringRef DirName, |
| 294 | SmallVectorImpl<std::string> &SubmodulePath) { |
| 295 | assert(llvm::sys::path::extension(DirName) == ".framework" && |
| 296 | "Not a framework directory"); |
| 297 | |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 298 | // Note: as an egregious but useful hack we use the real path here, because |
| 299 | // frameworks moving between top-level frameworks to embedded frameworks tend |
| 300 | // to be symlinked, and we base the logical structure of modules on the |
| 301 | // physical layout. In particular, we need to deal with crazy includes like |
| 302 | // |
| 303 | // #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h> |
| 304 | // |
| 305 | // where 'Bar' used to be embedded in 'Foo', is now a top-level framework |
| 306 | // which one should access with, e.g., |
| 307 | // |
| 308 | // #include <Bar/Wibble.h> |
| 309 | // |
| 310 | // Similar issues occur when a top-level framework has moved into an |
| 311 | // embedded framework. |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 312 | const DirectoryEntry *TopFrameworkDir = FileMgr.getDirectory(DirName); |
Douglas Gregor | 713b7c0 | 2013-01-26 00:55:12 +0000 | [diff] [blame] | 313 | DirName = FileMgr.getCanonicalName(TopFrameworkDir); |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 314 | do { |
| 315 | // Get the parent directory name. |
| 316 | DirName = llvm::sys::path::parent_path(DirName); |
| 317 | if (DirName.empty()) |
| 318 | break; |
| 319 | |
| 320 | // Determine whether this directory exists. |
| 321 | const DirectoryEntry *Dir = FileMgr.getDirectory(DirName); |
| 322 | if (!Dir) |
| 323 | break; |
| 324 | |
| 325 | // If this is a framework directory, then we're a subframework of this |
| 326 | // framework. |
| 327 | if (llvm::sys::path::extension(DirName) == ".framework") { |
| 328 | SubmodulePath.push_back(llvm::sys::path::stem(DirName)); |
| 329 | TopFrameworkDir = Dir; |
| 330 | } |
| 331 | } while (true); |
| 332 | |
| 333 | return TopFrameworkDir; |
| 334 | } |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 335 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 336 | /// DoFrameworkLookup - Do a lookup of the specified file in the current |
| 337 | /// DirectoryLookup, which is a framework directory. |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 338 | const FileEntry *DirectoryLookup::DoFrameworkLookup( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 339 | StringRef Filename, |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 340 | HeaderSearch &HS, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 341 | SmallVectorImpl<char> *SearchPath, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 342 | SmallVectorImpl<char> *RelativePath, |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 343 | ModuleMap::KnownHeader *SuggestedModule, |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 344 | bool &InUserSpecifiedSystemFramework) const |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 345 | { |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 346 | FileManager &FileMgr = HS.getFileMgr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 348 | // Framework names must have a '/' in the filename. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 349 | size_t SlashPos = Filename.find('/'); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 350 | if (SlashPos == StringRef::npos) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 352 | // Find out if this is the home for the specified framework, by checking |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 353 | // HeaderSearch. Possible answers are yes/no and unknown. |
| 354 | HeaderSearch::FrameworkCacheEntry &CacheEntry = |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 355 | HS.LookupFrameworkCache(Filename.substr(0, SlashPos)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 357 | // If it is known and in some other directory, fail. |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 358 | if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 359 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 361 | // Otherwise, construct the path to this framework dir. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 363 | // FrameworkName = "/System/Library/Frameworks/" |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 364 | SmallString<1024> FrameworkName; |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 365 | FrameworkName += getFrameworkDir()->getName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 366 | if (FrameworkName.empty() || FrameworkName.back() != '/') |
| 367 | FrameworkName.push_back('/'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 369 | // FrameworkName = "/System/Library/Frameworks/Cocoa" |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 370 | StringRef ModuleName(Filename.begin(), SlashPos); |
| 371 | FrameworkName += ModuleName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 373 | // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/" |
| 374 | FrameworkName += ".framework/"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 376 | // If the cache entry was unresolved, populate it now. |
| 377 | if (CacheEntry.Directory == 0) { |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 378 | HS.IncrementFrameworkLookupCount(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 380 | // If the framework dir doesn't exist, we fail. |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 381 | const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str()); |
| 382 | if (Dir == 0) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 384 | // Otherwise, if it does, remember that this is the right direntry for this |
| 385 | // framework. |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 386 | CacheEntry.Directory = getFrameworkDir(); |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 387 | |
| 388 | // If this is a user search directory, check if the framework has been |
| 389 | // user-specified as a system framework. |
| 390 | if (getDirCharacteristic() == SrcMgr::C_User) { |
| 391 | SmallString<1024> SystemFrameworkMarker(FrameworkName); |
| 392 | SystemFrameworkMarker += ".system_framework"; |
| 393 | if (llvm::sys::fs::exists(SystemFrameworkMarker.str())) { |
| 394 | CacheEntry.IsUserSpecifiedSystemFramework = true; |
| 395 | } |
| 396 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 397 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 398 | |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 399 | // Set the 'user-specified system framework' flag. |
| 400 | InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework; |
| 401 | |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 402 | if (RelativePath != NULL) { |
| 403 | RelativePath->clear(); |
| 404 | RelativePath->append(Filename.begin()+SlashPos+1, Filename.end()); |
| 405 | } |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 406 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 407 | // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h" |
| 408 | unsigned OrigSize = FrameworkName.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 410 | FrameworkName += "Headers/"; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 411 | |
| 412 | if (SearchPath != NULL) { |
| 413 | SearchPath->clear(); |
| 414 | // Without trailing '/'. |
| 415 | SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1); |
| 416 | } |
| 417 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 418 | FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end()); |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 419 | const FileEntry *FE = FileMgr.getFile(FrameworkName.str(), |
| 420 | /*openFile=*/!SuggestedModule); |
| 421 | if (!FE) { |
| 422 | // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h" |
| 423 | const char *Private = "Private"; |
| 424 | FrameworkName.insert(FrameworkName.begin()+OrigSize, Private, |
| 425 | Private+strlen(Private)); |
| 426 | if (SearchPath != NULL) |
| 427 | SearchPath->insert(SearchPath->begin()+OrigSize, Private, |
| 428 | Private+strlen(Private)); |
| 429 | |
| 430 | FE = FileMgr.getFile(FrameworkName.str(), /*openFile=*/!SuggestedModule); |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 431 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 433 | // If we found the header and are allowed to suggest a module, do so now. |
| 434 | if (FE && SuggestedModule) { |
| 435 | // Find the framework in which this header occurs. |
| 436 | StringRef FrameworkPath = FE->getName(); |
| 437 | bool FoundFramework = false; |
| 438 | do { |
| 439 | // Get the parent directory name. |
| 440 | FrameworkPath = llvm::sys::path::parent_path(FrameworkPath); |
| 441 | if (FrameworkPath.empty()) |
| 442 | break; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 443 | |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 444 | // Determine whether this directory exists. |
| 445 | const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath); |
| 446 | if (!Dir) |
| 447 | break; |
| 448 | |
| 449 | // If this is a framework directory, then we're a subframework of this |
| 450 | // framework. |
| 451 | if (llvm::sys::path::extension(FrameworkPath) == ".framework") { |
| 452 | FoundFramework = true; |
| 453 | break; |
| 454 | } |
| 455 | } while (true); |
| 456 | |
| 457 | if (FoundFramework) { |
| 458 | // Find the top-level framework based on this framework. |
| 459 | SmallVector<std::string, 4> SubmodulePath; |
| 460 | const DirectoryEntry *TopFrameworkDir |
| 461 | = ::getTopFrameworkDir(FileMgr, FrameworkPath, SubmodulePath); |
| 462 | |
| 463 | // Determine the name of the top-level framework. |
| 464 | StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName()); |
| 465 | |
| 466 | // Load this framework module. If that succeeds, find the suggested module |
| 467 | // for this header, if any. |
| 468 | bool IsSystem = getDirCharacteristic() != SrcMgr::C_User; |
| 469 | if (HS.loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem)) { |
| 470 | *SuggestedModule = HS.findModuleForHeader(FE); |
| 471 | } |
| 472 | } else { |
| 473 | *SuggestedModule = HS.findModuleForHeader(FE); |
| 474 | } |
| 475 | } |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 476 | return FE; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 479 | void HeaderSearch::setTarget(const TargetInfo &Target) { |
| 480 | ModMap.setTarget(Target); |
| 481 | } |
| 482 | |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 483 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 484 | //===----------------------------------------------------------------------===// |
| 485 | // Header File Location. |
| 486 | //===----------------------------------------------------------------------===// |
| 487 | |
| 488 | |
James Dennett | 853519c | 2012-06-20 00:56:32 +0000 | [diff] [blame] | 489 | /// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 490 | /// return null on failure. isAngled indicates whether the file reference is |
James Dennett | 853519c | 2012-06-20 00:56:32 +0000 | [diff] [blame] | 491 | /// for system \#include's or not (i.e. using <> instead of ""). CurFileEnt, if |
| 492 | /// non-null, indicates where the \#including file is, in case a relative search |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 493 | /// is needed. |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 494 | const FileEntry *HeaderSearch::LookupFile( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 495 | StringRef Filename, |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 496 | bool isAngled, |
| 497 | const DirectoryLookup *FromDir, |
| 498 | const DirectoryLookup *&CurDir, |
| 499 | const FileEntry *CurFileEnt, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 500 | SmallVectorImpl<char> *SearchPath, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 501 | SmallVectorImpl<char> *RelativePath, |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 502 | ModuleMap::KnownHeader *SuggestedModule, |
Douglas Gregor | 1c2e933 | 2011-11-20 17:46:46 +0000 | [diff] [blame] | 503 | bool SkipCache) |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 504 | { |
| 505 | if (SuggestedModule) |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 506 | *SuggestedModule = ModuleMap::KnownHeader(); |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 507 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 508 | // 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] | 509 | if (llvm::sys::path::is_absolute(Filename)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 510 | CurDir = 0; |
| 511 | |
| 512 | // If this was an #include_next "/absolute/file", fail. |
| 513 | if (FromDir) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 514 | |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 515 | if (SearchPath != NULL) |
| 516 | SearchPath->clear(); |
| 517 | if (RelativePath != NULL) { |
| 518 | RelativePath->clear(); |
| 519 | RelativePath->append(Filename.begin(), Filename.end()); |
| 520 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 521 | // Otherwise, just return the file. |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 522 | return FileMgr.getFile(Filename, /*openFile=*/true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 523 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 524 | |
Douglas Gregor | 65e02fa | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 525 | // 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] | 526 | // directory. This has to be based on CurFileEnt, not CurDir, because |
| 527 | // 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] | 528 | // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h". |
| 529 | // This search is not done for <> headers. |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 530 | if (CurFileEnt && !isAngled && !NoCurDirSearch) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 531 | SmallString<1024> TmpDir; |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 532 | // Concatenate the requested file onto the directory. |
| 533 | // FIXME: Portability. Filename concatenation should be in sys::Path. |
| 534 | TmpDir += CurFileEnt->getDir()->getName(); |
| 535 | TmpDir.push_back('/'); |
| 536 | TmpDir.append(Filename.begin(), Filename.end()); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 537 | if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 538 | // Leave CurDir unset. |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 539 | // This file is a system header or C++ unfriendly if the old file is. |
| 540 | // |
Douglas Gregor | 21efbb6 | 2012-08-13 15:47:39 +0000 | [diff] [blame] | 541 | // Note that we only use one of FromHFI/ToHFI at once, due to potential |
| 542 | // reallocation of the underlying vector potentially making the first |
| 543 | // reference binding dangling. |
| 544 | HeaderFileInfo &FromHFI = getFileInfo(CurFileEnt); |
| 545 | unsigned DirInfo = FromHFI.DirInfo; |
| 546 | bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader; |
| 547 | StringRef Framework = FromHFI.Framework; |
| 548 | |
| 549 | HeaderFileInfo &ToHFI = getFileInfo(FE); |
| 550 | ToHFI.DirInfo = DirInfo; |
| 551 | ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader; |
| 552 | ToHFI.Framework = Framework; |
| 553 | |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 554 | if (SearchPath != NULL) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 555 | StringRef SearchPathRef(CurFileEnt->getDir()->getName()); |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 556 | SearchPath->clear(); |
| 557 | SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); |
| 558 | } |
| 559 | if (RelativePath != NULL) { |
| 560 | RelativePath->clear(); |
| 561 | RelativePath->append(Filename.begin(), Filename.end()); |
| 562 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 563 | return FE; |
| 564 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 565 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 567 | CurDir = 0; |
| 568 | |
| 569 | // If this is a system #include, ignore the user #include locs. |
Nico Weber | 74a5fd8 | 2011-05-24 04:31:14 +0000 | [diff] [blame] | 570 | unsigned i = isAngled ? AngledDirIdx : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 572 | // If this is a #include_next request, start searching after the directory the |
| 573 | // file was found in. |
| 574 | if (FromDir) |
| 575 | i = FromDir-&SearchDirs[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 577 | // Cache all of the lookups performed by this method. Many headers are |
| 578 | // multiply included, and the "pragma once" optimization prevents them from |
| 579 | // being relex/pp'd, but they would still have to search through a |
| 580 | // (potentially huge) series of SearchDirs to find it. |
| 581 | std::pair<unsigned, unsigned> &CacheLookup = |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 582 | LookupFileCache.GetOrCreateValue(Filename).getValue(); |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 583 | |
| 584 | // If the entry has been previously looked up, the first value will be |
| 585 | // non-zero. If the value is equal to i (the start point of our search), then |
| 586 | // this is a matching hit. |
Douglas Gregor | 1c2e933 | 2011-11-20 17:46:46 +0000 | [diff] [blame] | 587 | if (!SkipCache && CacheLookup.first == i+1) { |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 588 | // Skip querying potentially lots of directories for this lookup. |
| 589 | i = CacheLookup.second; |
| 590 | } else { |
| 591 | // Otherwise, this is the first query, or the previous query didn't match |
| 592 | // our search start. We will fill in our found location below, so prime the |
| 593 | // start point value. |
| 594 | CacheLookup.first = i+1; |
| 595 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 597 | // Check each directory in sequence to see if it contains this file. |
| 598 | for (; i != SearchDirs.size(); ++i) { |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 599 | bool InUserSpecifiedSystemFramework = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 600 | const FileEntry *FE = |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 601 | SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath, |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 602 | SuggestedModule, InUserSpecifiedSystemFramework); |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 603 | if (!FE) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 605 | CurDir = &SearchDirs[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 606 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 607 | // 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] | 608 | HeaderFileInfo &HFI = getFileInfo(FE); |
| 609 | HFI.DirInfo = CurDir->getDirCharacteristic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
Daniel Dunbar | 85ff969 | 2012-04-05 17:10:06 +0000 | [diff] [blame] | 611 | // If the directory characteristic is User but this framework was |
| 612 | // user-specified to be treated as a system framework, promote the |
| 613 | // characteristic. |
| 614 | if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework) |
| 615 | HFI.DirInfo = SrcMgr::C_System; |
| 616 | |
Richard Smith | f122a13 | 2012-06-13 20:27:03 +0000 | [diff] [blame] | 617 | // If the filename matches a known system header prefix, override |
| 618 | // whether the file is a system header. |
Richard Trieu | 4ef2f6a | 2012-06-13 20:52:36 +0000 | [diff] [blame] | 619 | for (unsigned j = SystemHeaderPrefixes.size(); j; --j) { |
| 620 | if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) { |
| 621 | HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System |
Richard Smith | f122a13 | 2012-06-13 20:27:03 +0000 | [diff] [blame] | 622 | : SrcMgr::C_User; |
| 623 | break; |
| 624 | } |
| 625 | } |
| 626 | |
Douglas Gregor | 65e02fa | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 627 | // If this file is found in a header map and uses the framework style of |
| 628 | // includes, then this header is part of a framework we're building. |
| 629 | if (CurDir->isIndexHeaderMap()) { |
| 630 | size_t SlashPos = Filename.find('/'); |
| 631 | if (SlashPos != StringRef::npos) { |
| 632 | HFI.IndexHeaderMapHeader = 1; |
| 633 | HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(), |
| 634 | SlashPos)); |
| 635 | } |
| 636 | } |
| 637 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 638 | // Remember this location for the next lookup we do. |
| 639 | CacheLookup.second = i; |
| 640 | return FE; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 641 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 642 | |
Douglas Gregor | 2c7b780 | 2011-07-30 06:28:34 +0000 | [diff] [blame] | 643 | // If we are including a file with a quoted include "foo.h" from inside |
| 644 | // a header in a framework that is currently being built, and we couldn't |
| 645 | // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where |
| 646 | // "Foo" is the name of the framework in which the including header was found. |
| 647 | if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) { |
| 648 | HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt); |
| 649 | if (IncludingHFI.IndexHeaderMapHeader) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 650 | SmallString<128> ScratchFilename; |
Douglas Gregor | 2c7b780 | 2011-07-30 06:28:34 +0000 | [diff] [blame] | 651 | ScratchFilename += IncludingHFI.Framework; |
| 652 | ScratchFilename += '/'; |
| 653 | ScratchFilename += Filename; |
| 654 | |
| 655 | const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true, |
| 656 | FromDir, CurDir, CurFileEnt, |
Douglas Gregor | fba18aa | 2011-09-15 22:00:41 +0000 | [diff] [blame] | 657 | SearchPath, RelativePath, |
| 658 | SuggestedModule); |
Douglas Gregor | 2c7b780 | 2011-07-30 06:28:34 +0000 | [diff] [blame] | 659 | std::pair<unsigned, unsigned> &CacheLookup |
| 660 | = LookupFileCache.GetOrCreateValue(Filename).getValue(); |
| 661 | CacheLookup.second |
| 662 | = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second; |
| 663 | return Result; |
| 664 | } |
| 665 | } |
| 666 | |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 667 | // Otherwise, didn't find it. Remember we didn't find this. |
| 668 | CacheLookup.second = SearchDirs.size(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | /// LookupSubframeworkHeader - Look up a subframework for the specified |
James Dennett | 853519c | 2012-06-20 00:56:32 +0000 | [diff] [blame] | 673 | /// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 674 | /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox |
| 675 | /// is a subframework within Carbon.framework. If so, return the FileEntry |
| 676 | /// for the designated file, otherwise return null. |
| 677 | const FileEntry *HeaderSearch:: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 678 | LookupSubframeworkHeader(StringRef Filename, |
Chandler Carruth | b5142bb | 2011-03-16 18:34:36 +0000 | [diff] [blame] | 679 | const FileEntry *ContextFileEnt, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 680 | SmallVectorImpl<char> *SearchPath, |
Douglas Gregor | 1b58c74 | 2013-02-08 00:10:48 +0000 | [diff] [blame] | 681 | SmallVectorImpl<char> *RelativePath, |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 682 | ModuleMap::KnownHeader *SuggestedModule) { |
Chris Lattner | 9415a0c | 2008-02-01 05:34:02 +0000 | [diff] [blame] | 683 | assert(ContextFileEnt && "No context file?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 685 | // Framework names must have a '/' in the filename. Find it. |
Douglas Gregor | efda0e8 | 2011-12-09 16:48:01 +0000 | [diff] [blame] | 686 | // FIXME: Should we permit '\' on Windows? |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 687 | size_t SlashPos = Filename.find('/'); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 688 | if (SlashPos == StringRef::npos) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 689 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 690 | // Look up the base framework name of the ContextFileEnt. |
| 691 | const char *ContextName = ContextFileEnt->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 692 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 693 | // 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] | 694 | const unsigned DotFrameworkLen = 10; |
| 695 | const char *FrameworkPos = strstr(ContextName, ".framework"); |
| 696 | if (FrameworkPos == 0 || |
| 697 | (FrameworkPos[DotFrameworkLen] != '/' && |
| 698 | FrameworkPos[DotFrameworkLen] != '\\')) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 699 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 700 | |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 701 | SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 702 | |
| 703 | // Append Frameworks/HIToolbox.framework/ |
| 704 | FrameworkName += "Frameworks/"; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 705 | FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 706 | FrameworkName += ".framework/"; |
| 707 | |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 708 | llvm::StringMapEntry<FrameworkCacheEntry> &CacheLookup = |
Chris Lattner | 6538227 | 2010-11-21 09:55:08 +0000 | [diff] [blame] | 709 | FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 711 | // Some other location? |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 712 | if (CacheLookup.getValue().Directory && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 713 | CacheLookup.getKeyLength() == FrameworkName.size() && |
| 714 | memcmp(CacheLookup.getKeyData(), &FrameworkName[0], |
| 715 | CacheLookup.getKeyLength()) != 0) |
| 716 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 718 | // Cache subframework. |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 719 | if (CacheLookup.getValue().Directory == 0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 720 | ++NumSubFrameworkLookups; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 721 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 722 | // If the framework dir doesn't exist, we fail. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 723 | const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 724 | if (Dir == 0) return 0; |
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 | // Otherwise, if it does, remember that this is the right direntry for this |
| 727 | // framework. |
Daniel Dunbar | 9ee35f9 | 2012-04-05 17:09:40 +0000 | [diff] [blame] | 728 | CacheLookup.getValue().Directory = Dir; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 729 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 731 | const FileEntry *FE = 0; |
| 732 | |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 733 | if (RelativePath != NULL) { |
| 734 | RelativePath->clear(); |
| 735 | RelativePath->append(Filename.begin()+SlashPos+1, Filename.end()); |
| 736 | } |
| 737 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 738 | // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h" |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 739 | SmallString<1024> HeadersFilename(FrameworkName); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 740 | HeadersFilename += "Headers/"; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 741 | if (SearchPath != NULL) { |
| 742 | SearchPath->clear(); |
| 743 | // Without trailing '/'. |
| 744 | SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1); |
| 745 | } |
| 746 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 747 | HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 748 | if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) { |
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 | // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h" |
| 751 | HeadersFilename = FrameworkName; |
| 752 | HeadersFilename += "PrivateHeaders/"; |
Manuel Klimek | 7412494 | 2011-04-26 21:50:03 +0000 | [diff] [blame] | 753 | if (SearchPath != NULL) { |
| 754 | SearchPath->clear(); |
| 755 | // Without trailing '/'. |
| 756 | SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1); |
| 757 | } |
| 758 | |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 759 | HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); |
Argyrios Kyrtzidis | 3cd0128 | 2011-03-16 19:17:25 +0000 | [diff] [blame] | 760 | if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 761 | return 0; |
| 762 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 764 | // 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] | 765 | // |
Chris Lattner | c9dde4f | 2008-02-25 21:38:21 +0000 | [diff] [blame] | 766 | // Note that the temporary 'DirInfo' is required here, as either call to |
| 767 | // getFileInfo could resize the vector and we don't want to rely on order |
| 768 | // of evaluation. |
| 769 | unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo; |
| 770 | getFileInfo(FE).DirInfo = DirInfo; |
Douglas Gregor | 1b58c74 | 2013-02-08 00:10:48 +0000 | [diff] [blame] | 771 | |
| 772 | // If we're supposed to suggest a module, look for one now. |
| 773 | if (SuggestedModule) { |
| 774 | // Find the top-level framework based on this framework. |
| 775 | FrameworkName.pop_back(); // remove the trailing '/' |
| 776 | SmallVector<std::string, 4> SubmodulePath; |
| 777 | const DirectoryEntry *TopFrameworkDir |
| 778 | = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath); |
| 779 | |
| 780 | // Determine the name of the top-level framework. |
| 781 | StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName()); |
| 782 | |
| 783 | // Load this framework module. If that succeeds, find the suggested module |
| 784 | // for this header, if any. |
| 785 | bool IsSystem = false; |
| 786 | if (loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem)) { |
| 787 | *SuggestedModule = findModuleForHeader(FE); |
| 788 | } |
| 789 | } |
| 790 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 791 | return FE; |
| 792 | } |
| 793 | |
Chandler Carruth | cb381ea | 2011-12-09 01:33:57 +0000 | [diff] [blame] | 794 | /// \brief Helper static function to normalize a path for injection into |
| 795 | /// a synthetic header. |
| 796 | /*static*/ std::string |
| 797 | HeaderSearch::NormalizeDashIncludePath(StringRef File, FileManager &FileMgr) { |
| 798 | // Implicit include paths should be resolved relative to the current |
| 799 | // working directory first, and then use the regular header search |
| 800 | // mechanism. The proper way to handle this is to have the |
| 801 | // predefines buffer located at the current working directory, but |
| 802 | // it has no file entry. For now, workaround this by using an |
| 803 | // absolute path if we find the file here, and otherwise letting |
| 804 | // header search handle it. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 805 | SmallString<128> Path(File); |
Chandler Carruth | cb381ea | 2011-12-09 01:33:57 +0000 | [diff] [blame] | 806 | llvm::sys::fs::make_absolute(Path); |
| 807 | bool exists; |
| 808 | if (llvm::sys::fs::exists(Path.str(), exists) || !exists) |
| 809 | Path = File; |
| 810 | else if (exists) |
| 811 | FileMgr.getFile(File); |
| 812 | |
| 813 | return Lexer::Stringify(Path.str()); |
| 814 | } |
| 815 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 816 | //===----------------------------------------------------------------------===// |
| 817 | // File Info Management. |
| 818 | //===----------------------------------------------------------------------===// |
| 819 | |
Douglas Gregor | 8f8d581 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 820 | /// \brief Merge the header file info provided by \p OtherHFI into the current |
| 821 | /// header file info (\p HFI) |
| 822 | static void mergeHeaderFileInfo(HeaderFileInfo &HFI, |
| 823 | const HeaderFileInfo &OtherHFI) { |
| 824 | HFI.isImport |= OtherHFI.isImport; |
| 825 | HFI.isPragmaOnce |= OtherHFI.isPragmaOnce; |
Argyrios Kyrtzidis | 55ea75b | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 826 | HFI.isModuleHeader |= OtherHFI.isModuleHeader; |
Douglas Gregor | 8f8d581 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 827 | HFI.NumIncludes += OtherHFI.NumIncludes; |
| 828 | |
| 829 | if (!HFI.ControllingMacro && !HFI.ControllingMacroID) { |
| 830 | HFI.ControllingMacro = OtherHFI.ControllingMacro; |
| 831 | HFI.ControllingMacroID = OtherHFI.ControllingMacroID; |
| 832 | } |
| 833 | |
| 834 | if (OtherHFI.External) { |
| 835 | HFI.DirInfo = OtherHFI.DirInfo; |
| 836 | HFI.External = OtherHFI.External; |
| 837 | HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader; |
| 838 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 839 | |
Douglas Gregor | 8f8d581 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 840 | if (HFI.Framework.empty()) |
| 841 | HFI.Framework = OtherHFI.Framework; |
| 842 | |
| 843 | HFI.Resolved = true; |
| 844 | } |
| 845 | |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 846 | /// getFileInfo - Return the HeaderFileInfo structure for the specified |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 847 | /// FileEntry. |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 848 | HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 849 | if (FE->getUID() >= FileInfo.size()) |
| 850 | FileInfo.resize(FE->getUID()+1); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 851 | |
| 852 | HeaderFileInfo &HFI = FileInfo[FE->getUID()]; |
Douglas Gregor | 8f8d581 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 853 | if (ExternalSource && !HFI.Resolved) |
| 854 | mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE)); |
Douglas Gregor | cfbf1c7 | 2011-02-10 17:09:37 +0000 | [diff] [blame] | 855 | return HFI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 856 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 857 | |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 858 | bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) { |
| 859 | // Check if we've ever seen this file as a header. |
| 860 | if (File->getUID() >= FileInfo.size()) |
| 861 | return false; |
| 862 | |
| 863 | // Resolve header file info from the external source, if needed. |
| 864 | HeaderFileInfo &HFI = FileInfo[File->getUID()]; |
Douglas Gregor | 8f8d581 | 2011-09-17 05:35:18 +0000 | [diff] [blame] | 865 | if (ExternalSource && !HFI.Resolved) |
| 866 | mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File)); |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 867 | |
Argyrios Kyrtzidis | 44dfff6 | 2012-12-10 20:08:37 +0000 | [diff] [blame] | 868 | return HFI.isPragmaOnce || HFI.isImport || |
| 869 | HFI.ControllingMacro || HFI.ControllingMacroID; |
Douglas Gregor | dd3e554 | 2011-05-04 00:14:37 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Argyrios Kyrtzidis | d3220db | 2013-05-08 23:46:46 +0000 | [diff] [blame] | 872 | void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE, |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 873 | ModuleMap::ModuleHeaderRole Role, |
Argyrios Kyrtzidis | d3220db | 2013-05-08 23:46:46 +0000 | [diff] [blame] | 874 | bool isCompilingModuleHeader) { |
Argyrios Kyrtzidis | 55ea75b | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 875 | if (FE->getUID() >= FileInfo.size()) |
| 876 | FileInfo.resize(FE->getUID()+1); |
| 877 | |
| 878 | HeaderFileInfo &HFI = FileInfo[FE->getUID()]; |
| 879 | HFI.isModuleHeader = true; |
Argyrios Kyrtzidis | d3220db | 2013-05-08 23:46:46 +0000 | [diff] [blame] | 880 | HFI.isCompilingModuleHeader = isCompilingModuleHeader; |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 881 | HFI.setHeaderRole(Role); |
Argyrios Kyrtzidis | 55ea75b | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 884 | bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){ |
| 885 | ++NumIncluded; // Count # of attempted #includes. |
| 886 | |
| 887 | // Get information about this file. |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 888 | HeaderFileInfo &FileInfo = getFileInfo(File); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 889 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 890 | // If this is a #import directive, check that we have not already imported |
| 891 | // this header. |
| 892 | if (isImport) { |
| 893 | // If this has already been imported, don't import it again. |
| 894 | FileInfo.isImport = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 895 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 896 | // Has this already been #import'ed or #include'd? |
| 897 | if (FileInfo.NumIncludes) return false; |
| 898 | } else { |
| 899 | // Otherwise, if this is a #include of a file that was previously #import'd |
| 900 | // or if this is the second #include of a #pragma once file, ignore it. |
| 901 | if (FileInfo.isImport) |
| 902 | return false; |
| 903 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 904 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 905 | // Next, check to see if the file is wrapped with #ifndef guards. If so, and |
| 906 | // 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] | 907 | if (const IdentifierInfo *ControllingMacro |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 908 | = FileInfo.getControllingMacro(ExternalLookup)) |
| 909 | if (ControllingMacro->hasMacroDefinition()) { |
| 910 | ++NumMultiIncludeFileOptzn; |
| 911 | return false; |
| 912 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 914 | // Increment the number of times this file has been included. |
| 915 | ++FileInfo.NumIncludes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 916 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 917 | return true; |
| 918 | } |
| 919 | |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 920 | size_t HeaderSearch::getTotalMemory() const { |
| 921 | return SearchDirs.capacity() |
Ted Kremenek | eabea45 | 2011-07-27 18:41:18 +0000 | [diff] [blame] | 922 | + llvm::capacity_in_bytes(FileInfo) |
| 923 | + llvm::capacity_in_bytes(HeaderMaps) |
Ted Kremenek | d1194fb | 2011-07-26 23:46:11 +0000 | [diff] [blame] | 924 | + LookupFileCache.getAllocator().getTotalMemory() |
| 925 | + FrameworkMap.getAllocator().getTotalMemory(); |
| 926 | } |
Douglas Gregor | 65e02fa | 2011-07-28 04:45:53 +0000 | [diff] [blame] | 927 | |
| 928 | StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) { |
| 929 | return FrameworkNames.GetOrCreateValue(Framework).getKey(); |
| 930 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 931 | |
| 932 | bool HeaderSearch::hasModuleMap(StringRef FileName, |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 933 | const DirectoryEntry *Root, |
| 934 | bool IsSystem) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 935 | SmallVector<const DirectoryEntry *, 2> FixUpDirectories; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 936 | |
| 937 | StringRef DirName = FileName; |
| 938 | do { |
| 939 | // Get the parent directory name. |
| 940 | DirName = llvm::sys::path::parent_path(DirName); |
| 941 | if (DirName.empty()) |
| 942 | return false; |
| 943 | |
| 944 | // Determine whether this directory exists. |
| 945 | const DirectoryEntry *Dir = FileMgr.getDirectory(DirName); |
| 946 | if (!Dir) |
| 947 | return false; |
| 948 | |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 949 | // Try to load the module map file in this directory. |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 950 | switch (loadModuleMapFile(Dir, IsSystem)) { |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 951 | case LMM_NewlyLoaded: |
| 952 | case LMM_AlreadyLoaded: |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 953 | // Success. All of the directories we stepped through inherit this module |
| 954 | // map file. |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 955 | for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I) |
| 956 | DirectoryHasModuleMap[FixUpDirectories[I]] = true; |
| 957 | |
| 958 | return true; |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 959 | |
| 960 | case LMM_NoDirectory: |
| 961 | case LMM_InvalidModuleMap: |
| 962 | break; |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 963 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 964 | |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 965 | // If we hit the top of our search, we're done. |
| 966 | if (Dir == Root) |
| 967 | return false; |
| 968 | |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 969 | // Keep track of all of the directories we checked, so we can mark them as |
| 970 | // having module maps if we eventually do find a module map. |
| 971 | FixUpDirectories.push_back(Dir); |
| 972 | } while (true); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 975 | ModuleMap::KnownHeader |
| 976 | HeaderSearch::findModuleForHeader(const FileEntry *File) const { |
Argyrios Kyrtzidis | 55ea75b | 2013-03-13 21:13:51 +0000 | [diff] [blame] | 977 | if (ExternalSource) { |
| 978 | // Make sure the external source has handled header info about this file, |
| 979 | // which includes whether the file is part of a module. |
| 980 | (void)getFileInfo(File); |
| 981 | } |
Lawrence Crowl | bc3f628 | 2013-06-20 21:14:14 +0000 | [diff] [blame] | 982 | return ModMap.findModuleForHeader(File); |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 985 | bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem) { |
Douglas Gregor | db1cde7 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 986 | const DirectoryEntry *Dir = File->getDir(); |
| 987 | |
| 988 | llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir |
| 989 | = DirectoryHasModuleMap.find(Dir); |
| 990 | if (KnownDir != DirectoryHasModuleMap.end()) |
| 991 | return !KnownDir->second; |
| 992 | |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 993 | bool Result = ModMap.parseModuleMapFile(File, IsSystem); |
Douglas Gregor | 4813442c | 2011-12-07 21:25:07 +0000 | [diff] [blame] | 994 | if (!Result && llvm::sys::path::filename(File->getName()) == "module.map") { |
| 995 | // If the file we loaded was a module.map, look for the corresponding |
| 996 | // module_private.map. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 997 | SmallString<128> PrivateFilename(Dir->getName()); |
Douglas Gregor | 4813442c | 2011-12-07 21:25:07 +0000 | [diff] [blame] | 998 | llvm::sys::path::append(PrivateFilename, "module_private.map"); |
| 999 | if (const FileEntry *PrivateFile = FileMgr.getFile(PrivateFilename)) |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 1000 | Result = ModMap.parseModuleMapFile(PrivateFile, IsSystem); |
Douglas Gregor | 4813442c | 2011-12-07 21:25:07 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | DirectoryHasModuleMap[Dir] = !Result; |
Douglas Gregor | db1cde7 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 1004 | return Result; |
| 1005 | } |
| 1006 | |
Douglas Gregor | e434ec7 | 2012-01-29 17:08:11 +0000 | [diff] [blame] | 1007 | Module *HeaderSearch::loadFrameworkModule(StringRef Name, |
| 1008 | const DirectoryEntry *Dir, |
| 1009 | bool IsSystem) { |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 1010 | if (Module *Module = ModMap.findModule(Name)) |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 1011 | return Module; |
| 1012 | |
| 1013 | // Try to load a module map file. |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 1014 | switch (loadModuleMapFile(Dir, IsSystem)) { |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 1015 | case LMM_InvalidModuleMap: |
| 1016 | break; |
| 1017 | |
| 1018 | case LMM_AlreadyLoaded: |
| 1019 | case LMM_NoDirectory: |
| 1020 | return 0; |
| 1021 | |
| 1022 | case LMM_NewlyLoaded: |
| 1023 | return ModMap.findModule(Name); |
| 1024 | } |
Douglas Gregor | a8c6fea | 2012-01-13 22:31:52 +0000 | [diff] [blame] | 1025 | |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 1026 | // Figure out the top-level framework directory and the submodule path from |
| 1027 | // that top-level framework to the requested framework. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1028 | SmallVector<std::string, 2> SubmodulePath; |
Douglas Gregor | a8c6fea | 2012-01-13 22:31:52 +0000 | [diff] [blame] | 1029 | SubmodulePath.push_back(Name); |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 1030 | const DirectoryEntry *TopFrameworkDir |
| 1031 | = ::getTopFrameworkDir(FileMgr, Dir->getName(), SubmodulePath); |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1032 | |
Douglas Gregor | 82e5237 | 2012-11-06 19:39:40 +0000 | [diff] [blame] | 1033 | |
Douglas Gregor | a8c6fea | 2012-01-13 22:31:52 +0000 | [diff] [blame] | 1034 | // Try to infer a module map from the top-level framework directory. |
| 1035 | Module *Result = ModMap.inferFrameworkModule(SubmodulePath.back(), |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 1036 | TopFrameworkDir, |
| 1037 | IsSystem, |
Douglas Gregor | a8c6fea | 2012-01-13 22:31:52 +0000 | [diff] [blame] | 1038 | /*Parent=*/0); |
Douglas Gregor | 7005b90 | 2013-01-10 01:43:00 +0000 | [diff] [blame] | 1039 | if (!Result) |
| 1040 | return 0; |
Douglas Gregor | a8c6fea | 2012-01-13 22:31:52 +0000 | [diff] [blame] | 1041 | |
| 1042 | // Follow the submodule path to find the requested (sub)framework module |
| 1043 | // within the top-level framework module. |
| 1044 | SubmodulePath.pop_back(); |
| 1045 | while (!SubmodulePath.empty() && Result) { |
| 1046 | Result = ModMap.lookupModuleQualified(SubmodulePath.back(), Result); |
| 1047 | SubmodulePath.pop_back(); |
| 1048 | } |
| 1049 | return Result; |
Douglas Gregor | 2821c7f | 2011-11-17 01:41:17 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Douglas Gregor | db1cde7 | 2011-11-16 00:09:06 +0000 | [diff] [blame] | 1052 | |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 1053 | HeaderSearch::LoadModuleMapResult |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 1054 | HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem) { |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1055 | if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName)) |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 1056 | return loadModuleMapFile(Dir, IsSystem); |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1057 | |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 1058 | return LMM_NoDirectory; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 1061 | HeaderSearch::LoadModuleMapResult |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 1062 | HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem) { |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1063 | llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir |
| 1064 | = DirectoryHasModuleMap.find(Dir); |
| 1065 | if (KnownDir != DirectoryHasModuleMap.end()) |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 1066 | return KnownDir->second? LMM_AlreadyLoaded : LMM_InvalidModuleMap; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1067 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1068 | SmallString<128> ModuleMapFileName; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1069 | ModuleMapFileName += Dir->getName(); |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1070 | unsigned ModuleMapDirNameLen = ModuleMapFileName.size(); |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1071 | llvm::sys::path::append(ModuleMapFileName, "module.map"); |
| 1072 | if (const FileEntry *ModuleMapFile = FileMgr.getFile(ModuleMapFileName)) { |
| 1073 | // We have found a module map file. Try to parse it. |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 1074 | if (ModMap.parseModuleMapFile(ModuleMapFile, IsSystem)) { |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1075 | // No suitable module map. |
| 1076 | DirectoryHasModuleMap[Dir] = false; |
| 1077 | return LMM_InvalidModuleMap; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1078 | } |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1079 | |
| 1080 | // This directory has a module map. |
| 1081 | DirectoryHasModuleMap[Dir] = true; |
| 1082 | |
| 1083 | // Check whether there is a private module map that we need to load as well. |
| 1084 | ModuleMapFileName.erase(ModuleMapFileName.begin() + ModuleMapDirNameLen, |
| 1085 | ModuleMapFileName.end()); |
| 1086 | llvm::sys::path::append(ModuleMapFileName, "module_private.map"); |
| 1087 | if (const FileEntry *PrivateModuleMapFile |
| 1088 | = FileMgr.getFile(ModuleMapFileName)) { |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 1089 | if (ModMap.parseModuleMapFile(PrivateModuleMapFile, IsSystem)) { |
Douglas Gregor | 587986e | 2011-12-07 02:23:45 +0000 | [diff] [blame] | 1090 | // No suitable module map. |
| 1091 | DirectoryHasModuleMap[Dir] = false; |
| 1092 | return LMM_InvalidModuleMap; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | return LMM_NewlyLoaded; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | // No suitable module map. |
| 1100 | DirectoryHasModuleMap[Dir] = false; |
Douglas Gregor | 2669797 | 2011-11-12 00:22:19 +0000 | [diff] [blame] | 1101 | return LMM_InvalidModuleMap; |
Douglas Gregor | cf70d78 | 2011-11-12 00:05:07 +0000 | [diff] [blame] | 1102 | } |
Douglas Gregor | a30cfe5 | 2011-11-11 19:10:28 +0000 | [diff] [blame] | 1103 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1104 | void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) { |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 1105 | Modules.clear(); |
| 1106 | |
| 1107 | // Load module maps for each of the header search directories. |
| 1108 | for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 1109 | bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory(); |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 1110 | if (SearchDirs[Idx].isFramework()) { |
| 1111 | llvm::error_code EC; |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 1112 | SmallString<128> DirNative; |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 1113 | llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(), |
| 1114 | DirNative); |
| 1115 | |
| 1116 | // Search each of the ".framework" directories to load them as modules. |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 1117 | for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd; |
| 1118 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
| 1119 | if (llvm::sys::path::extension(Dir->path()) != ".framework") |
| 1120 | continue; |
| 1121 | |
| 1122 | const DirectoryEntry *FrameworkDir = FileMgr.getDirectory(Dir->path()); |
| 1123 | if (!FrameworkDir) |
| 1124 | continue; |
| 1125 | |
| 1126 | // Load this framework module. |
| 1127 | loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir, |
| 1128 | IsSystem); |
| 1129 | } |
| 1130 | continue; |
| 1131 | } |
| 1132 | |
| 1133 | // FIXME: Deal with header maps. |
| 1134 | if (SearchDirs[Idx].isHeaderMap()) |
| 1135 | continue; |
| 1136 | |
| 1137 | // Try to load a module map file for the search directory. |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 1138 | loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem); |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 1139 | |
| 1140 | // Try to load module map files for immediate subdirectories of this search |
| 1141 | // directory. |
Douglas Gregor | cdf2808 | 2013-03-21 01:08:50 +0000 | [diff] [blame] | 1142 | loadSubdirectoryModuleMaps(SearchDirs[Idx]); |
Douglas Gregor | c5b2e58 | 2012-01-29 18:15:03 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | // Populate the list of modules. |
| 1146 | for (ModuleMap::module_iterator M = ModMap.module_begin(), |
| 1147 | MEnd = ModMap.module_end(); |
| 1148 | M != MEnd; ++M) { |
| 1149 | Modules.push_back(M->getValue()); |
| 1150 | } |
| 1151 | } |
Douglas Gregor | cdf2808 | 2013-03-21 01:08:50 +0000 | [diff] [blame] | 1152 | |
Douglas Gregor | 30a16f1 | 2013-05-10 22:52:27 +0000 | [diff] [blame] | 1153 | void HeaderSearch::loadTopLevelSystemModules() { |
| 1154 | // Load module maps for each of the header search directories. |
| 1155 | for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { |
| 1156 | // We only care about normal system header directories. |
| 1157 | if (!SearchDirs[Idx].isNormalDir() || |
| 1158 | SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_System) { |
| 1159 | continue; |
| 1160 | } |
| 1161 | |
| 1162 | // Try to load a module map file for the search directory. |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 1163 | loadModuleMapFile(SearchDirs[Idx].getDir(), |
| 1164 | SearchDirs[Idx].isSystemHeaderDirectory()); |
Douglas Gregor | 30a16f1 | 2013-05-10 22:52:27 +0000 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
Douglas Gregor | cdf2808 | 2013-03-21 01:08:50 +0000 | [diff] [blame] | 1168 | void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) { |
| 1169 | if (SearchDir.haveSearchedAllModuleMaps()) |
| 1170 | return; |
| 1171 | |
| 1172 | llvm::error_code EC; |
| 1173 | SmallString<128> DirNative; |
| 1174 | llvm::sys::path::native(SearchDir.getDir()->getName(), DirNative); |
| 1175 | for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd; |
| 1176 | Dir != DirEnd && !EC; Dir.increment(EC)) { |
Douglas Gregor | 8f5d7d1 | 2013-06-21 16:28:10 +0000 | [diff] [blame^] | 1177 | loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory()); |
Douglas Gregor | cdf2808 | 2013-03-21 01:08:50 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | SearchDir.setSearchedAllModuleMaps(true); |
| 1181 | } |