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