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