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