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