1 //===- HeaderSearch.cpp - Resolve Header File Locations -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file implements the DirectoryLookup and HeaderSearch interfaces.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/Lex/HeaderSearch.h"
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Basic/FileManager.h"
16 #include "clang/Basic/IdentifierTable.h"
17 #include "clang/Basic/Module.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "clang/Lex/DirectoryLookup.h"
20 #include "clang/Lex/ExternalPreprocessorSource.h"
21 #include "clang/Lex/HeaderMap.h"
22 #include "clang/Lex/HeaderSearchOptions.h"
23 #include "clang/Lex/LexDiagnostic.h"
24 #include "clang/Lex/ModuleMap.h"
25 #include "clang/Lex/Preprocessor.h"
26 #include "llvm/ADT/APInt.h"
27 #include "llvm/ADT/Hashing.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/ADT/Statistic.h"
31 #include "llvm/ADT/StringRef.h"
32 #include "llvm/ADT/STLExtras.h"
33 #include "llvm/Support/Allocator.h"
34 #include "llvm/Support/Capacity.h"
35 #include "llvm/Support/Errc.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/FileSystem.h"
38 #include "llvm/Support/Path.h"
39 #include "llvm/Support/VirtualFileSystem.h"
40 #include <algorithm>
41 #include <cassert>
42 #include <cstddef>
43 #include <cstdio>
44 #include <cstring>
45 #include <string>
46 #include <system_error>
47 #include <utility>
48 
49 using namespace clang;
50 
51 #define DEBUG_TYPE "file-search"
52 
53 ALWAYS_ENABLED_STATISTIC(NumIncluded, "Number of attempted #includes.");
54 ALWAYS_ENABLED_STATISTIC(
55     NumMultiIncludeFileOptzn,
56     "Number of #includes skipped due to the multi-include optimization.");
57 ALWAYS_ENABLED_STATISTIC(NumFrameworkLookups, "Number of framework lookups.");
58 ALWAYS_ENABLED_STATISTIC(NumSubFrameworkLookups,
59                          "Number of subframework lookups.");
60 
61 const IdentifierInfo *
62 HeaderFileInfo::getControllingMacro(ExternalPreprocessorSource *External) {
63   if (ControllingMacro) {
64     if (ControllingMacro->isOutOfDate()) {
65       assert(External && "We must have an external source if we have a "
66                          "controlling macro that is out of date.");
67       External->updateOutOfDateIdentifier(
68           *const_cast<IdentifierInfo *>(ControllingMacro));
69     }
70     return ControllingMacro;
71   }
72 
73   if (!ControllingMacroID || !External)
74     return nullptr;
75 
76   ControllingMacro = External->GetIdentifier(ControllingMacroID);
77   return ControllingMacro;
78 }
79 
80 ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() = default;
81 
82 HeaderSearch::HeaderSearch(std::shared_ptr<HeaderSearchOptions> HSOpts,
83                            SourceManager &SourceMgr, DiagnosticsEngine &Diags,
84                            const LangOptions &LangOpts,
85                            const TargetInfo *Target)
86     : HSOpts(std::move(HSOpts)), Diags(Diags),
87       FileMgr(SourceMgr.getFileManager()), FrameworkMap(64),
88       ModMap(SourceMgr, Diags, LangOpts, Target, *this) {}
89 
90 void HeaderSearch::PrintStats() {
91   llvm::errs() << "\n*** HeaderSearch Stats:\n"
92                << FileInfo.size() << " files tracked.\n";
93   unsigned NumOnceOnlyFiles = 0;
94   for (unsigned i = 0, e = FileInfo.size(); i != e; ++i)
95     NumOnceOnlyFiles += (FileInfo[i].isPragmaOnce || FileInfo[i].isImport);
96   llvm::errs() << "  " << NumOnceOnlyFiles << " #import/#pragma once files.\n";
97 
98   llvm::errs() << "  " << NumIncluded << " #include/#include_next/#import.\n"
99                << "    " << NumMultiIncludeFileOptzn
100                << " #includes skipped due to the multi-include optimization.\n";
101 
102   llvm::errs() << NumFrameworkLookups << " framework lookups.\n"
103                << NumSubFrameworkLookups << " subframework lookups.\n";
104 }
105 
106 void HeaderSearch::SetSearchPaths(
107     std::vector<DirectoryLookup> dirs, unsigned int angledDirIdx,
108     unsigned int systemDirIdx, bool noCurDirSearch,
109     llvm::DenseMap<unsigned int, unsigned int> searchDirToHSEntry) {
110   assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() &&
111          "Directory indices are unordered");
112   SearchDirs = std::move(dirs);
113   SearchDirsUsage.assign(SearchDirs.size(), false);
114   AngledDirIdx = angledDirIdx;
115   SystemDirIdx = systemDirIdx;
116   NoCurDirSearch = noCurDirSearch;
117   SearchDirToHSEntry = std::move(searchDirToHSEntry);
118   //LookupFileCache.clear();
119   indexInitialHeaderMaps();
120 }
121 
122 void HeaderSearch::AddSearchPath(const DirectoryLookup &dir, bool isAngled) {
123   unsigned idx = isAngled ? SystemDirIdx : AngledDirIdx;
124   SearchDirs.insert(SearchDirs.begin() + idx, dir);
125   SearchDirsUsage.insert(SearchDirsUsage.begin() + idx, false);
126   if (!isAngled)
127     AngledDirIdx++;
128   SystemDirIdx++;
129 }
130 
131 std::vector<bool> HeaderSearch::computeUserEntryUsage() const {
132   std::vector<bool> UserEntryUsage(HSOpts->UserEntries.size());
133   for (unsigned I = 0, E = SearchDirsUsage.size(); I < E; ++I) {
134     // Check whether this DirectoryLookup has been successfully used.
135     if (SearchDirsUsage[I]) {
136       auto UserEntryIdxIt = SearchDirToHSEntry.find(I);
137       // Check whether this DirectoryLookup maps to a HeaderSearch::UserEntry.
138       if (UserEntryIdxIt != SearchDirToHSEntry.end())
139         UserEntryUsage[UserEntryIdxIt->second] = true;
140     }
141   }
142   return UserEntryUsage;
143 }
144 
145 /// CreateHeaderMap - This method returns a HeaderMap for the specified
146 /// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
147 const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
148   // We expect the number of headermaps to be small, and almost always empty.
149   // If it ever grows, use of a linear search should be re-evaluated.
150   if (!HeaderMaps.empty()) {
151     for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
152       // Pointer equality comparison of FileEntries works because they are
153       // already uniqued by inode.
154       if (HeaderMaps[i].first == FE)
155         return HeaderMaps[i].second.get();
156   }
157 
158   if (std::unique_ptr<HeaderMap> HM = HeaderMap::Create(FE, FileMgr)) {
159     HeaderMaps.emplace_back(FE, std::move(HM));
160     return HeaderMaps.back().second.get();
161   }
162 
163   return nullptr;
164 }
165 
166 /// Get filenames for all registered header maps.
167 void HeaderSearch::getHeaderMapFileNames(
168     SmallVectorImpl<std::string> &Names) const {
169   for (auto &HM : HeaderMaps)
170     Names.push_back(std::string(HM.first->getName()));
171 }
172 
173 std::string HeaderSearch::getCachedModuleFileName(Module *Module) {
174   OptionalFileEntryRef ModuleMap =
175       getModuleMap().getModuleMapFileForUniquing(Module);
176   // The ModuleMap maybe a nullptr, when we load a cached C++ module without
177   // *.modulemap file. In this case, just return an empty string.
178   if (!ModuleMap)
179     return {};
180   return getCachedModuleFileName(Module->Name, ModuleMap->getName());
181 }
182 
183 std::string HeaderSearch::getPrebuiltModuleFileName(StringRef ModuleName,
184                                                     bool FileMapOnly) {
185   // First check the module name to pcm file map.
186   auto i(HSOpts->PrebuiltModuleFiles.find(ModuleName));
187   if (i != HSOpts->PrebuiltModuleFiles.end())
188     return i->second;
189 
190   if (FileMapOnly || HSOpts->PrebuiltModulePaths.empty())
191     return {};
192 
193   // Then go through each prebuilt module directory and try to find the pcm
194   // file.
195   for (const std::string &Dir : HSOpts->PrebuiltModulePaths) {
196     SmallString<256> Result(Dir);
197     llvm::sys::fs::make_absolute(Result);
198     if (ModuleName.contains(':'))
199       // The separator of C++20 modules partitions (':') is not good for file
200       // systems, here clang and gcc choose '-' by default since it is not a
201       // valid character of C++ indentifiers. So we could avoid conflicts.
202       llvm::sys::path::append(Result, ModuleName.split(':').first + "-" +
203                                           ModuleName.split(':').second +
204                                           ".pcm");
205     else
206       llvm::sys::path::append(Result, ModuleName + ".pcm");
207     if (getFileMgr().getFile(Result.str()))
208       return std::string(Result);
209   }
210 
211   return {};
212 }
213 
214 std::string HeaderSearch::getPrebuiltImplicitModuleFileName(Module *Module) {
215   OptionalFileEntryRef ModuleMap =
216       getModuleMap().getModuleMapFileForUniquing(Module);
217   StringRef ModuleName = Module->Name;
218   StringRef ModuleMapPath = ModuleMap->getName();
219   StringRef ModuleCacheHash = HSOpts->DisableModuleHash ? "" : getModuleHash();
220   for (const std::string &Dir : HSOpts->PrebuiltModulePaths) {
221     SmallString<256> CachePath(Dir);
222     llvm::sys::fs::make_absolute(CachePath);
223     llvm::sys::path::append(CachePath, ModuleCacheHash);
224     std::string FileName =
225         getCachedModuleFileNameImpl(ModuleName, ModuleMapPath, CachePath);
226     if (!FileName.empty() && getFileMgr().getFile(FileName))
227       return FileName;
228   }
229   return {};
230 }
231 
232 std::string HeaderSearch::getCachedModuleFileName(StringRef ModuleName,
233                                                   StringRef ModuleMapPath) {
234   return getCachedModuleFileNameImpl(ModuleName, ModuleMapPath,
235                                      getModuleCachePath());
236 }
237 
238 std::string HeaderSearch::getCachedModuleFileNameImpl(StringRef ModuleName,
239                                                       StringRef ModuleMapPath,
240                                                       StringRef CachePath) {
241   // If we don't have a module cache path or aren't supposed to use one, we
242   // can't do anything.
243   if (CachePath.empty())
244     return {};
245 
246   SmallString<256> Result(CachePath);
247   llvm::sys::fs::make_absolute(Result);
248 
249   if (HSOpts->DisableModuleHash) {
250     llvm::sys::path::append(Result, ModuleName + ".pcm");
251   } else {
252     // Construct the name <ModuleName>-<hash of ModuleMapPath>.pcm which should
253     // ideally be globally unique to this particular module. Name collisions
254     // in the hash are safe (because any translation unit can only import one
255     // module with each name), but result in a loss of caching.
256     //
257     // To avoid false-negatives, we form as canonical a path as we can, and map
258     // to lower-case in case we're on a case-insensitive file system.
259     SmallString<128> CanonicalPath(ModuleMapPath);
260     if (getModuleMap().canonicalizeModuleMapPath(CanonicalPath))
261       return {};
262 
263     llvm::hash_code Hash = llvm::hash_combine(CanonicalPath.str().lower());
264 
265     SmallString<128> HashStr;
266     llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36);
267     llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm");
268   }
269   return Result.str().str();
270 }
271 
272 Module *HeaderSearch::lookupModule(StringRef ModuleName,
273                                    SourceLocation ImportLoc, bool AllowSearch,
274                                    bool AllowExtraModuleMapSearch) {
275   // Look in the module map to determine if there is a module by this name.
276   Module *Module = ModMap.findModule(ModuleName);
277   if (Module || !AllowSearch || !HSOpts->ImplicitModuleMaps)
278     return Module;
279 
280   StringRef SearchName = ModuleName;
281   Module = lookupModule(ModuleName, SearchName, ImportLoc,
282                         AllowExtraModuleMapSearch);
283 
284   // The facility for "private modules" -- adjacent, optional module maps named
285   // module.private.modulemap that are supposed to define private submodules --
286   // may have different flavors of names: FooPrivate, Foo_Private and Foo.Private.
287   //
288   // Foo.Private is now deprecated in favor of Foo_Private. Users of FooPrivate
289   // should also rename to Foo_Private. Representing private as submodules
290   // could force building unwanted dependencies into the parent module and cause
291   // dependency cycles.
292   if (!Module && SearchName.consume_back("_Private"))
293     Module = lookupModule(ModuleName, SearchName, ImportLoc,
294                           AllowExtraModuleMapSearch);
295   if (!Module && SearchName.consume_back("Private"))
296     Module = lookupModule(ModuleName, SearchName, ImportLoc,
297                           AllowExtraModuleMapSearch);
298   return Module;
299 }
300 
301 Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName,
302                                    SourceLocation ImportLoc,
303                                    bool AllowExtraModuleMapSearch) {
304   Module *Module = nullptr;
305 
306   // Look through the various header search paths to load any available module
307   // maps, searching for a module map that describes this module.
308   for (DirectoryLookup &Dir : search_dir_range()) {
309     if (Dir.isFramework()) {
310       // Search for or infer a module map for a framework. Here we use
311       // SearchName rather than ModuleName, to permit finding private modules
312       // named FooPrivate in buggy frameworks named Foo.
313       SmallString<128> FrameworkDirName;
314       FrameworkDirName += Dir.getFrameworkDir()->getName();
315       llvm::sys::path::append(FrameworkDirName, SearchName + ".framework");
316       if (auto FrameworkDir =
317               FileMgr.getOptionalDirectoryRef(FrameworkDirName)) {
318         bool IsSystem = Dir.getDirCharacteristic() != SrcMgr::C_User;
319         Module = loadFrameworkModule(ModuleName, *FrameworkDir, IsSystem);
320         if (Module)
321           break;
322       }
323     }
324 
325     // FIXME: Figure out how header maps and module maps will work together.
326 
327     // Only deal with normal search directories.
328     if (!Dir.isNormalDir())
329       continue;
330 
331     bool IsSystem = Dir.isSystemHeaderDirectory();
332     // Only returns std::nullopt if not a normal directory, which we just
333     // checked
334     DirectoryEntryRef NormalDir = *Dir.getDirRef();
335     // Search for a module map file in this directory.
336     if (loadModuleMapFile(NormalDir, IsSystem,
337                           /*IsFramework*/false) == LMM_NewlyLoaded) {
338       // We just loaded a module map file; check whether the module is
339       // available now.
340       Module = ModMap.findModule(ModuleName);
341       if (Module)
342         break;
343     }
344 
345     // Search for a module map in a subdirectory with the same name as the
346     // module.
347     SmallString<128> NestedModuleMapDirName;
348     NestedModuleMapDirName = Dir.getDir()->getName();
349     llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
350     if (loadModuleMapFile(NestedModuleMapDirName, IsSystem,
351                           /*IsFramework*/false) == LMM_NewlyLoaded){
352       // If we just loaded a module map file, look for the module again.
353       Module = ModMap.findModule(ModuleName);
354       if (Module)
355         break;
356     }
357 
358     // If we've already performed the exhaustive search for module maps in this
359     // search directory, don't do it again.
360     if (Dir.haveSearchedAllModuleMaps())
361       continue;
362 
363     // Load all module maps in the immediate subdirectories of this search
364     // directory if ModuleName was from @import.
365     if (AllowExtraModuleMapSearch)
366       loadSubdirectoryModuleMaps(Dir);
367 
368     // Look again for the module.
369     Module = ModMap.findModule(ModuleName);
370     if (Module)
371       break;
372   }
373 
374   return Module;
375 }
376 
377 void HeaderSearch::indexInitialHeaderMaps() {
378   llvm::StringMap<unsigned, llvm::BumpPtrAllocator> Index(SearchDirs.size());
379 
380   // Iterate over all filename keys and associate them with the index i.
381   unsigned i = 0;
382   for (; i != SearchDirs.size(); ++i) {
383     auto &Dir = SearchDirs[i];
384 
385     // We're concerned with only the initial contiguous run of header
386     // maps within SearchDirs, which can be 99% of SearchDirs when
387     // SearchDirs.size() is ~10000.
388     if (!Dir.isHeaderMap())
389       break;
390 
391     // Give earlier keys precedence over identical later keys.
392     auto Callback = [&](StringRef Filename) {
393       Index.try_emplace(Filename.lower(), i);
394     };
395     Dir.getHeaderMap()->forEachKey(Callback);
396   }
397 
398   SearchDirHeaderMapIndex = std::move(Index);
399   FirstNonHeaderMapSearchDirIdx = i;
400 }
401 
402 //===----------------------------------------------------------------------===//
403 // File lookup within a DirectoryLookup scope
404 //===----------------------------------------------------------------------===//
405 
406 /// getName - Return the directory or filename corresponding to this lookup
407 /// object.
408 StringRef DirectoryLookup::getName() const {
409   // FIXME: Use the name from \c DirectoryEntryRef.
410   if (isNormalDir())
411     return getDir()->getName();
412   if (isFramework())
413     return getFrameworkDir()->getName();
414   assert(isHeaderMap() && "Unknown DirectoryLookup");
415   return getHeaderMap()->getFileName();
416 }
417 
418 OptionalFileEntryRef HeaderSearch::getFileAndSuggestModule(
419     StringRef FileName, SourceLocation IncludeLoc, const DirectoryEntry *Dir,
420     bool IsSystemHeaderDir, Module *RequestingModule,
421     ModuleMap::KnownHeader *SuggestedModule, bool OpenFile /*=true*/,
422     bool CacheFailures /*=true*/) {
423   // If we have a module map that might map this header, load it and
424   // check whether we'll have a suggestion for a module.
425   auto File = getFileMgr().getFileRef(FileName, OpenFile, CacheFailures);
426   if (!File) {
427     // For rare, surprising errors (e.g. "out of file handles"), diag the EC
428     // message.
429     std::error_code EC = llvm::errorToErrorCode(File.takeError());
430     if (EC != llvm::errc::no_such_file_or_directory &&
431         EC != llvm::errc::invalid_argument &&
432         EC != llvm::errc::is_a_directory && EC != llvm::errc::not_a_directory) {
433       Diags.Report(IncludeLoc, diag::err_cannot_open_file)
434           << FileName << EC.message();
435     }
436     return std::nullopt;
437   }
438 
439   // If there is a module that corresponds to this header, suggest it.
440   if (!findUsableModuleForHeader(
441           &File->getFileEntry(), Dir ? Dir : File->getFileEntry().getDir(),
442           RequestingModule, SuggestedModule, IsSystemHeaderDir))
443     return std::nullopt;
444 
445   return *File;
446 }
447 
448 /// LookupFile - Lookup the specified file in this search path, returning it
449 /// if it exists or returning null if not.
450 OptionalFileEntryRef DirectoryLookup::LookupFile(
451     StringRef &Filename, HeaderSearch &HS, SourceLocation IncludeLoc,
452     SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
453     Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
454     bool &InUserSpecifiedSystemFramework, bool &IsFrameworkFound,
455     bool &IsInHeaderMap, SmallVectorImpl<char> &MappedName,
456     bool OpenFile) const {
457   InUserSpecifiedSystemFramework = false;
458   IsInHeaderMap = false;
459   MappedName.clear();
460 
461   SmallString<1024> TmpDir;
462   if (isNormalDir()) {
463     // Concatenate the requested file onto the directory.
464     TmpDir = getDirRef()->getName();
465     llvm::sys::path::append(TmpDir, Filename);
466     if (SearchPath) {
467       StringRef SearchPathRef(getDirRef()->getName());
468       SearchPath->clear();
469       SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
470     }
471     if (RelativePath) {
472       RelativePath->clear();
473       RelativePath->append(Filename.begin(), Filename.end());
474     }
475 
476     return HS.getFileAndSuggestModule(
477         TmpDir, IncludeLoc, getDir(), isSystemHeaderDirectory(),
478         RequestingModule, SuggestedModule, OpenFile);
479   }
480 
481   if (isFramework())
482     return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
483                              RequestingModule, SuggestedModule,
484                              InUserSpecifiedSystemFramework, IsFrameworkFound);
485 
486   assert(isHeaderMap() && "Unknown directory lookup");
487   const HeaderMap *HM = getHeaderMap();
488   SmallString<1024> Path;
489   StringRef Dest = HM->lookupFilename(Filename, Path);
490   if (Dest.empty())
491     return std::nullopt;
492 
493   IsInHeaderMap = true;
494 
495   auto FixupSearchPath = [&]() {
496     if (SearchPath) {
497       StringRef SearchPathRef(getName());
498       SearchPath->clear();
499       SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
500     }
501     if (RelativePath) {
502       RelativePath->clear();
503       RelativePath->append(Filename.begin(), Filename.end());
504     }
505   };
506 
507   // Check if the headermap maps the filename to a framework include
508   // ("Foo.h" -> "Foo/Foo.h"), in which case continue header lookup using the
509   // framework include.
510   if (llvm::sys::path::is_relative(Dest)) {
511     MappedName.append(Dest.begin(), Dest.end());
512     Filename = StringRef(MappedName.begin(), MappedName.size());
513     Dest = HM->lookupFilename(Filename, Path);
514   }
515 
516   if (auto Res = HS.getFileMgr().getOptionalFileRef(Dest, OpenFile)) {
517     FixupSearchPath();
518     return *Res;
519   }
520 
521   // Header maps need to be marked as used whenever the filename matches.
522   // The case where the target file **exists** is handled by callee of this
523   // function as part of the regular logic that applies to include search paths.
524   // The case where the target file **does not exist** is handled here:
525   HS.noteLookupUsage(HS.searchDirIdx(*this), IncludeLoc);
526   return std::nullopt;
527 }
528 
529 /// Given a framework directory, find the top-most framework directory.
530 ///
531 /// \param FileMgr The file manager to use for directory lookups.
532 /// \param DirName The name of the framework directory.
533 /// \param SubmodulePath Will be populated with the submodule path from the
534 /// returned top-level module to the originally named framework.
535 static OptionalDirectoryEntryRef
536 getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
537                    SmallVectorImpl<std::string> &SubmodulePath) {
538   assert(llvm::sys::path::extension(DirName) == ".framework" &&
539          "Not a framework directory");
540 
541   // Note: as an egregious but useful hack we use the real path here, because
542   // frameworks moving between top-level frameworks to embedded frameworks tend
543   // to be symlinked, and we base the logical structure of modules on the
544   // physical layout. In particular, we need to deal with crazy includes like
545   //
546   //   #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
547   //
548   // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
549   // which one should access with, e.g.,
550   //
551   //   #include <Bar/Wibble.h>
552   //
553   // Similar issues occur when a top-level framework has moved into an
554   // embedded framework.
555   auto TopFrameworkDir = FileMgr.getOptionalDirectoryRef(DirName);
556 
557   if (TopFrameworkDir)
558     DirName = FileMgr.getCanonicalName(*TopFrameworkDir);
559   do {
560     // Get the parent directory name.
561     DirName = llvm::sys::path::parent_path(DirName);
562     if (DirName.empty())
563       break;
564 
565     // Determine whether this directory exists.
566     auto Dir = FileMgr.getOptionalDirectoryRef(DirName);
567     if (!Dir)
568       break;
569 
570     // If this is a framework directory, then we're a subframework of this
571     // framework.
572     if (llvm::sys::path::extension(DirName) == ".framework") {
573       SubmodulePath.push_back(std::string(llvm::sys::path::stem(DirName)));
574       TopFrameworkDir = *Dir;
575     }
576   } while (true);
577 
578   return TopFrameworkDir;
579 }
580 
581 static bool needModuleLookup(Module *RequestingModule,
582                              bool HasSuggestedModule) {
583   return HasSuggestedModule ||
584          (RequestingModule && RequestingModule->NoUndeclaredIncludes);
585 }
586 
587 /// DoFrameworkLookup - Do a lookup of the specified file in the current
588 /// DirectoryLookup, which is a framework directory.
589 OptionalFileEntryRef DirectoryLookup::DoFrameworkLookup(
590     StringRef Filename, HeaderSearch &HS, SmallVectorImpl<char> *SearchPath,
591     SmallVectorImpl<char> *RelativePath, Module *RequestingModule,
592     ModuleMap::KnownHeader *SuggestedModule,
593     bool &InUserSpecifiedSystemFramework, bool &IsFrameworkFound) const {
594   FileManager &FileMgr = HS.getFileMgr();
595 
596   // Framework names must have a '/' in the filename.
597   size_t SlashPos = Filename.find('/');
598   if (SlashPos == StringRef::npos)
599     return std::nullopt;
600 
601   // Find out if this is the home for the specified framework, by checking
602   // HeaderSearch.  Possible answers are yes/no and unknown.
603   FrameworkCacheEntry &CacheEntry =
604     HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
605 
606   // If it is known and in some other directory, fail.
607   if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDirRef())
608     return std::nullopt;
609 
610   // Otherwise, construct the path to this framework dir.
611 
612   // FrameworkName = "/System/Library/Frameworks/"
613   SmallString<1024> FrameworkName;
614   FrameworkName += getFrameworkDirRef()->getName();
615   if (FrameworkName.empty() || FrameworkName.back() != '/')
616     FrameworkName.push_back('/');
617 
618   // FrameworkName = "/System/Library/Frameworks/Cocoa"
619   StringRef ModuleName(Filename.begin(), SlashPos);
620   FrameworkName += ModuleName;
621 
622   // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
623   FrameworkName += ".framework/";
624 
625   // If the cache entry was unresolved, populate it now.
626   if (!CacheEntry.Directory) {
627     ++NumFrameworkLookups;
628 
629     // If the framework dir doesn't exist, we fail.
630     auto Dir = FileMgr.getDirectory(FrameworkName);
631     if (!Dir)
632       return std::nullopt;
633 
634     // Otherwise, if it does, remember that this is the right direntry for this
635     // framework.
636     CacheEntry.Directory = getFrameworkDirRef();
637 
638     // If this is a user search directory, check if the framework has been
639     // user-specified as a system framework.
640     if (getDirCharacteristic() == SrcMgr::C_User) {
641       SmallString<1024> SystemFrameworkMarker(FrameworkName);
642       SystemFrameworkMarker += ".system_framework";
643       if (llvm::sys::fs::exists(SystemFrameworkMarker)) {
644         CacheEntry.IsUserSpecifiedSystemFramework = true;
645       }
646     }
647   }
648 
649   // Set out flags.
650   InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
651   IsFrameworkFound = CacheEntry.Directory.has_value();
652 
653   if (RelativePath) {
654     RelativePath->clear();
655     RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
656   }
657 
658   // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
659   unsigned OrigSize = FrameworkName.size();
660 
661   FrameworkName += "Headers/";
662 
663   if (SearchPath) {
664     SearchPath->clear();
665     // Without trailing '/'.
666     SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
667   }
668 
669   FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
670 
671   auto File =
672       FileMgr.getOptionalFileRef(FrameworkName, /*OpenFile=*/!SuggestedModule);
673   if (!File) {
674     // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
675     const char *Private = "Private";
676     FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
677                          Private+strlen(Private));
678     if (SearchPath)
679       SearchPath->insert(SearchPath->begin()+OrigSize, Private,
680                          Private+strlen(Private));
681 
682     File = FileMgr.getOptionalFileRef(FrameworkName,
683                                       /*OpenFile=*/!SuggestedModule);
684   }
685 
686   // If we found the header and are allowed to suggest a module, do so now.
687   if (File && needModuleLookup(RequestingModule, SuggestedModule)) {
688     // Find the framework in which this header occurs.
689     StringRef FrameworkPath = File->getFileEntry().getDir()->getName();
690     bool FoundFramework = false;
691     do {
692       // Determine whether this directory exists.
693       auto Dir = FileMgr.getDirectory(FrameworkPath);
694       if (!Dir)
695         break;
696 
697       // If this is a framework directory, then we're a subframework of this
698       // framework.
699       if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
700         FoundFramework = true;
701         break;
702       }
703 
704       // Get the parent directory name.
705       FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
706       if (FrameworkPath.empty())
707         break;
708     } while (true);
709 
710     bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
711     if (FoundFramework) {
712       if (!HS.findUsableModuleForFrameworkHeader(
713               &File->getFileEntry(), FrameworkPath, RequestingModule,
714               SuggestedModule, IsSystem))
715         return std::nullopt;
716     } else {
717       if (!HS.findUsableModuleForHeader(&File->getFileEntry(), getDir(),
718                                         RequestingModule, SuggestedModule,
719                                         IsSystem))
720         return std::nullopt;
721     }
722   }
723   if (File)
724     return *File;
725   return std::nullopt;
726 }
727 
728 void HeaderSearch::cacheLookupSuccess(LookupFileCacheInfo &CacheLookup,
729                                       ConstSearchDirIterator HitIt,
730                                       SourceLocation Loc) {
731   CacheLookup.HitIt = HitIt;
732   noteLookupUsage(HitIt.Idx, Loc);
733 }
734 
735 void HeaderSearch::noteLookupUsage(unsigned HitIdx, SourceLocation Loc) {
736   SearchDirsUsage[HitIdx] = true;
737 
738   auto UserEntryIdxIt = SearchDirToHSEntry.find(HitIdx);
739   if (UserEntryIdxIt != SearchDirToHSEntry.end())
740     Diags.Report(Loc, diag::remark_pp_search_path_usage)
741         << HSOpts->UserEntries[UserEntryIdxIt->second].Path;
742 }
743 
744 void HeaderSearch::setTarget(const TargetInfo &Target) {
745   ModMap.setTarget(Target);
746 }
747 
748 //===----------------------------------------------------------------------===//
749 // Header File Location.
750 //===----------------------------------------------------------------------===//
751 
752 /// Return true with a diagnostic if the file that MSVC would have found
753 /// fails to match the one that Clang would have found with MSVC header search
754 /// disabled.
755 static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags,
756                                   const FileEntry *MSFE, const FileEntry *FE,
757                                   SourceLocation IncludeLoc) {
758   if (MSFE && FE != MSFE) {
759     Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName();
760     return true;
761   }
762   return false;
763 }
764 
765 static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) {
766   assert(!Str.empty());
767   char *CopyStr = Alloc.Allocate<char>(Str.size()+1);
768   std::copy(Str.begin(), Str.end(), CopyStr);
769   CopyStr[Str.size()] = '\0';
770   return CopyStr;
771 }
772 
773 static bool isFrameworkStylePath(StringRef Path, bool &IsPrivateHeader,
774                                  SmallVectorImpl<char> &FrameworkName,
775                                  SmallVectorImpl<char> &IncludeSpelling) {
776   using namespace llvm::sys;
777   path::const_iterator I = path::begin(Path);
778   path::const_iterator E = path::end(Path);
779   IsPrivateHeader = false;
780 
781   // Detect different types of framework style paths:
782   //
783   //   ...Foo.framework/{Headers,PrivateHeaders}
784   //   ...Foo.framework/Versions/{A,Current}/{Headers,PrivateHeaders}
785   //   ...Foo.framework/Frameworks/Nested.framework/{Headers,PrivateHeaders}
786   //   ...<other variations with 'Versions' like in the above path>
787   //
788   // and some other variations among these lines.
789   int FoundComp = 0;
790   while (I != E) {
791     if (*I == "Headers") {
792       ++FoundComp;
793     } else if (*I == "PrivateHeaders") {
794       ++FoundComp;
795       IsPrivateHeader = true;
796     } else if (I->endswith(".framework")) {
797       StringRef Name = I->drop_back(10); // Drop .framework
798       // Need to reset the strings and counter to support nested frameworks.
799       FrameworkName.clear();
800       FrameworkName.append(Name.begin(), Name.end());
801       IncludeSpelling.clear();
802       IncludeSpelling.append(Name.begin(), Name.end());
803       FoundComp = 1;
804     } else if (FoundComp >= 2) {
805       IncludeSpelling.push_back('/');
806       IncludeSpelling.append(I->begin(), I->end());
807     }
808     ++I;
809   }
810 
811   return !FrameworkName.empty() && FoundComp >= 2;
812 }
813 
814 static void
815 diagnoseFrameworkInclude(DiagnosticsEngine &Diags, SourceLocation IncludeLoc,
816                          StringRef Includer, StringRef IncludeFilename,
817                          const FileEntry *IncludeFE, bool isAngled = false,
818                          bool FoundByHeaderMap = false) {
819   bool IsIncluderPrivateHeader = false;
820   SmallString<128> FromFramework, ToFramework;
821   SmallString<128> FromIncludeSpelling, ToIncludeSpelling;
822   if (!isFrameworkStylePath(Includer, IsIncluderPrivateHeader, FromFramework,
823                             FromIncludeSpelling))
824     return;
825   bool IsIncludeePrivateHeader = false;
826   bool IsIncludeeInFramework =
827       isFrameworkStylePath(IncludeFE->getName(), IsIncludeePrivateHeader,
828                            ToFramework, ToIncludeSpelling);
829 
830   if (!isAngled && !FoundByHeaderMap) {
831     SmallString<128> NewInclude("<");
832     if (IsIncludeeInFramework) {
833       NewInclude += ToIncludeSpelling;
834       NewInclude += ">";
835     } else {
836       NewInclude += IncludeFilename;
837       NewInclude += ">";
838     }
839     Diags.Report(IncludeLoc, diag::warn_quoted_include_in_framework_header)
840         << IncludeFilename
841         << FixItHint::CreateReplacement(IncludeLoc, NewInclude);
842   }
843 
844   // Headers in Foo.framework/Headers should not include headers
845   // from Foo.framework/PrivateHeaders, since this violates public/private
846   // API boundaries and can cause modular dependency cycles.
847   if (!IsIncluderPrivateHeader && IsIncludeeInFramework &&
848       IsIncludeePrivateHeader && FromFramework == ToFramework)
849     Diags.Report(IncludeLoc, diag::warn_framework_include_private_from_public)
850         << IncludeFilename;
851 }
852 
853 /// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
854 /// return null on failure.  isAngled indicates whether the file reference is
855 /// for system \#include's or not (i.e. using <> instead of ""). Includers, if
856 /// non-empty, indicates where the \#including file(s) are, in case a relative
857 /// search is needed. Microsoft mode will pass all \#including files.
858 OptionalFileEntryRef HeaderSearch::LookupFile(
859     StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
860     ConstSearchDirIterator FromDir, ConstSearchDirIterator *CurDirArg,
861     ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
862     SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
863     Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
864     bool *IsMapped, bool *IsFrameworkFound, bool SkipCache,
865     bool BuildSystemModule, bool OpenFile, bool CacheFailures) {
866   ConstSearchDirIterator CurDirLocal = nullptr;
867   ConstSearchDirIterator &CurDir = CurDirArg ? *CurDirArg : CurDirLocal;
868 
869   if (IsMapped)
870     *IsMapped = false;
871 
872   if (IsFrameworkFound)
873     *IsFrameworkFound = false;
874 
875   if (SuggestedModule)
876     *SuggestedModule = ModuleMap::KnownHeader();
877 
878   // If 'Filename' is absolute, check to see if it exists and no searching.
879   if (llvm::sys::path::is_absolute(Filename)) {
880     CurDir = nullptr;
881 
882     // If this was an #include_next "/absolute/file", fail.
883     if (FromDir)
884       return std::nullopt;
885 
886     if (SearchPath)
887       SearchPath->clear();
888     if (RelativePath) {
889       RelativePath->clear();
890       RelativePath->append(Filename.begin(), Filename.end());
891     }
892     // Otherwise, just return the file.
893     return getFileAndSuggestModule(Filename, IncludeLoc, nullptr,
894                                    /*IsSystemHeaderDir*/ false,
895                                    RequestingModule, SuggestedModule, OpenFile,
896                                    CacheFailures);
897   }
898 
899   // This is the header that MSVC's header search would have found.
900   ModuleMap::KnownHeader MSSuggestedModule;
901   OptionalFileEntryRef MSFE;
902 
903   // Unless disabled, check to see if the file is in the #includer's
904   // directory.  This cannot be based on CurDir, because each includer could be
905   // a #include of a subdirectory (#include "foo/bar.h") and a subsequent
906   // include of "baz.h" should resolve to "whatever/foo/baz.h".
907   // This search is not done for <> headers.
908   if (!Includers.empty() && !isAngled && !NoCurDirSearch) {
909     SmallString<1024> TmpDir;
910     bool First = true;
911     for (const auto &IncluderAndDir : Includers) {
912       const FileEntry *Includer = IncluderAndDir.first;
913 
914       // Concatenate the requested file onto the directory.
915       // FIXME: Portability.  Filename concatenation should be in sys::Path.
916       TmpDir = IncluderAndDir.second->getName();
917       TmpDir.push_back('/');
918       TmpDir.append(Filename.begin(), Filename.end());
919 
920       // FIXME: We don't cache the result of getFileInfo across the call to
921       // getFileAndSuggestModule, because it's a reference to an element of
922       // a container that could be reallocated across this call.
923       //
924       // If we have no includer, that means we're processing a #include
925       // from a module build. We should treat this as a system header if we're
926       // building a [system] module.
927       bool IncluderIsSystemHeader =
928           Includer ? getFileInfo(Includer).DirInfo != SrcMgr::C_User :
929           BuildSystemModule;
930       if (OptionalFileEntryRef FE = getFileAndSuggestModule(
931               TmpDir, IncludeLoc, IncluderAndDir.second, IncluderIsSystemHeader,
932               RequestingModule, SuggestedModule)) {
933         if (!Includer) {
934           assert(First && "only first includer can have no file");
935           return FE;
936         }
937 
938         // Leave CurDir unset.
939         // This file is a system header or C++ unfriendly if the old file is.
940         //
941         // Note that we only use one of FromHFI/ToHFI at once, due to potential
942         // reallocation of the underlying vector potentially making the first
943         // reference binding dangling.
944         HeaderFileInfo &FromHFI = getFileInfo(Includer);
945         unsigned DirInfo = FromHFI.DirInfo;
946         bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
947         StringRef Framework = FromHFI.Framework;
948 
949         HeaderFileInfo &ToHFI = getFileInfo(&FE->getFileEntry());
950         ToHFI.DirInfo = DirInfo;
951         ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
952         ToHFI.Framework = Framework;
953 
954         if (SearchPath) {
955           StringRef SearchPathRef(IncluderAndDir.second->getName());
956           SearchPath->clear();
957           SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
958         }
959         if (RelativePath) {
960           RelativePath->clear();
961           RelativePath->append(Filename.begin(), Filename.end());
962         }
963         if (First) {
964           diagnoseFrameworkInclude(Diags, IncludeLoc,
965                                    IncluderAndDir.second->getName(), Filename,
966                                    &FE->getFileEntry());
967           return FE;
968         }
969 
970         // Otherwise, we found the path via MSVC header search rules.  If
971         // -Wmsvc-include is enabled, we have to keep searching to see if we
972         // would've found this header in -I or -isystem directories.
973         if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) {
974           return FE;
975         } else {
976           MSFE = FE;
977           if (SuggestedModule) {
978             MSSuggestedModule = *SuggestedModule;
979             *SuggestedModule = ModuleMap::KnownHeader();
980           }
981           break;
982         }
983       }
984       First = false;
985     }
986   }
987 
988   CurDir = nullptr;
989 
990   // If this is a system #include, ignore the user #include locs.
991   ConstSearchDirIterator It =
992       isAngled ? angled_dir_begin() : search_dir_begin();
993 
994   // If this is a #include_next request, start searching after the directory the
995   // file was found in.
996   if (FromDir)
997     It = FromDir;
998 
999   // Cache all of the lookups performed by this method.  Many headers are
1000   // multiply included, and the "pragma once" optimization prevents them from
1001   // being relex/pp'd, but they would still have to search through a
1002   // (potentially huge) series of SearchDirs to find it.
1003   LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
1004 
1005   ConstSearchDirIterator NextIt = std::next(It);
1006 
1007   if (!SkipCache) {
1008     if (CacheLookup.StartIt == NextIt) {
1009       // HIT: Skip querying potentially lots of directories for this lookup.
1010       if (CacheLookup.HitIt)
1011         It = CacheLookup.HitIt;
1012       if (CacheLookup.MappedName) {
1013         Filename = CacheLookup.MappedName;
1014         if (IsMapped)
1015           *IsMapped = true;
1016       }
1017     } else {
1018       // MISS: This is the first query, or the previous query didn't match
1019       // our search start.  We will fill in our found location below, so prime
1020       // the start point value.
1021       CacheLookup.reset(/*NewStartIt=*/NextIt);
1022 
1023       if (It == search_dir_begin() && FirstNonHeaderMapSearchDirIdx > 0) {
1024         // Handle cold misses of user includes in the presence of many header
1025         // maps.  We avoid searching perhaps thousands of header maps by
1026         // jumping directly to the correct one or jumping beyond all of them.
1027         auto Iter = SearchDirHeaderMapIndex.find(Filename.lower());
1028         if (Iter == SearchDirHeaderMapIndex.end())
1029           // Not in index => Skip to first SearchDir after initial header maps
1030           It = search_dir_nth(FirstNonHeaderMapSearchDirIdx);
1031         else
1032           // In index => Start with a specific header map
1033           It = search_dir_nth(Iter->second);
1034       }
1035     }
1036   } else
1037     CacheLookup.reset(/*NewStartIt=*/NextIt);
1038 
1039   SmallString<64> MappedName;
1040 
1041   // Check each directory in sequence to see if it contains this file.
1042   for (; It != search_dir_end(); ++It) {
1043     bool InUserSpecifiedSystemFramework = false;
1044     bool IsInHeaderMap = false;
1045     bool IsFrameworkFoundInDir = false;
1046     OptionalFileEntryRef File = It->LookupFile(
1047         Filename, *this, IncludeLoc, SearchPath, RelativePath, RequestingModule,
1048         SuggestedModule, InUserSpecifiedSystemFramework, IsFrameworkFoundInDir,
1049         IsInHeaderMap, MappedName, OpenFile);
1050     if (!MappedName.empty()) {
1051       assert(IsInHeaderMap && "MappedName should come from a header map");
1052       CacheLookup.MappedName =
1053           copyString(MappedName, LookupFileCache.getAllocator());
1054     }
1055     if (IsMapped)
1056       // A filename is mapped when a header map remapped it to a relative path
1057       // used in subsequent header search or to an absolute path pointing to an
1058       // existing file.
1059       *IsMapped |= (!MappedName.empty() || (IsInHeaderMap && File));
1060     if (IsFrameworkFound)
1061       // Because we keep a filename remapped for subsequent search directory
1062       // lookups, ignore IsFrameworkFoundInDir after the first remapping and not
1063       // just for remapping in a current search directory.
1064       *IsFrameworkFound |= (IsFrameworkFoundInDir && !CacheLookup.MappedName);
1065     if (!File)
1066       continue;
1067 
1068     CurDir = It;
1069 
1070     const auto FE = &File->getFileEntry();
1071     IncludeNames[FE] = Filename;
1072 
1073     // This file is a system header or C++ unfriendly if the dir is.
1074     HeaderFileInfo &HFI = getFileInfo(FE);
1075     HFI.DirInfo = CurDir->getDirCharacteristic();
1076 
1077     // If the directory characteristic is User but this framework was
1078     // user-specified to be treated as a system framework, promote the
1079     // characteristic.
1080     if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
1081       HFI.DirInfo = SrcMgr::C_System;
1082 
1083     // If the filename matches a known system header prefix, override
1084     // whether the file is a system header.
1085     for (unsigned j = SystemHeaderPrefixes.size(); j; --j) {
1086       if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
1087         HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
1088                                                        : SrcMgr::C_User;
1089         break;
1090       }
1091     }
1092 
1093     // Set the `Framework` info if this file is in a header map with framework
1094     // style include spelling or found in a framework dir. The header map case
1095     // is possible when building frameworks which use header maps.
1096     if (CurDir->isHeaderMap() && isAngled) {
1097       size_t SlashPos = Filename.find('/');
1098       if (SlashPos != StringRef::npos)
1099         HFI.Framework =
1100             getUniqueFrameworkName(StringRef(Filename.begin(), SlashPos));
1101       if (CurDir->isIndexHeaderMap())
1102         HFI.IndexHeaderMapHeader = 1;
1103     } else if (CurDir->isFramework()) {
1104       size_t SlashPos = Filename.find('/');
1105       if (SlashPos != StringRef::npos)
1106         HFI.Framework =
1107             getUniqueFrameworkName(StringRef(Filename.begin(), SlashPos));
1108     }
1109 
1110     if (checkMSVCHeaderSearch(Diags, MSFE ? &MSFE->getFileEntry() : nullptr,
1111                               &File->getFileEntry(), IncludeLoc)) {
1112       if (SuggestedModule)
1113         *SuggestedModule = MSSuggestedModule;
1114       return MSFE;
1115     }
1116 
1117     bool FoundByHeaderMap = !IsMapped ? false : *IsMapped;
1118     if (!Includers.empty())
1119       diagnoseFrameworkInclude(
1120           Diags, IncludeLoc, Includers.front().second->getName(), Filename,
1121           &File->getFileEntry(), isAngled, FoundByHeaderMap);
1122 
1123     // Remember this location for the next lookup we do.
1124     cacheLookupSuccess(CacheLookup, It, IncludeLoc);
1125     return File;
1126   }
1127 
1128   // If we are including a file with a quoted include "foo.h" from inside
1129   // a header in a framework that is currently being built, and we couldn't
1130   // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
1131   // "Foo" is the name of the framework in which the including header was found.
1132   if (!Includers.empty() && Includers.front().first && !isAngled &&
1133       !Filename.contains('/')) {
1134     HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first);
1135     if (IncludingHFI.IndexHeaderMapHeader) {
1136       SmallString<128> ScratchFilename;
1137       ScratchFilename += IncludingHFI.Framework;
1138       ScratchFilename += '/';
1139       ScratchFilename += Filename;
1140 
1141       OptionalFileEntryRef File = LookupFile(
1142           ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir, &CurDir,
1143           Includers.front(), SearchPath, RelativePath, RequestingModule,
1144           SuggestedModule, IsMapped, /*IsFrameworkFound=*/nullptr);
1145 
1146       if (checkMSVCHeaderSearch(Diags, MSFE ? &MSFE->getFileEntry() : nullptr,
1147                                 File ? &File->getFileEntry() : nullptr,
1148                                 IncludeLoc)) {
1149         if (SuggestedModule)
1150           *SuggestedModule = MSSuggestedModule;
1151         return MSFE;
1152       }
1153 
1154       cacheLookupSuccess(LookupFileCache[Filename],
1155                          LookupFileCache[ScratchFilename].HitIt, IncludeLoc);
1156       // FIXME: SuggestedModule.
1157       return File;
1158     }
1159   }
1160 
1161   if (checkMSVCHeaderSearch(Diags, MSFE ? &MSFE->getFileEntry() : nullptr,
1162                             nullptr, IncludeLoc)) {
1163     if (SuggestedModule)
1164       *SuggestedModule = MSSuggestedModule;
1165     return MSFE;
1166   }
1167 
1168   // Otherwise, didn't find it. Remember we didn't find this.
1169   CacheLookup.HitIt = search_dir_end();
1170   return std::nullopt;
1171 }
1172 
1173 /// LookupSubframeworkHeader - Look up a subframework for the specified
1174 /// \#include file.  For example, if \#include'ing <HIToolbox/HIToolbox.h> from
1175 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
1176 /// is a subframework within Carbon.framework.  If so, return the FileEntry
1177 /// for the designated file, otherwise return null.
1178 OptionalFileEntryRef HeaderSearch::LookupSubframeworkHeader(
1179     StringRef Filename, const FileEntry *ContextFileEnt,
1180     SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
1181     Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule) {
1182   assert(ContextFileEnt && "No context file?");
1183 
1184   // Framework names must have a '/' in the filename.  Find it.
1185   // FIXME: Should we permit '\' on Windows?
1186   size_t SlashPos = Filename.find('/');
1187   if (SlashPos == StringRef::npos)
1188     return std::nullopt;
1189 
1190   // Look up the base framework name of the ContextFileEnt.
1191   StringRef ContextName = ContextFileEnt->getName();
1192 
1193   // If the context info wasn't a framework, couldn't be a subframework.
1194   const unsigned DotFrameworkLen = 10;
1195   auto FrameworkPos = ContextName.find(".framework");
1196   if (FrameworkPos == StringRef::npos ||
1197       (ContextName[FrameworkPos + DotFrameworkLen] != '/' &&
1198        ContextName[FrameworkPos + DotFrameworkLen] != '\\'))
1199     return std::nullopt;
1200 
1201   SmallString<1024> FrameworkName(ContextName.data(), ContextName.data() +
1202                                                           FrameworkPos +
1203                                                           DotFrameworkLen + 1);
1204 
1205   // Append Frameworks/HIToolbox.framework/
1206   FrameworkName += "Frameworks/";
1207   FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
1208   FrameworkName += ".framework/";
1209 
1210   auto &CacheLookup =
1211       *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos),
1212                                           FrameworkCacheEntry())).first;
1213 
1214   // Some other location?
1215   if (CacheLookup.second.Directory &&
1216       CacheLookup.first().size() == FrameworkName.size() &&
1217       memcmp(CacheLookup.first().data(), &FrameworkName[0],
1218              CacheLookup.first().size()) != 0)
1219     return std::nullopt;
1220 
1221   // Cache subframework.
1222   if (!CacheLookup.second.Directory) {
1223     ++NumSubFrameworkLookups;
1224 
1225     // If the framework dir doesn't exist, we fail.
1226     auto Dir = FileMgr.getOptionalDirectoryRef(FrameworkName);
1227     if (!Dir)
1228       return std::nullopt;
1229 
1230     // Otherwise, if it does, remember that this is the right direntry for this
1231     // framework.
1232     CacheLookup.second.Directory = Dir;
1233   }
1234 
1235 
1236   if (RelativePath) {
1237     RelativePath->clear();
1238     RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
1239   }
1240 
1241   // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
1242   SmallString<1024> HeadersFilename(FrameworkName);
1243   HeadersFilename += "Headers/";
1244   if (SearchPath) {
1245     SearchPath->clear();
1246     // Without trailing '/'.
1247     SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
1248   }
1249 
1250   HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
1251   auto File = FileMgr.getOptionalFileRef(HeadersFilename, /*OpenFile=*/true);
1252   if (!File) {
1253     // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
1254     HeadersFilename = FrameworkName;
1255     HeadersFilename += "PrivateHeaders/";
1256     if (SearchPath) {
1257       SearchPath->clear();
1258       // Without trailing '/'.
1259       SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
1260     }
1261 
1262     HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
1263     File = FileMgr.getOptionalFileRef(HeadersFilename, /*OpenFile=*/true);
1264 
1265     if (!File)
1266       return std::nullopt;
1267   }
1268 
1269   // This file is a system header or C++ unfriendly if the old file is.
1270   //
1271   // Note that the temporary 'DirInfo' is required here, as either call to
1272   // getFileInfo could resize the vector and we don't want to rely on order
1273   // of evaluation.
1274   unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
1275   getFileInfo(&File->getFileEntry()).DirInfo = DirInfo;
1276 
1277   FrameworkName.pop_back(); // remove the trailing '/'
1278   if (!findUsableModuleForFrameworkHeader(&File->getFileEntry(), FrameworkName,
1279                                           RequestingModule, SuggestedModule,
1280                                           /*IsSystem*/ false))
1281     return std::nullopt;
1282 
1283   return *File;
1284 }
1285 
1286 //===----------------------------------------------------------------------===//
1287 // File Info Management.
1288 //===----------------------------------------------------------------------===//
1289 
1290 /// Merge the header file info provided by \p OtherHFI into the current
1291 /// header file info (\p HFI)
1292 static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
1293                                 const HeaderFileInfo &OtherHFI) {
1294   assert(OtherHFI.External && "expected to merge external HFI");
1295 
1296   HFI.isImport |= OtherHFI.isImport;
1297   HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
1298   HFI.isModuleHeader |= OtherHFI.isModuleHeader;
1299 
1300   if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
1301     HFI.ControllingMacro = OtherHFI.ControllingMacro;
1302     HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
1303   }
1304 
1305   HFI.DirInfo = OtherHFI.DirInfo;
1306   HFI.External = (!HFI.IsValid || HFI.External);
1307   HFI.IsValid = true;
1308   HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
1309 
1310   if (HFI.Framework.empty())
1311     HFI.Framework = OtherHFI.Framework;
1312 }
1313 
1314 /// getFileInfo - Return the HeaderFileInfo structure for the specified
1315 /// FileEntry.
1316 HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
1317   if (FE->getUID() >= FileInfo.size())
1318     FileInfo.resize(FE->getUID() + 1);
1319 
1320   HeaderFileInfo *HFI = &FileInfo[FE->getUID()];
1321   // FIXME: Use a generation count to check whether this is really up to date.
1322   if (ExternalSource && !HFI->Resolved) {
1323     auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
1324     if (ExternalHFI.IsValid) {
1325       HFI->Resolved = true;
1326       if (ExternalHFI.External)
1327         mergeHeaderFileInfo(*HFI, ExternalHFI);
1328     }
1329   }
1330 
1331   HFI->IsValid = true;
1332   // We have local information about this header file, so it's no longer
1333   // strictly external.
1334   HFI->External = false;
1335   return *HFI;
1336 }
1337 
1338 const HeaderFileInfo *
1339 HeaderSearch::getExistingFileInfo(const FileEntry *FE,
1340                                   bool WantExternal) const {
1341   // If we have an external source, ensure we have the latest information.
1342   // FIXME: Use a generation count to check whether this is really up to date.
1343   HeaderFileInfo *HFI;
1344   if (ExternalSource) {
1345     if (FE->getUID() >= FileInfo.size()) {
1346       if (!WantExternal)
1347         return nullptr;
1348       FileInfo.resize(FE->getUID() + 1);
1349     }
1350 
1351     HFI = &FileInfo[FE->getUID()];
1352     if (!WantExternal && (!HFI->IsValid || HFI->External))
1353       return nullptr;
1354     if (!HFI->Resolved) {
1355       auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
1356       if (ExternalHFI.IsValid) {
1357         HFI->Resolved = true;
1358         if (ExternalHFI.External)
1359           mergeHeaderFileInfo(*HFI, ExternalHFI);
1360       }
1361     }
1362   } else if (FE->getUID() >= FileInfo.size()) {
1363     return nullptr;
1364   } else {
1365     HFI = &FileInfo[FE->getUID()];
1366   }
1367 
1368   if (!HFI->IsValid || (HFI->External && !WantExternal))
1369     return nullptr;
1370 
1371   return HFI;
1372 }
1373 
1374 bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
1375   // Check if we've entered this file and found an include guard or #pragma
1376   // once. Note that we dor't check for #import, because that's not a property
1377   // of the file itself.
1378   if (auto *HFI = getExistingFileInfo(File))
1379     return HFI->isPragmaOnce || HFI->ControllingMacro ||
1380            HFI->ControllingMacroID;
1381   return false;
1382 }
1383 
1384 void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
1385                                         ModuleMap::ModuleHeaderRole Role,
1386                                         bool isCompilingModuleHeader) {
1387   bool isModularHeader = ModuleMap::isModular(Role);
1388 
1389   // Don't mark the file info as non-external if there's nothing to change.
1390   if (!isCompilingModuleHeader) {
1391     if (!isModularHeader)
1392       return;
1393     auto *HFI = getExistingFileInfo(FE);
1394     if (HFI && HFI->isModuleHeader)
1395       return;
1396   }
1397 
1398   auto &HFI = getFileInfo(FE);
1399   HFI.isModuleHeader |= isModularHeader;
1400   HFI.isCompilingModuleHeader |= isCompilingModuleHeader;
1401 }
1402 
1403 bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
1404                                           const FileEntry *File, bool isImport,
1405                                           bool ModulesEnabled, Module *M,
1406                                           bool &IsFirstIncludeOfFile) {
1407   ++NumIncluded; // Count # of attempted #includes.
1408 
1409   IsFirstIncludeOfFile = false;
1410 
1411   // Get information about this file.
1412   HeaderFileInfo &FileInfo = getFileInfo(File);
1413 
1414   // FIXME: this is a workaround for the lack of proper modules-aware support
1415   // for #import / #pragma once
1416   auto TryEnterImported = [&]() -> bool {
1417     if (!ModulesEnabled)
1418       return false;
1419     // Ensure FileInfo bits are up to date.
1420     ModMap.resolveHeaderDirectives(File);
1421     // Modules with builtins are special; multiple modules use builtins as
1422     // modular headers, example:
1423     //
1424     //    module stddef { header "stddef.h" export * }
1425     //
1426     // After module map parsing, this expands to:
1427     //
1428     //    module stddef {
1429     //      header "/path_to_builtin_dirs/stddef.h"
1430     //      textual "stddef.h"
1431     //    }
1432     //
1433     // It's common that libc++ and system modules will both define such
1434     // submodules. Make sure cached results for a builtin header won't
1435     // prevent other builtin modules from potentially entering the builtin
1436     // header. Note that builtins are header guarded and the decision to
1437     // actually enter them is postponed to the controlling macros logic below.
1438     bool TryEnterHdr = false;
1439     if (FileInfo.isCompilingModuleHeader && FileInfo.isModuleHeader)
1440       TryEnterHdr = ModMap.isBuiltinHeader(File);
1441 
1442     // Textual headers can be #imported from different modules. Since ObjC
1443     // headers find in the wild might rely only on #import and do not contain
1444     // controlling macros, be conservative and only try to enter textual headers
1445     // if such macro is present.
1446     if (!FileInfo.isModuleHeader &&
1447         FileInfo.getControllingMacro(ExternalLookup))
1448       TryEnterHdr = true;
1449     return TryEnterHdr;
1450   };
1451 
1452   // If this is a #import directive, check that we have not already imported
1453   // this header.
1454   if (isImport) {
1455     // If this has already been imported, don't import it again.
1456     FileInfo.isImport = true;
1457 
1458     // Has this already been #import'ed or #include'd?
1459     if (PP.alreadyIncluded(File) && !TryEnterImported())
1460       return false;
1461   } else {
1462     // Otherwise, if this is a #include of a file that was previously #import'd
1463     // or if this is the second #include of a #pragma once file, ignore it.
1464     if ((FileInfo.isPragmaOnce || FileInfo.isImport) && !TryEnterImported())
1465       return false;
1466   }
1467 
1468   // Next, check to see if the file is wrapped with #ifndef guards.  If so, and
1469   // if the macro that guards it is defined, we know the #include has no effect.
1470   if (const IdentifierInfo *ControllingMacro
1471       = FileInfo.getControllingMacro(ExternalLookup)) {
1472     // If the header corresponds to a module, check whether the macro is already
1473     // defined in that module rather than checking in the current set of visible
1474     // modules.
1475     if (M ? PP.isMacroDefinedInLocalModule(ControllingMacro, M)
1476           : PP.isMacroDefined(ControllingMacro)) {
1477       ++NumMultiIncludeFileOptzn;
1478       return false;
1479     }
1480   }
1481 
1482   IsFirstIncludeOfFile = PP.markIncluded(File);
1483 
1484   return true;
1485 }
1486 
1487 size_t HeaderSearch::getTotalMemory() const {
1488   return SearchDirs.capacity()
1489     + llvm::capacity_in_bytes(FileInfo)
1490     + llvm::capacity_in_bytes(HeaderMaps)
1491     + LookupFileCache.getAllocator().getTotalMemory()
1492     + FrameworkMap.getAllocator().getTotalMemory();
1493 }
1494 
1495 unsigned HeaderSearch::searchDirIdx(const DirectoryLookup &DL) const {
1496   return &DL - &*SearchDirs.begin();
1497 }
1498 
1499 StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
1500   return FrameworkNames.insert(Framework).first->first();
1501 }
1502 
1503 StringRef HeaderSearch::getIncludeNameForHeader(const FileEntry *File) const {
1504   auto It = IncludeNames.find(File);
1505   if (It == IncludeNames.end())
1506     return {};
1507   return It->second;
1508 }
1509 
1510 bool HeaderSearch::hasModuleMap(StringRef FileName,
1511                                 const DirectoryEntry *Root,
1512                                 bool IsSystem) {
1513   if (!HSOpts->ImplicitModuleMaps)
1514     return false;
1515 
1516   SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
1517 
1518   StringRef DirName = FileName;
1519   do {
1520     // Get the parent directory name.
1521     DirName = llvm::sys::path::parent_path(DirName);
1522     if (DirName.empty())
1523       return false;
1524 
1525     // Determine whether this directory exists.
1526     auto Dir = FileMgr.getOptionalDirectoryRef(DirName);
1527     if (!Dir)
1528       return false;
1529 
1530     // Try to load the module map file in this directory.
1531     switch (loadModuleMapFile(*Dir, IsSystem,
1532                               llvm::sys::path::extension(Dir->getName()) ==
1533                                   ".framework")) {
1534     case LMM_NewlyLoaded:
1535     case LMM_AlreadyLoaded:
1536       // Success. All of the directories we stepped through inherit this module
1537       // map file.
1538       for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
1539         DirectoryHasModuleMap[FixUpDirectories[I]] = true;
1540       return true;
1541 
1542     case LMM_NoDirectory:
1543     case LMM_InvalidModuleMap:
1544       break;
1545     }
1546 
1547     // If we hit the top of our search, we're done.
1548     if (*Dir == Root)
1549       return false;
1550 
1551     // Keep track of all of the directories we checked, so we can mark them as
1552     // having module maps if we eventually do find a module map.
1553     FixUpDirectories.push_back(*Dir);
1554   } while (true);
1555 }
1556 
1557 ModuleMap::KnownHeader
1558 HeaderSearch::findModuleForHeader(const FileEntry *File, bool AllowTextual,
1559                                   bool AllowExcluded) const {
1560   if (ExternalSource) {
1561     // Make sure the external source has handled header info about this file,
1562     // which includes whether the file is part of a module.
1563     (void)getExistingFileInfo(File);
1564   }
1565   return ModMap.findModuleForHeader(File, AllowTextual, AllowExcluded);
1566 }
1567 
1568 ArrayRef<ModuleMap::KnownHeader>
1569 HeaderSearch::findAllModulesForHeader(const FileEntry *File) const {
1570   if (ExternalSource) {
1571     // Make sure the external source has handled header info about this file,
1572     // which includes whether the file is part of a module.
1573     (void)getExistingFileInfo(File);
1574   }
1575   return ModMap.findAllModulesForHeader(File);
1576 }
1577 
1578 static bool suggestModule(HeaderSearch &HS, const FileEntry *File,
1579                           Module *RequestingModule,
1580                           ModuleMap::KnownHeader *SuggestedModule) {
1581   ModuleMap::KnownHeader Module =
1582       HS.findModuleForHeader(File, /*AllowTextual*/true);
1583 
1584   // If this module specifies [no_undeclared_includes], we cannot find any
1585   // file that's in a non-dependency module.
1586   if (RequestingModule && Module && RequestingModule->NoUndeclaredIncludes) {
1587     HS.getModuleMap().resolveUses(RequestingModule, /*Complain*/ false);
1588     if (!RequestingModule->directlyUses(Module.getModule())) {
1589       // Builtin headers are a special case. Multiple modules can use the same
1590       // builtin as a modular header (see also comment in
1591       // ShouldEnterIncludeFile()), so the builtin header may have been
1592       // "claimed" by an unrelated module. This shouldn't prevent us from
1593       // including the builtin header textually in this module.
1594       if (HS.getModuleMap().isBuiltinHeader(File)) {
1595         if (SuggestedModule)
1596           *SuggestedModule = ModuleMap::KnownHeader();
1597         return true;
1598       }
1599       // TODO: Add this module (or just its module map file) into something like
1600       // `RequestingModule->AffectingClangModules`.
1601       return false;
1602     }
1603   }
1604 
1605   if (SuggestedModule)
1606     *SuggestedModule = (Module.getRole() & ModuleMap::TextualHeader)
1607                            ? ModuleMap::KnownHeader()
1608                            : Module;
1609 
1610   return true;
1611 }
1612 
1613 bool HeaderSearch::findUsableModuleForHeader(
1614     const FileEntry *File, const DirectoryEntry *Root, Module *RequestingModule,
1615     ModuleMap::KnownHeader *SuggestedModule, bool IsSystemHeaderDir) {
1616   if (File && needModuleLookup(RequestingModule, SuggestedModule)) {
1617     // If there is a module that corresponds to this header, suggest it.
1618     hasModuleMap(File->getName(), Root, IsSystemHeaderDir);
1619     return suggestModule(*this, File, RequestingModule, SuggestedModule);
1620   }
1621   return true;
1622 }
1623 
1624 bool HeaderSearch::findUsableModuleForFrameworkHeader(
1625     const FileEntry *File, StringRef FrameworkName, Module *RequestingModule,
1626     ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework) {
1627   // If we're supposed to suggest a module, look for one now.
1628   if (needModuleLookup(RequestingModule, SuggestedModule)) {
1629     // Find the top-level framework based on this framework.
1630     SmallVector<std::string, 4> SubmodulePath;
1631     OptionalDirectoryEntryRef TopFrameworkDir =
1632         ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
1633     assert(TopFrameworkDir && "Could not find the top-most framework dir");
1634 
1635     // Determine the name of the top-level framework.
1636     StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
1637 
1638     // Load this framework module. If that succeeds, find the suggested module
1639     // for this header, if any.
1640     loadFrameworkModule(ModuleName, *TopFrameworkDir, IsSystemFramework);
1641 
1642     // FIXME: This can find a module not part of ModuleName, which is
1643     // important so that we're consistent about whether this header
1644     // corresponds to a module. Possibly we should lock down framework modules
1645     // so that this is not possible.
1646     return suggestModule(*this, File, RequestingModule, SuggestedModule);
1647   }
1648   return true;
1649 }
1650 
1651 static const FileEntry *getPrivateModuleMap(const FileEntry *File,
1652                                             FileManager &FileMgr) {
1653   StringRef Filename = llvm::sys::path::filename(File->getName());
1654   SmallString<128>  PrivateFilename(File->getDir()->getName());
1655   if (Filename == "module.map")
1656     llvm::sys::path::append(PrivateFilename, "module_private.map");
1657   else if (Filename == "module.modulemap")
1658     llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
1659   else
1660     return nullptr;
1661   if (auto File = FileMgr.getFile(PrivateFilename))
1662     return *File;
1663   return nullptr;
1664 }
1665 
1666 bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem,
1667                                      FileID ID, unsigned *Offset,
1668                                      StringRef OriginalModuleMapFile) {
1669   // Find the directory for the module. For frameworks, that may require going
1670   // up from the 'Modules' directory.
1671   OptionalDirectoryEntryRef Dir;
1672   if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd) {
1673     Dir = FileMgr.getOptionalDirectoryRef(".");
1674   } else {
1675     if (!OriginalModuleMapFile.empty()) {
1676       // We're building a preprocessed module map. Find or invent the directory
1677       // that it originally occupied.
1678       Dir = FileMgr.getOptionalDirectoryRef(
1679           llvm::sys::path::parent_path(OriginalModuleMapFile));
1680       if (!Dir) {
1681         auto FakeFile = FileMgr.getVirtualFileRef(OriginalModuleMapFile, 0, 0);
1682         Dir = FakeFile.getDir();
1683       }
1684     } else {
1685       // TODO: Replace with `Dir = File.getDir()` when `File` is switched to
1686       // `FileEntryRef`.
1687       Dir = FileMgr.getOptionalDirectoryRef(File->getDir()->getName());
1688     }
1689 
1690     assert(Dir && "parent must exist");
1691     StringRef DirName(Dir->getName());
1692     if (llvm::sys::path::filename(DirName) == "Modules") {
1693       DirName = llvm::sys::path::parent_path(DirName);
1694       if (DirName.endswith(".framework"))
1695         if (auto MaybeDir = FileMgr.getOptionalDirectoryRef(DirName))
1696           Dir = *MaybeDir;
1697       // FIXME: This assert can fail if there's a race between the above check
1698       // and the removal of the directory.
1699       assert(Dir && "parent must exist");
1700     }
1701   }
1702 
1703   assert(Dir && "module map home directory must exist");
1704   switch (loadModuleMapFileImpl(File, IsSystem, *Dir, ID, Offset)) {
1705   case LMM_AlreadyLoaded:
1706   case LMM_NewlyLoaded:
1707     return false;
1708   case LMM_NoDirectory:
1709   case LMM_InvalidModuleMap:
1710     return true;
1711   }
1712   llvm_unreachable("Unknown load module map result");
1713 }
1714 
1715 HeaderSearch::LoadModuleMapResult
1716 HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
1717                                     DirectoryEntryRef Dir, FileID ID,
1718                                     unsigned *Offset) {
1719   assert(File && "expected FileEntry");
1720 
1721   // Check whether we've already loaded this module map, and mark it as being
1722   // loaded in case we recursively try to load it from itself.
1723   auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
1724   if (!AddResult.second)
1725     return AddResult.first->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
1726 
1727   if (ModMap.parseModuleMapFile(File, IsSystem, Dir, ID, Offset)) {
1728     LoadedModuleMaps[File] = false;
1729     return LMM_InvalidModuleMap;
1730   }
1731 
1732   // Try to load a corresponding private module map.
1733   if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) {
1734     if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
1735       LoadedModuleMaps[File] = false;
1736       return LMM_InvalidModuleMap;
1737     }
1738   }
1739 
1740   // This directory has a module map.
1741   return LMM_NewlyLoaded;
1742 }
1743 
1744 const FileEntry *
1745 HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
1746   if (!HSOpts->ImplicitModuleMaps)
1747     return nullptr;
1748   // For frameworks, the preferred spelling is Modules/module.modulemap, but
1749   // module.map at the framework root is also accepted.
1750   SmallString<128> ModuleMapFileName(Dir->getName());
1751   if (IsFramework)
1752     llvm::sys::path::append(ModuleMapFileName, "Modules");
1753   llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
1754   if (auto F = FileMgr.getFile(ModuleMapFileName))
1755     return *F;
1756 
1757   // Continue to allow module.map
1758   ModuleMapFileName = Dir->getName();
1759   llvm::sys::path::append(ModuleMapFileName, "module.map");
1760   if (auto F = FileMgr.getFile(ModuleMapFileName))
1761     return *F;
1762 
1763   // For frameworks, allow to have a private module map with a preferred
1764   // spelling when a public module map is absent.
1765   if (IsFramework) {
1766     ModuleMapFileName = Dir->getName();
1767     llvm::sys::path::append(ModuleMapFileName, "Modules",
1768                             "module.private.modulemap");
1769     if (auto F = FileMgr.getFile(ModuleMapFileName))
1770       return *F;
1771   }
1772   return nullptr;
1773 }
1774 
1775 Module *HeaderSearch::loadFrameworkModule(StringRef Name, DirectoryEntryRef Dir,
1776                                           bool IsSystem) {
1777   if (Module *Module = ModMap.findModule(Name))
1778     return Module;
1779 
1780   // Try to load a module map file.
1781   switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) {
1782   case LMM_InvalidModuleMap:
1783     // Try to infer a module map from the framework directory.
1784     if (HSOpts->ImplicitModuleMaps)
1785       ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr);
1786     break;
1787 
1788   case LMM_AlreadyLoaded:
1789   case LMM_NoDirectory:
1790     return nullptr;
1791 
1792   case LMM_NewlyLoaded:
1793     break;
1794   }
1795 
1796   return ModMap.findModule(Name);
1797 }
1798 
1799 HeaderSearch::LoadModuleMapResult
1800 HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
1801                                 bool IsFramework) {
1802   if (auto Dir = FileMgr.getOptionalDirectoryRef(DirName))
1803     return loadModuleMapFile(*Dir, IsSystem, IsFramework);
1804 
1805   return LMM_NoDirectory;
1806 }
1807 
1808 HeaderSearch::LoadModuleMapResult
1809 HeaderSearch::loadModuleMapFile(DirectoryEntryRef Dir, bool IsSystem,
1810                                 bool IsFramework) {
1811   auto KnownDir = DirectoryHasModuleMap.find(Dir);
1812   if (KnownDir != DirectoryHasModuleMap.end())
1813     return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
1814 
1815   if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
1816     LoadModuleMapResult Result =
1817         loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
1818     // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
1819     // E.g. Foo.framework/Modules/module.modulemap
1820     //      ^Dir                  ^ModuleMapFile
1821     if (Result == LMM_NewlyLoaded)
1822       DirectoryHasModuleMap[Dir] = true;
1823     else if (Result == LMM_InvalidModuleMap)
1824       DirectoryHasModuleMap[Dir] = false;
1825     return Result;
1826   }
1827   return LMM_InvalidModuleMap;
1828 }
1829 
1830 void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
1831   Modules.clear();
1832 
1833   if (HSOpts->ImplicitModuleMaps) {
1834     // Load module maps for each of the header search directories.
1835     for (DirectoryLookup &DL : search_dir_range()) {
1836       bool IsSystem = DL.isSystemHeaderDirectory();
1837       if (DL.isFramework()) {
1838         std::error_code EC;
1839         SmallString<128> DirNative;
1840         llvm::sys::path::native(DL.getFrameworkDir()->getName(), DirNative);
1841 
1842         // Search each of the ".framework" directories to load them as modules.
1843         llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
1844         for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC),
1845                                            DirEnd;
1846              Dir != DirEnd && !EC; Dir.increment(EC)) {
1847           if (llvm::sys::path::extension(Dir->path()) != ".framework")
1848             continue;
1849 
1850           auto FrameworkDir = FileMgr.getOptionalDirectoryRef(Dir->path());
1851           if (!FrameworkDir)
1852             continue;
1853 
1854           // Load this framework module.
1855           loadFrameworkModule(llvm::sys::path::stem(Dir->path()), *FrameworkDir,
1856                               IsSystem);
1857         }
1858         continue;
1859       }
1860 
1861       // FIXME: Deal with header maps.
1862       if (DL.isHeaderMap())
1863         continue;
1864 
1865       // Try to load a module map file for the search directory.
1866       loadModuleMapFile(*DL.getDirRef(), IsSystem, /*IsFramework*/ false);
1867 
1868       // Try to load module map files for immediate subdirectories of this
1869       // search directory.
1870       loadSubdirectoryModuleMaps(DL);
1871     }
1872   }
1873 
1874   // Populate the list of modules.
1875   llvm::transform(ModMap.modules(), std::back_inserter(Modules),
1876                   [](const auto &NameAndMod) { return NameAndMod.second; });
1877 }
1878 
1879 void HeaderSearch::loadTopLevelSystemModules() {
1880   if (!HSOpts->ImplicitModuleMaps)
1881     return;
1882 
1883   // Load module maps for each of the header search directories.
1884   for (const DirectoryLookup &DL : search_dir_range()) {
1885     // We only care about normal header directories.
1886     if (!DL.isNormalDir())
1887       continue;
1888 
1889     // Try to load a module map file for the search directory.
1890     loadModuleMapFile(*DL.getDirRef(), DL.isSystemHeaderDirectory(),
1891                       DL.isFramework());
1892   }
1893 }
1894 
1895 void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
1896   assert(HSOpts->ImplicitModuleMaps &&
1897          "Should not be loading subdirectory module maps");
1898 
1899   if (SearchDir.haveSearchedAllModuleMaps())
1900     return;
1901 
1902   std::error_code EC;
1903   SmallString<128> Dir = SearchDir.getDir()->getName();
1904   FileMgr.makeAbsolutePath(Dir);
1905   SmallString<128> DirNative;
1906   llvm::sys::path::native(Dir, DirNative);
1907   llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
1908   for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
1909        Dir != DirEnd && !EC; Dir.increment(EC)) {
1910     bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework";
1911     if (IsFramework == SearchDir.isFramework())
1912       loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(),
1913                         SearchDir.isFramework());
1914   }
1915 
1916   SearchDir.setSearchedAllModuleMaps(true);
1917 }
1918 
1919 std::string HeaderSearch::suggestPathToFileForDiagnostics(
1920     const FileEntry *File, llvm::StringRef MainFile, bool *IsSystem) {
1921   // FIXME: We assume that the path name currently cached in the FileEntry is
1922   // the most appropriate one for this analysis (and that it's spelled the
1923   // same way as the corresponding header search path).
1924   return suggestPathToFileForDiagnostics(File->getName(), /*WorkingDir=*/"",
1925                                          MainFile, IsSystem);
1926 }
1927 
1928 std::string HeaderSearch::suggestPathToFileForDiagnostics(
1929     llvm::StringRef File, llvm::StringRef WorkingDir, llvm::StringRef MainFile,
1930     bool *IsSystem) {
1931   using namespace llvm::sys;
1932 
1933   llvm::SmallString<32> FilePath = File;
1934   // remove_dots switches to backslashes on windows as a side-effect!
1935   // We always want to suggest forward slashes for includes.
1936   // (not remove_dots(..., posix) as that misparses windows paths).
1937   path::remove_dots(FilePath, /*remove_dot_dot=*/true);
1938   path::native(FilePath, path::Style::posix);
1939   File = FilePath;
1940 
1941   unsigned BestPrefixLength = 0;
1942   // Checks whether `Dir` is a strict path prefix of `File`. If so and that's
1943   // the longest prefix we've seen so for it, returns true and updates the
1944   // `BestPrefixLength` accordingly.
1945   auto CheckDir = [&](llvm::SmallString<32> Dir) -> bool {
1946     if (!WorkingDir.empty() && !path::is_absolute(Dir))
1947       fs::make_absolute(WorkingDir, Dir);
1948     path::remove_dots(Dir, /*remove_dot_dot=*/true);
1949     for (auto NI = path::begin(File), NE = path::end(File),
1950               DI = path::begin(Dir), DE = path::end(Dir);
1951          NI != NE; ++NI, ++DI) {
1952       if (DI == DE) {
1953         // Dir is a prefix of File, up to choice of path separators.
1954         unsigned PrefixLength = NI - path::begin(File);
1955         if (PrefixLength > BestPrefixLength) {
1956           BestPrefixLength = PrefixLength;
1957           return true;
1958         }
1959         break;
1960       }
1961 
1962       // Consider all path separators equal.
1963       if (NI->size() == 1 && DI->size() == 1 &&
1964           path::is_separator(NI->front()) && path::is_separator(DI->front()))
1965         continue;
1966 
1967       // Special case Apple .sdk folders since the search path is typically a
1968       // symlink like `iPhoneSimulator14.5.sdk` while the file is instead
1969       // located in `iPhoneSimulator.sdk` (the real folder).
1970       if (NI->endswith(".sdk") && DI->endswith(".sdk")) {
1971         StringRef NBasename = path::stem(*NI);
1972         StringRef DBasename = path::stem(*DI);
1973         if (DBasename.startswith(NBasename))
1974           continue;
1975       }
1976 
1977       if (*NI != *DI)
1978         break;
1979     }
1980     return false;
1981   };
1982 
1983   bool BestPrefixIsFramework = false;
1984   for (const DirectoryLookup &DL : search_dir_range()) {
1985     if (DL.isNormalDir()) {
1986       StringRef Dir = DL.getDir()->getName();
1987       if (CheckDir(Dir)) {
1988         if (IsSystem)
1989           *IsSystem = BestPrefixLength && isSystem(DL.getDirCharacteristic());
1990         BestPrefixIsFramework = false;
1991       }
1992     } else if (DL.isFramework()) {
1993       StringRef Dir = DL.getFrameworkDir()->getName();
1994       if (CheckDir(Dir)) {
1995         if (IsSystem)
1996           *IsSystem = BestPrefixLength && isSystem(DL.getDirCharacteristic());
1997         BestPrefixIsFramework = true;
1998       }
1999     }
2000   }
2001 
2002   // Try to shorten include path using TUs directory, if we couldn't find any
2003   // suitable prefix in include search paths.
2004   if (!BestPrefixLength && CheckDir(path::parent_path(MainFile))) {
2005     if (IsSystem)
2006       *IsSystem = false;
2007     BestPrefixIsFramework = false;
2008   }
2009 
2010   // Try resolving resulting filename via reverse search in header maps,
2011   // key from header name is user preferred name for the include file.
2012   StringRef Filename = File.drop_front(BestPrefixLength);
2013   for (const DirectoryLookup &DL : search_dir_range()) {
2014     if (!DL.isHeaderMap())
2015       continue;
2016 
2017     StringRef SpelledFilename =
2018         DL.getHeaderMap()->reverseLookupFilename(Filename);
2019     if (!SpelledFilename.empty()) {
2020       Filename = SpelledFilename;
2021       BestPrefixIsFramework = false;
2022       break;
2023     }
2024   }
2025 
2026   // If the best prefix is a framework path, we need to compute the proper
2027   // include spelling for the framework header.
2028   bool IsPrivateHeader;
2029   SmallString<128> FrameworkName, IncludeSpelling;
2030   if (BestPrefixIsFramework &&
2031       isFrameworkStylePath(Filename, IsPrivateHeader, FrameworkName,
2032                            IncludeSpelling)) {
2033     Filename = IncludeSpelling;
2034   }
2035   return path::convert_to_slash(Filename);
2036 }
2037