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