1 //===--- HeaderSearch.cpp - Resolve Header File Locations ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the DirectoryLookup and HeaderSearch interfaces.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Lex/HeaderSearch.h"
15 #include "clang/Basic/FileManager.h"
16 #include "clang/Basic/IdentifierTable.h"
17 #include "clang/Lex/HeaderMap.h"
18 #include "clang/Lex/HeaderSearchOptions.h"
19 #include "clang/Lex/LexDiagnostic.h"
20 #include "clang/Lex/Lexer.h"
21 #include "clang/Lex/Preprocessor.h"
22 #include "llvm/ADT/APInt.h"
23 #include "llvm/ADT/Hashing.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/Support/Capacity.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/raw_ostream.h"
30 #if defined(LLVM_ON_UNIX)
33 using namespace clang;
35 const IdentifierInfo *
36 HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) {
38 return ControllingMacro;
40 if (!ControllingMacroID || !External)
43 ControllingMacro = External->GetIdentifier(ControllingMacroID);
44 return ControllingMacro;
47 ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
49 HeaderSearch::HeaderSearch(IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts,
50 SourceManager &SourceMgr, DiagnosticsEngine &Diags,
51 const LangOptions &LangOpts,
52 const TargetInfo *Target)
53 : HSOpts(HSOpts), Diags(Diags), FileMgr(SourceMgr.getFileManager()),
54 FrameworkMap(64), ModMap(SourceMgr, Diags, LangOpts, Target, *this),
58 NoCurDirSearch = false;
60 ExternalLookup = nullptr;
61 ExternalSource = nullptr;
63 NumMultiIncludeFileOptzn = 0;
64 NumFrameworkLookups = NumSubFrameworkLookups = 0;
67 HeaderSearch::~HeaderSearch() {
69 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
70 delete HeaderMaps[i].second;
73 void HeaderSearch::PrintStats() {
74 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
75 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
76 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
77 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
78 NumOnceOnlyFiles += FileInfo[i].isImport;
79 if (MaxNumIncludes < FileInfo[i].NumIncludes)
80 MaxNumIncludes = FileInfo[i].NumIncludes;
81 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
83 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
84 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
85 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
87 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
88 fprintf(stderr, " %d #includes skipped due to"
89 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
91 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
92 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
95 /// CreateHeaderMap - This method returns a HeaderMap for the specified
96 /// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
97 const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
98 // We expect the number of headermaps to be small, and almost always empty.
99 // If it ever grows, use of a linear search should be re-evaluated.
100 if (!HeaderMaps.empty()) {
101 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
102 // Pointer equality comparison of FileEntries works because they are
103 // already uniqued by inode.
104 if (HeaderMaps[i].first == FE)
105 return HeaderMaps[i].second;
108 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
109 HeaderMaps.push_back(std::make_pair(FE, HM));
116 std::string HeaderSearch::getModuleFileName(Module *Module) {
117 const FileEntry *ModuleMap =
118 getModuleMap().getModuleMapFileForUniquing(Module);
119 return getModuleFileName(Module->Name, ModuleMap->getName());
122 std::string HeaderSearch::getModuleFileName(StringRef ModuleName,
123 StringRef ModuleMapPath) {
124 // If we don't have a module cache path, we can't do anything.
125 if (ModuleCachePath.empty())
126 return std::string();
128 SmallString<256> Result(ModuleCachePath);
129 llvm::sys::fs::make_absolute(Result);
131 if (HSOpts->DisableModuleHash) {
132 llvm::sys::path::append(Result, ModuleName + ".pcm");
134 // Construct the name <ModuleName>-<hash of ModuleMapPath>.pcm which should
135 // ideally be globally unique to this particular module. Name collisions
136 // in the hash are safe (because any translation unit can only import one
137 // module with each name), but result in a loss of caching.
139 // To avoid false-negatives, we form as canonical a path as we can, and map
140 // to lower-case in case we're on a case-insensitive file system.
142 FileMgr.getDirectory(llvm::sys::path::parent_path(ModuleMapPath));
144 return std::string();
145 auto DirName = FileMgr.getCanonicalName(Dir);
146 auto FileName = llvm::sys::path::filename(ModuleMapPath);
148 llvm::hash_code Hash =
149 llvm::hash_combine(DirName.lower(), FileName.lower());
151 SmallString<128> HashStr;
152 llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36);
153 llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm");
155 return Result.str().str();
158 Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) {
159 // Look in the module map to determine if there is a module by this name.
160 Module *Module = ModMap.findModule(ModuleName);
161 if (Module || !AllowSearch || !LangOpts.ModulesImplicitMaps)
164 // Look through the various header search paths to load any available module
165 // maps, searching for a module map that describes this module.
166 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
167 if (SearchDirs[Idx].isFramework()) {
168 // Search for or infer a module map for a framework.
169 SmallString<128> FrameworkDirName;
170 FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
171 llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
172 if (const DirectoryEntry *FrameworkDir
173 = FileMgr.getDirectory(FrameworkDirName)) {
175 = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
176 Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem);
182 // FIXME: Figure out how header maps and module maps will work together.
184 // Only deal with normal search directories.
185 if (!SearchDirs[Idx].isNormalDir())
188 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
189 // Search for a module map file in this directory.
190 if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
191 /*IsFramework*/false) == LMM_NewlyLoaded) {
192 // We just loaded a module map file; check whether the module is
194 Module = ModMap.findModule(ModuleName);
199 // Search for a module map in a subdirectory with the same name as the
201 SmallString<128> NestedModuleMapDirName;
202 NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
203 llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
204 if (loadModuleMapFile(NestedModuleMapDirName, IsSystem,
205 /*IsFramework*/false) == LMM_NewlyLoaded){
206 // If we just loaded a module map file, look for the module again.
207 Module = ModMap.findModule(ModuleName);
212 // If we've already performed the exhaustive search for module maps in this
213 // search directory, don't do it again.
214 if (SearchDirs[Idx].haveSearchedAllModuleMaps())
217 // Load all module maps in the immediate subdirectories of this search
219 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
221 // Look again for the module.
222 Module = ModMap.findModule(ModuleName);
230 //===----------------------------------------------------------------------===//
231 // File lookup within a DirectoryLookup scope
232 //===----------------------------------------------------------------------===//
234 /// getName - Return the directory or filename corresponding to this lookup
236 const char *DirectoryLookup::getName() const {
238 return getDir()->getName();
240 return getFrameworkDir()->getName();
241 assert(isHeaderMap() && "Unknown DirectoryLookup");
242 return getHeaderMap()->getFileName();
245 static const FileEntry *
246 getFileAndSuggestModule(HeaderSearch &HS, StringRef FileName,
247 const DirectoryEntry *Dir, bool IsSystemHeaderDir,
248 ModuleMap::KnownHeader *SuggestedModule) {
249 // If we have a module map that might map this header, load it and
250 // check whether we'll have a suggestion for a module.
251 HS.hasModuleMap(FileName, Dir, IsSystemHeaderDir);
252 if (SuggestedModule) {
253 const FileEntry *File = HS.getFileMgr().getFile(FileName,
256 // If there is a module that corresponds to this header, suggest it.
257 *SuggestedModule = HS.findModuleForHeader(File);
259 // FIXME: This appears to be a no-op. We loaded the module map for this
260 // directory at the start of this function.
261 if (!SuggestedModule->getModule() &&
262 HS.hasModuleMap(FileName, Dir, IsSystemHeaderDir))
263 *SuggestedModule = HS.findModuleForHeader(File);
269 return HS.getFileMgr().getFile(FileName, /*openFile=*/true);
272 /// LookupFile - Lookup the specified file in this search path, returning it
273 /// if it exists or returning null if not.
274 const FileEntry *DirectoryLookup::LookupFile(
277 SmallVectorImpl<char> *SearchPath,
278 SmallVectorImpl<char> *RelativePath,
279 ModuleMap::KnownHeader *SuggestedModule,
280 bool &InUserSpecifiedSystemFramework,
282 SmallVectorImpl<char> &MappedName) const {
283 InUserSpecifiedSystemFramework = false;
284 HasBeenMapped = false;
286 SmallString<1024> TmpDir;
288 // Concatenate the requested file onto the directory.
289 TmpDir = getDir()->getName();
290 llvm::sys::path::append(TmpDir, Filename);
292 StringRef SearchPathRef(getDir()->getName());
294 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
297 RelativePath->clear();
298 RelativePath->append(Filename.begin(), Filename.end());
301 return getFileAndSuggestModule(HS, TmpDir, getDir(),
302 isSystemHeaderDirectory(),
307 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
308 SuggestedModule, InUserSpecifiedSystemFramework);
310 assert(isHeaderMap() && "Unknown directory lookup");
311 const HeaderMap *HM = getHeaderMap();
312 SmallString<1024> Path;
313 StringRef Dest = HM->lookupFilename(Filename, Path);
317 const FileEntry *Result;
319 // Check if the headermap maps the filename to a framework include
320 // ("Foo.h" -> "Foo/Foo.h"), in which case continue header lookup using the
321 // framework include.
322 if (llvm::sys::path::is_relative(Dest)) {
324 MappedName.append(Dest.begin(), Dest.end());
325 Filename = StringRef(MappedName.begin(), MappedName.size());
326 HasBeenMapped = true;
327 Result = HM->LookupFile(Filename, HS.getFileMgr());
330 Result = HS.getFileMgr().getFile(Dest);
335 StringRef SearchPathRef(getName());
337 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
340 RelativePath->clear();
341 RelativePath->append(Filename.begin(), Filename.end());
347 /// \brief Given a framework directory, find the top-most framework directory.
349 /// \param FileMgr The file manager to use for directory lookups.
350 /// \param DirName The name of the framework directory.
351 /// \param SubmodulePath Will be populated with the submodule path from the
352 /// returned top-level module to the originally named framework.
353 static const DirectoryEntry *
354 getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
355 SmallVectorImpl<std::string> &SubmodulePath) {
356 assert(llvm::sys::path::extension(DirName) == ".framework" &&
357 "Not a framework directory");
359 // Note: as an egregious but useful hack we use the real path here, because
360 // frameworks moving between top-level frameworks to embedded frameworks tend
361 // to be symlinked, and we base the logical structure of modules on the
362 // physical layout. In particular, we need to deal with crazy includes like
364 // #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
366 // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
367 // which one should access with, e.g.,
369 // #include <Bar/Wibble.h>
371 // Similar issues occur when a top-level framework has moved into an
372 // embedded framework.
373 const DirectoryEntry *TopFrameworkDir = FileMgr.getDirectory(DirName);
374 DirName = FileMgr.getCanonicalName(TopFrameworkDir);
376 // Get the parent directory name.
377 DirName = llvm::sys::path::parent_path(DirName);
381 // Determine whether this directory exists.
382 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
386 // If this is a framework directory, then we're a subframework of this
388 if (llvm::sys::path::extension(DirName) == ".framework") {
389 SubmodulePath.push_back(llvm::sys::path::stem(DirName));
390 TopFrameworkDir = Dir;
394 return TopFrameworkDir;
397 /// DoFrameworkLookup - Do a lookup of the specified file in the current
398 /// DirectoryLookup, which is a framework directory.
399 const FileEntry *DirectoryLookup::DoFrameworkLookup(
402 SmallVectorImpl<char> *SearchPath,
403 SmallVectorImpl<char> *RelativePath,
404 ModuleMap::KnownHeader *SuggestedModule,
405 bool &InUserSpecifiedSystemFramework) const
407 FileManager &FileMgr = HS.getFileMgr();
409 // Framework names must have a '/' in the filename.
410 size_t SlashPos = Filename.find('/');
411 if (SlashPos == StringRef::npos) return nullptr;
413 // Find out if this is the home for the specified framework, by checking
414 // HeaderSearch. Possible answers are yes/no and unknown.
415 HeaderSearch::FrameworkCacheEntry &CacheEntry =
416 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
418 // If it is known and in some other directory, fail.
419 if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir())
422 // Otherwise, construct the path to this framework dir.
424 // FrameworkName = "/System/Library/Frameworks/"
425 SmallString<1024> FrameworkName;
426 FrameworkName += getFrameworkDir()->getName();
427 if (FrameworkName.empty() || FrameworkName.back() != '/')
428 FrameworkName.push_back('/');
430 // FrameworkName = "/System/Library/Frameworks/Cocoa"
431 StringRef ModuleName(Filename.begin(), SlashPos);
432 FrameworkName += ModuleName;
434 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
435 FrameworkName += ".framework/";
437 // If the cache entry was unresolved, populate it now.
438 if (!CacheEntry.Directory) {
439 HS.IncrementFrameworkLookupCount();
441 // If the framework dir doesn't exist, we fail.
442 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
443 if (!Dir) return nullptr;
445 // Otherwise, if it does, remember that this is the right direntry for this
447 CacheEntry.Directory = getFrameworkDir();
449 // If this is a user search directory, check if the framework has been
450 // user-specified as a system framework.
451 if (getDirCharacteristic() == SrcMgr::C_User) {
452 SmallString<1024> SystemFrameworkMarker(FrameworkName);
453 SystemFrameworkMarker += ".system_framework";
454 if (llvm::sys::fs::exists(SystemFrameworkMarker)) {
455 CacheEntry.IsUserSpecifiedSystemFramework = true;
460 // Set the 'user-specified system framework' flag.
461 InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
464 RelativePath->clear();
465 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
468 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
469 unsigned OrigSize = FrameworkName.size();
471 FrameworkName += "Headers/";
475 // Without trailing '/'.
476 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
479 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
480 const FileEntry *FE = FileMgr.getFile(FrameworkName,
481 /*openFile=*/!SuggestedModule);
483 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
484 const char *Private = "Private";
485 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
486 Private+strlen(Private));
488 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
489 Private+strlen(Private));
491 FE = FileMgr.getFile(FrameworkName, /*openFile=*/!SuggestedModule);
494 // If we found the header and are allowed to suggest a module, do so now.
495 if (FE && SuggestedModule) {
496 // Find the framework in which this header occurs.
497 StringRef FrameworkPath = FE->getDir()->getName();
498 bool FoundFramework = false;
500 // Determine whether this directory exists.
501 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath);
505 // If this is a framework directory, then we're a subframework of this
507 if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
508 FoundFramework = true;
512 // Get the parent directory name.
513 FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
514 if (FrameworkPath.empty())
518 if (FoundFramework) {
519 // Find the top-level framework based on this framework.
520 SmallVector<std::string, 4> SubmodulePath;
521 const DirectoryEntry *TopFrameworkDir
522 = ::getTopFrameworkDir(FileMgr, FrameworkPath, SubmodulePath);
524 // Determine the name of the top-level framework.
525 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
527 // Load this framework module. If that succeeds, find the suggested module
528 // for this header, if any.
529 bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
530 if (HS.loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem)) {
531 *SuggestedModule = HS.findModuleForHeader(FE);
534 *SuggestedModule = HS.findModuleForHeader(FE);
540 void HeaderSearch::setTarget(const TargetInfo &Target) {
541 ModMap.setTarget(Target);
545 //===----------------------------------------------------------------------===//
546 // Header File Location.
547 //===----------------------------------------------------------------------===//
549 /// \brief Return true with a diagnostic if the file that MSVC would have found
550 /// fails to match the one that Clang would have found with MSVC header search
552 static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags,
553 const FileEntry *MSFE, const FileEntry *FE,
554 SourceLocation IncludeLoc) {
555 if (MSFE && FE != MSFE) {
556 Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName();
562 static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) {
563 assert(!Str.empty());
564 char *CopyStr = Alloc.Allocate<char>(Str.size()+1);
565 std::copy(Str.begin(), Str.end(), CopyStr);
566 CopyStr[Str.size()] = '\0';
570 /// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
571 /// return null on failure. isAngled indicates whether the file reference is
572 /// for system \#include's or not (i.e. using <> instead of ""). Includers, if
573 /// non-empty, indicates where the \#including file(s) are, in case a relative
574 /// search is needed. Microsoft mode will pass all \#including files.
575 const FileEntry *HeaderSearch::LookupFile(
576 StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
577 const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir,
578 ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
579 SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
580 ModuleMap::KnownHeader *SuggestedModule, bool SkipCache) {
582 *SuggestedModule = ModuleMap::KnownHeader();
584 // If 'Filename' is absolute, check to see if it exists and no searching.
585 if (llvm::sys::path::is_absolute(Filename)) {
588 // If this was an #include_next "/absolute/file", fail.
589 if (FromDir) return nullptr;
594 RelativePath->clear();
595 RelativePath->append(Filename.begin(), Filename.end());
597 // Otherwise, just return the file.
598 const FileEntry *File = FileMgr.getFile(Filename, /*openFile=*/true);
599 if (File && SuggestedModule) {
600 // If there is a module that corresponds to this header, suggest it.
601 hasModuleMap(Filename, File->getDir(), /*SystemHeaderDir*/false);
602 *SuggestedModule = findModuleForHeader(File);
607 // This is the header that MSVC's header search would have found.
608 const FileEntry *MSFE = nullptr;
609 ModuleMap::KnownHeader MSSuggestedModule;
611 // Unless disabled, check to see if the file is in the #includer's
612 // directory. This cannot be based on CurDir, because each includer could be
613 // a #include of a subdirectory (#include "foo/bar.h") and a subsequent
614 // include of "baz.h" should resolve to "whatever/foo/baz.h".
615 // This search is not done for <> headers.
616 if (!Includers.empty() && !isAngled && !NoCurDirSearch) {
617 SmallString<1024> TmpDir;
619 for (const auto &IncluderAndDir : Includers) {
620 const FileEntry *Includer = IncluderAndDir.first;
622 // Concatenate the requested file onto the directory.
623 // FIXME: Portability. Filename concatenation should be in sys::Path.
624 TmpDir = IncluderAndDir.second->getName();
625 TmpDir.push_back('/');
626 TmpDir.append(Filename.begin(), Filename.end());
628 // FIXME: We don't cache the result of getFileInfo across the call to
629 // getFileAndSuggestModule, because it's a reference to an element of
630 // a container that could be reallocated across this call.
632 // FIXME: If we have no includer, that means we're processing a #include
633 // from a module build. We should treat this as a system header if we're
634 // building a [system] module.
635 bool IncluderIsSystemHeader =
636 Includer && getFileInfo(Includer).DirInfo != SrcMgr::C_User;
637 if (const FileEntry *FE = getFileAndSuggestModule(
638 *this, TmpDir, IncluderAndDir.second,
639 IncluderIsSystemHeader, SuggestedModule)) {
641 assert(First && "only first includer can have no file");
645 // Leave CurDir unset.
646 // This file is a system header or C++ unfriendly if the old file is.
648 // Note that we only use one of FromHFI/ToHFI at once, due to potential
649 // reallocation of the underlying vector potentially making the first
650 // reference binding dangling.
651 HeaderFileInfo &FromHFI = getFileInfo(Includer);
652 unsigned DirInfo = FromHFI.DirInfo;
653 bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
654 StringRef Framework = FromHFI.Framework;
656 HeaderFileInfo &ToHFI = getFileInfo(FE);
657 ToHFI.DirInfo = DirInfo;
658 ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
659 ToHFI.Framework = Framework;
662 StringRef SearchPathRef(IncluderAndDir.second->getName());
664 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
667 RelativePath->clear();
668 RelativePath->append(Filename.begin(), Filename.end());
673 // Otherwise, we found the path via MSVC header search rules. If
674 // -Wmsvc-include is enabled, we have to keep searching to see if we
675 // would've found this header in -I or -isystem directories.
676 if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) {
680 if (SuggestedModule) {
681 MSSuggestedModule = *SuggestedModule;
682 *SuggestedModule = ModuleMap::KnownHeader();
693 // If this is a system #include, ignore the user #include locs.
694 unsigned i = isAngled ? AngledDirIdx : 0;
696 // If this is a #include_next request, start searching after the directory the
697 // file was found in.
699 i = FromDir-&SearchDirs[0];
701 // Cache all of the lookups performed by this method. Many headers are
702 // multiply included, and the "pragma once" optimization prevents them from
703 // being relex/pp'd, but they would still have to search through a
704 // (potentially huge) series of SearchDirs to find it.
705 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
707 // If the entry has been previously looked up, the first value will be
708 // non-zero. If the value is equal to i (the start point of our search), then
709 // this is a matching hit.
710 if (!SkipCache && CacheLookup.StartIdx == i+1) {
711 // Skip querying potentially lots of directories for this lookup.
712 i = CacheLookup.HitIdx;
713 if (CacheLookup.MappedName)
714 Filename = CacheLookup.MappedName;
716 // Otherwise, this is the first query, or the previous query didn't match
717 // our search start. We will fill in our found location below, so prime the
718 // start point value.
719 CacheLookup.reset(/*StartIdx=*/i+1);
722 SmallString<64> MappedName;
724 // Check each directory in sequence to see if it contains this file.
725 for (; i != SearchDirs.size(); ++i) {
726 bool InUserSpecifiedSystemFramework = false;
727 bool HasBeenMapped = false;
728 const FileEntry *FE =
729 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath,
730 SuggestedModule, InUserSpecifiedSystemFramework,
731 HasBeenMapped, MappedName);
733 CacheLookup.MappedName =
734 copyString(Filename, LookupFileCache.getAllocator());
738 CurDir = &SearchDirs[i];
740 // This file is a system header or C++ unfriendly if the dir is.
741 HeaderFileInfo &HFI = getFileInfo(FE);
742 HFI.DirInfo = CurDir->getDirCharacteristic();
744 // If the directory characteristic is User but this framework was
745 // user-specified to be treated as a system framework, promote the
747 if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
748 HFI.DirInfo = SrcMgr::C_System;
750 // If the filename matches a known system header prefix, override
751 // whether the file is a system header.
752 for (unsigned j = SystemHeaderPrefixes.size(); j; --j) {
753 if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
754 HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
760 // If this file is found in a header map and uses the framework style of
761 // includes, then this header is part of a framework we're building.
762 if (CurDir->isIndexHeaderMap()) {
763 size_t SlashPos = Filename.find('/');
764 if (SlashPos != StringRef::npos) {
765 HFI.IndexHeaderMapHeader = 1;
766 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
771 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
773 *SuggestedModule = MSSuggestedModule;
777 // Remember this location for the next lookup we do.
778 CacheLookup.HitIdx = i;
782 // If we are including a file with a quoted include "foo.h" from inside
783 // a header in a framework that is currently being built, and we couldn't
784 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
785 // "Foo" is the name of the framework in which the including header was found.
786 if (!Includers.empty() && Includers.front().first && !isAngled &&
787 Filename.find('/') == StringRef::npos) {
788 HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first);
789 if (IncludingHFI.IndexHeaderMapHeader) {
790 SmallString<128> ScratchFilename;
791 ScratchFilename += IncludingHFI.Framework;
792 ScratchFilename += '/';
793 ScratchFilename += Filename;
795 const FileEntry *FE = LookupFile(
796 ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir, CurDir,
797 Includers.front(), SearchPath, RelativePath, SuggestedModule);
799 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
801 *SuggestedModule = MSSuggestedModule;
805 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
806 CacheLookup.HitIdx = LookupFileCache[ScratchFilename].HitIdx;
807 // FIXME: SuggestedModule.
812 if (checkMSVCHeaderSearch(Diags, MSFE, nullptr, IncludeLoc)) {
814 *SuggestedModule = MSSuggestedModule;
818 // Otherwise, didn't find it. Remember we didn't find this.
819 CacheLookup.HitIdx = SearchDirs.size();
823 /// LookupSubframeworkHeader - Look up a subframework for the specified
824 /// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from
825 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
826 /// is a subframework within Carbon.framework. If so, return the FileEntry
827 /// for the designated file, otherwise return null.
828 const FileEntry *HeaderSearch::
829 LookupSubframeworkHeader(StringRef Filename,
830 const FileEntry *ContextFileEnt,
831 SmallVectorImpl<char> *SearchPath,
832 SmallVectorImpl<char> *RelativePath,
833 ModuleMap::KnownHeader *SuggestedModule) {
834 assert(ContextFileEnt && "No context file?");
836 // Framework names must have a '/' in the filename. Find it.
837 // FIXME: Should we permit '\' on Windows?
838 size_t SlashPos = Filename.find('/');
839 if (SlashPos == StringRef::npos) return nullptr;
841 // Look up the base framework name of the ContextFileEnt.
842 const char *ContextName = ContextFileEnt->getName();
844 // If the context info wasn't a framework, couldn't be a subframework.
845 const unsigned DotFrameworkLen = 10;
846 const char *FrameworkPos = strstr(ContextName, ".framework");
847 if (FrameworkPos == nullptr ||
848 (FrameworkPos[DotFrameworkLen] != '/' &&
849 FrameworkPos[DotFrameworkLen] != '\\'))
852 SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1);
854 // Append Frameworks/HIToolbox.framework/
855 FrameworkName += "Frameworks/";
856 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
857 FrameworkName += ".framework/";
860 *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos),
861 FrameworkCacheEntry())).first;
863 // Some other location?
864 if (CacheLookup.second.Directory &&
865 CacheLookup.first().size() == FrameworkName.size() &&
866 memcmp(CacheLookup.first().data(), &FrameworkName[0],
867 CacheLookup.first().size()) != 0)
870 // Cache subframework.
871 if (!CacheLookup.second.Directory) {
872 ++NumSubFrameworkLookups;
874 // If the framework dir doesn't exist, we fail.
875 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
876 if (!Dir) return nullptr;
878 // Otherwise, if it does, remember that this is the right direntry for this
880 CacheLookup.second.Directory = Dir;
883 const FileEntry *FE = nullptr;
886 RelativePath->clear();
887 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
890 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
891 SmallString<1024> HeadersFilename(FrameworkName);
892 HeadersFilename += "Headers/";
895 // Without trailing '/'.
896 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
899 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
900 if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true))) {
902 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
903 HeadersFilename = FrameworkName;
904 HeadersFilename += "PrivateHeaders/";
907 // Without trailing '/'.
908 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
911 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
912 if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true)))
916 // This file is a system header or C++ unfriendly if the old file is.
918 // Note that the temporary 'DirInfo' is required here, as either call to
919 // getFileInfo could resize the vector and we don't want to rely on order
921 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
922 getFileInfo(FE).DirInfo = DirInfo;
924 // If we're supposed to suggest a module, look for one now.
925 if (SuggestedModule) {
926 // Find the top-level framework based on this framework.
927 FrameworkName.pop_back(); // remove the trailing '/'
928 SmallVector<std::string, 4> SubmodulePath;
929 const DirectoryEntry *TopFrameworkDir
930 = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
932 // Determine the name of the top-level framework.
933 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
935 // Load this framework module. If that succeeds, find the suggested module
936 // for this header, if any.
937 bool IsSystem = false;
938 if (loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem)) {
939 *SuggestedModule = findModuleForHeader(FE);
946 //===----------------------------------------------------------------------===//
947 // File Info Management.
948 //===----------------------------------------------------------------------===//
950 /// \brief Merge the header file info provided by \p OtherHFI into the current
951 /// header file info (\p HFI)
952 static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
953 const HeaderFileInfo &OtherHFI) {
954 HFI.isImport |= OtherHFI.isImport;
955 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
956 HFI.isModuleHeader |= OtherHFI.isModuleHeader;
957 HFI.NumIncludes += OtherHFI.NumIncludes;
959 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
960 HFI.ControllingMacro = OtherHFI.ControllingMacro;
961 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
964 if (OtherHFI.External) {
965 HFI.DirInfo = OtherHFI.DirInfo;
966 HFI.External = OtherHFI.External;
967 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
970 if (HFI.Framework.empty())
971 HFI.Framework = OtherHFI.Framework;
976 /// getFileInfo - Return the HeaderFileInfo structure for the specified
978 HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
979 if (FE->getUID() >= FileInfo.size())
980 FileInfo.resize(FE->getUID()+1);
982 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
983 if (ExternalSource && !HFI.Resolved)
984 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE));
989 bool HeaderSearch::tryGetFileInfo(const FileEntry *FE, HeaderFileInfo &Result) const {
990 if (FE->getUID() >= FileInfo.size())
992 const HeaderFileInfo &HFI = FileInfo[FE->getUID()];
1000 bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
1001 // Check if we've ever seen this file as a header.
1002 if (File->getUID() >= FileInfo.size())
1005 // Resolve header file info from the external source, if needed.
1006 HeaderFileInfo &HFI = FileInfo[File->getUID()];
1007 if (ExternalSource && !HFI.Resolved)
1008 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File));
1010 return HFI.isPragmaOnce || HFI.isImport ||
1011 HFI.ControllingMacro || HFI.ControllingMacroID;
1014 void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
1015 ModuleMap::ModuleHeaderRole Role,
1016 bool isCompilingModuleHeader) {
1017 if (FE->getUID() >= FileInfo.size())
1018 FileInfo.resize(FE->getUID()+1);
1020 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
1021 HFI.isModuleHeader = true;
1022 HFI.isCompilingModuleHeader = isCompilingModuleHeader;
1023 HFI.setHeaderRole(Role);
1026 bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
1027 const FileEntry *File,
1029 ++NumIncluded; // Count # of attempted #includes.
1031 // Get information about this file.
1032 HeaderFileInfo &FileInfo = getFileInfo(File);
1034 // If this is a #import directive, check that we have not already imported
1037 // If this has already been imported, don't import it again.
1038 FileInfo.isImport = true;
1040 // Has this already been #import'ed or #include'd?
1041 if (FileInfo.NumIncludes) return false;
1043 // Otherwise, if this is a #include of a file that was previously #import'd
1044 // or if this is the second #include of a #pragma once file, ignore it.
1045 if (FileInfo.isImport)
1049 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
1050 // if the macro that guards it is defined, we know the #include has no effect.
1051 if (const IdentifierInfo *ControllingMacro
1052 = FileInfo.getControllingMacro(ExternalLookup))
1053 if (PP.isMacroDefined(ControllingMacro)) {
1054 ++NumMultiIncludeFileOptzn;
1058 // Increment the number of times this file has been included.
1059 ++FileInfo.NumIncludes;
1064 size_t HeaderSearch::getTotalMemory() const {
1065 return SearchDirs.capacity()
1066 + llvm::capacity_in_bytes(FileInfo)
1067 + llvm::capacity_in_bytes(HeaderMaps)
1068 + LookupFileCache.getAllocator().getTotalMemory()
1069 + FrameworkMap.getAllocator().getTotalMemory();
1072 StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
1073 return FrameworkNames.insert(Framework).first->first();
1076 bool HeaderSearch::hasModuleMap(StringRef FileName,
1077 const DirectoryEntry *Root,
1079 if (!HSOpts->ModuleMaps || !LangOpts.ModulesImplicitMaps)
1082 SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
1084 StringRef DirName = FileName;
1086 // Get the parent directory name.
1087 DirName = llvm::sys::path::parent_path(DirName);
1088 if (DirName.empty())
1091 // Determine whether this directory exists.
1092 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
1096 // Try to load the module map file in this directory.
1097 switch (loadModuleMapFile(Dir, IsSystem,
1098 llvm::sys::path::extension(Dir->getName()) ==
1100 case LMM_NewlyLoaded:
1101 case LMM_AlreadyLoaded:
1102 // Success. All of the directories we stepped through inherit this module
1104 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
1105 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
1108 case LMM_NoDirectory:
1109 case LMM_InvalidModuleMap:
1113 // If we hit the top of our search, we're done.
1117 // Keep track of all of the directories we checked, so we can mark them as
1118 // having module maps if we eventually do find a module map.
1119 FixUpDirectories.push_back(Dir);
1123 ModuleMap::KnownHeader
1124 HeaderSearch::findModuleForHeader(const FileEntry *File) const {
1125 if (ExternalSource) {
1126 // Make sure the external source has handled header info about this file,
1127 // which includes whether the file is part of a module.
1128 (void)getFileInfo(File);
1130 return ModMap.findModuleForHeader(File);
1133 static const FileEntry *getPrivateModuleMap(const FileEntry *File,
1134 FileManager &FileMgr) {
1135 StringRef Filename = llvm::sys::path::filename(File->getName());
1136 SmallString<128> PrivateFilename(File->getDir()->getName());
1137 if (Filename == "module.map")
1138 llvm::sys::path::append(PrivateFilename, "module_private.map");
1139 else if (Filename == "module.modulemap")
1140 llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
1143 return FileMgr.getFile(PrivateFilename);
1146 bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem) {
1147 // Find the directory for the module. For frameworks, that may require going
1148 // up from the 'Modules' directory.
1149 const DirectoryEntry *Dir = nullptr;
1150 if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd)
1151 Dir = FileMgr.getDirectory(".");
1153 Dir = File->getDir();
1154 StringRef DirName(Dir->getName());
1155 if (llvm::sys::path::filename(DirName) == "Modules") {
1156 DirName = llvm::sys::path::parent_path(DirName);
1157 if (DirName.endswith(".framework"))
1158 Dir = FileMgr.getDirectory(DirName);
1159 // FIXME: This assert can fail if there's a race between the above check
1160 // and the removal of the directory.
1161 assert(Dir && "parent must exist");
1165 switch (loadModuleMapFileImpl(File, IsSystem, Dir)) {
1166 case LMM_AlreadyLoaded:
1167 case LMM_NewlyLoaded:
1169 case LMM_NoDirectory:
1170 case LMM_InvalidModuleMap:
1173 llvm_unreachable("Unknown load module map result");
1176 HeaderSearch::LoadModuleMapResult
1177 HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
1178 const DirectoryEntry *Dir) {
1179 assert(File && "expected FileEntry");
1181 // Check whether we've already loaded this module map, and mark it as being
1182 // loaded in case we recursively try to load it from itself.
1183 auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
1184 if (!AddResult.second)
1185 return AddResult.first->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
1187 if (ModMap.parseModuleMapFile(File, IsSystem, Dir)) {
1188 LoadedModuleMaps[File] = false;
1189 return LMM_InvalidModuleMap;
1192 // Try to load a corresponding private module map.
1193 if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) {
1194 if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
1195 LoadedModuleMaps[File] = false;
1196 return LMM_InvalidModuleMap;
1200 // This directory has a module map.
1201 return LMM_NewlyLoaded;
1205 HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
1206 if (!LangOpts.ModulesImplicitMaps)
1208 // For frameworks, the preferred spelling is Modules/module.modulemap, but
1209 // module.map at the framework root is also accepted.
1210 SmallString<128> ModuleMapFileName(Dir->getName());
1212 llvm::sys::path::append(ModuleMapFileName, "Modules");
1213 llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
1214 if (const FileEntry *F = FileMgr.getFile(ModuleMapFileName))
1217 // Continue to allow module.map
1218 ModuleMapFileName = Dir->getName();
1219 llvm::sys::path::append(ModuleMapFileName, "module.map");
1220 return FileMgr.getFile(ModuleMapFileName);
1223 Module *HeaderSearch::loadFrameworkModule(StringRef Name,
1224 const DirectoryEntry *Dir,
1226 if (Module *Module = ModMap.findModule(Name))
1229 // Try to load a module map file.
1230 switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) {
1231 case LMM_InvalidModuleMap:
1234 case LMM_AlreadyLoaded:
1235 case LMM_NoDirectory:
1238 case LMM_NewlyLoaded:
1239 return ModMap.findModule(Name);
1243 // Try to infer a module map from the framework directory.
1244 if (LangOpts.ModulesImplicitMaps)
1245 return ModMap.inferFrameworkModule(Name, Dir, IsSystem, /*Parent=*/nullptr);
1251 HeaderSearch::LoadModuleMapResult
1252 HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
1254 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
1255 return loadModuleMapFile(Dir, IsSystem, IsFramework);
1257 return LMM_NoDirectory;
1260 HeaderSearch::LoadModuleMapResult
1261 HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem,
1263 auto KnownDir = DirectoryHasModuleMap.find(Dir);
1264 if (KnownDir != DirectoryHasModuleMap.end())
1265 return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
1267 if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
1268 LoadModuleMapResult Result =
1269 loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
1270 // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
1271 // E.g. Foo.framework/Modules/module.modulemap
1272 // ^Dir ^ModuleMapFile
1273 if (Result == LMM_NewlyLoaded)
1274 DirectoryHasModuleMap[Dir] = true;
1275 else if (Result == LMM_InvalidModuleMap)
1276 DirectoryHasModuleMap[Dir] = false;
1279 return LMM_InvalidModuleMap;
1282 void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
1285 if (LangOpts.ModulesImplicitMaps) {
1286 // Load module maps for each of the header search directories.
1287 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
1288 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
1289 if (SearchDirs[Idx].isFramework()) {
1291 SmallString<128> DirNative;
1292 llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
1295 // Search each of the ".framework" directories to load them as modules.
1296 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
1297 Dir != DirEnd && !EC; Dir.increment(EC)) {
1298 if (llvm::sys::path::extension(Dir->path()) != ".framework")
1301 const DirectoryEntry *FrameworkDir =
1302 FileMgr.getDirectory(Dir->path());
1306 // Load this framework module.
1307 loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir,
1313 // FIXME: Deal with header maps.
1314 if (SearchDirs[Idx].isHeaderMap())
1317 // Try to load a module map file for the search directory.
1318 loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
1319 /*IsFramework*/ false);
1321 // Try to load module map files for immediate subdirectories of this
1322 // search directory.
1323 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
1327 // Populate the list of modules.
1328 for (ModuleMap::module_iterator M = ModMap.module_begin(),
1329 MEnd = ModMap.module_end();
1331 Modules.push_back(M->getValue());
1335 void HeaderSearch::loadTopLevelSystemModules() {
1336 if (!LangOpts.ModulesImplicitMaps)
1339 // Load module maps for each of the header search directories.
1340 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
1341 // We only care about normal header directories.
1342 if (!SearchDirs[Idx].isNormalDir()) {
1346 // Try to load a module map file for the search directory.
1347 loadModuleMapFile(SearchDirs[Idx].getDir(),
1348 SearchDirs[Idx].isSystemHeaderDirectory(),
1349 SearchDirs[Idx].isFramework());
1353 void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
1354 assert(LangOpts.ModulesImplicitMaps &&
1355 "Should not be loading subdirectory module maps");
1357 if (SearchDir.haveSearchedAllModuleMaps())
1361 SmallString<128> DirNative;
1362 llvm::sys::path::native(SearchDir.getDir()->getName(), DirNative);
1363 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
1364 Dir != DirEnd && !EC; Dir.increment(EC)) {
1365 bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework";
1366 if (IsFramework == SearchDir.isFramework())
1367 loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(),
1368 SearchDir.isFramework());
1371 SearchDir.setSearchedAllModuleMaps(true);