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