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