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