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::addHeader(Module *Mod, const FileEntry *Header) { 350 Mod->Headers.push_back(Header); 351 Headers[Header] = Mod; 352 } 353 354 const FileEntry * 355 ModuleMap::getContainingModuleMapFile(Module *Module) { 356 if (Module->DefinitionLoc.isInvalid() || !SourceMgr) 357 return 0; 358 359 return SourceMgr->getFileEntryForID( 360 SourceMgr->getFileID(Module->DefinitionLoc)); 361 } 362 363 void ModuleMap::dump() { 364 llvm::errs() << "Modules:"; 365 for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 366 MEnd = Modules.end(); 367 M != MEnd; ++M) 368 M->getValue()->print(llvm::errs(), 2); 369 370 llvm::errs() << "Headers:"; 371 for (llvm::DenseMap<const FileEntry *, Module *>::iterator 372 H = Headers.begin(), 373 HEnd = Headers.end(); 374 H != HEnd; ++H) { 375 llvm::errs() << " \"" << H->first->getName() << "\" -> " 376 << H->second->getFullModuleName() << "\n"; 377 } 378 } 379 380 bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 381 bool HadError = false; 382 for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { 383 Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 384 Complain); 385 if (Export.getPointer() || Export.getInt()) 386 Mod->Exports.push_back(Export); 387 else 388 HadError = true; 389 } 390 Mod->UnresolvedExports.clear(); 391 return HadError; 392 } 393 394 Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 395 if (Loc.isInvalid()) 396 return 0; 397 398 // Use the expansion location to determine which module we're in. 399 FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 400 if (!ExpansionLoc.isFileID()) 401 return 0; 402 403 404 const SourceManager &SrcMgr = Loc.getManager(); 405 FileID ExpansionFileID = ExpansionLoc.getFileID(); 406 const FileEntry *ExpansionFile = SrcMgr.getFileEntryForID(ExpansionFileID); 407 if (!ExpansionFile) 408 return 0; 409 410 // Find the module that owns this header. 411 return findModuleForHeader(ExpansionFile); 412 } 413 414 //----------------------------------------------------------------------------// 415 // Module map file parser 416 //----------------------------------------------------------------------------// 417 418 namespace clang { 419 /// \brief A token in a module map file. 420 struct MMToken { 421 enum TokenKind { 422 EndOfFile, 423 HeaderKeyword, 424 Identifier, 425 ExplicitKeyword, 426 ExportKeyword, 427 FrameworkKeyword, 428 ModuleKeyword, 429 Period, 430 UmbrellaKeyword, 431 Star, 432 StringLiteral, 433 LBrace, 434 RBrace 435 } Kind; 436 437 unsigned Location; 438 unsigned StringLength; 439 const char *StringData; 440 441 void clear() { 442 Kind = EndOfFile; 443 Location = 0; 444 StringLength = 0; 445 StringData = 0; 446 } 447 448 bool is(TokenKind K) const { return Kind == K; } 449 450 SourceLocation getLocation() const { 451 return SourceLocation::getFromRawEncoding(Location); 452 } 453 454 StringRef getString() const { 455 return StringRef(StringData, StringLength); 456 } 457 }; 458 459 class ModuleMapParser { 460 Lexer &L; 461 SourceManager &SourceMgr; 462 DiagnosticsEngine &Diags; 463 ModuleMap ⤅ 464 465 /// \brief The directory that this module map resides in. 466 const DirectoryEntry *Directory; 467 468 /// \brief Whether an error occurred. 469 bool HadError; 470 471 /// \brief Default target information, used only for string literal 472 /// parsing. 473 TargetInfo *Target; 474 475 /// \brief Stores string data for the various string literals referenced 476 /// during parsing. 477 llvm::BumpPtrAllocator StringData; 478 479 /// \brief The current token. 480 MMToken Tok; 481 482 /// \brief The active module. 483 Module *ActiveModule; 484 485 /// \brief Consume the current token and return its location. 486 SourceLocation consumeToken(); 487 488 /// \brief Skip tokens until we reach the a token with the given kind 489 /// (or the end of the file). 490 void skipUntil(MMToken::TokenKind K); 491 492 typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2> 493 ModuleId; 494 bool parseModuleId(ModuleId &Id); 495 void parseModuleDecl(); 496 void parseHeaderDecl(SourceLocation UmbrellaLoc); 497 void parseExportDecl(); 498 void parseInferredSubmoduleDecl(bool Explicit); 499 500 public: 501 explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 502 DiagnosticsEngine &Diags, 503 ModuleMap &Map, 504 const DirectoryEntry *Directory) 505 : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map), 506 Directory(Directory), HadError(false), ActiveModule(0) 507 { 508 TargetOptions TargetOpts; 509 TargetOpts.Triple = llvm::sys::getDefaultTargetTriple(); 510 Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts); 511 512 Tok.clear(); 513 consumeToken(); 514 } 515 516 bool parseModuleMapFile(); 517 }; 518 } 519 520 SourceLocation ModuleMapParser::consumeToken() { 521 retry: 522 SourceLocation Result = Tok.getLocation(); 523 Tok.clear(); 524 525 Token LToken; 526 L.LexFromRawLexer(LToken); 527 Tok.Location = LToken.getLocation().getRawEncoding(); 528 switch (LToken.getKind()) { 529 case tok::raw_identifier: 530 Tok.StringData = LToken.getRawIdentifierData(); 531 Tok.StringLength = LToken.getLength(); 532 Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString()) 533 .Case("header", MMToken::HeaderKeyword) 534 .Case("explicit", MMToken::ExplicitKeyword) 535 .Case("export", MMToken::ExportKeyword) 536 .Case("framework", MMToken::FrameworkKeyword) 537 .Case("module", MMToken::ModuleKeyword) 538 .Case("umbrella", MMToken::UmbrellaKeyword) 539 .Default(MMToken::Identifier); 540 break; 541 542 case tok::eof: 543 Tok.Kind = MMToken::EndOfFile; 544 break; 545 546 case tok::l_brace: 547 Tok.Kind = MMToken::LBrace; 548 break; 549 550 case tok::period: 551 Tok.Kind = MMToken::Period; 552 break; 553 554 case tok::r_brace: 555 Tok.Kind = MMToken::RBrace; 556 break; 557 558 case tok::star: 559 Tok.Kind = MMToken::Star; 560 break; 561 562 case tok::string_literal: { 563 // Parse the string literal. 564 LangOptions LangOpts; 565 StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target); 566 if (StringLiteral.hadError) 567 goto retry; 568 569 // Copy the string literal into our string data allocator. 570 unsigned Length = StringLiteral.GetStringLength(); 571 char *Saved = StringData.Allocate<char>(Length + 1); 572 memcpy(Saved, StringLiteral.GetString().data(), Length); 573 Saved[Length] = 0; 574 575 // Form the token. 576 Tok.Kind = MMToken::StringLiteral; 577 Tok.StringData = Saved; 578 Tok.StringLength = Length; 579 break; 580 } 581 582 case tok::comment: 583 goto retry; 584 585 default: 586 Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 587 HadError = true; 588 goto retry; 589 } 590 591 return Result; 592 } 593 594 void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 595 unsigned braceDepth = 0; 596 do { 597 switch (Tok.Kind) { 598 case MMToken::EndOfFile: 599 return; 600 601 case MMToken::LBrace: 602 if (Tok.is(K) && braceDepth == 0) 603 return; 604 605 ++braceDepth; 606 break; 607 608 case MMToken::RBrace: 609 if (braceDepth > 0) 610 --braceDepth; 611 else if (Tok.is(K)) 612 return; 613 break; 614 615 default: 616 if (braceDepth == 0 && Tok.is(K)) 617 return; 618 break; 619 } 620 621 consumeToken(); 622 } while (true); 623 } 624 625 /// \brief Parse a module-id. 626 /// 627 /// module-id: 628 /// identifier 629 /// identifier '.' module-id 630 /// 631 /// \returns true if an error occurred, false otherwise. 632 bool ModuleMapParser::parseModuleId(ModuleId &Id) { 633 Id.clear(); 634 do { 635 if (Tok.is(MMToken::Identifier)) { 636 Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 637 consumeToken(); 638 } else { 639 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 640 return true; 641 } 642 643 if (!Tok.is(MMToken::Period)) 644 break; 645 646 consumeToken(); 647 } while (true); 648 649 return false; 650 } 651 652 /// \brief Parse a module declaration. 653 /// 654 /// module-declaration: 655 /// 'explicit'[opt] 'framework'[opt] 'module' module-id { module-member* } 656 /// 657 /// module-member: 658 /// header-declaration 659 /// submodule-declaration 660 /// export-declaration 661 /// 662 /// submodule-declaration: 663 /// module-declaration 664 /// inferred-submodule-declaration 665 void ModuleMapParser::parseModuleDecl() { 666 assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 667 Tok.is(MMToken::FrameworkKeyword)); 668 // Parse 'explicit' or 'framework' keyword, if present. 669 SourceLocation ExplicitLoc; 670 bool Explicit = false; 671 bool Framework = false; 672 673 // Parse 'explicit' keyword, if present. 674 if (Tok.is(MMToken::ExplicitKeyword)) { 675 ExplicitLoc = consumeToken(); 676 Explicit = true; 677 } 678 679 // Parse 'framework' keyword, if present. 680 if (Tok.is(MMToken::FrameworkKeyword)) { 681 consumeToken(); 682 Framework = true; 683 } 684 685 // Parse 'module' keyword. 686 if (!Tok.is(MMToken::ModuleKeyword)) { 687 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 688 consumeToken(); 689 HadError = true; 690 return; 691 } 692 consumeToken(); // 'module' keyword 693 694 // If we have a wildcard for the module name, this is an inferred submodule. 695 // Parse it. 696 if (Tok.is(MMToken::Star)) 697 return parseInferredSubmoduleDecl(Explicit); 698 699 // Parse the module name. 700 ModuleId Id; 701 if (parseModuleId(Id)) { 702 HadError = true; 703 return; 704 } 705 706 if (ActiveModule) { 707 if (Id.size() > 1) { 708 Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 709 << SourceRange(Id.front().second, Id.back().second); 710 711 HadError = true; 712 return; 713 } 714 } else if (Id.size() == 1 && Explicit) { 715 // Top-level modules can't be explicit. 716 Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 717 Explicit = false; 718 ExplicitLoc = SourceLocation(); 719 HadError = true; 720 } 721 722 Module *PreviousActiveModule = ActiveModule; 723 if (Id.size() > 1) { 724 // This module map defines a submodule. Go find the module of which it 725 // is a submodule. 726 ActiveModule = 0; 727 for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 728 if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 729 ActiveModule = Next; 730 continue; 731 } 732 733 if (ActiveModule) { 734 Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 735 << Id[I].first << ActiveModule->getTopLevelModule(); 736 } else { 737 Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 738 } 739 HadError = true; 740 return; 741 } 742 } 743 744 StringRef ModuleName = Id.back().first; 745 SourceLocation ModuleNameLoc = Id.back().second; 746 747 // Parse the opening brace. 748 if (!Tok.is(MMToken::LBrace)) { 749 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 750 << ModuleName; 751 HadError = true; 752 return; 753 } 754 SourceLocation LBraceLoc = consumeToken(); 755 756 // Determine whether this (sub)module has already been defined. 757 llvm::StringMap<Module *> &ModuleSpace 758 = ActiveModule? ActiveModule->SubModules : Map.Modules; 759 llvm::StringMap<Module *>::iterator ExistingModule 760 = ModuleSpace.find(ModuleName); 761 if (ExistingModule != ModuleSpace.end()) { 762 Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 763 << ModuleName; 764 Diags.Report(ExistingModule->getValue()->DefinitionLoc, 765 diag::note_mmap_prev_definition); 766 767 // Skip the module definition. 768 skipUntil(MMToken::RBrace); 769 if (Tok.is(MMToken::RBrace)) 770 consumeToken(); 771 772 HadError = true; 773 return; 774 } 775 776 // Start defining this module. 777 ActiveModule = new Module(ModuleName, ModuleNameLoc, ActiveModule, Framework, 778 Explicit); 779 ModuleSpace[ModuleName] = ActiveModule; 780 781 bool Done = false; 782 do { 783 switch (Tok.Kind) { 784 case MMToken::EndOfFile: 785 case MMToken::RBrace: 786 Done = true; 787 break; 788 789 case MMToken::ExplicitKeyword: 790 case MMToken::FrameworkKeyword: 791 case MMToken::ModuleKeyword: 792 parseModuleDecl(); 793 break; 794 795 case MMToken::ExportKeyword: 796 parseExportDecl(); 797 break; 798 799 case MMToken::UmbrellaKeyword: 800 parseHeaderDecl(consumeToken()); 801 break; 802 803 case MMToken::HeaderKeyword: 804 parseHeaderDecl(SourceLocation()); 805 break; 806 807 default: 808 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 809 consumeToken(); 810 break; 811 } 812 } while (!Done); 813 814 if (Tok.is(MMToken::RBrace)) 815 consumeToken(); 816 else { 817 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 818 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 819 HadError = true; 820 } 821 822 // We're done parsing this module. Pop back to the previous module. 823 ActiveModule = PreviousActiveModule; 824 } 825 826 /// \brief Append to \p Paths the set of paths needed to get to the 827 /// subframework in which the given module lives. 828 void appendSubframeworkPaths(Module *Mod, llvm::SmallVectorImpl<char> &Path) { 829 // Collect the framework names from the given module to the top-level module. 830 llvm::SmallVector<StringRef, 2> Paths; 831 for (; Mod; Mod = Mod->Parent) { 832 if (Mod->IsFramework) 833 Paths.push_back(Mod->Name); 834 } 835 836 if (Paths.empty()) 837 return; 838 839 // Add Frameworks/Name.framework for each subframework. 840 for (unsigned I = Paths.size() - 1; I != 0; --I) { 841 llvm::sys::path::append(Path, "Frameworks"); 842 llvm::sys::path::append(Path, Paths[I-1] + ".framework"); 843 } 844 } 845 846 /// \brief Parse a header declaration. 847 /// 848 /// header-declaration: 849 /// 'umbrella'[opt] 'header' string-literal 850 void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc) { 851 assert(Tok.is(MMToken::HeaderKeyword)); 852 consumeToken(); 853 854 bool Umbrella = UmbrellaLoc.isValid(); 855 856 // Parse the header name. 857 if (!Tok.is(MMToken::StringLiteral)) { 858 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 859 << "header"; 860 HadError = true; 861 return; 862 } 863 std::string FileName = Tok.getString(); 864 SourceLocation FileNameLoc = consumeToken(); 865 866 // Check whether we already have an umbrella header. 867 if (Umbrella && ActiveModule->getUmbrellaHeader()) { 868 Diags.Report(FileNameLoc, diag::err_mmap_umbrella_header_conflict) 869 << ActiveModule->getFullModuleName() 870 << ActiveModule->getUmbrellaHeader()->getName(); 871 HadError = true; 872 return; 873 } 874 875 // Look for this file. 876 const FileEntry *File = 0; 877 llvm::SmallString<128> PathName; 878 if (llvm::sys::path::is_absolute(FileName)) { 879 PathName = FileName; 880 File = SourceMgr.getFileManager().getFile(PathName); 881 } else { 882 // Search for the header file within the search directory. 883 PathName += Directory->getName(); 884 unsigned PathLength = PathName.size(); 885 886 if (ActiveModule->isPartOfFramework()) { 887 appendSubframeworkPaths(ActiveModule, PathName); 888 889 // Check whether this file is in the public headers. 890 llvm::sys::path::append(PathName, "Headers"); 891 llvm::sys::path::append(PathName, FileName); 892 File = SourceMgr.getFileManager().getFile(PathName); 893 894 if (!File) { 895 // Check whether this file is in the private headers. 896 PathName.resize(PathLength); 897 llvm::sys::path::append(PathName, "PrivateHeaders"); 898 llvm::sys::path::append(PathName, FileName); 899 File = SourceMgr.getFileManager().getFile(PathName); 900 } 901 } else { 902 // Lookup for normal headers. 903 llvm::sys::path::append(PathName, FileName); 904 File = SourceMgr.getFileManager().getFile(PathName); 905 } 906 } 907 908 // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 909 // Come up with a lazy way to do this. 910 if (File) { 911 if (const Module *OwningModule = Map.Headers[File]) { 912 Diags.Report(FileNameLoc, diag::err_mmap_header_conflict) 913 << FileName << OwningModule->getFullModuleName(); 914 HadError = true; 915 } else if (Umbrella) { 916 const DirectoryEntry *UmbrellaDir = File->getDir(); 917 if (ActiveModule->IsFramework) { 918 // For framework modules, use the framework directory as the umbrella 919 // directory. 920 UmbrellaDir = SourceMgr.getFileManager().getDirectory( 921 llvm::sys::path::parent_path(UmbrellaDir->getName())); 922 } 923 924 if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) { 925 Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 926 << OwningModule->getFullModuleName(); 927 HadError = true; 928 } else { 929 // Record this umbrella header. 930 Map.setUmbrellaHeader(ActiveModule, File); 931 } 932 } else { 933 // Record this header. 934 Map.addHeader(ActiveModule, File); 935 } 936 } else { 937 Diags.Report(FileNameLoc, diag::err_mmap_header_not_found) 938 << false << FileName; 939 HadError = true; 940 } 941 } 942 943 /// \brief Parse a module export declaration. 944 /// 945 /// export-declaration: 946 /// 'export' wildcard-module-id 947 /// 948 /// wildcard-module-id: 949 /// identifier 950 /// '*' 951 /// identifier '.' wildcard-module-id 952 void ModuleMapParser::parseExportDecl() { 953 assert(Tok.is(MMToken::ExportKeyword)); 954 SourceLocation ExportLoc = consumeToken(); 955 956 // Parse the module-id with an optional wildcard at the end. 957 ModuleId ParsedModuleId; 958 bool Wildcard = false; 959 do { 960 if (Tok.is(MMToken::Identifier)) { 961 ParsedModuleId.push_back(std::make_pair(Tok.getString(), 962 Tok.getLocation())); 963 consumeToken(); 964 965 if (Tok.is(MMToken::Period)) { 966 consumeToken(); 967 continue; 968 } 969 970 break; 971 } 972 973 if(Tok.is(MMToken::Star)) { 974 Wildcard = true; 975 consumeToken(); 976 break; 977 } 978 979 Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id); 980 HadError = true; 981 return; 982 } while (true); 983 984 Module::UnresolvedExportDecl Unresolved = { 985 ExportLoc, ParsedModuleId, Wildcard 986 }; 987 ActiveModule->UnresolvedExports.push_back(Unresolved); 988 } 989 990 void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) { 991 assert(Tok.is(MMToken::Star)); 992 SourceLocation StarLoc = consumeToken(); 993 bool Failed = false; 994 995 // Inferred modules must be submodules. 996 if (!ActiveModule) { 997 Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 998 Failed = true; 999 } 1000 1001 // Inferred modules must have umbrella headers. 1002 if (!Failed && !ActiveModule->getUmbrellaHeader()) { 1003 Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 1004 Failed = true; 1005 } 1006 1007 // Check for redefinition of an inferred module. 1008 if (!Failed && ActiveModule->InferSubmodules) { 1009 Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 1010 if (ActiveModule->InferredSubmoduleLoc.isValid()) 1011 Diags.Report(ActiveModule->InferredSubmoduleLoc, 1012 diag::note_mmap_prev_definition); 1013 Failed = true; 1014 } 1015 1016 // If there were any problems with this inferred submodule, skip its body. 1017 if (Failed) { 1018 if (Tok.is(MMToken::LBrace)) { 1019 consumeToken(); 1020 skipUntil(MMToken::RBrace); 1021 if (Tok.is(MMToken::RBrace)) 1022 consumeToken(); 1023 } 1024 HadError = true; 1025 return; 1026 } 1027 1028 // Note that we have an inferred submodule. 1029 ActiveModule->InferSubmodules = true; 1030 ActiveModule->InferredSubmoduleLoc = StarLoc; 1031 ActiveModule->InferExplicitSubmodules = Explicit; 1032 1033 // Parse the opening brace. 1034 if (!Tok.is(MMToken::LBrace)) { 1035 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 1036 HadError = true; 1037 return; 1038 } 1039 SourceLocation LBraceLoc = consumeToken(); 1040 1041 // Parse the body of the inferred submodule. 1042 bool Done = false; 1043 do { 1044 switch (Tok.Kind) { 1045 case MMToken::EndOfFile: 1046 case MMToken::RBrace: 1047 Done = true; 1048 break; 1049 1050 case MMToken::ExportKeyword: { 1051 consumeToken(); 1052 if (Tok.is(MMToken::Star)) 1053 ActiveModule->InferExportWildcard = true; 1054 else 1055 Diags.Report(Tok.getLocation(), 1056 diag::err_mmap_expected_export_wildcard); 1057 consumeToken(); 1058 break; 1059 } 1060 1061 case MMToken::ExplicitKeyword: 1062 case MMToken::ModuleKeyword: 1063 case MMToken::HeaderKeyword: 1064 case MMToken::UmbrellaKeyword: 1065 default: 1066 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member); 1067 consumeToken(); 1068 break; 1069 } 1070 } while (!Done); 1071 1072 if (Tok.is(MMToken::RBrace)) 1073 consumeToken(); 1074 else { 1075 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1076 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1077 HadError = true; 1078 } 1079 } 1080 1081 /// \brief Parse a module map file. 1082 /// 1083 /// module-map-file: 1084 /// module-declaration* 1085 bool ModuleMapParser::parseModuleMapFile() { 1086 do { 1087 switch (Tok.Kind) { 1088 case MMToken::EndOfFile: 1089 return HadError; 1090 1091 case MMToken::ExplicitKeyword: 1092 case MMToken::ModuleKeyword: 1093 case MMToken::FrameworkKeyword: 1094 parseModuleDecl(); 1095 break; 1096 1097 case MMToken::ExportKeyword: 1098 case MMToken::HeaderKeyword: 1099 case MMToken::Identifier: 1100 case MMToken::LBrace: 1101 case MMToken::Period: 1102 case MMToken::RBrace: 1103 case MMToken::Star: 1104 case MMToken::StringLiteral: 1105 case MMToken::UmbrellaKeyword: 1106 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1107 HadError = true; 1108 consumeToken(); 1109 break; 1110 } 1111 } while (true); 1112 1113 return HadError; 1114 } 1115 1116 bool ModuleMap::parseModuleMapFile(const FileEntry *File) { 1117 FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User); 1118 const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID); 1119 if (!Buffer) 1120 return true; 1121 1122 // Parse this module map file. 1123 Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, LangOpts); 1124 Diags->getClient()->BeginSourceFile(LangOpts); 1125 ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir()); 1126 bool Result = Parser.parseModuleMapFile(); 1127 Diags->getClient()->EndSourceFile(); 1128 1129 return Result; 1130 } 1131