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