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