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