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