1 //===--- ModuleMap.cpp - Describe the layout of modules ---------*- C++ -*-===// 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 defines the ModuleMap implementation, which describes the layout 11 // of a module as it relates to headers. 12 // 13 //===----------------------------------------------------------------------===// 14 #include "clang/Lex/ModuleMap.h" 15 #include "clang/Basic/CharInfo.h" 16 #include "clang/Basic/Diagnostic.h" 17 #include "clang/Basic/DiagnosticOptions.h" 18 #include "clang/Basic/FileManager.h" 19 #include "clang/Basic/TargetInfo.h" 20 #include "clang/Basic/TargetOptions.h" 21 #include "clang/Lex/HeaderSearch.h" 22 #include "clang/Lex/LexDiagnostic.h" 23 #include "clang/Lex/Lexer.h" 24 #include "clang/Lex/LiteralSupport.h" 25 #include "llvm/ADT/StringRef.h" 26 #include "llvm/ADT/StringSwitch.h" 27 #include "llvm/Support/Allocator.h" 28 #include "llvm/Support/FileSystem.h" 29 #include "llvm/Support/Host.h" 30 #include "llvm/Support/PathV2.h" 31 #include "llvm/Support/raw_ostream.h" 32 #include <stdlib.h> 33 #if defined(LLVM_ON_UNIX) 34 #include <limits.h> 35 #endif 36 using namespace clang; 37 38 Module::ExportDecl 39 ModuleMap::resolveExport(Module *Mod, 40 const Module::UnresolvedExportDecl &Unresolved, 41 bool Complain) const { 42 // We may have just a wildcard. 43 if (Unresolved.Id.empty()) { 44 assert(Unresolved.Wildcard && "Invalid unresolved export"); 45 return Module::ExportDecl(0, true); 46 } 47 48 // Resolve the module-id. 49 Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain); 50 if (!Context) 51 return Module::ExportDecl(); 52 53 return Module::ExportDecl(Context, Unresolved.Wildcard); 54 } 55 56 Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod, 57 bool Complain) const { 58 // Find the starting module. 59 Module *Context = lookupModuleUnqualified(Id[0].first, Mod); 60 if (!Context) { 61 if (Complain) 62 Diags->Report(Id[0].second, diag::err_mmap_missing_module_unqualified) 63 << Id[0].first << Mod->getFullModuleName(); 64 65 return 0; 66 } 67 68 // Dig into the module path. 69 for (unsigned I = 1, N = Id.size(); I != N; ++I) { 70 Module *Sub = lookupModuleQualified(Id[I].first, Context); 71 if (!Sub) { 72 if (Complain) 73 Diags->Report(Id[I].second, diag::err_mmap_missing_module_qualified) 74 << Id[I].first << Context->getFullModuleName() 75 << SourceRange(Id[0].second, Id[I-1].second); 76 77 return 0; 78 } 79 80 Context = Sub; 81 } 82 83 return Context; 84 } 85 86 ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC, 87 const LangOptions &LangOpts, const TargetInfo *Target, 88 HeaderSearch &HeaderInfo) 89 : LangOpts(LangOpts), Target(Target), HeaderInfo(HeaderInfo), 90 BuiltinIncludeDir(0) 91 { 92 IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs); 93 Diags = IntrusiveRefCntPtr<DiagnosticsEngine>( 94 new DiagnosticsEngine(DiagIDs, new DiagnosticOptions)); 95 Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true); 96 SourceMgr = new SourceManager(*Diags, FileMgr); 97 } 98 99 ModuleMap::~ModuleMap() { 100 for (llvm::StringMap<Module *>::iterator I = Modules.begin(), 101 IEnd = Modules.end(); 102 I != IEnd; ++I) { 103 delete I->getValue(); 104 } 105 106 delete SourceMgr; 107 } 108 109 void ModuleMap::setTarget(const TargetInfo &Target) { 110 assert((!this->Target || this->Target == &Target) && 111 "Improper target override"); 112 this->Target = &Target; 113 } 114 115 /// \brief "Sanitize" a filename so that it can be used as an identifier. 116 static StringRef sanitizeFilenameAsIdentifier(StringRef Name, 117 SmallVectorImpl<char> &Buffer) { 118 if (Name.empty()) 119 return Name; 120 121 if (!isValidIdentifier(Name)) { 122 // If we don't already have something with the form of an identifier, 123 // create a buffer with the sanitized name. 124 Buffer.clear(); 125 if (isDigit(Name[0])) 126 Buffer.push_back('_'); 127 Buffer.reserve(Buffer.size() + Name.size()); 128 for (unsigned I = 0, N = Name.size(); I != N; ++I) { 129 if (isIdentifierBody(Name[I])) 130 Buffer.push_back(Name[I]); 131 else 132 Buffer.push_back('_'); 133 } 134 135 Name = StringRef(Buffer.data(), Buffer.size()); 136 } 137 138 while (llvm::StringSwitch<bool>(Name) 139 #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true) 140 #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true) 141 #include "clang/Basic/TokenKinds.def" 142 .Default(false)) { 143 if (Name.data() != Buffer.data()) 144 Buffer.append(Name.begin(), Name.end()); 145 Buffer.push_back('_'); 146 Name = StringRef(Buffer.data(), Buffer.size()); 147 } 148 149 return Name; 150 } 151 152 /// \brief Determine whether the given file name is the name of a builtin 153 /// header, supplied by Clang to replace, override, or augment existing system 154 /// headers. 155 static bool isBuiltinHeader(StringRef FileName) { 156 return llvm::StringSwitch<bool>(FileName) 157 .Case("float.h", true) 158 .Case("iso646.h", true) 159 .Case("limits.h", true) 160 .Case("stdalign.h", true) 161 .Case("stdarg.h", true) 162 .Case("stdbool.h", true) 163 .Case("stddef.h", true) 164 .Case("stdint.h", true) 165 .Case("tgmath.h", true) 166 .Case("unwind.h", true) 167 .Default(false); 168 } 169 170 Module *ModuleMap::findModuleForHeader(const FileEntry *File) { 171 HeadersMap::iterator Known = Headers.find(File); 172 if (Known != Headers.end()) { 173 // If a header is not available, don't report that it maps to anything. 174 if (!Known->second.isAvailable()) 175 return 0; 176 177 return Known->second.getModule(); 178 } 179 180 // If we've found a builtin header within Clang's builtin include directory, 181 // load all of the module maps to see if it will get associated with a 182 // specific module (e.g., in /usr/include). 183 if (File->getDir() == BuiltinIncludeDir && 184 isBuiltinHeader(llvm::sys::path::filename(File->getName()))) { 185 SmallVector<Module *, 4> AllModules; 186 HeaderInfo.collectAllModules(AllModules); 187 188 // Check again. 189 Known = Headers.find(File); 190 if (Known != Headers.end()) { 191 // If a header is not available, don't report that it maps to anything. 192 if (!Known->second.isAvailable()) 193 return 0; 194 195 return Known->second.getModule(); 196 } 197 } 198 199 const DirectoryEntry *Dir = File->getDir(); 200 SmallVector<const DirectoryEntry *, 2> SkippedDirs; 201 202 // Note: as an egregious but useful hack we use the real path here, because 203 // frameworks moving from top-level frameworks to embedded frameworks tend 204 // to be symlinked from the top-level location to the embedded location, 205 // and we need to resolve lookups as if we had found the embedded location. 206 StringRef DirName = SourceMgr->getFileManager().getCanonicalName(Dir); 207 208 // Keep walking up the directory hierarchy, looking for a directory with 209 // an umbrella header. 210 do { 211 llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir 212 = UmbrellaDirs.find(Dir); 213 if (KnownDir != UmbrellaDirs.end()) { 214 Module *Result = KnownDir->second; 215 216 // Search up the module stack until we find a module with an umbrella 217 // directory. 218 Module *UmbrellaModule = Result; 219 while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 220 UmbrellaModule = UmbrellaModule->Parent; 221 222 if (UmbrellaModule->InferSubmodules) { 223 // Infer submodules for each of the directories we found between 224 // the directory of the umbrella header and the directory where 225 // the actual header is located. 226 bool Explicit = UmbrellaModule->InferExplicitSubmodules; 227 228 for (unsigned I = SkippedDirs.size(); I != 0; --I) { 229 // Find or create the module that corresponds to this directory name. 230 SmallString<32> NameBuf; 231 StringRef Name = sanitizeFilenameAsIdentifier( 232 llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 233 NameBuf); 234 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 235 Explicit).first; 236 237 // Associate the module and the directory. 238 UmbrellaDirs[SkippedDirs[I-1]] = Result; 239 240 // If inferred submodules export everything they import, add a 241 // wildcard to the set of exports. 242 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 243 Result->Exports.push_back(Module::ExportDecl(0, true)); 244 } 245 246 // Infer a submodule with the same name as this header file. 247 SmallString<32> NameBuf; 248 StringRef Name = sanitizeFilenameAsIdentifier( 249 llvm::sys::path::stem(File->getName()), NameBuf); 250 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 251 Explicit).first; 252 Result->addTopHeader(File); 253 254 // If inferred submodules export everything they import, add a 255 // wildcard to the set of exports. 256 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 257 Result->Exports.push_back(Module::ExportDecl(0, true)); 258 } else { 259 // Record each of the directories we stepped through as being part of 260 // the module we found, since the umbrella header covers them all. 261 for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 262 UmbrellaDirs[SkippedDirs[I]] = Result; 263 } 264 265 Headers[File] = KnownHeader(Result, /*Excluded=*/false); 266 267 // If a header corresponds to an unavailable module, don't report 268 // that it maps to anything. 269 if (!Result->isAvailable()) 270 return 0; 271 272 return Result; 273 } 274 275 SkippedDirs.push_back(Dir); 276 277 // Retrieve our parent path. 278 DirName = llvm::sys::path::parent_path(DirName); 279 if (DirName.empty()) 280 break; 281 282 // Resolve the parent path to a directory entry. 283 Dir = SourceMgr->getFileManager().getDirectory(DirName); 284 } while (Dir); 285 286 return 0; 287 } 288 289 bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const { 290 HeadersMap::const_iterator Known = Headers.find(Header); 291 if (Known != Headers.end()) 292 return !Known->second.isAvailable(); 293 294 const DirectoryEntry *Dir = Header->getDir(); 295 SmallVector<const DirectoryEntry *, 2> SkippedDirs; 296 StringRef DirName = Dir->getName(); 297 298 // Keep walking up the directory hierarchy, looking for a directory with 299 // an umbrella header. 300 do { 301 llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir 302 = UmbrellaDirs.find(Dir); 303 if (KnownDir != UmbrellaDirs.end()) { 304 Module *Found = KnownDir->second; 305 if (!Found->isAvailable()) 306 return true; 307 308 // Search up the module stack until we find a module with an umbrella 309 // directory. 310 Module *UmbrellaModule = Found; 311 while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 312 UmbrellaModule = UmbrellaModule->Parent; 313 314 if (UmbrellaModule->InferSubmodules) { 315 for (unsigned I = SkippedDirs.size(); I != 0; --I) { 316 // Find or create the module that corresponds to this directory name. 317 SmallString<32> NameBuf; 318 StringRef Name = sanitizeFilenameAsIdentifier( 319 llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 320 NameBuf); 321 Found = lookupModuleQualified(Name, Found); 322 if (!Found) 323 return false; 324 if (!Found->isAvailable()) 325 return true; 326 } 327 328 // Infer a submodule with the same name as this header file. 329 SmallString<32> NameBuf; 330 StringRef Name = sanitizeFilenameAsIdentifier( 331 llvm::sys::path::stem(Header->getName()), 332 NameBuf); 333 Found = lookupModuleQualified(Name, Found); 334 if (!Found) 335 return false; 336 } 337 338 return !Found->isAvailable(); 339 } 340 341 SkippedDirs.push_back(Dir); 342 343 // Retrieve our parent path. 344 DirName = llvm::sys::path::parent_path(DirName); 345 if (DirName.empty()) 346 break; 347 348 // Resolve the parent path to a directory entry. 349 Dir = SourceMgr->getFileManager().getDirectory(DirName); 350 } while (Dir); 351 352 return false; 353 } 354 355 Module *ModuleMap::findModule(StringRef Name) const { 356 llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name); 357 if (Known != Modules.end()) 358 return Known->getValue(); 359 360 return 0; 361 } 362 363 Module *ModuleMap::lookupModuleUnqualified(StringRef Name, 364 Module *Context) const { 365 for(; Context; Context = Context->Parent) { 366 if (Module *Sub = lookupModuleQualified(Name, Context)) 367 return Sub; 368 } 369 370 return findModule(Name); 371 } 372 373 Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{ 374 if (!Context) 375 return findModule(Name); 376 377 return Context->findSubmodule(Name); 378 } 379 380 std::pair<Module *, bool> 381 ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 382 bool IsExplicit) { 383 // Try to find an existing module with this name. 384 if (Module *Sub = lookupModuleQualified(Name, Parent)) 385 return std::make_pair(Sub, false); 386 387 // Create a new module with this name. 388 Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework, 389 IsExplicit); 390 if (!Parent) 391 Modules[Name] = Result; 392 return std::make_pair(Result, true); 393 } 394 395 bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir, 396 StringRef Name, bool &IsSystem) const { 397 // Check whether we have already looked into the parent directory 398 // for a module map. 399 llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 400 inferred = InferredDirectories.find(ParentDir); 401 if (inferred == InferredDirectories.end()) 402 return false; 403 404 if (!inferred->second.InferModules) 405 return false; 406 407 // We're allowed to infer for this directory, but make sure it's okay 408 // to infer this particular module. 409 bool canInfer = std::find(inferred->second.ExcludedModules.begin(), 410 inferred->second.ExcludedModules.end(), 411 Name) == inferred->second.ExcludedModules.end(); 412 413 if (canInfer && inferred->second.InferSystemModules) 414 IsSystem = true; 415 416 return canInfer; 417 } 418 419 /// \brief For a framework module, infer the framework against which we 420 /// should link. 421 static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir, 422 FileManager &FileMgr) { 423 assert(Mod->IsFramework && "Can only infer linking for framework modules"); 424 assert(!Mod->isSubFramework() && 425 "Can only infer linking for top-level frameworks"); 426 427 SmallString<128> LibName; 428 LibName += FrameworkDir->getName(); 429 llvm::sys::path::append(LibName, Mod->Name); 430 if (FileMgr.getFile(LibName)) { 431 Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name, 432 /*IsFramework=*/true)); 433 } 434 } 435 436 Module * 437 ModuleMap::inferFrameworkModule(StringRef ModuleName, 438 const DirectoryEntry *FrameworkDir, 439 bool IsSystem, 440 Module *Parent) { 441 // Check whether we've already found this module. 442 if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 443 return Mod; 444 445 FileManager &FileMgr = SourceMgr->getFileManager(); 446 447 // If the framework has a parent path from which we're allowed to infer 448 // a framework module, do so. 449 if (!Parent) { 450 // Determine whether we're allowed to infer a module map. 451 452 // Note: as an egregious but useful hack we use the real path here, because 453 // we might be looking at an embedded framework that symlinks out to a 454 // top-level framework, and we need to infer as if we were naming the 455 // top-level framework. 456 StringRef FrameworkDirName 457 = SourceMgr->getFileManager().getCanonicalName(FrameworkDir); 458 459 bool canInfer = false; 460 if (llvm::sys::path::has_parent_path(FrameworkDirName)) { 461 // Figure out the parent path. 462 StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); 463 if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) { 464 // Check whether we have already looked into the parent directory 465 // for a module map. 466 llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 467 inferred = InferredDirectories.find(ParentDir); 468 if (inferred == InferredDirectories.end()) { 469 // We haven't looked here before. Load a module map, if there is 470 // one. 471 SmallString<128> ModMapPath = Parent; 472 llvm::sys::path::append(ModMapPath, "module.map"); 473 if (const FileEntry *ModMapFile = FileMgr.getFile(ModMapPath)) { 474 parseModuleMapFile(ModMapFile); 475 inferred = InferredDirectories.find(ParentDir); 476 } 477 478 if (inferred == InferredDirectories.end()) 479 inferred = InferredDirectories.insert( 480 std::make_pair(ParentDir, InferredDirectory())).first; 481 } 482 483 if (inferred->second.InferModules) { 484 // We're allowed to infer for this directory, but make sure it's okay 485 // to infer this particular module. 486 StringRef Name = llvm::sys::path::stem(FrameworkDirName); 487 canInfer = std::find(inferred->second.ExcludedModules.begin(), 488 inferred->second.ExcludedModules.end(), 489 Name) == inferred->second.ExcludedModules.end(); 490 491 if (inferred->second.InferSystemModules) 492 IsSystem = true; 493 } 494 } 495 } 496 497 // If we're not allowed to infer a framework module, don't. 498 if (!canInfer) 499 return 0; 500 } 501 502 503 // Look for an umbrella header. 504 SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 505 llvm::sys::path::append(UmbrellaName, "Headers"); 506 llvm::sys::path::append(UmbrellaName, ModuleName + ".h"); 507 const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 508 509 // FIXME: If there's no umbrella header, we could probably scan the 510 // framework to load *everything*. But, it's not clear that this is a good 511 // idea. 512 if (!UmbrellaHeader) 513 return 0; 514 515 Module *Result = new Module(ModuleName, SourceLocation(), Parent, 516 /*IsFramework=*/true, /*IsExplicit=*/false); 517 if (IsSystem) 518 Result->IsSystem = IsSystem; 519 520 if (!Parent) 521 Modules[ModuleName] = Result; 522 523 // umbrella header "umbrella-header-name" 524 Result->Umbrella = UmbrellaHeader; 525 Headers[UmbrellaHeader] = KnownHeader(Result, /*Excluded=*/false); 526 UmbrellaDirs[UmbrellaHeader->getDir()] = Result; 527 528 // export * 529 Result->Exports.push_back(Module::ExportDecl(0, true)); 530 531 // module * { export * } 532 Result->InferSubmodules = true; 533 Result->InferExportWildcard = true; 534 535 // Look for subframeworks. 536 llvm::error_code EC; 537 SmallString<128> SubframeworksDirName 538 = StringRef(FrameworkDir->getName()); 539 llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 540 SmallString<128> SubframeworksDirNameNative; 541 llvm::sys::path::native(SubframeworksDirName.str(), 542 SubframeworksDirNameNative); 543 for (llvm::sys::fs::directory_iterator 544 Dir(SubframeworksDirNameNative.str(), EC), DirEnd; 545 Dir != DirEnd && !EC; Dir.increment(EC)) { 546 if (!StringRef(Dir->path()).endswith(".framework")) 547 continue; 548 549 if (const DirectoryEntry *SubframeworkDir 550 = FileMgr.getDirectory(Dir->path())) { 551 // Note: as an egregious but useful hack, we use the real path here and 552 // check whether it is actually a subdirectory of the parent directory. 553 // This will not be the case if the 'subframework' is actually a symlink 554 // out to a top-level framework. 555 StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir); 556 bool FoundParent = false; 557 do { 558 // Get the parent directory name. 559 SubframeworkDirName 560 = llvm::sys::path::parent_path(SubframeworkDirName); 561 if (SubframeworkDirName.empty()) 562 break; 563 564 if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { 565 FoundParent = true; 566 break; 567 } 568 } while (true); 569 570 if (!FoundParent) 571 continue; 572 573 // FIXME: Do we want to warn about subframeworks without umbrella headers? 574 SmallString<32> NameBuf; 575 inferFrameworkModule(sanitizeFilenameAsIdentifier( 576 llvm::sys::path::stem(Dir->path()), NameBuf), 577 SubframeworkDir, IsSystem, Result); 578 } 579 } 580 581 // If the module is a top-level framework, automatically link against the 582 // framework. 583 if (!Result->isSubFramework()) { 584 inferFrameworkLink(Result, FrameworkDir, FileMgr); 585 } 586 587 return Result; 588 } 589 590 void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ 591 Headers[UmbrellaHeader] = KnownHeader(Mod, /*Excluded=*/false); 592 Mod->Umbrella = UmbrellaHeader; 593 UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; 594 } 595 596 void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) { 597 Mod->Umbrella = UmbrellaDir; 598 UmbrellaDirs[UmbrellaDir] = Mod; 599 } 600 601 void ModuleMap::addHeader(Module *Mod, const FileEntry *Header, 602 bool Excluded) { 603 if (Excluded) { 604 Mod->ExcludedHeaders.push_back(Header); 605 } else { 606 Mod->Headers.push_back(Header); 607 HeaderInfo.MarkFileModuleHeader(Header); 608 } 609 Headers[Header] = KnownHeader(Mod, Excluded); 610 } 611 612 const FileEntry * 613 ModuleMap::getContainingModuleMapFile(Module *Module) const { 614 if (Module->DefinitionLoc.isInvalid() || !SourceMgr) 615 return 0; 616 617 return SourceMgr->getFileEntryForID( 618 SourceMgr->getFileID(Module->DefinitionLoc)); 619 } 620 621 void ModuleMap::dump() { 622 llvm::errs() << "Modules:"; 623 for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 624 MEnd = Modules.end(); 625 M != MEnd; ++M) 626 M->getValue()->print(llvm::errs(), 2); 627 628 llvm::errs() << "Headers:"; 629 for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 630 H != HEnd; ++H) { 631 llvm::errs() << " \"" << H->first->getName() << "\" -> " 632 << H->second.getModule()->getFullModuleName() << "\n"; 633 } 634 } 635 636 bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 637 bool HadError = false; 638 for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { 639 Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 640 Complain); 641 if (Export.getPointer() || Export.getInt()) 642 Mod->Exports.push_back(Export); 643 else 644 HadError = true; 645 } 646 Mod->UnresolvedExports.clear(); 647 return HadError; 648 } 649 650 bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) { 651 bool HadError = false; 652 for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) { 653 Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id, 654 Mod, Complain); 655 if (!OtherMod) { 656 HadError = true; 657 continue; 658 } 659 660 Module::Conflict Conflict; 661 Conflict.Other = OtherMod; 662 Conflict.Message = Mod->UnresolvedConflicts[I].Message; 663 Mod->Conflicts.push_back(Conflict); 664 } 665 Mod->UnresolvedConflicts.clear(); 666 return HadError; 667 } 668 669 Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 670 if (Loc.isInvalid()) 671 return 0; 672 673 // Use the expansion location to determine which module we're in. 674 FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 675 if (!ExpansionLoc.isFileID()) 676 return 0; 677 678 679 const SourceManager &SrcMgr = Loc.getManager(); 680 FileID ExpansionFileID = ExpansionLoc.getFileID(); 681 682 while (const FileEntry *ExpansionFile 683 = SrcMgr.getFileEntryForID(ExpansionFileID)) { 684 // Find the module that owns this header (if any). 685 if (Module *Mod = findModuleForHeader(ExpansionFile)) 686 return Mod; 687 688 // No module owns this header, so look up the inclusion chain to see if 689 // any included header has an associated module. 690 SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); 691 if (IncludeLoc.isInvalid()) 692 return 0; 693 694 ExpansionFileID = SrcMgr.getFileID(IncludeLoc); 695 } 696 697 return 0; 698 } 699 700 //----------------------------------------------------------------------------// 701 // Module map file parser 702 //----------------------------------------------------------------------------// 703 704 namespace clang { 705 /// \brief A token in a module map file. 706 struct MMToken { 707 enum TokenKind { 708 Comma, 709 ConfigMacros, 710 Conflict, 711 EndOfFile, 712 HeaderKeyword, 713 Identifier, 714 ExcludeKeyword, 715 ExplicitKeyword, 716 ExportKeyword, 717 FrameworkKeyword, 718 LinkKeyword, 719 ModuleKeyword, 720 Period, 721 UmbrellaKeyword, 722 RequiresKeyword, 723 Star, 724 StringLiteral, 725 LBrace, 726 RBrace, 727 LSquare, 728 RSquare 729 } Kind; 730 731 unsigned Location; 732 unsigned StringLength; 733 const char *StringData; 734 735 void clear() { 736 Kind = EndOfFile; 737 Location = 0; 738 StringLength = 0; 739 StringData = 0; 740 } 741 742 bool is(TokenKind K) const { return Kind == K; } 743 744 SourceLocation getLocation() const { 745 return SourceLocation::getFromRawEncoding(Location); 746 } 747 748 StringRef getString() const { 749 return StringRef(StringData, StringLength); 750 } 751 }; 752 753 /// \brief The set of attributes that can be attached to a module. 754 struct Attributes { 755 Attributes() : IsSystem(), IsExhaustive() { } 756 757 /// \brief Whether this is a system module. 758 unsigned IsSystem : 1; 759 760 /// \brief Whether this is an exhaustive set of configuration macros. 761 unsigned IsExhaustive : 1; 762 }; 763 764 765 class ModuleMapParser { 766 Lexer &L; 767 SourceManager &SourceMgr; 768 769 /// \brief Default target information, used only for string literal 770 /// parsing. 771 const TargetInfo *Target; 772 773 DiagnosticsEngine &Diags; 774 ModuleMap ⤅ 775 776 /// \brief The directory that this module map resides in. 777 const DirectoryEntry *Directory; 778 779 /// \brief The directory containing Clang-supplied headers. 780 const DirectoryEntry *BuiltinIncludeDir; 781 782 /// \brief Whether an error occurred. 783 bool HadError; 784 785 /// \brief Stores string data for the various string literals referenced 786 /// during parsing. 787 llvm::BumpPtrAllocator StringData; 788 789 /// \brief The current token. 790 MMToken Tok; 791 792 /// \brief The active module. 793 Module *ActiveModule; 794 795 /// \brief Consume the current token and return its location. 796 SourceLocation consumeToken(); 797 798 /// \brief Skip tokens until we reach the a token with the given kind 799 /// (or the end of the file). 800 void skipUntil(MMToken::TokenKind K); 801 802 typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; 803 bool parseModuleId(ModuleId &Id); 804 void parseModuleDecl(); 805 void parseRequiresDecl(); 806 void parseHeaderDecl(SourceLocation UmbrellaLoc, SourceLocation ExcludeLoc); 807 void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 808 void parseExportDecl(); 809 void parseLinkDecl(); 810 void parseConfigMacros(); 811 void parseConflict(); 812 void parseInferredModuleDecl(bool Framework, bool Explicit); 813 bool parseOptionalAttributes(Attributes &Attrs); 814 815 const DirectoryEntry *getOverriddenHeaderSearchDir(); 816 817 public: 818 explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 819 const TargetInfo *Target, 820 DiagnosticsEngine &Diags, 821 ModuleMap &Map, 822 const DirectoryEntry *Directory, 823 const DirectoryEntry *BuiltinIncludeDir) 824 : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 825 Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir), 826 HadError(false), ActiveModule(0) 827 { 828 Tok.clear(); 829 consumeToken(); 830 } 831 832 bool parseModuleMapFile(); 833 }; 834 } 835 836 SourceLocation ModuleMapParser::consumeToken() { 837 retry: 838 SourceLocation Result = Tok.getLocation(); 839 Tok.clear(); 840 841 Token LToken; 842 L.LexFromRawLexer(LToken); 843 Tok.Location = LToken.getLocation().getRawEncoding(); 844 switch (LToken.getKind()) { 845 case tok::raw_identifier: 846 Tok.StringData = LToken.getRawIdentifierData(); 847 Tok.StringLength = LToken.getLength(); 848 Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString()) 849 .Case("config_macros", MMToken::ConfigMacros) 850 .Case("conflict", MMToken::Conflict) 851 .Case("exclude", MMToken::ExcludeKeyword) 852 .Case("explicit", MMToken::ExplicitKeyword) 853 .Case("export", MMToken::ExportKeyword) 854 .Case("framework", MMToken::FrameworkKeyword) 855 .Case("header", MMToken::HeaderKeyword) 856 .Case("link", MMToken::LinkKeyword) 857 .Case("module", MMToken::ModuleKeyword) 858 .Case("requires", MMToken::RequiresKeyword) 859 .Case("umbrella", MMToken::UmbrellaKeyword) 860 .Default(MMToken::Identifier); 861 break; 862 863 case tok::comma: 864 Tok.Kind = MMToken::Comma; 865 break; 866 867 case tok::eof: 868 Tok.Kind = MMToken::EndOfFile; 869 break; 870 871 case tok::l_brace: 872 Tok.Kind = MMToken::LBrace; 873 break; 874 875 case tok::l_square: 876 Tok.Kind = MMToken::LSquare; 877 break; 878 879 case tok::period: 880 Tok.Kind = MMToken::Period; 881 break; 882 883 case tok::r_brace: 884 Tok.Kind = MMToken::RBrace; 885 break; 886 887 case tok::r_square: 888 Tok.Kind = MMToken::RSquare; 889 break; 890 891 case tok::star: 892 Tok.Kind = MMToken::Star; 893 break; 894 895 case tok::string_literal: { 896 if (LToken.hasUDSuffix()) { 897 Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 898 HadError = true; 899 goto retry; 900 } 901 902 // Parse the string literal. 903 LangOptions LangOpts; 904 StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target); 905 if (StringLiteral.hadError) 906 goto retry; 907 908 // Copy the string literal into our string data allocator. 909 unsigned Length = StringLiteral.GetStringLength(); 910 char *Saved = StringData.Allocate<char>(Length + 1); 911 memcpy(Saved, StringLiteral.GetString().data(), Length); 912 Saved[Length] = 0; 913 914 // Form the token. 915 Tok.Kind = MMToken::StringLiteral; 916 Tok.StringData = Saved; 917 Tok.StringLength = Length; 918 break; 919 } 920 921 case tok::comment: 922 goto retry; 923 924 default: 925 Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 926 HadError = true; 927 goto retry; 928 } 929 930 return Result; 931 } 932 933 void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 934 unsigned braceDepth = 0; 935 unsigned squareDepth = 0; 936 do { 937 switch (Tok.Kind) { 938 case MMToken::EndOfFile: 939 return; 940 941 case MMToken::LBrace: 942 if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 943 return; 944 945 ++braceDepth; 946 break; 947 948 case MMToken::LSquare: 949 if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 950 return; 951 952 ++squareDepth; 953 break; 954 955 case MMToken::RBrace: 956 if (braceDepth > 0) 957 --braceDepth; 958 else if (Tok.is(K)) 959 return; 960 break; 961 962 case MMToken::RSquare: 963 if (squareDepth > 0) 964 --squareDepth; 965 else if (Tok.is(K)) 966 return; 967 break; 968 969 default: 970 if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 971 return; 972 break; 973 } 974 975 consumeToken(); 976 } while (true); 977 } 978 979 /// \brief Parse a module-id. 980 /// 981 /// module-id: 982 /// identifier 983 /// identifier '.' module-id 984 /// 985 /// \returns true if an error occurred, false otherwise. 986 bool ModuleMapParser::parseModuleId(ModuleId &Id) { 987 Id.clear(); 988 do { 989 if (Tok.is(MMToken::Identifier)) { 990 Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 991 consumeToken(); 992 } else { 993 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 994 return true; 995 } 996 997 if (!Tok.is(MMToken::Period)) 998 break; 999 1000 consumeToken(); 1001 } while (true); 1002 1003 return false; 1004 } 1005 1006 namespace { 1007 /// \brief Enumerates the known attributes. 1008 enum AttributeKind { 1009 /// \brief An unknown attribute. 1010 AT_unknown, 1011 /// \brief The 'system' attribute. 1012 AT_system, 1013 /// \brief The 'exhaustive' attribute. 1014 AT_exhaustive 1015 }; 1016 } 1017 1018 /// \brief Parse a module declaration. 1019 /// 1020 /// module-declaration: 1021 /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 1022 /// { module-member* } 1023 /// 1024 /// module-member: 1025 /// requires-declaration 1026 /// header-declaration 1027 /// submodule-declaration 1028 /// export-declaration 1029 /// link-declaration 1030 /// 1031 /// submodule-declaration: 1032 /// module-declaration 1033 /// inferred-submodule-declaration 1034 void ModuleMapParser::parseModuleDecl() { 1035 assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 1036 Tok.is(MMToken::FrameworkKeyword)); 1037 // Parse 'explicit' or 'framework' keyword, if present. 1038 SourceLocation ExplicitLoc; 1039 bool Explicit = false; 1040 bool Framework = false; 1041 1042 // Parse 'explicit' keyword, if present. 1043 if (Tok.is(MMToken::ExplicitKeyword)) { 1044 ExplicitLoc = consumeToken(); 1045 Explicit = true; 1046 } 1047 1048 // Parse 'framework' keyword, if present. 1049 if (Tok.is(MMToken::FrameworkKeyword)) { 1050 consumeToken(); 1051 Framework = true; 1052 } 1053 1054 // Parse 'module' keyword. 1055 if (!Tok.is(MMToken::ModuleKeyword)) { 1056 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1057 consumeToken(); 1058 HadError = true; 1059 return; 1060 } 1061 consumeToken(); // 'module' keyword 1062 1063 // If we have a wildcard for the module name, this is an inferred submodule. 1064 // Parse it. 1065 if (Tok.is(MMToken::Star)) 1066 return parseInferredModuleDecl(Framework, Explicit); 1067 1068 // Parse the module name. 1069 ModuleId Id; 1070 if (parseModuleId(Id)) { 1071 HadError = true; 1072 return; 1073 } 1074 1075 if (ActiveModule) { 1076 if (Id.size() > 1) { 1077 Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 1078 << SourceRange(Id.front().second, Id.back().second); 1079 1080 HadError = true; 1081 return; 1082 } 1083 } else if (Id.size() == 1 && Explicit) { 1084 // Top-level modules can't be explicit. 1085 Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 1086 Explicit = false; 1087 ExplicitLoc = SourceLocation(); 1088 HadError = true; 1089 } 1090 1091 Module *PreviousActiveModule = ActiveModule; 1092 if (Id.size() > 1) { 1093 // This module map defines a submodule. Go find the module of which it 1094 // is a submodule. 1095 ActiveModule = 0; 1096 for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 1097 if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 1098 ActiveModule = Next; 1099 continue; 1100 } 1101 1102 if (ActiveModule) { 1103 Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 1104 << Id[I].first << ActiveModule->getTopLevelModule(); 1105 } else { 1106 Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 1107 } 1108 HadError = true; 1109 return; 1110 } 1111 } 1112 1113 StringRef ModuleName = Id.back().first; 1114 SourceLocation ModuleNameLoc = Id.back().second; 1115 1116 // Parse the optional attribute list. 1117 Attributes Attrs; 1118 parseOptionalAttributes(Attrs); 1119 1120 // Parse the opening brace. 1121 if (!Tok.is(MMToken::LBrace)) { 1122 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 1123 << ModuleName; 1124 HadError = true; 1125 return; 1126 } 1127 SourceLocation LBraceLoc = consumeToken(); 1128 1129 // Determine whether this (sub)module has already been defined. 1130 if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 1131 if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { 1132 // Skip the module definition. 1133 skipUntil(MMToken::RBrace); 1134 if (Tok.is(MMToken::RBrace)) 1135 consumeToken(); 1136 else { 1137 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1138 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1139 HadError = true; 1140 } 1141 return; 1142 } 1143 1144 Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 1145 << ModuleName; 1146 Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 1147 1148 // Skip the module definition. 1149 skipUntil(MMToken::RBrace); 1150 if (Tok.is(MMToken::RBrace)) 1151 consumeToken(); 1152 1153 HadError = true; 1154 return; 1155 } 1156 1157 // Start defining this module. 1158 ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, 1159 Explicit).first; 1160 ActiveModule->DefinitionLoc = ModuleNameLoc; 1161 if (Attrs.IsSystem) 1162 ActiveModule->IsSystem = true; 1163 1164 bool Done = false; 1165 do { 1166 switch (Tok.Kind) { 1167 case MMToken::EndOfFile: 1168 case MMToken::RBrace: 1169 Done = true; 1170 break; 1171 1172 case MMToken::ConfigMacros: 1173 parseConfigMacros(); 1174 break; 1175 1176 case MMToken::Conflict: 1177 parseConflict(); 1178 break; 1179 1180 case MMToken::ExplicitKeyword: 1181 case MMToken::FrameworkKeyword: 1182 case MMToken::ModuleKeyword: 1183 parseModuleDecl(); 1184 break; 1185 1186 case MMToken::ExportKeyword: 1187 parseExportDecl(); 1188 break; 1189 1190 case MMToken::RequiresKeyword: 1191 parseRequiresDecl(); 1192 break; 1193 1194 case MMToken::UmbrellaKeyword: { 1195 SourceLocation UmbrellaLoc = consumeToken(); 1196 if (Tok.is(MMToken::HeaderKeyword)) 1197 parseHeaderDecl(UmbrellaLoc, SourceLocation()); 1198 else 1199 parseUmbrellaDirDecl(UmbrellaLoc); 1200 break; 1201 } 1202 1203 case MMToken::ExcludeKeyword: { 1204 SourceLocation ExcludeLoc = consumeToken(); 1205 if (Tok.is(MMToken::HeaderKeyword)) { 1206 parseHeaderDecl(SourceLocation(), ExcludeLoc); 1207 } else { 1208 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1209 << "exclude"; 1210 } 1211 break; 1212 } 1213 1214 case MMToken::HeaderKeyword: 1215 parseHeaderDecl(SourceLocation(), SourceLocation()); 1216 break; 1217 1218 case MMToken::LinkKeyword: 1219 parseLinkDecl(); 1220 break; 1221 1222 default: 1223 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 1224 consumeToken(); 1225 break; 1226 } 1227 } while (!Done); 1228 1229 if (Tok.is(MMToken::RBrace)) 1230 consumeToken(); 1231 else { 1232 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1233 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1234 HadError = true; 1235 } 1236 1237 // If the active module is a top-level framework, and there are no link 1238 // libraries, automatically link against the framework. 1239 if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() && 1240 ActiveModule->LinkLibraries.empty()) { 1241 inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager()); 1242 } 1243 1244 // We're done parsing this module. Pop back to the previous module. 1245 ActiveModule = PreviousActiveModule; 1246 } 1247 1248 /// \brief Parse a requires declaration. 1249 /// 1250 /// requires-declaration: 1251 /// 'requires' feature-list 1252 /// 1253 /// feature-list: 1254 /// identifier ',' feature-list 1255 /// identifier 1256 void ModuleMapParser::parseRequiresDecl() { 1257 assert(Tok.is(MMToken::RequiresKeyword)); 1258 1259 // Parse 'requires' keyword. 1260 consumeToken(); 1261 1262 // Parse the feature-list. 1263 do { 1264 if (!Tok.is(MMToken::Identifier)) { 1265 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 1266 HadError = true; 1267 return; 1268 } 1269 1270 // Consume the feature name. 1271 std::string Feature = Tok.getString(); 1272 consumeToken(); 1273 1274 // Add this feature. 1275 ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target); 1276 1277 if (!Tok.is(MMToken::Comma)) 1278 break; 1279 1280 // Consume the comma. 1281 consumeToken(); 1282 } while (true); 1283 } 1284 1285 /// \brief Append to \p Paths the set of paths needed to get to the 1286 /// subframework in which the given module lives. 1287 static void appendSubframeworkPaths(Module *Mod, 1288 SmallVectorImpl<char> &Path) { 1289 // Collect the framework names from the given module to the top-level module. 1290 SmallVector<StringRef, 2> Paths; 1291 for (; Mod; Mod = Mod->Parent) { 1292 if (Mod->IsFramework) 1293 Paths.push_back(Mod->Name); 1294 } 1295 1296 if (Paths.empty()) 1297 return; 1298 1299 // Add Frameworks/Name.framework for each subframework. 1300 for (unsigned I = Paths.size() - 1; I != 0; --I) { 1301 llvm::sys::path::append(Path, "Frameworks"); 1302 llvm::sys::path::append(Path, Paths[I-1] + ".framework"); 1303 } 1304 } 1305 1306 /// \brief Parse a header declaration. 1307 /// 1308 /// header-declaration: 1309 /// 'umbrella'[opt] 'header' string-literal 1310 /// 'exclude'[opt] 'header' string-literal 1311 void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc, 1312 SourceLocation ExcludeLoc) { 1313 assert(Tok.is(MMToken::HeaderKeyword)); 1314 consumeToken(); 1315 1316 bool Umbrella = UmbrellaLoc.isValid(); 1317 bool Exclude = ExcludeLoc.isValid(); 1318 assert(!(Umbrella && Exclude) && "Cannot have both 'umbrella' and 'exclude'"); 1319 // Parse the header name. 1320 if (!Tok.is(MMToken::StringLiteral)) { 1321 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1322 << "header"; 1323 HadError = true; 1324 return; 1325 } 1326 std::string FileName = Tok.getString(); 1327 SourceLocation FileNameLoc = consumeToken(); 1328 1329 // Check whether we already have an umbrella. 1330 if (Umbrella && ActiveModule->Umbrella) { 1331 Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash) 1332 << ActiveModule->getFullModuleName(); 1333 HadError = true; 1334 return; 1335 } 1336 1337 // Look for this file. 1338 const FileEntry *File = 0; 1339 const FileEntry *BuiltinFile = 0; 1340 SmallString<128> PathName; 1341 if (llvm::sys::path::is_absolute(FileName)) { 1342 PathName = FileName; 1343 File = SourceMgr.getFileManager().getFile(PathName); 1344 } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) { 1345 PathName = Dir->getName(); 1346 llvm::sys::path::append(PathName, FileName); 1347 File = SourceMgr.getFileManager().getFile(PathName); 1348 } else { 1349 // Search for the header file within the search directory. 1350 PathName = Directory->getName(); 1351 unsigned PathLength = PathName.size(); 1352 1353 if (ActiveModule->isPartOfFramework()) { 1354 appendSubframeworkPaths(ActiveModule, PathName); 1355 1356 // Check whether this file is in the public headers. 1357 llvm::sys::path::append(PathName, "Headers"); 1358 llvm::sys::path::append(PathName, FileName); 1359 File = SourceMgr.getFileManager().getFile(PathName); 1360 1361 if (!File) { 1362 // Check whether this file is in the private headers. 1363 PathName.resize(PathLength); 1364 llvm::sys::path::append(PathName, "PrivateHeaders"); 1365 llvm::sys::path::append(PathName, FileName); 1366 File = SourceMgr.getFileManager().getFile(PathName); 1367 } 1368 } else { 1369 // Lookup for normal headers. 1370 llvm::sys::path::append(PathName, FileName); 1371 File = SourceMgr.getFileManager().getFile(PathName); 1372 1373 // If this is a system module with a top-level header, this header 1374 // may have a counterpart (or replacement) in the set of headers 1375 // supplied by Clang. Find that builtin header. 1376 if (ActiveModule->IsSystem && !Umbrella && BuiltinIncludeDir && 1377 BuiltinIncludeDir != Directory && isBuiltinHeader(FileName)) { 1378 SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); 1379 llvm::sys::path::append(BuiltinPathName, FileName); 1380 BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); 1381 1382 // If Clang supplies this header but the underlying system does not, 1383 // just silently swap in our builtin version. Otherwise, we'll end 1384 // up adding both (later). 1385 if (!File && BuiltinFile) { 1386 File = BuiltinFile; 1387 BuiltinFile = 0; 1388 } 1389 } 1390 } 1391 } 1392 1393 // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 1394 // Come up with a lazy way to do this. 1395 if (File) { 1396 if (ModuleMap::KnownHeader OwningModule = Map.Headers[File]) { 1397 Diags.Report(FileNameLoc, diag::err_mmap_header_conflict) 1398 << FileName << OwningModule.getModule()->getFullModuleName(); 1399 HadError = true; 1400 } else if (Umbrella) { 1401 const DirectoryEntry *UmbrellaDir = File->getDir(); 1402 if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { 1403 Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1404 << UmbrellaModule->getFullModuleName(); 1405 HadError = true; 1406 } else { 1407 // Record this umbrella header. 1408 Map.setUmbrellaHeader(ActiveModule, File); 1409 } 1410 } else { 1411 // Record this header. 1412 Map.addHeader(ActiveModule, File, Exclude); 1413 1414 // If there is a builtin counterpart to this file, add it now. 1415 if (BuiltinFile) 1416 Map.addHeader(ActiveModule, BuiltinFile, Exclude); 1417 } 1418 } else if (!Exclude) { 1419 // Ignore excluded header files. They're optional anyway. 1420 1421 Diags.Report(FileNameLoc, diag::err_mmap_header_not_found) 1422 << Umbrella << FileName; 1423 HadError = true; 1424 } 1425 } 1426 1427 /// \brief Parse an umbrella directory declaration. 1428 /// 1429 /// umbrella-dir-declaration: 1430 /// umbrella string-literal 1431 void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1432 // Parse the directory name. 1433 if (!Tok.is(MMToken::StringLiteral)) { 1434 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1435 << "umbrella"; 1436 HadError = true; 1437 return; 1438 } 1439 1440 std::string DirName = Tok.getString(); 1441 SourceLocation DirNameLoc = consumeToken(); 1442 1443 // Check whether we already have an umbrella. 1444 if (ActiveModule->Umbrella) { 1445 Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1446 << ActiveModule->getFullModuleName(); 1447 HadError = true; 1448 return; 1449 } 1450 1451 // Look for this file. 1452 const DirectoryEntry *Dir = 0; 1453 if (llvm::sys::path::is_absolute(DirName)) 1454 Dir = SourceMgr.getFileManager().getDirectory(DirName); 1455 else { 1456 SmallString<128> PathName; 1457 PathName = Directory->getName(); 1458 llvm::sys::path::append(PathName, DirName); 1459 Dir = SourceMgr.getFileManager().getDirectory(PathName); 1460 } 1461 1462 if (!Dir) { 1463 Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1464 << DirName; 1465 HadError = true; 1466 return; 1467 } 1468 1469 if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1470 Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1471 << OwningModule->getFullModuleName(); 1472 HadError = true; 1473 return; 1474 } 1475 1476 // Record this umbrella directory. 1477 Map.setUmbrellaDir(ActiveModule, Dir); 1478 } 1479 1480 /// \brief Parse a module export declaration. 1481 /// 1482 /// export-declaration: 1483 /// 'export' wildcard-module-id 1484 /// 1485 /// wildcard-module-id: 1486 /// identifier 1487 /// '*' 1488 /// identifier '.' wildcard-module-id 1489 void ModuleMapParser::parseExportDecl() { 1490 assert(Tok.is(MMToken::ExportKeyword)); 1491 SourceLocation ExportLoc = consumeToken(); 1492 1493 // Parse the module-id with an optional wildcard at the end. 1494 ModuleId ParsedModuleId; 1495 bool Wildcard = false; 1496 do { 1497 if (Tok.is(MMToken::Identifier)) { 1498 ParsedModuleId.push_back(std::make_pair(Tok.getString(), 1499 Tok.getLocation())); 1500 consumeToken(); 1501 1502 if (Tok.is(MMToken::Period)) { 1503 consumeToken(); 1504 continue; 1505 } 1506 1507 break; 1508 } 1509 1510 if(Tok.is(MMToken::Star)) { 1511 Wildcard = true; 1512 consumeToken(); 1513 break; 1514 } 1515 1516 Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id); 1517 HadError = true; 1518 return; 1519 } while (true); 1520 1521 Module::UnresolvedExportDecl Unresolved = { 1522 ExportLoc, ParsedModuleId, Wildcard 1523 }; 1524 ActiveModule->UnresolvedExports.push_back(Unresolved); 1525 } 1526 1527 /// \brief Parse a link declaration. 1528 /// 1529 /// module-declaration: 1530 /// 'link' 'framework'[opt] string-literal 1531 void ModuleMapParser::parseLinkDecl() { 1532 assert(Tok.is(MMToken::LinkKeyword)); 1533 SourceLocation LinkLoc = consumeToken(); 1534 1535 // Parse the optional 'framework' keyword. 1536 bool IsFramework = false; 1537 if (Tok.is(MMToken::FrameworkKeyword)) { 1538 consumeToken(); 1539 IsFramework = true; 1540 } 1541 1542 // Parse the library name 1543 if (!Tok.is(MMToken::StringLiteral)) { 1544 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) 1545 << IsFramework << SourceRange(LinkLoc); 1546 HadError = true; 1547 return; 1548 } 1549 1550 std::string LibraryName = Tok.getString(); 1551 consumeToken(); 1552 ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, 1553 IsFramework)); 1554 } 1555 1556 /// \brief Parse a configuration macro declaration. 1557 /// 1558 /// module-declaration: 1559 /// 'config_macros' attributes[opt] config-macro-list? 1560 /// 1561 /// config-macro-list: 1562 /// identifier (',' identifier)? 1563 void ModuleMapParser::parseConfigMacros() { 1564 assert(Tok.is(MMToken::ConfigMacros)); 1565 SourceLocation ConfigMacrosLoc = consumeToken(); 1566 1567 // Only top-level modules can have configuration macros. 1568 if (ActiveModule->Parent) { 1569 Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule); 1570 } 1571 1572 // Parse the optional attributes. 1573 Attributes Attrs; 1574 parseOptionalAttributes(Attrs); 1575 if (Attrs.IsExhaustive && !ActiveModule->Parent) { 1576 ActiveModule->ConfigMacrosExhaustive = true; 1577 } 1578 1579 // If we don't have an identifier, we're done. 1580 if (!Tok.is(MMToken::Identifier)) 1581 return; 1582 1583 // Consume the first identifier. 1584 if (!ActiveModule->Parent) { 1585 ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 1586 } 1587 consumeToken(); 1588 1589 do { 1590 // If there's a comma, consume it. 1591 if (!Tok.is(MMToken::Comma)) 1592 break; 1593 consumeToken(); 1594 1595 // We expect to see a macro name here. 1596 if (!Tok.is(MMToken::Identifier)) { 1597 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro); 1598 break; 1599 } 1600 1601 // Consume the macro name. 1602 if (!ActiveModule->Parent) { 1603 ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 1604 } 1605 consumeToken(); 1606 } while (true); 1607 } 1608 1609 /// \brief Format a module-id into a string. 1610 static std::string formatModuleId(const ModuleId &Id) { 1611 std::string result; 1612 { 1613 llvm::raw_string_ostream OS(result); 1614 1615 for (unsigned I = 0, N = Id.size(); I != N; ++I) { 1616 if (I) 1617 OS << "."; 1618 OS << Id[I].first; 1619 } 1620 } 1621 1622 return result; 1623 } 1624 1625 /// \brief Parse a conflict declaration. 1626 /// 1627 /// module-declaration: 1628 /// 'conflict' module-id ',' string-literal 1629 void ModuleMapParser::parseConflict() { 1630 assert(Tok.is(MMToken::Conflict)); 1631 SourceLocation ConflictLoc = consumeToken(); 1632 Module::UnresolvedConflict Conflict; 1633 1634 // Parse the module-id. 1635 if (parseModuleId(Conflict.Id)) 1636 return; 1637 1638 // Parse the ','. 1639 if (!Tok.is(MMToken::Comma)) { 1640 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma) 1641 << SourceRange(ConflictLoc); 1642 return; 1643 } 1644 consumeToken(); 1645 1646 // Parse the message. 1647 if (!Tok.is(MMToken::StringLiteral)) { 1648 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message) 1649 << formatModuleId(Conflict.Id); 1650 return; 1651 } 1652 Conflict.Message = Tok.getString().str(); 1653 consumeToken(); 1654 1655 // Add this unresolved conflict. 1656 ActiveModule->UnresolvedConflicts.push_back(Conflict); 1657 } 1658 1659 /// \brief Parse an inferred module declaration (wildcard modules). 1660 /// 1661 /// module-declaration: 1662 /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] 1663 /// { inferred-module-member* } 1664 /// 1665 /// inferred-module-member: 1666 /// 'export' '*' 1667 /// 'exclude' identifier 1668 void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { 1669 assert(Tok.is(MMToken::Star)); 1670 SourceLocation StarLoc = consumeToken(); 1671 bool Failed = false; 1672 1673 // Inferred modules must be submodules. 1674 if (!ActiveModule && !Framework) { 1675 Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 1676 Failed = true; 1677 } 1678 1679 if (ActiveModule) { 1680 // Inferred modules must have umbrella directories. 1681 if (!Failed && !ActiveModule->getUmbrellaDir()) { 1682 Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 1683 Failed = true; 1684 } 1685 1686 // Check for redefinition of an inferred module. 1687 if (!Failed && ActiveModule->InferSubmodules) { 1688 Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 1689 if (ActiveModule->InferredSubmoduleLoc.isValid()) 1690 Diags.Report(ActiveModule->InferredSubmoduleLoc, 1691 diag::note_mmap_prev_definition); 1692 Failed = true; 1693 } 1694 1695 // Check for the 'framework' keyword, which is not permitted here. 1696 if (Framework) { 1697 Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); 1698 Framework = false; 1699 } 1700 } else if (Explicit) { 1701 Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); 1702 Explicit = false; 1703 } 1704 1705 // If there were any problems with this inferred submodule, skip its body. 1706 if (Failed) { 1707 if (Tok.is(MMToken::LBrace)) { 1708 consumeToken(); 1709 skipUntil(MMToken::RBrace); 1710 if (Tok.is(MMToken::RBrace)) 1711 consumeToken(); 1712 } 1713 HadError = true; 1714 return; 1715 } 1716 1717 // Parse optional attributes. 1718 Attributes Attrs; 1719 parseOptionalAttributes(Attrs); 1720 1721 if (ActiveModule) { 1722 // Note that we have an inferred submodule. 1723 ActiveModule->InferSubmodules = true; 1724 ActiveModule->InferredSubmoduleLoc = StarLoc; 1725 ActiveModule->InferExplicitSubmodules = Explicit; 1726 } else { 1727 // We'll be inferring framework modules for this directory. 1728 Map.InferredDirectories[Directory].InferModules = true; 1729 Map.InferredDirectories[Directory].InferSystemModules = Attrs.IsSystem; 1730 } 1731 1732 // Parse the opening brace. 1733 if (!Tok.is(MMToken::LBrace)) { 1734 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 1735 HadError = true; 1736 return; 1737 } 1738 SourceLocation LBraceLoc = consumeToken(); 1739 1740 // Parse the body of the inferred submodule. 1741 bool Done = false; 1742 do { 1743 switch (Tok.Kind) { 1744 case MMToken::EndOfFile: 1745 case MMToken::RBrace: 1746 Done = true; 1747 break; 1748 1749 case MMToken::ExcludeKeyword: { 1750 if (ActiveModule) { 1751 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 1752 << (ActiveModule != 0); 1753 consumeToken(); 1754 break; 1755 } 1756 1757 consumeToken(); 1758 if (!Tok.is(MMToken::Identifier)) { 1759 Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); 1760 break; 1761 } 1762 1763 Map.InferredDirectories[Directory].ExcludedModules 1764 .push_back(Tok.getString()); 1765 consumeToken(); 1766 break; 1767 } 1768 1769 case MMToken::ExportKeyword: 1770 if (!ActiveModule) { 1771 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 1772 << (ActiveModule != 0); 1773 consumeToken(); 1774 break; 1775 } 1776 1777 consumeToken(); 1778 if (Tok.is(MMToken::Star)) 1779 ActiveModule->InferExportWildcard = true; 1780 else 1781 Diags.Report(Tok.getLocation(), 1782 diag::err_mmap_expected_export_wildcard); 1783 consumeToken(); 1784 break; 1785 1786 case MMToken::ExplicitKeyword: 1787 case MMToken::ModuleKeyword: 1788 case MMToken::HeaderKeyword: 1789 case MMToken::UmbrellaKeyword: 1790 default: 1791 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 1792 << (ActiveModule != 0); 1793 consumeToken(); 1794 break; 1795 } 1796 } while (!Done); 1797 1798 if (Tok.is(MMToken::RBrace)) 1799 consumeToken(); 1800 else { 1801 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1802 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1803 HadError = true; 1804 } 1805 } 1806 1807 /// \brief Parse optional attributes. 1808 /// 1809 /// attributes: 1810 /// attribute attributes 1811 /// attribute 1812 /// 1813 /// attribute: 1814 /// [ identifier ] 1815 /// 1816 /// \param Attrs Will be filled in with the parsed attributes. 1817 /// 1818 /// \returns true if an error occurred, false otherwise. 1819 bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { 1820 bool HadError = false; 1821 1822 while (Tok.is(MMToken::LSquare)) { 1823 // Consume the '['. 1824 SourceLocation LSquareLoc = consumeToken(); 1825 1826 // Check whether we have an attribute name here. 1827 if (!Tok.is(MMToken::Identifier)) { 1828 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 1829 skipUntil(MMToken::RSquare); 1830 if (Tok.is(MMToken::RSquare)) 1831 consumeToken(); 1832 HadError = true; 1833 } 1834 1835 // Decode the attribute name. 1836 AttributeKind Attribute 1837 = llvm::StringSwitch<AttributeKind>(Tok.getString()) 1838 .Case("exhaustive", AT_exhaustive) 1839 .Case("system", AT_system) 1840 .Default(AT_unknown); 1841 switch (Attribute) { 1842 case AT_unknown: 1843 Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 1844 << Tok.getString(); 1845 break; 1846 1847 case AT_system: 1848 Attrs.IsSystem = true; 1849 break; 1850 1851 case AT_exhaustive: 1852 Attrs.IsExhaustive = true; 1853 break; 1854 } 1855 consumeToken(); 1856 1857 // Consume the ']'. 1858 if (!Tok.is(MMToken::RSquare)) { 1859 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 1860 Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 1861 skipUntil(MMToken::RSquare); 1862 HadError = true; 1863 } 1864 1865 if (Tok.is(MMToken::RSquare)) 1866 consumeToken(); 1867 } 1868 1869 return HadError; 1870 } 1871 1872 /// \brief If there is a specific header search directory due the presence 1873 /// of an umbrella directory, retrieve that directory. Otherwise, returns null. 1874 const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() { 1875 for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) { 1876 // If we have an umbrella directory, use that. 1877 if (Mod->hasUmbrellaDir()) 1878 return Mod->getUmbrellaDir(); 1879 1880 // If we have a framework directory, stop looking. 1881 if (Mod->IsFramework) 1882 return 0; 1883 } 1884 1885 return 0; 1886 } 1887 1888 /// \brief Parse a module map file. 1889 /// 1890 /// module-map-file: 1891 /// module-declaration* 1892 bool ModuleMapParser::parseModuleMapFile() { 1893 do { 1894 switch (Tok.Kind) { 1895 case MMToken::EndOfFile: 1896 return HadError; 1897 1898 case MMToken::ExplicitKeyword: 1899 case MMToken::ModuleKeyword: 1900 case MMToken::FrameworkKeyword: 1901 parseModuleDecl(); 1902 break; 1903 1904 case MMToken::Comma: 1905 case MMToken::ConfigMacros: 1906 case MMToken::Conflict: 1907 case MMToken::ExcludeKeyword: 1908 case MMToken::ExportKeyword: 1909 case MMToken::HeaderKeyword: 1910 case MMToken::Identifier: 1911 case MMToken::LBrace: 1912 case MMToken::LinkKeyword: 1913 case MMToken::LSquare: 1914 case MMToken::Period: 1915 case MMToken::RBrace: 1916 case MMToken::RSquare: 1917 case MMToken::RequiresKeyword: 1918 case MMToken::Star: 1919 case MMToken::StringLiteral: 1920 case MMToken::UmbrellaKeyword: 1921 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1922 HadError = true; 1923 consumeToken(); 1924 break; 1925 } 1926 } while (true); 1927 } 1928 1929 bool ModuleMap::parseModuleMapFile(const FileEntry *File) { 1930 llvm::DenseMap<const FileEntry *, bool>::iterator Known 1931 = ParsedModuleMap.find(File); 1932 if (Known != ParsedModuleMap.end()) 1933 return Known->second; 1934 1935 assert(Target != 0 && "Missing target information"); 1936 FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User); 1937 const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID); 1938 if (!Buffer) 1939 return ParsedModuleMap[File] = true; 1940 1941 // Parse this module map file. 1942 Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts); 1943 Diags->getClient()->BeginSourceFile(MMapLangOpts); 1944 ModuleMapParser Parser(L, *SourceMgr, Target, *Diags, *this, File->getDir(), 1945 BuiltinIncludeDir); 1946 bool Result = Parser.parseModuleMapFile(); 1947 Diags->getClient()->EndSourceFile(); 1948 ParsedModuleMap[File] = Result; 1949 return Result; 1950 } 1951