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