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/Frontend/PCHContainerOperations.h"
18 #include "clang/Lex/ExternalPreprocessorSource.h"
19 #include "clang/Lex/HeaderMap.h"
20 #include "clang/Lex/HeaderSearchOptions.h"
21 #include "clang/Lex/LexDiagnostic.h"
22 #include "clang/Lex/Lexer.h"
23 #include "clang/Lex/Preprocessor.h"
24 #include "llvm/ADT/APInt.h"
25 #include "llvm/ADT/Hashing.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/Support/Capacity.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/raw_ostream.h"
32 #if defined(LLVM_ON_UNIX)
35 using namespace clang;
37 const IdentifierInfo *
38 HeaderFileInfo::getControllingMacro(ExternalPreprocessorSource *External) {
39 if (ControllingMacro) {
40 if (ControllingMacro->isOutOfDate())
41 External->updateOutOfDateIdentifier(
42 *const_cast<IdentifierInfo *>(ControllingMacro));
43 return ControllingMacro;
46 if (!ControllingMacroID || !External)
49 ControllingMacro = External->GetIdentifier(ControllingMacroID);
50 return ControllingMacro;
53 ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
55 HeaderSearch::HeaderSearch(IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts,
56 SourceManager &SourceMgr, DiagnosticsEngine &Diags,
57 const LangOptions &LangOpts,
58 const TargetInfo *Target)
59 : HSOpts(HSOpts), Diags(Diags), FileMgr(SourceMgr.getFileManager()),
60 FrameworkMap(64), ModMap(SourceMgr, Diags, LangOpts, Target, *this),
64 NoCurDirSearch = false;
66 ExternalLookup = nullptr;
67 ExternalSource = nullptr;
69 NumMultiIncludeFileOptzn = 0;
70 NumFrameworkLookups = NumSubFrameworkLookups = 0;
73 HeaderSearch::~HeaderSearch() {
75 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
76 delete HeaderMaps[i].second;
79 void HeaderSearch::PrintStats() {
80 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
81 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
82 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
83 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
84 NumOnceOnlyFiles += FileInfo[i].isImport;
85 if (MaxNumIncludes < FileInfo[i].NumIncludes)
86 MaxNumIncludes = FileInfo[i].NumIncludes;
87 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
89 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
90 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
91 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
93 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
94 fprintf(stderr, " %d #includes skipped due to"
95 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
97 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
98 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
101 /// CreateHeaderMap - This method returns a HeaderMap for the specified
102 /// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
103 const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
104 // We expect the number of headermaps to be small, and almost always empty.
105 // If it ever grows, use of a linear search should be re-evaluated.
106 if (!HeaderMaps.empty()) {
107 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
108 // Pointer equality comparison of FileEntries works because they are
109 // already uniqued by inode.
110 if (HeaderMaps[i].first == FE)
111 return HeaderMaps[i].second;
114 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
115 HeaderMaps.push_back(std::make_pair(FE, HM));
122 std::string HeaderSearch::getModuleFileName(Module *Module) {
123 const FileEntry *ModuleMap =
124 getModuleMap().getModuleMapFileForUniquing(Module);
125 return getModuleFileName(Module->Name, ModuleMap->getName());
128 std::string HeaderSearch::getModuleFileName(StringRef ModuleName,
129 StringRef ModuleMapPath) {
130 // If we don't have a module cache path or aren't supposed to use one, we
131 // can't do anything.
132 if (getModuleCachePath().empty())
133 return std::string();
135 SmallString<256> Result(getModuleCachePath());
136 llvm::sys::fs::make_absolute(Result);
138 if (HSOpts->DisableModuleHash) {
139 llvm::sys::path::append(Result, ModuleName + ".pcm");
141 // Construct the name <ModuleName>-<hash of ModuleMapPath>.pcm which should
142 // ideally be globally unique to this particular module. Name collisions
143 // in the hash are safe (because any translation unit can only import one
144 // module with each name), but result in a loss of caching.
146 // To avoid false-negatives, we form as canonical a path as we can, and map
147 // to lower-case in case we're on a case-insensitive file system.
149 FileMgr.getDirectory(llvm::sys::path::parent_path(ModuleMapPath));
151 return std::string();
152 auto DirName = FileMgr.getCanonicalName(Dir);
153 auto FileName = llvm::sys::path::filename(ModuleMapPath);
155 llvm::hash_code Hash =
156 llvm::hash_combine(DirName.lower(), FileName.lower(),
157 HSOpts->ModuleFormat, HSOpts->UseDebugInfo);
159 SmallString<128> HashStr;
160 llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36);
161 llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm");
163 return Result.str().str();
166 Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) {
167 // Look in the module map to determine if there is a module by this name.
168 Module *Module = ModMap.findModule(ModuleName);
169 if (Module || !AllowSearch || !HSOpts->ImplicitModuleMaps)
172 // Look through the various header search paths to load any available module
173 // maps, searching for a module map that describes this module.
174 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
175 if (SearchDirs[Idx].isFramework()) {
176 // Search for or infer a module map for a framework.
177 SmallString<128> FrameworkDirName;
178 FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
179 llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
180 if (const DirectoryEntry *FrameworkDir
181 = FileMgr.getDirectory(FrameworkDirName)) {
183 = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
184 Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem);
190 // FIXME: Figure out how header maps and module maps will work together.
192 // Only deal with normal search directories.
193 if (!SearchDirs[Idx].isNormalDir())
196 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
197 // Search for a module map file in this directory.
198 if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
199 /*IsFramework*/false) == LMM_NewlyLoaded) {
200 // We just loaded a module map file; check whether the module is
202 Module = ModMap.findModule(ModuleName);
207 // Search for a module map in a subdirectory with the same name as the
209 SmallString<128> NestedModuleMapDirName;
210 NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
211 llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
212 if (loadModuleMapFile(NestedModuleMapDirName, IsSystem,
213 /*IsFramework*/false) == LMM_NewlyLoaded){
214 // If we just loaded a module map file, look for the module again.
215 Module = ModMap.findModule(ModuleName);
220 // If we've already performed the exhaustive search for module maps in this
221 // search directory, don't do it again.
222 if (SearchDirs[Idx].haveSearchedAllModuleMaps())
225 // Load all module maps in the immediate subdirectories of this search
227 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
229 // Look again for the module.
230 Module = ModMap.findModule(ModuleName);
238 //===----------------------------------------------------------------------===//
239 // File lookup within a DirectoryLookup scope
240 //===----------------------------------------------------------------------===//
242 /// getName - Return the directory or filename corresponding to this lookup
244 const char *DirectoryLookup::getName() const {
246 return getDir()->getName();
248 return getFrameworkDir()->getName();
249 assert(isHeaderMap() && "Unknown DirectoryLookup");
250 return getHeaderMap()->getFileName();
253 const FileEntry *HeaderSearch::getFileAndSuggestModule(
254 StringRef FileName, const DirectoryEntry *Dir, bool IsSystemHeaderDir,
255 Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule) {
256 // If we have a module map that might map this header, load it and
257 // check whether we'll have a suggestion for a module.
258 const FileEntry *File = getFileMgr().getFile(FileName, /*OpenFile=*/true);
260 // If there is a module that corresponds to this header, suggest it.
261 if (!findUsableModuleForHeader(File, Dir ? Dir : File->getDir(),
262 RequestingModule, SuggestedModule,
269 /// LookupFile - Lookup the specified file in this search path, returning it
270 /// if it exists or returning null if not.
271 const FileEntry *DirectoryLookup::LookupFile(
274 SmallVectorImpl<char> *SearchPath,
275 SmallVectorImpl<char> *RelativePath,
276 Module *RequestingModule,
277 ModuleMap::KnownHeader *SuggestedModule,
278 bool &InUserSpecifiedSystemFramework,
280 SmallVectorImpl<char> &MappedName) const {
281 InUserSpecifiedSystemFramework = false;
282 HasBeenMapped = false;
284 SmallString<1024> TmpDir;
286 // Concatenate the requested file onto the directory.
287 TmpDir = getDir()->getName();
288 llvm::sys::path::append(TmpDir, Filename);
290 StringRef SearchPathRef(getDir()->getName());
292 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
295 RelativePath->clear();
296 RelativePath->append(Filename.begin(), Filename.end());
299 return HS.getFileAndSuggestModule(TmpDir, getDir(),
300 isSystemHeaderDirectory(),
301 RequestingModule, SuggestedModule);
305 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
306 RequestingModule, SuggestedModule,
307 InUserSpecifiedSystemFramework);
309 assert(isHeaderMap() && "Unknown directory lookup");
310 const HeaderMap *HM = getHeaderMap();
311 SmallString<1024> Path;
312 StringRef Dest = HM->lookupFilename(Filename, Path);
316 const FileEntry *Result;
318 // Check if the headermap maps the filename to a framework include
319 // ("Foo.h" -> "Foo/Foo.h"), in which case continue header lookup using the
320 // framework include.
321 if (llvm::sys::path::is_relative(Dest)) {
323 MappedName.append(Dest.begin(), Dest.end());
324 Filename = StringRef(MappedName.begin(), MappedName.size());
325 HasBeenMapped = true;
326 Result = HM->LookupFile(Filename, HS.getFileMgr());
329 Result = HS.getFileMgr().getFile(Dest);
334 StringRef SearchPathRef(getName());
336 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
339 RelativePath->clear();
340 RelativePath->append(Filename.begin(), Filename.end());
346 /// \brief Given a framework directory, find the top-most framework directory.
348 /// \param FileMgr The file manager to use for directory lookups.
349 /// \param DirName The name of the framework directory.
350 /// \param SubmodulePath Will be populated with the submodule path from the
351 /// returned top-level module to the originally named framework.
352 static const DirectoryEntry *
353 getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
354 SmallVectorImpl<std::string> &SubmodulePath) {
355 assert(llvm::sys::path::extension(DirName) == ".framework" &&
356 "Not a framework directory");
358 // Note: as an egregious but useful hack we use the real path here, because
359 // frameworks moving between top-level frameworks to embedded frameworks tend
360 // to be symlinked, and we base the logical structure of modules on the
361 // physical layout. In particular, we need to deal with crazy includes like
363 // #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h>
365 // where 'Bar' used to be embedded in 'Foo', is now a top-level framework
366 // which one should access with, e.g.,
368 // #include <Bar/Wibble.h>
370 // Similar issues occur when a top-level framework has moved into an
371 // embedded framework.
372 const DirectoryEntry *TopFrameworkDir = FileMgr.getDirectory(DirName);
373 DirName = FileMgr.getCanonicalName(TopFrameworkDir);
375 // Get the parent directory name.
376 DirName = llvm::sys::path::parent_path(DirName);
380 // Determine whether this directory exists.
381 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
385 // If this is a framework directory, then we're a subframework of this
387 if (llvm::sys::path::extension(DirName) == ".framework") {
388 SubmodulePath.push_back(llvm::sys::path::stem(DirName));
389 TopFrameworkDir = Dir;
393 return TopFrameworkDir;
396 /// DoFrameworkLookup - Do a lookup of the specified file in the current
397 /// DirectoryLookup, which is a framework directory.
398 const FileEntry *DirectoryLookup::DoFrameworkLookup(
399 StringRef Filename, HeaderSearch &HS, SmallVectorImpl<char> *SearchPath,
400 SmallVectorImpl<char> *RelativePath, Module *RequestingModule,
401 ModuleMap::KnownHeader *SuggestedModule,
402 bool &InUserSpecifiedSystemFramework) const {
403 FileManager &FileMgr = HS.getFileMgr();
405 // Framework names must have a '/' in the filename.
406 size_t SlashPos = Filename.find('/');
407 if (SlashPos == StringRef::npos) return nullptr;
409 // Find out if this is the home for the specified framework, by checking
410 // HeaderSearch. Possible answers are yes/no and unknown.
411 HeaderSearch::FrameworkCacheEntry &CacheEntry =
412 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
414 // If it is known and in some other directory, fail.
415 if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir())
418 // Otherwise, construct the path to this framework dir.
420 // FrameworkName = "/System/Library/Frameworks/"
421 SmallString<1024> FrameworkName;
422 FrameworkName += getFrameworkDir()->getName();
423 if (FrameworkName.empty() || FrameworkName.back() != '/')
424 FrameworkName.push_back('/');
426 // FrameworkName = "/System/Library/Frameworks/Cocoa"
427 StringRef ModuleName(Filename.begin(), SlashPos);
428 FrameworkName += ModuleName;
430 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
431 FrameworkName += ".framework/";
433 // If the cache entry was unresolved, populate it now.
434 if (!CacheEntry.Directory) {
435 HS.IncrementFrameworkLookupCount();
437 // If the framework dir doesn't exist, we fail.
438 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
439 if (!Dir) return nullptr;
441 // Otherwise, if it does, remember that this is the right direntry for this
443 CacheEntry.Directory = getFrameworkDir();
445 // If this is a user search directory, check if the framework has been
446 // user-specified as a system framework.
447 if (getDirCharacteristic() == SrcMgr::C_User) {
448 SmallString<1024> SystemFrameworkMarker(FrameworkName);
449 SystemFrameworkMarker += ".system_framework";
450 if (llvm::sys::fs::exists(SystemFrameworkMarker)) {
451 CacheEntry.IsUserSpecifiedSystemFramework = true;
456 // Set the 'user-specified system framework' flag.
457 InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
460 RelativePath->clear();
461 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
464 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
465 unsigned OrigSize = FrameworkName.size();
467 FrameworkName += "Headers/";
471 // Without trailing '/'.
472 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
475 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
476 const FileEntry *FE = FileMgr.getFile(FrameworkName,
477 /*openFile=*/!SuggestedModule);
479 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
480 const char *Private = "Private";
481 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
482 Private+strlen(Private));
484 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
485 Private+strlen(Private));
487 FE = FileMgr.getFile(FrameworkName, /*openFile=*/!SuggestedModule);
490 // If we found the header and are allowed to suggest a module, do so now.
491 if (FE && SuggestedModule) {
492 // Find the framework in which this header occurs.
493 StringRef FrameworkPath = FE->getDir()->getName();
494 bool FoundFramework = false;
496 // Determine whether this directory exists.
497 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath);
501 // If this is a framework directory, then we're a subframework of this
503 if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
504 FoundFramework = true;
508 // Get the parent directory name.
509 FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
510 if (FrameworkPath.empty())
514 bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
515 if (FoundFramework) {
516 if (!HS.findUsableModuleForFrameworkHeader(
517 FE, FrameworkPath, RequestingModule, SuggestedModule, IsSystem))
520 if (!HS.findUsableModuleForHeader(FE, getDir(), RequestingModule,
521 SuggestedModule, IsSystem))
528 void HeaderSearch::setTarget(const TargetInfo &Target) {
529 ModMap.setTarget(Target);
533 //===----------------------------------------------------------------------===//
534 // Header File Location.
535 //===----------------------------------------------------------------------===//
537 /// \brief Return true with a diagnostic if the file that MSVC would have found
538 /// fails to match the one that Clang would have found with MSVC header search
540 static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags,
541 const FileEntry *MSFE, const FileEntry *FE,
542 SourceLocation IncludeLoc) {
543 if (MSFE && FE != MSFE) {
544 Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName();
550 static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) {
551 assert(!Str.empty());
552 char *CopyStr = Alloc.Allocate<char>(Str.size()+1);
553 std::copy(Str.begin(), Str.end(), CopyStr);
554 CopyStr[Str.size()] = '\0';
558 /// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
559 /// return null on failure. isAngled indicates whether the file reference is
560 /// for system \#include's or not (i.e. using <> instead of ""). Includers, if
561 /// non-empty, indicates where the \#including file(s) are, in case a relative
562 /// search is needed. Microsoft mode will pass all \#including files.
563 const FileEntry *HeaderSearch::LookupFile(
564 StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
565 const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir,
566 ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
567 SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
568 Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
571 *SuggestedModule = ModuleMap::KnownHeader();
573 // If 'Filename' is absolute, check to see if it exists and no searching.
574 if (llvm::sys::path::is_absolute(Filename)) {
577 // If this was an #include_next "/absolute/file", fail.
578 if (FromDir) return nullptr;
583 RelativePath->clear();
584 RelativePath->append(Filename.begin(), Filename.end());
586 // Otherwise, just return the file.
587 return getFileAndSuggestModule(Filename, nullptr,
588 /*IsSystemHeaderDir*/false,
589 RequestingModule, SuggestedModule);
592 // This is the header that MSVC's header search would have found.
593 const FileEntry *MSFE = nullptr;
594 ModuleMap::KnownHeader MSSuggestedModule;
596 // Unless disabled, check to see if the file is in the #includer's
597 // directory. This cannot be based on CurDir, because each includer could be
598 // a #include of a subdirectory (#include "foo/bar.h") and a subsequent
599 // include of "baz.h" should resolve to "whatever/foo/baz.h".
600 // This search is not done for <> headers.
601 if (!Includers.empty() && !isAngled && !NoCurDirSearch) {
602 SmallString<1024> TmpDir;
604 for (const auto &IncluderAndDir : Includers) {
605 const FileEntry *Includer = IncluderAndDir.first;
607 // Concatenate the requested file onto the directory.
608 // FIXME: Portability. Filename concatenation should be in sys::Path.
609 TmpDir = IncluderAndDir.second->getName();
610 TmpDir.push_back('/');
611 TmpDir.append(Filename.begin(), Filename.end());
613 // FIXME: We don't cache the result of getFileInfo across the call to
614 // getFileAndSuggestModule, because it's a reference to an element of
615 // a container that could be reallocated across this call.
617 // FIXME: If we have no includer, that means we're processing a #include
618 // from a module build. We should treat this as a system header if we're
619 // building a [system] module.
620 bool IncluderIsSystemHeader =
621 Includer && getFileInfo(Includer).DirInfo != SrcMgr::C_User;
622 if (const FileEntry *FE = getFileAndSuggestModule(
623 TmpDir, IncluderAndDir.second, IncluderIsSystemHeader,
624 RequestingModule, SuggestedModule)) {
626 assert(First && "only first includer can have no file");
630 // Leave CurDir unset.
631 // This file is a system header or C++ unfriendly if the old file is.
633 // Note that we only use one of FromHFI/ToHFI at once, due to potential
634 // reallocation of the underlying vector potentially making the first
635 // reference binding dangling.
636 HeaderFileInfo &FromHFI = getFileInfo(Includer);
637 unsigned DirInfo = FromHFI.DirInfo;
638 bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
639 StringRef Framework = FromHFI.Framework;
641 HeaderFileInfo &ToHFI = getFileInfo(FE);
642 ToHFI.DirInfo = DirInfo;
643 ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
644 ToHFI.Framework = Framework;
647 StringRef SearchPathRef(IncluderAndDir.second->getName());
649 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
652 RelativePath->clear();
653 RelativePath->append(Filename.begin(), Filename.end());
658 // Otherwise, we found the path via MSVC header search rules. If
659 // -Wmsvc-include is enabled, we have to keep searching to see if we
660 // would've found this header in -I or -isystem directories.
661 if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) {
665 if (SuggestedModule) {
666 MSSuggestedModule = *SuggestedModule;
667 *SuggestedModule = ModuleMap::KnownHeader();
678 // If this is a system #include, ignore the user #include locs.
679 unsigned i = isAngled ? AngledDirIdx : 0;
681 // If this is a #include_next request, start searching after the directory the
682 // file was found in.
684 i = FromDir-&SearchDirs[0];
686 // Cache all of the lookups performed by this method. Many headers are
687 // multiply included, and the "pragma once" optimization prevents them from
688 // being relex/pp'd, but they would still have to search through a
689 // (potentially huge) series of SearchDirs to find it.
690 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
692 // If the entry has been previously looked up, the first value will be
693 // non-zero. If the value is equal to i (the start point of our search), then
694 // this is a matching hit.
695 if (!SkipCache && CacheLookup.StartIdx == i+1) {
696 // Skip querying potentially lots of directories for this lookup.
697 i = CacheLookup.HitIdx;
698 if (CacheLookup.MappedName)
699 Filename = CacheLookup.MappedName;
701 // Otherwise, this is the first query, or the previous query didn't match
702 // our search start. We will fill in our found location below, so prime the
703 // start point value.
704 CacheLookup.reset(/*StartIdx=*/i+1);
707 SmallString<64> MappedName;
709 // Check each directory in sequence to see if it contains this file.
710 for (; i != SearchDirs.size(); ++i) {
711 bool InUserSpecifiedSystemFramework = false;
712 bool HasBeenMapped = false;
713 const FileEntry *FE = SearchDirs[i].LookupFile(
714 Filename, *this, SearchPath, RelativePath, RequestingModule,
715 SuggestedModule, InUserSpecifiedSystemFramework, HasBeenMapped,
718 CacheLookup.MappedName =
719 copyString(Filename, LookupFileCache.getAllocator());
723 CurDir = &SearchDirs[i];
725 // This file is a system header or C++ unfriendly if the dir is.
726 HeaderFileInfo &HFI = getFileInfo(FE);
727 HFI.DirInfo = CurDir->getDirCharacteristic();
729 // If the directory characteristic is User but this framework was
730 // user-specified to be treated as a system framework, promote the
732 if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
733 HFI.DirInfo = SrcMgr::C_System;
735 // If the filename matches a known system header prefix, override
736 // whether the file is a system header.
737 for (unsigned j = SystemHeaderPrefixes.size(); j; --j) {
738 if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
739 HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
745 // If this file is found in a header map and uses the framework style of
746 // includes, then this header is part of a framework we're building.
747 if (CurDir->isIndexHeaderMap()) {
748 size_t SlashPos = Filename.find('/');
749 if (SlashPos != StringRef::npos) {
750 HFI.IndexHeaderMapHeader = 1;
751 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
756 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
758 *SuggestedModule = MSSuggestedModule;
762 // Remember this location for the next lookup we do.
763 CacheLookup.HitIdx = i;
767 // If we are including a file with a quoted include "foo.h" from inside
768 // a header in a framework that is currently being built, and we couldn't
769 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
770 // "Foo" is the name of the framework in which the including header was found.
771 if (!Includers.empty() && Includers.front().first && !isAngled &&
772 Filename.find('/') == StringRef::npos) {
773 HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first);
774 if (IncludingHFI.IndexHeaderMapHeader) {
775 SmallString<128> ScratchFilename;
776 ScratchFilename += IncludingHFI.Framework;
777 ScratchFilename += '/';
778 ScratchFilename += Filename;
780 const FileEntry *FE =
781 LookupFile(ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir,
782 CurDir, Includers.front(), SearchPath, RelativePath,
783 RequestingModule, SuggestedModule);
785 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
787 *SuggestedModule = MSSuggestedModule;
791 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
792 CacheLookup.HitIdx = LookupFileCache[ScratchFilename].HitIdx;
793 // FIXME: SuggestedModule.
798 if (checkMSVCHeaderSearch(Diags, MSFE, nullptr, IncludeLoc)) {
800 *SuggestedModule = MSSuggestedModule;
804 // Otherwise, didn't find it. Remember we didn't find this.
805 CacheLookup.HitIdx = SearchDirs.size();
809 /// LookupSubframeworkHeader - Look up a subframework for the specified
810 /// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from
811 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
812 /// is a subframework within Carbon.framework. If so, return the FileEntry
813 /// for the designated file, otherwise return null.
814 const FileEntry *HeaderSearch::
815 LookupSubframeworkHeader(StringRef Filename,
816 const FileEntry *ContextFileEnt,
817 SmallVectorImpl<char> *SearchPath,
818 SmallVectorImpl<char> *RelativePath,
819 Module *RequestingModule,
820 ModuleMap::KnownHeader *SuggestedModule) {
821 assert(ContextFileEnt && "No context file?");
823 // Framework names must have a '/' in the filename. Find it.
824 // FIXME: Should we permit '\' on Windows?
825 size_t SlashPos = Filename.find('/');
826 if (SlashPos == StringRef::npos) return nullptr;
828 // Look up the base framework name of the ContextFileEnt.
829 const char *ContextName = ContextFileEnt->getName();
831 // If the context info wasn't a framework, couldn't be a subframework.
832 const unsigned DotFrameworkLen = 10;
833 const char *FrameworkPos = strstr(ContextName, ".framework");
834 if (FrameworkPos == nullptr ||
835 (FrameworkPos[DotFrameworkLen] != '/' &&
836 FrameworkPos[DotFrameworkLen] != '\\'))
839 SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1);
841 // Append Frameworks/HIToolbox.framework/
842 FrameworkName += "Frameworks/";
843 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
844 FrameworkName += ".framework/";
847 *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos),
848 FrameworkCacheEntry())).first;
850 // Some other location?
851 if (CacheLookup.second.Directory &&
852 CacheLookup.first().size() == FrameworkName.size() &&
853 memcmp(CacheLookup.first().data(), &FrameworkName[0],
854 CacheLookup.first().size()) != 0)
857 // Cache subframework.
858 if (!CacheLookup.second.Directory) {
859 ++NumSubFrameworkLookups;
861 // If the framework dir doesn't exist, we fail.
862 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
863 if (!Dir) return nullptr;
865 // Otherwise, if it does, remember that this is the right direntry for this
867 CacheLookup.second.Directory = Dir;
870 const FileEntry *FE = nullptr;
873 RelativePath->clear();
874 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
877 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
878 SmallString<1024> HeadersFilename(FrameworkName);
879 HeadersFilename += "Headers/";
882 // Without trailing '/'.
883 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
886 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
887 if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true))) {
889 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
890 HeadersFilename = FrameworkName;
891 HeadersFilename += "PrivateHeaders/";
894 // Without trailing '/'.
895 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
898 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
899 if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true)))
903 // This file is a system header or C++ unfriendly if the old file is.
905 // Note that the temporary 'DirInfo' is required here, as either call to
906 // getFileInfo could resize the vector and we don't want to rely on order
908 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
909 getFileInfo(FE).DirInfo = DirInfo;
911 FrameworkName.pop_back(); // remove the trailing '/'
912 if (!findUsableModuleForFrameworkHeader(FE, FrameworkName, RequestingModule,
913 SuggestedModule, /*IsSystem*/ false))
919 //===----------------------------------------------------------------------===//
920 // File Info Management.
921 //===----------------------------------------------------------------------===//
923 /// \brief Merge the header file info provided by \p OtherHFI into the current
924 /// header file info (\p HFI)
925 static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
926 const HeaderFileInfo &OtherHFI) {
927 assert(OtherHFI.External && "expected to merge external HFI");
929 HFI.isImport |= OtherHFI.isImport;
930 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
931 HFI.isModuleHeader |= OtherHFI.isModuleHeader;
932 HFI.NumIncludes += OtherHFI.NumIncludes;
934 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
935 HFI.ControllingMacro = OtherHFI.ControllingMacro;
936 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
939 HFI.DirInfo = OtherHFI.DirInfo;
940 HFI.External = (!HFI.IsValid || HFI.External);
942 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
944 if (HFI.Framework.empty())
945 HFI.Framework = OtherHFI.Framework;
948 /// getFileInfo - Return the HeaderFileInfo structure for the specified
950 HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
951 if (FE->getUID() >= FileInfo.size())
952 FileInfo.resize(FE->getUID() + 1);
954 HeaderFileInfo *HFI = &FileInfo[FE->getUID()];
955 // FIXME: Use a generation count to check whether this is really up to date.
956 if (ExternalSource && !HFI->Resolved) {
957 HFI->Resolved = true;
958 auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
960 HFI = &FileInfo[FE->getUID()];
961 if (ExternalHFI.External)
962 mergeHeaderFileInfo(*HFI, ExternalHFI);
966 // We have local information about this header file, so it's no longer
967 // strictly external.
968 HFI->External = false;
972 const HeaderFileInfo *
973 HeaderSearch::getExistingFileInfo(const FileEntry *FE,
974 bool WantExternal) const {
975 // If we have an external source, ensure we have the latest information.
976 // FIXME: Use a generation count to check whether this is really up to date.
978 if (ExternalSource) {
979 if (FE->getUID() >= FileInfo.size()) {
982 FileInfo.resize(FE->getUID() + 1);
985 HFI = &FileInfo[FE->getUID()];
986 if (!WantExternal && (!HFI->IsValid || HFI->External))
988 if (!HFI->Resolved) {
989 HFI->Resolved = true;
990 auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
992 HFI = &FileInfo[FE->getUID()];
993 if (ExternalHFI.External)
994 mergeHeaderFileInfo(*HFI, ExternalHFI);
996 } else if (FE->getUID() >= FileInfo.size()) {
999 HFI = &FileInfo[FE->getUID()];
1002 if (!HFI->IsValid || (HFI->External && !WantExternal))
1008 bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
1009 // Check if we've ever seen this file as a header.
1010 if (auto *HFI = getExistingFileInfo(File))
1011 return HFI->isPragmaOnce || HFI->isImport || HFI->ControllingMacro ||
1012 HFI->ControllingMacroID;
1016 void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
1017 ModuleMap::ModuleHeaderRole Role,
1018 bool isCompilingModuleHeader) {
1019 bool isModularHeader = !(Role & ModuleMap::TextualHeader);
1021 // Don't mark the file info as non-external if there's nothing to change.
1022 if (!isCompilingModuleHeader) {
1023 if (!isModularHeader)
1025 auto *HFI = getExistingFileInfo(FE);
1026 if (HFI && HFI->isModuleHeader)
1030 auto &HFI = getFileInfo(FE);
1031 HFI.isModuleHeader |= isModularHeader;
1032 HFI.isCompilingModuleHeader |= isCompilingModuleHeader;
1035 bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
1036 const FileEntry *File,
1037 bool isImport, Module *M) {
1038 ++NumIncluded; // Count # of attempted #includes.
1040 // Get information about this file.
1041 HeaderFileInfo &FileInfo = getFileInfo(File);
1043 // If this is a #import directive, check that we have not already imported
1046 // If this has already been imported, don't import it again.
1047 FileInfo.isImport = true;
1049 // Has this already been #import'ed or #include'd?
1050 if (FileInfo.NumIncludes) return false;
1052 // Otherwise, if this is a #include of a file that was previously #import'd
1053 // or if this is the second #include of a #pragma once file, ignore it.
1054 if (FileInfo.isImport)
1058 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
1059 // if the macro that guards it is defined, we know the #include has no effect.
1060 if (const IdentifierInfo *ControllingMacro
1061 = FileInfo.getControllingMacro(ExternalLookup)) {
1062 // If the header corresponds to a module, check whether the macro is already
1063 // defined in that module rather than checking in the current set of visible
1065 if (M ? PP.isMacroDefinedInLocalModule(ControllingMacro, M)
1066 : PP.isMacroDefined(ControllingMacro)) {
1067 ++NumMultiIncludeFileOptzn;
1072 // Increment the number of times this file has been included.
1073 ++FileInfo.NumIncludes;
1078 size_t HeaderSearch::getTotalMemory() const {
1079 return SearchDirs.capacity()
1080 + llvm::capacity_in_bytes(FileInfo)
1081 + llvm::capacity_in_bytes(HeaderMaps)
1082 + LookupFileCache.getAllocator().getTotalMemory()
1083 + FrameworkMap.getAllocator().getTotalMemory();
1086 StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
1087 return FrameworkNames.insert(Framework).first->first();
1090 bool HeaderSearch::hasModuleMap(StringRef FileName,
1091 const DirectoryEntry *Root,
1093 if (!HSOpts->ImplicitModuleMaps)
1096 SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
1098 StringRef DirName = FileName;
1100 // Get the parent directory name.
1101 DirName = llvm::sys::path::parent_path(DirName);
1102 if (DirName.empty())
1105 // Determine whether this directory exists.
1106 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
1110 // Try to load the module map file in this directory.
1111 switch (loadModuleMapFile(Dir, IsSystem,
1112 llvm::sys::path::extension(Dir->getName()) ==
1114 case LMM_NewlyLoaded:
1115 case LMM_AlreadyLoaded:
1116 // Success. All of the directories we stepped through inherit this module
1118 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
1119 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
1122 case LMM_NoDirectory:
1123 case LMM_InvalidModuleMap:
1127 // If we hit the top of our search, we're done.
1131 // Keep track of all of the directories we checked, so we can mark them as
1132 // having module maps if we eventually do find a module map.
1133 FixUpDirectories.push_back(Dir);
1137 ModuleMap::KnownHeader
1138 HeaderSearch::findModuleForHeader(const FileEntry *File) const {
1139 if (ExternalSource) {
1140 // Make sure the external source has handled header info about this file,
1141 // which includes whether the file is part of a module.
1142 (void)getExistingFileInfo(File);
1144 return ModMap.findModuleForHeader(File);
1147 bool HeaderSearch::findUsableModuleForHeader(
1148 const FileEntry *File, const DirectoryEntry *Root, Module *RequestingModule,
1149 ModuleMap::KnownHeader *SuggestedModule, bool IsSystemHeaderDir) {
1150 if (File && SuggestedModule) {
1151 // If there is a module that corresponds to this header, suggest it.
1152 hasModuleMap(File->getName(), Root, IsSystemHeaderDir);
1153 *SuggestedModule = findModuleForHeader(File);
1158 bool HeaderSearch::findUsableModuleForFrameworkHeader(
1159 const FileEntry *File, StringRef FrameworkName, Module *RequestingModule,
1160 ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework) {
1161 // If we're supposed to suggest a module, look for one now.
1162 if (SuggestedModule) {
1163 // Find the top-level framework based on this framework.
1164 SmallVector<std::string, 4> SubmodulePath;
1165 const DirectoryEntry *TopFrameworkDir
1166 = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
1168 // Determine the name of the top-level framework.
1169 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
1171 // Load this framework module. If that succeeds, find the suggested module
1172 // for this header, if any.
1173 loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystemFramework);
1175 // FIXME: This can find a module not part of ModuleName, which is
1176 // important so that we're consistent about whether this header
1177 // corresponds to a module. Possibly we should lock down framework modules
1178 // so that this is not possible.
1179 *SuggestedModule = findModuleForHeader(File);
1184 static const FileEntry *getPrivateModuleMap(const FileEntry *File,
1185 FileManager &FileMgr) {
1186 StringRef Filename = llvm::sys::path::filename(File->getName());
1187 SmallString<128> PrivateFilename(File->getDir()->getName());
1188 if (Filename == "module.map")
1189 llvm::sys::path::append(PrivateFilename, "module_private.map");
1190 else if (Filename == "module.modulemap")
1191 llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
1194 return FileMgr.getFile(PrivateFilename);
1197 bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem) {
1198 // Find the directory for the module. For frameworks, that may require going
1199 // up from the 'Modules' directory.
1200 const DirectoryEntry *Dir = nullptr;
1201 if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd)
1202 Dir = FileMgr.getDirectory(".");
1204 Dir = File->getDir();
1205 StringRef DirName(Dir->getName());
1206 if (llvm::sys::path::filename(DirName) == "Modules") {
1207 DirName = llvm::sys::path::parent_path(DirName);
1208 if (DirName.endswith(".framework"))
1209 Dir = FileMgr.getDirectory(DirName);
1210 // FIXME: This assert can fail if there's a race between the above check
1211 // and the removal of the directory.
1212 assert(Dir && "parent must exist");
1216 switch (loadModuleMapFileImpl(File, IsSystem, Dir)) {
1217 case LMM_AlreadyLoaded:
1218 case LMM_NewlyLoaded:
1220 case LMM_NoDirectory:
1221 case LMM_InvalidModuleMap:
1224 llvm_unreachable("Unknown load module map result");
1227 HeaderSearch::LoadModuleMapResult
1228 HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
1229 const DirectoryEntry *Dir) {
1230 assert(File && "expected FileEntry");
1232 // Check whether we've already loaded this module map, and mark it as being
1233 // loaded in case we recursively try to load it from itself.
1234 auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
1235 if (!AddResult.second)
1236 return AddResult.first->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
1238 if (ModMap.parseModuleMapFile(File, IsSystem, Dir)) {
1239 LoadedModuleMaps[File] = false;
1240 return LMM_InvalidModuleMap;
1243 // Try to load a corresponding private module map.
1244 if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) {
1245 if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
1246 LoadedModuleMaps[File] = false;
1247 return LMM_InvalidModuleMap;
1251 // This directory has a module map.
1252 return LMM_NewlyLoaded;
1256 HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
1257 if (!HSOpts->ImplicitModuleMaps)
1259 // For frameworks, the preferred spelling is Modules/module.modulemap, but
1260 // module.map at the framework root is also accepted.
1261 SmallString<128> ModuleMapFileName(Dir->getName());
1263 llvm::sys::path::append(ModuleMapFileName, "Modules");
1264 llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
1265 if (const FileEntry *F = FileMgr.getFile(ModuleMapFileName))
1268 // Continue to allow module.map
1269 ModuleMapFileName = Dir->getName();
1270 llvm::sys::path::append(ModuleMapFileName, "module.map");
1271 return FileMgr.getFile(ModuleMapFileName);
1274 Module *HeaderSearch::loadFrameworkModule(StringRef Name,
1275 const DirectoryEntry *Dir,
1277 if (Module *Module = ModMap.findModule(Name))
1280 // Try to load a module map file.
1281 switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) {
1282 case LMM_InvalidModuleMap:
1283 // Try to infer a module map from the framework directory.
1284 if (HSOpts->ImplicitModuleMaps)
1285 ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr);
1288 case LMM_AlreadyLoaded:
1289 case LMM_NoDirectory:
1292 case LMM_NewlyLoaded:
1296 return ModMap.findModule(Name);
1300 HeaderSearch::LoadModuleMapResult
1301 HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
1303 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
1304 return loadModuleMapFile(Dir, IsSystem, IsFramework);
1306 return LMM_NoDirectory;
1309 HeaderSearch::LoadModuleMapResult
1310 HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem,
1312 auto KnownDir = DirectoryHasModuleMap.find(Dir);
1313 if (KnownDir != DirectoryHasModuleMap.end())
1314 return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
1316 if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
1317 LoadModuleMapResult Result =
1318 loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
1319 // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
1320 // E.g. Foo.framework/Modules/module.modulemap
1321 // ^Dir ^ModuleMapFile
1322 if (Result == LMM_NewlyLoaded)
1323 DirectoryHasModuleMap[Dir] = true;
1324 else if (Result == LMM_InvalidModuleMap)
1325 DirectoryHasModuleMap[Dir] = false;
1328 return LMM_InvalidModuleMap;
1331 void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
1334 if (HSOpts->ImplicitModuleMaps) {
1335 // Load module maps for each of the header search directories.
1336 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
1337 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
1338 if (SearchDirs[Idx].isFramework()) {
1340 SmallString<128> DirNative;
1341 llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
1344 // Search each of the ".framework" directories to load them as modules.
1345 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
1346 Dir != DirEnd && !EC; Dir.increment(EC)) {
1347 if (llvm::sys::path::extension(Dir->path()) != ".framework")
1350 const DirectoryEntry *FrameworkDir =
1351 FileMgr.getDirectory(Dir->path());
1355 // Load this framework module.
1356 loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir,
1362 // FIXME: Deal with header maps.
1363 if (SearchDirs[Idx].isHeaderMap())
1366 // Try to load a module map file for the search directory.
1367 loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
1368 /*IsFramework*/ false);
1370 // Try to load module map files for immediate subdirectories of this
1371 // search directory.
1372 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
1376 // Populate the list of modules.
1377 for (ModuleMap::module_iterator M = ModMap.module_begin(),
1378 MEnd = ModMap.module_end();
1380 Modules.push_back(M->getValue());
1384 void HeaderSearch::loadTopLevelSystemModules() {
1385 if (!HSOpts->ImplicitModuleMaps)
1388 // Load module maps for each of the header search directories.
1389 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
1390 // We only care about normal header directories.
1391 if (!SearchDirs[Idx].isNormalDir()) {
1395 // Try to load a module map file for the search directory.
1396 loadModuleMapFile(SearchDirs[Idx].getDir(),
1397 SearchDirs[Idx].isSystemHeaderDirectory(),
1398 SearchDirs[Idx].isFramework());
1402 void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
1403 assert(HSOpts->ImplicitModuleMaps &&
1404 "Should not be loading subdirectory module maps");
1406 if (SearchDir.haveSearchedAllModuleMaps())
1410 SmallString<128> DirNative;
1411 llvm::sys::path::native(SearchDir.getDir()->getName(), DirNative);
1412 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
1413 Dir != DirEnd && !EC; Dir.increment(EC)) {
1414 bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework";
1415 if (IsFramework == SearchDir.isFramework())
1416 loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(),
1417 SearchDir.isFramework());
1420 SearchDir.setSearchedAllModuleMaps(true);