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 available modules. 324 if (New.getModule()->isAvailable() && !Old.getModule()->isAvailable()) 325 return true; 326 327 // Prefer a public header over a private header. 328 if ((New.getRole() & ModuleMap::PrivateHeader) != 329 (Old.getRole() & ModuleMap::PrivateHeader)) 330 return !(New.getRole() & ModuleMap::PrivateHeader); 331 332 // Prefer a non-textual header over a textual header. 333 if ((New.getRole() & ModuleMap::TextualHeader) != 334 (Old.getRole() & ModuleMap::TextualHeader)) 335 return !(New.getRole() & ModuleMap::TextualHeader); 336 337 // Don't have a reason to choose between these. Just keep the first one. 338 return false; 339 } 340 341 ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File) { 342 auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader { 343 if (R.getRole() & ModuleMap::TextualHeader) 344 return ModuleMap::KnownHeader(); 345 return R; 346 }; 347 348 HeadersMap::iterator Known = findKnownHeader(File); 349 if (Known != Headers.end()) { 350 ModuleMap::KnownHeader Result; 351 // Iterate over all modules that 'File' is part of to find the best fit. 352 for (KnownHeader &H : Known->second) { 353 // Prefer a header from the current module over all others. 354 if (H.getModule()->getTopLevelModule() == CompilingModule) 355 return MakeResult(H); 356 if (!Result || isBetterKnownHeader(H, Result)) 357 Result = H; 358 } 359 return MakeResult(Result); 360 } 361 362 return MakeResult(findOrCreateModuleForHeaderInUmbrellaDir(File)); 363 } 364 365 ModuleMap::KnownHeader 366 ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File) { 367 assert(!Headers.count(File) && "already have a module for this header"); 368 369 SmallVector<const DirectoryEntry *, 2> SkippedDirs; 370 KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs); 371 if (H) { 372 Module *Result = H.getModule(); 373 374 // Search up the module stack until we find a module with an umbrella 375 // directory. 376 Module *UmbrellaModule = Result; 377 while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 378 UmbrellaModule = UmbrellaModule->Parent; 379 380 if (UmbrellaModule->InferSubmodules) { 381 const FileEntry *UmbrellaModuleMap = 382 getModuleMapFileForUniquing(UmbrellaModule); 383 384 // Infer submodules for each of the directories we found between 385 // the directory of the umbrella header and the directory where 386 // the actual header is located. 387 bool Explicit = UmbrellaModule->InferExplicitSubmodules; 388 389 for (unsigned I = SkippedDirs.size(); I != 0; --I) { 390 // Find or create the module that corresponds to this directory name. 391 SmallString<32> NameBuf; 392 StringRef Name = sanitizeFilenameAsIdentifier( 393 llvm::sys::path::stem(SkippedDirs[I-1]->getName()), NameBuf); 394 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 395 Explicit).first; 396 InferredModuleAllowedBy[Result] = UmbrellaModuleMap; 397 Result->IsInferred = true; 398 399 // Associate the module and the directory. 400 UmbrellaDirs[SkippedDirs[I-1]] = Result; 401 402 // If inferred submodules export everything they import, add a 403 // wildcard to the set of exports. 404 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 405 Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 406 } 407 408 // Infer a submodule with the same name as this header file. 409 SmallString<32> NameBuf; 410 StringRef Name = sanitizeFilenameAsIdentifier( 411 llvm::sys::path::stem(File->getName()), NameBuf); 412 Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 413 Explicit).first; 414 InferredModuleAllowedBy[Result] = UmbrellaModuleMap; 415 Result->IsInferred = true; 416 Result->addTopHeader(File); 417 418 // If inferred submodules export everything they import, add a 419 // wildcard to the set of exports. 420 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 421 Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 422 } else { 423 // Record each of the directories we stepped through as being part of 424 // the module we found, since the umbrella header covers them all. 425 for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 426 UmbrellaDirs[SkippedDirs[I]] = Result; 427 } 428 429 KnownHeader Header(Result, NormalHeader); 430 Headers[File].push_back(Header); 431 return Header; 432 } 433 434 return KnownHeader(); 435 } 436 437 ArrayRef<ModuleMap::KnownHeader> 438 ModuleMap::findAllModulesForHeader(const FileEntry *File) const { 439 auto It = Headers.find(File); 440 if (It == Headers.end()) 441 return None; 442 return It->second; 443 } 444 445 bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const { 446 return isHeaderUnavailableInModule(Header, nullptr); 447 } 448 449 bool 450 ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header, 451 const Module *RequestingModule) const { 452 HeadersMap::const_iterator Known = Headers.find(Header); 453 if (Known != Headers.end()) { 454 for (SmallVectorImpl<KnownHeader>::const_iterator 455 I = Known->second.begin(), 456 E = Known->second.end(); 457 I != E; ++I) { 458 if (I->isAvailable() && (!RequestingModule || 459 I->getModule()->isSubModuleOf(RequestingModule))) 460 return false; 461 } 462 return true; 463 } 464 465 const DirectoryEntry *Dir = Header->getDir(); 466 SmallVector<const DirectoryEntry *, 2> SkippedDirs; 467 StringRef DirName = Dir->getName(); 468 469 auto IsUnavailable = [&](const Module *M) { 470 return !M->isAvailable() && (!RequestingModule || 471 M->isSubModuleOf(RequestingModule)); 472 }; 473 474 // Keep walking up the directory hierarchy, looking for a directory with 475 // an umbrella header. 476 do { 477 llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir 478 = UmbrellaDirs.find(Dir); 479 if (KnownDir != UmbrellaDirs.end()) { 480 Module *Found = KnownDir->second; 481 if (IsUnavailable(Found)) 482 return true; 483 484 // Search up the module stack until we find a module with an umbrella 485 // directory. 486 Module *UmbrellaModule = Found; 487 while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 488 UmbrellaModule = UmbrellaModule->Parent; 489 490 if (UmbrellaModule->InferSubmodules) { 491 for (unsigned I = SkippedDirs.size(); I != 0; --I) { 492 // Find or create the module that corresponds to this directory name. 493 SmallString<32> NameBuf; 494 StringRef Name = sanitizeFilenameAsIdentifier( 495 llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 496 NameBuf); 497 Found = lookupModuleQualified(Name, Found); 498 if (!Found) 499 return false; 500 if (IsUnavailable(Found)) 501 return true; 502 } 503 504 // Infer a submodule with the same name as this header file. 505 SmallString<32> NameBuf; 506 StringRef Name = sanitizeFilenameAsIdentifier( 507 llvm::sys::path::stem(Header->getName()), 508 NameBuf); 509 Found = lookupModuleQualified(Name, Found); 510 if (!Found) 511 return false; 512 } 513 514 return IsUnavailable(Found); 515 } 516 517 SkippedDirs.push_back(Dir); 518 519 // Retrieve our parent path. 520 DirName = llvm::sys::path::parent_path(DirName); 521 if (DirName.empty()) 522 break; 523 524 // Resolve the parent path to a directory entry. 525 Dir = SourceMgr.getFileManager().getDirectory(DirName); 526 } while (Dir); 527 528 return false; 529 } 530 531 Module *ModuleMap::findModule(StringRef Name) const { 532 llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name); 533 if (Known != Modules.end()) 534 return Known->getValue(); 535 536 return nullptr; 537 } 538 539 Module *ModuleMap::lookupModuleUnqualified(StringRef Name, 540 Module *Context) const { 541 for(; Context; Context = Context->Parent) { 542 if (Module *Sub = lookupModuleQualified(Name, Context)) 543 return Sub; 544 } 545 546 return findModule(Name); 547 } 548 549 Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{ 550 if (!Context) 551 return findModule(Name); 552 553 return Context->findSubmodule(Name); 554 } 555 556 std::pair<Module *, bool> 557 ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 558 bool IsExplicit) { 559 // Try to find an existing module with this name. 560 if (Module *Sub = lookupModuleQualified(Name, Parent)) 561 return std::make_pair(Sub, false); 562 563 // Create a new module with this name. 564 Module *Result = new Module(Name, SourceLocation(), Parent, 565 IsFramework, IsExplicit, NumCreatedModules++); 566 if (LangOpts.CurrentModule == Name) { 567 SourceModule = Result; 568 SourceModuleName = Name; 569 } 570 if (!Parent) { 571 Modules[Name] = Result; 572 if (!LangOpts.CurrentModule.empty() && !CompilingModule && 573 Name == LangOpts.CurrentModule) { 574 CompilingModule = Result; 575 } 576 } 577 return std::make_pair(Result, true); 578 } 579 580 /// \brief For a framework module, infer the framework against which we 581 /// should link. 582 static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir, 583 FileManager &FileMgr) { 584 assert(Mod->IsFramework && "Can only infer linking for framework modules"); 585 assert(!Mod->isSubFramework() && 586 "Can only infer linking for top-level frameworks"); 587 588 SmallString<128> LibName; 589 LibName += FrameworkDir->getName(); 590 llvm::sys::path::append(LibName, Mod->Name); 591 if (FileMgr.getFile(LibName)) { 592 Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name, 593 /*IsFramework=*/true)); 594 } 595 } 596 597 Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir, 598 bool IsSystem, Module *Parent) { 599 Attributes Attrs; 600 Attrs.IsSystem = IsSystem; 601 return inferFrameworkModule(FrameworkDir, Attrs, Parent); 602 } 603 604 Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir, 605 Attributes Attrs, Module *Parent) { 606 // Note: as an egregious but useful hack we use the real path here, because 607 // we might be looking at an embedded framework that symlinks out to a 608 // top-level framework, and we need to infer as if we were naming the 609 // top-level framework. 610 StringRef FrameworkDirName = 611 SourceMgr.getFileManager().getCanonicalName(FrameworkDir); 612 613 // In case this is a case-insensitive filesystem, use the canonical 614 // directory name as the ModuleName, since modules are case-sensitive. 615 // FIXME: we should be able to give a fix-it hint for the correct spelling. 616 SmallString<32> ModuleNameStorage; 617 StringRef ModuleName = sanitizeFilenameAsIdentifier( 618 llvm::sys::path::stem(FrameworkDirName), ModuleNameStorage); 619 620 // Check whether we've already found this module. 621 if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 622 return Mod; 623 624 FileManager &FileMgr = SourceMgr.getFileManager(); 625 626 // If the framework has a parent path from which we're allowed to infer 627 // a framework module, do so. 628 const FileEntry *ModuleMapFile = nullptr; 629 if (!Parent) { 630 // Determine whether we're allowed to infer a module map. 631 bool canInfer = false; 632 if (llvm::sys::path::has_parent_path(FrameworkDirName)) { 633 // Figure out the parent path. 634 StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); 635 if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) { 636 // Check whether we have already looked into the parent directory 637 // for a module map. 638 llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 639 inferred = InferredDirectories.find(ParentDir); 640 if (inferred == InferredDirectories.end()) { 641 // We haven't looked here before. Load a module map, if there is 642 // one. 643 bool IsFrameworkDir = Parent.endswith(".framework"); 644 if (const FileEntry *ModMapFile = 645 HeaderInfo.lookupModuleMapFile(ParentDir, IsFrameworkDir)) { 646 parseModuleMapFile(ModMapFile, Attrs.IsSystem, ParentDir); 647 inferred = InferredDirectories.find(ParentDir); 648 } 649 650 if (inferred == InferredDirectories.end()) 651 inferred = InferredDirectories.insert( 652 std::make_pair(ParentDir, InferredDirectory())).first; 653 } 654 655 if (inferred->second.InferModules) { 656 // We're allowed to infer for this directory, but make sure it's okay 657 // to infer this particular module. 658 StringRef Name = llvm::sys::path::stem(FrameworkDirName); 659 canInfer = std::find(inferred->second.ExcludedModules.begin(), 660 inferred->second.ExcludedModules.end(), 661 Name) == inferred->second.ExcludedModules.end(); 662 663 Attrs.IsSystem |= inferred->second.Attrs.IsSystem; 664 Attrs.IsExternC |= inferred->second.Attrs.IsExternC; 665 Attrs.IsExhaustive |= inferred->second.Attrs.IsExhaustive; 666 ModuleMapFile = inferred->second.ModuleMapFile; 667 } 668 } 669 } 670 671 // If we're not allowed to infer a framework module, don't. 672 if (!canInfer) 673 return nullptr; 674 } else 675 ModuleMapFile = getModuleMapFileForUniquing(Parent); 676 677 678 // Look for an umbrella header. 679 SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 680 llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h"); 681 const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 682 683 // FIXME: If there's no umbrella header, we could probably scan the 684 // framework to load *everything*. But, it's not clear that this is a good 685 // idea. 686 if (!UmbrellaHeader) 687 return nullptr; 688 689 Module *Result = new Module(ModuleName, SourceLocation(), Parent, 690 /*IsFramework=*/true, /*IsExplicit=*/false, 691 NumCreatedModules++); 692 InferredModuleAllowedBy[Result] = ModuleMapFile; 693 Result->IsInferred = true; 694 if (LangOpts.CurrentModule == ModuleName) { 695 SourceModule = Result; 696 SourceModuleName = ModuleName; 697 } 698 699 Result->IsSystem |= Attrs.IsSystem; 700 Result->IsExternC |= Attrs.IsExternC; 701 Result->ConfigMacrosExhaustive |= Attrs.IsExhaustive; 702 Result->Directory = FrameworkDir; 703 704 if (!Parent) 705 Modules[ModuleName] = Result; 706 707 // umbrella header "umbrella-header-name" 708 // 709 // The "Headers/" component of the name is implied because this is 710 // a framework module. 711 setUmbrellaHeader(Result, UmbrellaHeader, ModuleName + ".h"); 712 713 // export * 714 Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 715 716 // module * { export * } 717 Result->InferSubmodules = true; 718 Result->InferExportWildcard = true; 719 720 // Look for subframeworks. 721 std::error_code EC; 722 SmallString<128> SubframeworksDirName 723 = StringRef(FrameworkDir->getName()); 724 llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 725 llvm::sys::path::native(SubframeworksDirName); 726 for (llvm::sys::fs::directory_iterator Dir(SubframeworksDirName, EC), DirEnd; 727 Dir != DirEnd && !EC; Dir.increment(EC)) { 728 if (!StringRef(Dir->path()).endswith(".framework")) 729 continue; 730 731 if (const DirectoryEntry *SubframeworkDir 732 = FileMgr.getDirectory(Dir->path())) { 733 // Note: as an egregious but useful hack, we use the real path here and 734 // check whether it is actually a subdirectory of the parent directory. 735 // This will not be the case if the 'subframework' is actually a symlink 736 // out to a top-level framework. 737 StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir); 738 bool FoundParent = false; 739 do { 740 // Get the parent directory name. 741 SubframeworkDirName 742 = llvm::sys::path::parent_path(SubframeworkDirName); 743 if (SubframeworkDirName.empty()) 744 break; 745 746 if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { 747 FoundParent = true; 748 break; 749 } 750 } while (true); 751 752 if (!FoundParent) 753 continue; 754 755 // FIXME: Do we want to warn about subframeworks without umbrella headers? 756 inferFrameworkModule(SubframeworkDir, Attrs, Result); 757 } 758 } 759 760 // If the module is a top-level framework, automatically link against the 761 // framework. 762 if (!Result->isSubFramework()) { 763 inferFrameworkLink(Result, FrameworkDir, FileMgr); 764 } 765 766 return Result; 767 } 768 769 void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader, 770 Twine NameAsWritten) { 771 Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader)); 772 Mod->Umbrella = UmbrellaHeader; 773 Mod->UmbrellaAsWritten = NameAsWritten.str(); 774 UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; 775 } 776 777 void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir, 778 Twine NameAsWritten) { 779 Mod->Umbrella = UmbrellaDir; 780 Mod->UmbrellaAsWritten = NameAsWritten.str(); 781 UmbrellaDirs[UmbrellaDir] = Mod; 782 } 783 784 static Module::HeaderKind headerRoleToKind(ModuleMap::ModuleHeaderRole Role) { 785 switch ((int)Role) { 786 default: llvm_unreachable("unknown header role"); 787 case ModuleMap::NormalHeader: 788 return Module::HK_Normal; 789 case ModuleMap::PrivateHeader: 790 return Module::HK_Private; 791 case ModuleMap::TextualHeader: 792 return Module::HK_Textual; 793 case ModuleMap::PrivateHeader | ModuleMap::TextualHeader: 794 return Module::HK_PrivateTextual; 795 } 796 } 797 798 void ModuleMap::addHeader(Module *Mod, Module::Header Header, 799 ModuleHeaderRole Role, bool Imported) { 800 KnownHeader KH(Mod, Role); 801 802 // Only add each header to the headers list once. 803 // FIXME: Should we diagnose if a header is listed twice in the 804 // same module definition? 805 auto &HeaderList = Headers[Header.Entry]; 806 for (auto H : HeaderList) 807 if (H == KH) 808 return; 809 810 HeaderList.push_back(KH); 811 Mod->Headers[headerRoleToKind(Role)].push_back(std::move(Header)); 812 813 bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule; 814 if (!Imported || isCompilingModuleHeader) { 815 // When we import HeaderFileInfo, the external source is expected to 816 // set the isModuleHeader flag itself. 817 HeaderInfo.MarkFileModuleHeader(Header.Entry, Role, 818 isCompilingModuleHeader); 819 } 820 } 821 822 void ModuleMap::excludeHeader(Module *Mod, Module::Header Header) { 823 // Add this as a known header so we won't implicitly add it to any 824 // umbrella directory module. 825 // FIXME: Should we only exclude it from umbrella modules within the 826 // specified module? 827 (void) Headers[Header.Entry]; 828 829 Mod->Headers[Module::HK_Excluded].push_back(std::move(Header)); 830 } 831 832 const FileEntry * 833 ModuleMap::getContainingModuleMapFile(const Module *Module) const { 834 if (Module->DefinitionLoc.isInvalid()) 835 return nullptr; 836 837 return SourceMgr.getFileEntryForID( 838 SourceMgr.getFileID(Module->DefinitionLoc)); 839 } 840 841 const FileEntry *ModuleMap::getModuleMapFileForUniquing(const Module *M) const { 842 if (M->IsInferred) { 843 assert(InferredModuleAllowedBy.count(M) && "missing inferred module map"); 844 return InferredModuleAllowedBy.find(M)->second; 845 } 846 return getContainingModuleMapFile(M); 847 } 848 849 void ModuleMap::setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap) { 850 assert(M->IsInferred && "module not inferred"); 851 InferredModuleAllowedBy[M] = ModMap; 852 } 853 854 void ModuleMap::dump() { 855 llvm::errs() << "Modules:"; 856 for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 857 MEnd = Modules.end(); 858 M != MEnd; ++M) 859 M->getValue()->print(llvm::errs(), 2); 860 861 llvm::errs() << "Headers:"; 862 for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 863 H != HEnd; ++H) { 864 llvm::errs() << " \"" << H->first->getName() << "\" -> "; 865 for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(), 866 E = H->second.end(); 867 I != E; ++I) { 868 if (I != H->second.begin()) 869 llvm::errs() << ","; 870 llvm::errs() << I->getModule()->getFullModuleName(); 871 } 872 llvm::errs() << "\n"; 873 } 874 } 875 876 bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 877 auto Unresolved = std::move(Mod->UnresolvedExports); 878 Mod->UnresolvedExports.clear(); 879 for (auto &UE : Unresolved) { 880 Module::ExportDecl Export = resolveExport(Mod, UE, Complain); 881 if (Export.getPointer() || Export.getInt()) 882 Mod->Exports.push_back(Export); 883 else 884 Mod->UnresolvedExports.push_back(UE); 885 } 886 return !Mod->UnresolvedExports.empty(); 887 } 888 889 bool ModuleMap::resolveUses(Module *Mod, bool Complain) { 890 auto Unresolved = std::move(Mod->UnresolvedDirectUses); 891 Mod->UnresolvedDirectUses.clear(); 892 for (auto &UDU : Unresolved) { 893 Module *DirectUse = resolveModuleId(UDU, Mod, Complain); 894 if (DirectUse) 895 Mod->DirectUses.push_back(DirectUse); 896 else 897 Mod->UnresolvedDirectUses.push_back(UDU); 898 } 899 return !Mod->UnresolvedDirectUses.empty(); 900 } 901 902 bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) { 903 auto Unresolved = std::move(Mod->UnresolvedConflicts); 904 Mod->UnresolvedConflicts.clear(); 905 for (auto &UC : Unresolved) { 906 if (Module *OtherMod = resolveModuleId(UC.Id, Mod, Complain)) { 907 Module::Conflict Conflict; 908 Conflict.Other = OtherMod; 909 Conflict.Message = UC.Message; 910 Mod->Conflicts.push_back(Conflict); 911 } else 912 Mod->UnresolvedConflicts.push_back(UC); 913 } 914 return !Mod->UnresolvedConflicts.empty(); 915 } 916 917 Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 918 if (Loc.isInvalid()) 919 return nullptr; 920 921 // Use the expansion location to determine which module we're in. 922 FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 923 if (!ExpansionLoc.isFileID()) 924 return nullptr; 925 926 const SourceManager &SrcMgr = Loc.getManager(); 927 FileID ExpansionFileID = ExpansionLoc.getFileID(); 928 929 while (const FileEntry *ExpansionFile 930 = SrcMgr.getFileEntryForID(ExpansionFileID)) { 931 // Find the module that owns this header (if any). 932 if (Module *Mod = findModuleForHeader(ExpansionFile).getModule()) 933 return Mod; 934 935 // No module owns this header, so look up the inclusion chain to see if 936 // any included header has an associated module. 937 SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); 938 if (IncludeLoc.isInvalid()) 939 return nullptr; 940 941 ExpansionFileID = SrcMgr.getFileID(IncludeLoc); 942 } 943 944 return nullptr; 945 } 946 947 //----------------------------------------------------------------------------// 948 // Module map file parser 949 //----------------------------------------------------------------------------// 950 951 namespace clang { 952 /// \brief A token in a module map file. 953 struct MMToken { 954 enum TokenKind { 955 Comma, 956 ConfigMacros, 957 Conflict, 958 EndOfFile, 959 HeaderKeyword, 960 Identifier, 961 Exclaim, 962 ExcludeKeyword, 963 ExplicitKeyword, 964 ExportKeyword, 965 ExternKeyword, 966 FrameworkKeyword, 967 LinkKeyword, 968 ModuleKeyword, 969 Period, 970 PrivateKeyword, 971 UmbrellaKeyword, 972 UseKeyword, 973 RequiresKeyword, 974 Star, 975 StringLiteral, 976 TextualKeyword, 977 LBrace, 978 RBrace, 979 LSquare, 980 RSquare 981 } Kind; 982 983 unsigned Location; 984 unsigned StringLength; 985 const char *StringData; 986 987 void clear() { 988 Kind = EndOfFile; 989 Location = 0; 990 StringLength = 0; 991 StringData = nullptr; 992 } 993 994 bool is(TokenKind K) const { return Kind == K; } 995 996 SourceLocation getLocation() const { 997 return SourceLocation::getFromRawEncoding(Location); 998 } 999 1000 StringRef getString() const { 1001 return StringRef(StringData, StringLength); 1002 } 1003 }; 1004 1005 class ModuleMapParser { 1006 Lexer &L; 1007 SourceManager &SourceMgr; 1008 1009 /// \brief Default target information, used only for string literal 1010 /// parsing. 1011 const TargetInfo *Target; 1012 1013 DiagnosticsEngine &Diags; 1014 ModuleMap ⤅ 1015 1016 /// \brief The current module map file. 1017 const FileEntry *ModuleMapFile; 1018 1019 /// \brief The directory that file names in this module map file should 1020 /// be resolved relative to. 1021 const DirectoryEntry *Directory; 1022 1023 /// \brief The directory containing Clang-supplied headers. 1024 const DirectoryEntry *BuiltinIncludeDir; 1025 1026 /// \brief Whether this module map is in a system header directory. 1027 bool IsSystem; 1028 1029 /// \brief Whether an error occurred. 1030 bool HadError; 1031 1032 /// \brief Stores string data for the various string literals referenced 1033 /// during parsing. 1034 llvm::BumpPtrAllocator StringData; 1035 1036 /// \brief The current token. 1037 MMToken Tok; 1038 1039 /// \brief The active module. 1040 Module *ActiveModule; 1041 1042 /// \brief Whether a module uses the 'requires excluded' hack to mark its 1043 /// contents as 'textual'. 1044 /// 1045 /// On older Darwin SDK versions, 'requires excluded' is used to mark the 1046 /// contents of the Darwin.C.excluded (assert.h) and Tcl.Private modules as 1047 /// non-modular headers. For backwards compatibility, we continue to 1048 /// support this idiom for just these modules, and map the headers to 1049 /// 'textual' to match the original intent. 1050 llvm::SmallPtrSet<Module *, 2> UsesRequiresExcludedHack; 1051 1052 /// \brief Consume the current token and return its location. 1053 SourceLocation consumeToken(); 1054 1055 /// \brief Skip tokens until we reach the a token with the given kind 1056 /// (or the end of the file). 1057 void skipUntil(MMToken::TokenKind K); 1058 1059 typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; 1060 bool parseModuleId(ModuleId &Id); 1061 void parseModuleDecl(); 1062 void parseExternModuleDecl(); 1063 void parseRequiresDecl(); 1064 void parseHeaderDecl(clang::MMToken::TokenKind, 1065 SourceLocation LeadingLoc); 1066 void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 1067 void parseExportDecl(); 1068 void parseUseDecl(); 1069 void parseLinkDecl(); 1070 void parseConfigMacros(); 1071 void parseConflict(); 1072 void parseInferredModuleDecl(bool Framework, bool Explicit); 1073 1074 typedef ModuleMap::Attributes Attributes; 1075 bool parseOptionalAttributes(Attributes &Attrs); 1076 1077 public: 1078 explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 1079 const TargetInfo *Target, 1080 DiagnosticsEngine &Diags, 1081 ModuleMap &Map, 1082 const FileEntry *ModuleMapFile, 1083 const DirectoryEntry *Directory, 1084 const DirectoryEntry *BuiltinIncludeDir, 1085 bool IsSystem) 1086 : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 1087 ModuleMapFile(ModuleMapFile), Directory(Directory), 1088 BuiltinIncludeDir(BuiltinIncludeDir), IsSystem(IsSystem), 1089 HadError(false), ActiveModule(nullptr) 1090 { 1091 Tok.clear(); 1092 consumeToken(); 1093 } 1094 1095 bool parseModuleMapFile(); 1096 }; 1097 } 1098 1099 SourceLocation ModuleMapParser::consumeToken() { 1100 retry: 1101 SourceLocation Result = Tok.getLocation(); 1102 Tok.clear(); 1103 1104 Token LToken; 1105 L.LexFromRawLexer(LToken); 1106 Tok.Location = LToken.getLocation().getRawEncoding(); 1107 switch (LToken.getKind()) { 1108 case tok::raw_identifier: { 1109 StringRef RI = LToken.getRawIdentifier(); 1110 Tok.StringData = RI.data(); 1111 Tok.StringLength = RI.size(); 1112 Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI) 1113 .Case("config_macros", MMToken::ConfigMacros) 1114 .Case("conflict", MMToken::Conflict) 1115 .Case("exclude", MMToken::ExcludeKeyword) 1116 .Case("explicit", MMToken::ExplicitKeyword) 1117 .Case("export", MMToken::ExportKeyword) 1118 .Case("extern", MMToken::ExternKeyword) 1119 .Case("framework", MMToken::FrameworkKeyword) 1120 .Case("header", MMToken::HeaderKeyword) 1121 .Case("link", MMToken::LinkKeyword) 1122 .Case("module", MMToken::ModuleKeyword) 1123 .Case("private", MMToken::PrivateKeyword) 1124 .Case("requires", MMToken::RequiresKeyword) 1125 .Case("textual", MMToken::TextualKeyword) 1126 .Case("umbrella", MMToken::UmbrellaKeyword) 1127 .Case("use", MMToken::UseKeyword) 1128 .Default(MMToken::Identifier); 1129 break; 1130 } 1131 1132 case tok::comma: 1133 Tok.Kind = MMToken::Comma; 1134 break; 1135 1136 case tok::eof: 1137 Tok.Kind = MMToken::EndOfFile; 1138 break; 1139 1140 case tok::l_brace: 1141 Tok.Kind = MMToken::LBrace; 1142 break; 1143 1144 case tok::l_square: 1145 Tok.Kind = MMToken::LSquare; 1146 break; 1147 1148 case tok::period: 1149 Tok.Kind = MMToken::Period; 1150 break; 1151 1152 case tok::r_brace: 1153 Tok.Kind = MMToken::RBrace; 1154 break; 1155 1156 case tok::r_square: 1157 Tok.Kind = MMToken::RSquare; 1158 break; 1159 1160 case tok::star: 1161 Tok.Kind = MMToken::Star; 1162 break; 1163 1164 case tok::exclaim: 1165 Tok.Kind = MMToken::Exclaim; 1166 break; 1167 1168 case tok::string_literal: { 1169 if (LToken.hasUDSuffix()) { 1170 Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 1171 HadError = true; 1172 goto retry; 1173 } 1174 1175 // Parse the string literal. 1176 LangOptions LangOpts; 1177 StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target); 1178 if (StringLiteral.hadError) 1179 goto retry; 1180 1181 // Copy the string literal into our string data allocator. 1182 unsigned Length = StringLiteral.GetStringLength(); 1183 char *Saved = StringData.Allocate<char>(Length + 1); 1184 memcpy(Saved, StringLiteral.GetString().data(), Length); 1185 Saved[Length] = 0; 1186 1187 // Form the token. 1188 Tok.Kind = MMToken::StringLiteral; 1189 Tok.StringData = Saved; 1190 Tok.StringLength = Length; 1191 break; 1192 } 1193 1194 case tok::comment: 1195 goto retry; 1196 1197 default: 1198 Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 1199 HadError = true; 1200 goto retry; 1201 } 1202 1203 return Result; 1204 } 1205 1206 void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 1207 unsigned braceDepth = 0; 1208 unsigned squareDepth = 0; 1209 do { 1210 switch (Tok.Kind) { 1211 case MMToken::EndOfFile: 1212 return; 1213 1214 case MMToken::LBrace: 1215 if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1216 return; 1217 1218 ++braceDepth; 1219 break; 1220 1221 case MMToken::LSquare: 1222 if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1223 return; 1224 1225 ++squareDepth; 1226 break; 1227 1228 case MMToken::RBrace: 1229 if (braceDepth > 0) 1230 --braceDepth; 1231 else if (Tok.is(K)) 1232 return; 1233 break; 1234 1235 case MMToken::RSquare: 1236 if (squareDepth > 0) 1237 --squareDepth; 1238 else if (Tok.is(K)) 1239 return; 1240 break; 1241 1242 default: 1243 if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 1244 return; 1245 break; 1246 } 1247 1248 consumeToken(); 1249 } while (true); 1250 } 1251 1252 /// \brief Parse a module-id. 1253 /// 1254 /// module-id: 1255 /// identifier 1256 /// identifier '.' module-id 1257 /// 1258 /// \returns true if an error occurred, false otherwise. 1259 bool ModuleMapParser::parseModuleId(ModuleId &Id) { 1260 Id.clear(); 1261 do { 1262 if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) { 1263 Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 1264 consumeToken(); 1265 } else { 1266 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 1267 return true; 1268 } 1269 1270 if (!Tok.is(MMToken::Period)) 1271 break; 1272 1273 consumeToken(); 1274 } while (true); 1275 1276 return false; 1277 } 1278 1279 namespace { 1280 /// \brief Enumerates the known attributes. 1281 enum AttributeKind { 1282 /// \brief An unknown attribute. 1283 AT_unknown, 1284 /// \brief The 'system' attribute. 1285 AT_system, 1286 /// \brief The 'extern_c' attribute. 1287 AT_extern_c, 1288 /// \brief The 'exhaustive' attribute. 1289 AT_exhaustive 1290 }; 1291 } 1292 1293 /// \brief Parse a module declaration. 1294 /// 1295 /// module-declaration: 1296 /// 'extern' 'module' module-id string-literal 1297 /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 1298 /// { module-member* } 1299 /// 1300 /// module-member: 1301 /// requires-declaration 1302 /// header-declaration 1303 /// submodule-declaration 1304 /// export-declaration 1305 /// link-declaration 1306 /// 1307 /// submodule-declaration: 1308 /// module-declaration 1309 /// inferred-submodule-declaration 1310 void ModuleMapParser::parseModuleDecl() { 1311 assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 1312 Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword)); 1313 if (Tok.is(MMToken::ExternKeyword)) { 1314 parseExternModuleDecl(); 1315 return; 1316 } 1317 1318 // Parse 'explicit' or 'framework' keyword, if present. 1319 SourceLocation ExplicitLoc; 1320 bool Explicit = false; 1321 bool Framework = false; 1322 1323 // Parse 'explicit' keyword, if present. 1324 if (Tok.is(MMToken::ExplicitKeyword)) { 1325 ExplicitLoc = consumeToken(); 1326 Explicit = true; 1327 } 1328 1329 // Parse 'framework' keyword, if present. 1330 if (Tok.is(MMToken::FrameworkKeyword)) { 1331 consumeToken(); 1332 Framework = true; 1333 } 1334 1335 // Parse 'module' keyword. 1336 if (!Tok.is(MMToken::ModuleKeyword)) { 1337 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1338 consumeToken(); 1339 HadError = true; 1340 return; 1341 } 1342 consumeToken(); // 'module' keyword 1343 1344 // If we have a wildcard for the module name, this is an inferred submodule. 1345 // Parse it. 1346 if (Tok.is(MMToken::Star)) 1347 return parseInferredModuleDecl(Framework, Explicit); 1348 1349 // Parse the module name. 1350 ModuleId Id; 1351 if (parseModuleId(Id)) { 1352 HadError = true; 1353 return; 1354 } 1355 1356 if (ActiveModule) { 1357 if (Id.size() > 1) { 1358 Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 1359 << SourceRange(Id.front().second, Id.back().second); 1360 1361 HadError = true; 1362 return; 1363 } 1364 } else if (Id.size() == 1 && Explicit) { 1365 // Top-level modules can't be explicit. 1366 Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 1367 Explicit = false; 1368 ExplicitLoc = SourceLocation(); 1369 HadError = true; 1370 } 1371 1372 Module *PreviousActiveModule = ActiveModule; 1373 if (Id.size() > 1) { 1374 // This module map defines a submodule. Go find the module of which it 1375 // is a submodule. 1376 ActiveModule = nullptr; 1377 const Module *TopLevelModule = nullptr; 1378 for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 1379 if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 1380 if (I == 0) 1381 TopLevelModule = Next; 1382 ActiveModule = Next; 1383 continue; 1384 } 1385 1386 if (ActiveModule) { 1387 Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 1388 << Id[I].first 1389 << ActiveModule->getTopLevelModule()->getFullModuleName(); 1390 } else { 1391 Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 1392 } 1393 HadError = true; 1394 return; 1395 } 1396 1397 if (ModuleMapFile != Map.getContainingModuleMapFile(TopLevelModule)) { 1398 assert(ModuleMapFile != Map.getModuleMapFileForUniquing(TopLevelModule) && 1399 "submodule defined in same file as 'module *' that allowed its " 1400 "top-level module"); 1401 Map.addAdditionalModuleMapFile(TopLevelModule, ModuleMapFile); 1402 } 1403 } 1404 1405 StringRef ModuleName = Id.back().first; 1406 SourceLocation ModuleNameLoc = Id.back().second; 1407 1408 // Parse the optional attribute list. 1409 Attributes Attrs; 1410 parseOptionalAttributes(Attrs); 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 parseOptionalAttributes(Attrs); 2076 if (Attrs.IsExhaustive && !ActiveModule->Parent) { 2077 ActiveModule->ConfigMacrosExhaustive = true; 2078 } 2079 2080 // If we don't have an identifier, we're done. 2081 // FIXME: Support macros with the same name as a keyword here. 2082 if (!Tok.is(MMToken::Identifier)) 2083 return; 2084 2085 // Consume the first identifier. 2086 if (!ActiveModule->Parent) { 2087 ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 2088 } 2089 consumeToken(); 2090 2091 do { 2092 // If there's a comma, consume it. 2093 if (!Tok.is(MMToken::Comma)) 2094 break; 2095 consumeToken(); 2096 2097 // We expect to see a macro name here. 2098 // FIXME: Support macros with the same name as a keyword here. 2099 if (!Tok.is(MMToken::Identifier)) { 2100 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro); 2101 break; 2102 } 2103 2104 // Consume the macro name. 2105 if (!ActiveModule->Parent) { 2106 ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 2107 } 2108 consumeToken(); 2109 } while (true); 2110 } 2111 2112 /// \brief Format a module-id into a string. 2113 static std::string formatModuleId(const ModuleId &Id) { 2114 std::string result; 2115 { 2116 llvm::raw_string_ostream OS(result); 2117 2118 for (unsigned I = 0, N = Id.size(); I != N; ++I) { 2119 if (I) 2120 OS << "."; 2121 OS << Id[I].first; 2122 } 2123 } 2124 2125 return result; 2126 } 2127 2128 /// \brief Parse a conflict declaration. 2129 /// 2130 /// module-declaration: 2131 /// 'conflict' module-id ',' string-literal 2132 void ModuleMapParser::parseConflict() { 2133 assert(Tok.is(MMToken::Conflict)); 2134 SourceLocation ConflictLoc = consumeToken(); 2135 Module::UnresolvedConflict Conflict; 2136 2137 // Parse the module-id. 2138 if (parseModuleId(Conflict.Id)) 2139 return; 2140 2141 // Parse the ','. 2142 if (!Tok.is(MMToken::Comma)) { 2143 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma) 2144 << SourceRange(ConflictLoc); 2145 return; 2146 } 2147 consumeToken(); 2148 2149 // Parse the message. 2150 if (!Tok.is(MMToken::StringLiteral)) { 2151 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message) 2152 << formatModuleId(Conflict.Id); 2153 return; 2154 } 2155 Conflict.Message = Tok.getString().str(); 2156 consumeToken(); 2157 2158 // Add this unresolved conflict. 2159 ActiveModule->UnresolvedConflicts.push_back(Conflict); 2160 } 2161 2162 /// \brief Parse an inferred module declaration (wildcard modules). 2163 /// 2164 /// module-declaration: 2165 /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] 2166 /// { inferred-module-member* } 2167 /// 2168 /// inferred-module-member: 2169 /// 'export' '*' 2170 /// 'exclude' identifier 2171 void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { 2172 assert(Tok.is(MMToken::Star)); 2173 SourceLocation StarLoc = consumeToken(); 2174 bool Failed = false; 2175 2176 // Inferred modules must be submodules. 2177 if (!ActiveModule && !Framework) { 2178 Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 2179 Failed = true; 2180 } 2181 2182 if (ActiveModule) { 2183 // Inferred modules must have umbrella directories. 2184 if (!Failed && ActiveModule->IsAvailable && 2185 !ActiveModule->getUmbrellaDir()) { 2186 Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 2187 Failed = true; 2188 } 2189 2190 // Check for redefinition of an inferred module. 2191 if (!Failed && ActiveModule->InferSubmodules) { 2192 Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 2193 if (ActiveModule->InferredSubmoduleLoc.isValid()) 2194 Diags.Report(ActiveModule->InferredSubmoduleLoc, 2195 diag::note_mmap_prev_definition); 2196 Failed = true; 2197 } 2198 2199 // Check for the 'framework' keyword, which is not permitted here. 2200 if (Framework) { 2201 Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); 2202 Framework = false; 2203 } 2204 } else if (Explicit) { 2205 Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); 2206 Explicit = false; 2207 } 2208 2209 // If there were any problems with this inferred submodule, skip its body. 2210 if (Failed) { 2211 if (Tok.is(MMToken::LBrace)) { 2212 consumeToken(); 2213 skipUntil(MMToken::RBrace); 2214 if (Tok.is(MMToken::RBrace)) 2215 consumeToken(); 2216 } 2217 HadError = true; 2218 return; 2219 } 2220 2221 // Parse optional attributes. 2222 Attributes Attrs; 2223 parseOptionalAttributes(Attrs); 2224 2225 if (ActiveModule) { 2226 // Note that we have an inferred submodule. 2227 ActiveModule->InferSubmodules = true; 2228 ActiveModule->InferredSubmoduleLoc = StarLoc; 2229 ActiveModule->InferExplicitSubmodules = Explicit; 2230 } else { 2231 // We'll be inferring framework modules for this directory. 2232 Map.InferredDirectories[Directory].InferModules = true; 2233 Map.InferredDirectories[Directory].Attrs = Attrs; 2234 Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile; 2235 // FIXME: Handle the 'framework' keyword. 2236 } 2237 2238 // Parse the opening brace. 2239 if (!Tok.is(MMToken::LBrace)) { 2240 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 2241 HadError = true; 2242 return; 2243 } 2244 SourceLocation LBraceLoc = consumeToken(); 2245 2246 // Parse the body of the inferred submodule. 2247 bool Done = false; 2248 do { 2249 switch (Tok.Kind) { 2250 case MMToken::EndOfFile: 2251 case MMToken::RBrace: 2252 Done = true; 2253 break; 2254 2255 case MMToken::ExcludeKeyword: { 2256 if (ActiveModule) { 2257 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2258 << (ActiveModule != nullptr); 2259 consumeToken(); 2260 break; 2261 } 2262 2263 consumeToken(); 2264 // FIXME: Support string-literal module names here. 2265 if (!Tok.is(MMToken::Identifier)) { 2266 Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); 2267 break; 2268 } 2269 2270 Map.InferredDirectories[Directory].ExcludedModules 2271 .push_back(Tok.getString()); 2272 consumeToken(); 2273 break; 2274 } 2275 2276 case MMToken::ExportKeyword: 2277 if (!ActiveModule) { 2278 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2279 << (ActiveModule != nullptr); 2280 consumeToken(); 2281 break; 2282 } 2283 2284 consumeToken(); 2285 if (Tok.is(MMToken::Star)) 2286 ActiveModule->InferExportWildcard = true; 2287 else 2288 Diags.Report(Tok.getLocation(), 2289 diag::err_mmap_expected_export_wildcard); 2290 consumeToken(); 2291 break; 2292 2293 case MMToken::ExplicitKeyword: 2294 case MMToken::ModuleKeyword: 2295 case MMToken::HeaderKeyword: 2296 case MMToken::PrivateKeyword: 2297 case MMToken::UmbrellaKeyword: 2298 default: 2299 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2300 << (ActiveModule != nullptr); 2301 consumeToken(); 2302 break; 2303 } 2304 } while (!Done); 2305 2306 if (Tok.is(MMToken::RBrace)) 2307 consumeToken(); 2308 else { 2309 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 2310 Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 2311 HadError = true; 2312 } 2313 } 2314 2315 /// \brief Parse optional attributes. 2316 /// 2317 /// attributes: 2318 /// attribute attributes 2319 /// attribute 2320 /// 2321 /// attribute: 2322 /// [ identifier ] 2323 /// 2324 /// \param Attrs Will be filled in with the parsed attributes. 2325 /// 2326 /// \returns true if an error occurred, false otherwise. 2327 bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { 2328 bool HadError = false; 2329 2330 while (Tok.is(MMToken::LSquare)) { 2331 // Consume the '['. 2332 SourceLocation LSquareLoc = consumeToken(); 2333 2334 // Check whether we have an attribute name here. 2335 if (!Tok.is(MMToken::Identifier)) { 2336 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 2337 skipUntil(MMToken::RSquare); 2338 if (Tok.is(MMToken::RSquare)) 2339 consumeToken(); 2340 HadError = true; 2341 } 2342 2343 // Decode the attribute name. 2344 AttributeKind Attribute 2345 = llvm::StringSwitch<AttributeKind>(Tok.getString()) 2346 .Case("exhaustive", AT_exhaustive) 2347 .Case("extern_c", AT_extern_c) 2348 .Case("system", AT_system) 2349 .Default(AT_unknown); 2350 switch (Attribute) { 2351 case AT_unknown: 2352 Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 2353 << Tok.getString(); 2354 break; 2355 2356 case AT_system: 2357 Attrs.IsSystem = true; 2358 break; 2359 2360 case AT_extern_c: 2361 Attrs.IsExternC = true; 2362 break; 2363 2364 case AT_exhaustive: 2365 Attrs.IsExhaustive = true; 2366 break; 2367 } 2368 consumeToken(); 2369 2370 // Consume the ']'. 2371 if (!Tok.is(MMToken::RSquare)) { 2372 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 2373 Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 2374 skipUntil(MMToken::RSquare); 2375 HadError = true; 2376 } 2377 2378 if (Tok.is(MMToken::RSquare)) 2379 consumeToken(); 2380 } 2381 2382 return HadError; 2383 } 2384 2385 /// \brief Parse a module map file. 2386 /// 2387 /// module-map-file: 2388 /// module-declaration* 2389 bool ModuleMapParser::parseModuleMapFile() { 2390 do { 2391 switch (Tok.Kind) { 2392 case MMToken::EndOfFile: 2393 return HadError; 2394 2395 case MMToken::ExplicitKeyword: 2396 case MMToken::ExternKeyword: 2397 case MMToken::ModuleKeyword: 2398 case MMToken::FrameworkKeyword: 2399 parseModuleDecl(); 2400 break; 2401 2402 case MMToken::Comma: 2403 case MMToken::ConfigMacros: 2404 case MMToken::Conflict: 2405 case MMToken::Exclaim: 2406 case MMToken::ExcludeKeyword: 2407 case MMToken::ExportKeyword: 2408 case MMToken::HeaderKeyword: 2409 case MMToken::Identifier: 2410 case MMToken::LBrace: 2411 case MMToken::LinkKeyword: 2412 case MMToken::LSquare: 2413 case MMToken::Period: 2414 case MMToken::PrivateKeyword: 2415 case MMToken::RBrace: 2416 case MMToken::RSquare: 2417 case MMToken::RequiresKeyword: 2418 case MMToken::Star: 2419 case MMToken::StringLiteral: 2420 case MMToken::TextualKeyword: 2421 case MMToken::UmbrellaKeyword: 2422 case MMToken::UseKeyword: 2423 Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 2424 HadError = true; 2425 consumeToken(); 2426 break; 2427 } 2428 } while (true); 2429 } 2430 2431 bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem, 2432 const DirectoryEntry *Dir, 2433 SourceLocation ExternModuleLoc) { 2434 llvm::DenseMap<const FileEntry *, bool>::iterator Known 2435 = ParsedModuleMap.find(File); 2436 if (Known != ParsedModuleMap.end()) 2437 return Known->second; 2438 2439 assert(Target && "Missing target information"); 2440 auto FileCharacter = IsSystem ? SrcMgr::C_System : SrcMgr::C_User; 2441 FileID ID = SourceMgr.createFileID(File, ExternModuleLoc, FileCharacter); 2442 const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID); 2443 if (!Buffer) 2444 return ParsedModuleMap[File] = true; 2445 2446 // Parse this module map file. 2447 Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts); 2448 SourceLocation Start = L.getSourceLocation(); 2449 ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir, 2450 BuiltinIncludeDir, IsSystem); 2451 bool Result = Parser.parseModuleMapFile(); 2452 ParsedModuleMap[File] = Result; 2453 2454 // Notify callbacks that we parsed it. 2455 for (const auto &Cb : Callbacks) 2456 Cb->moduleMapFileRead(Start, *File, IsSystem); 2457 return Result; 2458 } 2459