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());
158 SmallString<128> HashStr;
159 llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36);
160 llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm");
162 return Result.str().str();
165 Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) {
166 // Look in the module map to determine if there is a module by this name.
167 Module *Module = ModMap.findModule(ModuleName);
168 if (Module || !AllowSearch || !HSOpts->ImplicitModuleMaps)
171 // Look through the various header search paths to load any available module
172 // maps, searching for a module map that describes this module.
173 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
174 if (SearchDirs[Idx].isFramework()) {
175 // Search for or infer a module map for a framework.
176 SmallString<128> FrameworkDirName;
177 FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName();
178 llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework");
179 if (const DirectoryEntry *FrameworkDir
180 = FileMgr.getDirectory(FrameworkDirName)) {
182 = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User;
183 Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem);
189 // FIXME: Figure out how header maps and module maps will work together.
191 // Only deal with normal search directories.
192 if (!SearchDirs[Idx].isNormalDir())
195 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
196 // Search for a module map file in this directory.
197 if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
198 /*IsFramework*/false) == LMM_NewlyLoaded) {
199 // We just loaded a module map file; check whether the module is
201 Module = ModMap.findModule(ModuleName);
206 // Search for a module map in a subdirectory with the same name as the
208 SmallString<128> NestedModuleMapDirName;
209 NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName();
210 llvm::sys::path::append(NestedModuleMapDirName, ModuleName);
211 if (loadModuleMapFile(NestedModuleMapDirName, IsSystem,
212 /*IsFramework*/false) == LMM_NewlyLoaded){
213 // If we just loaded a module map file, look for the module again.
214 Module = ModMap.findModule(ModuleName);
219 // If we've already performed the exhaustive search for module maps in this
220 // search directory, don't do it again.
221 if (SearchDirs[Idx].haveSearchedAllModuleMaps())
224 // Load all module maps in the immediate subdirectories of this search
226 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
228 // Look again for the module.
229 Module = ModMap.findModule(ModuleName);
237 //===----------------------------------------------------------------------===//
238 // File lookup within a DirectoryLookup scope
239 //===----------------------------------------------------------------------===//
241 /// getName - Return the directory or filename corresponding to this lookup
243 const char *DirectoryLookup::getName() const {
245 return getDir()->getName();
247 return getFrameworkDir()->getName();
248 assert(isHeaderMap() && "Unknown DirectoryLookup");
249 return getHeaderMap()->getFileName();
252 const FileEntry *HeaderSearch::getFileAndSuggestModule(
253 StringRef FileName, const DirectoryEntry *Dir, bool IsSystemHeaderDir,
254 Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule) {
255 // If we have a module map that might map this header, load it and
256 // check whether we'll have a suggestion for a module.
257 const FileEntry *File = getFileMgr().getFile(FileName, /*OpenFile=*/true);
261 // If there is a module that corresponds to this header, suggest it.
262 if (!findUsableModuleForHeader(File, Dir ? Dir : File->getDir(),
263 RequestingModule, SuggestedModule,
270 /// LookupFile - Lookup the specified file in this search path, returning it
271 /// if it exists or returning null if not.
272 const FileEntry *DirectoryLookup::LookupFile(
275 SmallVectorImpl<char> *SearchPath,
276 SmallVectorImpl<char> *RelativePath,
277 Module *RequestingModule,
278 ModuleMap::KnownHeader *SuggestedModule,
279 bool &InUserSpecifiedSystemFramework,
281 SmallVectorImpl<char> &MappedName) const {
282 InUserSpecifiedSystemFramework = false;
283 HasBeenMapped = false;
285 SmallString<1024> TmpDir;
287 // Concatenate the requested file onto the directory.
288 TmpDir = getDir()->getName();
289 llvm::sys::path::append(TmpDir, Filename);
291 StringRef SearchPathRef(getDir()->getName());
293 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
296 RelativePath->clear();
297 RelativePath->append(Filename.begin(), Filename.end());
300 return HS.getFileAndSuggestModule(TmpDir, getDir(),
301 isSystemHeaderDirectory(),
302 RequestingModule, SuggestedModule);
306 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
307 RequestingModule, SuggestedModule,
308 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(
400 StringRef Filename, HeaderSearch &HS, SmallVectorImpl<char> *SearchPath,
401 SmallVectorImpl<char> *RelativePath, Module *RequestingModule,
402 ModuleMap::KnownHeader *SuggestedModule,
403 bool &InUserSpecifiedSystemFramework) const {
404 FileManager &FileMgr = HS.getFileMgr();
406 // Framework names must have a '/' in the filename.
407 size_t SlashPos = Filename.find('/');
408 if (SlashPos == StringRef::npos) return nullptr;
410 // Find out if this is the home for the specified framework, by checking
411 // HeaderSearch. Possible answers are yes/no and unknown.
412 HeaderSearch::FrameworkCacheEntry &CacheEntry =
413 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
415 // If it is known and in some other directory, fail.
416 if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir())
419 // Otherwise, construct the path to this framework dir.
421 // FrameworkName = "/System/Library/Frameworks/"
422 SmallString<1024> FrameworkName;
423 FrameworkName += getFrameworkDir()->getName();
424 if (FrameworkName.empty() || FrameworkName.back() != '/')
425 FrameworkName.push_back('/');
427 // FrameworkName = "/System/Library/Frameworks/Cocoa"
428 StringRef ModuleName(Filename.begin(), SlashPos);
429 FrameworkName += ModuleName;
431 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
432 FrameworkName += ".framework/";
434 // If the cache entry was unresolved, populate it now.
435 if (!CacheEntry.Directory) {
436 HS.IncrementFrameworkLookupCount();
438 // If the framework dir doesn't exist, we fail.
439 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
440 if (!Dir) return nullptr;
442 // Otherwise, if it does, remember that this is the right direntry for this
444 CacheEntry.Directory = getFrameworkDir();
446 // If this is a user search directory, check if the framework has been
447 // user-specified as a system framework.
448 if (getDirCharacteristic() == SrcMgr::C_User) {
449 SmallString<1024> SystemFrameworkMarker(FrameworkName);
450 SystemFrameworkMarker += ".system_framework";
451 if (llvm::sys::fs::exists(SystemFrameworkMarker)) {
452 CacheEntry.IsUserSpecifiedSystemFramework = true;
457 // Set the 'user-specified system framework' flag.
458 InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework;
461 RelativePath->clear();
462 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
465 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
466 unsigned OrigSize = FrameworkName.size();
468 FrameworkName += "Headers/";
472 // Without trailing '/'.
473 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
476 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
477 const FileEntry *FE = FileMgr.getFile(FrameworkName,
478 /*openFile=*/!SuggestedModule);
480 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
481 const char *Private = "Private";
482 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
483 Private+strlen(Private));
485 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
486 Private+strlen(Private));
488 FE = FileMgr.getFile(FrameworkName, /*openFile=*/!SuggestedModule);
491 // If we found the header and are allowed to suggest a module, do so now.
492 if (FE && SuggestedModule) {
493 // Find the framework in which this header occurs.
494 StringRef FrameworkPath = FE->getDir()->getName();
495 bool FoundFramework = false;
497 // Determine whether this directory exists.
498 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath);
502 // If this is a framework directory, then we're a subframework of this
504 if (llvm::sys::path::extension(FrameworkPath) == ".framework") {
505 FoundFramework = true;
509 // Get the parent directory name.
510 FrameworkPath = llvm::sys::path::parent_path(FrameworkPath);
511 if (FrameworkPath.empty())
515 bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
516 if (FoundFramework) {
517 if (!HS.findUsableModuleForFrameworkHeader(
518 FE, FrameworkPath, RequestingModule, SuggestedModule, IsSystem))
521 if (!HS.findUsableModuleForHeader(FE, getDir(), RequestingModule,
522 SuggestedModule, IsSystem))
529 void HeaderSearch::setTarget(const TargetInfo &Target) {
530 ModMap.setTarget(Target);
534 //===----------------------------------------------------------------------===//
535 // Header File Location.
536 //===----------------------------------------------------------------------===//
538 /// \brief Return true with a diagnostic if the file that MSVC would have found
539 /// fails to match the one that Clang would have found with MSVC header search
541 static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags,
542 const FileEntry *MSFE, const FileEntry *FE,
543 SourceLocation IncludeLoc) {
544 if (MSFE && FE != MSFE) {
545 Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName();
551 static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) {
552 assert(!Str.empty());
553 char *CopyStr = Alloc.Allocate<char>(Str.size()+1);
554 std::copy(Str.begin(), Str.end(), CopyStr);
555 CopyStr[Str.size()] = '\0';
559 /// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file,
560 /// return null on failure. isAngled indicates whether the file reference is
561 /// for system \#include's or not (i.e. using <> instead of ""). Includers, if
562 /// non-empty, indicates where the \#including file(s) are, in case a relative
563 /// search is needed. Microsoft mode will pass all \#including files.
564 const FileEntry *HeaderSearch::LookupFile(
565 StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
566 const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir,
567 ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
568 SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
569 Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
572 *SuggestedModule = ModuleMap::KnownHeader();
574 // If 'Filename' is absolute, check to see if it exists and no searching.
575 if (llvm::sys::path::is_absolute(Filename)) {
578 // If this was an #include_next "/absolute/file", fail.
579 if (FromDir) return nullptr;
584 RelativePath->clear();
585 RelativePath->append(Filename.begin(), Filename.end());
587 // Otherwise, just return the file.
588 return getFileAndSuggestModule(Filename, nullptr,
589 /*IsSystemHeaderDir*/false,
590 RequestingModule, SuggestedModule);
593 // This is the header that MSVC's header search would have found.
594 const FileEntry *MSFE = nullptr;
595 ModuleMap::KnownHeader MSSuggestedModule;
597 // Unless disabled, check to see if the file is in the #includer's
598 // directory. This cannot be based on CurDir, because each includer could be
599 // a #include of a subdirectory (#include "foo/bar.h") and a subsequent
600 // include of "baz.h" should resolve to "whatever/foo/baz.h".
601 // This search is not done for <> headers.
602 if (!Includers.empty() && !isAngled && !NoCurDirSearch) {
603 SmallString<1024> TmpDir;
605 for (const auto &IncluderAndDir : Includers) {
606 const FileEntry *Includer = IncluderAndDir.first;
608 // Concatenate the requested file onto the directory.
609 // FIXME: Portability. Filename concatenation should be in sys::Path.
610 TmpDir = IncluderAndDir.second->getName();
611 TmpDir.push_back('/');
612 TmpDir.append(Filename.begin(), Filename.end());
614 // FIXME: We don't cache the result of getFileInfo across the call to
615 // getFileAndSuggestModule, because it's a reference to an element of
616 // a container that could be reallocated across this call.
618 // FIXME: If we have no includer, that means we're processing a #include
619 // from a module build. We should treat this as a system header if we're
620 // building a [system] module.
621 bool IncluderIsSystemHeader =
622 Includer && getFileInfo(Includer).DirInfo != SrcMgr::C_User;
623 if (const FileEntry *FE = getFileAndSuggestModule(
624 TmpDir, IncluderAndDir.second, IncluderIsSystemHeader,
625 RequestingModule, SuggestedModule)) {
627 assert(First && "only first includer can have no file");
631 // Leave CurDir unset.
632 // This file is a system header or C++ unfriendly if the old file is.
634 // Note that we only use one of FromHFI/ToHFI at once, due to potential
635 // reallocation of the underlying vector potentially making the first
636 // reference binding dangling.
637 HeaderFileInfo &FromHFI = getFileInfo(Includer);
638 unsigned DirInfo = FromHFI.DirInfo;
639 bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader;
640 StringRef Framework = FromHFI.Framework;
642 HeaderFileInfo &ToHFI = getFileInfo(FE);
643 ToHFI.DirInfo = DirInfo;
644 ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader;
645 ToHFI.Framework = Framework;
648 StringRef SearchPathRef(IncluderAndDir.second->getName());
650 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
653 RelativePath->clear();
654 RelativePath->append(Filename.begin(), Filename.end());
659 // Otherwise, we found the path via MSVC header search rules. If
660 // -Wmsvc-include is enabled, we have to keep searching to see if we
661 // would've found this header in -I or -isystem directories.
662 if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) {
666 if (SuggestedModule) {
667 MSSuggestedModule = *SuggestedModule;
668 *SuggestedModule = ModuleMap::KnownHeader();
679 // If this is a system #include, ignore the user #include locs.
680 unsigned i = isAngled ? AngledDirIdx : 0;
682 // If this is a #include_next request, start searching after the directory the
683 // file was found in.
685 i = FromDir-&SearchDirs[0];
687 // Cache all of the lookups performed by this method. Many headers are
688 // multiply included, and the "pragma once" optimization prevents them from
689 // being relex/pp'd, but they would still have to search through a
690 // (potentially huge) series of SearchDirs to find it.
691 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
693 // If the entry has been previously looked up, the first value will be
694 // non-zero. If the value is equal to i (the start point of our search), then
695 // this is a matching hit.
696 if (!SkipCache && CacheLookup.StartIdx == i+1) {
697 // Skip querying potentially lots of directories for this lookup.
698 i = CacheLookup.HitIdx;
699 if (CacheLookup.MappedName)
700 Filename = CacheLookup.MappedName;
702 // Otherwise, this is the first query, or the previous query didn't match
703 // our search start. We will fill in our found location below, so prime the
704 // start point value.
705 CacheLookup.reset(/*StartIdx=*/i+1);
708 SmallString<64> MappedName;
710 // Check each directory in sequence to see if it contains this file.
711 for (; i != SearchDirs.size(); ++i) {
712 bool InUserSpecifiedSystemFramework = false;
713 bool HasBeenMapped = false;
714 const FileEntry *FE = SearchDirs[i].LookupFile(
715 Filename, *this, SearchPath, RelativePath, RequestingModule,
716 SuggestedModule, InUserSpecifiedSystemFramework, HasBeenMapped,
719 CacheLookup.MappedName =
720 copyString(Filename, LookupFileCache.getAllocator());
724 CurDir = &SearchDirs[i];
726 // This file is a system header or C++ unfriendly if the dir is.
727 HeaderFileInfo &HFI = getFileInfo(FE);
728 HFI.DirInfo = CurDir->getDirCharacteristic();
730 // If the directory characteristic is User but this framework was
731 // user-specified to be treated as a system framework, promote the
733 if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework)
734 HFI.DirInfo = SrcMgr::C_System;
736 // If the filename matches a known system header prefix, override
737 // whether the file is a system header.
738 for (unsigned j = SystemHeaderPrefixes.size(); j; --j) {
739 if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) {
740 HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System
746 // If this file is found in a header map and uses the framework style of
747 // includes, then this header is part of a framework we're building.
748 if (CurDir->isIndexHeaderMap()) {
749 size_t SlashPos = Filename.find('/');
750 if (SlashPos != StringRef::npos) {
751 HFI.IndexHeaderMapHeader = 1;
752 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
757 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
759 *SuggestedModule = MSSuggestedModule;
763 // Remember this location for the next lookup we do.
764 CacheLookup.HitIdx = i;
768 // If we are including a file with a quoted include "foo.h" from inside
769 // a header in a framework that is currently being built, and we couldn't
770 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
771 // "Foo" is the name of the framework in which the including header was found.
772 if (!Includers.empty() && Includers.front().first && !isAngled &&
773 Filename.find('/') == StringRef::npos) {
774 HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first);
775 if (IncludingHFI.IndexHeaderMapHeader) {
776 SmallString<128> ScratchFilename;
777 ScratchFilename += IncludingHFI.Framework;
778 ScratchFilename += '/';
779 ScratchFilename += Filename;
781 const FileEntry *FE =
782 LookupFile(ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir,
783 CurDir, Includers.front(), SearchPath, RelativePath,
784 RequestingModule, SuggestedModule);
786 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) {
788 *SuggestedModule = MSSuggestedModule;
792 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename];
793 CacheLookup.HitIdx = LookupFileCache[ScratchFilename].HitIdx;
794 // FIXME: SuggestedModule.
799 if (checkMSVCHeaderSearch(Diags, MSFE, nullptr, IncludeLoc)) {
801 *SuggestedModule = MSSuggestedModule;
805 // Otherwise, didn't find it. Remember we didn't find this.
806 CacheLookup.HitIdx = SearchDirs.size();
810 /// LookupSubframeworkHeader - Look up a subframework for the specified
811 /// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from
812 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
813 /// is a subframework within Carbon.framework. If so, return the FileEntry
814 /// for the designated file, otherwise return null.
815 const FileEntry *HeaderSearch::
816 LookupSubframeworkHeader(StringRef Filename,
817 const FileEntry *ContextFileEnt,
818 SmallVectorImpl<char> *SearchPath,
819 SmallVectorImpl<char> *RelativePath,
820 Module *RequestingModule,
821 ModuleMap::KnownHeader *SuggestedModule) {
822 assert(ContextFileEnt && "No context file?");
824 // Framework names must have a '/' in the filename. Find it.
825 // FIXME: Should we permit '\' on Windows?
826 size_t SlashPos = Filename.find('/');
827 if (SlashPos == StringRef::npos) return nullptr;
829 // Look up the base framework name of the ContextFileEnt.
830 const char *ContextName = ContextFileEnt->getName();
832 // If the context info wasn't a framework, couldn't be a subframework.
833 const unsigned DotFrameworkLen = 10;
834 const char *FrameworkPos = strstr(ContextName, ".framework");
835 if (FrameworkPos == nullptr ||
836 (FrameworkPos[DotFrameworkLen] != '/' &&
837 FrameworkPos[DotFrameworkLen] != '\\'))
840 SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1);
842 // Append Frameworks/HIToolbox.framework/
843 FrameworkName += "Frameworks/";
844 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
845 FrameworkName += ".framework/";
848 *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos),
849 FrameworkCacheEntry())).first;
851 // Some other location?
852 if (CacheLookup.second.Directory &&
853 CacheLookup.first().size() == FrameworkName.size() &&
854 memcmp(CacheLookup.first().data(), &FrameworkName[0],
855 CacheLookup.first().size()) != 0)
858 // Cache subframework.
859 if (!CacheLookup.second.Directory) {
860 ++NumSubFrameworkLookups;
862 // If the framework dir doesn't exist, we fail.
863 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName);
864 if (!Dir) return nullptr;
866 // Otherwise, if it does, remember that this is the right direntry for this
868 CacheLookup.second.Directory = Dir;
871 const FileEntry *FE = nullptr;
874 RelativePath->clear();
875 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
878 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
879 SmallString<1024> HeadersFilename(FrameworkName);
880 HeadersFilename += "Headers/";
883 // Without trailing '/'.
884 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
887 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
888 if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true))) {
890 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
891 HeadersFilename = FrameworkName;
892 HeadersFilename += "PrivateHeaders/";
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)))
904 // This file is a system header or C++ unfriendly if the old file is.
906 // Note that the temporary 'DirInfo' is required here, as either call to
907 // getFileInfo could resize the vector and we don't want to rely on order
909 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
910 getFileInfo(FE).DirInfo = DirInfo;
912 FrameworkName.pop_back(); // remove the trailing '/'
913 if (!findUsableModuleForFrameworkHeader(FE, FrameworkName, RequestingModule,
914 SuggestedModule, /*IsSystem*/ false))
920 //===----------------------------------------------------------------------===//
921 // File Info Management.
922 //===----------------------------------------------------------------------===//
924 /// \brief Merge the header file info provided by \p OtherHFI into the current
925 /// header file info (\p HFI)
926 static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
927 const HeaderFileInfo &OtherHFI) {
928 assert(OtherHFI.External && "expected to merge external HFI");
930 HFI.isImport |= OtherHFI.isImport;
931 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
932 HFI.isModuleHeader |= OtherHFI.isModuleHeader;
933 HFI.NumIncludes += OtherHFI.NumIncludes;
935 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
936 HFI.ControllingMacro = OtherHFI.ControllingMacro;
937 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
940 HFI.DirInfo = OtherHFI.DirInfo;
941 HFI.External = (!HFI.IsValid || HFI.External);
943 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
945 if (HFI.Framework.empty())
946 HFI.Framework = OtherHFI.Framework;
949 /// getFileInfo - Return the HeaderFileInfo structure for the specified
951 HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
952 if (FE->getUID() >= FileInfo.size())
953 FileInfo.resize(FE->getUID() + 1);
955 HeaderFileInfo *HFI = &FileInfo[FE->getUID()];
956 // FIXME: Use a generation count to check whether this is really up to date.
957 if (ExternalSource && !HFI->Resolved) {
958 HFI->Resolved = true;
959 auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
961 HFI = &FileInfo[FE->getUID()];
962 if (ExternalHFI.External)
963 mergeHeaderFileInfo(*HFI, ExternalHFI);
967 // We have local information about this header file, so it's no longer
968 // strictly external.
969 HFI->External = false;
973 const HeaderFileInfo *
974 HeaderSearch::getExistingFileInfo(const FileEntry *FE,
975 bool WantExternal) const {
976 // If we have an external source, ensure we have the latest information.
977 // FIXME: Use a generation count to check whether this is really up to date.
979 if (ExternalSource) {
980 if (FE->getUID() >= FileInfo.size()) {
983 FileInfo.resize(FE->getUID() + 1);
986 HFI = &FileInfo[FE->getUID()];
987 if (!WantExternal && (!HFI->IsValid || HFI->External))
989 if (!HFI->Resolved) {
990 HFI->Resolved = true;
991 auto ExternalHFI = ExternalSource->GetHeaderFileInfo(FE);
993 HFI = &FileInfo[FE->getUID()];
994 if (ExternalHFI.External)
995 mergeHeaderFileInfo(*HFI, ExternalHFI);
997 } else if (FE->getUID() >= FileInfo.size()) {
1000 HFI = &FileInfo[FE->getUID()];
1003 if (!HFI->IsValid || (HFI->External && !WantExternal))
1009 bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
1010 // Check if we've ever seen this file as a header.
1011 if (auto *HFI = getExistingFileInfo(File))
1012 return HFI->isPragmaOnce || HFI->isImport || HFI->ControllingMacro ||
1013 HFI->ControllingMacroID;
1017 void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE,
1018 ModuleMap::ModuleHeaderRole Role,
1019 bool isCompilingModuleHeader) {
1020 bool isModularHeader = !(Role & ModuleMap::TextualHeader);
1022 // Don't mark the file info as non-external if there's nothing to change.
1023 if (!isCompilingModuleHeader) {
1024 if (!isModularHeader)
1026 auto *HFI = getExistingFileInfo(FE);
1027 if (HFI && HFI->isModuleHeader)
1031 auto &HFI = getFileInfo(FE);
1032 HFI.isModuleHeader |= isModularHeader;
1033 HFI.isCompilingModuleHeader |= isCompilingModuleHeader;
1036 bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP,
1037 const FileEntry *File,
1038 bool isImport, Module *M) {
1039 ++NumIncluded; // Count # of attempted #includes.
1041 // Get information about this file.
1042 HeaderFileInfo &FileInfo = getFileInfo(File);
1044 // If this is a #import directive, check that we have not already imported
1047 // If this has already been imported, don't import it again.
1048 FileInfo.isImport = true;
1050 // Has this already been #import'ed or #include'd?
1051 if (FileInfo.NumIncludes) return false;
1053 // Otherwise, if this is a #include of a file that was previously #import'd
1054 // or if this is the second #include of a #pragma once file, ignore it.
1055 if (FileInfo.isImport)
1059 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
1060 // if the macro that guards it is defined, we know the #include has no effect.
1061 if (const IdentifierInfo *ControllingMacro
1062 = FileInfo.getControllingMacro(ExternalLookup)) {
1063 // If the header corresponds to a module, check whether the macro is already
1064 // defined in that module rather than checking in the current set of visible
1066 if (M ? PP.isMacroDefinedInLocalModule(ControllingMacro, M)
1067 : PP.isMacroDefined(ControllingMacro)) {
1068 ++NumMultiIncludeFileOptzn;
1073 // Increment the number of times this file has been included.
1074 ++FileInfo.NumIncludes;
1079 size_t HeaderSearch::getTotalMemory() const {
1080 return SearchDirs.capacity()
1081 + llvm::capacity_in_bytes(FileInfo)
1082 + llvm::capacity_in_bytes(HeaderMaps)
1083 + LookupFileCache.getAllocator().getTotalMemory()
1084 + FrameworkMap.getAllocator().getTotalMemory();
1087 StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
1088 return FrameworkNames.insert(Framework).first->first();
1091 bool HeaderSearch::hasModuleMap(StringRef FileName,
1092 const DirectoryEntry *Root,
1094 if (!HSOpts->ImplicitModuleMaps)
1097 SmallVector<const DirectoryEntry *, 2> FixUpDirectories;
1099 StringRef DirName = FileName;
1101 // Get the parent directory name.
1102 DirName = llvm::sys::path::parent_path(DirName);
1103 if (DirName.empty())
1106 // Determine whether this directory exists.
1107 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName);
1111 // Try to load the module map file in this directory.
1112 switch (loadModuleMapFile(Dir, IsSystem,
1113 llvm::sys::path::extension(Dir->getName()) ==
1115 case LMM_NewlyLoaded:
1116 case LMM_AlreadyLoaded:
1117 // Success. All of the directories we stepped through inherit this module
1119 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I)
1120 DirectoryHasModuleMap[FixUpDirectories[I]] = true;
1123 case LMM_NoDirectory:
1124 case LMM_InvalidModuleMap:
1128 // If we hit the top of our search, we're done.
1132 // Keep track of all of the directories we checked, so we can mark them as
1133 // having module maps if we eventually do find a module map.
1134 FixUpDirectories.push_back(Dir);
1138 ModuleMap::KnownHeader
1139 HeaderSearch::findModuleForHeader(const FileEntry *File) const {
1140 if (ExternalSource) {
1141 // Make sure the external source has handled header info about this file,
1142 // which includes whether the file is part of a module.
1143 (void)getExistingFileInfo(File);
1145 return ModMap.findModuleForHeader(File);
1148 bool HeaderSearch::findUsableModuleForHeader(
1149 const FileEntry *File, const DirectoryEntry *Root, Module *RequestingModule,
1150 ModuleMap::KnownHeader *SuggestedModule, bool IsSystemHeaderDir) {
1151 if (File && SuggestedModule) {
1152 // If there is a module that corresponds to this header, suggest it.
1153 hasModuleMap(File->getName(), Root, IsSystemHeaderDir);
1154 *SuggestedModule = findModuleForHeader(File);
1159 bool HeaderSearch::findUsableModuleForFrameworkHeader(
1160 const FileEntry *File, StringRef FrameworkName, Module *RequestingModule,
1161 ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework) {
1162 // If we're supposed to suggest a module, look for one now.
1163 if (SuggestedModule) {
1164 // Find the top-level framework based on this framework.
1165 SmallVector<std::string, 4> SubmodulePath;
1166 const DirectoryEntry *TopFrameworkDir
1167 = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
1169 // Determine the name of the top-level framework.
1170 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
1172 // Load this framework module. If that succeeds, find the suggested module
1173 // for this header, if any.
1174 loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystemFramework);
1176 // FIXME: This can find a module not part of ModuleName, which is
1177 // important so that we're consistent about whether this header
1178 // corresponds to a module. Possibly we should lock down framework modules
1179 // so that this is not possible.
1180 *SuggestedModule = findModuleForHeader(File);
1185 static const FileEntry *getPrivateModuleMap(const FileEntry *File,
1186 FileManager &FileMgr) {
1187 StringRef Filename = llvm::sys::path::filename(File->getName());
1188 SmallString<128> PrivateFilename(File->getDir()->getName());
1189 if (Filename == "module.map")
1190 llvm::sys::path::append(PrivateFilename, "module_private.map");
1191 else if (Filename == "module.modulemap")
1192 llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
1195 return FileMgr.getFile(PrivateFilename);
1198 bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem) {
1199 // Find the directory for the module. For frameworks, that may require going
1200 // up from the 'Modules' directory.
1201 const DirectoryEntry *Dir = nullptr;
1202 if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd)
1203 Dir = FileMgr.getDirectory(".");
1205 Dir = File->getDir();
1206 StringRef DirName(Dir->getName());
1207 if (llvm::sys::path::filename(DirName) == "Modules") {
1208 DirName = llvm::sys::path::parent_path(DirName);
1209 if (DirName.endswith(".framework"))
1210 Dir = FileMgr.getDirectory(DirName);
1211 // FIXME: This assert can fail if there's a race between the above check
1212 // and the removal of the directory.
1213 assert(Dir && "parent must exist");
1217 switch (loadModuleMapFileImpl(File, IsSystem, Dir)) {
1218 case LMM_AlreadyLoaded:
1219 case LMM_NewlyLoaded:
1221 case LMM_NoDirectory:
1222 case LMM_InvalidModuleMap:
1225 llvm_unreachable("Unknown load module map result");
1228 HeaderSearch::LoadModuleMapResult
1229 HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
1230 const DirectoryEntry *Dir) {
1231 assert(File && "expected FileEntry");
1233 // Check whether we've already loaded this module map, and mark it as being
1234 // loaded in case we recursively try to load it from itself.
1235 auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
1236 if (!AddResult.second)
1237 return AddResult.first->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
1239 if (ModMap.parseModuleMapFile(File, IsSystem, Dir)) {
1240 LoadedModuleMaps[File] = false;
1241 return LMM_InvalidModuleMap;
1244 // Try to load a corresponding private module map.
1245 if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) {
1246 if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
1247 LoadedModuleMaps[File] = false;
1248 return LMM_InvalidModuleMap;
1252 // This directory has a module map.
1253 return LMM_NewlyLoaded;
1257 HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
1258 if (!HSOpts->ImplicitModuleMaps)
1260 // For frameworks, the preferred spelling is Modules/module.modulemap, but
1261 // module.map at the framework root is also accepted.
1262 SmallString<128> ModuleMapFileName(Dir->getName());
1264 llvm::sys::path::append(ModuleMapFileName, "Modules");
1265 llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
1266 if (const FileEntry *F = FileMgr.getFile(ModuleMapFileName))
1269 // Continue to allow module.map
1270 ModuleMapFileName = Dir->getName();
1271 llvm::sys::path::append(ModuleMapFileName, "module.map");
1272 return FileMgr.getFile(ModuleMapFileName);
1275 Module *HeaderSearch::loadFrameworkModule(StringRef Name,
1276 const DirectoryEntry *Dir,
1278 if (Module *Module = ModMap.findModule(Name))
1281 // Try to load a module map file.
1282 switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) {
1283 case LMM_InvalidModuleMap:
1284 // Try to infer a module map from the framework directory.
1285 if (HSOpts->ImplicitModuleMaps)
1286 ModMap.inferFrameworkModule(Dir, IsSystem, /*Parent=*/nullptr);
1289 case LMM_AlreadyLoaded:
1290 case LMM_NoDirectory:
1293 case LMM_NewlyLoaded:
1297 return ModMap.findModule(Name);
1301 HeaderSearch::LoadModuleMapResult
1302 HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
1304 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName))
1305 return loadModuleMapFile(Dir, IsSystem, IsFramework);
1307 return LMM_NoDirectory;
1310 HeaderSearch::LoadModuleMapResult
1311 HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem,
1313 auto KnownDir = DirectoryHasModuleMap.find(Dir);
1314 if (KnownDir != DirectoryHasModuleMap.end())
1315 return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
1317 if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
1318 LoadModuleMapResult Result =
1319 loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
1320 // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
1321 // E.g. Foo.framework/Modules/module.modulemap
1322 // ^Dir ^ModuleMapFile
1323 if (Result == LMM_NewlyLoaded)
1324 DirectoryHasModuleMap[Dir] = true;
1325 else if (Result == LMM_InvalidModuleMap)
1326 DirectoryHasModuleMap[Dir] = false;
1329 return LMM_InvalidModuleMap;
1332 void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
1335 if (HSOpts->ImplicitModuleMaps) {
1336 // Load module maps for each of the header search directories.
1337 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
1338 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory();
1339 if (SearchDirs[Idx].isFramework()) {
1341 SmallString<128> DirNative;
1342 llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(),
1345 // Search each of the ".framework" directories to load them as modules.
1346 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
1347 Dir != DirEnd && !EC; Dir.increment(EC)) {
1348 if (llvm::sys::path::extension(Dir->path()) != ".framework")
1351 const DirectoryEntry *FrameworkDir =
1352 FileMgr.getDirectory(Dir->path());
1356 // Load this framework module.
1357 loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir,
1363 // FIXME: Deal with header maps.
1364 if (SearchDirs[Idx].isHeaderMap())
1367 // Try to load a module map file for the search directory.
1368 loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem,
1369 /*IsFramework*/ false);
1371 // Try to load module map files for immediate subdirectories of this
1372 // search directory.
1373 loadSubdirectoryModuleMaps(SearchDirs[Idx]);
1377 // Populate the list of modules.
1378 for (ModuleMap::module_iterator M = ModMap.module_begin(),
1379 MEnd = ModMap.module_end();
1381 Modules.push_back(M->getValue());
1385 void HeaderSearch::loadTopLevelSystemModules() {
1386 if (!HSOpts->ImplicitModuleMaps)
1389 // Load module maps for each of the header search directories.
1390 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
1391 // We only care about normal header directories.
1392 if (!SearchDirs[Idx].isNormalDir()) {
1396 // Try to load a module map file for the search directory.
1397 loadModuleMapFile(SearchDirs[Idx].getDir(),
1398 SearchDirs[Idx].isSystemHeaderDirectory(),
1399 SearchDirs[Idx].isFramework());
1403 void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) {
1404 assert(HSOpts->ImplicitModuleMaps &&
1405 "Should not be loading subdirectory module maps");
1407 if (SearchDir.haveSearchedAllModuleMaps())
1411 SmallString<128> DirNative;
1412 llvm::sys::path::native(SearchDir.getDir()->getName(), DirNative);
1413 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
1414 Dir != DirEnd && !EC; Dir.increment(EC)) {
1415 bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework";
1416 if (IsFramework == SearchDir.isFramework())
1417 loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(),
1418 SearchDir.isFramework());
1421 SearchDir.setSearchedAllModuleMaps(true);