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