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