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