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