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