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 // Notify callbacks that we just added a new header. 816 for (const auto &Cb : Callbacks) 817 Cb->moduleMapAddHeader(*Header.Entry); 818 } 819 820 void ModuleMap::excludeHeader(Module *Mod, Module::Header Header) { 821 // Add this as a known header so we won't implicitly add it to any 822 // umbrella directory module. 823 // FIXME: Should we only exclude it from umbrella modules within the 824 // specified module? 825 (void) Headers[Header.Entry]; 826 827 Mod->Headers[Module::HK_Excluded].push_back(std::move(Header)); 828 } 829 830 const FileEntry * 831 ModuleMap::getContainingModuleMapFile(const Module *Module) const { 832 if (Module->DefinitionLoc.isInvalid()) 833 return nullptr; 834 835 return SourceMgr.getFileEntryForID( 836 SourceMgr.getFileID(Module->DefinitionLoc)); 837 } 838 839 const FileEntry *ModuleMap::getModuleMapFileForUniquing(const Module *M) const { 840 if (M->IsInferred) { 841 assert(InferredModuleAllowedBy.count(M) && "missing inferred module map"); 842 return InferredModuleAllowedBy.find(M)->second; 843 } 844 return getContainingModuleMapFile(M); 845 } 846 847 void ModuleMap::setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap) { 848 assert(M->IsInferred && "module not inferred"); 849 InferredModuleAllowedBy[M] = ModMap; 850 } 851 852 LLVM_DUMP_METHOD void ModuleMap::dump() { 853 llvm::errs() << "Modules:"; 854 for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 855 MEnd = Modules.end(); 856 M != MEnd; ++M) 857 M->getValue()->print(llvm::errs(), 2); 858 859 llvm::errs() << "Headers:"; 860 for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 861 H != HEnd; ++H) { 862 llvm::errs() << " \"" << H->first->getName() << "\" -> "; 863 for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(), 864 E = H->second.end(); 865 I != E; ++I) { 866 if (I != H->second.begin()) 867 llvm::errs() << ","; 868 llvm::errs() << I->getModule()->getFullModuleName(); 869 } 870 llvm::errs() << "\n"; 871 } 872 } 873 874 bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 875 auto Unresolved = std::move(Mod->UnresolvedExports); 876 Mod->UnresolvedExports.clear(); 877 for (auto &UE : Unresolved) { 878 Module::ExportDecl Export = resolveExport(Mod, UE, Complain); 879 if (Export.getPointer() || Export.getInt()) 880 Mod->Exports.push_back(Export); 881 else 882 Mod->UnresolvedExports.push_back(UE); 883 } 884 return !Mod->UnresolvedExports.empty(); 885 } 886 887 bool ModuleMap::resolveUses(Module *Mod, bool Complain) { 888 auto Unresolved = std::move(Mod->UnresolvedDirectUses); 889 Mod->UnresolvedDirectUses.clear(); 890 for (auto &UDU : Unresolved) { 891 Module *DirectUse = resolveModuleId(UDU, Mod, Complain); 892 if (DirectUse) 893 Mod->DirectUses.push_back(DirectUse); 894 else 895 Mod->UnresolvedDirectUses.push_back(UDU); 896 } 897 return !Mod->UnresolvedDirectUses.empty(); 898 } 899 900 bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) { 901 auto Unresolved = std::move(Mod->UnresolvedConflicts); 902 Mod->UnresolvedConflicts.clear(); 903 for (auto &UC : Unresolved) { 904 if (Module *OtherMod = resolveModuleId(UC.Id, Mod, Complain)) { 905 Module::Conflict Conflict; 906 Conflict.Other = OtherMod; 907 Conflict.Message = UC.Message; 908 Mod->Conflicts.push_back(Conflict); 909 } else 910 Mod->UnresolvedConflicts.push_back(UC); 911 } 912 return !Mod->UnresolvedConflicts.empty(); 913 } 914 915 Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 916 if (Loc.isInvalid()) 917 return nullptr; 918 919 // Use the expansion location to determine which module we're in. 920 FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 921 if (!ExpansionLoc.isFileID()) 922 return nullptr; 923 924 const SourceManager &SrcMgr = Loc.getManager(); 925 FileID ExpansionFileID = ExpansionLoc.getFileID(); 926 927 while (const FileEntry *ExpansionFile 928 = SrcMgr.getFileEntryForID(ExpansionFileID)) { 929 // Find the module that owns this header (if any). 930 if (Module *Mod = findModuleForHeader(ExpansionFile).getModule()) 931 return Mod; 932 933 // No module owns this header, so look up the inclusion chain to see if 934 // any included header has an associated module. 935 SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); 936 if (IncludeLoc.isInvalid()) 937 return nullptr; 938 939 ExpansionFileID = SrcMgr.getFileID(IncludeLoc); 940 } 941 942 return nullptr; 943 } 944 945 //----------------------------------------------------------------------------// 946 // Module map file parser 947 //----------------------------------------------------------------------------// 948 949 namespace clang { 950 /// \brief A token in a module map file. 951 struct MMToken { 952 enum TokenKind { 953 Comma, 954 ConfigMacros, 955 Conflict, 956 EndOfFile, 957 HeaderKeyword, 958 Identifier, 959 Exclaim, 960 ExcludeKeyword, 961 ExplicitKeyword, 962 ExportKeyword, 963 ExternKeyword, 964 FrameworkKeyword, 965 LinkKeyword, 966 ModuleKeyword, 967 Period, 968 PrivateKeyword, 969 UmbrellaKeyword, 970 UseKeyword, 971 RequiresKeyword, 972 Star, 973 StringLiteral, 974 TextualKeyword, 975 LBrace, 976 RBrace, 977 LSquare, 978 RSquare 979 } Kind; 980 981 unsigned Location; 982 unsigned StringLength; 983 const char *StringData; 984 985 void clear() { 986 Kind = EndOfFile; 987 Location = 0; 988 StringLength = 0; 989 StringData = nullptr; 990 } 991 992 bool is(TokenKind K) const { return Kind == K; } 993 994 SourceLocation getLocation() const { 995 return SourceLocation::getFromRawEncoding(Location); 996 } 997 998 StringRef getString() const { 999 return StringRef(StringData, StringLength); 1000 } 1001 }; 1002 1003 class ModuleMapParser { 1004 Lexer &L; 1005 SourceManager &SourceMgr; 1006 1007 /// \brief Default target information, used only for string literal 1008 /// parsing. 1009 const TargetInfo *Target; 1010 1011 DiagnosticsEngine &Diags; 1012 ModuleMap ⤅ 1013 1014 /// \brief The current module map file. 1015 const FileEntry *ModuleMapFile; 1016 1017 /// \brief The directory that file names in this module map file should 1018 /// be resolved relative to. 1019 const DirectoryEntry *Directory; 1020 1021 /// \brief The directory containing Clang-supplied headers. 1022 const DirectoryEntry *BuiltinIncludeDir; 1023 1024 /// \brief Whether this module map is in a system header directory. 1025 bool IsSystem; 1026 1027 /// \brief Whether an error occurred. 1028 bool HadError; 1029 1030 /// \brief Stores string data for the various string literals referenced 1031 /// during parsing. 1032 llvm::BumpPtrAllocator StringData; 1033 1034 /// \brief The current token. 1035 MMToken Tok; 1036 1037 /// \brief The active module. 1038 Module *ActiveModule; 1039 1040 /// \brief Whether a module uses the 'requires excluded' hack to mark its 1041 /// contents as 'textual'. 1042 /// 1043 /// On older Darwin SDK versions, 'requires excluded' is used to mark the 1044 /// contents of the Darwin.C.excluded (assert.h) and Tcl.Private modules as 1045 /// non-modular headers. For backwards compatibility, we continue to 1046 /// support this idiom for just these modules, and map the headers to 1047 /// 'textual' to match the original intent. 1048 llvm::SmallPtrSet<Module *, 2> UsesRequiresExcludedHack; 1049 1050 /// \brief Consume the current token and return its location. 1051 SourceLocation consumeToken(); 1052 1053 /// \brief Skip tokens until we reach the a token with the given kind 1054 /// (or the end of the file). 1055 void skipUntil(MMToken::TokenKind K); 1056 1057 typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; 1058 bool parseModuleId(ModuleId &Id); 1059 void parseModuleDecl(); 1060 void parseExternModuleDecl(); 1061 void parseRequiresDecl(); 1062 void parseHeaderDecl(clang::MMToken::TokenKind, 1063 SourceLocation LeadingLoc); 1064 void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 1065 void parseExportDecl(); 1066 void parseUseDecl(); 1067 void parseLinkDecl(); 1068 void parseConfigMacros(); 1069 void parseConflict(); 1070 void parseInferredModuleDecl(bool Framework, bool Explicit); 1071 1072 typedef ModuleMap::Attributes Attributes; 1073 bool parseOptionalAttributes(Attributes &Attrs); 1074 1075 public: 1076 explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 1077 const TargetInfo *Target, 1078 DiagnosticsEngine &Diags, 1079 ModuleMap &Map, 1080 const FileEntry *ModuleMapFile, 1081 const DirectoryEntry *Directory, 1082 const DirectoryEntry *BuiltinIncludeDir, 1083 bool IsSystem) 1084 : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 1085 ModuleMapFile(ModuleMapFile), Directory(Directory), 1086 BuiltinIncludeDir(BuiltinIncludeDir), IsSystem(IsSystem), 1087 HadError(false), ActiveModule(nullptr) 1088 { 1089 Tok.clear(); 1090 consumeToken(); 1091 } 1092 1093 bool parseModuleMapFile(); 1094 }; 1095 } 1096 1097 SourceLocation ModuleMapParser::consumeToken() { 1098 retry: 1099 SourceLocation Result = Tok.getLocation(); 1100 Tok.clear(); 1101 1102 Token LToken; 1103 L.LexFromRawLexer(LToken); 1104 Tok.Location = LToken.getLocation().getRawEncoding(); 1105 switch (LToken.getKind()) { 1106 case tok::raw_identifier: { 1107 StringRef RI = LToken.getRawIdentifier(); 1108 Tok.StringData = RI.data(); 1109 Tok.StringLength = RI.size(); 1110 Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI) 1111 .Case("config_macros", MMToken::ConfigMacros) 1112 .Case("conflict", MMToken::Conflict) 1113 .Case("exclude", MMToken::ExcludeKeyword) 1114 .Case("explicit", MMToken::ExplicitKeyword) 1115 .Case("export", MMToken::ExportKeyword) 1116 .Case("extern", MMToken::ExternKeyword) 1117 .Case("framework", MMToken::FrameworkKeyword) 1118 .Case("header", MMToken::HeaderKeyword) 1119 .Case("link", MMToken::LinkKeyword) 1120 .Case("module", MMToken::ModuleKeyword) 1121 .Case("private", MMToken::PrivateKeyword) 1122 .Case("requires", MMToken::RequiresKeyword) 1123 .Case("textual", MMToken::TextualKeyword) 1124 .Case("umbrella", MMToken::UmbrellaKeyword) 1125 .Case("use", MMToken::UseKeyword) 1126 .Default(MMToken::Identifier); 1127 break; 1128 } 1129 1130 case tok::comma: 1131 Tok.Kind = MMToken::Comma; 1132 break; 1133 1134 case tok::eof: 1135 Tok.Kind = MMToken::EndOfFile; 1136 break; 1137 1138 case tok::l_brace: 1139 Tok.Kind = MMToken::LBrace; 1140 break; 1141 1142 case tok::l_square: 1143 Tok.Kind = MMToken::LSquare; 1144 break; 1145 1146 case tok::period: 1147 Tok.Kind = MMToken::Period; 1148 break; 1149 1150 case tok::r_brace: 1151 Tok.Kind = MMToken::RBrace; 1152 break; 1153 1154 case tok::r_square: 1155 Tok.Kind = MMToken::RSquare; 1156 break; 1157 1158 case tok::star: 1159 Tok.Kind = MMToken::Star; 1160 break; 1161 1162 case tok::exclaim: 1163 Tok.Kind = MMToken::Exclaim; 1164 break; 1165 1166 case tok::string_literal: { 1167 if (LToken.hasUDSuffix()) { 1168 Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 1169 HadError = true; 1170 goto retry; 1171 } 1172 1173 // Parse the string literal. 1174 LangOptions LangOpts; 1175 StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target); 1176 if (StringLiteral.hadError) 1177 goto retry; 1178 1179 // Copy the string literal into our string data allocator. 1180 unsigned Length = StringLiteral.GetStringLength(); 1181 char *Saved = StringData.Allocate<char>(Length + 1); 1182 memcpy(Saved, StringLiteral.GetString().data(), Length); 1183 Saved[Length] = 0; 1184 1185 // Form the token. 1186 Tok.Kind = MMToken::StringLiteral; 1187 Tok.StringData = Saved; 1188 Tok.StringLength = Length; 1189 break; 1190 } 1191 1192 case tok::comment: 1193 goto retry; 1194 1195 default: 1196 Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 1197 HadError = true; 1198 goto retry; 1199 } 1200 1201 return Result; 1202 } 1203 1204 void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 1205 unsigned braceDepth = 0; 1206 unsigned squareDepth = 0; 1207 do { 1208 switch (Tok.Kind) { 1209 case MMToken::EndOfFile: 1210 return; 1211 1212 case MMToken::LBrace: 1213 if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1214 return; 1215 1216 ++braceDepth; 1217 break; 1218 1219 case MMToken::LSquare: 1220 if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1221 return; 1222 1223 ++squareDepth; 1224 break; 1225 1226 case MMToken::RBrace: 1227 if (braceDepth > 0) 1228 --braceDepth; 1229 else if (Tok.is(K)) 1230 return; 1231 break; 1232 1233 case MMToken::RSquare: 1234 if (squareDepth > 0) 1235 --squareDepth; 1236 else if (Tok.is(K)) 1237 return; 1238 break; 1239 1240 default: 1241 if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 1242 return; 1243 break; 1244 } 1245 1246 consumeToken(); 1247 } while (true); 1248 } 1249 1250 /// \brief Parse a module-id. 1251 /// 1252 /// module-id: 1253 /// identifier 1254 /// identifier '.' module-id 1255 /// 1256 /// \returns true if an error occurred, false otherwise. 1257 bool ModuleMapParser::parseModuleId(ModuleId &Id) { 1258 Id.clear(); 1259 do { 1260 if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) { 1261 Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 1262 consumeToken(); 1263 } else { 1264 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 1265 return true; 1266 } 1267 1268 if (!Tok.is(MMToken::Period)) 1269 break; 1270 1271 consumeToken(); 1272 } while (true); 1273 1274 return false; 1275 } 1276 1277 namespace { 1278 /// \brief Enumerates the known attributes. 1279 enum AttributeKind { 1280 /// \brief An unknown attribute. 1281 AT_unknown, 1282 /// \brief The 'system' attribute. 1283 AT_system, 1284 /// \brief The 'extern_c' attribute. 1285 AT_extern_c, 1286 /// \brief The 'exhaustive' attribute. 1287 AT_exhaustive 1288 }; 1289 } 1290 1291 /// \brief Parse a module declaration. 1292 /// 1293 /// module-declaration: 1294 /// 'extern' 'module' module-id string-literal 1295 /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 1296 /// { module-member* } 1297 /// 1298 /// module-member: 1299 /// requires-declaration 1300 /// header-declaration 1301 /// submodule-declaration 1302 /// export-declaration 1303 /// link-declaration 1304 /// 1305 /// submodule-declaration: 1306 /// module-declaration 1307 /// inferred-submodule-declaration 1308 void ModuleMapParser::parseModuleDecl() { 1309 assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 1310 Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword)); 1311 if (Tok.is(MMToken::ExternKeyword)) { 1312 parseExternModuleDecl(); 1313 return; 1314 } 1315 1316 // Parse 'explicit' or 'framework' keyword, if present. 1317 SourceLocation ExplicitLoc; 1318 bool Explicit = false; 1319 bool Framework = false; 1320 1321 // Parse 'explicit' keyword, if present. 1322 if (Tok.is(MMToken::ExplicitKeyword)) { 1323 ExplicitLoc = consumeToken(); 1324 Explicit = true; 1325 } 1326 1327 // Parse 'framework' keyword, if present. 1328 if (Tok.is(MMToken::FrameworkKeyword)) { 1329 consumeToken(); 1330 Framework = true; 1331 } 1332 1333 // Parse 'module' keyword. 1334 if (!Tok.is(MMToken::ModuleKeyword)) { 1335 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1336 consumeToken(); 1337 HadError = true; 1338 return; 1339 } 1340 consumeToken(); // 'module' keyword 1341 1342 // If we have a wildcard for the module name, this is an inferred submodule. 1343 // Parse it. 1344 if (Tok.is(MMToken::Star)) 1345 return parseInferredModuleDecl(Framework, Explicit); 1346 1347 // Parse the module name. 1348 ModuleId Id; 1349 if (parseModuleId(Id)) { 1350 HadError = true; 1351 return; 1352 } 1353 1354 if (ActiveModule) { 1355 if (Id.size() > 1) { 1356 Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 1357 << SourceRange(Id.front().second, Id.back().second); 1358 1359 HadError = true; 1360 return; 1361 } 1362 } else if (Id.size() == 1 && Explicit) { 1363 // Top-level modules can't be explicit. 1364 Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 1365 Explicit = false; 1366 ExplicitLoc = SourceLocation(); 1367 HadError = true; 1368 } 1369 1370 Module *PreviousActiveModule = ActiveModule; 1371 if (Id.size() > 1) { 1372 // This module map defines a submodule. Go find the module of which it 1373 // is a submodule. 1374 ActiveModule = nullptr; 1375 const Module *TopLevelModule = nullptr; 1376 for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 1377 if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 1378 if (I == 0) 1379 TopLevelModule = Next; 1380 ActiveModule = Next; 1381 continue; 1382 } 1383 1384 if (ActiveModule) { 1385 Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 1386 << Id[I].first 1387 << ActiveModule->getTopLevelModule()->getFullModuleName(); 1388 } else { 1389 Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 1390 } 1391 HadError = true; 1392 return; 1393 } 1394 1395 if (ModuleMapFile != Map.getContainingModuleMapFile(TopLevelModule)) { 1396 assert(ModuleMapFile != Map.getModuleMapFileForUniquing(TopLevelModule) && 1397 "submodule defined in same file as 'module *' that allowed its " 1398 "top-level module"); 1399 Map.addAdditionalModuleMapFile(TopLevelModule, ModuleMapFile); 1400 } 1401 } 1402 1403 StringRef ModuleName = Id.back().first; 1404 SourceLocation ModuleNameLoc = Id.back().second; 1405 1406 // Parse the optional attribute list. 1407 Attributes Attrs; 1408 if (parseOptionalAttributes(Attrs)) 1409 return; 1410 1411 1412 // Parse the opening brace. 1413 if (!Tok.is(MMToken::LBrace)) { 1414 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 1415 << ModuleName; 1416 HadError = true; 1417 return; 1418 } 1419 SourceLocation LBraceLoc = consumeToken(); 1420 1421 // Determine whether this (sub)module has already been defined. 1422 if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 1423 if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { 1424 // Skip the module definition. 1425 skipUntil(MMToken::RBrace); 1426 if (Tok.is(MMToken::RBrace)) 1427 consumeToken(); 1428 else { 1429 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1430 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1431 HadError = true; 1432 } 1433 return; 1434 } 1435 1436 Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 1437 << ModuleName; 1438 Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 1439 1440 // Skip the module definition. 1441 skipUntil(MMToken::RBrace); 1442 if (Tok.is(MMToken::RBrace)) 1443 consumeToken(); 1444 1445 HadError = true; 1446 return; 1447 } 1448 1449 // Start defining this module. 1450 ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, 1451 Explicit).first; 1452 ActiveModule->DefinitionLoc = ModuleNameLoc; 1453 if (Attrs.IsSystem || IsSystem) 1454 ActiveModule->IsSystem = true; 1455 if (Attrs.IsExternC) 1456 ActiveModule->IsExternC = true; 1457 ActiveModule->Directory = Directory; 1458 1459 bool Done = false; 1460 do { 1461 switch (Tok.Kind) { 1462 case MMToken::EndOfFile: 1463 case MMToken::RBrace: 1464 Done = true; 1465 break; 1466 1467 case MMToken::ConfigMacros: 1468 parseConfigMacros(); 1469 break; 1470 1471 case MMToken::Conflict: 1472 parseConflict(); 1473 break; 1474 1475 case MMToken::ExplicitKeyword: 1476 case MMToken::ExternKeyword: 1477 case MMToken::FrameworkKeyword: 1478 case MMToken::ModuleKeyword: 1479 parseModuleDecl(); 1480 break; 1481 1482 case MMToken::ExportKeyword: 1483 parseExportDecl(); 1484 break; 1485 1486 case MMToken::UseKeyword: 1487 parseUseDecl(); 1488 break; 1489 1490 case MMToken::RequiresKeyword: 1491 parseRequiresDecl(); 1492 break; 1493 1494 case MMToken::TextualKeyword: 1495 parseHeaderDecl(MMToken::TextualKeyword, consumeToken()); 1496 break; 1497 1498 case MMToken::UmbrellaKeyword: { 1499 SourceLocation UmbrellaLoc = consumeToken(); 1500 if (Tok.is(MMToken::HeaderKeyword)) 1501 parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc); 1502 else 1503 parseUmbrellaDirDecl(UmbrellaLoc); 1504 break; 1505 } 1506 1507 case MMToken::ExcludeKeyword: 1508 parseHeaderDecl(MMToken::ExcludeKeyword, consumeToken()); 1509 break; 1510 1511 case MMToken::PrivateKeyword: 1512 parseHeaderDecl(MMToken::PrivateKeyword, consumeToken()); 1513 break; 1514 1515 case MMToken::HeaderKeyword: 1516 parseHeaderDecl(MMToken::HeaderKeyword, consumeToken()); 1517 break; 1518 1519 case MMToken::LinkKeyword: 1520 parseLinkDecl(); 1521 break; 1522 1523 default: 1524 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 1525 consumeToken(); 1526 break; 1527 } 1528 } while (!Done); 1529 1530 if (Tok.is(MMToken::RBrace)) 1531 consumeToken(); 1532 else { 1533 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1534 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1535 HadError = true; 1536 } 1537 1538 // If the active module is a top-level framework, and there are no link 1539 // libraries, automatically link against the framework. 1540 if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() && 1541 ActiveModule->LinkLibraries.empty()) { 1542 inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager()); 1543 } 1544 1545 // If the module meets all requirements but is still unavailable, mark the 1546 // whole tree as unavailable to prevent it from building. 1547 if (!ActiveModule->IsAvailable && !ActiveModule->IsMissingRequirement && 1548 ActiveModule->Parent) { 1549 ActiveModule->getTopLevelModule()->markUnavailable(); 1550 ActiveModule->getTopLevelModule()->MissingHeaders.append( 1551 ActiveModule->MissingHeaders.begin(), ActiveModule->MissingHeaders.end()); 1552 } 1553 1554 // We're done parsing this module. Pop back to the previous module. 1555 ActiveModule = PreviousActiveModule; 1556 } 1557 1558 /// \brief Parse an extern module declaration. 1559 /// 1560 /// extern module-declaration: 1561 /// 'extern' 'module' module-id string-literal 1562 void ModuleMapParser::parseExternModuleDecl() { 1563 assert(Tok.is(MMToken::ExternKeyword)); 1564 SourceLocation ExternLoc = consumeToken(); // 'extern' keyword 1565 1566 // Parse 'module' keyword. 1567 if (!Tok.is(MMToken::ModuleKeyword)) { 1568 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1569 consumeToken(); 1570 HadError = true; 1571 return; 1572 } 1573 consumeToken(); // 'module' keyword 1574 1575 // Parse the module name. 1576 ModuleId Id; 1577 if (parseModuleId(Id)) { 1578 HadError = true; 1579 return; 1580 } 1581 1582 // Parse the referenced module map file name. 1583 if (!Tok.is(MMToken::StringLiteral)) { 1584 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file); 1585 HadError = true; 1586 return; 1587 } 1588 std::string FileName = Tok.getString(); 1589 consumeToken(); // filename 1590 1591 StringRef FileNameRef = FileName; 1592 SmallString<128> ModuleMapFileName; 1593 if (llvm::sys::path::is_relative(FileNameRef)) { 1594 ModuleMapFileName += Directory->getName(); 1595 llvm::sys::path::append(ModuleMapFileName, FileName); 1596 FileNameRef = ModuleMapFileName; 1597 } 1598 if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef)) 1599 Map.parseModuleMapFile( 1600 File, /*IsSystem=*/false, 1601 Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd 1602 ? Directory 1603 : File->getDir(), ExternLoc); 1604 } 1605 1606 /// Whether to add the requirement \p Feature to the module \p M. 1607 /// 1608 /// This preserves backwards compatibility for two hacks in the Darwin system 1609 /// module map files: 1610 /// 1611 /// 1. The use of 'requires excluded' to make headers non-modular, which 1612 /// should really be mapped to 'textual' now that we have this feature. We 1613 /// drop the 'excluded' requirement, and set \p IsRequiresExcludedHack to 1614 /// true. Later, this bit will be used to map all the headers inside this 1615 /// module to 'textual'. 1616 /// 1617 /// This affects Darwin.C.excluded (for assert.h) and Tcl.Private. 1618 /// 1619 /// 2. Removes a bogus cplusplus requirement from IOKit.avc. This requirement 1620 /// was never correct and causes issues now that we check it, so drop it. 1621 static bool shouldAddRequirement(Module *M, StringRef Feature, 1622 bool &IsRequiresExcludedHack) { 1623 static const StringRef DarwinCExcluded[] = {"Darwin", "C", "excluded"}; 1624 static const StringRef TclPrivate[] = {"Tcl", "Private"}; 1625 static const StringRef IOKitAVC[] = {"IOKit", "avc"}; 1626 1627 if (Feature == "excluded" && (M->fullModuleNameIs(DarwinCExcluded) || 1628 M->fullModuleNameIs(TclPrivate))) { 1629 IsRequiresExcludedHack = true; 1630 return false; 1631 } else if (Feature == "cplusplus" && M->fullModuleNameIs(IOKitAVC)) { 1632 return false; 1633 } 1634 1635 return true; 1636 } 1637 1638 /// \brief Parse a requires declaration. 1639 /// 1640 /// requires-declaration: 1641 /// 'requires' feature-list 1642 /// 1643 /// feature-list: 1644 /// feature ',' feature-list 1645 /// feature 1646 /// 1647 /// feature: 1648 /// '!'[opt] identifier 1649 void ModuleMapParser::parseRequiresDecl() { 1650 assert(Tok.is(MMToken::RequiresKeyword)); 1651 1652 // Parse 'requires' keyword. 1653 consumeToken(); 1654 1655 // Parse the feature-list. 1656 do { 1657 bool RequiredState = true; 1658 if (Tok.is(MMToken::Exclaim)) { 1659 RequiredState = false; 1660 consumeToken(); 1661 } 1662 1663 if (!Tok.is(MMToken::Identifier)) { 1664 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 1665 HadError = true; 1666 return; 1667 } 1668 1669 // Consume the feature name. 1670 std::string Feature = Tok.getString(); 1671 consumeToken(); 1672 1673 bool IsRequiresExcludedHack = false; 1674 bool ShouldAddRequirement = 1675 shouldAddRequirement(ActiveModule, Feature, IsRequiresExcludedHack); 1676 1677 if (IsRequiresExcludedHack) 1678 UsesRequiresExcludedHack.insert(ActiveModule); 1679 1680 if (ShouldAddRequirement) { 1681 // Add this feature. 1682 ActiveModule->addRequirement(Feature, RequiredState, Map.LangOpts, 1683 *Map.Target); 1684 } 1685 1686 if (!Tok.is(MMToken::Comma)) 1687 break; 1688 1689 // Consume the comma. 1690 consumeToken(); 1691 } while (true); 1692 } 1693 1694 /// \brief Append to \p Paths the set of paths needed to get to the 1695 /// subframework in which the given module lives. 1696 static void appendSubframeworkPaths(Module *Mod, 1697 SmallVectorImpl<char> &Path) { 1698 // Collect the framework names from the given module to the top-level module. 1699 SmallVector<StringRef, 2> Paths; 1700 for (; Mod; Mod = Mod->Parent) { 1701 if (Mod->IsFramework) 1702 Paths.push_back(Mod->Name); 1703 } 1704 1705 if (Paths.empty()) 1706 return; 1707 1708 // Add Frameworks/Name.framework for each subframework. 1709 for (unsigned I = Paths.size() - 1; I != 0; --I) 1710 llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework"); 1711 } 1712 1713 /// \brief Parse a header declaration. 1714 /// 1715 /// header-declaration: 1716 /// 'textual'[opt] 'header' string-literal 1717 /// 'private' 'textual'[opt] 'header' string-literal 1718 /// 'exclude' 'header' string-literal 1719 /// 'umbrella' 'header' string-literal 1720 /// 1721 /// FIXME: Support 'private textual header'. 1722 void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken, 1723 SourceLocation LeadingLoc) { 1724 // We've already consumed the first token. 1725 ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader; 1726 if (LeadingToken == MMToken::PrivateKeyword) { 1727 Role = ModuleMap::PrivateHeader; 1728 // 'private' may optionally be followed by 'textual'. 1729 if (Tok.is(MMToken::TextualKeyword)) { 1730 LeadingToken = Tok.Kind; 1731 consumeToken(); 1732 } 1733 } 1734 1735 if (LeadingToken == MMToken::TextualKeyword) 1736 Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader); 1737 1738 if (UsesRequiresExcludedHack.count(ActiveModule)) { 1739 // Mark this header 'textual' (see doc comment for 1740 // Module::UsesRequiresExcludedHack). 1741 Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader); 1742 } 1743 1744 if (LeadingToken != MMToken::HeaderKeyword) { 1745 if (!Tok.is(MMToken::HeaderKeyword)) { 1746 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1747 << (LeadingToken == MMToken::PrivateKeyword ? "private" : 1748 LeadingToken == MMToken::ExcludeKeyword ? "exclude" : 1749 LeadingToken == MMToken::TextualKeyword ? "textual" : "umbrella"); 1750 return; 1751 } 1752 consumeToken(); 1753 } 1754 1755 // Parse the header name. 1756 if (!Tok.is(MMToken::StringLiteral)) { 1757 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1758 << "header"; 1759 HadError = true; 1760 return; 1761 } 1762 Module::UnresolvedHeaderDirective Header; 1763 Header.FileName = Tok.getString(); 1764 Header.FileNameLoc = consumeToken(); 1765 1766 // Check whether we already have an umbrella. 1767 if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) { 1768 Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash) 1769 << ActiveModule->getFullModuleName(); 1770 HadError = true; 1771 return; 1772 } 1773 1774 // Look for this file. 1775 const FileEntry *File = nullptr; 1776 const FileEntry *BuiltinFile = nullptr; 1777 SmallString<128> RelativePathName; 1778 if (llvm::sys::path::is_absolute(Header.FileName)) { 1779 RelativePathName = Header.FileName; 1780 File = SourceMgr.getFileManager().getFile(RelativePathName); 1781 } else { 1782 // Search for the header file within the search directory. 1783 SmallString<128> FullPathName(Directory->getName()); 1784 unsigned FullPathLength = FullPathName.size(); 1785 1786 if (ActiveModule->isPartOfFramework()) { 1787 appendSubframeworkPaths(ActiveModule, RelativePathName); 1788 1789 // Check whether this file is in the public headers. 1790 llvm::sys::path::append(RelativePathName, "Headers", Header.FileName); 1791 llvm::sys::path::append(FullPathName, RelativePathName); 1792 File = SourceMgr.getFileManager().getFile(FullPathName); 1793 1794 if (!File) { 1795 // Check whether this file is in the private headers. 1796 // FIXME: Should we retain the subframework paths here? 1797 RelativePathName.clear(); 1798 FullPathName.resize(FullPathLength); 1799 llvm::sys::path::append(RelativePathName, "PrivateHeaders", 1800 Header.FileName); 1801 llvm::sys::path::append(FullPathName, RelativePathName); 1802 File = SourceMgr.getFileManager().getFile(FullPathName); 1803 } 1804 } else { 1805 // Lookup for normal headers. 1806 llvm::sys::path::append(RelativePathName, Header.FileName); 1807 llvm::sys::path::append(FullPathName, RelativePathName); 1808 File = SourceMgr.getFileManager().getFile(FullPathName); 1809 1810 // If this is a system module with a top-level header, this header 1811 // may have a counterpart (or replacement) in the set of headers 1812 // supplied by Clang. Find that builtin header. 1813 if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword && 1814 BuiltinIncludeDir && BuiltinIncludeDir != Directory && 1815 isBuiltinHeader(Header.FileName)) { 1816 SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); 1817 llvm::sys::path::append(BuiltinPathName, Header.FileName); 1818 BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); 1819 1820 // If Clang supplies this header but the underlying system does not, 1821 // just silently swap in our builtin version. Otherwise, we'll end 1822 // up adding both (later). 1823 // 1824 // For local visibility, entirely replace the system file with our 1825 // one and textually include the system one. We need to pass macros 1826 // from our header to the system one if we #include_next it. 1827 // 1828 // FIXME: Can we do this in all cases? 1829 if (BuiltinFile && (!File || Map.LangOpts.ModulesLocalVisibility)) { 1830 File = BuiltinFile; 1831 RelativePathName = BuiltinPathName; 1832 BuiltinFile = nullptr; 1833 } 1834 } 1835 } 1836 } 1837 1838 // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 1839 // Come up with a lazy way to do this. 1840 if (File) { 1841 if (LeadingToken == MMToken::UmbrellaKeyword) { 1842 const DirectoryEntry *UmbrellaDir = File->getDir(); 1843 if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { 1844 Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash) 1845 << UmbrellaModule->getFullModuleName(); 1846 HadError = true; 1847 } else { 1848 // Record this umbrella header. 1849 Map.setUmbrellaHeader(ActiveModule, File, RelativePathName.str()); 1850 } 1851 } else if (LeadingToken == MMToken::ExcludeKeyword) { 1852 Module::Header H = {RelativePathName.str(), File}; 1853 Map.excludeHeader(ActiveModule, H); 1854 } else { 1855 // If there is a builtin counterpart to this file, add it now, before 1856 // the "real" header, so we build the built-in one first when building 1857 // the module. 1858 if (BuiltinFile) { 1859 // FIXME: Taking the name from the FileEntry is unstable and can give 1860 // different results depending on how we've previously named that file 1861 // in this build. 1862 Module::Header H = { BuiltinFile->getName(), BuiltinFile }; 1863 Map.addHeader(ActiveModule, H, Role); 1864 } 1865 1866 // Record this header. 1867 Module::Header H = { RelativePathName.str(), File }; 1868 Map.addHeader(ActiveModule, H, Role); 1869 } 1870 } else if (LeadingToken != MMToken::ExcludeKeyword) { 1871 // Ignore excluded header files. They're optional anyway. 1872 1873 // If we find a module that has a missing header, we mark this module as 1874 // unavailable and store the header directive for displaying diagnostics. 1875 Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword; 1876 ActiveModule->markUnavailable(); 1877 ActiveModule->MissingHeaders.push_back(Header); 1878 } 1879 } 1880 1881 static int compareModuleHeaders(const Module::Header *A, 1882 const Module::Header *B) { 1883 return A->NameAsWritten.compare(B->NameAsWritten); 1884 } 1885 1886 /// \brief Parse an umbrella directory declaration. 1887 /// 1888 /// umbrella-dir-declaration: 1889 /// umbrella string-literal 1890 void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1891 // Parse the directory name. 1892 if (!Tok.is(MMToken::StringLiteral)) { 1893 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1894 << "umbrella"; 1895 HadError = true; 1896 return; 1897 } 1898 1899 std::string DirName = Tok.getString(); 1900 SourceLocation DirNameLoc = consumeToken(); 1901 1902 // Check whether we already have an umbrella. 1903 if (ActiveModule->Umbrella) { 1904 Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1905 << ActiveModule->getFullModuleName(); 1906 HadError = true; 1907 return; 1908 } 1909 1910 // Look for this file. 1911 const DirectoryEntry *Dir = nullptr; 1912 if (llvm::sys::path::is_absolute(DirName)) 1913 Dir = SourceMgr.getFileManager().getDirectory(DirName); 1914 else { 1915 SmallString<128> PathName; 1916 PathName = Directory->getName(); 1917 llvm::sys::path::append(PathName, DirName); 1918 Dir = SourceMgr.getFileManager().getDirectory(PathName); 1919 } 1920 1921 if (!Dir) { 1922 Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1923 << DirName; 1924 HadError = true; 1925 return; 1926 } 1927 1928 if (UsesRequiresExcludedHack.count(ActiveModule)) { 1929 // Mark this header 'textual' (see doc comment for 1930 // ModuleMapParser::UsesRequiresExcludedHack). Although iterating over the 1931 // directory is relatively expensive, in practice this only applies to the 1932 // uncommonly used Tcl module on Darwin platforms. 1933 std::error_code EC; 1934 SmallVector<Module::Header, 6> Headers; 1935 for (llvm::sys::fs::recursive_directory_iterator I(Dir->getName(), EC), E; 1936 I != E && !EC; I.increment(EC)) { 1937 if (const FileEntry *FE = SourceMgr.getFileManager().getFile(I->path())) { 1938 1939 Module::Header Header = {I->path(), FE}; 1940 Headers.push_back(std::move(Header)); 1941 } 1942 } 1943 1944 // Sort header paths so that the pcm doesn't depend on iteration order. 1945 llvm::array_pod_sort(Headers.begin(), Headers.end(), compareModuleHeaders); 1946 1947 for (auto &Header : Headers) 1948 Map.addHeader(ActiveModule, std::move(Header), ModuleMap::TextualHeader); 1949 return; 1950 } 1951 1952 if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1953 Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1954 << OwningModule->getFullModuleName(); 1955 HadError = true; 1956 return; 1957 } 1958 1959 // Record this umbrella directory. 1960 Map.setUmbrellaDir(ActiveModule, Dir, DirName); 1961 } 1962 1963 /// \brief Parse a module export declaration. 1964 /// 1965 /// export-declaration: 1966 /// 'export' wildcard-module-id 1967 /// 1968 /// wildcard-module-id: 1969 /// identifier 1970 /// '*' 1971 /// identifier '.' wildcard-module-id 1972 void ModuleMapParser::parseExportDecl() { 1973 assert(Tok.is(MMToken::ExportKeyword)); 1974 SourceLocation ExportLoc = consumeToken(); 1975 1976 // Parse the module-id with an optional wildcard at the end. 1977 ModuleId ParsedModuleId; 1978 bool Wildcard = false; 1979 do { 1980 // FIXME: Support string-literal module names here. 1981 if (Tok.is(MMToken::Identifier)) { 1982 ParsedModuleId.push_back(std::make_pair(Tok.getString(), 1983 Tok.getLocation())); 1984 consumeToken(); 1985 1986 if (Tok.is(MMToken::Period)) { 1987 consumeToken(); 1988 continue; 1989 } 1990 1991 break; 1992 } 1993 1994 if(Tok.is(MMToken::Star)) { 1995 Wildcard = true; 1996 consumeToken(); 1997 break; 1998 } 1999 2000 Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); 2001 HadError = true; 2002 return; 2003 } while (true); 2004 2005 Module::UnresolvedExportDecl Unresolved = { 2006 ExportLoc, ParsedModuleId, Wildcard 2007 }; 2008 ActiveModule->UnresolvedExports.push_back(Unresolved); 2009 } 2010 2011 /// \brief Parse a module use declaration. 2012 /// 2013 /// use-declaration: 2014 /// 'use' wildcard-module-id 2015 void ModuleMapParser::parseUseDecl() { 2016 assert(Tok.is(MMToken::UseKeyword)); 2017 auto KWLoc = consumeToken(); 2018 // Parse the module-id. 2019 ModuleId ParsedModuleId; 2020 parseModuleId(ParsedModuleId); 2021 2022 if (ActiveModule->Parent) 2023 Diags.Report(KWLoc, diag::err_mmap_use_decl_submodule); 2024 else 2025 ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId); 2026 } 2027 2028 /// \brief Parse a link declaration. 2029 /// 2030 /// module-declaration: 2031 /// 'link' 'framework'[opt] string-literal 2032 void ModuleMapParser::parseLinkDecl() { 2033 assert(Tok.is(MMToken::LinkKeyword)); 2034 SourceLocation LinkLoc = consumeToken(); 2035 2036 // Parse the optional 'framework' keyword. 2037 bool IsFramework = false; 2038 if (Tok.is(MMToken::FrameworkKeyword)) { 2039 consumeToken(); 2040 IsFramework = true; 2041 } 2042 2043 // Parse the library name 2044 if (!Tok.is(MMToken::StringLiteral)) { 2045 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) 2046 << IsFramework << SourceRange(LinkLoc); 2047 HadError = true; 2048 return; 2049 } 2050 2051 std::string LibraryName = Tok.getString(); 2052 consumeToken(); 2053 ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, 2054 IsFramework)); 2055 } 2056 2057 /// \brief Parse a configuration macro declaration. 2058 /// 2059 /// module-declaration: 2060 /// 'config_macros' attributes[opt] config-macro-list? 2061 /// 2062 /// config-macro-list: 2063 /// identifier (',' identifier)? 2064 void ModuleMapParser::parseConfigMacros() { 2065 assert(Tok.is(MMToken::ConfigMacros)); 2066 SourceLocation ConfigMacrosLoc = consumeToken(); 2067 2068 // Only top-level modules can have configuration macros. 2069 if (ActiveModule->Parent) { 2070 Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule); 2071 } 2072 2073 // Parse the optional attributes. 2074 Attributes Attrs; 2075 if (parseOptionalAttributes(Attrs)) 2076 return; 2077 2078 if (Attrs.IsExhaustive && !ActiveModule->Parent) { 2079 ActiveModule->ConfigMacrosExhaustive = true; 2080 } 2081 2082 // If we don't have an identifier, we're done. 2083 // FIXME: Support macros with the same name as a keyword here. 2084 if (!Tok.is(MMToken::Identifier)) 2085 return; 2086 2087 // Consume the first identifier. 2088 if (!ActiveModule->Parent) { 2089 ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 2090 } 2091 consumeToken(); 2092 2093 do { 2094 // If there's a comma, consume it. 2095 if (!Tok.is(MMToken::Comma)) 2096 break; 2097 consumeToken(); 2098 2099 // We expect to see a macro name here. 2100 // FIXME: Support macros with the same name as a keyword here. 2101 if (!Tok.is(MMToken::Identifier)) { 2102 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro); 2103 break; 2104 } 2105 2106 // Consume the macro name. 2107 if (!ActiveModule->Parent) { 2108 ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 2109 } 2110 consumeToken(); 2111 } while (true); 2112 } 2113 2114 /// \brief Format a module-id into a string. 2115 static std::string formatModuleId(const ModuleId &Id) { 2116 std::string result; 2117 { 2118 llvm::raw_string_ostream OS(result); 2119 2120 for (unsigned I = 0, N = Id.size(); I != N; ++I) { 2121 if (I) 2122 OS << "."; 2123 OS << Id[I].first; 2124 } 2125 } 2126 2127 return result; 2128 } 2129 2130 /// \brief Parse a conflict declaration. 2131 /// 2132 /// module-declaration: 2133 /// 'conflict' module-id ',' string-literal 2134 void ModuleMapParser::parseConflict() { 2135 assert(Tok.is(MMToken::Conflict)); 2136 SourceLocation ConflictLoc = consumeToken(); 2137 Module::UnresolvedConflict Conflict; 2138 2139 // Parse the module-id. 2140 if (parseModuleId(Conflict.Id)) 2141 return; 2142 2143 // Parse the ','. 2144 if (!Tok.is(MMToken::Comma)) { 2145 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma) 2146 << SourceRange(ConflictLoc); 2147 return; 2148 } 2149 consumeToken(); 2150 2151 // Parse the message. 2152 if (!Tok.is(MMToken::StringLiteral)) { 2153 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message) 2154 << formatModuleId(Conflict.Id); 2155 return; 2156 } 2157 Conflict.Message = Tok.getString().str(); 2158 consumeToken(); 2159 2160 // Add this unresolved conflict. 2161 ActiveModule->UnresolvedConflicts.push_back(Conflict); 2162 } 2163 2164 /// \brief Parse an inferred module declaration (wildcard modules). 2165 /// 2166 /// module-declaration: 2167 /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] 2168 /// { inferred-module-member* } 2169 /// 2170 /// inferred-module-member: 2171 /// 'export' '*' 2172 /// 'exclude' identifier 2173 void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { 2174 assert(Tok.is(MMToken::Star)); 2175 SourceLocation StarLoc = consumeToken(); 2176 bool Failed = false; 2177 2178 // Inferred modules must be submodules. 2179 if (!ActiveModule && !Framework) { 2180 Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 2181 Failed = true; 2182 } 2183 2184 if (ActiveModule) { 2185 // Inferred modules must have umbrella directories. 2186 if (!Failed && ActiveModule->IsAvailable && 2187 !ActiveModule->getUmbrellaDir()) { 2188 Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 2189 Failed = true; 2190 } 2191 2192 // Check for redefinition of an inferred module. 2193 if (!Failed && ActiveModule->InferSubmodules) { 2194 Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 2195 if (ActiveModule->InferredSubmoduleLoc.isValid()) 2196 Diags.Report(ActiveModule->InferredSubmoduleLoc, 2197 diag::note_mmap_prev_definition); 2198 Failed = true; 2199 } 2200 2201 // Check for the 'framework' keyword, which is not permitted here. 2202 if (Framework) { 2203 Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); 2204 Framework = false; 2205 } 2206 } else if (Explicit) { 2207 Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); 2208 Explicit = false; 2209 } 2210 2211 // If there were any problems with this inferred submodule, skip its body. 2212 if (Failed) { 2213 if (Tok.is(MMToken::LBrace)) { 2214 consumeToken(); 2215 skipUntil(MMToken::RBrace); 2216 if (Tok.is(MMToken::RBrace)) 2217 consumeToken(); 2218 } 2219 HadError = true; 2220 return; 2221 } 2222 2223 // Parse optional attributes. 2224 Attributes Attrs; 2225 if (parseOptionalAttributes(Attrs)) 2226 return; 2227 2228 if (ActiveModule) { 2229 // Note that we have an inferred submodule. 2230 ActiveModule->InferSubmodules = true; 2231 ActiveModule->InferredSubmoduleLoc = StarLoc; 2232 ActiveModule->InferExplicitSubmodules = Explicit; 2233 } else { 2234 // We'll be inferring framework modules for this directory. 2235 Map.InferredDirectories[Directory].InferModules = true; 2236 Map.InferredDirectories[Directory].Attrs = Attrs; 2237 Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile; 2238 // FIXME: Handle the 'framework' keyword. 2239 } 2240 2241 // Parse the opening brace. 2242 if (!Tok.is(MMToken::LBrace)) { 2243 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 2244 HadError = true; 2245 return; 2246 } 2247 SourceLocation LBraceLoc = consumeToken(); 2248 2249 // Parse the body of the inferred submodule. 2250 bool Done = false; 2251 do { 2252 switch (Tok.Kind) { 2253 case MMToken::EndOfFile: 2254 case MMToken::RBrace: 2255 Done = true; 2256 break; 2257 2258 case MMToken::ExcludeKeyword: { 2259 if (ActiveModule) { 2260 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2261 << (ActiveModule != nullptr); 2262 consumeToken(); 2263 break; 2264 } 2265 2266 consumeToken(); 2267 // FIXME: Support string-literal module names here. 2268 if (!Tok.is(MMToken::Identifier)) { 2269 Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); 2270 break; 2271 } 2272 2273 Map.InferredDirectories[Directory].ExcludedModules 2274 .push_back(Tok.getString()); 2275 consumeToken(); 2276 break; 2277 } 2278 2279 case MMToken::ExportKeyword: 2280 if (!ActiveModule) { 2281 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2282 << (ActiveModule != nullptr); 2283 consumeToken(); 2284 break; 2285 } 2286 2287 consumeToken(); 2288 if (Tok.is(MMToken::Star)) 2289 ActiveModule->InferExportWildcard = true; 2290 else 2291 Diags.Report(Tok.getLocation(), 2292 diag::err_mmap_expected_export_wildcard); 2293 consumeToken(); 2294 break; 2295 2296 case MMToken::ExplicitKeyword: 2297 case MMToken::ModuleKeyword: 2298 case MMToken::HeaderKeyword: 2299 case MMToken::PrivateKeyword: 2300 case MMToken::UmbrellaKeyword: 2301 default: 2302 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2303 << (ActiveModule != nullptr); 2304 consumeToken(); 2305 break; 2306 } 2307 } while (!Done); 2308 2309 if (Tok.is(MMToken::RBrace)) 2310 consumeToken(); 2311 else { 2312 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 2313 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 2314 HadError = true; 2315 } 2316 } 2317 2318 /// \brief Parse optional attributes. 2319 /// 2320 /// attributes: 2321 /// attribute attributes 2322 /// attribute 2323 /// 2324 /// attribute: 2325 /// [ identifier ] 2326 /// 2327 /// \param Attrs Will be filled in with the parsed attributes. 2328 /// 2329 /// \returns true if an error occurred, false otherwise. 2330 bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { 2331 bool HadError = false; 2332 2333 while (Tok.is(MMToken::LSquare)) { 2334 // Consume the '['. 2335 SourceLocation LSquareLoc = consumeToken(); 2336 2337 // Check whether we have an attribute name here. 2338 if (!Tok.is(MMToken::Identifier)) { 2339 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 2340 skipUntil(MMToken::RSquare); 2341 if (Tok.is(MMToken::RSquare)) 2342 consumeToken(); 2343 HadError = true; 2344 } 2345 2346 // Decode the attribute name. 2347 AttributeKind Attribute 2348 = llvm::StringSwitch<AttributeKind>(Tok.getString()) 2349 .Case("exhaustive", AT_exhaustive) 2350 .Case("extern_c", AT_extern_c) 2351 .Case("system", AT_system) 2352 .Default(AT_unknown); 2353 switch (Attribute) { 2354 case AT_unknown: 2355 Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 2356 << Tok.getString(); 2357 break; 2358 2359 case AT_system: 2360 Attrs.IsSystem = true; 2361 break; 2362 2363 case AT_extern_c: 2364 Attrs.IsExternC = true; 2365 break; 2366 2367 case AT_exhaustive: 2368 Attrs.IsExhaustive = true; 2369 break; 2370 } 2371 consumeToken(); 2372 2373 // Consume the ']'. 2374 if (!Tok.is(MMToken::RSquare)) { 2375 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 2376 Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 2377 skipUntil(MMToken::RSquare); 2378 HadError = true; 2379 } 2380 2381 if (Tok.is(MMToken::RSquare)) 2382 consumeToken(); 2383 } 2384 2385 return HadError; 2386 } 2387 2388 /// \brief Parse a module map file. 2389 /// 2390 /// module-map-file: 2391 /// module-declaration* 2392 bool ModuleMapParser::parseModuleMapFile() { 2393 do { 2394 switch (Tok.Kind) { 2395 case MMToken::EndOfFile: 2396 return HadError; 2397 2398 case MMToken::ExplicitKeyword: 2399 case MMToken::ExternKeyword: 2400 case MMToken::ModuleKeyword: 2401 case MMToken::FrameworkKeyword: 2402 parseModuleDecl(); 2403 break; 2404 2405 case MMToken::Comma: 2406 case MMToken::ConfigMacros: 2407 case MMToken::Conflict: 2408 case MMToken::Exclaim: 2409 case MMToken::ExcludeKeyword: 2410 case MMToken::ExportKeyword: 2411 case MMToken::HeaderKeyword: 2412 case MMToken::Identifier: 2413 case MMToken::LBrace: 2414 case MMToken::LinkKeyword: 2415 case MMToken::LSquare: 2416 case MMToken::Period: 2417 case MMToken::PrivateKeyword: 2418 case MMToken::RBrace: 2419 case MMToken::RSquare: 2420 case MMToken::RequiresKeyword: 2421 case MMToken::Star: 2422 case MMToken::StringLiteral: 2423 case MMToken::TextualKeyword: 2424 case MMToken::UmbrellaKeyword: 2425 case MMToken::UseKeyword: 2426 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 2427 HadError = true; 2428 consumeToken(); 2429 break; 2430 } 2431 } while (true); 2432 } 2433 2434 bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem, 2435 const DirectoryEntry *Dir, 2436 SourceLocation ExternModuleLoc) { 2437 llvm::DenseMap<const FileEntry *, bool>::iterator Known 2438 = ParsedModuleMap.find(File); 2439 if (Known != ParsedModuleMap.end()) 2440 return Known->second; 2441 2442 assert(Target && "Missing target information"); 2443 auto FileCharacter = IsSystem ? SrcMgr::C_System : SrcMgr::C_User; 2444 FileID ID = SourceMgr.createFileID(File, ExternModuleLoc, FileCharacter); 2445 const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID); 2446 if (!Buffer) 2447 return ParsedModuleMap[File] = true; 2448 2449 // Parse this module map file. 2450 Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts); 2451 SourceLocation Start = L.getSourceLocation(); 2452 ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir, 2453 BuiltinIncludeDir, IsSystem); 2454 bool Result = Parser.parseModuleMapFile(); 2455 ParsedModuleMap[File] = Result; 2456 2457 // Notify callbacks that we parsed it. 2458 for (const auto &Cb : Callbacks) 2459 Cb->moduleMapFileRead(Start, *File, IsSystem); 2460 return Result; 2461 } 2462