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