1 //===--- HeaderSearch.cpp - Resolve Header File Locations ---===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the DirectoryLookup and HeaderSearch interfaces. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Lex/HeaderSearch.h" 15 #include "clang/Basic/FileManager.h" 16 #include "clang/Basic/IdentifierTable.h" 17 #include "clang/Lex/HeaderMap.h" 18 #include "clang/Lex/HeaderSearchOptions.h" 19 #include "clang/Lex/LexDiagnostic.h" 20 #include "clang/Lex/Lexer.h" 21 #include "clang/Lex/Preprocessor.h" 22 #include "llvm/ADT/APInt.h" 23 #include "llvm/ADT/Hashing.h" 24 #include "llvm/ADT/SmallString.h" 25 #include "llvm/Support/Capacity.h" 26 #include "llvm/Support/FileSystem.h" 27 #include "llvm/Support/Path.h" 28 #include "llvm/Support/raw_ostream.h" 29 #include <cstdio> 30 #if defined(LLVM_ON_UNIX) 31 #include <limits.h> 32 #endif 33 using namespace clang; 34 35 const IdentifierInfo * 36 HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) { 37 if (ControllingMacro) 38 return ControllingMacro; 39 40 if (!ControllingMacroID || !External) 41 return nullptr; 42 43 ControllingMacro = External->GetIdentifier(ControllingMacroID); 44 return ControllingMacro; 45 } 46 47 ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {} 48 49 HeaderSearch::HeaderSearch(IntrusiveRefCntPtr<HeaderSearchOptions> HSOpts, 50 SourceManager &SourceMgr, DiagnosticsEngine &Diags, 51 const LangOptions &LangOpts, 52 const TargetInfo *Target) 53 : HSOpts(HSOpts), Diags(Diags), FileMgr(SourceMgr.getFileManager()), 54 FrameworkMap(64), ModMap(SourceMgr, Diags, LangOpts, Target, *this), 55 LangOpts(LangOpts) { 56 AngledDirIdx = 0; 57 SystemDirIdx = 0; 58 NoCurDirSearch = false; 59 60 ExternalLookup = nullptr; 61 ExternalSource = nullptr; 62 NumIncluded = 0; 63 NumMultiIncludeFileOptzn = 0; 64 NumFrameworkLookups = NumSubFrameworkLookups = 0; 65 } 66 67 HeaderSearch::~HeaderSearch() { 68 // Delete headermaps. 69 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) 70 delete HeaderMaps[i].second; 71 } 72 73 void HeaderSearch::PrintStats() { 74 fprintf(stderr, "\n*** HeaderSearch Stats:\n"); 75 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size()); 76 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0; 77 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) { 78 NumOnceOnlyFiles += FileInfo[i].isImport; 79 if (MaxNumIncludes < FileInfo[i].NumIncludes) 80 MaxNumIncludes = FileInfo[i].NumIncludes; 81 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1; 82 } 83 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles); 84 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles); 85 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes); 86 87 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded); 88 fprintf(stderr, " %d #includes skipped due to" 89 " the multi-include optimization.\n", NumMultiIncludeFileOptzn); 90 91 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups); 92 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups); 93 } 94 95 /// CreateHeaderMap - This method returns a HeaderMap for the specified 96 /// FileEntry, uniquing them through the 'HeaderMaps' datastructure. 97 const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) { 98 // We expect the number of headermaps to be small, and almost always empty. 99 // If it ever grows, use of a linear search should be re-evaluated. 100 if (!HeaderMaps.empty()) { 101 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) 102 // Pointer equality comparison of FileEntries works because they are 103 // already uniqued by inode. 104 if (HeaderMaps[i].first == FE) 105 return HeaderMaps[i].second; 106 } 107 108 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) { 109 HeaderMaps.push_back(std::make_pair(FE, HM)); 110 return HM; 111 } 112 113 return nullptr; 114 } 115 116 std::string HeaderSearch::getModuleFileName(Module *Module) { 117 const FileEntry *ModuleMap = 118 getModuleMap().getModuleMapFileForUniquing(Module); 119 return getModuleFileName(Module->Name, ModuleMap->getName()); 120 } 121 122 std::string HeaderSearch::getModuleFileName(StringRef ModuleName, 123 StringRef ModuleMapPath) { 124 // If we don't have a module cache path, we can't do anything. 125 if (ModuleCachePath.empty()) 126 return std::string(); 127 128 SmallString<256> Result(ModuleCachePath); 129 llvm::sys::fs::make_absolute(Result); 130 131 if (HSOpts->DisableModuleHash) { 132 llvm::sys::path::append(Result, ModuleName + ".pcm"); 133 } else { 134 // Construct the name <ModuleName>-<hash of ModuleMapPath>.pcm which should 135 // ideally be globally unique to this particular module. Name collisions 136 // in the hash are safe (because any translation unit can only import one 137 // module with each name), but result in a loss of caching. 138 // 139 // To avoid false-negatives, we form as canonical a path as we can, and map 140 // to lower-case in case we're on a case-insensitive file system. 141 auto *Dir = 142 FileMgr.getDirectory(llvm::sys::path::parent_path(ModuleMapPath)); 143 if (!Dir) 144 return std::string(); 145 auto DirName = FileMgr.getCanonicalName(Dir); 146 auto FileName = llvm::sys::path::filename(ModuleMapPath); 147 148 llvm::hash_code Hash = 149 llvm::hash_combine(DirName.lower(), FileName.lower()); 150 151 SmallString<128> HashStr; 152 llvm::APInt(64, size_t(Hash)).toStringUnsigned(HashStr, /*Radix*/36); 153 llvm::sys::path::append(Result, ModuleName + "-" + HashStr + ".pcm"); 154 } 155 return Result.str().str(); 156 } 157 158 Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch) { 159 // Look in the module map to determine if there is a module by this name. 160 Module *Module = ModMap.findModule(ModuleName); 161 if (Module || !AllowSearch || !LangOpts.ModulesImplicitMaps) 162 return Module; 163 164 // Look through the various header search paths to load any available module 165 // maps, searching for a module map that describes this module. 166 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { 167 if (SearchDirs[Idx].isFramework()) { 168 // Search for or infer a module map for a framework. 169 SmallString<128> FrameworkDirName; 170 FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName(); 171 llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework"); 172 if (const DirectoryEntry *FrameworkDir 173 = FileMgr.getDirectory(FrameworkDirName)) { 174 bool IsSystem 175 = SearchDirs[Idx].getDirCharacteristic() != SrcMgr::C_User; 176 Module = loadFrameworkModule(ModuleName, FrameworkDir, IsSystem); 177 if (Module) 178 break; 179 } 180 } 181 182 // FIXME: Figure out how header maps and module maps will work together. 183 184 // Only deal with normal search directories. 185 if (!SearchDirs[Idx].isNormalDir()) 186 continue; 187 188 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory(); 189 // Search for a module map file in this directory. 190 if (loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem, 191 /*IsFramework*/false) == LMM_NewlyLoaded) { 192 // We just loaded a module map file; check whether the module is 193 // available now. 194 Module = ModMap.findModule(ModuleName); 195 if (Module) 196 break; 197 } 198 199 // Search for a module map in a subdirectory with the same name as the 200 // module. 201 SmallString<128> NestedModuleMapDirName; 202 NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName(); 203 llvm::sys::path::append(NestedModuleMapDirName, ModuleName); 204 if (loadModuleMapFile(NestedModuleMapDirName, IsSystem, 205 /*IsFramework*/false) == LMM_NewlyLoaded){ 206 // If we just loaded a module map file, look for the module again. 207 Module = ModMap.findModule(ModuleName); 208 if (Module) 209 break; 210 } 211 212 // If we've already performed the exhaustive search for module maps in this 213 // search directory, don't do it again. 214 if (SearchDirs[Idx].haveSearchedAllModuleMaps()) 215 continue; 216 217 // Load all module maps in the immediate subdirectories of this search 218 // directory. 219 loadSubdirectoryModuleMaps(SearchDirs[Idx]); 220 221 // Look again for the module. 222 Module = ModMap.findModule(ModuleName); 223 if (Module) 224 break; 225 } 226 227 return Module; 228 } 229 230 //===----------------------------------------------------------------------===// 231 // File lookup within a DirectoryLookup scope 232 //===----------------------------------------------------------------------===// 233 234 /// getName - Return the directory or filename corresponding to this lookup 235 /// object. 236 const char *DirectoryLookup::getName() const { 237 if (isNormalDir()) 238 return getDir()->getName(); 239 if (isFramework()) 240 return getFrameworkDir()->getName(); 241 assert(isHeaderMap() && "Unknown DirectoryLookup"); 242 return getHeaderMap()->getFileName(); 243 } 244 245 static const FileEntry * 246 getFileAndSuggestModule(HeaderSearch &HS, StringRef FileName, 247 const DirectoryEntry *Dir, bool IsSystemHeaderDir, 248 ModuleMap::KnownHeader *SuggestedModule) { 249 // If we have a module map that might map this header, load it and 250 // check whether we'll have a suggestion for a module. 251 HS.hasModuleMap(FileName, Dir, IsSystemHeaderDir); 252 if (SuggestedModule) { 253 const FileEntry *File = HS.getFileMgr().getFile(FileName, 254 /*OpenFile=*/false); 255 if (File) { 256 // If there is a module that corresponds to this header, suggest it. 257 *SuggestedModule = HS.findModuleForHeader(File); 258 259 // FIXME: This appears to be a no-op. We loaded the module map for this 260 // directory at the start of this function. 261 if (!SuggestedModule->getModule() && 262 HS.hasModuleMap(FileName, Dir, IsSystemHeaderDir)) 263 *SuggestedModule = HS.findModuleForHeader(File); 264 } 265 266 return File; 267 } 268 269 return HS.getFileMgr().getFile(FileName, /*openFile=*/true); 270 } 271 272 /// LookupFile - Lookup the specified file in this search path, returning it 273 /// if it exists or returning null if not. 274 const FileEntry *DirectoryLookup::LookupFile( 275 StringRef &Filename, 276 HeaderSearch &HS, 277 SmallVectorImpl<char> *SearchPath, 278 SmallVectorImpl<char> *RelativePath, 279 ModuleMap::KnownHeader *SuggestedModule, 280 bool &InUserSpecifiedSystemFramework, 281 bool &HasBeenMapped, 282 SmallVectorImpl<char> &MappedName) const { 283 InUserSpecifiedSystemFramework = false; 284 HasBeenMapped = false; 285 286 SmallString<1024> TmpDir; 287 if (isNormalDir()) { 288 // Concatenate the requested file onto the directory. 289 TmpDir = getDir()->getName(); 290 llvm::sys::path::append(TmpDir, Filename); 291 if (SearchPath) { 292 StringRef SearchPathRef(getDir()->getName()); 293 SearchPath->clear(); 294 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); 295 } 296 if (RelativePath) { 297 RelativePath->clear(); 298 RelativePath->append(Filename.begin(), Filename.end()); 299 } 300 301 return getFileAndSuggestModule(HS, TmpDir, getDir(), 302 isSystemHeaderDirectory(), 303 SuggestedModule); 304 } 305 306 if (isFramework()) 307 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath, 308 SuggestedModule, InUserSpecifiedSystemFramework); 309 310 assert(isHeaderMap() && "Unknown directory lookup"); 311 const HeaderMap *HM = getHeaderMap(); 312 SmallString<1024> Path; 313 StringRef Dest = HM->lookupFilename(Filename, Path); 314 if (Dest.empty()) 315 return nullptr; 316 317 const FileEntry *Result; 318 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)) { 323 MappedName.clear(); 324 MappedName.append(Dest.begin(), Dest.end()); 325 Filename = StringRef(MappedName.begin(), MappedName.size()); 326 HasBeenMapped = true; 327 Result = HM->LookupFile(Filename, HS.getFileMgr()); 328 329 } else { 330 Result = HS.getFileMgr().getFile(Dest); 331 } 332 333 if (Result) { 334 if (SearchPath) { 335 StringRef SearchPathRef(getName()); 336 SearchPath->clear(); 337 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); 338 } 339 if (RelativePath) { 340 RelativePath->clear(); 341 RelativePath->append(Filename.begin(), Filename.end()); 342 } 343 } 344 return Result; 345 } 346 347 /// \brief Given a framework directory, find the top-most framework directory. 348 /// 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"); 358 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 363 // 364 // #include <Foo/Frameworks/Bar.framework/Headers/Wibble.h> 365 // 366 // where 'Bar' used to be embedded in 'Foo', is now a top-level framework 367 // which one should access with, e.g., 368 // 369 // #include <Bar/Wibble.h> 370 // 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); 375 do { 376 // Get the parent directory name. 377 DirName = llvm::sys::path::parent_path(DirName); 378 if (DirName.empty()) 379 break; 380 381 // Determine whether this directory exists. 382 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName); 383 if (!Dir) 384 break; 385 386 // If this is a framework directory, then we're a subframework of this 387 // framework. 388 if (llvm::sys::path::extension(DirName) == ".framework") { 389 SubmodulePath.push_back(llvm::sys::path::stem(DirName)); 390 TopFrameworkDir = Dir; 391 } 392 } while (true); 393 394 return TopFrameworkDir; 395 } 396 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, 401 HeaderSearch &HS, 402 SmallVectorImpl<char> *SearchPath, 403 SmallVectorImpl<char> *RelativePath, 404 ModuleMap::KnownHeader *SuggestedModule, 405 bool &InUserSpecifiedSystemFramework) const 406 { 407 FileManager &FileMgr = HS.getFileMgr(); 408 409 // Framework names must have a '/' in the filename. 410 size_t SlashPos = Filename.find('/'); 411 if (SlashPos == StringRef::npos) return nullptr; 412 413 // Find out if this is the home for the specified framework, by checking 414 // HeaderSearch. Possible answers are yes/no and unknown. 415 HeaderSearch::FrameworkCacheEntry &CacheEntry = 416 HS.LookupFrameworkCache(Filename.substr(0, SlashPos)); 417 418 // If it is known and in some other directory, fail. 419 if (CacheEntry.Directory && CacheEntry.Directory != getFrameworkDir()) 420 return nullptr; 421 422 // Otherwise, construct the path to this framework dir. 423 424 // FrameworkName = "/System/Library/Frameworks/" 425 SmallString<1024> FrameworkName; 426 FrameworkName += getFrameworkDir()->getName(); 427 if (FrameworkName.empty() || FrameworkName.back() != '/') 428 FrameworkName.push_back('/'); 429 430 // FrameworkName = "/System/Library/Frameworks/Cocoa" 431 StringRef ModuleName(Filename.begin(), SlashPos); 432 FrameworkName += ModuleName; 433 434 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/" 435 FrameworkName += ".framework/"; 436 437 // If the cache entry was unresolved, populate it now. 438 if (!CacheEntry.Directory) { 439 HS.IncrementFrameworkLookupCount(); 440 441 // If the framework dir doesn't exist, we fail. 442 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName); 443 if (!Dir) return nullptr; 444 445 // Otherwise, if it does, remember that this is the right direntry for this 446 // framework. 447 CacheEntry.Directory = getFrameworkDir(); 448 449 // If this is a user search directory, check if the framework has been 450 // user-specified as a system framework. 451 if (getDirCharacteristic() == SrcMgr::C_User) { 452 SmallString<1024> SystemFrameworkMarker(FrameworkName); 453 SystemFrameworkMarker += ".system_framework"; 454 if (llvm::sys::fs::exists(SystemFrameworkMarker)) { 455 CacheEntry.IsUserSpecifiedSystemFramework = true; 456 } 457 } 458 } 459 460 // Set the 'user-specified system framework' flag. 461 InUserSpecifiedSystemFramework = CacheEntry.IsUserSpecifiedSystemFramework; 462 463 if (RelativePath) { 464 RelativePath->clear(); 465 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end()); 466 } 467 468 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h" 469 unsigned OrigSize = FrameworkName.size(); 470 471 FrameworkName += "Headers/"; 472 473 if (SearchPath) { 474 SearchPath->clear(); 475 // Without trailing '/'. 476 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1); 477 } 478 479 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end()); 480 const FileEntry *FE = FileMgr.getFile(FrameworkName, 481 /*openFile=*/!SuggestedModule); 482 if (!FE) { 483 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h" 484 const char *Private = "Private"; 485 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private, 486 Private+strlen(Private)); 487 if (SearchPath) 488 SearchPath->insert(SearchPath->begin()+OrigSize, Private, 489 Private+strlen(Private)); 490 491 FE = FileMgr.getFile(FrameworkName, /*openFile=*/!SuggestedModule); 492 } 493 494 // If we found the header and are allowed to suggest a module, do so now. 495 if (FE && SuggestedModule) { 496 // Find the framework in which this header occurs. 497 StringRef FrameworkPath = FE->getDir()->getName(); 498 bool FoundFramework = false; 499 do { 500 // Determine whether this directory exists. 501 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkPath); 502 if (!Dir) 503 break; 504 505 // If this is a framework directory, then we're a subframework of this 506 // framework. 507 if (llvm::sys::path::extension(FrameworkPath) == ".framework") { 508 FoundFramework = true; 509 break; 510 } 511 512 // Get the parent directory name. 513 FrameworkPath = llvm::sys::path::parent_path(FrameworkPath); 514 if (FrameworkPath.empty()) 515 break; 516 } while (true); 517 518 if (FoundFramework) { 519 // Find the top-level framework based on this framework. 520 SmallVector<std::string, 4> SubmodulePath; 521 const DirectoryEntry *TopFrameworkDir 522 = ::getTopFrameworkDir(FileMgr, FrameworkPath, SubmodulePath); 523 524 // Determine the name of the top-level framework. 525 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName()); 526 527 // Load this framework module. If that succeeds, find the suggested module 528 // for this header, if any. 529 bool IsSystem = getDirCharacteristic() != SrcMgr::C_User; 530 if (HS.loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem)) { 531 *SuggestedModule = HS.findModuleForHeader(FE); 532 } 533 } else { 534 *SuggestedModule = HS.findModuleForHeader(FE); 535 } 536 } 537 return FE; 538 } 539 540 void HeaderSearch::setTarget(const TargetInfo &Target) { 541 ModMap.setTarget(Target); 542 } 543 544 545 //===----------------------------------------------------------------------===// 546 // Header File Location. 547 //===----------------------------------------------------------------------===// 548 549 /// \brief Return true with a diagnostic if the file that MSVC would have found 550 /// fails to match the one that Clang would have found with MSVC header search 551 /// disabled. 552 static bool checkMSVCHeaderSearch(DiagnosticsEngine &Diags, 553 const FileEntry *MSFE, const FileEntry *FE, 554 SourceLocation IncludeLoc) { 555 if (MSFE && FE != MSFE) { 556 Diags.Report(IncludeLoc, diag::ext_pp_include_search_ms) << MSFE->getName(); 557 return true; 558 } 559 return false; 560 } 561 562 static const char *copyString(StringRef Str, llvm::BumpPtrAllocator &Alloc) { 563 assert(!Str.empty()); 564 char *CopyStr = Alloc.Allocate<char>(Str.size()+1); 565 std::copy(Str.begin(), Str.end(), CopyStr); 566 CopyStr[Str.size()] = '\0'; 567 return CopyStr; 568 } 569 570 /// LookupFile - Given a "foo" or \<foo> reference, look up the indicated file, 571 /// return null on failure. isAngled indicates whether the file reference is 572 /// for system \#include's or not (i.e. using <> instead of ""). Includers, if 573 /// non-empty, indicates where the \#including file(s) are, in case a relative 574 /// search is needed. Microsoft mode will pass all \#including files. 575 const FileEntry *HeaderSearch::LookupFile( 576 StringRef Filename, SourceLocation IncludeLoc, bool isAngled, 577 const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir, 578 ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers, 579 SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath, 580 ModuleMap::KnownHeader *SuggestedModule, bool SkipCache) { 581 if (SuggestedModule) 582 *SuggestedModule = ModuleMap::KnownHeader(); 583 584 // If 'Filename' is absolute, check to see if it exists and no searching. 585 if (llvm::sys::path::is_absolute(Filename)) { 586 CurDir = nullptr; 587 588 // If this was an #include_next "/absolute/file", fail. 589 if (FromDir) return nullptr; 590 591 if (SearchPath) 592 SearchPath->clear(); 593 if (RelativePath) { 594 RelativePath->clear(); 595 RelativePath->append(Filename.begin(), Filename.end()); 596 } 597 // Otherwise, just return the file. 598 const FileEntry *File = FileMgr.getFile(Filename, /*openFile=*/true); 599 if (File && SuggestedModule) { 600 // If there is a module that corresponds to this header, suggest it. 601 hasModuleMap(Filename, File->getDir(), /*SystemHeaderDir*/false); 602 *SuggestedModule = findModuleForHeader(File); 603 } 604 return File; 605 } 606 607 // This is the header that MSVC's header search would have found. 608 const FileEntry *MSFE = nullptr; 609 ModuleMap::KnownHeader MSSuggestedModule; 610 611 // Unless disabled, check to see if the file is in the #includer's 612 // directory. This cannot be based on CurDir, because each includer could be 613 // a #include of a subdirectory (#include "foo/bar.h") and a subsequent 614 // include of "baz.h" should resolve to "whatever/foo/baz.h". 615 // This search is not done for <> headers. 616 if (!Includers.empty() && !isAngled && !NoCurDirSearch) { 617 SmallString<1024> TmpDir; 618 bool First = true; 619 for (const auto &IncluderAndDir : Includers) { 620 const FileEntry *Includer = IncluderAndDir.first; 621 622 // Concatenate the requested file onto the directory. 623 // FIXME: Portability. Filename concatenation should be in sys::Path. 624 TmpDir = IncluderAndDir.second->getName(); 625 TmpDir.push_back('/'); 626 TmpDir.append(Filename.begin(), Filename.end()); 627 628 // FIXME: We don't cache the result of getFileInfo across the call to 629 // getFileAndSuggestModule, because it's a reference to an element of 630 // a container that could be reallocated across this call. 631 // 632 // FIXME: If we have no includer, that means we're processing a #include 633 // from a module build. We should treat this as a system header if we're 634 // building a [system] module. 635 bool IncluderIsSystemHeader = 636 Includer && getFileInfo(Includer).DirInfo != SrcMgr::C_User; 637 if (const FileEntry *FE = getFileAndSuggestModule( 638 *this, TmpDir, IncluderAndDir.second, 639 IncluderIsSystemHeader, SuggestedModule)) { 640 if (!Includer) { 641 assert(First && "only first includer can have no file"); 642 return FE; 643 } 644 645 // Leave CurDir unset. 646 // This file is a system header or C++ unfriendly if the old file is. 647 // 648 // Note that we only use one of FromHFI/ToHFI at once, due to potential 649 // reallocation of the underlying vector potentially making the first 650 // reference binding dangling. 651 HeaderFileInfo &FromHFI = getFileInfo(Includer); 652 unsigned DirInfo = FromHFI.DirInfo; 653 bool IndexHeaderMapHeader = FromHFI.IndexHeaderMapHeader; 654 StringRef Framework = FromHFI.Framework; 655 656 HeaderFileInfo &ToHFI = getFileInfo(FE); 657 ToHFI.DirInfo = DirInfo; 658 ToHFI.IndexHeaderMapHeader = IndexHeaderMapHeader; 659 ToHFI.Framework = Framework; 660 661 if (SearchPath) { 662 StringRef SearchPathRef(IncluderAndDir.second->getName()); 663 SearchPath->clear(); 664 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); 665 } 666 if (RelativePath) { 667 RelativePath->clear(); 668 RelativePath->append(Filename.begin(), Filename.end()); 669 } 670 if (First) 671 return FE; 672 673 // Otherwise, we found the path via MSVC header search rules. If 674 // -Wmsvc-include is enabled, we have to keep searching to see if we 675 // would've found this header in -I or -isystem directories. 676 if (Diags.isIgnored(diag::ext_pp_include_search_ms, IncludeLoc)) { 677 return FE; 678 } else { 679 MSFE = FE; 680 if (SuggestedModule) { 681 MSSuggestedModule = *SuggestedModule; 682 *SuggestedModule = ModuleMap::KnownHeader(); 683 } 684 break; 685 } 686 } 687 First = false; 688 } 689 } 690 691 CurDir = nullptr; 692 693 // If this is a system #include, ignore the user #include locs. 694 unsigned i = isAngled ? AngledDirIdx : 0; 695 696 // If this is a #include_next request, start searching after the directory the 697 // file was found in. 698 if (FromDir) 699 i = FromDir-&SearchDirs[0]; 700 701 // Cache all of the lookups performed by this method. Many headers are 702 // multiply included, and the "pragma once" optimization prevents them from 703 // being relex/pp'd, but they would still have to search through a 704 // (potentially huge) series of SearchDirs to find it. 705 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename]; 706 707 // If the entry has been previously looked up, the first value will be 708 // non-zero. If the value is equal to i (the start point of our search), then 709 // this is a matching hit. 710 if (!SkipCache && CacheLookup.StartIdx == i+1) { 711 // Skip querying potentially lots of directories for this lookup. 712 i = CacheLookup.HitIdx; 713 if (CacheLookup.MappedName) 714 Filename = CacheLookup.MappedName; 715 } else { 716 // Otherwise, this is the first query, or the previous query didn't match 717 // our search start. We will fill in our found location below, so prime the 718 // start point value. 719 CacheLookup.reset(/*StartIdx=*/i+1); 720 } 721 722 SmallString<64> MappedName; 723 724 // Check each directory in sequence to see if it contains this file. 725 for (; i != SearchDirs.size(); ++i) { 726 bool InUserSpecifiedSystemFramework = false; 727 bool HasBeenMapped = false; 728 const FileEntry *FE = 729 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath, 730 SuggestedModule, InUserSpecifiedSystemFramework, 731 HasBeenMapped, MappedName); 732 if (HasBeenMapped) { 733 CacheLookup.MappedName = 734 copyString(Filename, LookupFileCache.getAllocator()); 735 } 736 if (!FE) continue; 737 738 CurDir = &SearchDirs[i]; 739 740 // This file is a system header or C++ unfriendly if the dir is. 741 HeaderFileInfo &HFI = getFileInfo(FE); 742 HFI.DirInfo = CurDir->getDirCharacteristic(); 743 744 // If the directory characteristic is User but this framework was 745 // user-specified to be treated as a system framework, promote the 746 // characteristic. 747 if (HFI.DirInfo == SrcMgr::C_User && InUserSpecifiedSystemFramework) 748 HFI.DirInfo = SrcMgr::C_System; 749 750 // If the filename matches a known system header prefix, override 751 // whether the file is a system header. 752 for (unsigned j = SystemHeaderPrefixes.size(); j; --j) { 753 if (Filename.startswith(SystemHeaderPrefixes[j-1].first)) { 754 HFI.DirInfo = SystemHeaderPrefixes[j-1].second ? SrcMgr::C_System 755 : SrcMgr::C_User; 756 break; 757 } 758 } 759 760 // If this file is found in a header map and uses the framework style of 761 // includes, then this header is part of a framework we're building. 762 if (CurDir->isIndexHeaderMap()) { 763 size_t SlashPos = Filename.find('/'); 764 if (SlashPos != StringRef::npos) { 765 HFI.IndexHeaderMapHeader = 1; 766 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(), 767 SlashPos)); 768 } 769 } 770 771 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) { 772 if (SuggestedModule) 773 *SuggestedModule = MSSuggestedModule; 774 return MSFE; 775 } 776 777 // Remember this location for the next lookup we do. 778 CacheLookup.HitIdx = i; 779 return FE; 780 } 781 782 // If we are including a file with a quoted include "foo.h" from inside 783 // a header in a framework that is currently being built, and we couldn't 784 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where 785 // "Foo" is the name of the framework in which the including header was found. 786 if (!Includers.empty() && Includers.front().first && !isAngled && 787 Filename.find('/') == StringRef::npos) { 788 HeaderFileInfo &IncludingHFI = getFileInfo(Includers.front().first); 789 if (IncludingHFI.IndexHeaderMapHeader) { 790 SmallString<128> ScratchFilename; 791 ScratchFilename += IncludingHFI.Framework; 792 ScratchFilename += '/'; 793 ScratchFilename += Filename; 794 795 const FileEntry *FE = LookupFile( 796 ScratchFilename, IncludeLoc, /*isAngled=*/true, FromDir, CurDir, 797 Includers.front(), SearchPath, RelativePath, SuggestedModule); 798 799 if (checkMSVCHeaderSearch(Diags, MSFE, FE, IncludeLoc)) { 800 if (SuggestedModule) 801 *SuggestedModule = MSSuggestedModule; 802 return MSFE; 803 } 804 805 LookupFileCacheInfo &CacheLookup = LookupFileCache[Filename]; 806 CacheLookup.HitIdx = LookupFileCache[ScratchFilename].HitIdx; 807 // FIXME: SuggestedModule. 808 return FE; 809 } 810 } 811 812 if (checkMSVCHeaderSearch(Diags, MSFE, nullptr, IncludeLoc)) { 813 if (SuggestedModule) 814 *SuggestedModule = MSSuggestedModule; 815 return MSFE; 816 } 817 818 // Otherwise, didn't find it. Remember we didn't find this. 819 CacheLookup.HitIdx = SearchDirs.size(); 820 return nullptr; 821 } 822 823 /// LookupSubframeworkHeader - Look up a subframework for the specified 824 /// \#include file. For example, if \#include'ing <HIToolbox/HIToolbox.h> from 825 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox 826 /// is a subframework within Carbon.framework. If so, return the FileEntry 827 /// for the designated file, otherwise return null. 828 const FileEntry *HeaderSearch:: 829 LookupSubframeworkHeader(StringRef Filename, 830 const FileEntry *ContextFileEnt, 831 SmallVectorImpl<char> *SearchPath, 832 SmallVectorImpl<char> *RelativePath, 833 ModuleMap::KnownHeader *SuggestedModule) { 834 assert(ContextFileEnt && "No context file?"); 835 836 // Framework names must have a '/' in the filename. Find it. 837 // FIXME: Should we permit '\' on Windows? 838 size_t SlashPos = Filename.find('/'); 839 if (SlashPos == StringRef::npos) return nullptr; 840 841 // Look up the base framework name of the ContextFileEnt. 842 const char *ContextName = ContextFileEnt->getName(); 843 844 // If the context info wasn't a framework, couldn't be a subframework. 845 const unsigned DotFrameworkLen = 10; 846 const char *FrameworkPos = strstr(ContextName, ".framework"); 847 if (FrameworkPos == nullptr || 848 (FrameworkPos[DotFrameworkLen] != '/' && 849 FrameworkPos[DotFrameworkLen] != '\\')) 850 return nullptr; 851 852 SmallString<1024> FrameworkName(ContextName, FrameworkPos+DotFrameworkLen+1); 853 854 // Append Frameworks/HIToolbox.framework/ 855 FrameworkName += "Frameworks/"; 856 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos); 857 FrameworkName += ".framework/"; 858 859 auto &CacheLookup = 860 *FrameworkMap.insert(std::make_pair(Filename.substr(0, SlashPos), 861 FrameworkCacheEntry())).first; 862 863 // Some other location? 864 if (CacheLookup.second.Directory && 865 CacheLookup.first().size() == FrameworkName.size() && 866 memcmp(CacheLookup.first().data(), &FrameworkName[0], 867 CacheLookup.first().size()) != 0) 868 return nullptr; 869 870 // Cache subframework. 871 if (!CacheLookup.second.Directory) { 872 ++NumSubFrameworkLookups; 873 874 // If the framework dir doesn't exist, we fail. 875 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName); 876 if (!Dir) return nullptr; 877 878 // Otherwise, if it does, remember that this is the right direntry for this 879 // framework. 880 CacheLookup.second.Directory = Dir; 881 } 882 883 const FileEntry *FE = nullptr; 884 885 if (RelativePath) { 886 RelativePath->clear(); 887 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end()); 888 } 889 890 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h" 891 SmallString<1024> HeadersFilename(FrameworkName); 892 HeadersFilename += "Headers/"; 893 if (SearchPath) { 894 SearchPath->clear(); 895 // Without trailing '/'. 896 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1); 897 } 898 899 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); 900 if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true))) { 901 902 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h" 903 HeadersFilename = FrameworkName; 904 HeadersFilename += "PrivateHeaders/"; 905 if (SearchPath) { 906 SearchPath->clear(); 907 // Without trailing '/'. 908 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1); 909 } 910 911 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); 912 if (!(FE = FileMgr.getFile(HeadersFilename, /*openFile=*/true))) 913 return nullptr; 914 } 915 916 // This file is a system header or C++ unfriendly if the old file is. 917 // 918 // Note that the temporary 'DirInfo' is required here, as either call to 919 // getFileInfo could resize the vector and we don't want to rely on order 920 // of evaluation. 921 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo; 922 getFileInfo(FE).DirInfo = DirInfo; 923 924 // If we're supposed to suggest a module, look for one now. 925 if (SuggestedModule) { 926 // Find the top-level framework based on this framework. 927 FrameworkName.pop_back(); // remove the trailing '/' 928 SmallVector<std::string, 4> SubmodulePath; 929 const DirectoryEntry *TopFrameworkDir 930 = ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath); 931 932 // Determine the name of the top-level framework. 933 StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName()); 934 935 // Load this framework module. If that succeeds, find the suggested module 936 // for this header, if any. 937 bool IsSystem = false; 938 if (loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystem)) { 939 *SuggestedModule = findModuleForHeader(FE); 940 } 941 } 942 943 return FE; 944 } 945 946 //===----------------------------------------------------------------------===// 947 // File Info Management. 948 //===----------------------------------------------------------------------===// 949 950 /// \brief Merge the header file info provided by \p OtherHFI into the current 951 /// header file info (\p HFI) 952 static void mergeHeaderFileInfo(HeaderFileInfo &HFI, 953 const HeaderFileInfo &OtherHFI) { 954 HFI.isImport |= OtherHFI.isImport; 955 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce; 956 HFI.isModuleHeader |= OtherHFI.isModuleHeader; 957 HFI.NumIncludes += OtherHFI.NumIncludes; 958 959 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) { 960 HFI.ControllingMacro = OtherHFI.ControllingMacro; 961 HFI.ControllingMacroID = OtherHFI.ControllingMacroID; 962 } 963 964 if (OtherHFI.External) { 965 HFI.DirInfo = OtherHFI.DirInfo; 966 HFI.External = OtherHFI.External; 967 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader; 968 } 969 970 if (HFI.Framework.empty()) 971 HFI.Framework = OtherHFI.Framework; 972 973 HFI.Resolved = true; 974 } 975 976 /// getFileInfo - Return the HeaderFileInfo structure for the specified 977 /// FileEntry. 978 HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) { 979 if (FE->getUID() >= FileInfo.size()) 980 FileInfo.resize(FE->getUID()+1); 981 982 HeaderFileInfo &HFI = FileInfo[FE->getUID()]; 983 if (ExternalSource && !HFI.Resolved) 984 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE)); 985 HFI.IsValid = 1; 986 return HFI; 987 } 988 989 bool HeaderSearch::tryGetFileInfo(const FileEntry *FE, HeaderFileInfo &Result) const { 990 if (FE->getUID() >= FileInfo.size()) 991 return false; 992 const HeaderFileInfo &HFI = FileInfo[FE->getUID()]; 993 if (HFI.IsValid) { 994 Result = HFI; 995 return true; 996 } 997 return false; 998 } 999 1000 bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) { 1001 // Check if we've ever seen this file as a header. 1002 if (File->getUID() >= FileInfo.size()) 1003 return false; 1004 1005 // Resolve header file info from the external source, if needed. 1006 HeaderFileInfo &HFI = FileInfo[File->getUID()]; 1007 if (ExternalSource && !HFI.Resolved) 1008 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File)); 1009 1010 return HFI.isPragmaOnce || HFI.isImport || 1011 HFI.ControllingMacro || HFI.ControllingMacroID; 1012 } 1013 1014 void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE, 1015 ModuleMap::ModuleHeaderRole Role, 1016 bool isCompilingModuleHeader) { 1017 if (FE->getUID() >= FileInfo.size()) 1018 FileInfo.resize(FE->getUID()+1); 1019 1020 HeaderFileInfo &HFI = FileInfo[FE->getUID()]; 1021 HFI.isModuleHeader = true; 1022 HFI.isCompilingModuleHeader = isCompilingModuleHeader; 1023 HFI.setHeaderRole(Role); 1024 } 1025 1026 bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP, 1027 const FileEntry *File, 1028 bool isImport) { 1029 ++NumIncluded; // Count # of attempted #includes. 1030 1031 // Get information about this file. 1032 HeaderFileInfo &FileInfo = getFileInfo(File); 1033 1034 // If this is a #import directive, check that we have not already imported 1035 // this header. 1036 if (isImport) { 1037 // If this has already been imported, don't import it again. 1038 FileInfo.isImport = true; 1039 1040 // Has this already been #import'ed or #include'd? 1041 if (FileInfo.NumIncludes) return false; 1042 } else { 1043 // Otherwise, if this is a #include of a file that was previously #import'd 1044 // or if this is the second #include of a #pragma once file, ignore it. 1045 if (FileInfo.isImport) 1046 return false; 1047 } 1048 1049 // Next, check to see if the file is wrapped with #ifndef guards. If so, and 1050 // if the macro that guards it is defined, we know the #include has no effect. 1051 if (const IdentifierInfo *ControllingMacro 1052 = FileInfo.getControllingMacro(ExternalLookup)) 1053 if (PP.isMacroDefined(ControllingMacro)) { 1054 ++NumMultiIncludeFileOptzn; 1055 return false; 1056 } 1057 1058 // Increment the number of times this file has been included. 1059 ++FileInfo.NumIncludes; 1060 1061 return true; 1062 } 1063 1064 size_t HeaderSearch::getTotalMemory() const { 1065 return SearchDirs.capacity() 1066 + llvm::capacity_in_bytes(FileInfo) 1067 + llvm::capacity_in_bytes(HeaderMaps) 1068 + LookupFileCache.getAllocator().getTotalMemory() 1069 + FrameworkMap.getAllocator().getTotalMemory(); 1070 } 1071 1072 StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) { 1073 return FrameworkNames.insert(Framework).first->first(); 1074 } 1075 1076 bool HeaderSearch::hasModuleMap(StringRef FileName, 1077 const DirectoryEntry *Root, 1078 bool IsSystem) { 1079 if (!HSOpts->ModuleMaps || !LangOpts.ModulesImplicitMaps) 1080 return false; 1081 1082 SmallVector<const DirectoryEntry *, 2> FixUpDirectories; 1083 1084 StringRef DirName = FileName; 1085 do { 1086 // Get the parent directory name. 1087 DirName = llvm::sys::path::parent_path(DirName); 1088 if (DirName.empty()) 1089 return false; 1090 1091 // Determine whether this directory exists. 1092 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName); 1093 if (!Dir) 1094 return false; 1095 1096 // Try to load the module map file in this directory. 1097 switch (loadModuleMapFile(Dir, IsSystem, 1098 llvm::sys::path::extension(Dir->getName()) == 1099 ".framework")) { 1100 case LMM_NewlyLoaded: 1101 case LMM_AlreadyLoaded: 1102 // Success. All of the directories we stepped through inherit this module 1103 // map file. 1104 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I) 1105 DirectoryHasModuleMap[FixUpDirectories[I]] = true; 1106 return true; 1107 1108 case LMM_NoDirectory: 1109 case LMM_InvalidModuleMap: 1110 break; 1111 } 1112 1113 // If we hit the top of our search, we're done. 1114 if (Dir == Root) 1115 return false; 1116 1117 // Keep track of all of the directories we checked, so we can mark them as 1118 // having module maps if we eventually do find a module map. 1119 FixUpDirectories.push_back(Dir); 1120 } while (true); 1121 } 1122 1123 ModuleMap::KnownHeader 1124 HeaderSearch::findModuleForHeader(const FileEntry *File) const { 1125 if (ExternalSource) { 1126 // Make sure the external source has handled header info about this file, 1127 // which includes whether the file is part of a module. 1128 (void)getFileInfo(File); 1129 } 1130 return ModMap.findModuleForHeader(File); 1131 } 1132 1133 static const FileEntry *getPrivateModuleMap(const FileEntry *File, 1134 FileManager &FileMgr) { 1135 StringRef Filename = llvm::sys::path::filename(File->getName()); 1136 SmallString<128> PrivateFilename(File->getDir()->getName()); 1137 if (Filename == "module.map") 1138 llvm::sys::path::append(PrivateFilename, "module_private.map"); 1139 else if (Filename == "module.modulemap") 1140 llvm::sys::path::append(PrivateFilename, "module.private.modulemap"); 1141 else 1142 return nullptr; 1143 return FileMgr.getFile(PrivateFilename); 1144 } 1145 1146 bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem) { 1147 // Find the directory for the module. For frameworks, that may require going 1148 // up from the 'Modules' directory. 1149 const DirectoryEntry *Dir = nullptr; 1150 if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd) 1151 Dir = FileMgr.getDirectory("."); 1152 else { 1153 Dir = File->getDir(); 1154 StringRef DirName(Dir->getName()); 1155 if (llvm::sys::path::filename(DirName) == "Modules") { 1156 DirName = llvm::sys::path::parent_path(DirName); 1157 if (DirName.endswith(".framework")) 1158 Dir = FileMgr.getDirectory(DirName); 1159 // FIXME: This assert can fail if there's a race between the above check 1160 // and the removal of the directory. 1161 assert(Dir && "parent must exist"); 1162 } 1163 } 1164 1165 switch (loadModuleMapFileImpl(File, IsSystem, Dir)) { 1166 case LMM_AlreadyLoaded: 1167 case LMM_NewlyLoaded: 1168 return false; 1169 case LMM_NoDirectory: 1170 case LMM_InvalidModuleMap: 1171 return true; 1172 } 1173 llvm_unreachable("Unknown load module map result"); 1174 } 1175 1176 HeaderSearch::LoadModuleMapResult 1177 HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem, 1178 const DirectoryEntry *Dir) { 1179 assert(File && "expected FileEntry"); 1180 1181 // Check whether we've already loaded this module map, and mark it as being 1182 // loaded in case we recursively try to load it from itself. 1183 auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true)); 1184 if (!AddResult.second) 1185 return AddResult.first->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap; 1186 1187 if (ModMap.parseModuleMapFile(File, IsSystem, Dir)) { 1188 LoadedModuleMaps[File] = false; 1189 return LMM_InvalidModuleMap; 1190 } 1191 1192 // Try to load a corresponding private module map. 1193 if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) { 1194 if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) { 1195 LoadedModuleMaps[File] = false; 1196 return LMM_InvalidModuleMap; 1197 } 1198 } 1199 1200 // This directory has a module map. 1201 return LMM_NewlyLoaded; 1202 } 1203 1204 const FileEntry * 1205 HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) { 1206 if (!LangOpts.ModulesImplicitMaps) 1207 return nullptr; 1208 // For frameworks, the preferred spelling is Modules/module.modulemap, but 1209 // module.map at the framework root is also accepted. 1210 SmallString<128> ModuleMapFileName(Dir->getName()); 1211 if (IsFramework) 1212 llvm::sys::path::append(ModuleMapFileName, "Modules"); 1213 llvm::sys::path::append(ModuleMapFileName, "module.modulemap"); 1214 if (const FileEntry *F = FileMgr.getFile(ModuleMapFileName)) 1215 return F; 1216 1217 // Continue to allow module.map 1218 ModuleMapFileName = Dir->getName(); 1219 llvm::sys::path::append(ModuleMapFileName, "module.map"); 1220 return FileMgr.getFile(ModuleMapFileName); 1221 } 1222 1223 Module *HeaderSearch::loadFrameworkModule(StringRef Name, 1224 const DirectoryEntry *Dir, 1225 bool IsSystem) { 1226 if (Module *Module = ModMap.findModule(Name)) 1227 return Module; 1228 1229 // Try to load a module map file. 1230 switch (loadModuleMapFile(Dir, IsSystem, /*IsFramework*/true)) { 1231 case LMM_InvalidModuleMap: 1232 break; 1233 1234 case LMM_AlreadyLoaded: 1235 case LMM_NoDirectory: 1236 return nullptr; 1237 1238 case LMM_NewlyLoaded: 1239 return ModMap.findModule(Name); 1240 } 1241 1242 1243 // Try to infer a module map from the framework directory. 1244 if (LangOpts.ModulesImplicitMaps) 1245 return ModMap.inferFrameworkModule(Name, Dir, IsSystem, /*Parent=*/nullptr); 1246 1247 return nullptr; 1248 } 1249 1250 1251 HeaderSearch::LoadModuleMapResult 1252 HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem, 1253 bool IsFramework) { 1254 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName)) 1255 return loadModuleMapFile(Dir, IsSystem, IsFramework); 1256 1257 return LMM_NoDirectory; 1258 } 1259 1260 HeaderSearch::LoadModuleMapResult 1261 HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem, 1262 bool IsFramework) { 1263 auto KnownDir = DirectoryHasModuleMap.find(Dir); 1264 if (KnownDir != DirectoryHasModuleMap.end()) 1265 return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap; 1266 1267 if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) { 1268 LoadModuleMapResult Result = 1269 loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir); 1270 // Add Dir explicitly in case ModuleMapFile is in a subdirectory. 1271 // E.g. Foo.framework/Modules/module.modulemap 1272 // ^Dir ^ModuleMapFile 1273 if (Result == LMM_NewlyLoaded) 1274 DirectoryHasModuleMap[Dir] = true; 1275 else if (Result == LMM_InvalidModuleMap) 1276 DirectoryHasModuleMap[Dir] = false; 1277 return Result; 1278 } 1279 return LMM_InvalidModuleMap; 1280 } 1281 1282 void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) { 1283 Modules.clear(); 1284 1285 if (LangOpts.ModulesImplicitMaps) { 1286 // Load module maps for each of the header search directories. 1287 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { 1288 bool IsSystem = SearchDirs[Idx].isSystemHeaderDirectory(); 1289 if (SearchDirs[Idx].isFramework()) { 1290 std::error_code EC; 1291 SmallString<128> DirNative; 1292 llvm::sys::path::native(SearchDirs[Idx].getFrameworkDir()->getName(), 1293 DirNative); 1294 1295 // Search each of the ".framework" directories to load them as modules. 1296 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd; 1297 Dir != DirEnd && !EC; Dir.increment(EC)) { 1298 if (llvm::sys::path::extension(Dir->path()) != ".framework") 1299 continue; 1300 1301 const DirectoryEntry *FrameworkDir = 1302 FileMgr.getDirectory(Dir->path()); 1303 if (!FrameworkDir) 1304 continue; 1305 1306 // Load this framework module. 1307 loadFrameworkModule(llvm::sys::path::stem(Dir->path()), FrameworkDir, 1308 IsSystem); 1309 } 1310 continue; 1311 } 1312 1313 // FIXME: Deal with header maps. 1314 if (SearchDirs[Idx].isHeaderMap()) 1315 continue; 1316 1317 // Try to load a module map file for the search directory. 1318 loadModuleMapFile(SearchDirs[Idx].getDir(), IsSystem, 1319 /*IsFramework*/ false); 1320 1321 // Try to load module map files for immediate subdirectories of this 1322 // search directory. 1323 loadSubdirectoryModuleMaps(SearchDirs[Idx]); 1324 } 1325 } 1326 1327 // Populate the list of modules. 1328 for (ModuleMap::module_iterator M = ModMap.module_begin(), 1329 MEnd = ModMap.module_end(); 1330 M != MEnd; ++M) { 1331 Modules.push_back(M->getValue()); 1332 } 1333 } 1334 1335 void HeaderSearch::loadTopLevelSystemModules() { 1336 if (!LangOpts.ModulesImplicitMaps) 1337 return; 1338 1339 // Load module maps for each of the header search directories. 1340 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { 1341 // We only care about normal header directories. 1342 if (!SearchDirs[Idx].isNormalDir()) { 1343 continue; 1344 } 1345 1346 // Try to load a module map file for the search directory. 1347 loadModuleMapFile(SearchDirs[Idx].getDir(), 1348 SearchDirs[Idx].isSystemHeaderDirectory(), 1349 SearchDirs[Idx].isFramework()); 1350 } 1351 } 1352 1353 void HeaderSearch::loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir) { 1354 assert(LangOpts.ModulesImplicitMaps && 1355 "Should not be loading subdirectory module maps"); 1356 1357 if (SearchDir.haveSearchedAllModuleMaps()) 1358 return; 1359 1360 std::error_code EC; 1361 SmallString<128> DirNative; 1362 llvm::sys::path::native(SearchDir.getDir()->getName(), DirNative); 1363 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd; 1364 Dir != DirEnd && !EC; Dir.increment(EC)) { 1365 bool IsFramework = llvm::sys::path::extension(Dir->path()) == ".framework"; 1366 if (IsFramework == SearchDir.isFramework()) 1367 loadModuleMapFile(Dir->path(), SearchDir.isSystemHeaderDirectory(), 1368 SearchDir.isFramework()); 1369 } 1370 1371 SearchDir.setSearchedAllModuleMaps(true); 1372 } 1373