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