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