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