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/Lex/HeaderMap.h" 16 #include "clang/Lex/Lexer.h" 17 #include "clang/Basic/Diagnostic.h" 18 #include "clang/Basic/FileManager.h" 19 #include "clang/Basic/IdentifierTable.h" 20 #include "llvm/Support/FileSystem.h" 21 #include "llvm/Support/Path.h" 22 #include "llvm/ADT/SmallString.h" 23 #include "llvm/Support/Capacity.h" 24 #include <cstdio> 25 using namespace clang; 26 27 const IdentifierInfo * 28 HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) { 29 if (ControllingMacro) 30 return ControllingMacro; 31 32 if (!ControllingMacroID || !External) 33 return 0; 34 35 ControllingMacro = External->GetIdentifier(ControllingMacroID); 36 return ControllingMacro; 37 } 38 39 ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {} 40 41 HeaderSearch::HeaderSearch(FileManager &FM, DiagnosticsEngine &Diags, 42 const LangOptions &LangOpts) 43 : FileMgr(FM), Diags(Diags), FrameworkMap(64), 44 ModMap(FileMgr, *Diags.getClient(), LangOpts) 45 { 46 AngledDirIdx = 0; 47 SystemDirIdx = 0; 48 NoCurDirSearch = false; 49 50 ExternalLookup = 0; 51 ExternalSource = 0; 52 NumIncluded = 0; 53 NumMultiIncludeFileOptzn = 0; 54 NumFrameworkLookups = NumSubFrameworkLookups = 0; 55 } 56 57 HeaderSearch::~HeaderSearch() { 58 // Delete headermaps. 59 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) 60 delete HeaderMaps[i].second; 61 } 62 63 void HeaderSearch::PrintStats() { 64 fprintf(stderr, "\n*** HeaderSearch Stats:\n"); 65 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size()); 66 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0; 67 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) { 68 NumOnceOnlyFiles += FileInfo[i].isImport; 69 if (MaxNumIncludes < FileInfo[i].NumIncludes) 70 MaxNumIncludes = FileInfo[i].NumIncludes; 71 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1; 72 } 73 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles); 74 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles); 75 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes); 76 77 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded); 78 fprintf(stderr, " %d #includes skipped due to" 79 " the multi-include optimization.\n", NumMultiIncludeFileOptzn); 80 81 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups); 82 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups); 83 } 84 85 /// CreateHeaderMap - This method returns a HeaderMap for the specified 86 /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure. 87 const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) { 88 // We expect the number of headermaps to be small, and almost always empty. 89 // If it ever grows, use of a linear search should be re-evaluated. 90 if (!HeaderMaps.empty()) { 91 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) 92 // Pointer equality comparison of FileEntries works because they are 93 // already uniqued by inode. 94 if (HeaderMaps[i].first == FE) 95 return HeaderMaps[i].second; 96 } 97 98 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) { 99 HeaderMaps.push_back(std::make_pair(FE, HM)); 100 return HM; 101 } 102 103 return 0; 104 } 105 106 const FileEntry *HeaderSearch::lookupModule(StringRef ModuleName, 107 Module *&Module, 108 std::string *ModuleFileName) { 109 Module = 0; 110 111 // If we don't have a module cache path, we can't do anything. 112 if (ModuleCachePath.empty()) { 113 if (ModuleFileName) 114 ModuleFileName->clear(); 115 return 0; 116 } 117 118 // Try to find the module path. 119 llvm::SmallString<256> FileName(ModuleCachePath); 120 llvm::sys::path::append(FileName, ModuleName + ".pcm"); 121 if (ModuleFileName) 122 *ModuleFileName = FileName.str(); 123 124 // Look in the module map to determine if there is a module by this name. 125 Module = ModMap.findModule(ModuleName); 126 if (!Module) { 127 // Look through the various header search paths to load any avaiable module 128 // maps, searching for a module map that describes this module. 129 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) { 130 if (SearchDirs[Idx].isFramework()) { 131 // Search for or infer a module map for a framework. 132 llvm::SmallString<128> FrameworkDirName; 133 FrameworkDirName += SearchDirs[Idx].getFrameworkDir()->getName(); 134 llvm::sys::path::append(FrameworkDirName, ModuleName + ".framework"); 135 if (const DirectoryEntry *FrameworkDir 136 = FileMgr.getDirectory(FrameworkDirName)) { 137 Module = getFrameworkModule(ModuleName, FrameworkDir); 138 if (Module) 139 break; 140 } 141 } 142 143 // FIXME: Figure out how header maps and module maps will work together. 144 145 // Only deal with normal search directories. 146 if (!SearchDirs[Idx].isNormalDir()) 147 continue; 148 149 // Search for a module map file in this directory. 150 if (loadModuleMapFile(SearchDirs[Idx].getDir()) == LMM_NewlyLoaded) { 151 // We just loaded a module map file; check whether the module is 152 // available now. 153 Module = ModMap.findModule(ModuleName); 154 if (Module) 155 break; 156 } 157 158 // Search for a module map in a subdirectory with the same name as the 159 // module. 160 llvm::SmallString<128> NestedModuleMapDirName; 161 NestedModuleMapDirName = SearchDirs[Idx].getDir()->getName(); 162 llvm::sys::path::append(NestedModuleMapDirName, ModuleName); 163 if (loadModuleMapFile(NestedModuleMapDirName) == LMM_NewlyLoaded) { 164 // If we just loaded a module map file, look for the module again. 165 Module = ModMap.findModule(ModuleName); 166 if (Module) 167 break; 168 } 169 } 170 } 171 172 // Look for the module file in the module cache. 173 // FIXME: If we didn't find a description of the module itself, should we 174 // even try to find the module in the cache? 175 return getFileMgr().getFile(FileName, /*OpenFile=*/false, 176 /*CacheFailure=*/false); 177 } 178 179 //===----------------------------------------------------------------------===// 180 // File lookup within a DirectoryLookup scope 181 //===----------------------------------------------------------------------===// 182 183 /// getName - Return the directory or filename corresponding to this lookup 184 /// object. 185 const char *DirectoryLookup::getName() const { 186 if (isNormalDir()) 187 return getDir()->getName(); 188 if (isFramework()) 189 return getFrameworkDir()->getName(); 190 assert(isHeaderMap() && "Unknown DirectoryLookup"); 191 return getHeaderMap()->getFileName(); 192 } 193 194 195 /// LookupFile - Lookup the specified file in this search path, returning it 196 /// if it exists or returning null if not. 197 const FileEntry *DirectoryLookup::LookupFile( 198 StringRef Filename, 199 HeaderSearch &HS, 200 SmallVectorImpl<char> *SearchPath, 201 SmallVectorImpl<char> *RelativePath, 202 Module **SuggestedModule) const { 203 llvm::SmallString<1024> TmpDir; 204 if (isNormalDir()) { 205 // Concatenate the requested file onto the directory. 206 TmpDir = getDir()->getName(); 207 llvm::sys::path::append(TmpDir, Filename); 208 if (SearchPath != NULL) { 209 StringRef SearchPathRef(getDir()->getName()); 210 SearchPath->clear(); 211 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); 212 } 213 if (RelativePath != NULL) { 214 RelativePath->clear(); 215 RelativePath->append(Filename.begin(), Filename.end()); 216 } 217 218 // If we have a module map that might map this header, load it and 219 // check whether we'll have a suggestion for a module. 220 if (SuggestedModule && HS.hasModuleMap(TmpDir, getDir())) { 221 const FileEntry *File = HS.getFileMgr().getFile(TmpDir.str(), 222 /*openFile=*/false); 223 if (!File) 224 return File; 225 226 // If there is a module that corresponds to this header, 227 // suggest it. 228 *SuggestedModule = HS.findModuleForHeader(File); 229 return File; 230 } 231 232 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true); 233 } 234 235 if (isFramework()) 236 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath, 237 SuggestedModule); 238 239 assert(isHeaderMap() && "Unknown directory lookup"); 240 const FileEntry * const Result = getHeaderMap()->LookupFile( 241 Filename, HS.getFileMgr()); 242 if (Result) { 243 if (SearchPath != NULL) { 244 StringRef SearchPathRef(getName()); 245 SearchPath->clear(); 246 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); 247 } 248 if (RelativePath != NULL) { 249 RelativePath->clear(); 250 RelativePath->append(Filename.begin(), Filename.end()); 251 } 252 } 253 return Result; 254 } 255 256 257 /// DoFrameworkLookup - Do a lookup of the specified file in the current 258 /// DirectoryLookup, which is a framework directory. 259 const FileEntry *DirectoryLookup::DoFrameworkLookup( 260 StringRef Filename, 261 HeaderSearch &HS, 262 SmallVectorImpl<char> *SearchPath, 263 SmallVectorImpl<char> *RelativePath, 264 Module **SuggestedModule) const 265 { 266 FileManager &FileMgr = HS.getFileMgr(); 267 268 // Framework names must have a '/' in the filename. 269 size_t SlashPos = Filename.find('/'); 270 if (SlashPos == StringRef::npos) return 0; 271 272 // Find out if this is the home for the specified framework, by checking 273 // HeaderSearch. Possible answer are yes/no and unknown. 274 const DirectoryEntry *&FrameworkDirCache = 275 HS.LookupFrameworkCache(Filename.substr(0, SlashPos)); 276 277 // If it is known and in some other directory, fail. 278 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir()) 279 return 0; 280 281 // Otherwise, construct the path to this framework dir. 282 283 // FrameworkName = "/System/Library/Frameworks/" 284 llvm::SmallString<1024> FrameworkName; 285 FrameworkName += getFrameworkDir()->getName(); 286 if (FrameworkName.empty() || FrameworkName.back() != '/') 287 FrameworkName.push_back('/'); 288 289 // FrameworkName = "/System/Library/Frameworks/Cocoa" 290 StringRef ModuleName(Filename.begin(), SlashPos); 291 FrameworkName += ModuleName; 292 293 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/" 294 FrameworkName += ".framework/"; 295 296 // If the cache entry is still unresolved, query to see if the cache entry is 297 // still unresolved. If so, check its existence now. 298 if (FrameworkDirCache == 0) { 299 HS.IncrementFrameworkLookupCount(); 300 301 // If the framework dir doesn't exist, we fail. 302 // FIXME: It's probably more efficient to query this with FileMgr.getDir. 303 bool Exists; 304 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists) 305 return 0; 306 307 // Otherwise, if it does, remember that this is the right direntry for this 308 // framework. 309 FrameworkDirCache = getFrameworkDir(); 310 } 311 312 if (RelativePath != NULL) { 313 RelativePath->clear(); 314 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end()); 315 } 316 317 // If we're allowed to look for modules, try to load or create the module 318 // corresponding to this framework. 319 Module *Module = 0; 320 if (SuggestedModule) { 321 if (const DirectoryEntry *FrameworkDir 322 = FileMgr.getDirectory(FrameworkName)) 323 Module = HS.getFrameworkModule(ModuleName, FrameworkDir); 324 } 325 326 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h" 327 unsigned OrigSize = FrameworkName.size(); 328 329 FrameworkName += "Headers/"; 330 331 if (SearchPath != NULL) { 332 SearchPath->clear(); 333 // Without trailing '/'. 334 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1); 335 } 336 337 // Determine whether this is the module we're building or not. 338 bool AutomaticImport = Module; 339 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end()); 340 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(), 341 /*openFile=*/!AutomaticImport)) { 342 if (AutomaticImport) 343 *SuggestedModule = HS.findModuleForHeader(FE); 344 return FE; 345 } 346 347 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h" 348 const char *Private = "Private"; 349 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private, 350 Private+strlen(Private)); 351 if (SearchPath != NULL) 352 SearchPath->insert(SearchPath->begin()+OrigSize, Private, 353 Private+strlen(Private)); 354 355 const FileEntry *FE = FileMgr.getFile(FrameworkName.str(), 356 /*openFile=*/!AutomaticImport); 357 if (FE && AutomaticImport) 358 *SuggestedModule = HS.findModuleForHeader(FE); 359 return FE; 360 } 361 362 363 //===----------------------------------------------------------------------===// 364 // Header File Location. 365 //===----------------------------------------------------------------------===// 366 367 368 /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, 369 /// return null on failure. isAngled indicates whether the file reference is 370 /// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if 371 /// non-null, indicates where the #including file is, in case a relative search 372 /// is needed. 373 const FileEntry *HeaderSearch::LookupFile( 374 StringRef Filename, 375 bool isAngled, 376 const DirectoryLookup *FromDir, 377 const DirectoryLookup *&CurDir, 378 const FileEntry *CurFileEnt, 379 SmallVectorImpl<char> *SearchPath, 380 SmallVectorImpl<char> *RelativePath, 381 Module **SuggestedModule, 382 bool SkipCache) 383 { 384 if (SuggestedModule) 385 *SuggestedModule = 0; 386 387 // If 'Filename' is absolute, check to see if it exists and no searching. 388 if (llvm::sys::path::is_absolute(Filename)) { 389 CurDir = 0; 390 391 // If this was an #include_next "/absolute/file", fail. 392 if (FromDir) return 0; 393 394 if (SearchPath != NULL) 395 SearchPath->clear(); 396 if (RelativePath != NULL) { 397 RelativePath->clear(); 398 RelativePath->append(Filename.begin(), Filename.end()); 399 } 400 // Otherwise, just return the file. 401 return FileMgr.getFile(Filename, /*openFile=*/true); 402 } 403 404 // Unless disabled, check to see if the file is in the #includer's 405 // directory. This has to be based on CurFileEnt, not CurDir, because 406 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and 407 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h". 408 // This search is not done for <> headers. 409 if (CurFileEnt && !isAngled && !NoCurDirSearch) { 410 llvm::SmallString<1024> TmpDir; 411 // Concatenate the requested file onto the directory. 412 // FIXME: Portability. Filename concatenation should be in sys::Path. 413 TmpDir += CurFileEnt->getDir()->getName(); 414 TmpDir.push_back('/'); 415 TmpDir.append(Filename.begin(), Filename.end()); 416 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) { 417 // Leave CurDir unset. 418 // This file is a system header or C++ unfriendly if the old file is. 419 // 420 // Note that the temporary 'DirInfo' is required here, as either call to 421 // getFileInfo could resize the vector and we don't want to rely on order 422 // of evaluation. 423 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo; 424 getFileInfo(FE).DirInfo = DirInfo; 425 if (SearchPath != NULL) { 426 StringRef SearchPathRef(CurFileEnt->getDir()->getName()); 427 SearchPath->clear(); 428 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end()); 429 } 430 if (RelativePath != NULL) { 431 RelativePath->clear(); 432 RelativePath->append(Filename.begin(), Filename.end()); 433 } 434 return FE; 435 } 436 } 437 438 CurDir = 0; 439 440 // If this is a system #include, ignore the user #include locs. 441 unsigned i = isAngled ? AngledDirIdx : 0; 442 443 // If this is a #include_next request, start searching after the directory the 444 // file was found in. 445 if (FromDir) 446 i = FromDir-&SearchDirs[0]; 447 448 // Cache all of the lookups performed by this method. Many headers are 449 // multiply included, and the "pragma once" optimization prevents them from 450 // being relex/pp'd, but they would still have to search through a 451 // (potentially huge) series of SearchDirs to find it. 452 std::pair<unsigned, unsigned> &CacheLookup = 453 LookupFileCache.GetOrCreateValue(Filename).getValue(); 454 455 // If the entry has been previously looked up, the first value will be 456 // non-zero. If the value is equal to i (the start point of our search), then 457 // this is a matching hit. 458 if (!SkipCache && CacheLookup.first == i+1) { 459 // Skip querying potentially lots of directories for this lookup. 460 i = CacheLookup.second; 461 } else { 462 // Otherwise, this is the first query, or the previous query didn't match 463 // our search start. We will fill in our found location below, so prime the 464 // start point value. 465 CacheLookup.first = i+1; 466 } 467 468 // Check each directory in sequence to see if it contains this file. 469 for (; i != SearchDirs.size(); ++i) { 470 const FileEntry *FE = 471 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath, 472 SuggestedModule); 473 if (!FE) continue; 474 475 CurDir = &SearchDirs[i]; 476 477 // This file is a system header or C++ unfriendly if the dir is. 478 HeaderFileInfo &HFI = getFileInfo(FE); 479 HFI.DirInfo = CurDir->getDirCharacteristic(); 480 481 // If this file is found in a header map and uses the framework style of 482 // includes, then this header is part of a framework we're building. 483 if (CurDir->isIndexHeaderMap()) { 484 size_t SlashPos = Filename.find('/'); 485 if (SlashPos != StringRef::npos) { 486 HFI.IndexHeaderMapHeader = 1; 487 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(), 488 SlashPos)); 489 } 490 } 491 492 // Remember this location for the next lookup we do. 493 CacheLookup.second = i; 494 return FE; 495 } 496 497 // If we are including a file with a quoted include "foo.h" from inside 498 // a header in a framework that is currently being built, and we couldn't 499 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where 500 // "Foo" is the name of the framework in which the including header was found. 501 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) { 502 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt); 503 if (IncludingHFI.IndexHeaderMapHeader) { 504 llvm::SmallString<128> ScratchFilename; 505 ScratchFilename += IncludingHFI.Framework; 506 ScratchFilename += '/'; 507 ScratchFilename += Filename; 508 509 const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true, 510 FromDir, CurDir, CurFileEnt, 511 SearchPath, RelativePath, 512 SuggestedModule); 513 std::pair<unsigned, unsigned> &CacheLookup 514 = LookupFileCache.GetOrCreateValue(Filename).getValue(); 515 CacheLookup.second 516 = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second; 517 return Result; 518 } 519 } 520 521 // Otherwise, didn't find it. Remember we didn't find this. 522 CacheLookup.second = SearchDirs.size(); 523 return 0; 524 } 525 526 /// LookupSubframeworkHeader - Look up a subframework for the specified 527 /// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from 528 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox 529 /// is a subframework within Carbon.framework. If so, return the FileEntry 530 /// for the designated file, otherwise return null. 531 const FileEntry *HeaderSearch:: 532 LookupSubframeworkHeader(StringRef Filename, 533 const FileEntry *ContextFileEnt, 534 SmallVectorImpl<char> *SearchPath, 535 SmallVectorImpl<char> *RelativePath) { 536 assert(ContextFileEnt && "No context file?"); 537 538 // Framework names must have a '/' in the filename. Find it. 539 // FIXME: Should we permit '\' on Windows? 540 size_t SlashPos = Filename.find('/'); 541 if (SlashPos == StringRef::npos) return 0; 542 543 // Look up the base framework name of the ContextFileEnt. 544 const char *ContextName = ContextFileEnt->getName(); 545 546 // If the context info wasn't a framework, couldn't be a subframework. 547 const unsigned DotFrameworkLen = 10; 548 const char *FrameworkPos = strstr(ContextName, ".framework"); 549 if (FrameworkPos == 0 || 550 (FrameworkPos[DotFrameworkLen] != '/' && 551 FrameworkPos[DotFrameworkLen] != '\\')) 552 return 0; 553 554 llvm::SmallString<1024> FrameworkName(ContextName, 555 FrameworkPos+DotFrameworkLen+1); 556 557 // Append Frameworks/HIToolbox.framework/ 558 FrameworkName += "Frameworks/"; 559 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos); 560 FrameworkName += ".framework/"; 561 562 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup = 563 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos)); 564 565 // Some other location? 566 if (CacheLookup.getValue() && 567 CacheLookup.getKeyLength() == FrameworkName.size() && 568 memcmp(CacheLookup.getKeyData(), &FrameworkName[0], 569 CacheLookup.getKeyLength()) != 0) 570 return 0; 571 572 // Cache subframework. 573 if (CacheLookup.getValue() == 0) { 574 ++NumSubFrameworkLookups; 575 576 // If the framework dir doesn't exist, we fail. 577 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str()); 578 if (Dir == 0) return 0; 579 580 // Otherwise, if it does, remember that this is the right direntry for this 581 // framework. 582 CacheLookup.setValue(Dir); 583 } 584 585 const FileEntry *FE = 0; 586 587 if (RelativePath != NULL) { 588 RelativePath->clear(); 589 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end()); 590 } 591 592 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h" 593 llvm::SmallString<1024> HeadersFilename(FrameworkName); 594 HeadersFilename += "Headers/"; 595 if (SearchPath != NULL) { 596 SearchPath->clear(); 597 // Without trailing '/'. 598 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1); 599 } 600 601 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); 602 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) { 603 604 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h" 605 HeadersFilename = FrameworkName; 606 HeadersFilename += "PrivateHeaders/"; 607 if (SearchPath != NULL) { 608 SearchPath->clear(); 609 // Without trailing '/'. 610 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1); 611 } 612 613 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); 614 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) 615 return 0; 616 } 617 618 // This file is a system header or C++ unfriendly if the old file is. 619 // 620 // Note that the temporary 'DirInfo' is required here, as either call to 621 // getFileInfo could resize the vector and we don't want to rely on order 622 // of evaluation. 623 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo; 624 getFileInfo(FE).DirInfo = DirInfo; 625 return FE; 626 } 627 628 /// \brief Helper static function to normalize a path for injection into 629 /// a synthetic header. 630 /*static*/ std::string 631 HeaderSearch::NormalizeDashIncludePath(StringRef File, FileManager &FileMgr) { 632 // Implicit include paths should be resolved relative to the current 633 // working directory first, and then use the regular header search 634 // mechanism. The proper way to handle this is to have the 635 // predefines buffer located at the current working directory, but 636 // it has no file entry. For now, workaround this by using an 637 // absolute path if we find the file here, and otherwise letting 638 // header search handle it. 639 llvm::SmallString<128> Path(File); 640 llvm::sys::fs::make_absolute(Path); 641 bool exists; 642 if (llvm::sys::fs::exists(Path.str(), exists) || !exists) 643 Path = File; 644 else if (exists) 645 FileMgr.getFile(File); 646 647 return Lexer::Stringify(Path.str()); 648 } 649 650 //===----------------------------------------------------------------------===// 651 // File Info Management. 652 //===----------------------------------------------------------------------===// 653 654 /// \brief Merge the header file info provided by \p OtherHFI into the current 655 /// header file info (\p HFI) 656 static void mergeHeaderFileInfo(HeaderFileInfo &HFI, 657 const HeaderFileInfo &OtherHFI) { 658 HFI.isImport |= OtherHFI.isImport; 659 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce; 660 HFI.NumIncludes += OtherHFI.NumIncludes; 661 662 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) { 663 HFI.ControllingMacro = OtherHFI.ControllingMacro; 664 HFI.ControllingMacroID = OtherHFI.ControllingMacroID; 665 } 666 667 if (OtherHFI.External) { 668 HFI.DirInfo = OtherHFI.DirInfo; 669 HFI.External = OtherHFI.External; 670 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader; 671 } 672 673 if (HFI.Framework.empty()) 674 HFI.Framework = OtherHFI.Framework; 675 676 HFI.Resolved = true; 677 } 678 679 /// getFileInfo - Return the HeaderFileInfo structure for the specified 680 /// FileEntry. 681 HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) { 682 if (FE->getUID() >= FileInfo.size()) 683 FileInfo.resize(FE->getUID()+1); 684 685 HeaderFileInfo &HFI = FileInfo[FE->getUID()]; 686 if (ExternalSource && !HFI.Resolved) 687 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE)); 688 return HFI; 689 } 690 691 bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) { 692 // Check if we've ever seen this file as a header. 693 if (File->getUID() >= FileInfo.size()) 694 return false; 695 696 // Resolve header file info from the external source, if needed. 697 HeaderFileInfo &HFI = FileInfo[File->getUID()]; 698 if (ExternalSource && !HFI.Resolved) 699 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File)); 700 701 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID; 702 } 703 704 void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) { 705 if (UID >= FileInfo.size()) 706 FileInfo.resize(UID+1); 707 HFI.Resolved = true; 708 FileInfo[UID] = HFI; 709 } 710 711 /// ShouldEnterIncludeFile - Mark the specified file as a target of of a 712 /// #include, #include_next, or #import directive. Return false if #including 713 /// the file will have no effect or true if we should include it. 714 bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){ 715 ++NumIncluded; // Count # of attempted #includes. 716 717 // Get information about this file. 718 HeaderFileInfo &FileInfo = getFileInfo(File); 719 720 // If this is a #import directive, check that we have not already imported 721 // this header. 722 if (isImport) { 723 // If this has already been imported, don't import it again. 724 FileInfo.isImport = true; 725 726 // Has this already been #import'ed or #include'd? 727 if (FileInfo.NumIncludes) return false; 728 } else { 729 // Otherwise, if this is a #include of a file that was previously #import'd 730 // or if this is the second #include of a #pragma once file, ignore it. 731 if (FileInfo.isImport) 732 return false; 733 } 734 735 // Next, check to see if the file is wrapped with #ifndef guards. If so, and 736 // if the macro that guards it is defined, we know the #include has no effect. 737 if (const IdentifierInfo *ControllingMacro 738 = FileInfo.getControllingMacro(ExternalLookup)) 739 if (ControllingMacro->hasMacroDefinition()) { 740 ++NumMultiIncludeFileOptzn; 741 return false; 742 } 743 744 // Increment the number of times this file has been included. 745 ++FileInfo.NumIncludes; 746 747 return true; 748 } 749 750 size_t HeaderSearch::getTotalMemory() const { 751 return SearchDirs.capacity() 752 + llvm::capacity_in_bytes(FileInfo) 753 + llvm::capacity_in_bytes(HeaderMaps) 754 + LookupFileCache.getAllocator().getTotalMemory() 755 + FrameworkMap.getAllocator().getTotalMemory(); 756 } 757 758 StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) { 759 return FrameworkNames.GetOrCreateValue(Framework).getKey(); 760 } 761 762 bool HeaderSearch::hasModuleMap(StringRef FileName, 763 const DirectoryEntry *Root) { 764 llvm::SmallVector<const DirectoryEntry *, 2> FixUpDirectories; 765 766 StringRef DirName = FileName; 767 do { 768 // Get the parent directory name. 769 DirName = llvm::sys::path::parent_path(DirName); 770 if (DirName.empty()) 771 return false; 772 773 // Determine whether this directory exists. 774 const DirectoryEntry *Dir = FileMgr.getDirectory(DirName); 775 if (!Dir) 776 return false; 777 778 // Try to load the module map file in this directory. 779 switch (loadModuleMapFile(Dir)) { 780 case LMM_NewlyLoaded: 781 case LMM_AlreadyLoaded: 782 // Success. All of the directories we stepped through inherit this module 783 // map file. 784 for (unsigned I = 0, N = FixUpDirectories.size(); I != N; ++I) 785 DirectoryHasModuleMap[FixUpDirectories[I]] = true; 786 787 return true; 788 789 case LMM_NoDirectory: 790 case LMM_InvalidModuleMap: 791 break; 792 } 793 794 // If we hit the top of our search, we're done. 795 if (Dir == Root) 796 return false; 797 798 // Keep track of all of the directories we checked, so we can mark them as 799 // having module maps if we eventually do find a module map. 800 FixUpDirectories.push_back(Dir); 801 } while (true); 802 } 803 804 Module *HeaderSearch::findModuleForHeader(const FileEntry *File) { 805 if (Module *Mod = ModMap.findModuleForHeader(File)) 806 return Mod; 807 808 return 0; 809 } 810 811 bool HeaderSearch::loadModuleMapFile(const FileEntry *File) { 812 const DirectoryEntry *Dir = File->getDir(); 813 814 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir 815 = DirectoryHasModuleMap.find(Dir); 816 if (KnownDir != DirectoryHasModuleMap.end()) 817 return !KnownDir->second; 818 819 bool Result = ModMap.parseModuleMapFile(File); 820 if (!Result && llvm::sys::path::filename(File->getName()) == "module.map") { 821 // If the file we loaded was a module.map, look for the corresponding 822 // module_private.map. 823 llvm::SmallString<128> PrivateFilename(Dir->getName()); 824 llvm::sys::path::append(PrivateFilename, "module_private.map"); 825 if (const FileEntry *PrivateFile = FileMgr.getFile(PrivateFilename)) 826 Result = ModMap.parseModuleMapFile(PrivateFile); 827 } 828 829 DirectoryHasModuleMap[Dir] = !Result; 830 return Result; 831 } 832 833 Module *HeaderSearch::getModule(StringRef Name, bool AllowSearch) { 834 if (Module *Module = ModMap.findModule(Name)) 835 return Module; 836 837 if (!AllowSearch) 838 return 0; 839 840 for (unsigned I = 0, N = SearchDirs.size(); I != N; ++I) { 841 if (!SearchDirs[I].isNormalDir()) 842 continue; 843 844 switch (loadModuleMapFile(SearchDirs[I].getDir())) { 845 case LMM_AlreadyLoaded: 846 case LMM_InvalidModuleMap: 847 case LMM_NoDirectory: 848 break; 849 850 case LMM_NewlyLoaded: 851 if (Module *Module = ModMap.findModule(Name)) 852 return Module; 853 break; 854 } 855 } 856 857 return 0; 858 } 859 860 Module *HeaderSearch::getFrameworkModule(StringRef Name, 861 const DirectoryEntry *Dir) { 862 if (Module *Module = ModMap.findModule(Name)) 863 return Module; 864 865 // Try to load a module map file. 866 switch (loadModuleMapFile(Dir)) { 867 case LMM_InvalidModuleMap: 868 break; 869 870 case LMM_AlreadyLoaded: 871 case LMM_NoDirectory: 872 return 0; 873 874 case LMM_NewlyLoaded: 875 return ModMap.findModule(Name); 876 } 877 878 // The top-level framework directory, from which we'll infer a framework 879 // module. 880 const DirectoryEntry *TopFrameworkDir = Dir; 881 882 // The path from the module we're actually looking for back to the top-level 883 // framework name. 884 llvm::SmallVector<StringRef, 2> SubmodulePath; 885 SubmodulePath.push_back(Name); 886 887 // Walk the directory structure to find any enclosing frameworks. 888 StringRef DirName = Dir->getName(); 889 do { 890 // Get the parent directory name. 891 DirName = llvm::sys::path::parent_path(DirName); 892 if (DirName.empty()) 893 break; 894 895 // Determine whether this directory exists. 896 Dir = FileMgr.getDirectory(DirName); 897 if (!Dir) 898 break; 899 900 // If this is a framework directory, then we're a subframework of this 901 // framework. 902 if (llvm::sys::path::extension(DirName) == ".framework") { 903 SubmodulePath.push_back(llvm::sys::path::stem(DirName)); 904 TopFrameworkDir = Dir; 905 } 906 } while (true); 907 908 // Try to infer a module map from the top-level framework directory. 909 Module *Result = ModMap.inferFrameworkModule(SubmodulePath.back(), 910 TopFrameworkDir, 911 /*Parent=*/0); 912 913 // Follow the submodule path to find the requested (sub)framework module 914 // within the top-level framework module. 915 SubmodulePath.pop_back(); 916 while (!SubmodulePath.empty() && Result) { 917 Result = ModMap.lookupModuleQualified(SubmodulePath.back(), Result); 918 SubmodulePath.pop_back(); 919 } 920 return Result; 921 } 922 923 924 HeaderSearch::LoadModuleMapResult 925 HeaderSearch::loadModuleMapFile(StringRef DirName) { 926 if (const DirectoryEntry *Dir = FileMgr.getDirectory(DirName)) 927 return loadModuleMapFile(Dir); 928 929 return LMM_NoDirectory; 930 } 931 932 HeaderSearch::LoadModuleMapResult 933 HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir) { 934 llvm::DenseMap<const DirectoryEntry *, bool>::iterator KnownDir 935 = DirectoryHasModuleMap.find(Dir); 936 if (KnownDir != DirectoryHasModuleMap.end()) 937 return KnownDir->second? LMM_AlreadyLoaded : LMM_InvalidModuleMap; 938 939 llvm::SmallString<128> ModuleMapFileName; 940 ModuleMapFileName += Dir->getName(); 941 unsigned ModuleMapDirNameLen = ModuleMapFileName.size(); 942 llvm::sys::path::append(ModuleMapFileName, "module.map"); 943 if (const FileEntry *ModuleMapFile = FileMgr.getFile(ModuleMapFileName)) { 944 // We have found a module map file. Try to parse it. 945 if (ModMap.parseModuleMapFile(ModuleMapFile)) { 946 // No suitable module map. 947 DirectoryHasModuleMap[Dir] = false; 948 return LMM_InvalidModuleMap; 949 } 950 951 // This directory has a module map. 952 DirectoryHasModuleMap[Dir] = true; 953 954 // Check whether there is a private module map that we need to load as well. 955 ModuleMapFileName.erase(ModuleMapFileName.begin() + ModuleMapDirNameLen, 956 ModuleMapFileName.end()); 957 llvm::sys::path::append(ModuleMapFileName, "module_private.map"); 958 if (const FileEntry *PrivateModuleMapFile 959 = FileMgr.getFile(ModuleMapFileName)) { 960 if (ModMap.parseModuleMapFile(PrivateModuleMapFile)) { 961 // No suitable module map. 962 DirectoryHasModuleMap[Dir] = false; 963 return LMM_InvalidModuleMap; 964 } 965 } 966 967 return LMM_NewlyLoaded; 968 } 969 970 // No suitable module map. 971 DirectoryHasModuleMap[Dir] = false; 972 return LMM_InvalidModuleMap; 973 } 974 975