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