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