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