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