1 //===- ModuleMap.cpp - Describe the layout of modules ---------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the ModuleMap implementation, which describes the layout 10 // of a module as it relates to headers. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Lex/ModuleMap.h" 15 #include "clang/Basic/CharInfo.h" 16 #include "clang/Basic/Diagnostic.h" 17 #include "clang/Basic/FileManager.h" 18 #include "clang/Basic/LLVM.h" 19 #include "clang/Basic/LangOptions.h" 20 #include "clang/Basic/Module.h" 21 #include "clang/Basic/SourceLocation.h" 22 #include "clang/Basic/SourceManager.h" 23 #include "clang/Basic/TargetInfo.h" 24 #include "clang/Lex/HeaderSearch.h" 25 #include "clang/Lex/HeaderSearchOptions.h" 26 #include "clang/Lex/LexDiagnostic.h" 27 #include "clang/Lex/Lexer.h" 28 #include "clang/Lex/LiteralSupport.h" 29 #include "clang/Lex/Token.h" 30 #include "llvm/ADT/DenseMap.h" 31 #include "llvm/ADT/STLExtras.h" 32 #include "llvm/ADT/SmallPtrSet.h" 33 #include "llvm/ADT/SmallString.h" 34 #include "llvm/ADT/SmallVector.h" 35 #include "llvm/ADT/StringMap.h" 36 #include "llvm/ADT/StringRef.h" 37 #include "llvm/ADT/StringSwitch.h" 38 #include "llvm/Support/Allocator.h" 39 #include "llvm/Support/Compiler.h" 40 #include "llvm/Support/ErrorHandling.h" 41 #include "llvm/Support/MemoryBuffer.h" 42 #include "llvm/Support/Path.h" 43 #include "llvm/Support/VirtualFileSystem.h" 44 #include "llvm/Support/raw_ostream.h" 45 #include <algorithm> 46 #include <cassert> 47 #include <cstdint> 48 #include <cstring> 49 #include <optional> 50 #include <string> 51 #include <system_error> 52 #include <utility> 53 54 using namespace clang; 55 56 void ModuleMapCallbacks::anchor() {} 57 58 void ModuleMap::resolveLinkAsDependencies(Module *Mod) { 59 auto PendingLinkAs = PendingLinkAsModule.find(Mod->Name); 60 if (PendingLinkAs != PendingLinkAsModule.end()) { 61 for (auto &Name : PendingLinkAs->second) { 62 auto *M = findModule(Name.getKey()); 63 if (M) 64 M->UseExportAsModuleLinkName = true; 65 } 66 } 67 } 68 69 void ModuleMap::addLinkAsDependency(Module *Mod) { 70 if (findModule(Mod->ExportAsModule)) 71 Mod->UseExportAsModuleLinkName = true; 72 else 73 PendingLinkAsModule[Mod->ExportAsModule].insert(Mod->Name); 74 } 75 76 Module::HeaderKind ModuleMap::headerRoleToKind(ModuleHeaderRole Role) { 77 switch ((int)Role) { 78 case NormalHeader: 79 return Module::HK_Normal; 80 case PrivateHeader: 81 return Module::HK_Private; 82 case TextualHeader: 83 return Module::HK_Textual; 84 case PrivateHeader | TextualHeader: 85 return Module::HK_PrivateTextual; 86 case ExcludedHeader: 87 return Module::HK_Excluded; 88 } 89 llvm_unreachable("unknown header role"); 90 } 91 92 ModuleMap::ModuleHeaderRole 93 ModuleMap::headerKindToRole(Module::HeaderKind Kind) { 94 switch ((int)Kind) { 95 case Module::HK_Normal: 96 return NormalHeader; 97 case Module::HK_Private: 98 return PrivateHeader; 99 case Module::HK_Textual: 100 return TextualHeader; 101 case Module::HK_PrivateTextual: 102 return ModuleHeaderRole(PrivateHeader | TextualHeader); 103 case Module::HK_Excluded: 104 return ExcludedHeader; 105 } 106 llvm_unreachable("unknown header kind"); 107 } 108 109 bool ModuleMap::isModular(ModuleHeaderRole Role) { 110 return !(Role & (ModuleMap::TextualHeader | ModuleMap::ExcludedHeader)); 111 } 112 113 Module::ExportDecl 114 ModuleMap::resolveExport(Module *Mod, 115 const Module::UnresolvedExportDecl &Unresolved, 116 bool Complain) const { 117 // We may have just a wildcard. 118 if (Unresolved.Id.empty()) { 119 assert(Unresolved.Wildcard && "Invalid unresolved export"); 120 return Module::ExportDecl(nullptr, true); 121 } 122 123 // Resolve the module-id. 124 Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain); 125 if (!Context) 126 return {}; 127 128 return Module::ExportDecl(Context, Unresolved.Wildcard); 129 } 130 131 Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod, 132 bool Complain) const { 133 // Find the starting module. 134 Module *Context = lookupModuleUnqualified(Id[0].first, Mod); 135 if (!Context) { 136 if (Complain) 137 Diags.Report(Id[0].second, diag::err_mmap_missing_module_unqualified) 138 << Id[0].first << Mod->getFullModuleName(); 139 140 return nullptr; 141 } 142 143 // Dig into the module path. 144 for (unsigned I = 1, N = Id.size(); I != N; ++I) { 145 Module *Sub = lookupModuleQualified(Id[I].first, Context); 146 if (!Sub) { 147 if (Complain) 148 Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 149 << Id[I].first << Context->getFullModuleName() 150 << SourceRange(Id[0].second, Id[I-1].second); 151 152 return nullptr; 153 } 154 155 Context = Sub; 156 } 157 158 return Context; 159 } 160 161 /// Append to \p Paths the set of paths needed to get to the 162 /// subframework in which the given module lives. 163 static void appendSubframeworkPaths(Module *Mod, 164 SmallVectorImpl<char> &Path) { 165 // Collect the framework names from the given module to the top-level module. 166 SmallVector<StringRef, 2> Paths; 167 for (; Mod; Mod = Mod->Parent) { 168 if (Mod->IsFramework) 169 Paths.push_back(Mod->Name); 170 } 171 172 if (Paths.empty()) 173 return; 174 175 // Add Frameworks/Name.framework for each subframework. 176 for (StringRef Framework : llvm::drop_begin(llvm::reverse(Paths))) 177 llvm::sys::path::append(Path, "Frameworks", Framework + ".framework"); 178 } 179 180 OptionalFileEntryRef ModuleMap::findHeader( 181 Module *M, const Module::UnresolvedHeaderDirective &Header, 182 SmallVectorImpl<char> &RelativePathName, bool &NeedsFramework) { 183 // Search for the header file within the module's home directory. 184 auto Directory = M->Directory; 185 SmallString<128> FullPathName(Directory->getName()); 186 187 auto GetFile = [&](StringRef Filename) -> OptionalFileEntryRef { 188 auto File = 189 expectedToOptional(SourceMgr.getFileManager().getFileRef(Filename)); 190 if (!File || (Header.Size && File->getSize() != *Header.Size) || 191 (Header.ModTime && File->getModificationTime() != *Header.ModTime)) 192 return std::nullopt; 193 return *File; 194 }; 195 196 auto GetFrameworkFile = [&]() -> OptionalFileEntryRef { 197 unsigned FullPathLength = FullPathName.size(); 198 appendSubframeworkPaths(M, RelativePathName); 199 unsigned RelativePathLength = RelativePathName.size(); 200 201 // Check whether this file is in the public headers. 202 llvm::sys::path::append(RelativePathName, "Headers", Header.FileName); 203 llvm::sys::path::append(FullPathName, RelativePathName); 204 if (auto File = GetFile(FullPathName)) 205 return File; 206 207 // Check whether this file is in the private headers. 208 // Ideally, private modules in the form 'FrameworkName.Private' should 209 // be defined as 'module FrameworkName.Private', and not as 210 // 'framework module FrameworkName.Private', since a 'Private.Framework' 211 // does not usually exist. However, since both are currently widely used 212 // for private modules, make sure we find the right path in both cases. 213 if (M->IsFramework && M->Name == "Private") 214 RelativePathName.clear(); 215 else 216 RelativePathName.resize(RelativePathLength); 217 FullPathName.resize(FullPathLength); 218 llvm::sys::path::append(RelativePathName, "PrivateHeaders", 219 Header.FileName); 220 llvm::sys::path::append(FullPathName, RelativePathName); 221 return GetFile(FullPathName); 222 }; 223 224 if (llvm::sys::path::is_absolute(Header.FileName)) { 225 RelativePathName.clear(); 226 RelativePathName.append(Header.FileName.begin(), Header.FileName.end()); 227 return GetFile(Header.FileName); 228 } 229 230 if (M->isPartOfFramework()) 231 return GetFrameworkFile(); 232 233 // Lookup for normal headers. 234 llvm::sys::path::append(RelativePathName, Header.FileName); 235 llvm::sys::path::append(FullPathName, RelativePathName); 236 auto NormalHdrFile = GetFile(FullPathName); 237 238 if (!NormalHdrFile && Directory->getName().ends_with(".framework")) { 239 // The lack of 'framework' keyword in a module declaration it's a simple 240 // mistake we can diagnose when the header exists within the proper 241 // framework style path. 242 FullPathName.assign(Directory->getName()); 243 RelativePathName.clear(); 244 if (GetFrameworkFile()) { 245 Diags.Report(Header.FileNameLoc, 246 diag::warn_mmap_incomplete_framework_module_declaration) 247 << Header.FileName << M->getFullModuleName(); 248 NeedsFramework = true; 249 } 250 return std::nullopt; 251 } 252 253 return NormalHdrFile; 254 } 255 256 /// Determine whether the given file name is the name of a builtin 257 /// header, supplied by Clang to replace, override, or augment existing system 258 /// headers. 259 static bool isBuiltinHeaderName(StringRef FileName) { 260 return llvm::StringSwitch<bool>(FileName) 261 .Case("float.h", true) 262 .Case("iso646.h", true) 263 .Case("limits.h", true) 264 .Case("stdalign.h", true) 265 .Case("stdarg.h", true) 266 .Case("stdatomic.h", true) 267 .Case("stdbool.h", true) 268 .Case("stddef.h", true) 269 .Case("stdint.h", true) 270 .Case("tgmath.h", true) 271 .Case("unwind.h", true) 272 .Default(false); 273 } 274 275 /// Determine whether the given module name is the name of a builtin 276 /// module that is cyclic with a system module on some platforms. 277 static bool isBuiltInModuleName(StringRef ModuleName) { 278 return llvm::StringSwitch<bool>(ModuleName) 279 .Case("_Builtin_float", true) 280 .Case("_Builtin_inttypes", true) 281 .Case("_Builtin_iso646", true) 282 .Case("_Builtin_limits", true) 283 .Case("_Builtin_stdalign", true) 284 .Case("_Builtin_stdarg", true) 285 .Case("_Builtin_stdatomic", true) 286 .Case("_Builtin_stdbool", true) 287 .Case("_Builtin_stddef", true) 288 .Case("_Builtin_stdint", true) 289 .Case("_Builtin_stdnoreturn", true) 290 .Case("_Builtin_tgmath", true) 291 .Case("_Builtin_unwind", true) 292 .Default(false); 293 } 294 295 void ModuleMap::resolveHeader(Module *Mod, 296 const Module::UnresolvedHeaderDirective &Header, 297 bool &NeedsFramework) { 298 SmallString<128> RelativePathName; 299 if (OptionalFileEntryRef File = 300 findHeader(Mod, Header, RelativePathName, NeedsFramework)) { 301 if (Header.IsUmbrella) { 302 const DirectoryEntry *UmbrellaDir = &File->getDir().getDirEntry(); 303 if (Module *UmbrellaMod = UmbrellaDirs[UmbrellaDir]) 304 Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash) 305 << UmbrellaMod->getFullModuleName(); 306 else 307 // Record this umbrella header. 308 setUmbrellaHeaderAsWritten(Mod, *File, Header.FileName, 309 RelativePathName.str()); 310 } else { 311 Module::Header H = {Header.FileName, std::string(RelativePathName), 312 *File}; 313 addHeader(Mod, H, headerKindToRole(Header.Kind)); 314 } 315 } else if (Header.HasBuiltinHeader && !Header.Size && !Header.ModTime) { 316 // There's a builtin header but no corresponding on-disk header. Assume 317 // this was supposed to modularize the builtin header alone. 318 } else if (Header.Kind == Module::HK_Excluded) { 319 // Ignore missing excluded header files. They're optional anyway. 320 } else { 321 // If we find a module that has a missing header, we mark this module as 322 // unavailable and store the header directive for displaying diagnostics. 323 Mod->MissingHeaders.push_back(Header); 324 // A missing header with stat information doesn't make the module 325 // unavailable; this keeps our behavior consistent as headers are lazily 326 // resolved. (Such a module still can't be built though, except from 327 // preprocessed source.) 328 if (!Header.Size && !Header.ModTime) 329 Mod->markUnavailable(/*Unimportable=*/false); 330 } 331 } 332 333 bool ModuleMap::resolveAsBuiltinHeader( 334 Module *Mod, const Module::UnresolvedHeaderDirective &Header) { 335 if (Header.Kind == Module::HK_Excluded || 336 llvm::sys::path::is_absolute(Header.FileName) || 337 Mod->isPartOfFramework() || !Mod->IsSystem || Header.IsUmbrella || 338 !BuiltinIncludeDir || BuiltinIncludeDir == Mod->Directory || 339 !LangOpts.BuiltinHeadersInSystemModules || !isBuiltinHeaderName(Header.FileName)) 340 return false; 341 342 // This is a system module with a top-level header. This header 343 // may have a counterpart (or replacement) in the set of headers 344 // supplied by Clang. Find that builtin header. 345 SmallString<128> Path; 346 llvm::sys::path::append(Path, BuiltinIncludeDir->getName(), Header.FileName); 347 auto File = SourceMgr.getFileManager().getOptionalFileRef(Path); 348 if (!File) 349 return false; 350 351 Module::Header H = {Header.FileName, Header.FileName, *File}; 352 auto Role = headerKindToRole(Header.Kind); 353 addHeader(Mod, H, Role); 354 return true; 355 } 356 357 ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags, 358 const LangOptions &LangOpts, const TargetInfo *Target, 359 HeaderSearch &HeaderInfo) 360 : SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target), 361 HeaderInfo(HeaderInfo) { 362 MMapLangOpts.LineComment = true; 363 } 364 365 ModuleMap::~ModuleMap() { 366 for (auto &M : Modules) 367 delete M.getValue(); 368 for (auto *M : ShadowModules) 369 delete M; 370 } 371 372 void ModuleMap::setTarget(const TargetInfo &Target) { 373 assert((!this->Target || this->Target == &Target) && 374 "Improper target override"); 375 this->Target = &Target; 376 } 377 378 /// "Sanitize" a filename so that it can be used as an identifier. 379 static StringRef sanitizeFilenameAsIdentifier(StringRef Name, 380 SmallVectorImpl<char> &Buffer) { 381 if (Name.empty()) 382 return Name; 383 384 if (!isValidAsciiIdentifier(Name)) { 385 // If we don't already have something with the form of an identifier, 386 // create a buffer with the sanitized name. 387 Buffer.clear(); 388 if (isDigit(Name[0])) 389 Buffer.push_back('_'); 390 Buffer.reserve(Buffer.size() + Name.size()); 391 for (unsigned I = 0, N = Name.size(); I != N; ++I) { 392 if (isAsciiIdentifierContinue(Name[I])) 393 Buffer.push_back(Name[I]); 394 else 395 Buffer.push_back('_'); 396 } 397 398 Name = StringRef(Buffer.data(), Buffer.size()); 399 } 400 401 while (llvm::StringSwitch<bool>(Name) 402 #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true) 403 #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true) 404 #include "clang/Basic/TokenKinds.def" 405 .Default(false)) { 406 if (Name.data() != Buffer.data()) 407 Buffer.append(Name.begin(), Name.end()); 408 Buffer.push_back('_'); 409 Name = StringRef(Buffer.data(), Buffer.size()); 410 } 411 412 return Name; 413 } 414 415 bool ModuleMap::isBuiltinHeader(FileEntryRef File) { 416 return File.getDir() == BuiltinIncludeDir && LangOpts.BuiltinHeadersInSystemModules && 417 isBuiltinHeaderName(llvm::sys::path::filename(File.getName())); 418 } 419 420 bool ModuleMap::shouldImportRelativeToBuiltinIncludeDir(StringRef FileName, 421 Module *Module) const { 422 return LangOpts.BuiltinHeadersInSystemModules && BuiltinIncludeDir && 423 Module->IsSystem && !Module->isPartOfFramework() && 424 isBuiltinHeaderName(FileName); 425 } 426 427 ModuleMap::HeadersMap::iterator ModuleMap::findKnownHeader(FileEntryRef File) { 428 resolveHeaderDirectives(File); 429 HeadersMap::iterator Known = Headers.find(File); 430 if (HeaderInfo.getHeaderSearchOpts().ImplicitModuleMaps && 431 Known == Headers.end() && ModuleMap::isBuiltinHeader(File)) { 432 HeaderInfo.loadTopLevelSystemModules(); 433 return Headers.find(File); 434 } 435 return Known; 436 } 437 438 ModuleMap::KnownHeader ModuleMap::findHeaderInUmbrellaDirs( 439 FileEntryRef File, SmallVectorImpl<DirectoryEntryRef> &IntermediateDirs) { 440 if (UmbrellaDirs.empty()) 441 return {}; 442 443 OptionalDirectoryEntryRef Dir = File.getDir(); 444 445 // Note: as an egregious but useful hack we use the real path here, because 446 // frameworks moving from top-level frameworks to embedded frameworks tend 447 // to be symlinked from the top-level location to the embedded location, 448 // and we need to resolve lookups as if we had found the embedded location. 449 StringRef DirName = SourceMgr.getFileManager().getCanonicalName(*Dir); 450 451 // Keep walking up the directory hierarchy, looking for a directory with 452 // an umbrella header. 453 do { 454 auto KnownDir = UmbrellaDirs.find(*Dir); 455 if (KnownDir != UmbrellaDirs.end()) 456 return KnownHeader(KnownDir->second, NormalHeader); 457 458 IntermediateDirs.push_back(*Dir); 459 460 // Retrieve our parent path. 461 DirName = llvm::sys::path::parent_path(DirName); 462 if (DirName.empty()) 463 break; 464 465 // Resolve the parent path to a directory entry. 466 Dir = SourceMgr.getFileManager().getOptionalDirectoryRef(DirName); 467 } while (Dir); 468 return {}; 469 } 470 471 static bool violatesPrivateInclude(Module *RequestingModule, 472 const FileEntry *IncFileEnt, 473 ModuleMap::KnownHeader Header) { 474 #ifndef NDEBUG 475 if (Header.getRole() & ModuleMap::PrivateHeader) { 476 // Check for consistency between the module header role 477 // as obtained from the lookup and as obtained from the module. 478 // This check is not cheap, so enable it only for debugging. 479 bool IsPrivate = false; 480 SmallVectorImpl<Module::Header> *HeaderList[] = { 481 &Header.getModule()->Headers[Module::HK_Private], 482 &Header.getModule()->Headers[Module::HK_PrivateTextual]}; 483 for (auto *Hs : HeaderList) 484 IsPrivate |= llvm::any_of( 485 *Hs, [&](const Module::Header &H) { return H.Entry == IncFileEnt; }); 486 assert(IsPrivate && "inconsistent headers and roles"); 487 } 488 #endif 489 return !Header.isAccessibleFrom(RequestingModule); 490 } 491 492 static Module *getTopLevelOrNull(Module *M) { 493 return M ? M->getTopLevelModule() : nullptr; 494 } 495 496 void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule, 497 bool RequestingModuleIsModuleInterface, 498 SourceLocation FilenameLoc, 499 StringRef Filename, FileEntryRef File) { 500 // No errors for indirect modules. This may be a bit of a problem for modules 501 // with no source files. 502 if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule)) 503 return; 504 505 if (RequestingModule) { 506 resolveUses(RequestingModule, /*Complain=*/false); 507 resolveHeaderDirectives(RequestingModule, /*File=*/std::nullopt); 508 } 509 510 bool Excluded = false; 511 Module *Private = nullptr; 512 Module *NotUsed = nullptr; 513 514 HeadersMap::iterator Known = findKnownHeader(File); 515 if (Known != Headers.end()) { 516 for (const KnownHeader &Header : Known->second) { 517 // Excluded headers don't really belong to a module. 518 if (Header.getRole() == ModuleMap::ExcludedHeader) { 519 Excluded = true; 520 continue; 521 } 522 523 // Remember private headers for later printing of a diagnostic. 524 if (violatesPrivateInclude(RequestingModule, File, Header)) { 525 Private = Header.getModule(); 526 continue; 527 } 528 529 // If uses need to be specified explicitly, we are only allowed to return 530 // modules that are explicitly used by the requesting module. 531 if (RequestingModule && LangOpts.ModulesDeclUse && 532 !RequestingModule->directlyUses(Header.getModule())) { 533 NotUsed = Header.getModule(); 534 continue; 535 } 536 537 // We have found a module that we can happily use. 538 return; 539 } 540 541 Excluded = true; 542 } 543 544 // We have found a header, but it is private. 545 if (Private) { 546 Diags.Report(FilenameLoc, diag::warn_use_of_private_header_outside_module) 547 << Filename; 548 return; 549 } 550 551 // We have found a module, but we don't use it. 552 if (NotUsed) { 553 Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module_indirect) 554 << RequestingModule->getTopLevelModule()->Name << Filename 555 << NotUsed->Name; 556 return; 557 } 558 559 if (Excluded || isHeaderInUmbrellaDirs(File)) 560 return; 561 562 // At this point, only non-modular includes remain. 563 564 if (RequestingModule && LangOpts.ModulesStrictDeclUse) { 565 Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module) 566 << RequestingModule->getTopLevelModule()->Name << Filename; 567 } else if (RequestingModule && RequestingModuleIsModuleInterface && 568 LangOpts.isCompilingModule()) { 569 // Do not diagnose when we are not compiling a module. 570 diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ? 571 diag::warn_non_modular_include_in_framework_module : 572 diag::warn_non_modular_include_in_module; 573 Diags.Report(FilenameLoc, DiagID) << RequestingModule->getFullModuleName() 574 << File.getName(); 575 } 576 } 577 578 static bool isBetterKnownHeader(const ModuleMap::KnownHeader &New, 579 const ModuleMap::KnownHeader &Old) { 580 // Prefer available modules. 581 // FIXME: Considering whether the module is available rather than merely 582 // importable is non-hermetic and can result in surprising behavior for 583 // prebuilt modules. Consider only checking for importability here. 584 if (New.getModule()->isAvailable() && !Old.getModule()->isAvailable()) 585 return true; 586 587 // Prefer a public header over a private header. 588 if ((New.getRole() & ModuleMap::PrivateHeader) != 589 (Old.getRole() & ModuleMap::PrivateHeader)) 590 return !(New.getRole() & ModuleMap::PrivateHeader); 591 592 // Prefer a non-textual header over a textual header. 593 if ((New.getRole() & ModuleMap::TextualHeader) != 594 (Old.getRole() & ModuleMap::TextualHeader)) 595 return !(New.getRole() & ModuleMap::TextualHeader); 596 597 // Prefer a non-excluded header over an excluded header. 598 if ((New.getRole() == ModuleMap::ExcludedHeader) != 599 (Old.getRole() == ModuleMap::ExcludedHeader)) 600 return New.getRole() != ModuleMap::ExcludedHeader; 601 602 // Don't have a reason to choose between these. Just keep the first one. 603 return false; 604 } 605 606 ModuleMap::KnownHeader ModuleMap::findModuleForHeader(FileEntryRef File, 607 bool AllowTextual, 608 bool AllowExcluded) { 609 auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader { 610 if (!AllowTextual && R.getRole() & ModuleMap::TextualHeader) 611 return {}; 612 return R; 613 }; 614 615 HeadersMap::iterator Known = findKnownHeader(File); 616 if (Known != Headers.end()) { 617 ModuleMap::KnownHeader Result; 618 // Iterate over all modules that 'File' is part of to find the best fit. 619 for (KnownHeader &H : Known->second) { 620 // Cannot use a module if the header is excluded in it. 621 if (!AllowExcluded && H.getRole() == ModuleMap::ExcludedHeader) 622 continue; 623 // Prefer a header from the source module over all others. 624 if (H.getModule()->getTopLevelModule() == SourceModule) 625 return MakeResult(H); 626 if (!Result || isBetterKnownHeader(H, Result)) 627 Result = H; 628 } 629 return MakeResult(Result); 630 } 631 632 return MakeResult(findOrCreateModuleForHeaderInUmbrellaDir(File)); 633 } 634 635 ModuleMap::KnownHeader 636 ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(FileEntryRef File) { 637 assert(!Headers.count(File) && "already have a module for this header"); 638 639 SmallVector<DirectoryEntryRef, 2> SkippedDirs; 640 KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs); 641 if (H) { 642 Module *Result = H.getModule(); 643 644 // Search up the module stack until we find a module with an umbrella 645 // directory. 646 Module *UmbrellaModule = Result; 647 while (!UmbrellaModule->getEffectiveUmbrellaDir() && UmbrellaModule->Parent) 648 UmbrellaModule = UmbrellaModule->Parent; 649 650 if (UmbrellaModule->InferSubmodules) { 651 FileID UmbrellaModuleMap = getModuleMapFileIDForUniquing(UmbrellaModule); 652 653 // Infer submodules for each of the directories we found between 654 // the directory of the umbrella header and the directory where 655 // the actual header is located. 656 bool Explicit = UmbrellaModule->InferExplicitSubmodules; 657 658 for (DirectoryEntryRef SkippedDir : llvm::reverse(SkippedDirs)) { 659 // Find or create the module that corresponds to this directory name. 660 SmallString<32> NameBuf; 661 StringRef Name = sanitizeFilenameAsIdentifier( 662 llvm::sys::path::stem(SkippedDir.getName()), NameBuf); 663 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 664 Explicit).first; 665 InferredModuleAllowedBy[Result] = UmbrellaModuleMap; 666 Result->IsInferred = true; 667 668 // Associate the module and the directory. 669 UmbrellaDirs[SkippedDir] = Result; 670 671 // If inferred submodules export everything they import, add a 672 // wildcard to the set of exports. 673 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 674 Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 675 } 676 677 // Infer a submodule with the same name as this header file. 678 SmallString<32> NameBuf; 679 StringRef Name = sanitizeFilenameAsIdentifier( 680 llvm::sys::path::stem(File.getName()), NameBuf); 681 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 682 Explicit).first; 683 InferredModuleAllowedBy[Result] = UmbrellaModuleMap; 684 Result->IsInferred = true; 685 Result->addTopHeader(File); 686 687 // If inferred submodules export everything they import, add a 688 // wildcard to the set of exports. 689 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 690 Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 691 } else { 692 // Record each of the directories we stepped through as being part of 693 // the module we found, since the umbrella header covers them all. 694 for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 695 UmbrellaDirs[SkippedDirs[I]] = Result; 696 } 697 698 KnownHeader Header(Result, NormalHeader); 699 Headers[File].push_back(Header); 700 return Header; 701 } 702 703 return {}; 704 } 705 706 ArrayRef<ModuleMap::KnownHeader> 707 ModuleMap::findAllModulesForHeader(FileEntryRef File) { 708 HeadersMap::iterator Known = findKnownHeader(File); 709 if (Known != Headers.end()) 710 return Known->second; 711 712 if (findOrCreateModuleForHeaderInUmbrellaDir(File)) 713 return Headers.find(File)->second; 714 715 return std::nullopt; 716 } 717 718 ArrayRef<ModuleMap::KnownHeader> 719 ModuleMap::findResolvedModulesForHeader(FileEntryRef File) const { 720 // FIXME: Is this necessary? 721 resolveHeaderDirectives(File); 722 auto It = Headers.find(File); 723 if (It == Headers.end()) 724 return std::nullopt; 725 return It->second; 726 } 727 728 bool ModuleMap::isHeaderInUnavailableModule(FileEntryRef Header) const { 729 return isHeaderUnavailableInModule(Header, nullptr); 730 } 731 732 bool ModuleMap::isHeaderUnavailableInModule( 733 FileEntryRef Header, const Module *RequestingModule) const { 734 resolveHeaderDirectives(Header); 735 HeadersMap::const_iterator Known = Headers.find(Header); 736 if (Known != Headers.end()) { 737 for (SmallVectorImpl<KnownHeader>::const_iterator 738 I = Known->second.begin(), 739 E = Known->second.end(); 740 I != E; ++I) { 741 742 if (I->getRole() == ModuleMap::ExcludedHeader) 743 continue; 744 745 if (I->isAvailable() && 746 (!RequestingModule || 747 I->getModule()->isSubModuleOf(RequestingModule))) { 748 // When no requesting module is available, the caller is looking if a 749 // header is part a module by only looking into the module map. This is 750 // done by warn_uncovered_module_header checks; don't consider textual 751 // headers part of it in this mode, otherwise we get misleading warnings 752 // that a umbrella header is not including a textual header. 753 if (!RequestingModule && I->getRole() == ModuleMap::TextualHeader) 754 continue; 755 return false; 756 } 757 } 758 return true; 759 } 760 761 OptionalDirectoryEntryRef Dir = Header.getDir(); 762 SmallVector<DirectoryEntryRef, 2> SkippedDirs; 763 StringRef DirName = Dir->getName(); 764 765 auto IsUnavailable = [&](const Module *M) { 766 return !M->isAvailable() && (!RequestingModule || 767 M->isSubModuleOf(RequestingModule)); 768 }; 769 770 // Keep walking up the directory hierarchy, looking for a directory with 771 // an umbrella header. 772 do { 773 auto KnownDir = UmbrellaDirs.find(*Dir); 774 if (KnownDir != UmbrellaDirs.end()) { 775 Module *Found = KnownDir->second; 776 if (IsUnavailable(Found)) 777 return true; 778 779 // Search up the module stack until we find a module with an umbrella 780 // directory. 781 Module *UmbrellaModule = Found; 782 while (!UmbrellaModule->getEffectiveUmbrellaDir() && 783 UmbrellaModule->Parent) 784 UmbrellaModule = UmbrellaModule->Parent; 785 786 if (UmbrellaModule->InferSubmodules) { 787 for (DirectoryEntryRef SkippedDir : llvm::reverse(SkippedDirs)) { 788 // Find or create the module that corresponds to this directory name. 789 SmallString<32> NameBuf; 790 StringRef Name = sanitizeFilenameAsIdentifier( 791 llvm::sys::path::stem(SkippedDir.getName()), NameBuf); 792 Found = lookupModuleQualified(Name, Found); 793 if (!Found) 794 return false; 795 if (IsUnavailable(Found)) 796 return true; 797 } 798 799 // Infer a submodule with the same name as this header file. 800 SmallString<32> NameBuf; 801 StringRef Name = sanitizeFilenameAsIdentifier( 802 llvm::sys::path::stem(Header.getName()), 803 NameBuf); 804 Found = lookupModuleQualified(Name, Found); 805 if (!Found) 806 return false; 807 } 808 809 return IsUnavailable(Found); 810 } 811 812 SkippedDirs.push_back(*Dir); 813 814 // Retrieve our parent path. 815 DirName = llvm::sys::path::parent_path(DirName); 816 if (DirName.empty()) 817 break; 818 819 // Resolve the parent path to a directory entry. 820 Dir = SourceMgr.getFileManager().getOptionalDirectoryRef(DirName); 821 } while (Dir); 822 823 return false; 824 } 825 826 Module *ModuleMap::findModule(StringRef Name) const { 827 llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name); 828 if (Known != Modules.end()) 829 return Known->getValue(); 830 831 return nullptr; 832 } 833 834 Module *ModuleMap::lookupModuleUnqualified(StringRef Name, 835 Module *Context) const { 836 for(; Context; Context = Context->Parent) { 837 if (Module *Sub = lookupModuleQualified(Name, Context)) 838 return Sub; 839 } 840 841 return findModule(Name); 842 } 843 844 Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{ 845 if (!Context) 846 return findModule(Name); 847 848 return Context->findSubmodule(Name); 849 } 850 851 std::pair<Module *, bool> ModuleMap::findOrCreateModule(StringRef Name, 852 Module *Parent, 853 bool IsFramework, 854 bool IsExplicit) { 855 // Try to find an existing module with this name. 856 if (Module *Sub = lookupModuleQualified(Name, Parent)) 857 return std::make_pair(Sub, false); 858 859 // Create a new module with this name. 860 Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework, 861 IsExplicit, NumCreatedModules++); 862 if (!Parent) { 863 if (LangOpts.CurrentModule == Name) 864 SourceModule = Result; 865 Modules[Name] = Result; 866 ModuleScopeIDs[Result] = CurrentModuleScopeID; 867 } 868 return std::make_pair(Result, true); 869 } 870 871 Module *ModuleMap::createGlobalModuleFragmentForModuleUnit(SourceLocation Loc, 872 Module *Parent) { 873 auto *Result = new Module("<global>", Loc, Parent, /*IsFramework*/ false, 874 /*IsExplicit*/ true, NumCreatedModules++); 875 Result->Kind = Module::ExplicitGlobalModuleFragment; 876 // If the created module isn't owned by a parent, send it to PendingSubmodules 877 // to wait for its parent. 878 if (!Result->Parent) 879 PendingSubmodules.emplace_back(Result); 880 return Result; 881 } 882 883 Module * 884 ModuleMap::createImplicitGlobalModuleFragmentForModuleUnit(SourceLocation Loc, 885 Module *Parent) { 886 assert(Parent && "We should only create an implicit global module fragment " 887 "in a module purview"); 888 // Note: Here the `IsExplicit` parameter refers to the semantics in clang 889 // modules. All the non-explicit submodules in clang modules will be exported 890 // too. Here we simplify the implementation by using the concept. 891 auto *Result = 892 new Module("<implicit global>", Loc, Parent, /*IsFramework=*/false, 893 /*IsExplicit=*/false, NumCreatedModules++); 894 Result->Kind = Module::ImplicitGlobalModuleFragment; 895 return Result; 896 } 897 898 Module * 899 ModuleMap::createPrivateModuleFragmentForInterfaceUnit(Module *Parent, 900 SourceLocation Loc) { 901 auto *Result = 902 new Module("<private>", Loc, Parent, /*IsFramework*/ false, 903 /*IsExplicit*/ true, NumCreatedModules++); 904 Result->Kind = Module::PrivateModuleFragment; 905 return Result; 906 } 907 908 Module *ModuleMap::createModuleUnitWithKind(SourceLocation Loc, StringRef Name, 909 Module::ModuleKind Kind) { 910 auto *Result = 911 new Module(Name, Loc, nullptr, /*IsFramework*/ false, 912 /*IsExplicit*/ false, NumCreatedModules++); 913 Result->Kind = Kind; 914 915 // Reparent any current global module fragment as a submodule of this module. 916 for (auto &Submodule : PendingSubmodules) { 917 Submodule->setParent(Result); 918 Submodule.release(); // now owned by parent 919 } 920 PendingSubmodules.clear(); 921 return Result; 922 } 923 924 Module *ModuleMap::createModuleForInterfaceUnit(SourceLocation Loc, 925 StringRef Name) { 926 assert(LangOpts.CurrentModule == Name && "module name mismatch"); 927 assert(!Modules[Name] && "redefining existing module"); 928 929 auto *Result = 930 createModuleUnitWithKind(Loc, Name, Module::ModuleInterfaceUnit); 931 Modules[Name] = SourceModule = Result; 932 933 // Mark the main source file as being within the newly-created module so that 934 // declarations and macros are properly visibility-restricted to it. 935 auto MainFile = SourceMgr.getFileEntryRefForID(SourceMgr.getMainFileID()); 936 assert(MainFile && "no input file for module interface"); 937 Headers[*MainFile].push_back(KnownHeader(Result, PrivateHeader)); 938 939 return Result; 940 } 941 942 Module *ModuleMap::createModuleForImplementationUnit(SourceLocation Loc, 943 StringRef Name) { 944 assert(LangOpts.CurrentModule == Name && "module name mismatch"); 945 // The interface for this implementation must exist and be loaded. 946 assert(Modules[Name] && Modules[Name]->Kind == Module::ModuleInterfaceUnit && 947 "creating implementation module without an interface"); 948 949 // Create an entry in the modules map to own the implementation unit module. 950 // User module names must not start with a period (so that this cannot clash 951 // with any legal user-defined module name). 952 StringRef IName = ".ImplementationUnit"; 953 assert(!Modules[IName] && "multiple implementation units?"); 954 955 auto *Result = 956 createModuleUnitWithKind(Loc, Name, Module::ModuleImplementationUnit); 957 Modules[IName] = SourceModule = Result; 958 959 // Check that the main file is present. 960 assert(SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()) && 961 "no input file for module implementation"); 962 963 return Result; 964 } 965 966 Module *ModuleMap::createHeaderUnit(SourceLocation Loc, StringRef Name, 967 Module::Header H) { 968 assert(LangOpts.CurrentModule == Name && "module name mismatch"); 969 assert(!Modules[Name] && "redefining existing module"); 970 971 auto *Result = new Module(Name, Loc, nullptr, /*IsFramework*/ false, 972 /*IsExplicit*/ false, NumCreatedModules++); 973 Result->Kind = Module::ModuleHeaderUnit; 974 Modules[Name] = SourceModule = Result; 975 addHeader(Result, H, NormalHeader); 976 return Result; 977 } 978 979 /// For a framework module, infer the framework against which we 980 /// should link. 981 static void inferFrameworkLink(Module *Mod) { 982 assert(Mod->IsFramework && "Can only infer linking for framework modules"); 983 assert(!Mod->isSubFramework() && 984 "Can only infer linking for top-level frameworks"); 985 986 StringRef FrameworkName(Mod->Name); 987 FrameworkName.consume_back("_Private"); 988 Mod->LinkLibraries.push_back(Module::LinkLibrary(FrameworkName.str(), 989 /*IsFramework=*/true)); 990 } 991 992 Module *ModuleMap::inferFrameworkModule(DirectoryEntryRef FrameworkDir, 993 bool IsSystem, Module *Parent) { 994 Attributes Attrs; 995 Attrs.IsSystem = IsSystem; 996 return inferFrameworkModule(FrameworkDir, Attrs, Parent); 997 } 998 999 Module *ModuleMap::inferFrameworkModule(DirectoryEntryRef FrameworkDir, 1000 Attributes Attrs, Module *Parent) { 1001 // Note: as an egregious but useful hack we use the real path here, because 1002 // we might be looking at an embedded framework that symlinks out to a 1003 // top-level framework, and we need to infer as if we were naming the 1004 // top-level framework. 1005 StringRef FrameworkDirName = 1006 SourceMgr.getFileManager().getCanonicalName(FrameworkDir); 1007 1008 // In case this is a case-insensitive filesystem, use the canonical 1009 // directory name as the ModuleName, since modules are case-sensitive. 1010 // FIXME: we should be able to give a fix-it hint for the correct spelling. 1011 SmallString<32> ModuleNameStorage; 1012 StringRef ModuleName = sanitizeFilenameAsIdentifier( 1013 llvm::sys::path::stem(FrameworkDirName), ModuleNameStorage); 1014 1015 // Check whether we've already found this module. 1016 if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 1017 return Mod; 1018 1019 FileManager &FileMgr = SourceMgr.getFileManager(); 1020 1021 // If the framework has a parent path from which we're allowed to infer 1022 // a framework module, do so. 1023 FileID ModuleMapFID; 1024 if (!Parent) { 1025 // Determine whether we're allowed to infer a module map. 1026 bool canInfer = false; 1027 if (llvm::sys::path::has_parent_path(FrameworkDirName)) { 1028 // Figure out the parent path. 1029 StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); 1030 if (auto ParentDir = FileMgr.getOptionalDirectoryRef(Parent)) { 1031 // Check whether we have already looked into the parent directory 1032 // for a module map. 1033 llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 1034 inferred = InferredDirectories.find(*ParentDir); 1035 if (inferred == InferredDirectories.end()) { 1036 // We haven't looked here before. Load a module map, if there is 1037 // one. 1038 bool IsFrameworkDir = Parent.ends_with(".framework"); 1039 if (OptionalFileEntryRef ModMapFile = 1040 HeaderInfo.lookupModuleMapFile(*ParentDir, IsFrameworkDir)) { 1041 parseModuleMapFile(*ModMapFile, Attrs.IsSystem, *ParentDir); 1042 inferred = InferredDirectories.find(*ParentDir); 1043 } 1044 1045 if (inferred == InferredDirectories.end()) 1046 inferred = InferredDirectories.insert( 1047 std::make_pair(*ParentDir, InferredDirectory())).first; 1048 } 1049 1050 if (inferred->second.InferModules) { 1051 // We're allowed to infer for this directory, but make sure it's okay 1052 // to infer this particular module. 1053 StringRef Name = llvm::sys::path::stem(FrameworkDirName); 1054 canInfer = 1055 !llvm::is_contained(inferred->second.ExcludedModules, Name); 1056 1057 Attrs.IsSystem |= inferred->second.Attrs.IsSystem; 1058 Attrs.IsExternC |= inferred->second.Attrs.IsExternC; 1059 Attrs.IsExhaustive |= inferred->second.Attrs.IsExhaustive; 1060 Attrs.NoUndeclaredIncludes |= 1061 inferred->second.Attrs.NoUndeclaredIncludes; 1062 ModuleMapFID = inferred->second.ModuleMapFID; 1063 } 1064 } 1065 } 1066 1067 // If we're not allowed to infer a framework module, don't. 1068 if (!canInfer) 1069 return nullptr; 1070 } else { 1071 ModuleMapFID = getModuleMapFileIDForUniquing(Parent); 1072 } 1073 1074 // Look for an umbrella header. 1075 SmallString<128> UmbrellaName = FrameworkDir.getName(); 1076 llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h"); 1077 auto UmbrellaHeader = FileMgr.getOptionalFileRef(UmbrellaName); 1078 1079 // FIXME: If there's no umbrella header, we could probably scan the 1080 // framework to load *everything*. But, it's not clear that this is a good 1081 // idea. 1082 if (!UmbrellaHeader) 1083 return nullptr; 1084 1085 Module *Result = new Module(ModuleName, SourceLocation(), Parent, 1086 /*IsFramework=*/true, /*IsExplicit=*/false, 1087 NumCreatedModules++); 1088 InferredModuleAllowedBy[Result] = ModuleMapFID; 1089 Result->IsInferred = true; 1090 if (!Parent) { 1091 if (LangOpts.CurrentModule == ModuleName) 1092 SourceModule = Result; 1093 Modules[ModuleName] = Result; 1094 ModuleScopeIDs[Result] = CurrentModuleScopeID; 1095 } 1096 1097 Result->IsSystem |= Attrs.IsSystem; 1098 Result->IsExternC |= Attrs.IsExternC; 1099 Result->ConfigMacrosExhaustive |= Attrs.IsExhaustive; 1100 Result->NoUndeclaredIncludes |= Attrs.NoUndeclaredIncludes; 1101 Result->Directory = FrameworkDir; 1102 1103 // Chop off the first framework bit, as that is implied. 1104 StringRef RelativePath = UmbrellaName.str().substr( 1105 Result->getTopLevelModule()->Directory->getName().size()); 1106 RelativePath = llvm::sys::path::relative_path(RelativePath); 1107 1108 // umbrella header "umbrella-header-name" 1109 setUmbrellaHeaderAsWritten(Result, *UmbrellaHeader, ModuleName + ".h", 1110 RelativePath); 1111 1112 // export * 1113 Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 1114 1115 // module * { export * } 1116 Result->InferSubmodules = true; 1117 Result->InferExportWildcard = true; 1118 1119 // Look for subframeworks. 1120 std::error_code EC; 1121 SmallString<128> SubframeworksDirName = FrameworkDir.getName(); 1122 llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 1123 llvm::sys::path::native(SubframeworksDirName); 1124 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); 1125 for (llvm::vfs::directory_iterator 1126 Dir = FS.dir_begin(SubframeworksDirName, EC), 1127 DirEnd; 1128 Dir != DirEnd && !EC; Dir.increment(EC)) { 1129 if (!StringRef(Dir->path()).ends_with(".framework")) 1130 continue; 1131 1132 if (auto SubframeworkDir = FileMgr.getOptionalDirectoryRef(Dir->path())) { 1133 // Note: as an egregious but useful hack, we use the real path here and 1134 // check whether it is actually a subdirectory of the parent directory. 1135 // This will not be the case if the 'subframework' is actually a symlink 1136 // out to a top-level framework. 1137 StringRef SubframeworkDirName = 1138 FileMgr.getCanonicalName(*SubframeworkDir); 1139 bool FoundParent = false; 1140 do { 1141 // Get the parent directory name. 1142 SubframeworkDirName 1143 = llvm::sys::path::parent_path(SubframeworkDirName); 1144 if (SubframeworkDirName.empty()) 1145 break; 1146 1147 if (auto SubDir = 1148 FileMgr.getOptionalDirectoryRef(SubframeworkDirName)) { 1149 if (*SubDir == FrameworkDir) { 1150 FoundParent = true; 1151 break; 1152 } 1153 } 1154 } while (true); 1155 1156 if (!FoundParent) 1157 continue; 1158 1159 // FIXME: Do we want to warn about subframeworks without umbrella headers? 1160 inferFrameworkModule(*SubframeworkDir, Attrs, Result); 1161 } 1162 } 1163 1164 // If the module is a top-level framework, automatically link against the 1165 // framework. 1166 if (!Result->isSubFramework()) 1167 inferFrameworkLink(Result); 1168 1169 return Result; 1170 } 1171 1172 Module *ModuleMap::createShadowedModule(StringRef Name, bool IsFramework, 1173 Module *ShadowingModule) { 1174 1175 // Create a new module with this name. 1176 Module *Result = 1177 new Module(Name, SourceLocation(), /*Parent=*/nullptr, IsFramework, 1178 /*IsExplicit=*/false, NumCreatedModules++); 1179 Result->ShadowingModule = ShadowingModule; 1180 Result->markUnavailable(/*Unimportable*/true); 1181 ModuleScopeIDs[Result] = CurrentModuleScopeID; 1182 ShadowModules.push_back(Result); 1183 1184 return Result; 1185 } 1186 1187 void ModuleMap::setUmbrellaHeaderAsWritten( 1188 Module *Mod, FileEntryRef UmbrellaHeader, const Twine &NameAsWritten, 1189 const Twine &PathRelativeToRootModuleDirectory) { 1190 Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader)); 1191 Mod->Umbrella = UmbrellaHeader; 1192 Mod->UmbrellaAsWritten = NameAsWritten.str(); 1193 Mod->UmbrellaRelativeToRootModuleDirectory = 1194 PathRelativeToRootModuleDirectory.str(); 1195 UmbrellaDirs[UmbrellaHeader.getDir()] = Mod; 1196 1197 // Notify callbacks that we just added a new header. 1198 for (const auto &Cb : Callbacks) 1199 Cb->moduleMapAddUmbrellaHeader(UmbrellaHeader); 1200 } 1201 1202 void ModuleMap::setUmbrellaDirAsWritten( 1203 Module *Mod, DirectoryEntryRef UmbrellaDir, const Twine &NameAsWritten, 1204 const Twine &PathRelativeToRootModuleDirectory) { 1205 Mod->Umbrella = UmbrellaDir; 1206 Mod->UmbrellaAsWritten = NameAsWritten.str(); 1207 Mod->UmbrellaRelativeToRootModuleDirectory = 1208 PathRelativeToRootModuleDirectory.str(); 1209 UmbrellaDirs[UmbrellaDir] = Mod; 1210 } 1211 1212 void ModuleMap::addUnresolvedHeader(Module *Mod, 1213 Module::UnresolvedHeaderDirective Header, 1214 bool &NeedsFramework) { 1215 // If there is a builtin counterpart to this file, add it now so it can 1216 // wrap the system header. 1217 if (resolveAsBuiltinHeader(Mod, Header)) { 1218 // If we have both a builtin and system version of the file, the 1219 // builtin version may want to inject macros into the system header, so 1220 // force the system header to be treated as a textual header in this 1221 // case. 1222 Header.Kind = headerRoleToKind(ModuleMap::ModuleHeaderRole( 1223 headerKindToRole(Header.Kind) | ModuleMap::TextualHeader)); 1224 Header.HasBuiltinHeader = true; 1225 } 1226 1227 // If possible, don't stat the header until we need to. This requires the 1228 // user to have provided us with some stat information about the file. 1229 // FIXME: Add support for lazily stat'ing umbrella headers and excluded 1230 // headers. 1231 if ((Header.Size || Header.ModTime) && !Header.IsUmbrella && 1232 Header.Kind != Module::HK_Excluded) { 1233 // We expect more variation in mtime than size, so if we're given both, 1234 // use the mtime as the key. 1235 if (Header.ModTime) 1236 LazyHeadersByModTime[*Header.ModTime].push_back(Mod); 1237 else 1238 LazyHeadersBySize[*Header.Size].push_back(Mod); 1239 Mod->UnresolvedHeaders.push_back(Header); 1240 return; 1241 } 1242 1243 // We don't have stat information or can't defer looking this file up. 1244 // Perform the lookup now. 1245 resolveHeader(Mod, Header, NeedsFramework); 1246 } 1247 1248 void ModuleMap::resolveHeaderDirectives(const FileEntry *File) const { 1249 auto BySize = LazyHeadersBySize.find(File->getSize()); 1250 if (BySize != LazyHeadersBySize.end()) { 1251 for (auto *M : BySize->second) 1252 resolveHeaderDirectives(M, File); 1253 LazyHeadersBySize.erase(BySize); 1254 } 1255 1256 auto ByModTime = LazyHeadersByModTime.find(File->getModificationTime()); 1257 if (ByModTime != LazyHeadersByModTime.end()) { 1258 for (auto *M : ByModTime->second) 1259 resolveHeaderDirectives(M, File); 1260 LazyHeadersByModTime.erase(ByModTime); 1261 } 1262 } 1263 1264 void ModuleMap::resolveHeaderDirectives( 1265 Module *Mod, std::optional<const FileEntry *> File) const { 1266 bool NeedsFramework = false; 1267 SmallVector<Module::UnresolvedHeaderDirective, 1> NewHeaders; 1268 const auto Size = File ? (*File)->getSize() : 0; 1269 const auto ModTime = File ? (*File)->getModificationTime() : 0; 1270 1271 for (auto &Header : Mod->UnresolvedHeaders) { 1272 if (File && ((Header.ModTime && Header.ModTime != ModTime) || 1273 (Header.Size && Header.Size != Size))) 1274 NewHeaders.push_back(Header); 1275 else 1276 // This operation is logically const; we're just changing how we represent 1277 // the header information for this file. 1278 const_cast<ModuleMap *>(this)->resolveHeader(Mod, Header, NeedsFramework); 1279 } 1280 Mod->UnresolvedHeaders.swap(NewHeaders); 1281 } 1282 1283 void ModuleMap::addHeader(Module *Mod, Module::Header Header, 1284 ModuleHeaderRole Role, bool Imported) { 1285 KnownHeader KH(Mod, Role); 1286 1287 // Only add each header to the headers list once. 1288 // FIXME: Should we diagnose if a header is listed twice in the 1289 // same module definition? 1290 auto &HeaderList = Headers[Header.Entry]; 1291 if (llvm::is_contained(HeaderList, KH)) 1292 return; 1293 1294 HeaderList.push_back(KH); 1295 Mod->Headers[headerRoleToKind(Role)].push_back(Header); 1296 1297 bool isCompilingModuleHeader = Mod->isForBuilding(LangOpts); 1298 if (!Imported || isCompilingModuleHeader) { 1299 // When we import HeaderFileInfo, the external source is expected to 1300 // set the isModuleHeader flag itself. 1301 HeaderInfo.MarkFileModuleHeader(Header.Entry, Role, 1302 isCompilingModuleHeader); 1303 } 1304 1305 // Notify callbacks that we just added a new header. 1306 for (const auto &Cb : Callbacks) 1307 Cb->moduleMapAddHeader(Header.Entry.getName()); 1308 } 1309 1310 FileID ModuleMap::getContainingModuleMapFileID(const Module *Module) const { 1311 if (Module->DefinitionLoc.isInvalid()) 1312 return {}; 1313 1314 return SourceMgr.getFileID(Module->DefinitionLoc); 1315 } 1316 1317 OptionalFileEntryRef 1318 ModuleMap::getContainingModuleMapFile(const Module *Module) const { 1319 return SourceMgr.getFileEntryRefForID(getContainingModuleMapFileID(Module)); 1320 } 1321 1322 FileID ModuleMap::getModuleMapFileIDForUniquing(const Module *M) const { 1323 if (M->IsInferred) { 1324 assert(InferredModuleAllowedBy.count(M) && "missing inferred module map"); 1325 return InferredModuleAllowedBy.find(M)->second; 1326 } 1327 return getContainingModuleMapFileID(M); 1328 } 1329 1330 OptionalFileEntryRef 1331 ModuleMap::getModuleMapFileForUniquing(const Module *M) const { 1332 return SourceMgr.getFileEntryRefForID(getModuleMapFileIDForUniquing(M)); 1333 } 1334 1335 void ModuleMap::setInferredModuleAllowedBy(Module *M, FileID ModMapFID) { 1336 assert(M->IsInferred && "module not inferred"); 1337 InferredModuleAllowedBy[M] = ModMapFID; 1338 } 1339 1340 std::error_code 1341 ModuleMap::canonicalizeModuleMapPath(SmallVectorImpl<char> &Path) { 1342 StringRef Dir = llvm::sys::path::parent_path({Path.data(), Path.size()}); 1343 1344 // Do not canonicalize within the framework; the module map parser expects 1345 // Modules/ not Versions/A/Modules. 1346 if (llvm::sys::path::filename(Dir) == "Modules") { 1347 StringRef Parent = llvm::sys::path::parent_path(Dir); 1348 if (Parent.ends_with(".framework")) 1349 Dir = Parent; 1350 } 1351 1352 FileManager &FM = SourceMgr.getFileManager(); 1353 auto DirEntry = FM.getDirectoryRef(Dir.empty() ? "." : Dir); 1354 if (!DirEntry) 1355 return llvm::errorToErrorCode(DirEntry.takeError()); 1356 1357 // Canonicalize the directory. 1358 StringRef CanonicalDir = FM.getCanonicalName(*DirEntry); 1359 if (CanonicalDir != Dir) 1360 llvm::sys::path::replace_path_prefix(Path, Dir, CanonicalDir); 1361 1362 // In theory, the filename component should also be canonicalized if it 1363 // on a case-insensitive filesystem. However, the extra canonicalization is 1364 // expensive and if clang looked up the filename it will always be lowercase. 1365 1366 // Remove ., remove redundant separators, and switch to native separators. 1367 // This is needed for separators between CanonicalDir and the filename. 1368 llvm::sys::path::remove_dots(Path); 1369 1370 return std::error_code(); 1371 } 1372 1373 void ModuleMap::addAdditionalModuleMapFile(const Module *M, 1374 FileEntryRef ModuleMap) { 1375 AdditionalModMaps[M].insert(ModuleMap); 1376 } 1377 1378 LLVM_DUMP_METHOD void ModuleMap::dump() { 1379 llvm::errs() << "Modules:"; 1380 for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 1381 MEnd = Modules.end(); 1382 M != MEnd; ++M) 1383 M->getValue()->print(llvm::errs(), 2); 1384 1385 llvm::errs() << "Headers:"; 1386 for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 1387 H != HEnd; ++H) { 1388 llvm::errs() << " \"" << H->first.getName() << "\" -> "; 1389 for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(), 1390 E = H->second.end(); 1391 I != E; ++I) { 1392 if (I != H->second.begin()) 1393 llvm::errs() << ","; 1394 llvm::errs() << I->getModule()->getFullModuleName(); 1395 } 1396 llvm::errs() << "\n"; 1397 } 1398 } 1399 1400 bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 1401 auto Unresolved = std::move(Mod->UnresolvedExports); 1402 Mod->UnresolvedExports.clear(); 1403 for (auto &UE : Unresolved) { 1404 Module::ExportDecl Export = resolveExport(Mod, UE, Complain); 1405 if (Export.getPointer() || Export.getInt()) 1406 Mod->Exports.push_back(Export); 1407 else 1408 Mod->UnresolvedExports.push_back(UE); 1409 } 1410 return !Mod->UnresolvedExports.empty(); 1411 } 1412 1413 bool ModuleMap::resolveUses(Module *Mod, bool Complain) { 1414 auto *Top = Mod->getTopLevelModule(); 1415 auto Unresolved = std::move(Top->UnresolvedDirectUses); 1416 Top->UnresolvedDirectUses.clear(); 1417 for (auto &UDU : Unresolved) { 1418 Module *DirectUse = resolveModuleId(UDU, Top, Complain); 1419 if (DirectUse) 1420 Top->DirectUses.push_back(DirectUse); 1421 else 1422 Top->UnresolvedDirectUses.push_back(UDU); 1423 } 1424 return !Top->UnresolvedDirectUses.empty(); 1425 } 1426 1427 bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) { 1428 auto Unresolved = std::move(Mod->UnresolvedConflicts); 1429 Mod->UnresolvedConflicts.clear(); 1430 for (auto &UC : Unresolved) { 1431 if (Module *OtherMod = resolveModuleId(UC.Id, Mod, Complain)) { 1432 Module::Conflict Conflict; 1433 Conflict.Other = OtherMod; 1434 Conflict.Message = UC.Message; 1435 Mod->Conflicts.push_back(Conflict); 1436 } else 1437 Mod->UnresolvedConflicts.push_back(UC); 1438 } 1439 return !Mod->UnresolvedConflicts.empty(); 1440 } 1441 1442 //----------------------------------------------------------------------------// 1443 // Module map file parser 1444 //----------------------------------------------------------------------------// 1445 1446 namespace clang { 1447 1448 /// A token in a module map file. 1449 struct MMToken { 1450 enum TokenKind { 1451 Comma, 1452 ConfigMacros, 1453 Conflict, 1454 EndOfFile, 1455 HeaderKeyword, 1456 Identifier, 1457 Exclaim, 1458 ExcludeKeyword, 1459 ExplicitKeyword, 1460 ExportKeyword, 1461 ExportAsKeyword, 1462 ExternKeyword, 1463 FrameworkKeyword, 1464 LinkKeyword, 1465 ModuleKeyword, 1466 Period, 1467 PrivateKeyword, 1468 UmbrellaKeyword, 1469 UseKeyword, 1470 RequiresKeyword, 1471 Star, 1472 StringLiteral, 1473 IntegerLiteral, 1474 TextualKeyword, 1475 LBrace, 1476 RBrace, 1477 LSquare, 1478 RSquare 1479 } Kind; 1480 1481 SourceLocation::UIntTy Location; 1482 unsigned StringLength; 1483 union { 1484 // If Kind != IntegerLiteral. 1485 const char *StringData; 1486 1487 // If Kind == IntegerLiteral. 1488 uint64_t IntegerValue; 1489 }; 1490 1491 void clear() { 1492 Kind = EndOfFile; 1493 Location = 0; 1494 StringLength = 0; 1495 StringData = nullptr; 1496 } 1497 1498 bool is(TokenKind K) const { return Kind == K; } 1499 1500 SourceLocation getLocation() const { 1501 return SourceLocation::getFromRawEncoding(Location); 1502 } 1503 1504 uint64_t getInteger() const { 1505 return Kind == IntegerLiteral ? IntegerValue : 0; 1506 } 1507 1508 StringRef getString() const { 1509 return Kind == IntegerLiteral ? StringRef() 1510 : StringRef(StringData, StringLength); 1511 } 1512 }; 1513 1514 class ModuleMapParser { 1515 Lexer &L; 1516 SourceManager &SourceMgr; 1517 1518 /// Default target information, used only for string literal 1519 /// parsing. 1520 const TargetInfo *Target; 1521 1522 DiagnosticsEngine &Diags; 1523 ModuleMap ⤅ 1524 1525 /// The current module map file. 1526 FileID ModuleMapFID; 1527 1528 /// Source location of most recent parsed module declaration 1529 SourceLocation CurrModuleDeclLoc; 1530 1531 /// The directory that file names in this module map file should 1532 /// be resolved relative to. 1533 DirectoryEntryRef Directory; 1534 1535 /// Whether this module map is in a system header directory. 1536 bool IsSystem; 1537 1538 /// Whether an error occurred. 1539 bool HadError = false; 1540 1541 /// Stores string data for the various string literals referenced 1542 /// during parsing. 1543 llvm::BumpPtrAllocator StringData; 1544 1545 /// The current token. 1546 MMToken Tok; 1547 1548 /// The active module. 1549 Module *ActiveModule = nullptr; 1550 1551 /// Whether a module uses the 'requires excluded' hack to mark its 1552 /// contents as 'textual'. 1553 /// 1554 /// On older Darwin SDK versions, 'requires excluded' is used to mark the 1555 /// contents of the Darwin.C.excluded (assert.h) and Tcl.Private modules as 1556 /// non-modular headers. For backwards compatibility, we continue to 1557 /// support this idiom for just these modules, and map the headers to 1558 /// 'textual' to match the original intent. 1559 llvm::SmallPtrSet<Module *, 2> UsesRequiresExcludedHack; 1560 1561 /// Consume the current token and return its location. 1562 SourceLocation consumeToken(); 1563 1564 /// Skip tokens until we reach the a token with the given kind 1565 /// (or the end of the file). 1566 void skipUntil(MMToken::TokenKind K); 1567 1568 bool parseModuleId(ModuleId &Id); 1569 void parseModuleDecl(); 1570 void parseExternModuleDecl(); 1571 void parseRequiresDecl(); 1572 void parseHeaderDecl(MMToken::TokenKind, SourceLocation LeadingLoc); 1573 void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 1574 void parseExportDecl(); 1575 void parseExportAsDecl(); 1576 void parseUseDecl(); 1577 void parseLinkDecl(); 1578 void parseConfigMacros(); 1579 void parseConflict(); 1580 void parseInferredModuleDecl(bool Framework, bool Explicit); 1581 1582 /// Private modules are canonicalized as Foo_Private. Clang provides extra 1583 /// module map search logic to find the appropriate private module when PCH 1584 /// is used with implicit module maps. Warn when private modules are written 1585 /// in other ways (FooPrivate and Foo.Private), providing notes and fixits. 1586 void diagnosePrivateModules(SourceLocation ExplicitLoc, 1587 SourceLocation FrameworkLoc); 1588 1589 using Attributes = ModuleMap::Attributes; 1590 1591 bool parseOptionalAttributes(Attributes &Attrs); 1592 1593 public: 1594 ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 1595 const TargetInfo *Target, DiagnosticsEngine &Diags, 1596 ModuleMap &Map, FileID ModuleMapFID, 1597 DirectoryEntryRef Directory, bool IsSystem) 1598 : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 1599 ModuleMapFID(ModuleMapFID), Directory(Directory), IsSystem(IsSystem) { 1600 Tok.clear(); 1601 consumeToken(); 1602 } 1603 1604 bool parseModuleMapFile(); 1605 1606 bool terminatedByDirective() { return false; } 1607 SourceLocation getLocation() { return Tok.getLocation(); } 1608 }; 1609 1610 } // namespace clang 1611 1612 SourceLocation ModuleMapParser::consumeToken() { 1613 SourceLocation Result = Tok.getLocation(); 1614 1615 retry: 1616 Tok.clear(); 1617 Token LToken; 1618 L.LexFromRawLexer(LToken); 1619 Tok.Location = LToken.getLocation().getRawEncoding(); 1620 switch (LToken.getKind()) { 1621 case tok::raw_identifier: { 1622 StringRef RI = LToken.getRawIdentifier(); 1623 Tok.StringData = RI.data(); 1624 Tok.StringLength = RI.size(); 1625 Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI) 1626 .Case("config_macros", MMToken::ConfigMacros) 1627 .Case("conflict", MMToken::Conflict) 1628 .Case("exclude", MMToken::ExcludeKeyword) 1629 .Case("explicit", MMToken::ExplicitKeyword) 1630 .Case("export", MMToken::ExportKeyword) 1631 .Case("export_as", MMToken::ExportAsKeyword) 1632 .Case("extern", MMToken::ExternKeyword) 1633 .Case("framework", MMToken::FrameworkKeyword) 1634 .Case("header", MMToken::HeaderKeyword) 1635 .Case("link", MMToken::LinkKeyword) 1636 .Case("module", MMToken::ModuleKeyword) 1637 .Case("private", MMToken::PrivateKeyword) 1638 .Case("requires", MMToken::RequiresKeyword) 1639 .Case("textual", MMToken::TextualKeyword) 1640 .Case("umbrella", MMToken::UmbrellaKeyword) 1641 .Case("use", MMToken::UseKeyword) 1642 .Default(MMToken::Identifier); 1643 break; 1644 } 1645 1646 case tok::comma: 1647 Tok.Kind = MMToken::Comma; 1648 break; 1649 1650 case tok::eof: 1651 Tok.Kind = MMToken::EndOfFile; 1652 break; 1653 1654 case tok::l_brace: 1655 Tok.Kind = MMToken::LBrace; 1656 break; 1657 1658 case tok::l_square: 1659 Tok.Kind = MMToken::LSquare; 1660 break; 1661 1662 case tok::period: 1663 Tok.Kind = MMToken::Period; 1664 break; 1665 1666 case tok::r_brace: 1667 Tok.Kind = MMToken::RBrace; 1668 break; 1669 1670 case tok::r_square: 1671 Tok.Kind = MMToken::RSquare; 1672 break; 1673 1674 case tok::star: 1675 Tok.Kind = MMToken::Star; 1676 break; 1677 1678 case tok::exclaim: 1679 Tok.Kind = MMToken::Exclaim; 1680 break; 1681 1682 case tok::string_literal: { 1683 if (LToken.hasUDSuffix()) { 1684 Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 1685 HadError = true; 1686 goto retry; 1687 } 1688 1689 // Parse the string literal. 1690 LangOptions LangOpts; 1691 StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target); 1692 if (StringLiteral.hadError) 1693 goto retry; 1694 1695 // Copy the string literal into our string data allocator. 1696 unsigned Length = StringLiteral.GetStringLength(); 1697 char *Saved = StringData.Allocate<char>(Length + 1); 1698 memcpy(Saved, StringLiteral.GetString().data(), Length); 1699 Saved[Length] = 0; 1700 1701 // Form the token. 1702 Tok.Kind = MMToken::StringLiteral; 1703 Tok.StringData = Saved; 1704 Tok.StringLength = Length; 1705 break; 1706 } 1707 1708 case tok::numeric_constant: { 1709 // We don't support any suffixes or other complications. 1710 SmallString<32> SpellingBuffer; 1711 SpellingBuffer.resize(LToken.getLength() + 1); 1712 const char *Start = SpellingBuffer.data(); 1713 unsigned Length = 1714 Lexer::getSpelling(LToken, Start, SourceMgr, Map.LangOpts); 1715 uint64_t Value; 1716 if (StringRef(Start, Length).getAsInteger(0, Value)) { 1717 Diags.Report(Tok.getLocation(), diag::err_mmap_unknown_token); 1718 HadError = true; 1719 goto retry; 1720 } 1721 1722 Tok.Kind = MMToken::IntegerLiteral; 1723 Tok.IntegerValue = Value; 1724 break; 1725 } 1726 1727 case tok::comment: 1728 goto retry; 1729 1730 case tok::hash: 1731 // A module map can be terminated prematurely by 1732 // #pragma clang module contents 1733 // When building the module, we'll treat the rest of the file as the 1734 // contents of the module. 1735 { 1736 auto NextIsIdent = [&](StringRef Str) -> bool { 1737 L.LexFromRawLexer(LToken); 1738 return !LToken.isAtStartOfLine() && LToken.is(tok::raw_identifier) && 1739 LToken.getRawIdentifier() == Str; 1740 }; 1741 if (NextIsIdent("pragma") && NextIsIdent("clang") && 1742 NextIsIdent("module") && NextIsIdent("contents")) { 1743 Tok.Kind = MMToken::EndOfFile; 1744 break; 1745 } 1746 } 1747 [[fallthrough]]; 1748 1749 default: 1750 Diags.Report(Tok.getLocation(), diag::err_mmap_unknown_token); 1751 HadError = true; 1752 goto retry; 1753 } 1754 1755 return Result; 1756 } 1757 1758 void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 1759 unsigned braceDepth = 0; 1760 unsigned squareDepth = 0; 1761 do { 1762 switch (Tok.Kind) { 1763 case MMToken::EndOfFile: 1764 return; 1765 1766 case MMToken::LBrace: 1767 if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1768 return; 1769 1770 ++braceDepth; 1771 break; 1772 1773 case MMToken::LSquare: 1774 if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1775 return; 1776 1777 ++squareDepth; 1778 break; 1779 1780 case MMToken::RBrace: 1781 if (braceDepth > 0) 1782 --braceDepth; 1783 else if (Tok.is(K)) 1784 return; 1785 break; 1786 1787 case MMToken::RSquare: 1788 if (squareDepth > 0) 1789 --squareDepth; 1790 else if (Tok.is(K)) 1791 return; 1792 break; 1793 1794 default: 1795 if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 1796 return; 1797 break; 1798 } 1799 1800 consumeToken(); 1801 } while (true); 1802 } 1803 1804 /// Parse a module-id. 1805 /// 1806 /// module-id: 1807 /// identifier 1808 /// identifier '.' module-id 1809 /// 1810 /// \returns true if an error occurred, false otherwise. 1811 bool ModuleMapParser::parseModuleId(ModuleId &Id) { 1812 Id.clear(); 1813 do { 1814 if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) { 1815 Id.push_back( 1816 std::make_pair(std::string(Tok.getString()), Tok.getLocation())); 1817 consumeToken(); 1818 } else { 1819 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 1820 return true; 1821 } 1822 1823 if (!Tok.is(MMToken::Period)) 1824 break; 1825 1826 consumeToken(); 1827 } while (true); 1828 1829 return false; 1830 } 1831 1832 namespace { 1833 1834 /// Enumerates the known attributes. 1835 enum AttributeKind { 1836 /// An unknown attribute. 1837 AT_unknown, 1838 1839 /// The 'system' attribute. 1840 AT_system, 1841 1842 /// The 'extern_c' attribute. 1843 AT_extern_c, 1844 1845 /// The 'exhaustive' attribute. 1846 AT_exhaustive, 1847 1848 /// The 'no_undeclared_includes' attribute. 1849 AT_no_undeclared_includes 1850 }; 1851 1852 } // namespace 1853 1854 /// Private modules are canonicalized as Foo_Private. Clang provides extra 1855 /// module map search logic to find the appropriate private module when PCH 1856 /// is used with implicit module maps. Warn when private modules are written 1857 /// in other ways (FooPrivate and Foo.Private), providing notes and fixits. 1858 void ModuleMapParser::diagnosePrivateModules(SourceLocation ExplicitLoc, 1859 SourceLocation FrameworkLoc) { 1860 auto GenNoteAndFixIt = [&](StringRef BadName, StringRef Canonical, 1861 const Module *M, SourceRange ReplLoc) { 1862 auto D = Diags.Report(ActiveModule->DefinitionLoc, 1863 diag::note_mmap_rename_top_level_private_module); 1864 D << BadName << M->Name; 1865 D << FixItHint::CreateReplacement(ReplLoc, Canonical); 1866 }; 1867 1868 for (auto E = Map.module_begin(); E != Map.module_end(); ++E) { 1869 auto const *M = E->getValue(); 1870 if (M->Directory != ActiveModule->Directory) 1871 continue; 1872 1873 SmallString<128> FullName(ActiveModule->getFullModuleName()); 1874 if (!FullName.starts_with(M->Name) && !FullName.ends_with("Private")) 1875 continue; 1876 SmallString<128> FixedPrivModDecl; 1877 SmallString<128> Canonical(M->Name); 1878 Canonical.append("_Private"); 1879 1880 // Foo.Private -> Foo_Private 1881 if (ActiveModule->Parent && ActiveModule->Name == "Private" && !M->Parent && 1882 M->Name == ActiveModule->Parent->Name) { 1883 Diags.Report(ActiveModule->DefinitionLoc, 1884 diag::warn_mmap_mismatched_private_submodule) 1885 << FullName; 1886 1887 SourceLocation FixItInitBegin = CurrModuleDeclLoc; 1888 if (FrameworkLoc.isValid()) 1889 FixItInitBegin = FrameworkLoc; 1890 if (ExplicitLoc.isValid()) 1891 FixItInitBegin = ExplicitLoc; 1892 1893 if (FrameworkLoc.isValid() || ActiveModule->Parent->IsFramework) 1894 FixedPrivModDecl.append("framework "); 1895 FixedPrivModDecl.append("module "); 1896 FixedPrivModDecl.append(Canonical); 1897 1898 GenNoteAndFixIt(FullName, FixedPrivModDecl, M, 1899 SourceRange(FixItInitBegin, ActiveModule->DefinitionLoc)); 1900 continue; 1901 } 1902 1903 // FooPrivate and whatnots -> Foo_Private 1904 if (!ActiveModule->Parent && !M->Parent && M->Name != ActiveModule->Name && 1905 ActiveModule->Name != Canonical) { 1906 Diags.Report(ActiveModule->DefinitionLoc, 1907 diag::warn_mmap_mismatched_private_module_name) 1908 << ActiveModule->Name; 1909 GenNoteAndFixIt(ActiveModule->Name, Canonical, M, 1910 SourceRange(ActiveModule->DefinitionLoc)); 1911 } 1912 } 1913 } 1914 1915 /// Parse a module declaration. 1916 /// 1917 /// module-declaration: 1918 /// 'extern' 'module' module-id string-literal 1919 /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 1920 /// { module-member* } 1921 /// 1922 /// module-member: 1923 /// requires-declaration 1924 /// header-declaration 1925 /// submodule-declaration 1926 /// export-declaration 1927 /// export-as-declaration 1928 /// link-declaration 1929 /// 1930 /// submodule-declaration: 1931 /// module-declaration 1932 /// inferred-submodule-declaration 1933 void ModuleMapParser::parseModuleDecl() { 1934 assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 1935 Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword)); 1936 if (Tok.is(MMToken::ExternKeyword)) { 1937 parseExternModuleDecl(); 1938 return; 1939 } 1940 1941 // Parse 'explicit' or 'framework' keyword, if present. 1942 SourceLocation ExplicitLoc; 1943 SourceLocation FrameworkLoc; 1944 bool Explicit = false; 1945 bool Framework = false; 1946 1947 // Parse 'explicit' keyword, if present. 1948 if (Tok.is(MMToken::ExplicitKeyword)) { 1949 ExplicitLoc = consumeToken(); 1950 Explicit = true; 1951 } 1952 1953 // Parse 'framework' keyword, if present. 1954 if (Tok.is(MMToken::FrameworkKeyword)) { 1955 FrameworkLoc = consumeToken(); 1956 Framework = true; 1957 } 1958 1959 // Parse 'module' keyword. 1960 if (!Tok.is(MMToken::ModuleKeyword)) { 1961 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1962 consumeToken(); 1963 HadError = true; 1964 return; 1965 } 1966 CurrModuleDeclLoc = consumeToken(); // 'module' keyword 1967 1968 // If we have a wildcard for the module name, this is an inferred submodule. 1969 // Parse it. 1970 if (Tok.is(MMToken::Star)) 1971 return parseInferredModuleDecl(Framework, Explicit); 1972 1973 // Parse the module name. 1974 ModuleId Id; 1975 if (parseModuleId(Id)) { 1976 HadError = true; 1977 return; 1978 } 1979 1980 if (ActiveModule) { 1981 if (Id.size() > 1) { 1982 Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 1983 << SourceRange(Id.front().second, Id.back().second); 1984 1985 HadError = true; 1986 return; 1987 } 1988 } else if (Id.size() == 1 && Explicit) { 1989 // Top-level modules can't be explicit. 1990 Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 1991 Explicit = false; 1992 ExplicitLoc = SourceLocation(); 1993 HadError = true; 1994 } 1995 1996 Module *PreviousActiveModule = ActiveModule; 1997 if (Id.size() > 1) { 1998 // This module map defines a submodule. Go find the module of which it 1999 // is a submodule. 2000 ActiveModule = nullptr; 2001 const Module *TopLevelModule = nullptr; 2002 for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 2003 if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 2004 if (I == 0) 2005 TopLevelModule = Next; 2006 ActiveModule = Next; 2007 continue; 2008 } 2009 2010 Diags.Report(Id[I].second, diag::err_mmap_missing_parent_module) 2011 << Id[I].first << (ActiveModule != nullptr) 2012 << (ActiveModule 2013 ? ActiveModule->getTopLevelModule()->getFullModuleName() 2014 : ""); 2015 HadError = true; 2016 } 2017 2018 if (TopLevelModule && 2019 ModuleMapFID != Map.getContainingModuleMapFileID(TopLevelModule)) { 2020 assert(ModuleMapFID != 2021 Map.getModuleMapFileIDForUniquing(TopLevelModule) && 2022 "submodule defined in same file as 'module *' that allowed its " 2023 "top-level module"); 2024 Map.addAdditionalModuleMapFile( 2025 TopLevelModule, *SourceMgr.getFileEntryRefForID(ModuleMapFID)); 2026 } 2027 } 2028 2029 StringRef ModuleName = Id.back().first; 2030 SourceLocation ModuleNameLoc = Id.back().second; 2031 2032 // Parse the optional attribute list. 2033 Attributes Attrs; 2034 if (parseOptionalAttributes(Attrs)) 2035 return; 2036 2037 // Parse the opening brace. 2038 if (!Tok.is(MMToken::LBrace)) { 2039 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 2040 << ModuleName; 2041 HadError = true; 2042 return; 2043 } 2044 SourceLocation LBraceLoc = consumeToken(); 2045 2046 // Determine whether this (sub)module has already been defined. 2047 Module *ShadowingModule = nullptr; 2048 if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 2049 // We might see a (re)definition of a module that we already have a 2050 // definition for in four cases: 2051 // - If we loaded one definition from an AST file and we've just found a 2052 // corresponding definition in a module map file, or 2053 bool LoadedFromASTFile = Existing->IsFromModuleFile; 2054 // - If we previously inferred this module from different module map file. 2055 bool Inferred = Existing->IsInferred; 2056 // - If we're building a framework that vends a module map, we might've 2057 // previously seen the one in intermediate products and now the system 2058 // one. 2059 // FIXME: If we're parsing module map file that looks like this: 2060 // framework module FW { ... } 2061 // module FW.Sub { ... } 2062 // We can't check the framework qualifier, since it's not attached to 2063 // the definition of Sub. Checking that qualifier on \c Existing is 2064 // not correct either, since we might've previously seen: 2065 // module FW { ... } 2066 // module FW.Sub { ... } 2067 // We should enforce consistency of redefinitions so that we can rely 2068 // that \c Existing is part of a framework iff the redefinition of FW 2069 // we have just skipped had it too. Once we do that, stop checking 2070 // the local framework qualifier and only rely on \c Existing. 2071 bool PartOfFramework = Framework || Existing->isPartOfFramework(); 2072 // - If we're building a (preprocessed) module and we've just loaded the 2073 // module map file from which it was created. 2074 bool ParsedAsMainInput = 2075 Map.LangOpts.getCompilingModule() == LangOptions::CMK_ModuleMap && 2076 Map.LangOpts.CurrentModule == ModuleName && 2077 SourceMgr.getDecomposedLoc(ModuleNameLoc).first != 2078 SourceMgr.getDecomposedLoc(Existing->DefinitionLoc).first; 2079 if (LoadedFromASTFile || Inferred || PartOfFramework || ParsedAsMainInput) { 2080 ActiveModule = PreviousActiveModule; 2081 // Skip the module definition. 2082 skipUntil(MMToken::RBrace); 2083 if (Tok.is(MMToken::RBrace)) 2084 consumeToken(); 2085 else { 2086 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 2087 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 2088 HadError = true; 2089 } 2090 return; 2091 } 2092 2093 if (!Existing->Parent && Map.mayShadowNewModule(Existing)) { 2094 ShadowingModule = Existing; 2095 } else { 2096 // This is not a shawdowed module decl, it is an illegal redefinition. 2097 Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 2098 << ModuleName; 2099 Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 2100 2101 // Skip the module definition. 2102 skipUntil(MMToken::RBrace); 2103 if (Tok.is(MMToken::RBrace)) 2104 consumeToken(); 2105 2106 HadError = true; 2107 return; 2108 } 2109 } 2110 2111 // Start defining this module. 2112 if (ShadowingModule) { 2113 ActiveModule = 2114 Map.createShadowedModule(ModuleName, Framework, ShadowingModule); 2115 } else { 2116 ActiveModule = 2117 Map.findOrCreateModule(ModuleName, ActiveModule, Framework, Explicit) 2118 .first; 2119 } 2120 2121 ActiveModule->DefinitionLoc = ModuleNameLoc; 2122 if (Attrs.IsSystem || IsSystem) 2123 ActiveModule->IsSystem = true; 2124 if (Attrs.IsExternC) 2125 ActiveModule->IsExternC = true; 2126 if (Attrs.NoUndeclaredIncludes) 2127 ActiveModule->NoUndeclaredIncludes = true; 2128 ActiveModule->Directory = Directory; 2129 2130 StringRef MapFileName( 2131 SourceMgr.getFileEntryRefForID(ModuleMapFID)->getName()); 2132 if (MapFileName.ends_with("module.private.modulemap") || 2133 MapFileName.ends_with("module_private.map")) { 2134 ActiveModule->ModuleMapIsPrivate = true; 2135 } 2136 2137 // Private modules named as FooPrivate, Foo.Private or similar are likely a 2138 // user error; provide warnings, notes and fixits to direct users to use 2139 // Foo_Private instead. 2140 SourceLocation StartLoc = 2141 SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 2142 if (Map.HeaderInfo.getHeaderSearchOpts().ImplicitModuleMaps && 2143 !Diags.isIgnored(diag::warn_mmap_mismatched_private_submodule, 2144 StartLoc) && 2145 !Diags.isIgnored(diag::warn_mmap_mismatched_private_module_name, 2146 StartLoc) && 2147 ActiveModule->ModuleMapIsPrivate) 2148 diagnosePrivateModules(ExplicitLoc, FrameworkLoc); 2149 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::ConfigMacros: 2159 parseConfigMacros(); 2160 break; 2161 2162 case MMToken::Conflict: 2163 parseConflict(); 2164 break; 2165 2166 case MMToken::ExplicitKeyword: 2167 case MMToken::ExternKeyword: 2168 case MMToken::FrameworkKeyword: 2169 case MMToken::ModuleKeyword: 2170 parseModuleDecl(); 2171 break; 2172 2173 case MMToken::ExportKeyword: 2174 parseExportDecl(); 2175 break; 2176 2177 case MMToken::ExportAsKeyword: 2178 parseExportAsDecl(); 2179 break; 2180 2181 case MMToken::UseKeyword: 2182 parseUseDecl(); 2183 break; 2184 2185 case MMToken::RequiresKeyword: 2186 parseRequiresDecl(); 2187 break; 2188 2189 case MMToken::TextualKeyword: 2190 parseHeaderDecl(MMToken::TextualKeyword, consumeToken()); 2191 break; 2192 2193 case MMToken::UmbrellaKeyword: { 2194 SourceLocation UmbrellaLoc = consumeToken(); 2195 if (Tok.is(MMToken::HeaderKeyword)) 2196 parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc); 2197 else 2198 parseUmbrellaDirDecl(UmbrellaLoc); 2199 break; 2200 } 2201 2202 case MMToken::ExcludeKeyword: 2203 parseHeaderDecl(MMToken::ExcludeKeyword, consumeToken()); 2204 break; 2205 2206 case MMToken::PrivateKeyword: 2207 parseHeaderDecl(MMToken::PrivateKeyword, consumeToken()); 2208 break; 2209 2210 case MMToken::HeaderKeyword: 2211 parseHeaderDecl(MMToken::HeaderKeyword, consumeToken()); 2212 break; 2213 2214 case MMToken::LinkKeyword: 2215 parseLinkDecl(); 2216 break; 2217 2218 default: 2219 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 2220 consumeToken(); 2221 break; 2222 } 2223 } while (!Done); 2224 2225 if (Tok.is(MMToken::RBrace)) 2226 consumeToken(); 2227 else { 2228 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 2229 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 2230 HadError = true; 2231 } 2232 2233 // If the active module is a top-level framework, and there are no link 2234 // libraries, automatically link against the framework. 2235 if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() && 2236 ActiveModule->LinkLibraries.empty()) 2237 inferFrameworkLink(ActiveModule); 2238 2239 // If the module meets all requirements but is still unavailable, mark the 2240 // whole tree as unavailable to prevent it from building. 2241 if (!ActiveModule->IsAvailable && !ActiveModule->IsUnimportable && 2242 ActiveModule->Parent) { 2243 ActiveModule->getTopLevelModule()->markUnavailable(/*Unimportable=*/false); 2244 ActiveModule->getTopLevelModule()->MissingHeaders.append( 2245 ActiveModule->MissingHeaders.begin(), ActiveModule->MissingHeaders.end()); 2246 } 2247 2248 // We're done parsing this module. Pop back to the previous module. 2249 ActiveModule = PreviousActiveModule; 2250 } 2251 2252 /// Parse an extern module declaration. 2253 /// 2254 /// extern module-declaration: 2255 /// 'extern' 'module' module-id string-literal 2256 void ModuleMapParser::parseExternModuleDecl() { 2257 assert(Tok.is(MMToken::ExternKeyword)); 2258 SourceLocation ExternLoc = consumeToken(); // 'extern' keyword 2259 2260 // Parse 'module' keyword. 2261 if (!Tok.is(MMToken::ModuleKeyword)) { 2262 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 2263 consumeToken(); 2264 HadError = true; 2265 return; 2266 } 2267 consumeToken(); // 'module' keyword 2268 2269 // Parse the module name. 2270 ModuleId Id; 2271 if (parseModuleId(Id)) { 2272 HadError = true; 2273 return; 2274 } 2275 2276 // Parse the referenced module map file name. 2277 if (!Tok.is(MMToken::StringLiteral)) { 2278 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file); 2279 HadError = true; 2280 return; 2281 } 2282 std::string FileName = std::string(Tok.getString()); 2283 consumeToken(); // filename 2284 2285 StringRef FileNameRef = FileName; 2286 SmallString<128> ModuleMapFileName; 2287 if (llvm::sys::path::is_relative(FileNameRef)) { 2288 ModuleMapFileName += Directory.getName(); 2289 llvm::sys::path::append(ModuleMapFileName, FileName); 2290 FileNameRef = ModuleMapFileName; 2291 } 2292 if (auto File = SourceMgr.getFileManager().getOptionalFileRef(FileNameRef)) 2293 Map.parseModuleMapFile( 2294 *File, IsSystem, 2295 Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd 2296 ? Directory 2297 : File->getDir(), 2298 FileID(), nullptr, ExternLoc); 2299 } 2300 2301 /// Whether to add the requirement \p Feature to the module \p M. 2302 /// 2303 /// This preserves backwards compatibility for two hacks in the Darwin system 2304 /// module map files: 2305 /// 2306 /// 1. The use of 'requires excluded' to make headers non-modular, which 2307 /// should really be mapped to 'textual' now that we have this feature. We 2308 /// drop the 'excluded' requirement, and set \p IsRequiresExcludedHack to 2309 /// true. Later, this bit will be used to map all the headers inside this 2310 /// module to 'textual'. 2311 /// 2312 /// This affects Darwin.C.excluded (for assert.h) and Tcl.Private. 2313 /// 2314 /// 2. Removes a bogus cplusplus requirement from IOKit.avc. This requirement 2315 /// was never correct and causes issues now that we check it, so drop it. 2316 static bool shouldAddRequirement(Module *M, StringRef Feature, 2317 bool &IsRequiresExcludedHack) { 2318 if (Feature == "excluded" && 2319 (M->fullModuleNameIs({"Darwin", "C", "excluded"}) || 2320 M->fullModuleNameIs({"Tcl", "Private"}))) { 2321 IsRequiresExcludedHack = true; 2322 return false; 2323 } else if (Feature == "cplusplus" && M->fullModuleNameIs({"IOKit", "avc"})) { 2324 return false; 2325 } 2326 2327 return true; 2328 } 2329 2330 /// Parse a requires declaration. 2331 /// 2332 /// requires-declaration: 2333 /// 'requires' feature-list 2334 /// 2335 /// feature-list: 2336 /// feature ',' feature-list 2337 /// feature 2338 /// 2339 /// feature: 2340 /// '!'[opt] identifier 2341 void ModuleMapParser::parseRequiresDecl() { 2342 assert(Tok.is(MMToken::RequiresKeyword)); 2343 2344 // Parse 'requires' keyword. 2345 consumeToken(); 2346 2347 // Parse the feature-list. 2348 do { 2349 bool RequiredState = true; 2350 if (Tok.is(MMToken::Exclaim)) { 2351 RequiredState = false; 2352 consumeToken(); 2353 } 2354 2355 if (!Tok.is(MMToken::Identifier)) { 2356 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 2357 HadError = true; 2358 return; 2359 } 2360 2361 // Consume the feature name. 2362 std::string Feature = std::string(Tok.getString()); 2363 consumeToken(); 2364 2365 bool IsRequiresExcludedHack = false; 2366 bool ShouldAddRequirement = 2367 shouldAddRequirement(ActiveModule, Feature, IsRequiresExcludedHack); 2368 2369 if (IsRequiresExcludedHack) 2370 UsesRequiresExcludedHack.insert(ActiveModule); 2371 2372 if (ShouldAddRequirement) { 2373 // Add this feature. 2374 ActiveModule->addRequirement(Feature, RequiredState, Map.LangOpts, 2375 *Map.Target); 2376 } 2377 2378 if (!Tok.is(MMToken::Comma)) 2379 break; 2380 2381 // Consume the comma. 2382 consumeToken(); 2383 } while (true); 2384 } 2385 2386 /// Parse a header declaration. 2387 /// 2388 /// header-declaration: 2389 /// 'textual'[opt] 'header' string-literal 2390 /// 'private' 'textual'[opt] 'header' string-literal 2391 /// 'exclude' 'header' string-literal 2392 /// 'umbrella' 'header' string-literal 2393 /// 2394 /// FIXME: Support 'private textual header'. 2395 void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken, 2396 SourceLocation LeadingLoc) { 2397 // We've already consumed the first token. 2398 ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader; 2399 2400 if (LeadingToken == MMToken::PrivateKeyword) { 2401 Role = ModuleMap::PrivateHeader; 2402 // 'private' may optionally be followed by 'textual'. 2403 if (Tok.is(MMToken::TextualKeyword)) { 2404 LeadingToken = Tok.Kind; 2405 consumeToken(); 2406 } 2407 } else if (LeadingToken == MMToken::ExcludeKeyword) { 2408 Role = ModuleMap::ExcludedHeader; 2409 } 2410 2411 if (LeadingToken == MMToken::TextualKeyword) 2412 Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader); 2413 2414 if (UsesRequiresExcludedHack.count(ActiveModule)) { 2415 // Mark this header 'textual' (see doc comment for 2416 // Module::UsesRequiresExcludedHack). 2417 Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader); 2418 } 2419 2420 if (LeadingToken != MMToken::HeaderKeyword) { 2421 if (!Tok.is(MMToken::HeaderKeyword)) { 2422 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 2423 << (LeadingToken == MMToken::PrivateKeyword ? "private" : 2424 LeadingToken == MMToken::ExcludeKeyword ? "exclude" : 2425 LeadingToken == MMToken::TextualKeyword ? "textual" : "umbrella"); 2426 return; 2427 } 2428 consumeToken(); 2429 } 2430 2431 // Parse the header name. 2432 if (!Tok.is(MMToken::StringLiteral)) { 2433 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 2434 << "header"; 2435 HadError = true; 2436 return; 2437 } 2438 Module::UnresolvedHeaderDirective Header; 2439 Header.FileName = std::string(Tok.getString()); 2440 Header.FileNameLoc = consumeToken(); 2441 Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword; 2442 Header.Kind = Map.headerRoleToKind(Role); 2443 2444 // Check whether we already have an umbrella. 2445 if (Header.IsUmbrella && 2446 !std::holds_alternative<std::monostate>(ActiveModule->Umbrella)) { 2447 Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash) 2448 << ActiveModule->getFullModuleName(); 2449 HadError = true; 2450 return; 2451 } 2452 2453 // If we were given stat information, parse it so we can skip looking for 2454 // the file. 2455 if (Tok.is(MMToken::LBrace)) { 2456 SourceLocation LBraceLoc = consumeToken(); 2457 2458 while (!Tok.is(MMToken::RBrace) && !Tok.is(MMToken::EndOfFile)) { 2459 enum Attribute { Size, ModTime, Unknown }; 2460 StringRef Str = Tok.getString(); 2461 SourceLocation Loc = consumeToken(); 2462 switch (llvm::StringSwitch<Attribute>(Str) 2463 .Case("size", Size) 2464 .Case("mtime", ModTime) 2465 .Default(Unknown)) { 2466 case Size: 2467 if (Header.Size) 2468 Diags.Report(Loc, diag::err_mmap_duplicate_header_attribute) << Str; 2469 if (!Tok.is(MMToken::IntegerLiteral)) { 2470 Diags.Report(Tok.getLocation(), 2471 diag::err_mmap_invalid_header_attribute_value) << Str; 2472 skipUntil(MMToken::RBrace); 2473 break; 2474 } 2475 Header.Size = Tok.getInteger(); 2476 consumeToken(); 2477 break; 2478 2479 case ModTime: 2480 if (Header.ModTime) 2481 Diags.Report(Loc, diag::err_mmap_duplicate_header_attribute) << Str; 2482 if (!Tok.is(MMToken::IntegerLiteral)) { 2483 Diags.Report(Tok.getLocation(), 2484 diag::err_mmap_invalid_header_attribute_value) << Str; 2485 skipUntil(MMToken::RBrace); 2486 break; 2487 } 2488 Header.ModTime = Tok.getInteger(); 2489 consumeToken(); 2490 break; 2491 2492 case Unknown: 2493 Diags.Report(Loc, diag::err_mmap_expected_header_attribute); 2494 skipUntil(MMToken::RBrace); 2495 break; 2496 } 2497 } 2498 2499 if (Tok.is(MMToken::RBrace)) 2500 consumeToken(); 2501 else { 2502 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 2503 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 2504 HadError = true; 2505 } 2506 } 2507 2508 bool NeedsFramework = false; 2509 // Don't add headers to the builtin modules if the builtin headers belong to 2510 // the system modules, with the exception of __stddef_max_align_t.h which 2511 // always had its own module. 2512 if (!Map.LangOpts.BuiltinHeadersInSystemModules || 2513 !isBuiltInModuleName(ActiveModule->getTopLevelModuleName()) || 2514 ActiveModule->fullModuleNameIs({"_Builtin_stddef", "max_align_t"})) 2515 Map.addUnresolvedHeader(ActiveModule, std::move(Header), NeedsFramework); 2516 2517 if (NeedsFramework) 2518 Diags.Report(CurrModuleDeclLoc, diag::note_mmap_add_framework_keyword) 2519 << ActiveModule->getFullModuleName() 2520 << FixItHint::CreateReplacement(CurrModuleDeclLoc, "framework module"); 2521 } 2522 2523 static bool compareModuleHeaders(const Module::Header &A, 2524 const Module::Header &B) { 2525 return A.NameAsWritten < B.NameAsWritten; 2526 } 2527 2528 /// Parse an umbrella directory declaration. 2529 /// 2530 /// umbrella-dir-declaration: 2531 /// umbrella string-literal 2532 void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 2533 // Parse the directory name. 2534 if (!Tok.is(MMToken::StringLiteral)) { 2535 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 2536 << "umbrella"; 2537 HadError = true; 2538 return; 2539 } 2540 2541 std::string DirName = std::string(Tok.getString()); 2542 std::string DirNameAsWritten = DirName; 2543 SourceLocation DirNameLoc = consumeToken(); 2544 2545 // Check whether we already have an umbrella. 2546 if (!std::holds_alternative<std::monostate>(ActiveModule->Umbrella)) { 2547 Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 2548 << ActiveModule->getFullModuleName(); 2549 HadError = true; 2550 return; 2551 } 2552 2553 // Look for this file. 2554 OptionalDirectoryEntryRef Dir; 2555 if (llvm::sys::path::is_absolute(DirName)) { 2556 Dir = SourceMgr.getFileManager().getOptionalDirectoryRef(DirName); 2557 } else { 2558 SmallString<128> PathName; 2559 PathName = Directory.getName(); 2560 llvm::sys::path::append(PathName, DirName); 2561 Dir = SourceMgr.getFileManager().getOptionalDirectoryRef(PathName); 2562 } 2563 2564 if (!Dir) { 2565 Diags.Report(DirNameLoc, diag::warn_mmap_umbrella_dir_not_found) 2566 << DirName; 2567 return; 2568 } 2569 2570 if (UsesRequiresExcludedHack.count(ActiveModule)) { 2571 // Mark this header 'textual' (see doc comment for 2572 // ModuleMapParser::UsesRequiresExcludedHack). Although iterating over the 2573 // directory is relatively expensive, in practice this only applies to the 2574 // uncommonly used Tcl module on Darwin platforms. 2575 std::error_code EC; 2576 SmallVector<Module::Header, 6> Headers; 2577 llvm::vfs::FileSystem &FS = 2578 SourceMgr.getFileManager().getVirtualFileSystem(); 2579 for (llvm::vfs::recursive_directory_iterator I(FS, Dir->getName(), EC), E; 2580 I != E && !EC; I.increment(EC)) { 2581 if (auto FE = SourceMgr.getFileManager().getOptionalFileRef(I->path())) { 2582 Module::Header Header = {"", std::string(I->path()), *FE}; 2583 Headers.push_back(std::move(Header)); 2584 } 2585 } 2586 2587 // Sort header paths so that the pcm doesn't depend on iteration order. 2588 std::stable_sort(Headers.begin(), Headers.end(), compareModuleHeaders); 2589 2590 for (auto &Header : Headers) 2591 Map.addHeader(ActiveModule, std::move(Header), ModuleMap::TextualHeader); 2592 return; 2593 } 2594 2595 if (Module *OwningModule = Map.UmbrellaDirs[*Dir]) { 2596 Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 2597 << OwningModule->getFullModuleName(); 2598 HadError = true; 2599 return; 2600 } 2601 2602 // Record this umbrella directory. 2603 Map.setUmbrellaDirAsWritten(ActiveModule, *Dir, DirNameAsWritten, DirName); 2604 } 2605 2606 /// Parse a module export declaration. 2607 /// 2608 /// export-declaration: 2609 /// 'export' wildcard-module-id 2610 /// 2611 /// wildcard-module-id: 2612 /// identifier 2613 /// '*' 2614 /// identifier '.' wildcard-module-id 2615 void ModuleMapParser::parseExportDecl() { 2616 assert(Tok.is(MMToken::ExportKeyword)); 2617 SourceLocation ExportLoc = consumeToken(); 2618 2619 // Parse the module-id with an optional wildcard at the end. 2620 ModuleId ParsedModuleId; 2621 bool Wildcard = false; 2622 do { 2623 // FIXME: Support string-literal module names here. 2624 if (Tok.is(MMToken::Identifier)) { 2625 ParsedModuleId.push_back( 2626 std::make_pair(std::string(Tok.getString()), Tok.getLocation())); 2627 consumeToken(); 2628 2629 if (Tok.is(MMToken::Period)) { 2630 consumeToken(); 2631 continue; 2632 } 2633 2634 break; 2635 } 2636 2637 if(Tok.is(MMToken::Star)) { 2638 Wildcard = true; 2639 consumeToken(); 2640 break; 2641 } 2642 2643 Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); 2644 HadError = true; 2645 return; 2646 } while (true); 2647 2648 Module::UnresolvedExportDecl Unresolved = { 2649 ExportLoc, ParsedModuleId, Wildcard 2650 }; 2651 ActiveModule->UnresolvedExports.push_back(Unresolved); 2652 } 2653 2654 /// Parse a module export_as declaration. 2655 /// 2656 /// export-as-declaration: 2657 /// 'export_as' identifier 2658 void ModuleMapParser::parseExportAsDecl() { 2659 assert(Tok.is(MMToken::ExportAsKeyword)); 2660 consumeToken(); 2661 2662 if (!Tok.is(MMToken::Identifier)) { 2663 Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); 2664 HadError = true; 2665 return; 2666 } 2667 2668 if (ActiveModule->Parent) { 2669 Diags.Report(Tok.getLocation(), diag::err_mmap_submodule_export_as); 2670 consumeToken(); 2671 return; 2672 } 2673 2674 if (!ActiveModule->ExportAsModule.empty()) { 2675 if (ActiveModule->ExportAsModule == Tok.getString()) { 2676 Diags.Report(Tok.getLocation(), diag::warn_mmap_redundant_export_as) 2677 << ActiveModule->Name << Tok.getString(); 2678 } else { 2679 Diags.Report(Tok.getLocation(), diag::err_mmap_conflicting_export_as) 2680 << ActiveModule->Name << ActiveModule->ExportAsModule 2681 << Tok.getString(); 2682 } 2683 } 2684 2685 ActiveModule->ExportAsModule = std::string(Tok.getString()); 2686 Map.addLinkAsDependency(ActiveModule); 2687 2688 consumeToken(); 2689 } 2690 2691 /// Parse a module use declaration. 2692 /// 2693 /// use-declaration: 2694 /// 'use' wildcard-module-id 2695 void ModuleMapParser::parseUseDecl() { 2696 assert(Tok.is(MMToken::UseKeyword)); 2697 auto KWLoc = consumeToken(); 2698 // Parse the module-id. 2699 ModuleId ParsedModuleId; 2700 parseModuleId(ParsedModuleId); 2701 2702 if (ActiveModule->Parent) 2703 Diags.Report(KWLoc, diag::err_mmap_use_decl_submodule); 2704 else 2705 ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId); 2706 } 2707 2708 /// Parse a link declaration. 2709 /// 2710 /// module-declaration: 2711 /// 'link' 'framework'[opt] string-literal 2712 void ModuleMapParser::parseLinkDecl() { 2713 assert(Tok.is(MMToken::LinkKeyword)); 2714 SourceLocation LinkLoc = consumeToken(); 2715 2716 // Parse the optional 'framework' keyword. 2717 bool IsFramework = false; 2718 if (Tok.is(MMToken::FrameworkKeyword)) { 2719 consumeToken(); 2720 IsFramework = true; 2721 } 2722 2723 // Parse the library name 2724 if (!Tok.is(MMToken::StringLiteral)) { 2725 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) 2726 << IsFramework << SourceRange(LinkLoc); 2727 HadError = true; 2728 return; 2729 } 2730 2731 std::string LibraryName = std::string(Tok.getString()); 2732 consumeToken(); 2733 ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, 2734 IsFramework)); 2735 } 2736 2737 /// Parse a configuration macro declaration. 2738 /// 2739 /// module-declaration: 2740 /// 'config_macros' attributes[opt] config-macro-list? 2741 /// 2742 /// config-macro-list: 2743 /// identifier (',' identifier)? 2744 void ModuleMapParser::parseConfigMacros() { 2745 assert(Tok.is(MMToken::ConfigMacros)); 2746 SourceLocation ConfigMacrosLoc = consumeToken(); 2747 2748 // Only top-level modules can have configuration macros. 2749 if (ActiveModule->Parent) { 2750 Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule); 2751 } 2752 2753 // Parse the optional attributes. 2754 Attributes Attrs; 2755 if (parseOptionalAttributes(Attrs)) 2756 return; 2757 2758 if (Attrs.IsExhaustive && !ActiveModule->Parent) { 2759 ActiveModule->ConfigMacrosExhaustive = true; 2760 } 2761 2762 // If we don't have an identifier, we're done. 2763 // FIXME: Support macros with the same name as a keyword here. 2764 if (!Tok.is(MMToken::Identifier)) 2765 return; 2766 2767 // Consume the first identifier. 2768 if (!ActiveModule->Parent) { 2769 ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 2770 } 2771 consumeToken(); 2772 2773 do { 2774 // If there's a comma, consume it. 2775 if (!Tok.is(MMToken::Comma)) 2776 break; 2777 consumeToken(); 2778 2779 // We expect to see a macro name here. 2780 // FIXME: Support macros with the same name as a keyword here. 2781 if (!Tok.is(MMToken::Identifier)) { 2782 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro); 2783 break; 2784 } 2785 2786 // Consume the macro name. 2787 if (!ActiveModule->Parent) { 2788 ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 2789 } 2790 consumeToken(); 2791 } while (true); 2792 } 2793 2794 /// Format a module-id into a string. 2795 static std::string formatModuleId(const ModuleId &Id) { 2796 std::string result; 2797 { 2798 llvm::raw_string_ostream OS(result); 2799 2800 for (unsigned I = 0, N = Id.size(); I != N; ++I) { 2801 if (I) 2802 OS << "."; 2803 OS << Id[I].first; 2804 } 2805 } 2806 2807 return result; 2808 } 2809 2810 /// Parse a conflict declaration. 2811 /// 2812 /// module-declaration: 2813 /// 'conflict' module-id ',' string-literal 2814 void ModuleMapParser::parseConflict() { 2815 assert(Tok.is(MMToken::Conflict)); 2816 SourceLocation ConflictLoc = consumeToken(); 2817 Module::UnresolvedConflict Conflict; 2818 2819 // Parse the module-id. 2820 if (parseModuleId(Conflict.Id)) 2821 return; 2822 2823 // Parse the ','. 2824 if (!Tok.is(MMToken::Comma)) { 2825 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma) 2826 << SourceRange(ConflictLoc); 2827 return; 2828 } 2829 consumeToken(); 2830 2831 // Parse the message. 2832 if (!Tok.is(MMToken::StringLiteral)) { 2833 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message) 2834 << formatModuleId(Conflict.Id); 2835 return; 2836 } 2837 Conflict.Message = Tok.getString().str(); 2838 consumeToken(); 2839 2840 // Add this unresolved conflict. 2841 ActiveModule->UnresolvedConflicts.push_back(Conflict); 2842 } 2843 2844 /// Parse an inferred module declaration (wildcard modules). 2845 /// 2846 /// module-declaration: 2847 /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] 2848 /// { inferred-module-member* } 2849 /// 2850 /// inferred-module-member: 2851 /// 'export' '*' 2852 /// 'exclude' identifier 2853 void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { 2854 assert(Tok.is(MMToken::Star)); 2855 SourceLocation StarLoc = consumeToken(); 2856 bool Failed = false; 2857 2858 // Inferred modules must be submodules. 2859 if (!ActiveModule && !Framework) { 2860 Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 2861 Failed = true; 2862 } 2863 2864 if (ActiveModule) { 2865 // Inferred modules must have umbrella directories. 2866 if (!Failed && ActiveModule->IsAvailable && 2867 !ActiveModule->getEffectiveUmbrellaDir()) { 2868 Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 2869 Failed = true; 2870 } 2871 2872 // Check for redefinition of an inferred module. 2873 if (!Failed && ActiveModule->InferSubmodules) { 2874 Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 2875 if (ActiveModule->InferredSubmoduleLoc.isValid()) 2876 Diags.Report(ActiveModule->InferredSubmoduleLoc, 2877 diag::note_mmap_prev_definition); 2878 Failed = true; 2879 } 2880 2881 // Check for the 'framework' keyword, which is not permitted here. 2882 if (Framework) { 2883 Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); 2884 Framework = false; 2885 } 2886 } else if (Explicit) { 2887 Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); 2888 Explicit = false; 2889 } 2890 2891 // If there were any problems with this inferred submodule, skip its body. 2892 if (Failed) { 2893 if (Tok.is(MMToken::LBrace)) { 2894 consumeToken(); 2895 skipUntil(MMToken::RBrace); 2896 if (Tok.is(MMToken::RBrace)) 2897 consumeToken(); 2898 } 2899 HadError = true; 2900 return; 2901 } 2902 2903 // Parse optional attributes. 2904 Attributes Attrs; 2905 if (parseOptionalAttributes(Attrs)) 2906 return; 2907 2908 if (ActiveModule) { 2909 // Note that we have an inferred submodule. 2910 ActiveModule->InferSubmodules = true; 2911 ActiveModule->InferredSubmoduleLoc = StarLoc; 2912 ActiveModule->InferExplicitSubmodules = Explicit; 2913 } else { 2914 // We'll be inferring framework modules for this directory. 2915 Map.InferredDirectories[Directory].InferModules = true; 2916 Map.InferredDirectories[Directory].Attrs = Attrs; 2917 Map.InferredDirectories[Directory].ModuleMapFID = ModuleMapFID; 2918 // FIXME: Handle the 'framework' keyword. 2919 } 2920 2921 // Parse the opening brace. 2922 if (!Tok.is(MMToken::LBrace)) { 2923 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 2924 HadError = true; 2925 return; 2926 } 2927 SourceLocation LBraceLoc = consumeToken(); 2928 2929 // Parse the body of the inferred submodule. 2930 bool Done = false; 2931 do { 2932 switch (Tok.Kind) { 2933 case MMToken::EndOfFile: 2934 case MMToken::RBrace: 2935 Done = true; 2936 break; 2937 2938 case MMToken::ExcludeKeyword: 2939 if (ActiveModule) { 2940 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2941 << (ActiveModule != nullptr); 2942 consumeToken(); 2943 break; 2944 } 2945 2946 consumeToken(); 2947 // FIXME: Support string-literal module names here. 2948 if (!Tok.is(MMToken::Identifier)) { 2949 Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); 2950 break; 2951 } 2952 2953 Map.InferredDirectories[Directory].ExcludedModules.push_back( 2954 std::string(Tok.getString())); 2955 consumeToken(); 2956 break; 2957 2958 case MMToken::ExportKeyword: 2959 if (!ActiveModule) { 2960 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2961 << (ActiveModule != nullptr); 2962 consumeToken(); 2963 break; 2964 } 2965 2966 consumeToken(); 2967 if (Tok.is(MMToken::Star)) 2968 ActiveModule->InferExportWildcard = true; 2969 else 2970 Diags.Report(Tok.getLocation(), 2971 diag::err_mmap_expected_export_wildcard); 2972 consumeToken(); 2973 break; 2974 2975 case MMToken::ExplicitKeyword: 2976 case MMToken::ModuleKeyword: 2977 case MMToken::HeaderKeyword: 2978 case MMToken::PrivateKeyword: 2979 case MMToken::UmbrellaKeyword: 2980 default: 2981 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2982 << (ActiveModule != nullptr); 2983 consumeToken(); 2984 break; 2985 } 2986 } while (!Done); 2987 2988 if (Tok.is(MMToken::RBrace)) 2989 consumeToken(); 2990 else { 2991 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 2992 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 2993 HadError = true; 2994 } 2995 } 2996 2997 /// Parse optional attributes. 2998 /// 2999 /// attributes: 3000 /// attribute attributes 3001 /// attribute 3002 /// 3003 /// attribute: 3004 /// [ identifier ] 3005 /// 3006 /// \param Attrs Will be filled in with the parsed attributes. 3007 /// 3008 /// \returns true if an error occurred, false otherwise. 3009 bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { 3010 bool HadError = false; 3011 3012 while (Tok.is(MMToken::LSquare)) { 3013 // Consume the '['. 3014 SourceLocation LSquareLoc = consumeToken(); 3015 3016 // Check whether we have an attribute name here. 3017 if (!Tok.is(MMToken::Identifier)) { 3018 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 3019 skipUntil(MMToken::RSquare); 3020 if (Tok.is(MMToken::RSquare)) 3021 consumeToken(); 3022 HadError = true; 3023 } 3024 3025 // Decode the attribute name. 3026 AttributeKind Attribute 3027 = llvm::StringSwitch<AttributeKind>(Tok.getString()) 3028 .Case("exhaustive", AT_exhaustive) 3029 .Case("extern_c", AT_extern_c) 3030 .Case("no_undeclared_includes", AT_no_undeclared_includes) 3031 .Case("system", AT_system) 3032 .Default(AT_unknown); 3033 switch (Attribute) { 3034 case AT_unknown: 3035 Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 3036 << Tok.getString(); 3037 break; 3038 3039 case AT_system: 3040 Attrs.IsSystem = true; 3041 break; 3042 3043 case AT_extern_c: 3044 Attrs.IsExternC = true; 3045 break; 3046 3047 case AT_exhaustive: 3048 Attrs.IsExhaustive = true; 3049 break; 3050 3051 case AT_no_undeclared_includes: 3052 Attrs.NoUndeclaredIncludes = true; 3053 break; 3054 } 3055 consumeToken(); 3056 3057 // Consume the ']'. 3058 if (!Tok.is(MMToken::RSquare)) { 3059 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 3060 Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 3061 skipUntil(MMToken::RSquare); 3062 HadError = true; 3063 } 3064 3065 if (Tok.is(MMToken::RSquare)) 3066 consumeToken(); 3067 } 3068 3069 return HadError; 3070 } 3071 3072 /// Parse a module map file. 3073 /// 3074 /// module-map-file: 3075 /// module-declaration* 3076 bool ModuleMapParser::parseModuleMapFile() { 3077 do { 3078 switch (Tok.Kind) { 3079 case MMToken::EndOfFile: 3080 return HadError; 3081 3082 case MMToken::ExplicitKeyword: 3083 case MMToken::ExternKeyword: 3084 case MMToken::ModuleKeyword: 3085 case MMToken::FrameworkKeyword: 3086 parseModuleDecl(); 3087 break; 3088 3089 case MMToken::Comma: 3090 case MMToken::ConfigMacros: 3091 case MMToken::Conflict: 3092 case MMToken::Exclaim: 3093 case MMToken::ExcludeKeyword: 3094 case MMToken::ExportKeyword: 3095 case MMToken::ExportAsKeyword: 3096 case MMToken::HeaderKeyword: 3097 case MMToken::Identifier: 3098 case MMToken::LBrace: 3099 case MMToken::LinkKeyword: 3100 case MMToken::LSquare: 3101 case MMToken::Period: 3102 case MMToken::PrivateKeyword: 3103 case MMToken::RBrace: 3104 case MMToken::RSquare: 3105 case MMToken::RequiresKeyword: 3106 case MMToken::Star: 3107 case MMToken::StringLiteral: 3108 case MMToken::IntegerLiteral: 3109 case MMToken::TextualKeyword: 3110 case MMToken::UmbrellaKeyword: 3111 case MMToken::UseKeyword: 3112 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 3113 HadError = true; 3114 consumeToken(); 3115 break; 3116 } 3117 } while (true); 3118 } 3119 3120 bool ModuleMap::parseModuleMapFile(FileEntryRef File, bool IsSystem, 3121 DirectoryEntryRef Dir, FileID ID, 3122 unsigned *Offset, 3123 SourceLocation ExternModuleLoc) { 3124 assert(Target && "Missing target information"); 3125 llvm::DenseMap<const FileEntry *, bool>::iterator Known 3126 = ParsedModuleMap.find(File); 3127 if (Known != ParsedModuleMap.end()) 3128 return Known->second; 3129 3130 // If the module map file wasn't already entered, do so now. 3131 if (ID.isInvalid()) { 3132 auto FileCharacter = 3133 IsSystem ? SrcMgr::C_System_ModuleMap : SrcMgr::C_User_ModuleMap; 3134 ID = SourceMgr.createFileID(File, ExternModuleLoc, FileCharacter); 3135 } 3136 3137 assert(Target && "Missing target information"); 3138 std::optional<llvm::MemoryBufferRef> Buffer = SourceMgr.getBufferOrNone(ID); 3139 if (!Buffer) 3140 return ParsedModuleMap[File] = true; 3141 assert((!Offset || *Offset <= Buffer->getBufferSize()) && 3142 "invalid buffer offset"); 3143 3144 // Parse this module map file. 3145 Lexer L(SourceMgr.getLocForStartOfFile(ID), MMapLangOpts, 3146 Buffer->getBufferStart(), 3147 Buffer->getBufferStart() + (Offset ? *Offset : 0), 3148 Buffer->getBufferEnd()); 3149 SourceLocation Start = L.getSourceLocation(); 3150 ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, ID, Dir, IsSystem); 3151 bool Result = Parser.parseModuleMapFile(); 3152 ParsedModuleMap[File] = Result; 3153 3154 if (Offset) { 3155 auto Loc = SourceMgr.getDecomposedLoc(Parser.getLocation()); 3156 assert(Loc.first == ID && "stopped in a different file?"); 3157 *Offset = Loc.second; 3158 } 3159 3160 // Notify callbacks that we parsed it. 3161 for (const auto &Cb : Callbacks) 3162 Cb->moduleMapFileRead(Start, File, IsSystem); 3163 3164 return Result; 3165 } 3166