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