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