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