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