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