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