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