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