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