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/Lex/Lexer.h" 16 #include "clang/Lex/LiteralSupport.h" 17 #include "clang/Lex/LexDiagnostic.h" 18 #include "clang/Basic/Diagnostic.h" 19 #include "clang/Basic/FileManager.h" 20 #include "clang/Basic/TargetInfo.h" 21 #include "clang/Basic/TargetOptions.h" 22 #include "llvm/Support/Allocator.h" 23 #include "llvm/Support/FileSystem.h" 24 #include "llvm/Support/Host.h" 25 #include "llvm/Support/PathV2.h" 26 #include "llvm/Support/raw_ostream.h" 27 #include "llvm/ADT/StringRef.h" 28 #include "llvm/ADT/StringSwitch.h" 29 using namespace clang; 30 31 Module::ExportDecl 32 ModuleMap::resolveExport(Module *Mod, 33 const Module::UnresolvedExportDecl &Unresolved, 34 bool Complain) { 35 // We may have just a wildcard. 36 if (Unresolved.Id.empty()) { 37 assert(Unresolved.Wildcard && "Invalid unresolved export"); 38 return Module::ExportDecl(0, true); 39 } 40 41 // Find the starting module. 42 Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod); 43 if (!Context) { 44 if (Complain) 45 Diags->Report(Unresolved.Id[0].second, 46 diag::err_mmap_missing_module_unqualified) 47 << Unresolved.Id[0].first << Mod->getFullModuleName(); 48 49 return Module::ExportDecl(); 50 } 51 52 // Dig into the module path. 53 for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) { 54 Module *Sub = lookupModuleQualified(Unresolved.Id[I].first, 55 Context); 56 if (!Sub) { 57 if (Complain) 58 Diags->Report(Unresolved.Id[I].second, 59 diag::err_mmap_missing_module_qualified) 60 << Unresolved.Id[I].first << Context->getFullModuleName() 61 << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second); 62 63 return Module::ExportDecl(); 64 } 65 66 Context = Sub; 67 } 68 69 return Module::ExportDecl(Context, Unresolved.Wildcard); 70 } 71 72 ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC) { 73 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs); 74 Diags = llvm::IntrusiveRefCntPtr<DiagnosticsEngine>( 75 new DiagnosticsEngine(DiagIDs)); 76 Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true); 77 SourceMgr = new SourceManager(*Diags, FileMgr); 78 } 79 80 ModuleMap::~ModuleMap() { 81 for (llvm::StringMap<Module *>::iterator I = Modules.begin(), 82 IEnd = Modules.end(); 83 I != IEnd; ++I) { 84 delete I->getValue(); 85 } 86 87 delete SourceMgr; 88 } 89 90 Module *ModuleMap::findModuleForHeader(const FileEntry *File) { 91 llvm::DenseMap<const FileEntry *, Module *>::iterator Known 92 = Headers.find(File); 93 if (Known != Headers.end()) 94 return Known->second; 95 96 const DirectoryEntry *Dir = File->getDir(); 97 llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs; 98 StringRef DirName = Dir->getName(); 99 100 // Keep walking up the directory hierarchy, looking for a directory with 101 // an umbrella header. 102 do { 103 llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir 104 = UmbrellaDirs.find(Dir); 105 if (KnownDir != UmbrellaDirs.end()) { 106 Module *Result = KnownDir->second; 107 108 // Search up the module stack until we find a module with an umbrella 109 // directory. 110 Module *UmbrellaModule = Result; 111 while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 112 UmbrellaModule = UmbrellaModule->Parent; 113 114 if (UmbrellaModule->InferSubmodules) { 115 // Infer submodules for each of the directories we found between 116 // the directory of the umbrella header and the directory where 117 // the actual header is located. 118 119 // For a framework module, the umbrella directory is the framework 120 // directory, so strip off the "Headers" or "PrivateHeaders". 121 bool Explicit = UmbrellaModule->InferExplicitSubmodules; 122 unsigned LastSkippedDir = SkippedDirs.size(); 123 if (LastSkippedDir && UmbrellaModule->IsFramework) { 124 if (llvm::sys::path::filename(SkippedDirs.back()->getName()) 125 == "PrivateHeaders") { 126 // For private headers, add an explicit "Private" module. 127 // FIXME: This feels somewhat hackish. Do we want to introduce 128 // some kind of "umbrella directory" here? 129 Result = findOrCreateModule("Private", Result, 130 /*IsFramework=*/false, 131 /*IsExplicit=*/true).first; 132 Explicit = true; 133 } 134 135 --LastSkippedDir; 136 } 137 138 for (unsigned I = LastSkippedDir; I != 0; --I) { 139 // Find or create the module that corresponds to this directory name. 140 StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName()); 141 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 142 Explicit).first; 143 144 // Associate the module and the directory. 145 UmbrellaDirs[SkippedDirs[I-1]] = Result; 146 147 // If inferred submodules export everything they import, add a 148 // wildcard to the set of exports. 149 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 150 Result->Exports.push_back(Module::ExportDecl(0, true)); 151 } 152 153 // Infer a submodule with the same name as this header file. 154 StringRef Name = llvm::sys::path::stem(File->getName()); 155 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 156 Explicit).first; 157 158 // If inferred submodules export everything they import, add a 159 // wildcard to the set of exports. 160 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 161 Result->Exports.push_back(Module::ExportDecl(0, true)); 162 } else { 163 // Record each of the directories we stepped through as being part of 164 // the module we found, since the umbrella header covers them all. 165 for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 166 UmbrellaDirs[SkippedDirs[I]] = Result; 167 } 168 169 Headers[File] = Result; 170 return Result; 171 } 172 173 SkippedDirs.push_back(Dir); 174 175 // Retrieve our parent path. 176 DirName = llvm::sys::path::parent_path(DirName); 177 if (DirName.empty()) 178 break; 179 180 // Resolve the parent path to a directory entry. 181 Dir = SourceMgr->getFileManager().getDirectory(DirName); 182 } while (Dir); 183 184 return 0; 185 } 186 187 Module *ModuleMap::findModule(StringRef Name) { 188 llvm::StringMap<Module *>::iterator Known = Modules.find(Name); 189 if (Known != Modules.end()) 190 return Known->getValue(); 191 192 return 0; 193 } 194 195 Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) { 196 for(; Context; Context = Context->Parent) { 197 if (Module *Sub = lookupModuleQualified(Name, Context)) 198 return Sub; 199 } 200 201 return findModule(Name); 202 } 203 204 Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) { 205 if (!Context) 206 return findModule(Name); 207 208 llvm::StringMap<Module *>::iterator Sub = Context->SubModules.find(Name); 209 if (Sub != Context->SubModules.end()) 210 return Sub->getValue(); 211 212 return 0; 213 } 214 215 std::pair<Module *, bool> 216 ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 217 bool IsExplicit) { 218 // Try to find an existing module with this name. 219 if (Module *Found = Parent? Parent->SubModules[Name] : Modules[Name]) 220 return std::make_pair(Found, false); 221 222 // Create a new module with this name. 223 Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework, 224 IsExplicit); 225 if (Parent) 226 Parent->SubModules[Name] = Result; 227 else 228 Modules[Name] = Result; 229 return std::make_pair(Result, true); 230 } 231 232 Module * 233 ModuleMap::inferFrameworkModule(StringRef ModuleName, 234 const DirectoryEntry *FrameworkDir, 235 Module *Parent) { 236 // Check whether we've already found this module. 237 if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 238 return Mod; 239 240 FileManager &FileMgr = SourceMgr->getFileManager(); 241 242 // Look for an umbrella header. 243 llvm::SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 244 llvm::sys::path::append(UmbrellaName, "Headers"); 245 llvm::sys::path::append(UmbrellaName, ModuleName + ".h"); 246 const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 247 248 // FIXME: If there's no umbrella header, we could probably scan the 249 // framework to load *everything*. But, it's not clear that this is a good 250 // idea. 251 if (!UmbrellaHeader) 252 return 0; 253 254 Module *Result = new Module(ModuleName, SourceLocation(), Parent, 255 /*IsFramework=*/true, /*IsExplicit=*/false); 256 257 if (Parent) 258 Parent->SubModules[ModuleName] = Result; 259 else 260 Modules[ModuleName] = Result; 261 262 // umbrella header "umbrella-header-name" 263 Result->Umbrella = UmbrellaHeader; 264 Headers[UmbrellaHeader] = Result; 265 UmbrellaDirs[FrameworkDir] = Result; 266 267 // export * 268 Result->Exports.push_back(Module::ExportDecl(0, true)); 269 270 // module * { export * } 271 Result->InferSubmodules = true; 272 Result->InferExportWildcard = true; 273 274 // Look for subframeworks. 275 llvm::error_code EC; 276 llvm::SmallString<128> SubframeworksDirName 277 = StringRef(FrameworkDir->getName()); 278 llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 279 llvm::SmallString<128> SubframeworksDirNameNative; 280 llvm::sys::path::native(SubframeworksDirName.str(), 281 SubframeworksDirNameNative); 282 for (llvm::sys::fs::directory_iterator 283 Dir(SubframeworksDirNameNative.str(), EC), DirEnd; 284 Dir != DirEnd && !EC; Dir.increment(EC)) { 285 if (!StringRef(Dir->path()).endswith(".framework")) 286 continue; 287 288 if (const DirectoryEntry *SubframeworkDir 289 = FileMgr.getDirectory(Dir->path())) { 290 // FIXME: Do we want to warn about subframeworks without umbrella headers? 291 inferFrameworkModule(llvm::sys::path::stem(Dir->path()), SubframeworkDir, 292 Result); 293 } 294 } 295 296 // Look for private headers. 297 Module *ModulePrivate = 0; 298 llvm::SmallString<128> PrivateHeadersDirName(FrameworkDir->getName()); 299 llvm::sys::path::append(PrivateHeadersDirName, "PrivateHeaders"); 300 llvm::SmallString<128> PrivateHeadersDirNameNative; 301 llvm::sys::path::native(PrivateHeadersDirName.str(), 302 PrivateHeadersDirNameNative); 303 for (llvm::sys::fs::directory_iterator 304 Dir(PrivateHeadersDirNameNative.str(), EC), DirEnd; 305 Dir != DirEnd && !EC; Dir.increment(EC)) { 306 // Check whether this entry has an extension typically associated with 307 // headers. 308 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path())) 309 .Cases(".h", ".H", ".hh", ".hpp", true) 310 .Default(false)) 311 continue; 312 313 if (const FileEntry *PrivateHeader = FileMgr.getFile(Dir->path())) { 314 // Create the "private" submodule, if we haven't done so already. 315 if (!ModulePrivate) { 316 ModulePrivate = findOrCreateModule("Private", Result, 317 /*IsFramework=*/false, 318 /*IsExplicit=*/true).first; 319 } 320 321 Module *Sub = findOrCreateModule(llvm::sys::path::stem(Dir->path()), 322 ModulePrivate, /*IsFramework=*/false, 323 /*IsExplicit=*/true).first; 324 // header "the private header" 325 Sub->Headers.push_back(PrivateHeader); 326 327 // export * 328 Sub->Exports.push_back(Module::ExportDecl(0, true)); 329 330 Headers[PrivateHeader] = Sub; 331 } 332 } 333 334 return Result; 335 } 336 337 void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ 338 Headers[UmbrellaHeader] = Mod; 339 Mod->Umbrella = UmbrellaHeader; 340 341 const DirectoryEntry *UmbrellaDir = UmbrellaHeader->getDir(); 342 if (Mod->IsFramework) 343 UmbrellaDir = SourceMgr->getFileManager().getDirectory( 344 llvm::sys::path::parent_path(UmbrellaDir->getName())); 345 346 UmbrellaDirs[UmbrellaDir] = Mod; 347 } 348 349 void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) { 350 Mod->Umbrella = UmbrellaDir; 351 UmbrellaDirs[UmbrellaDir] = Mod; 352 } 353 354 void ModuleMap::addHeader(Module *Mod, const FileEntry *Header) { 355 Mod->Headers.push_back(Header); 356 Headers[Header] = Mod; 357 } 358 359 const FileEntry * 360 ModuleMap::getContainingModuleMapFile(Module *Module) { 361 if (Module->DefinitionLoc.isInvalid() || !SourceMgr) 362 return 0; 363 364 return SourceMgr->getFileEntryForID( 365 SourceMgr->getFileID(Module->DefinitionLoc)); 366 } 367 368 void ModuleMap::dump() { 369 llvm::errs() << "Modules:"; 370 for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 371 MEnd = Modules.end(); 372 M != MEnd; ++M) 373 M->getValue()->print(llvm::errs(), 2); 374 375 llvm::errs() << "Headers:"; 376 for (llvm::DenseMap<const FileEntry *, Module *>::iterator 377 H = Headers.begin(), 378 HEnd = Headers.end(); 379 H != HEnd; ++H) { 380 llvm::errs() << " \"" << H->first->getName() << "\" -> " 381 << H->second->getFullModuleName() << "\n"; 382 } 383 } 384 385 bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 386 bool HadError = false; 387 for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { 388 Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 389 Complain); 390 if (Export.getPointer() || Export.getInt()) 391 Mod->Exports.push_back(Export); 392 else 393 HadError = true; 394 } 395 Mod->UnresolvedExports.clear(); 396 return HadError; 397 } 398 399 Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 400 if (Loc.isInvalid()) 401 return 0; 402 403 // Use the expansion location to determine which module we're in. 404 FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 405 if (!ExpansionLoc.isFileID()) 406 return 0; 407 408 409 const SourceManager &SrcMgr = Loc.getManager(); 410 FileID ExpansionFileID = ExpansionLoc.getFileID(); 411 const FileEntry *ExpansionFile = SrcMgr.getFileEntryForID(ExpansionFileID); 412 if (!ExpansionFile) 413 return 0; 414 415 // Find the module that owns this header. 416 return findModuleForHeader(ExpansionFile); 417 } 418 419 //----------------------------------------------------------------------------// 420 // Module map file parser 421 //----------------------------------------------------------------------------// 422 423 namespace clang { 424 /// \brief A token in a module map file. 425 struct MMToken { 426 enum TokenKind { 427 EndOfFile, 428 HeaderKeyword, 429 Identifier, 430 ExplicitKeyword, 431 ExportKeyword, 432 FrameworkKeyword, 433 ModuleKeyword, 434 Period, 435 UmbrellaKeyword, 436 Star, 437 StringLiteral, 438 LBrace, 439 RBrace 440 } Kind; 441 442 unsigned Location; 443 unsigned StringLength; 444 const char *StringData; 445 446 void clear() { 447 Kind = EndOfFile; 448 Location = 0; 449 StringLength = 0; 450 StringData = 0; 451 } 452 453 bool is(TokenKind K) const { return Kind == K; } 454 455 SourceLocation getLocation() const { 456 return SourceLocation::getFromRawEncoding(Location); 457 } 458 459 StringRef getString() const { 460 return StringRef(StringData, StringLength); 461 } 462 }; 463 464 class ModuleMapParser { 465 Lexer &L; 466 SourceManager &SourceMgr; 467 DiagnosticsEngine &Diags; 468 ModuleMap ⤅ 469 470 /// \brief The directory that this module map resides in. 471 const DirectoryEntry *Directory; 472 473 /// \brief Whether an error occurred. 474 bool HadError; 475 476 /// \brief Default target information, used only for string literal 477 /// parsing. 478 TargetInfo *Target; 479 480 /// \brief Stores string data for the various string literals referenced 481 /// during parsing. 482 llvm::BumpPtrAllocator StringData; 483 484 /// \brief The current token. 485 MMToken Tok; 486 487 /// \brief The active module. 488 Module *ActiveModule; 489 490 /// \brief Consume the current token and return its location. 491 SourceLocation consumeToken(); 492 493 /// \brief Skip tokens until we reach the a token with the given kind 494 /// (or the end of the file). 495 void skipUntil(MMToken::TokenKind K); 496 497 typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2> 498 ModuleId; 499 bool parseModuleId(ModuleId &Id); 500 void parseModuleDecl(); 501 void parseHeaderDecl(SourceLocation UmbrellaLoc); 502 void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 503 void parseExportDecl(); 504 void parseInferredSubmoduleDecl(bool Explicit); 505 506 public: 507 explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 508 DiagnosticsEngine &Diags, 509 ModuleMap &Map, 510 const DirectoryEntry *Directory) 511 : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map), 512 Directory(Directory), HadError(false), ActiveModule(0) 513 { 514 TargetOptions TargetOpts; 515 TargetOpts.Triple = llvm::sys::getDefaultTargetTriple(); 516 Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts); 517 518 Tok.clear(); 519 consumeToken(); 520 } 521 522 bool parseModuleMapFile(); 523 }; 524 } 525 526 SourceLocation ModuleMapParser::consumeToken() { 527 retry: 528 SourceLocation Result = Tok.getLocation(); 529 Tok.clear(); 530 531 Token LToken; 532 L.LexFromRawLexer(LToken); 533 Tok.Location = LToken.getLocation().getRawEncoding(); 534 switch (LToken.getKind()) { 535 case tok::raw_identifier: 536 Tok.StringData = LToken.getRawIdentifierData(); 537 Tok.StringLength = LToken.getLength(); 538 Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString()) 539 .Case("header", MMToken::HeaderKeyword) 540 .Case("explicit", MMToken::ExplicitKeyword) 541 .Case("export", MMToken::ExportKeyword) 542 .Case("framework", MMToken::FrameworkKeyword) 543 .Case("module", MMToken::ModuleKeyword) 544 .Case("umbrella", MMToken::UmbrellaKeyword) 545 .Default(MMToken::Identifier); 546 break; 547 548 case tok::eof: 549 Tok.Kind = MMToken::EndOfFile; 550 break; 551 552 case tok::l_brace: 553 Tok.Kind = MMToken::LBrace; 554 break; 555 556 case tok::period: 557 Tok.Kind = MMToken::Period; 558 break; 559 560 case tok::r_brace: 561 Tok.Kind = MMToken::RBrace; 562 break; 563 564 case tok::star: 565 Tok.Kind = MMToken::Star; 566 break; 567 568 case tok::string_literal: { 569 // Parse the string literal. 570 LangOptions LangOpts; 571 StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target); 572 if (StringLiteral.hadError) 573 goto retry; 574 575 // Copy the string literal into our string data allocator. 576 unsigned Length = StringLiteral.GetStringLength(); 577 char *Saved = StringData.Allocate<char>(Length + 1); 578 memcpy(Saved, StringLiteral.GetString().data(), Length); 579 Saved[Length] = 0; 580 581 // Form the token. 582 Tok.Kind = MMToken::StringLiteral; 583 Tok.StringData = Saved; 584 Tok.StringLength = Length; 585 break; 586 } 587 588 case tok::comment: 589 goto retry; 590 591 default: 592 Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 593 HadError = true; 594 goto retry; 595 } 596 597 return Result; 598 } 599 600 void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 601 unsigned braceDepth = 0; 602 do { 603 switch (Tok.Kind) { 604 case MMToken::EndOfFile: 605 return; 606 607 case MMToken::LBrace: 608 if (Tok.is(K) && braceDepth == 0) 609 return; 610 611 ++braceDepth; 612 break; 613 614 case MMToken::RBrace: 615 if (braceDepth > 0) 616 --braceDepth; 617 else if (Tok.is(K)) 618 return; 619 break; 620 621 default: 622 if (braceDepth == 0 && Tok.is(K)) 623 return; 624 break; 625 } 626 627 consumeToken(); 628 } while (true); 629 } 630 631 /// \brief Parse a module-id. 632 /// 633 /// module-id: 634 /// identifier 635 /// identifier '.' module-id 636 /// 637 /// \returns true if an error occurred, false otherwise. 638 bool ModuleMapParser::parseModuleId(ModuleId &Id) { 639 Id.clear(); 640 do { 641 if (Tok.is(MMToken::Identifier)) { 642 Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 643 consumeToken(); 644 } else { 645 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 646 return true; 647 } 648 649 if (!Tok.is(MMToken::Period)) 650 break; 651 652 consumeToken(); 653 } while (true); 654 655 return false; 656 } 657 658 /// \brief Parse a module declaration. 659 /// 660 /// module-declaration: 661 /// 'explicit'[opt] 'framework'[opt] 'module' module-id { module-member* } 662 /// 663 /// module-member: 664 /// header-declaration 665 /// submodule-declaration 666 /// export-declaration 667 /// 668 /// submodule-declaration: 669 /// module-declaration 670 /// inferred-submodule-declaration 671 void ModuleMapParser::parseModuleDecl() { 672 assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 673 Tok.is(MMToken::FrameworkKeyword)); 674 // Parse 'explicit' or 'framework' keyword, if present. 675 SourceLocation ExplicitLoc; 676 bool Explicit = false; 677 bool Framework = false; 678 679 // Parse 'explicit' keyword, if present. 680 if (Tok.is(MMToken::ExplicitKeyword)) { 681 ExplicitLoc = consumeToken(); 682 Explicit = true; 683 } 684 685 // Parse 'framework' keyword, if present. 686 if (Tok.is(MMToken::FrameworkKeyword)) { 687 consumeToken(); 688 Framework = true; 689 } 690 691 // Parse 'module' keyword. 692 if (!Tok.is(MMToken::ModuleKeyword)) { 693 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 694 consumeToken(); 695 HadError = true; 696 return; 697 } 698 consumeToken(); // 'module' keyword 699 700 // If we have a wildcard for the module name, this is an inferred submodule. 701 // Parse it. 702 if (Tok.is(MMToken::Star)) 703 return parseInferredSubmoduleDecl(Explicit); 704 705 // Parse the module name. 706 ModuleId Id; 707 if (parseModuleId(Id)) { 708 HadError = true; 709 return; 710 } 711 712 if (ActiveModule) { 713 if (Id.size() > 1) { 714 Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 715 << SourceRange(Id.front().second, Id.back().second); 716 717 HadError = true; 718 return; 719 } 720 } else if (Id.size() == 1 && Explicit) { 721 // Top-level modules can't be explicit. 722 Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 723 Explicit = false; 724 ExplicitLoc = SourceLocation(); 725 HadError = true; 726 } 727 728 Module *PreviousActiveModule = ActiveModule; 729 if (Id.size() > 1) { 730 // This module map defines a submodule. Go find the module of which it 731 // is a submodule. 732 ActiveModule = 0; 733 for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 734 if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 735 ActiveModule = Next; 736 continue; 737 } 738 739 if (ActiveModule) { 740 Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 741 << Id[I].first << ActiveModule->getTopLevelModule(); 742 } else { 743 Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 744 } 745 HadError = true; 746 return; 747 } 748 } 749 750 StringRef ModuleName = Id.back().first; 751 SourceLocation ModuleNameLoc = Id.back().second; 752 753 // Parse the opening brace. 754 if (!Tok.is(MMToken::LBrace)) { 755 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 756 << ModuleName; 757 HadError = true; 758 return; 759 } 760 SourceLocation LBraceLoc = consumeToken(); 761 762 // Determine whether this (sub)module has already been defined. 763 llvm::StringMap<Module *> &ModuleSpace 764 = ActiveModule? ActiveModule->SubModules : Map.Modules; 765 llvm::StringMap<Module *>::iterator ExistingModule 766 = ModuleSpace.find(ModuleName); 767 if (ExistingModule != ModuleSpace.end()) { 768 Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 769 << ModuleName; 770 Diags.Report(ExistingModule->getValue()->DefinitionLoc, 771 diag::note_mmap_prev_definition); 772 773 // Skip the module definition. 774 skipUntil(MMToken::RBrace); 775 if (Tok.is(MMToken::RBrace)) 776 consumeToken(); 777 778 HadError = true; 779 return; 780 } 781 782 // Start defining this module. 783 ActiveModule = new Module(ModuleName, ModuleNameLoc, ActiveModule, Framework, 784 Explicit); 785 ModuleSpace[ModuleName] = ActiveModule; 786 787 bool Done = false; 788 do { 789 switch (Tok.Kind) { 790 case MMToken::EndOfFile: 791 case MMToken::RBrace: 792 Done = true; 793 break; 794 795 case MMToken::ExplicitKeyword: 796 case MMToken::FrameworkKeyword: 797 case MMToken::ModuleKeyword: 798 parseModuleDecl(); 799 break; 800 801 case MMToken::ExportKeyword: 802 parseExportDecl(); 803 break; 804 805 case MMToken::UmbrellaKeyword: { 806 SourceLocation UmbrellaLoc = consumeToken(); 807 if (Tok.is(MMToken::HeaderKeyword)) 808 parseHeaderDecl(UmbrellaLoc); 809 else 810 parseUmbrellaDirDecl(UmbrellaLoc); 811 break; 812 } 813 814 case MMToken::HeaderKeyword: 815 parseHeaderDecl(SourceLocation()); 816 break; 817 818 default: 819 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 820 consumeToken(); 821 break; 822 } 823 } while (!Done); 824 825 if (Tok.is(MMToken::RBrace)) 826 consumeToken(); 827 else { 828 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 829 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 830 HadError = true; 831 } 832 833 // We're done parsing this module. Pop back to the previous module. 834 ActiveModule = PreviousActiveModule; 835 } 836 837 /// \brief Append to \p Paths the set of paths needed to get to the 838 /// subframework in which the given module lives. 839 void appendSubframeworkPaths(Module *Mod, llvm::SmallVectorImpl<char> &Path) { 840 // Collect the framework names from the given module to the top-level module. 841 llvm::SmallVector<StringRef, 2> Paths; 842 for (; Mod; Mod = Mod->Parent) { 843 if (Mod->IsFramework) 844 Paths.push_back(Mod->Name); 845 } 846 847 if (Paths.empty()) 848 return; 849 850 // Add Frameworks/Name.framework for each subframework. 851 for (unsigned I = Paths.size() - 1; I != 0; --I) { 852 llvm::sys::path::append(Path, "Frameworks"); 853 llvm::sys::path::append(Path, Paths[I-1] + ".framework"); 854 } 855 } 856 857 /// \brief Parse a header declaration. 858 /// 859 /// header-declaration: 860 /// 'umbrella'[opt] 'header' string-literal 861 void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc) { 862 assert(Tok.is(MMToken::HeaderKeyword)); 863 consumeToken(); 864 865 bool Umbrella = UmbrellaLoc.isValid(); 866 867 // Parse the header name. 868 if (!Tok.is(MMToken::StringLiteral)) { 869 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 870 << "header"; 871 HadError = true; 872 return; 873 } 874 std::string FileName = Tok.getString(); 875 SourceLocation FileNameLoc = consumeToken(); 876 877 // Check whether we already have an umbrella. 878 if (Umbrella && ActiveModule->Umbrella) { 879 Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash) 880 << ActiveModule->getFullModuleName(); 881 HadError = true; 882 return; 883 } 884 885 // Look for this file. 886 const FileEntry *File = 0; 887 llvm::SmallString<128> PathName; 888 if (llvm::sys::path::is_absolute(FileName)) { 889 PathName = FileName; 890 File = SourceMgr.getFileManager().getFile(PathName); 891 } else { 892 // Search for the header file within the search directory. 893 PathName += Directory->getName(); 894 unsigned PathLength = PathName.size(); 895 896 if (ActiveModule->isPartOfFramework()) { 897 appendSubframeworkPaths(ActiveModule, PathName); 898 899 // Check whether this file is in the public headers. 900 llvm::sys::path::append(PathName, "Headers"); 901 llvm::sys::path::append(PathName, FileName); 902 File = SourceMgr.getFileManager().getFile(PathName); 903 904 if (!File) { 905 // Check whether this file is in the private headers. 906 PathName.resize(PathLength); 907 llvm::sys::path::append(PathName, "PrivateHeaders"); 908 llvm::sys::path::append(PathName, FileName); 909 File = SourceMgr.getFileManager().getFile(PathName); 910 } 911 } else { 912 // Lookup for normal headers. 913 llvm::sys::path::append(PathName, FileName); 914 File = SourceMgr.getFileManager().getFile(PathName); 915 } 916 } 917 918 // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 919 // Come up with a lazy way to do this. 920 if (File) { 921 if (const Module *OwningModule = Map.Headers[File]) { 922 Diags.Report(FileNameLoc, diag::err_mmap_header_conflict) 923 << FileName << OwningModule->getFullModuleName(); 924 HadError = true; 925 } else if (Umbrella) { 926 const DirectoryEntry *UmbrellaDir = File->getDir(); 927 if (ActiveModule->IsFramework) { 928 // For framework modules, use the framework directory as the umbrella 929 // directory. 930 UmbrellaDir = SourceMgr.getFileManager().getDirectory( 931 llvm::sys::path::parent_path(UmbrellaDir->getName())); 932 } 933 934 if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) { 935 Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 936 << OwningModule->getFullModuleName(); 937 HadError = true; 938 } else { 939 // Record this umbrella header. 940 Map.setUmbrellaHeader(ActiveModule, File); 941 } 942 } else { 943 // Record this header. 944 Map.addHeader(ActiveModule, File); 945 } 946 } else { 947 Diags.Report(FileNameLoc, diag::err_mmap_header_not_found) 948 << Umbrella << FileName; 949 HadError = true; 950 } 951 } 952 953 /// \brief Parse an umbrella directory declaration. 954 /// 955 /// umbrella-dir-declaration: 956 /// umbrella string-literal 957 void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 958 // Parse the directory name. 959 if (!Tok.is(MMToken::StringLiteral)) { 960 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 961 << "umbrella"; 962 HadError = true; 963 return; 964 } 965 966 std::string DirName = Tok.getString(); 967 SourceLocation DirNameLoc = consumeToken(); 968 969 // Check whether we already have an umbrella. 970 if (ActiveModule->Umbrella) { 971 Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 972 << ActiveModule->getFullModuleName(); 973 HadError = true; 974 return; 975 } 976 977 // Look for this file. 978 const DirectoryEntry *Dir = 0; 979 if (llvm::sys::path::is_absolute(DirName)) 980 Dir = SourceMgr.getFileManager().getDirectory(DirName); 981 else { 982 llvm::SmallString<128> PathName; 983 PathName = Directory->getName(); 984 llvm::sys::path::append(PathName, DirName); 985 Dir = SourceMgr.getFileManager().getDirectory(PathName); 986 } 987 988 if (!Dir) { 989 Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 990 << DirName; 991 HadError = true; 992 return; 993 } 994 995 if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 996 Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 997 << OwningModule->getFullModuleName(); 998 HadError = true; 999 return; 1000 } 1001 1002 // Record this umbrella directory. 1003 Map.setUmbrellaDir(ActiveModule, Dir); 1004 } 1005 1006 /// \brief Parse a module export declaration. 1007 /// 1008 /// export-declaration: 1009 /// 'export' wildcard-module-id 1010 /// 1011 /// wildcard-module-id: 1012 /// identifier 1013 /// '*' 1014 /// identifier '.' wildcard-module-id 1015 void ModuleMapParser::parseExportDecl() { 1016 assert(Tok.is(MMToken::ExportKeyword)); 1017 SourceLocation ExportLoc = consumeToken(); 1018 1019 // Parse the module-id with an optional wildcard at the end. 1020 ModuleId ParsedModuleId; 1021 bool Wildcard = false; 1022 do { 1023 if (Tok.is(MMToken::Identifier)) { 1024 ParsedModuleId.push_back(std::make_pair(Tok.getString(), 1025 Tok.getLocation())); 1026 consumeToken(); 1027 1028 if (Tok.is(MMToken::Period)) { 1029 consumeToken(); 1030 continue; 1031 } 1032 1033 break; 1034 } 1035 1036 if(Tok.is(MMToken::Star)) { 1037 Wildcard = true; 1038 consumeToken(); 1039 break; 1040 } 1041 1042 Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id); 1043 HadError = true; 1044 return; 1045 } while (true); 1046 1047 Module::UnresolvedExportDecl Unresolved = { 1048 ExportLoc, ParsedModuleId, Wildcard 1049 }; 1050 ActiveModule->UnresolvedExports.push_back(Unresolved); 1051 } 1052 1053 void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) { 1054 assert(Tok.is(MMToken::Star)); 1055 SourceLocation StarLoc = consumeToken(); 1056 bool Failed = false; 1057 1058 // Inferred modules must be submodules. 1059 if (!ActiveModule) { 1060 Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 1061 Failed = true; 1062 } 1063 1064 // Inferred modules must have umbrella directories. 1065 if (!Failed && !ActiveModule->getUmbrellaDir()) { 1066 Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 1067 Failed = true; 1068 } 1069 1070 // Check for redefinition of an inferred module. 1071 if (!Failed && ActiveModule->InferSubmodules) { 1072 Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 1073 if (ActiveModule->InferredSubmoduleLoc.isValid()) 1074 Diags.Report(ActiveModule->InferredSubmoduleLoc, 1075 diag::note_mmap_prev_definition); 1076 Failed = true; 1077 } 1078 1079 // If there were any problems with this inferred submodule, skip its body. 1080 if (Failed) { 1081 if (Tok.is(MMToken::LBrace)) { 1082 consumeToken(); 1083 skipUntil(MMToken::RBrace); 1084 if (Tok.is(MMToken::RBrace)) 1085 consumeToken(); 1086 } 1087 HadError = true; 1088 return; 1089 } 1090 1091 // Note that we have an inferred submodule. 1092 ActiveModule->InferSubmodules = true; 1093 ActiveModule->InferredSubmoduleLoc = StarLoc; 1094 ActiveModule->InferExplicitSubmodules = Explicit; 1095 1096 // Parse the opening brace. 1097 if (!Tok.is(MMToken::LBrace)) { 1098 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 1099 HadError = true; 1100 return; 1101 } 1102 SourceLocation LBraceLoc = consumeToken(); 1103 1104 // Parse the body of the inferred submodule. 1105 bool Done = false; 1106 do { 1107 switch (Tok.Kind) { 1108 case MMToken::EndOfFile: 1109 case MMToken::RBrace: 1110 Done = true; 1111 break; 1112 1113 case MMToken::ExportKeyword: { 1114 consumeToken(); 1115 if (Tok.is(MMToken::Star)) 1116 ActiveModule->InferExportWildcard = true; 1117 else 1118 Diags.Report(Tok.getLocation(), 1119 diag::err_mmap_expected_export_wildcard); 1120 consumeToken(); 1121 break; 1122 } 1123 1124 case MMToken::ExplicitKeyword: 1125 case MMToken::ModuleKeyword: 1126 case MMToken::HeaderKeyword: 1127 case MMToken::UmbrellaKeyword: 1128 default: 1129 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member); 1130 consumeToken(); 1131 break; 1132 } 1133 } while (!Done); 1134 1135 if (Tok.is(MMToken::RBrace)) 1136 consumeToken(); 1137 else { 1138 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1139 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1140 HadError = true; 1141 } 1142 } 1143 1144 /// \brief Parse a module map file. 1145 /// 1146 /// module-map-file: 1147 /// module-declaration* 1148 bool ModuleMapParser::parseModuleMapFile() { 1149 do { 1150 switch (Tok.Kind) { 1151 case MMToken::EndOfFile: 1152 return HadError; 1153 1154 case MMToken::ExplicitKeyword: 1155 case MMToken::ModuleKeyword: 1156 case MMToken::FrameworkKeyword: 1157 parseModuleDecl(); 1158 break; 1159 1160 case MMToken::ExportKeyword: 1161 case MMToken::HeaderKeyword: 1162 case MMToken::Identifier: 1163 case MMToken::LBrace: 1164 case MMToken::Period: 1165 case MMToken::RBrace: 1166 case MMToken::Star: 1167 case MMToken::StringLiteral: 1168 case MMToken::UmbrellaKeyword: 1169 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1170 HadError = true; 1171 consumeToken(); 1172 break; 1173 } 1174 } while (true); 1175 1176 return HadError; 1177 } 1178 1179 bool ModuleMap::parseModuleMapFile(const FileEntry *File) { 1180 FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User); 1181 const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID); 1182 if (!Buffer) 1183 return true; 1184 1185 // Parse this module map file. 1186 Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, LangOpts); 1187 Diags->getClient()->BeginSourceFile(LangOpts); 1188 ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir()); 1189 bool Result = Parser.parseModuleMapFile(); 1190 Diags->getClient()->EndSourceFile(); 1191 1192 return Result; 1193 } 1194