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