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