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