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 static bool violatesPrivateInclude(Module *RequestingModule, 209 const FileEntry *IncFileEnt, 210 ModuleMap::ModuleHeaderRole Role, 211 Module *RequestedModule) { 212 bool IsPrivateRole = Role & ModuleMap::PrivateHeader; 213 #ifndef NDEBUG 214 if (IsPrivateRole) { 215 // Check for consistency between the module header role 216 // as obtained from the lookup and as obtained from the module. 217 // This check is not cheap, so enable it only for debugging. 218 bool IsPrivate = false; 219 SmallVectorImpl<Module::Header> *HeaderList[] = { 220 &RequestedModule->Headers[Module::HK_Private], 221 &RequestedModule->Headers[Module::HK_PrivateTextual]}; 222 for (auto *Hs : HeaderList) 223 IsPrivate |= 224 std::find_if(Hs->begin(), Hs->end(), [&](const Module::Header &H) { 225 return H.Entry == IncFileEnt; 226 }) != Hs->end(); 227 assert((!IsPrivateRole || IsPrivate) && "inconsistent headers and roles"); 228 } 229 #endif 230 return IsPrivateRole && 231 // FIXME: Should we map RequestingModule to its top-level module here 232 // too? This check is redundant with the isSubModuleOf check in 233 // diagnoseHeaderInclusion. 234 RequestedModule->getTopLevelModule() != RequestingModule; 235 } 236 237 static Module *getTopLevelOrNull(Module *M) { 238 return M ? M->getTopLevelModule() : nullptr; 239 } 240 241 void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule, 242 SourceLocation FilenameLoc, 243 StringRef Filename, 244 const FileEntry *File) { 245 // No errors for indirect modules. This may be a bit of a problem for modules 246 // with no source files. 247 if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule)) 248 return; 249 250 if (RequestingModule) 251 resolveUses(RequestingModule, /*Complain=*/false); 252 253 bool Excluded = false; 254 Module *Private = nullptr; 255 Module *NotUsed = nullptr; 256 257 HeadersMap::iterator Known = findKnownHeader(File); 258 if (Known != Headers.end()) { 259 for (const KnownHeader &Header : Known->second) { 260 // If 'File' is part of 'RequestingModule' we can definitely include it. 261 if (Header.getModule() && 262 Header.getModule()->isSubModuleOf(RequestingModule)) 263 return; 264 265 // Remember private headers for later printing of a diagnostic. 266 if (violatesPrivateInclude(RequestingModule, File, Header.getRole(), 267 Header.getModule())) { 268 Private = Header.getModule(); 269 continue; 270 } 271 272 // If uses need to be specified explicitly, we are only allowed to return 273 // modules that are explicitly used by the requesting module. 274 if (RequestingModule && LangOpts.ModulesDeclUse && 275 !RequestingModule->directlyUses(Header.getModule())) { 276 NotUsed = Header.getModule(); 277 continue; 278 } 279 280 // We have found a module that we can happily use. 281 return; 282 } 283 284 Excluded = true; 285 } 286 287 // We have found a header, but it is private. 288 if (Private) { 289 Diags.Report(FilenameLoc, diag::warn_use_of_private_header_outside_module) 290 << Filename; 291 return; 292 } 293 294 // We have found a module, but we don't use it. 295 if (NotUsed) { 296 Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module) 297 << RequestingModule->getFullModuleName() << Filename; 298 return; 299 } 300 301 if (Excluded || isHeaderInUmbrellaDirs(File)) 302 return; 303 304 // At this point, only non-modular includes remain. 305 306 if (LangOpts.ModulesStrictDeclUse) { 307 Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module) 308 << RequestingModule->getFullModuleName() << Filename; 309 } else if (RequestingModule) { 310 diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ? 311 diag::warn_non_modular_include_in_framework_module : 312 diag::warn_non_modular_include_in_module; 313 Diags.Report(FilenameLoc, DiagID) << RequestingModule->getFullModuleName(); 314 } 315 } 316 317 static bool isBetterKnownHeader(const ModuleMap::KnownHeader &New, 318 const ModuleMap::KnownHeader &Old) { 319 // Prefer a public header over a private header. 320 if ((New.getRole() & ModuleMap::PrivateHeader) != 321 (Old.getRole() & ModuleMap::PrivateHeader)) 322 return !(New.getRole() & ModuleMap::PrivateHeader); 323 324 // Prefer a non-textual header over a textual header. 325 if ((New.getRole() & ModuleMap::TextualHeader) != 326 (Old.getRole() & ModuleMap::TextualHeader)) 327 return !(New.getRole() & ModuleMap::TextualHeader); 328 329 // Don't have a reason to choose between these. Just keep the first one. 330 return false; 331 } 332 333 ModuleMap::KnownHeader 334 ModuleMap::findModuleForHeader(const FileEntry *File, 335 Module *RequestingModule, 336 bool IncludeTextualHeaders) { 337 HeadersMap::iterator Known = findKnownHeader(File); 338 339 auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader { 340 if (!IncludeTextualHeaders && (R.getRole() & ModuleMap::TextualHeader)) 341 return ModuleMap::KnownHeader(); 342 return R; 343 }; 344 345 if (Known != Headers.end()) { 346 ModuleMap::KnownHeader Result; 347 348 // Iterate over all modules that 'File' is part of to find the best fit. 349 for (SmallVectorImpl<KnownHeader>::iterator I = Known->second.begin(), 350 E = Known->second.end(); 351 I != E; ++I) { 352 // Cannot use a module if it is unavailable. 353 if (!I->getModule()->isAvailable()) 354 continue; 355 356 // If 'File' is part of 'RequestingModule', 'RequestingModule' is the 357 // module we are looking for. 358 if (I->getModule() == RequestingModule) 359 return MakeResult(*I); 360 361 // If uses need to be specified explicitly, we are only allowed to return 362 // modules that are explicitly used by the requesting module. 363 if (RequestingModule && LangOpts.ModulesDeclUse && 364 !RequestingModule->directlyUses(I->getModule())) 365 continue; 366 367 if (!Result || isBetterKnownHeader(*I, Result)) 368 Result = *I; 369 } 370 return MakeResult(Result); 371 } 372 373 SmallVector<const DirectoryEntry *, 2> SkippedDirs; 374 KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs); 375 if (H) { 376 Module *Result = H.getModule(); 377 378 // Search up the module stack until we find a module with an umbrella 379 // directory. 380 Module *UmbrellaModule = Result; 381 while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 382 UmbrellaModule = UmbrellaModule->Parent; 383 384 if (UmbrellaModule->InferSubmodules) { 385 const FileEntry *UmbrellaModuleMap = 386 getModuleMapFileForUniquing(UmbrellaModule); 387 388 // Infer submodules for each of the directories we found between 389 // the directory of the umbrella header and the directory where 390 // the actual header is located. 391 bool Explicit = UmbrellaModule->InferExplicitSubmodules; 392 393 for (unsigned I = SkippedDirs.size(); I != 0; --I) { 394 // Find or create the module that corresponds to this directory name. 395 SmallString<32> NameBuf; 396 StringRef Name = sanitizeFilenameAsIdentifier( 397 llvm::sys::path::stem(SkippedDirs[I-1]->getName()), NameBuf); 398 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 399 Explicit).first; 400 InferredModuleAllowedBy[Result] = UmbrellaModuleMap; 401 Result->IsInferred = true; 402 403 // Associate the module and the directory. 404 UmbrellaDirs[SkippedDirs[I-1]] = Result; 405 406 // If inferred submodules export everything they import, add a 407 // wildcard to the set of exports. 408 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 409 Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 410 } 411 412 // Infer a submodule with the same name as this header file. 413 SmallString<32> NameBuf; 414 StringRef Name = sanitizeFilenameAsIdentifier( 415 llvm::sys::path::stem(File->getName()), NameBuf); 416 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 417 Explicit).first; 418 InferredModuleAllowedBy[Result] = UmbrellaModuleMap; 419 Result->IsInferred = true; 420 Result->addTopHeader(File); 421 422 // If inferred submodules export everything they import, add a 423 // wildcard to the set of exports. 424 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 425 Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 426 } else { 427 // Record each of the directories we stepped through as being part of 428 // the module we found, since the umbrella header covers them all. 429 for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 430 UmbrellaDirs[SkippedDirs[I]] = Result; 431 } 432 433 Headers[File].push_back(KnownHeader(Result, NormalHeader)); 434 435 // If a header corresponds to an unavailable module, don't report 436 // that it maps to anything. 437 if (!Result->isAvailable()) 438 return KnownHeader(); 439 440 return MakeResult(Headers[File].back()); 441 } 442 443 return KnownHeader(); 444 } 445 446 bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const { 447 return isHeaderUnavailableInModule(Header, nullptr); 448 } 449 450 bool 451 ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header, 452 const Module *RequestingModule) const { 453 HeadersMap::const_iterator Known = Headers.find(Header); 454 if (Known != Headers.end()) { 455 for (SmallVectorImpl<KnownHeader>::const_iterator 456 I = Known->second.begin(), 457 E = Known->second.end(); 458 I != E; ++I) { 459 if (I->isAvailable() && (!RequestingModule || 460 I->getModule()->isSubModuleOf(RequestingModule))) 461 return false; 462 } 463 return true; 464 } 465 466 const DirectoryEntry *Dir = Header->getDir(); 467 SmallVector<const DirectoryEntry *, 2> SkippedDirs; 468 StringRef DirName = Dir->getName(); 469 470 auto IsUnavailable = [&](const Module *M) { 471 return !M->isAvailable() && (!RequestingModule || 472 M->isSubModuleOf(RequestingModule)); 473 }; 474 475 // Keep walking up the directory hierarchy, looking for a directory with 476 // an umbrella header. 477 do { 478 llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir 479 = UmbrellaDirs.find(Dir); 480 if (KnownDir != UmbrellaDirs.end()) { 481 Module *Found = KnownDir->second; 482 if (IsUnavailable(Found)) 483 return true; 484 485 // Search up the module stack until we find a module with an umbrella 486 // directory. 487 Module *UmbrellaModule = Found; 488 while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 489 UmbrellaModule = UmbrellaModule->Parent; 490 491 if (UmbrellaModule->InferSubmodules) { 492 for (unsigned I = SkippedDirs.size(); I != 0; --I) { 493 // Find or create the module that corresponds to this directory name. 494 SmallString<32> NameBuf; 495 StringRef Name = sanitizeFilenameAsIdentifier( 496 llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 497 NameBuf); 498 Found = lookupModuleQualified(Name, Found); 499 if (!Found) 500 return false; 501 if (IsUnavailable(Found)) 502 return true; 503 } 504 505 // Infer a submodule with the same name as this header file. 506 SmallString<32> NameBuf; 507 StringRef Name = sanitizeFilenameAsIdentifier( 508 llvm::sys::path::stem(Header->getName()), 509 NameBuf); 510 Found = lookupModuleQualified(Name, Found); 511 if (!Found) 512 return false; 513 } 514 515 return IsUnavailable(Found); 516 } 517 518 SkippedDirs.push_back(Dir); 519 520 // Retrieve our parent path. 521 DirName = llvm::sys::path::parent_path(DirName); 522 if (DirName.empty()) 523 break; 524 525 // Resolve the parent path to a directory entry. 526 Dir = SourceMgr.getFileManager().getDirectory(DirName); 527 } while (Dir); 528 529 return false; 530 } 531 532 Module *ModuleMap::findModule(StringRef Name) const { 533 llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name); 534 if (Known != Modules.end()) 535 return Known->getValue(); 536 537 return nullptr; 538 } 539 540 Module *ModuleMap::lookupModuleUnqualified(StringRef Name, 541 Module *Context) const { 542 for(; Context; Context = Context->Parent) { 543 if (Module *Sub = lookupModuleQualified(Name, Context)) 544 return Sub; 545 } 546 547 return findModule(Name); 548 } 549 550 Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{ 551 if (!Context) 552 return findModule(Name); 553 554 return Context->findSubmodule(Name); 555 } 556 557 std::pair<Module *, bool> 558 ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 559 bool IsExplicit) { 560 // Try to find an existing module with this name. 561 if (Module *Sub = lookupModuleQualified(Name, Parent)) 562 return std::make_pair(Sub, false); 563 564 // Create a new module with this name. 565 Module *Result = new Module(Name, SourceLocation(), Parent, 566 IsFramework, IsExplicit); 567 if (LangOpts.CurrentModule == Name) { 568 SourceModule = Result; 569 SourceModuleName = Name; 570 } 571 if (!Parent) { 572 Modules[Name] = Result; 573 if (!LangOpts.CurrentModule.empty() && !CompilingModule && 574 Name == LangOpts.CurrentModule) { 575 CompilingModule = Result; 576 } 577 } 578 return std::make_pair(Result, true); 579 } 580 581 /// \brief For a framework module, infer the framework against which we 582 /// should link. 583 static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir, 584 FileManager &FileMgr) { 585 assert(Mod->IsFramework && "Can only infer linking for framework modules"); 586 assert(!Mod->isSubFramework() && 587 "Can only infer linking for top-level frameworks"); 588 589 SmallString<128> LibName; 590 LibName += FrameworkDir->getName(); 591 llvm::sys::path::append(LibName, Mod->Name); 592 if (FileMgr.getFile(LibName)) { 593 Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name, 594 /*IsFramework=*/true)); 595 } 596 } 597 598 Module * 599 ModuleMap::inferFrameworkModule(StringRef ModuleName, 600 const DirectoryEntry *FrameworkDir, 601 bool IsSystem, 602 Module *Parent) { 603 Attributes Attrs; 604 Attrs.IsSystem = IsSystem; 605 return inferFrameworkModule(ModuleName, FrameworkDir, Attrs, Parent); 606 } 607 608 Module *ModuleMap::inferFrameworkModule(StringRef ModuleName, 609 const DirectoryEntry *FrameworkDir, 610 Attributes Attrs, Module *Parent) { 611 612 // Check whether we've already found this module. 613 if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 614 return Mod; 615 616 FileManager &FileMgr = SourceMgr.getFileManager(); 617 618 // If the framework has a parent path from which we're allowed to infer 619 // a framework module, do so. 620 const FileEntry *ModuleMapFile = nullptr; 621 if (!Parent) { 622 // Determine whether we're allowed to infer a module map. 623 624 // Note: as an egregious but useful hack we use the real path here, because 625 // we might be looking at an embedded framework that symlinks out to a 626 // top-level framework, and we need to infer as if we were naming the 627 // top-level framework. 628 StringRef FrameworkDirName 629 = SourceMgr.getFileManager().getCanonicalName(FrameworkDir); 630 631 // In case this is a case-insensitive filesystem, make sure the canonical 632 // directory name matches ModuleName exactly. Modules are case-sensitive. 633 // FIXME: we should be able to give a fix-it hint for the correct spelling. 634 if (llvm::sys::path::stem(FrameworkDirName) != ModuleName) 635 return nullptr; 636 637 bool canInfer = false; 638 if (llvm::sys::path::has_parent_path(FrameworkDirName)) { 639 // Figure out the parent path. 640 StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); 641 if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) { 642 // Check whether we have already looked into the parent directory 643 // for a module map. 644 llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 645 inferred = InferredDirectories.find(ParentDir); 646 if (inferred == InferredDirectories.end()) { 647 // We haven't looked here before. Load a module map, if there is 648 // one. 649 bool IsFrameworkDir = Parent.endswith(".framework"); 650 if (const FileEntry *ModMapFile = 651 HeaderInfo.lookupModuleMapFile(ParentDir, IsFrameworkDir)) { 652 parseModuleMapFile(ModMapFile, Attrs.IsSystem, ParentDir); 653 inferred = InferredDirectories.find(ParentDir); 654 } 655 656 if (inferred == InferredDirectories.end()) 657 inferred = InferredDirectories.insert( 658 std::make_pair(ParentDir, InferredDirectory())).first; 659 } 660 661 if (inferred->second.InferModules) { 662 // We're allowed to infer for this directory, but make sure it's okay 663 // to infer this particular module. 664 StringRef Name = llvm::sys::path::stem(FrameworkDirName); 665 canInfer = std::find(inferred->second.ExcludedModules.begin(), 666 inferred->second.ExcludedModules.end(), 667 Name) == inferred->second.ExcludedModules.end(); 668 669 Attrs.IsSystem |= inferred->second.Attrs.IsSystem; 670 Attrs.IsExternC |= inferred->second.Attrs.IsExternC; 671 Attrs.IsExhaustive |= inferred->second.Attrs.IsExhaustive; 672 ModuleMapFile = inferred->second.ModuleMapFile; 673 } 674 } 675 } 676 677 // If we're not allowed to infer a framework module, don't. 678 if (!canInfer) 679 return nullptr; 680 } else 681 ModuleMapFile = getModuleMapFileForUniquing(Parent); 682 683 684 // Look for an umbrella header. 685 SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 686 llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h"); 687 const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 688 689 // FIXME: If there's no umbrella header, we could probably scan the 690 // framework to load *everything*. But, it's not clear that this is a good 691 // idea. 692 if (!UmbrellaHeader) 693 return nullptr; 694 695 Module *Result = new Module(ModuleName, SourceLocation(), Parent, 696 /*IsFramework=*/true, /*IsExplicit=*/false); 697 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 708 if (!Parent) 709 Modules[ModuleName] = Result; 710 711 // umbrella header "umbrella-header-name" 712 Result->Umbrella = UmbrellaHeader; 713 Headers[UmbrellaHeader].push_back(KnownHeader(Result, NormalHeader)); 714 UmbrellaDirs[UmbrellaHeader->getDir()] = Result; 715 716 // export * 717 Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 718 719 // module * { export * } 720 Result->InferSubmodules = true; 721 Result->InferExportWildcard = true; 722 723 // Look for subframeworks. 724 std::error_code EC; 725 SmallString<128> SubframeworksDirName 726 = StringRef(FrameworkDir->getName()); 727 llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 728 llvm::sys::path::native(SubframeworksDirName); 729 for (llvm::sys::fs::directory_iterator Dir(SubframeworksDirName, EC), DirEnd; 730 Dir != DirEnd && !EC; Dir.increment(EC)) { 731 if (!StringRef(Dir->path()).endswith(".framework")) 732 continue; 733 734 if (const DirectoryEntry *SubframeworkDir 735 = FileMgr.getDirectory(Dir->path())) { 736 // Note: as an egregious but useful hack, we use the real path here and 737 // check whether it is actually a subdirectory of the parent directory. 738 // This will not be the case if the 'subframework' is actually a symlink 739 // out to a top-level framework. 740 StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir); 741 bool FoundParent = false; 742 do { 743 // Get the parent directory name. 744 SubframeworkDirName 745 = llvm::sys::path::parent_path(SubframeworkDirName); 746 if (SubframeworkDirName.empty()) 747 break; 748 749 if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { 750 FoundParent = true; 751 break; 752 } 753 } while (true); 754 755 if (!FoundParent) 756 continue; 757 758 // FIXME: Do we want to warn about subframeworks without umbrella headers? 759 SmallString<32> NameBuf; 760 inferFrameworkModule(sanitizeFilenameAsIdentifier( 761 llvm::sys::path::stem(Dir->path()), NameBuf), 762 SubframeworkDir, Attrs, Result); 763 } 764 } 765 766 // If the module is a top-level framework, automatically link against the 767 // framework. 768 if (!Result->isSubFramework()) { 769 inferFrameworkLink(Result, FrameworkDir, FileMgr); 770 } 771 772 return Result; 773 } 774 775 void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ 776 Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader)); 777 Mod->Umbrella = UmbrellaHeader; 778 UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; 779 } 780 781 void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) { 782 Mod->Umbrella = UmbrellaDir; 783 UmbrellaDirs[UmbrellaDir] = Mod; 784 } 785 786 static Module::HeaderKind headerRoleToKind(ModuleMap::ModuleHeaderRole Role) { 787 switch ((int)Role) { 788 default: llvm_unreachable("unknown header role"); 789 case ModuleMap::NormalHeader: 790 return Module::HK_Normal; 791 case ModuleMap::PrivateHeader: 792 return Module::HK_Private; 793 case ModuleMap::TextualHeader: 794 return Module::HK_Textual; 795 case ModuleMap::PrivateHeader | ModuleMap::TextualHeader: 796 return Module::HK_PrivateTextual; 797 } 798 } 799 800 void ModuleMap::addHeader(Module *Mod, Module::Header Header, 801 ModuleHeaderRole Role) { 802 if (!(Role & TextualHeader)) { 803 bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule; 804 HeaderInfo.MarkFileModuleHeader(Header.Entry, Role, 805 isCompilingModuleHeader); 806 } 807 Headers[Header.Entry].push_back(KnownHeader(Mod, Role)); 808 809 Mod->Headers[headerRoleToKind(Role)].push_back(std::move(Header)); 810 } 811 812 void ModuleMap::excludeHeader(Module *Mod, Module::Header Header) { 813 // Add this as a known header so we won't implicitly add it to any 814 // umbrella directory module. 815 // FIXME: Should we only exclude it from umbrella modules within the 816 // specified module? 817 (void) Headers[Header.Entry]; 818 819 Mod->Headers[Module::HK_Excluded].push_back(std::move(Header)); 820 } 821 822 const FileEntry * 823 ModuleMap::getContainingModuleMapFile(const Module *Module) const { 824 if (Module->DefinitionLoc.isInvalid()) 825 return nullptr; 826 827 return SourceMgr.getFileEntryForID( 828 SourceMgr.getFileID(Module->DefinitionLoc)); 829 } 830 831 const FileEntry *ModuleMap::getModuleMapFileForUniquing(const Module *M) const { 832 if (M->IsInferred) { 833 assert(InferredModuleAllowedBy.count(M) && "missing inferred module map"); 834 return InferredModuleAllowedBy.find(M)->second; 835 } 836 return getContainingModuleMapFile(M); 837 } 838 839 void ModuleMap::setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap) { 840 assert(M->IsInferred && "module not inferred"); 841 InferredModuleAllowedBy[M] = ModMap; 842 } 843 844 void ModuleMap::dump() { 845 llvm::errs() << "Modules:"; 846 for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 847 MEnd = Modules.end(); 848 M != MEnd; ++M) 849 M->getValue()->print(llvm::errs(), 2); 850 851 llvm::errs() << "Headers:"; 852 for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 853 H != HEnd; ++H) { 854 llvm::errs() << " \"" << H->first->getName() << "\" -> "; 855 for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(), 856 E = H->second.end(); 857 I != E; ++I) { 858 if (I != H->second.begin()) 859 llvm::errs() << ","; 860 llvm::errs() << I->getModule()->getFullModuleName(); 861 } 862 llvm::errs() << "\n"; 863 } 864 } 865 866 bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 867 bool HadError = false; 868 for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { 869 Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 870 Complain); 871 if (Export.getPointer() || Export.getInt()) 872 Mod->Exports.push_back(Export); 873 else 874 HadError = true; 875 } 876 Mod->UnresolvedExports.clear(); 877 return HadError; 878 } 879 880 bool ModuleMap::resolveUses(Module *Mod, bool Complain) { 881 bool HadError = false; 882 for (unsigned I = 0, N = Mod->UnresolvedDirectUses.size(); I != N; ++I) { 883 Module *DirectUse = 884 resolveModuleId(Mod->UnresolvedDirectUses[I], Mod, Complain); 885 if (DirectUse) 886 Mod->DirectUses.push_back(DirectUse); 887 else 888 HadError = true; 889 } 890 Mod->UnresolvedDirectUses.clear(); 891 return HadError; 892 } 893 894 bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) { 895 bool HadError = false; 896 for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) { 897 Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id, 898 Mod, Complain); 899 if (!OtherMod) { 900 HadError = true; 901 continue; 902 } 903 904 Module::Conflict Conflict; 905 Conflict.Other = OtherMod; 906 Conflict.Message = Mod->UnresolvedConflicts[I].Message; 907 Mod->Conflicts.push_back(Conflict); 908 } 909 Mod->UnresolvedConflicts.clear(); 910 return HadError; 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 if (!File && BuiltinFile) { 1762 File = BuiltinFile; 1763 RelativePathName = BuiltinPathName; 1764 BuiltinFile = nullptr; 1765 } 1766 } 1767 } 1768 } 1769 1770 // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 1771 // Come up with a lazy way to do this. 1772 if (File) { 1773 if (LeadingToken == MMToken::UmbrellaKeyword) { 1774 const DirectoryEntry *UmbrellaDir = File->getDir(); 1775 if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { 1776 Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash) 1777 << UmbrellaModule->getFullModuleName(); 1778 HadError = true; 1779 } else { 1780 // Record this umbrella header. 1781 Map.setUmbrellaHeader(ActiveModule, File); 1782 } 1783 } else if (LeadingToken == MMToken::ExcludeKeyword) { 1784 Module::Header H = {RelativePathName.str(), File}; 1785 Map.excludeHeader(ActiveModule, H); 1786 } else { 1787 // If there is a builtin counterpart to this file, add it now, before 1788 // the "real" header, so we build the built-in one first when building 1789 // the module. 1790 if (BuiltinFile) { 1791 // FIXME: Taking the name from the FileEntry is unstable and can give 1792 // different results depending on how we've previously named that file 1793 // in this build. 1794 Module::Header H = { BuiltinFile->getName(), BuiltinFile }; 1795 Map.addHeader(ActiveModule, H, Role); 1796 } 1797 1798 // Record this header. 1799 Module::Header H = { RelativePathName.str(), File }; 1800 Map.addHeader(ActiveModule, H, Role); 1801 } 1802 } else if (LeadingToken != MMToken::ExcludeKeyword) { 1803 // Ignore excluded header files. They're optional anyway. 1804 1805 // If we find a module that has a missing header, we mark this module as 1806 // unavailable and store the header directive for displaying diagnostics. 1807 Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword; 1808 ActiveModule->markUnavailable(); 1809 ActiveModule->MissingHeaders.push_back(Header); 1810 } 1811 } 1812 1813 /// \brief Parse an umbrella directory declaration. 1814 /// 1815 /// umbrella-dir-declaration: 1816 /// umbrella string-literal 1817 void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1818 // Parse the directory name. 1819 if (!Tok.is(MMToken::StringLiteral)) { 1820 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1821 << "umbrella"; 1822 HadError = true; 1823 return; 1824 } 1825 1826 std::string DirName = Tok.getString(); 1827 SourceLocation DirNameLoc = consumeToken(); 1828 1829 // Check whether we already have an umbrella. 1830 if (ActiveModule->Umbrella) { 1831 Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1832 << ActiveModule->getFullModuleName(); 1833 HadError = true; 1834 return; 1835 } 1836 1837 // Look for this file. 1838 const DirectoryEntry *Dir = nullptr; 1839 if (llvm::sys::path::is_absolute(DirName)) 1840 Dir = SourceMgr.getFileManager().getDirectory(DirName); 1841 else { 1842 SmallString<128> PathName; 1843 PathName = Directory->getName(); 1844 llvm::sys::path::append(PathName, DirName); 1845 Dir = SourceMgr.getFileManager().getDirectory(PathName); 1846 } 1847 1848 if (!Dir) { 1849 Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1850 << DirName; 1851 HadError = true; 1852 return; 1853 } 1854 1855 if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1856 Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1857 << OwningModule->getFullModuleName(); 1858 HadError = true; 1859 return; 1860 } 1861 1862 // Record this umbrella directory. 1863 Map.setUmbrellaDir(ActiveModule, Dir); 1864 } 1865 1866 /// \brief Parse a module export declaration. 1867 /// 1868 /// export-declaration: 1869 /// 'export' wildcard-module-id 1870 /// 1871 /// wildcard-module-id: 1872 /// identifier 1873 /// '*' 1874 /// identifier '.' wildcard-module-id 1875 void ModuleMapParser::parseExportDecl() { 1876 assert(Tok.is(MMToken::ExportKeyword)); 1877 SourceLocation ExportLoc = consumeToken(); 1878 1879 // Parse the module-id with an optional wildcard at the end. 1880 ModuleId ParsedModuleId; 1881 bool Wildcard = false; 1882 do { 1883 // FIXME: Support string-literal module names here. 1884 if (Tok.is(MMToken::Identifier)) { 1885 ParsedModuleId.push_back(std::make_pair(Tok.getString(), 1886 Tok.getLocation())); 1887 consumeToken(); 1888 1889 if (Tok.is(MMToken::Period)) { 1890 consumeToken(); 1891 continue; 1892 } 1893 1894 break; 1895 } 1896 1897 if(Tok.is(MMToken::Star)) { 1898 Wildcard = true; 1899 consumeToken(); 1900 break; 1901 } 1902 1903 Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); 1904 HadError = true; 1905 return; 1906 } while (true); 1907 1908 Module::UnresolvedExportDecl Unresolved = { 1909 ExportLoc, ParsedModuleId, Wildcard 1910 }; 1911 ActiveModule->UnresolvedExports.push_back(Unresolved); 1912 } 1913 1914 /// \brief Parse a module use declaration. 1915 /// 1916 /// use-declaration: 1917 /// 'use' wildcard-module-id 1918 void ModuleMapParser::parseUseDecl() { 1919 assert(Tok.is(MMToken::UseKeyword)); 1920 auto KWLoc = consumeToken(); 1921 // Parse the module-id. 1922 ModuleId ParsedModuleId; 1923 parseModuleId(ParsedModuleId); 1924 1925 if (ActiveModule->Parent) 1926 Diags.Report(KWLoc, diag::err_mmap_use_decl_submodule); 1927 else 1928 ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId); 1929 } 1930 1931 /// \brief Parse a link declaration. 1932 /// 1933 /// module-declaration: 1934 /// 'link' 'framework'[opt] string-literal 1935 void ModuleMapParser::parseLinkDecl() { 1936 assert(Tok.is(MMToken::LinkKeyword)); 1937 SourceLocation LinkLoc = consumeToken(); 1938 1939 // Parse the optional 'framework' keyword. 1940 bool IsFramework = false; 1941 if (Tok.is(MMToken::FrameworkKeyword)) { 1942 consumeToken(); 1943 IsFramework = true; 1944 } 1945 1946 // Parse the library name 1947 if (!Tok.is(MMToken::StringLiteral)) { 1948 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) 1949 << IsFramework << SourceRange(LinkLoc); 1950 HadError = true; 1951 return; 1952 } 1953 1954 std::string LibraryName = Tok.getString(); 1955 consumeToken(); 1956 ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, 1957 IsFramework)); 1958 } 1959 1960 /// \brief Parse a configuration macro declaration. 1961 /// 1962 /// module-declaration: 1963 /// 'config_macros' attributes[opt] config-macro-list? 1964 /// 1965 /// config-macro-list: 1966 /// identifier (',' identifier)? 1967 void ModuleMapParser::parseConfigMacros() { 1968 assert(Tok.is(MMToken::ConfigMacros)); 1969 SourceLocation ConfigMacrosLoc = consumeToken(); 1970 1971 // Only top-level modules can have configuration macros. 1972 if (ActiveModule->Parent) { 1973 Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule); 1974 } 1975 1976 // Parse the optional attributes. 1977 Attributes Attrs; 1978 parseOptionalAttributes(Attrs); 1979 if (Attrs.IsExhaustive && !ActiveModule->Parent) { 1980 ActiveModule->ConfigMacrosExhaustive = true; 1981 } 1982 1983 // If we don't have an identifier, we're done. 1984 // FIXME: Support macros with the same name as a keyword here. 1985 if (!Tok.is(MMToken::Identifier)) 1986 return; 1987 1988 // Consume the first identifier. 1989 if (!ActiveModule->Parent) { 1990 ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 1991 } 1992 consumeToken(); 1993 1994 do { 1995 // If there's a comma, consume it. 1996 if (!Tok.is(MMToken::Comma)) 1997 break; 1998 consumeToken(); 1999 2000 // We expect to see a macro name here. 2001 // FIXME: Support macros with the same name as a keyword here. 2002 if (!Tok.is(MMToken::Identifier)) { 2003 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro); 2004 break; 2005 } 2006 2007 // Consume the macro name. 2008 if (!ActiveModule->Parent) { 2009 ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 2010 } 2011 consumeToken(); 2012 } while (true); 2013 } 2014 2015 /// \brief Format a module-id into a string. 2016 static std::string formatModuleId(const ModuleId &Id) { 2017 std::string result; 2018 { 2019 llvm::raw_string_ostream OS(result); 2020 2021 for (unsigned I = 0, N = Id.size(); I != N; ++I) { 2022 if (I) 2023 OS << "."; 2024 OS << Id[I].first; 2025 } 2026 } 2027 2028 return result; 2029 } 2030 2031 /// \brief Parse a conflict declaration. 2032 /// 2033 /// module-declaration: 2034 /// 'conflict' module-id ',' string-literal 2035 void ModuleMapParser::parseConflict() { 2036 assert(Tok.is(MMToken::Conflict)); 2037 SourceLocation ConflictLoc = consumeToken(); 2038 Module::UnresolvedConflict Conflict; 2039 2040 // Parse the module-id. 2041 if (parseModuleId(Conflict.Id)) 2042 return; 2043 2044 // Parse the ','. 2045 if (!Tok.is(MMToken::Comma)) { 2046 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma) 2047 << SourceRange(ConflictLoc); 2048 return; 2049 } 2050 consumeToken(); 2051 2052 // Parse the message. 2053 if (!Tok.is(MMToken::StringLiteral)) { 2054 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message) 2055 << formatModuleId(Conflict.Id); 2056 return; 2057 } 2058 Conflict.Message = Tok.getString().str(); 2059 consumeToken(); 2060 2061 // Add this unresolved conflict. 2062 ActiveModule->UnresolvedConflicts.push_back(Conflict); 2063 } 2064 2065 /// \brief Parse an inferred module declaration (wildcard modules). 2066 /// 2067 /// module-declaration: 2068 /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] 2069 /// { inferred-module-member* } 2070 /// 2071 /// inferred-module-member: 2072 /// 'export' '*' 2073 /// 'exclude' identifier 2074 void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { 2075 assert(Tok.is(MMToken::Star)); 2076 SourceLocation StarLoc = consumeToken(); 2077 bool Failed = false; 2078 2079 // Inferred modules must be submodules. 2080 if (!ActiveModule && !Framework) { 2081 Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 2082 Failed = true; 2083 } 2084 2085 if (ActiveModule) { 2086 // Inferred modules must have umbrella directories. 2087 if (!Failed && ActiveModule->IsAvailable && 2088 !ActiveModule->getUmbrellaDir()) { 2089 Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 2090 Failed = true; 2091 } 2092 2093 // Check for redefinition of an inferred module. 2094 if (!Failed && ActiveModule->InferSubmodules) { 2095 Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 2096 if (ActiveModule->InferredSubmoduleLoc.isValid()) 2097 Diags.Report(ActiveModule->InferredSubmoduleLoc, 2098 diag::note_mmap_prev_definition); 2099 Failed = true; 2100 } 2101 2102 // Check for the 'framework' keyword, which is not permitted here. 2103 if (Framework) { 2104 Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); 2105 Framework = false; 2106 } 2107 } else if (Explicit) { 2108 Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); 2109 Explicit = false; 2110 } 2111 2112 // If there were any problems with this inferred submodule, skip its body. 2113 if (Failed) { 2114 if (Tok.is(MMToken::LBrace)) { 2115 consumeToken(); 2116 skipUntil(MMToken::RBrace); 2117 if (Tok.is(MMToken::RBrace)) 2118 consumeToken(); 2119 } 2120 HadError = true; 2121 return; 2122 } 2123 2124 // Parse optional attributes. 2125 Attributes Attrs; 2126 parseOptionalAttributes(Attrs); 2127 2128 if (ActiveModule) { 2129 // Note that we have an inferred submodule. 2130 ActiveModule->InferSubmodules = true; 2131 ActiveModule->InferredSubmoduleLoc = StarLoc; 2132 ActiveModule->InferExplicitSubmodules = Explicit; 2133 } else { 2134 // We'll be inferring framework modules for this directory. 2135 Map.InferredDirectories[Directory].InferModules = true; 2136 Map.InferredDirectories[Directory].Attrs = Attrs; 2137 Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile; 2138 // FIXME: Handle the 'framework' keyword. 2139 } 2140 2141 // Parse the opening brace. 2142 if (!Tok.is(MMToken::LBrace)) { 2143 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 2144 HadError = true; 2145 return; 2146 } 2147 SourceLocation LBraceLoc = consumeToken(); 2148 2149 // Parse the body of the inferred submodule. 2150 bool Done = false; 2151 do { 2152 switch (Tok.Kind) { 2153 case MMToken::EndOfFile: 2154 case MMToken::RBrace: 2155 Done = true; 2156 break; 2157 2158 case MMToken::ExcludeKeyword: { 2159 if (ActiveModule) { 2160 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2161 << (ActiveModule != nullptr); 2162 consumeToken(); 2163 break; 2164 } 2165 2166 consumeToken(); 2167 // FIXME: Support string-literal module names here. 2168 if (!Tok.is(MMToken::Identifier)) { 2169 Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); 2170 break; 2171 } 2172 2173 Map.InferredDirectories[Directory].ExcludedModules 2174 .push_back(Tok.getString()); 2175 consumeToken(); 2176 break; 2177 } 2178 2179 case MMToken::ExportKeyword: 2180 if (!ActiveModule) { 2181 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2182 << (ActiveModule != nullptr); 2183 consumeToken(); 2184 break; 2185 } 2186 2187 consumeToken(); 2188 if (Tok.is(MMToken::Star)) 2189 ActiveModule->InferExportWildcard = true; 2190 else 2191 Diags.Report(Tok.getLocation(), 2192 diag::err_mmap_expected_export_wildcard); 2193 consumeToken(); 2194 break; 2195 2196 case MMToken::ExplicitKeyword: 2197 case MMToken::ModuleKeyword: 2198 case MMToken::HeaderKeyword: 2199 case MMToken::PrivateKeyword: 2200 case MMToken::UmbrellaKeyword: 2201 default: 2202 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2203 << (ActiveModule != nullptr); 2204 consumeToken(); 2205 break; 2206 } 2207 } while (!Done); 2208 2209 if (Tok.is(MMToken::RBrace)) 2210 consumeToken(); 2211 else { 2212 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 2213 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 2214 HadError = true; 2215 } 2216 } 2217 2218 /// \brief Parse optional attributes. 2219 /// 2220 /// attributes: 2221 /// attribute attributes 2222 /// attribute 2223 /// 2224 /// attribute: 2225 /// [ identifier ] 2226 /// 2227 /// \param Attrs Will be filled in with the parsed attributes. 2228 /// 2229 /// \returns true if an error occurred, false otherwise. 2230 bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { 2231 bool HadError = false; 2232 2233 while (Tok.is(MMToken::LSquare)) { 2234 // Consume the '['. 2235 SourceLocation LSquareLoc = consumeToken(); 2236 2237 // Check whether we have an attribute name here. 2238 if (!Tok.is(MMToken::Identifier)) { 2239 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 2240 skipUntil(MMToken::RSquare); 2241 if (Tok.is(MMToken::RSquare)) 2242 consumeToken(); 2243 HadError = true; 2244 } 2245 2246 // Decode the attribute name. 2247 AttributeKind Attribute 2248 = llvm::StringSwitch<AttributeKind>(Tok.getString()) 2249 .Case("exhaustive", AT_exhaustive) 2250 .Case("extern_c", AT_extern_c) 2251 .Case("system", AT_system) 2252 .Default(AT_unknown); 2253 switch (Attribute) { 2254 case AT_unknown: 2255 Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 2256 << Tok.getString(); 2257 break; 2258 2259 case AT_system: 2260 Attrs.IsSystem = true; 2261 break; 2262 2263 case AT_extern_c: 2264 Attrs.IsExternC = true; 2265 break; 2266 2267 case AT_exhaustive: 2268 Attrs.IsExhaustive = true; 2269 break; 2270 } 2271 consumeToken(); 2272 2273 // Consume the ']'. 2274 if (!Tok.is(MMToken::RSquare)) { 2275 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 2276 Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 2277 skipUntil(MMToken::RSquare); 2278 HadError = true; 2279 } 2280 2281 if (Tok.is(MMToken::RSquare)) 2282 consumeToken(); 2283 } 2284 2285 return HadError; 2286 } 2287 2288 /// \brief Parse a module map file. 2289 /// 2290 /// module-map-file: 2291 /// module-declaration* 2292 bool ModuleMapParser::parseModuleMapFile() { 2293 do { 2294 switch (Tok.Kind) { 2295 case MMToken::EndOfFile: 2296 return HadError; 2297 2298 case MMToken::ExplicitKeyword: 2299 case MMToken::ExternKeyword: 2300 case MMToken::ModuleKeyword: 2301 case MMToken::FrameworkKeyword: 2302 parseModuleDecl(); 2303 break; 2304 2305 case MMToken::Comma: 2306 case MMToken::ConfigMacros: 2307 case MMToken::Conflict: 2308 case MMToken::Exclaim: 2309 case MMToken::ExcludeKeyword: 2310 case MMToken::ExportKeyword: 2311 case MMToken::HeaderKeyword: 2312 case MMToken::Identifier: 2313 case MMToken::LBrace: 2314 case MMToken::LinkKeyword: 2315 case MMToken::LSquare: 2316 case MMToken::Period: 2317 case MMToken::PrivateKeyword: 2318 case MMToken::RBrace: 2319 case MMToken::RSquare: 2320 case MMToken::RequiresKeyword: 2321 case MMToken::Star: 2322 case MMToken::StringLiteral: 2323 case MMToken::TextualKeyword: 2324 case MMToken::UmbrellaKeyword: 2325 case MMToken::UseKeyword: 2326 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 2327 HadError = true; 2328 consumeToken(); 2329 break; 2330 } 2331 } while (true); 2332 } 2333 2334 bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem, 2335 const DirectoryEntry *Dir) { 2336 llvm::DenseMap<const FileEntry *, bool>::iterator Known 2337 = ParsedModuleMap.find(File); 2338 if (Known != ParsedModuleMap.end()) 2339 return Known->second; 2340 2341 assert(Target && "Missing target information"); 2342 auto FileCharacter = IsSystem ? SrcMgr::C_System : SrcMgr::C_User; 2343 FileID ID = SourceMgr.createFileID(File, SourceLocation(), FileCharacter); 2344 const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID); 2345 if (!Buffer) 2346 return ParsedModuleMap[File] = true; 2347 2348 // Parse this module map file. 2349 Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts); 2350 ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir, 2351 BuiltinIncludeDir, IsSystem); 2352 bool Result = Parser.parseModuleMapFile(); 2353 ParsedModuleMap[File] = Result; 2354 return Result; 2355 } 2356