1718292f2SDouglas Gregor //===--- ModuleMap.cpp - Describe the layout of modules ---------*- C++ -*-===// 2718292f2SDouglas Gregor // 3718292f2SDouglas Gregor // The LLVM Compiler Infrastructure 4718292f2SDouglas Gregor // 5718292f2SDouglas Gregor // This file is distributed under the University of Illinois Open Source 6718292f2SDouglas Gregor // License. See LICENSE.TXT for details. 7718292f2SDouglas Gregor // 8718292f2SDouglas Gregor //===----------------------------------------------------------------------===// 9718292f2SDouglas Gregor // 10718292f2SDouglas Gregor // This file defines the ModuleMap implementation, which describes the layout 11718292f2SDouglas Gregor // of a module as it relates to headers. 12718292f2SDouglas Gregor // 13718292f2SDouglas Gregor //===----------------------------------------------------------------------===// 14718292f2SDouglas Gregor #include "clang/Lex/ModuleMap.h" 15a7d03840SJordan Rose #include "clang/Basic/CharInfo.h" 16718292f2SDouglas Gregor #include "clang/Basic/Diagnostic.h" 17811db4eaSDouglas Gregor #include "clang/Basic/DiagnosticOptions.h" 18718292f2SDouglas Gregor #include "clang/Basic/FileManager.h" 19718292f2SDouglas Gregor #include "clang/Basic/TargetInfo.h" 20718292f2SDouglas Gregor #include "clang/Basic/TargetOptions.h" 21b146baabSArgyrios Kyrtzidis #include "clang/Lex/HeaderSearch.h" 229acb99e3SRichard Smith #include "clang/Lex/HeaderSearchOptions.h" 233a02247dSChandler Carruth #include "clang/Lex/LexDiagnostic.h" 243a02247dSChandler Carruth #include "clang/Lex/Lexer.h" 253a02247dSChandler Carruth #include "clang/Lex/LiteralSupport.h" 263a02247dSChandler Carruth #include "llvm/ADT/StringRef.h" 273a02247dSChandler Carruth #include "llvm/ADT/StringSwitch.h" 28718292f2SDouglas Gregor #include "llvm/Support/Allocator.h" 29e89dbc1dSDouglas Gregor #include "llvm/Support/FileSystem.h" 30718292f2SDouglas Gregor #include "llvm/Support/Host.h" 31552c169eSRafael Espindola #include "llvm/Support/Path.h" 32718292f2SDouglas Gregor #include "llvm/Support/raw_ostream.h" 3307c22b78SDouglas Gregor #include <stdlib.h> 3401c7cfa2SDouglas Gregor #if defined(LLVM_ON_UNIX) 35eadae014SDmitri Gribenko #include <limits.h> 3601c7cfa2SDouglas Gregor #endif 37718292f2SDouglas Gregor using namespace clang; 38718292f2SDouglas Gregor 392b82c2a5SDouglas Gregor Module::ExportDecl 402b82c2a5SDouglas Gregor ModuleMap::resolveExport(Module *Mod, 412b82c2a5SDouglas Gregor const Module::UnresolvedExportDecl &Unresolved, 42e4412640SArgyrios Kyrtzidis bool Complain) const { 43f5eedd05SDouglas Gregor // We may have just a wildcard. 44f5eedd05SDouglas Gregor if (Unresolved.Id.empty()) { 45f5eedd05SDouglas Gregor assert(Unresolved.Wildcard && "Invalid unresolved export"); 46d2d442caSCraig Topper return Module::ExportDecl(nullptr, true); 47f5eedd05SDouglas Gregor } 48f5eedd05SDouglas Gregor 49fb912657SDouglas Gregor // Resolve the module-id. 50fb912657SDouglas Gregor Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain); 51fb912657SDouglas Gregor if (!Context) 52fb912657SDouglas Gregor return Module::ExportDecl(); 53fb912657SDouglas Gregor 54fb912657SDouglas Gregor return Module::ExportDecl(Context, Unresolved.Wildcard); 55fb912657SDouglas Gregor } 56fb912657SDouglas Gregor 57fb912657SDouglas Gregor Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod, 58fb912657SDouglas Gregor bool Complain) const { 592b82c2a5SDouglas Gregor // Find the starting module. 60fb912657SDouglas Gregor Module *Context = lookupModuleUnqualified(Id[0].first, Mod); 612b82c2a5SDouglas Gregor if (!Context) { 622b82c2a5SDouglas Gregor if (Complain) 630761a8a0SDaniel Jasper Diags.Report(Id[0].second, diag::err_mmap_missing_module_unqualified) 64fb912657SDouglas Gregor << Id[0].first << Mod->getFullModuleName(); 652b82c2a5SDouglas Gregor 66d2d442caSCraig Topper return nullptr; 672b82c2a5SDouglas Gregor } 682b82c2a5SDouglas Gregor 692b82c2a5SDouglas Gregor // Dig into the module path. 70fb912657SDouglas Gregor for (unsigned I = 1, N = Id.size(); I != N; ++I) { 71fb912657SDouglas Gregor Module *Sub = lookupModuleQualified(Id[I].first, Context); 722b82c2a5SDouglas Gregor if (!Sub) { 732b82c2a5SDouglas Gregor if (Complain) 740761a8a0SDaniel Jasper Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 75fb912657SDouglas Gregor << Id[I].first << Context->getFullModuleName() 76fb912657SDouglas Gregor << SourceRange(Id[0].second, Id[I-1].second); 772b82c2a5SDouglas Gregor 78d2d442caSCraig Topper return nullptr; 792b82c2a5SDouglas Gregor } 802b82c2a5SDouglas Gregor 812b82c2a5SDouglas Gregor Context = Sub; 822b82c2a5SDouglas Gregor } 832b82c2a5SDouglas Gregor 84fb912657SDouglas Gregor return Context; 852b82c2a5SDouglas Gregor } 862b82c2a5SDouglas Gregor 870761a8a0SDaniel Jasper ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags, 88b146baabSArgyrios Kyrtzidis const LangOptions &LangOpts, const TargetInfo *Target, 89b146baabSArgyrios Kyrtzidis HeaderSearch &HeaderInfo) 900761a8a0SDaniel Jasper : SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target), 91d2d442caSCraig Topper HeaderInfo(HeaderInfo), BuiltinIncludeDir(nullptr), 92a7e2cc68SRichard Smith CompilingModule(nullptr), SourceModule(nullptr), NumCreatedModules(0) { 930414b857SRichard Smith MMapLangOpts.LineComment = true; 940414b857SRichard Smith } 95718292f2SDouglas Gregor 96718292f2SDouglas Gregor ModuleMap::~ModuleMap() { 975acdf59eSDouglas Gregor for (llvm::StringMap<Module *>::iterator I = Modules.begin(), 985acdf59eSDouglas Gregor IEnd = Modules.end(); 995acdf59eSDouglas Gregor I != IEnd; ++I) { 1005acdf59eSDouglas Gregor delete I->getValue(); 1015acdf59eSDouglas Gregor } 102718292f2SDouglas Gregor } 103718292f2SDouglas Gregor 10489929282SDouglas Gregor void ModuleMap::setTarget(const TargetInfo &Target) { 10589929282SDouglas Gregor assert((!this->Target || this->Target == &Target) && 10689929282SDouglas Gregor "Improper target override"); 10789929282SDouglas Gregor this->Target = &Target; 10889929282SDouglas Gregor } 10989929282SDouglas Gregor 110056396aeSDouglas Gregor /// \brief "Sanitize" a filename so that it can be used as an identifier. 111056396aeSDouglas Gregor static StringRef sanitizeFilenameAsIdentifier(StringRef Name, 112056396aeSDouglas Gregor SmallVectorImpl<char> &Buffer) { 113056396aeSDouglas Gregor if (Name.empty()) 114056396aeSDouglas Gregor return Name; 115056396aeSDouglas Gregor 116a7d03840SJordan Rose if (!isValidIdentifier(Name)) { 117056396aeSDouglas Gregor // If we don't already have something with the form of an identifier, 118056396aeSDouglas Gregor // create a buffer with the sanitized name. 119056396aeSDouglas Gregor Buffer.clear(); 120a7d03840SJordan Rose if (isDigit(Name[0])) 121056396aeSDouglas Gregor Buffer.push_back('_'); 122056396aeSDouglas Gregor Buffer.reserve(Buffer.size() + Name.size()); 123056396aeSDouglas Gregor for (unsigned I = 0, N = Name.size(); I != N; ++I) { 124a7d03840SJordan Rose if (isIdentifierBody(Name[I])) 125056396aeSDouglas Gregor Buffer.push_back(Name[I]); 126056396aeSDouglas Gregor else 127056396aeSDouglas Gregor Buffer.push_back('_'); 128056396aeSDouglas Gregor } 129056396aeSDouglas Gregor 130056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 131056396aeSDouglas Gregor } 132056396aeSDouglas Gregor 133056396aeSDouglas Gregor while (llvm::StringSwitch<bool>(Name) 134056396aeSDouglas Gregor #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true) 135056396aeSDouglas Gregor #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true) 136056396aeSDouglas Gregor #include "clang/Basic/TokenKinds.def" 137056396aeSDouglas Gregor .Default(false)) { 138056396aeSDouglas Gregor if (Name.data() != Buffer.data()) 139056396aeSDouglas Gregor Buffer.append(Name.begin(), Name.end()); 140056396aeSDouglas Gregor Buffer.push_back('_'); 141056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 142056396aeSDouglas Gregor } 143056396aeSDouglas Gregor 144056396aeSDouglas Gregor return Name; 145056396aeSDouglas Gregor } 146056396aeSDouglas Gregor 14734d52749SDouglas Gregor /// \brief Determine whether the given file name is the name of a builtin 14834d52749SDouglas Gregor /// header, supplied by Clang to replace, override, or augment existing system 14934d52749SDouglas Gregor /// headers. 15034d52749SDouglas Gregor static bool isBuiltinHeader(StringRef FileName) { 15134d52749SDouglas Gregor return llvm::StringSwitch<bool>(FileName) 15234d52749SDouglas Gregor .Case("float.h", true) 15334d52749SDouglas Gregor .Case("iso646.h", true) 15434d52749SDouglas Gregor .Case("limits.h", true) 15534d52749SDouglas Gregor .Case("stdalign.h", true) 15634d52749SDouglas Gregor .Case("stdarg.h", true) 15734d52749SDouglas Gregor .Case("stdbool.h", true) 15834d52749SDouglas Gregor .Case("stddef.h", true) 15934d52749SDouglas Gregor .Case("stdint.h", true) 16034d52749SDouglas Gregor .Case("tgmath.h", true) 16134d52749SDouglas Gregor .Case("unwind.h", true) 16234d52749SDouglas Gregor .Default(false); 16334d52749SDouglas Gregor } 16434d52749SDouglas Gregor 16592669ee4SDaniel Jasper ModuleMap::HeadersMap::iterator 16692669ee4SDaniel Jasper ModuleMap::findKnownHeader(const FileEntry *File) { 16759527666SDouglas Gregor HeadersMap::iterator Known = Headers.find(File); 16847972afdSRichard Smith if (HeaderInfo.getHeaderSearchOpts().ImplicitModuleMaps && 16947972afdSRichard Smith Known == Headers.end() && File->getDir() == BuiltinIncludeDir && 1704eaf0a6cSDaniel Jasper isBuiltinHeader(llvm::sys::path::filename(File->getName()))) { 1714eaf0a6cSDaniel Jasper HeaderInfo.loadTopLevelSystemModules(); 17292669ee4SDaniel Jasper return Headers.find(File); 1734eaf0a6cSDaniel Jasper } 17492669ee4SDaniel Jasper return Known; 17592669ee4SDaniel Jasper } 17692669ee4SDaniel Jasper 1774469138eSBen Langmuir ModuleMap::KnownHeader 1784469138eSBen Langmuir ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File, 1794469138eSBen Langmuir SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs) { 18047972afdSRichard Smith if (UmbrellaDirs.empty()) 18147972afdSRichard Smith return KnownHeader(); 18247972afdSRichard Smith 1834469138eSBen Langmuir const DirectoryEntry *Dir = File->getDir(); 1844469138eSBen Langmuir assert(Dir && "file in no directory"); 1854469138eSBen Langmuir 1864469138eSBen Langmuir // Note: as an egregious but useful hack we use the real path here, because 1874469138eSBen Langmuir // frameworks moving from top-level frameworks to embedded frameworks tend 1884469138eSBen Langmuir // to be symlinked from the top-level location to the embedded location, 1894469138eSBen Langmuir // and we need to resolve lookups as if we had found the embedded location. 1904469138eSBen Langmuir StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir); 1914469138eSBen Langmuir 1924469138eSBen Langmuir // Keep walking up the directory hierarchy, looking for a directory with 1934469138eSBen Langmuir // an umbrella header. 1944469138eSBen Langmuir do { 1954469138eSBen Langmuir auto KnownDir = UmbrellaDirs.find(Dir); 1964469138eSBen Langmuir if (KnownDir != UmbrellaDirs.end()) 1974469138eSBen Langmuir return KnownHeader(KnownDir->second, NormalHeader); 1984469138eSBen Langmuir 1994469138eSBen Langmuir IntermediateDirs.push_back(Dir); 2004469138eSBen Langmuir 2014469138eSBen Langmuir // Retrieve our parent path. 2024469138eSBen Langmuir DirName = llvm::sys::path::parent_path(DirName); 2034469138eSBen Langmuir if (DirName.empty()) 2044469138eSBen Langmuir break; 2054469138eSBen Langmuir 2064469138eSBen Langmuir // Resolve the parent path to a directory entry. 2074469138eSBen Langmuir Dir = SourceMgr.getFileManager().getDirectory(DirName); 2084469138eSBen Langmuir } while (Dir); 2094469138eSBen Langmuir return KnownHeader(); 2104469138eSBen Langmuir } 2114469138eSBen Langmuir 21292669ee4SDaniel Jasper static bool violatesPrivateInclude(Module *RequestingModule, 21392669ee4SDaniel Jasper const FileEntry *IncFileEnt, 21492669ee4SDaniel Jasper ModuleMap::ModuleHeaderRole Role, 21592669ee4SDaniel Jasper Module *RequestedModule) { 216202210b3SRichard Smith bool IsPrivateRole = Role & ModuleMap::PrivateHeader; 21792669ee4SDaniel Jasper #ifndef NDEBUG 2182708e520SRichard Smith if (IsPrivateRole) { 21992669ee4SDaniel Jasper // Check for consistency between the module header role 22092669ee4SDaniel Jasper // as obtained from the lookup and as obtained from the module. 22192669ee4SDaniel Jasper // This check is not cheap, so enable it only for debugging. 2222708e520SRichard Smith bool IsPrivate = false; 2232708e520SRichard Smith SmallVectorImpl<Module::Header> *HeaderList[] = { 2242708e520SRichard Smith &RequestedModule->Headers[Module::HK_Private], 2252708e520SRichard Smith &RequestedModule->Headers[Module::HK_PrivateTextual]}; 2262708e520SRichard Smith for (auto *Hs : HeaderList) 2272708e520SRichard Smith IsPrivate |= 2282708e520SRichard Smith std::find_if(Hs->begin(), Hs->end(), [&](const Module::Header &H) { 2293c1a41adSRichard Smith return H.Entry == IncFileEnt; 2302708e520SRichard Smith }) != Hs->end(); 2312708e520SRichard Smith assert((!IsPrivateRole || IsPrivate) && "inconsistent headers and roles"); 23200bc95ecSRichard Smith } 23392669ee4SDaniel Jasper #endif 234202210b3SRichard Smith return IsPrivateRole && 2358f4d3ff1SRichard Smith // FIXME: Should we map RequestingModule to its top-level module here 2368f4d3ff1SRichard Smith // too? This check is redundant with the isSubModuleOf check in 2378f4d3ff1SRichard Smith // diagnoseHeaderInclusion. 23892669ee4SDaniel Jasper RequestedModule->getTopLevelModule() != RequestingModule; 23992669ee4SDaniel Jasper } 24092669ee4SDaniel Jasper 24171e1a64fSBen Langmuir static Module *getTopLevelOrNull(Module *M) { 24271e1a64fSBen Langmuir return M ? M->getTopLevelModule() : nullptr; 24371e1a64fSBen Langmuir } 24471e1a64fSBen Langmuir 24592669ee4SDaniel Jasper void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule, 24692669ee4SDaniel Jasper SourceLocation FilenameLoc, 24792669ee4SDaniel Jasper StringRef Filename, 24892669ee4SDaniel Jasper const FileEntry *File) { 24992669ee4SDaniel Jasper // No errors for indirect modules. This may be a bit of a problem for modules 25092669ee4SDaniel Jasper // with no source files. 25171e1a64fSBen Langmuir if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule)) 25292669ee4SDaniel Jasper return; 25392669ee4SDaniel Jasper 25492669ee4SDaniel Jasper if (RequestingModule) 25592669ee4SDaniel Jasper resolveUses(RequestingModule, /*Complain=*/false); 25692669ee4SDaniel Jasper 25771e1a64fSBen Langmuir bool Excluded = false; 258d2d442caSCraig Topper Module *Private = nullptr; 259d2d442caSCraig Topper Module *NotUsed = nullptr; 26071e1a64fSBen Langmuir 26171e1a64fSBen Langmuir HeadersMap::iterator Known = findKnownHeader(File); 26271e1a64fSBen Langmuir if (Known != Headers.end()) { 26371e1a64fSBen Langmuir for (const KnownHeader &Header : Known->second) { 26492669ee4SDaniel Jasper // If 'File' is part of 'RequestingModule' we can definitely include it. 2650ab544f1SDaniel Jasper if (Header.getModule() && 2660ab544f1SDaniel Jasper Header.getModule()->isSubModuleOf(RequestingModule)) 26792669ee4SDaniel Jasper return; 26892669ee4SDaniel Jasper 26992669ee4SDaniel Jasper // Remember private headers for later printing of a diagnostic. 27071e1a64fSBen Langmuir if (violatesPrivateInclude(RequestingModule, File, Header.getRole(), 27171e1a64fSBen Langmuir Header.getModule())) { 27271e1a64fSBen Langmuir Private = Header.getModule(); 27392669ee4SDaniel Jasper continue; 27492669ee4SDaniel Jasper } 27592669ee4SDaniel Jasper 27692669ee4SDaniel Jasper // If uses need to be specified explicitly, we are only allowed to return 27792669ee4SDaniel Jasper // modules that are explicitly used by the requesting module. 27892669ee4SDaniel Jasper if (RequestingModule && LangOpts.ModulesDeclUse && 2798f4d3ff1SRichard Smith !RequestingModule->directlyUses(Header.getModule())) { 28071e1a64fSBen Langmuir NotUsed = Header.getModule(); 28192669ee4SDaniel Jasper continue; 28292669ee4SDaniel Jasper } 28392669ee4SDaniel Jasper 28492669ee4SDaniel Jasper // We have found a module that we can happily use. 28592669ee4SDaniel Jasper return; 28692669ee4SDaniel Jasper } 287feb54b6dSRichard Smith 288feb54b6dSRichard Smith Excluded = true; 28971e1a64fSBen Langmuir } 29092669ee4SDaniel Jasper 29192669ee4SDaniel Jasper // We have found a header, but it is private. 292d2d442caSCraig Topper if (Private) { 29311152dd5SRichard Smith Diags.Report(FilenameLoc, diag::warn_use_of_private_header_outside_module) 29492669ee4SDaniel Jasper << Filename; 29592669ee4SDaniel Jasper return; 29692669ee4SDaniel Jasper } 29792669ee4SDaniel Jasper 29892669ee4SDaniel Jasper // We have found a module, but we don't use it. 299d2d442caSCraig Topper if (NotUsed) { 30011152dd5SRichard Smith Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module) 30192669ee4SDaniel Jasper << RequestingModule->getFullModuleName() << Filename; 30292669ee4SDaniel Jasper return; 30392669ee4SDaniel Jasper } 30492669ee4SDaniel Jasper 30571e1a64fSBen Langmuir if (Excluded || isHeaderInUmbrellaDirs(File)) 30671e1a64fSBen Langmuir return; 30771e1a64fSBen Langmuir 30871e1a64fSBen Langmuir // At this point, only non-modular includes remain. 30971e1a64fSBen Langmuir 31071e1a64fSBen Langmuir if (LangOpts.ModulesStrictDeclUse) { 31111152dd5SRichard Smith Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module) 31271e1a64fSBen Langmuir << RequestingModule->getFullModuleName() << Filename; 31371e1a64fSBen Langmuir } else if (RequestingModule) { 31471e1a64fSBen Langmuir diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ? 31571e1a64fSBen Langmuir diag::warn_non_modular_include_in_framework_module : 31671e1a64fSBen Langmuir diag::warn_non_modular_include_in_module; 31771e1a64fSBen Langmuir Diags.Report(FilenameLoc, DiagID) << RequestingModule->getFullModuleName(); 31871e1a64fSBen Langmuir } 31992669ee4SDaniel Jasper } 32092669ee4SDaniel Jasper 321ec87a50aSRichard Smith static bool isBetterKnownHeader(const ModuleMap::KnownHeader &New, 322ec87a50aSRichard Smith const ModuleMap::KnownHeader &Old) { 323ec87a50aSRichard Smith // Prefer a public header over a private header. 324ec87a50aSRichard Smith if ((New.getRole() & ModuleMap::PrivateHeader) != 325ec87a50aSRichard Smith (Old.getRole() & ModuleMap::PrivateHeader)) 326ec87a50aSRichard Smith return !(New.getRole() & ModuleMap::PrivateHeader); 327ec87a50aSRichard Smith 328ec87a50aSRichard Smith // Prefer a non-textual header over a textual header. 329ec87a50aSRichard Smith if ((New.getRole() & ModuleMap::TextualHeader) != 330ec87a50aSRichard Smith (Old.getRole() & ModuleMap::TextualHeader)) 331ec87a50aSRichard Smith return !(New.getRole() & ModuleMap::TextualHeader); 332ec87a50aSRichard Smith 333ec87a50aSRichard Smith // Don't have a reason to choose between these. Just keep the first one. 334ec87a50aSRichard Smith return false; 335ec87a50aSRichard Smith } 336ec87a50aSRichard Smith 3374881e8b2SSean Silva ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File) { 338306d8920SRichard Smith auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader { 3398230e5eeSSean Silva if (R.getRole() & ModuleMap::TextualHeader) 340306d8920SRichard Smith return ModuleMap::KnownHeader(); 341306d8920SRichard Smith return R; 342306d8920SRichard Smith }; 343306d8920SRichard Smith 3444881e8b2SSean Silva HeadersMap::iterator Known = findKnownHeader(File); 3451fb5c3a6SDouglas Gregor if (Known != Headers.end()) { 346202210b3SRichard Smith ModuleMap::KnownHeader Result; 34797da9178SDaniel Jasper // Iterate over all modules that 'File' is part of to find the best fit. 3484881e8b2SSean Silva for (KnownHeader &H : Known->second) { 3492f633e7cSRichard Smith // Prefer a header from the current module over all others. 3508692a4d1SRichard Smith if (H.getModule()->getTopLevelModule() == CompilingModule) 3512f633e7cSRichard Smith return MakeResult(H); 3524eaf0a6cSDaniel Jasper // Cannot use a module if it is unavailable. 3534881e8b2SSean Silva if (!H.getModule()->isAvailable()) 35497da9178SDaniel Jasper continue; 3554881e8b2SSean Silva if (!Result || isBetterKnownHeader(H, Result)) 3564881e8b2SSean Silva Result = H; 35797da9178SDaniel Jasper } 358306d8920SRichard Smith return MakeResult(Result); 3591fb5c3a6SDouglas Gregor } 360ab0c8a84SDouglas Gregor 361f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 3624469138eSBen Langmuir KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs); 3634469138eSBen Langmuir if (H) { 3644469138eSBen Langmuir Module *Result = H.getModule(); 365930a85ccSDouglas Gregor 366930a85ccSDouglas Gregor // Search up the module stack until we find a module with an umbrella 36773141fa9SDouglas Gregor // directory. 368930a85ccSDouglas Gregor Module *UmbrellaModule = Result; 36973141fa9SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 370930a85ccSDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 371930a85ccSDouglas Gregor 372930a85ccSDouglas Gregor if (UmbrellaModule->InferSubmodules) { 3739d6448b1SBen Langmuir const FileEntry *UmbrellaModuleMap = 3749d6448b1SBen Langmuir getModuleMapFileForUniquing(UmbrellaModule); 3759d6448b1SBen Langmuir 376a89c5ac4SDouglas Gregor // Infer submodules for each of the directories we found between 377a89c5ac4SDouglas Gregor // the directory of the umbrella header and the directory where 378a89c5ac4SDouglas Gregor // the actual header is located. 3799458f82dSDouglas Gregor bool Explicit = UmbrellaModule->InferExplicitSubmodules; 3809458f82dSDouglas Gregor 3817033127bSDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 382a89c5ac4SDouglas Gregor // Find or create the module that corresponds to this directory name. 383056396aeSDouglas Gregor SmallString<32> NameBuf; 384056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 3854469138eSBen Langmuir llvm::sys::path::stem(SkippedDirs[I-1]->getName()), NameBuf); 3869d6448b1SBen Langmuir Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 3879d6448b1SBen Langmuir Explicit).first; 3889d6448b1SBen Langmuir InferredModuleAllowedBy[Result] = UmbrellaModuleMap; 389ffbafa2aSBen Langmuir Result->IsInferred = true; 390a89c5ac4SDouglas Gregor 391a89c5ac4SDouglas Gregor // Associate the module and the directory. 392a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I-1]] = Result; 393a89c5ac4SDouglas Gregor 394a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 395a89c5ac4SDouglas Gregor // wildcard to the set of exports. 396930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 397d2d442caSCraig Topper Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 398a89c5ac4SDouglas Gregor } 399a89c5ac4SDouglas Gregor 400a89c5ac4SDouglas Gregor // Infer a submodule with the same name as this header file. 401056396aeSDouglas Gregor SmallString<32> NameBuf; 402056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 403056396aeSDouglas Gregor llvm::sys::path::stem(File->getName()), NameBuf); 4049d6448b1SBen Langmuir Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 4059d6448b1SBen Langmuir Explicit).first; 4069d6448b1SBen Langmuir InferredModuleAllowedBy[Result] = UmbrellaModuleMap; 407ffbafa2aSBen Langmuir Result->IsInferred = true; 4083c5305c1SArgyrios Kyrtzidis Result->addTopHeader(File); 409a89c5ac4SDouglas Gregor 410a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 411a89c5ac4SDouglas Gregor // wildcard to the set of exports. 412930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 413d2d442caSCraig Topper Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 414a89c5ac4SDouglas Gregor } else { 415a89c5ac4SDouglas Gregor // Record each of the directories we stepped through as being part of 416a89c5ac4SDouglas Gregor // the module we found, since the umbrella header covers them all. 417a89c5ac4SDouglas Gregor for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 418a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I]] = Result; 419a89c5ac4SDouglas Gregor } 420a89c5ac4SDouglas Gregor 42197da9178SDaniel Jasper Headers[File].push_back(KnownHeader(Result, NormalHeader)); 4221fb5c3a6SDouglas Gregor 4231fb5c3a6SDouglas Gregor // If a header corresponds to an unavailable module, don't report 4241fb5c3a6SDouglas Gregor // that it maps to anything. 4251fb5c3a6SDouglas Gregor if (!Result->isAvailable()) 426b53e5483SLawrence Crowl return KnownHeader(); 4271fb5c3a6SDouglas Gregor 428306d8920SRichard Smith return MakeResult(Headers[File].back()); 429a89c5ac4SDouglas Gregor } 430a89c5ac4SDouglas Gregor 431b53e5483SLawrence Crowl return KnownHeader(); 432ab0c8a84SDouglas Gregor } 433ab0c8a84SDouglas Gregor 434e4412640SArgyrios Kyrtzidis bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const { 435d2d442caSCraig Topper return isHeaderUnavailableInModule(Header, nullptr); 43650996ce1SRichard Smith } 43750996ce1SRichard Smith 43862bcd925SDmitri Gribenko bool 43962bcd925SDmitri Gribenko ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header, 44062bcd925SDmitri Gribenko const Module *RequestingModule) const { 441e4412640SArgyrios Kyrtzidis HeadersMap::const_iterator Known = Headers.find(Header); 44297da9178SDaniel Jasper if (Known != Headers.end()) { 44397da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::const_iterator 44497da9178SDaniel Jasper I = Known->second.begin(), 44597da9178SDaniel Jasper E = Known->second.end(); 44697da9178SDaniel Jasper I != E; ++I) { 44750996ce1SRichard Smith if (I->isAvailable() && (!RequestingModule || 44850996ce1SRichard Smith I->getModule()->isSubModuleOf(RequestingModule))) 44997da9178SDaniel Jasper return false; 45097da9178SDaniel Jasper } 45197da9178SDaniel Jasper return true; 45297da9178SDaniel Jasper } 4531fb5c3a6SDouglas Gregor 4541fb5c3a6SDouglas Gregor const DirectoryEntry *Dir = Header->getDir(); 455f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 4561fb5c3a6SDouglas Gregor StringRef DirName = Dir->getName(); 4571fb5c3a6SDouglas Gregor 45850996ce1SRichard Smith auto IsUnavailable = [&](const Module *M) { 45950996ce1SRichard Smith return !M->isAvailable() && (!RequestingModule || 46050996ce1SRichard Smith M->isSubModuleOf(RequestingModule)); 46150996ce1SRichard Smith }; 46250996ce1SRichard Smith 4631fb5c3a6SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 4641fb5c3a6SDouglas Gregor // an umbrella header. 4651fb5c3a6SDouglas Gregor do { 466e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir 4671fb5c3a6SDouglas Gregor = UmbrellaDirs.find(Dir); 4681fb5c3a6SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 4691fb5c3a6SDouglas Gregor Module *Found = KnownDir->second; 47050996ce1SRichard Smith if (IsUnavailable(Found)) 4711fb5c3a6SDouglas Gregor return true; 4721fb5c3a6SDouglas Gregor 4731fb5c3a6SDouglas Gregor // Search up the module stack until we find a module with an umbrella 4741fb5c3a6SDouglas Gregor // directory. 4751fb5c3a6SDouglas Gregor Module *UmbrellaModule = Found; 4761fb5c3a6SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 4771fb5c3a6SDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 4781fb5c3a6SDouglas Gregor 4791fb5c3a6SDouglas Gregor if (UmbrellaModule->InferSubmodules) { 4801fb5c3a6SDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 4811fb5c3a6SDouglas Gregor // Find or create the module that corresponds to this directory name. 482056396aeSDouglas Gregor SmallString<32> NameBuf; 483056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 484056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 485056396aeSDouglas Gregor NameBuf); 4861fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 4871fb5c3a6SDouglas Gregor if (!Found) 4881fb5c3a6SDouglas Gregor return false; 48950996ce1SRichard Smith if (IsUnavailable(Found)) 4901fb5c3a6SDouglas Gregor return true; 4911fb5c3a6SDouglas Gregor } 4921fb5c3a6SDouglas Gregor 4931fb5c3a6SDouglas Gregor // Infer a submodule with the same name as this header file. 494056396aeSDouglas Gregor SmallString<32> NameBuf; 495056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 496056396aeSDouglas Gregor llvm::sys::path::stem(Header->getName()), 497056396aeSDouglas Gregor NameBuf); 4981fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 4991fb5c3a6SDouglas Gregor if (!Found) 5001fb5c3a6SDouglas Gregor return false; 5011fb5c3a6SDouglas Gregor } 5021fb5c3a6SDouglas Gregor 50350996ce1SRichard Smith return IsUnavailable(Found); 5041fb5c3a6SDouglas Gregor } 5051fb5c3a6SDouglas Gregor 5061fb5c3a6SDouglas Gregor SkippedDirs.push_back(Dir); 5071fb5c3a6SDouglas Gregor 5081fb5c3a6SDouglas Gregor // Retrieve our parent path. 5091fb5c3a6SDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 5101fb5c3a6SDouglas Gregor if (DirName.empty()) 5111fb5c3a6SDouglas Gregor break; 5121fb5c3a6SDouglas Gregor 5131fb5c3a6SDouglas Gregor // Resolve the parent path to a directory entry. 5141f76c4e8SManuel Klimek Dir = SourceMgr.getFileManager().getDirectory(DirName); 5151fb5c3a6SDouglas Gregor } while (Dir); 5161fb5c3a6SDouglas Gregor 5171fb5c3a6SDouglas Gregor return false; 5181fb5c3a6SDouglas Gregor } 5191fb5c3a6SDouglas Gregor 520e4412640SArgyrios Kyrtzidis Module *ModuleMap::findModule(StringRef Name) const { 521e4412640SArgyrios Kyrtzidis llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name); 52288bdfb0eSDouglas Gregor if (Known != Modules.end()) 52388bdfb0eSDouglas Gregor return Known->getValue(); 52488bdfb0eSDouglas Gregor 525d2d442caSCraig Topper return nullptr; 52688bdfb0eSDouglas Gregor } 52788bdfb0eSDouglas Gregor 528e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleUnqualified(StringRef Name, 529e4412640SArgyrios Kyrtzidis Module *Context) const { 5302b82c2a5SDouglas Gregor for(; Context; Context = Context->Parent) { 5312b82c2a5SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Context)) 5322b82c2a5SDouglas Gregor return Sub; 5332b82c2a5SDouglas Gregor } 5342b82c2a5SDouglas Gregor 5352b82c2a5SDouglas Gregor return findModule(Name); 5362b82c2a5SDouglas Gregor } 5372b82c2a5SDouglas Gregor 538e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{ 5392b82c2a5SDouglas Gregor if (!Context) 5402b82c2a5SDouglas Gregor return findModule(Name); 5412b82c2a5SDouglas Gregor 542eb90e830SDouglas Gregor return Context->findSubmodule(Name); 5432b82c2a5SDouglas Gregor } 5442b82c2a5SDouglas Gregor 545de3ef502SDouglas Gregor std::pair<Module *, bool> 5469d6448b1SBen Langmuir ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 54769021974SDouglas Gregor bool IsExplicit) { 54869021974SDouglas Gregor // Try to find an existing module with this name. 549eb90e830SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Parent)) 550eb90e830SDouglas Gregor return std::make_pair(Sub, false); 55169021974SDouglas Gregor 55269021974SDouglas Gregor // Create a new module with this name. 5539d6448b1SBen Langmuir Module *Result = new Module(Name, SourceLocation(), Parent, 554a7e2cc68SRichard Smith IsFramework, IsExplicit, NumCreatedModules++); 555ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == Name) { 556ba7f2f71SDaniel Jasper SourceModule = Result; 557ba7f2f71SDaniel Jasper SourceModuleName = Name; 558ba7f2f71SDaniel Jasper } 5596f722b4eSArgyrios Kyrtzidis if (!Parent) { 56069021974SDouglas Gregor Modules[Name] = Result; 5616f722b4eSArgyrios Kyrtzidis if (!LangOpts.CurrentModule.empty() && !CompilingModule && 5626f722b4eSArgyrios Kyrtzidis Name == LangOpts.CurrentModule) { 5636f722b4eSArgyrios Kyrtzidis CompilingModule = Result; 5646f722b4eSArgyrios Kyrtzidis } 5656f722b4eSArgyrios Kyrtzidis } 56669021974SDouglas Gregor return std::make_pair(Result, true); 56769021974SDouglas Gregor } 56869021974SDouglas Gregor 56911dfe6feSDouglas Gregor /// \brief For a framework module, infer the framework against which we 57011dfe6feSDouglas Gregor /// should link. 57111dfe6feSDouglas Gregor static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir, 57211dfe6feSDouglas Gregor FileManager &FileMgr) { 57311dfe6feSDouglas Gregor assert(Mod->IsFramework && "Can only infer linking for framework modules"); 57411dfe6feSDouglas Gregor assert(!Mod->isSubFramework() && 57511dfe6feSDouglas Gregor "Can only infer linking for top-level frameworks"); 57611dfe6feSDouglas Gregor 57711dfe6feSDouglas Gregor SmallString<128> LibName; 57811dfe6feSDouglas Gregor LibName += FrameworkDir->getName(); 57911dfe6feSDouglas Gregor llvm::sys::path::append(LibName, Mod->Name); 58011dfe6feSDouglas Gregor if (FileMgr.getFile(LibName)) { 58111dfe6feSDouglas Gregor Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name, 58211dfe6feSDouglas Gregor /*IsFramework=*/true)); 58311dfe6feSDouglas Gregor } 58411dfe6feSDouglas Gregor } 58511dfe6feSDouglas Gregor 586a525400dSBen Langmuir Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir, 587a525400dSBen Langmuir bool IsSystem, Module *Parent) { 588c1d88ea5SBen Langmuir Attributes Attrs; 589c1d88ea5SBen Langmuir Attrs.IsSystem = IsSystem; 590a525400dSBen Langmuir return inferFrameworkModule(FrameworkDir, Attrs, Parent); 591c1d88ea5SBen Langmuir } 592c1d88ea5SBen Langmuir 593a525400dSBen Langmuir Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir, 594c1d88ea5SBen Langmuir Attributes Attrs, Module *Parent) { 595a525400dSBen Langmuir // Note: as an egregious but useful hack we use the real path here, because 596a525400dSBen Langmuir // we might be looking at an embedded framework that symlinks out to a 597a525400dSBen Langmuir // top-level framework, and we need to infer as if we were naming the 598a525400dSBen Langmuir // top-level framework. 599a525400dSBen Langmuir StringRef FrameworkDirName = 600a525400dSBen Langmuir SourceMgr.getFileManager().getCanonicalName(FrameworkDir); 601a525400dSBen Langmuir 602a525400dSBen Langmuir // In case this is a case-insensitive filesystem, use the canonical 603a525400dSBen Langmuir // directory name as the ModuleName, since modules are case-sensitive. 604a525400dSBen Langmuir // FIXME: we should be able to give a fix-it hint for the correct spelling. 605a525400dSBen Langmuir SmallString<32> ModuleNameStorage; 606a525400dSBen Langmuir StringRef ModuleName = sanitizeFilenameAsIdentifier( 607a525400dSBen Langmuir llvm::sys::path::stem(FrameworkDirName), ModuleNameStorage); 608c1d88ea5SBen Langmuir 60956c64013SDouglas Gregor // Check whether we've already found this module. 610e89dbc1dSDouglas Gregor if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 611e89dbc1dSDouglas Gregor return Mod; 612e89dbc1dSDouglas Gregor 6131f76c4e8SManuel Klimek FileManager &FileMgr = SourceMgr.getFileManager(); 61456c64013SDouglas Gregor 6159194a91dSDouglas Gregor // If the framework has a parent path from which we're allowed to infer 6169194a91dSDouglas Gregor // a framework module, do so. 617beee15e7SBen Langmuir const FileEntry *ModuleMapFile = nullptr; 6189194a91dSDouglas Gregor if (!Parent) { 6194ddf2221SDouglas Gregor // Determine whether we're allowed to infer a module map. 6209194a91dSDouglas Gregor bool canInfer = false; 6214ddf2221SDouglas Gregor if (llvm::sys::path::has_parent_path(FrameworkDirName)) { 6229194a91dSDouglas Gregor // Figure out the parent path. 6234ddf2221SDouglas Gregor StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); 6249194a91dSDouglas Gregor if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) { 6259194a91dSDouglas Gregor // Check whether we have already looked into the parent directory 6269194a91dSDouglas Gregor // for a module map. 627e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 6289194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 6299194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) { 6309194a91dSDouglas Gregor // We haven't looked here before. Load a module map, if there is 6319194a91dSDouglas Gregor // one. 632984e1df7SBen Langmuir bool IsFrameworkDir = Parent.endswith(".framework"); 633984e1df7SBen Langmuir if (const FileEntry *ModMapFile = 634984e1df7SBen Langmuir HeaderInfo.lookupModuleMapFile(ParentDir, IsFrameworkDir)) { 635c1d88ea5SBen Langmuir parseModuleMapFile(ModMapFile, Attrs.IsSystem, ParentDir); 6369194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 6379194a91dSDouglas Gregor } 6389194a91dSDouglas Gregor 6399194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) 6409194a91dSDouglas Gregor inferred = InferredDirectories.insert( 6419194a91dSDouglas Gregor std::make_pair(ParentDir, InferredDirectory())).first; 6429194a91dSDouglas Gregor } 6439194a91dSDouglas Gregor 6449194a91dSDouglas Gregor if (inferred->second.InferModules) { 6459194a91dSDouglas Gregor // We're allowed to infer for this directory, but make sure it's okay 6469194a91dSDouglas Gregor // to infer this particular module. 6474ddf2221SDouglas Gregor StringRef Name = llvm::sys::path::stem(FrameworkDirName); 6489194a91dSDouglas Gregor canInfer = std::find(inferred->second.ExcludedModules.begin(), 6499194a91dSDouglas Gregor inferred->second.ExcludedModules.end(), 6509194a91dSDouglas Gregor Name) == inferred->second.ExcludedModules.end(); 6519194a91dSDouglas Gregor 652c1d88ea5SBen Langmuir Attrs.IsSystem |= inferred->second.Attrs.IsSystem; 653c1d88ea5SBen Langmuir Attrs.IsExternC |= inferred->second.Attrs.IsExternC; 654c1d88ea5SBen Langmuir Attrs.IsExhaustive |= inferred->second.Attrs.IsExhaustive; 655beee15e7SBen Langmuir ModuleMapFile = inferred->second.ModuleMapFile; 6569194a91dSDouglas Gregor } 6579194a91dSDouglas Gregor } 6589194a91dSDouglas Gregor } 6599194a91dSDouglas Gregor 6609194a91dSDouglas Gregor // If we're not allowed to infer a framework module, don't. 6619194a91dSDouglas Gregor if (!canInfer) 662d2d442caSCraig Topper return nullptr; 663beee15e7SBen Langmuir } else 6649d6448b1SBen Langmuir ModuleMapFile = getModuleMapFileForUniquing(Parent); 6659194a91dSDouglas Gregor 6669194a91dSDouglas Gregor 66756c64013SDouglas Gregor // Look for an umbrella header. 6682c1dd271SDylan Noblesmith SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 66917381a06SBenjamin Kramer llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h"); 670e89dbc1dSDouglas Gregor const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 67156c64013SDouglas Gregor 67256c64013SDouglas Gregor // FIXME: If there's no umbrella header, we could probably scan the 67356c64013SDouglas Gregor // framework to load *everything*. But, it's not clear that this is a good 67456c64013SDouglas Gregor // idea. 67556c64013SDouglas Gregor if (!UmbrellaHeader) 676d2d442caSCraig Topper return nullptr; 67756c64013SDouglas Gregor 6789d6448b1SBen Langmuir Module *Result = new Module(ModuleName, SourceLocation(), Parent, 679a7e2cc68SRichard Smith /*IsFramework=*/true, /*IsExplicit=*/false, 680a7e2cc68SRichard Smith NumCreatedModules++); 6819d6448b1SBen Langmuir InferredModuleAllowedBy[Result] = ModuleMapFile; 6829d6448b1SBen Langmuir Result->IsInferred = true; 683ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == ModuleName) { 684ba7f2f71SDaniel Jasper SourceModule = Result; 685ba7f2f71SDaniel Jasper SourceModuleName = ModuleName; 686ba7f2f71SDaniel Jasper } 687c1d88ea5SBen Langmuir 688c1d88ea5SBen Langmuir Result->IsSystem |= Attrs.IsSystem; 689c1d88ea5SBen Langmuir Result->IsExternC |= Attrs.IsExternC; 690c1d88ea5SBen Langmuir Result->ConfigMacrosExhaustive |= Attrs.IsExhaustive; 6912b63d15fSRichard Smith Result->Directory = FrameworkDir; 692a686e1b0SDouglas Gregor 693eb90e830SDouglas Gregor if (!Parent) 694e89dbc1dSDouglas Gregor Modules[ModuleName] = Result; 695e89dbc1dSDouglas Gregor 696322f633cSDouglas Gregor // umbrella header "umbrella-header-name" 6972b63d15fSRichard Smith // 6982b63d15fSRichard Smith // The "Headers/" component of the name is implied because this is 6992b63d15fSRichard Smith // a framework module. 7002b63d15fSRichard Smith setUmbrellaHeader(Result, UmbrellaHeader, ModuleName + ".h"); 701d8bd7537SDouglas Gregor 702d8bd7537SDouglas Gregor // export * 703d2d442caSCraig Topper Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 704d8bd7537SDouglas Gregor 705a89c5ac4SDouglas Gregor // module * { export * } 706a89c5ac4SDouglas Gregor Result->InferSubmodules = true; 707a89c5ac4SDouglas Gregor Result->InferExportWildcard = true; 708a89c5ac4SDouglas Gregor 709e89dbc1dSDouglas Gregor // Look for subframeworks. 710c080917eSRafael Espindola std::error_code EC; 7112c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirName 712ddaa69cbSDouglas Gregor = StringRef(FrameworkDir->getName()); 713e89dbc1dSDouglas Gregor llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 7142d4d8cb3SBenjamin Kramer llvm::sys::path::native(SubframeworksDirName); 71592e1b62dSYaron Keren for (llvm::sys::fs::directory_iterator Dir(SubframeworksDirName, EC), DirEnd; 716e89dbc1dSDouglas Gregor Dir != DirEnd && !EC; Dir.increment(EC)) { 717e89dbc1dSDouglas Gregor if (!StringRef(Dir->path()).endswith(".framework")) 718e89dbc1dSDouglas Gregor continue; 719f2161a70SDouglas Gregor 720e89dbc1dSDouglas Gregor if (const DirectoryEntry *SubframeworkDir 721e89dbc1dSDouglas Gregor = FileMgr.getDirectory(Dir->path())) { 72207c22b78SDouglas Gregor // Note: as an egregious but useful hack, we use the real path here and 72307c22b78SDouglas Gregor // check whether it is actually a subdirectory of the parent directory. 72407c22b78SDouglas Gregor // This will not be the case if the 'subframework' is actually a symlink 72507c22b78SDouglas Gregor // out to a top-level framework. 726e00c8b20SDouglas Gregor StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir); 72707c22b78SDouglas Gregor bool FoundParent = false; 72807c22b78SDouglas Gregor do { 72907c22b78SDouglas Gregor // Get the parent directory name. 73007c22b78SDouglas Gregor SubframeworkDirName 73107c22b78SDouglas Gregor = llvm::sys::path::parent_path(SubframeworkDirName); 73207c22b78SDouglas Gregor if (SubframeworkDirName.empty()) 73307c22b78SDouglas Gregor break; 73407c22b78SDouglas Gregor 73507c22b78SDouglas Gregor if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { 73607c22b78SDouglas Gregor FoundParent = true; 73707c22b78SDouglas Gregor break; 73807c22b78SDouglas Gregor } 73907c22b78SDouglas Gregor } while (true); 74007c22b78SDouglas Gregor 74107c22b78SDouglas Gregor if (!FoundParent) 74207c22b78SDouglas Gregor continue; 74307c22b78SDouglas Gregor 744e89dbc1dSDouglas Gregor // FIXME: Do we want to warn about subframeworks without umbrella headers? 745a525400dSBen Langmuir inferFrameworkModule(SubframeworkDir, Attrs, Result); 746e89dbc1dSDouglas Gregor } 747e89dbc1dSDouglas Gregor } 748e89dbc1dSDouglas Gregor 74911dfe6feSDouglas Gregor // If the module is a top-level framework, automatically link against the 75011dfe6feSDouglas Gregor // framework. 75111dfe6feSDouglas Gregor if (!Result->isSubFramework()) { 75211dfe6feSDouglas Gregor inferFrameworkLink(Result, FrameworkDir, FileMgr); 75311dfe6feSDouglas Gregor } 75411dfe6feSDouglas Gregor 75556c64013SDouglas Gregor return Result; 75656c64013SDouglas Gregor } 75756c64013SDouglas Gregor 7582b63d15fSRichard Smith void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader, 7592b63d15fSRichard Smith Twine NameAsWritten) { 76097da9178SDaniel Jasper Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader)); 76173141fa9SDouglas Gregor Mod->Umbrella = UmbrellaHeader; 7622b63d15fSRichard Smith Mod->UmbrellaAsWritten = NameAsWritten.str(); 7637033127bSDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; 764a89c5ac4SDouglas Gregor } 765a89c5ac4SDouglas Gregor 7662b63d15fSRichard Smith void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir, 7672b63d15fSRichard Smith Twine NameAsWritten) { 768524e33e1SDouglas Gregor Mod->Umbrella = UmbrellaDir; 7692b63d15fSRichard Smith Mod->UmbrellaAsWritten = NameAsWritten.str(); 770524e33e1SDouglas Gregor UmbrellaDirs[UmbrellaDir] = Mod; 771524e33e1SDouglas Gregor } 772524e33e1SDouglas Gregor 7733c1a41adSRichard Smith static Module::HeaderKind headerRoleToKind(ModuleMap::ModuleHeaderRole Role) { 7740e98d938SNAKAMURA Takumi switch ((int)Role) { 7753c1a41adSRichard Smith default: llvm_unreachable("unknown header role"); 7763c1a41adSRichard Smith case ModuleMap::NormalHeader: 7773c1a41adSRichard Smith return Module::HK_Normal; 7783c1a41adSRichard Smith case ModuleMap::PrivateHeader: 7793c1a41adSRichard Smith return Module::HK_Private; 7803c1a41adSRichard Smith case ModuleMap::TextualHeader: 7813c1a41adSRichard Smith return Module::HK_Textual; 7823c1a41adSRichard Smith case ModuleMap::PrivateHeader | ModuleMap::TextualHeader: 7833c1a41adSRichard Smith return Module::HK_PrivateTextual; 7843c1a41adSRichard Smith } 7850e98d938SNAKAMURA Takumi } 786202210b3SRichard Smith 7873c1a41adSRichard Smith void ModuleMap::addHeader(Module *Mod, Module::Header Header, 7883c1a41adSRichard Smith ModuleHeaderRole Role) { 789202210b3SRichard Smith if (!(Role & TextualHeader)) { 7906f722b4eSArgyrios Kyrtzidis bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule; 7913c1a41adSRichard Smith HeaderInfo.MarkFileModuleHeader(Header.Entry, Role, 7923c1a41adSRichard Smith isCompilingModuleHeader); 793b146baabSArgyrios Kyrtzidis } 7943c1a41adSRichard Smith Headers[Header.Entry].push_back(KnownHeader(Mod, Role)); 7953c1a41adSRichard Smith 7963c1a41adSRichard Smith Mod->Headers[headerRoleToKind(Role)].push_back(std::move(Header)); 797a89c5ac4SDouglas Gregor } 798a89c5ac4SDouglas Gregor 7993c1a41adSRichard Smith void ModuleMap::excludeHeader(Module *Mod, Module::Header Header) { 800feb54b6dSRichard Smith // Add this as a known header so we won't implicitly add it to any 801feb54b6dSRichard Smith // umbrella directory module. 802feb54b6dSRichard Smith // FIXME: Should we only exclude it from umbrella modules within the 803feb54b6dSRichard Smith // specified module? 8043c1a41adSRichard Smith (void) Headers[Header.Entry]; 8053c1a41adSRichard Smith 8063c1a41adSRichard Smith Mod->Headers[Module::HK_Excluded].push_back(std::move(Header)); 807feb54b6dSRichard Smith } 808feb54b6dSRichard Smith 809514b636aSDouglas Gregor const FileEntry * 8104b8a9e95SBen Langmuir ModuleMap::getContainingModuleMapFile(const Module *Module) const { 8111f76c4e8SManuel Klimek if (Module->DefinitionLoc.isInvalid()) 812d2d442caSCraig Topper return nullptr; 813514b636aSDouglas Gregor 8141f76c4e8SManuel Klimek return SourceMgr.getFileEntryForID( 8151f76c4e8SManuel Klimek SourceMgr.getFileID(Module->DefinitionLoc)); 816514b636aSDouglas Gregor } 817514b636aSDouglas Gregor 8184b8a9e95SBen Langmuir const FileEntry *ModuleMap::getModuleMapFileForUniquing(const Module *M) const { 8199d6448b1SBen Langmuir if (M->IsInferred) { 8209d6448b1SBen Langmuir assert(InferredModuleAllowedBy.count(M) && "missing inferred module map"); 8219d6448b1SBen Langmuir return InferredModuleAllowedBy.find(M)->second; 8229d6448b1SBen Langmuir } 8239d6448b1SBen Langmuir return getContainingModuleMapFile(M); 8249d6448b1SBen Langmuir } 8259d6448b1SBen Langmuir 8269d6448b1SBen Langmuir void ModuleMap::setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap) { 8279d6448b1SBen Langmuir assert(M->IsInferred && "module not inferred"); 8289d6448b1SBen Langmuir InferredModuleAllowedBy[M] = ModMap; 8299d6448b1SBen Langmuir } 8309d6448b1SBen Langmuir 831718292f2SDouglas Gregor void ModuleMap::dump() { 832718292f2SDouglas Gregor llvm::errs() << "Modules:"; 833718292f2SDouglas Gregor for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 834718292f2SDouglas Gregor MEnd = Modules.end(); 835718292f2SDouglas Gregor M != MEnd; ++M) 836d28d1b8dSDouglas Gregor M->getValue()->print(llvm::errs(), 2); 837718292f2SDouglas Gregor 838718292f2SDouglas Gregor llvm::errs() << "Headers:"; 83959527666SDouglas Gregor for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 840718292f2SDouglas Gregor H != HEnd; ++H) { 84197da9178SDaniel Jasper llvm::errs() << " \"" << H->first->getName() << "\" -> "; 84297da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(), 84397da9178SDaniel Jasper E = H->second.end(); 84497da9178SDaniel Jasper I != E; ++I) { 84597da9178SDaniel Jasper if (I != H->second.begin()) 84697da9178SDaniel Jasper llvm::errs() << ","; 84797da9178SDaniel Jasper llvm::errs() << I->getModule()->getFullModuleName(); 84897da9178SDaniel Jasper } 84997da9178SDaniel Jasper llvm::errs() << "\n"; 850718292f2SDouglas Gregor } 851718292f2SDouglas Gregor } 852718292f2SDouglas Gregor 8532b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 85442413141SRichard Smith auto Unresolved = std::move(Mod->UnresolvedExports); 85542413141SRichard Smith Mod->UnresolvedExports.clear(); 85642413141SRichard Smith for (auto &UE : Unresolved) { 85742413141SRichard Smith Module::ExportDecl Export = resolveExport(Mod, UE, Complain); 858f5eedd05SDouglas Gregor if (Export.getPointer() || Export.getInt()) 8592b82c2a5SDouglas Gregor Mod->Exports.push_back(Export); 8602b82c2a5SDouglas Gregor else 86142413141SRichard Smith Mod->UnresolvedExports.push_back(UE); 8622b82c2a5SDouglas Gregor } 86342413141SRichard Smith return !Mod->UnresolvedExports.empty(); 8642b82c2a5SDouglas Gregor } 8652b82c2a5SDouglas Gregor 866ba7f2f71SDaniel Jasper bool ModuleMap::resolveUses(Module *Mod, bool Complain) { 86742413141SRichard Smith auto Unresolved = std::move(Mod->UnresolvedDirectUses); 86842413141SRichard Smith Mod->UnresolvedDirectUses.clear(); 86942413141SRichard Smith for (auto &UDU : Unresolved) { 87042413141SRichard Smith Module *DirectUse = resolveModuleId(UDU, Mod, Complain); 871ba7f2f71SDaniel Jasper if (DirectUse) 872ba7f2f71SDaniel Jasper Mod->DirectUses.push_back(DirectUse); 873ba7f2f71SDaniel Jasper else 87442413141SRichard Smith Mod->UnresolvedDirectUses.push_back(UDU); 875ba7f2f71SDaniel Jasper } 87642413141SRichard Smith return !Mod->UnresolvedDirectUses.empty(); 877ba7f2f71SDaniel Jasper } 878ba7f2f71SDaniel Jasper 879fb912657SDouglas Gregor bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) { 88042413141SRichard Smith auto Unresolved = std::move(Mod->UnresolvedConflicts); 88142413141SRichard Smith Mod->UnresolvedConflicts.clear(); 88242413141SRichard Smith for (auto &UC : Unresolved) { 88342413141SRichard Smith if (Module *OtherMod = resolveModuleId(UC.Id, Mod, Complain)) { 884fb912657SDouglas Gregor Module::Conflict Conflict; 885fb912657SDouglas Gregor Conflict.Other = OtherMod; 88642413141SRichard Smith Conflict.Message = UC.Message; 887fb912657SDouglas Gregor Mod->Conflicts.push_back(Conflict); 88842413141SRichard Smith } else 88942413141SRichard Smith Mod->UnresolvedConflicts.push_back(UC); 890fb912657SDouglas Gregor } 89142413141SRichard Smith return !Mod->UnresolvedConflicts.empty(); 892fb912657SDouglas Gregor } 893fb912657SDouglas Gregor 8940093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 8950093b3c7SDouglas Gregor if (Loc.isInvalid()) 896d2d442caSCraig Topper return nullptr; 8970093b3c7SDouglas Gregor 8980093b3c7SDouglas Gregor // Use the expansion location to determine which module we're in. 8990093b3c7SDouglas Gregor FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 9000093b3c7SDouglas Gregor if (!ExpansionLoc.isFileID()) 901d2d442caSCraig Topper return nullptr; 9020093b3c7SDouglas Gregor 9030093b3c7SDouglas Gregor const SourceManager &SrcMgr = Loc.getManager(); 9040093b3c7SDouglas Gregor FileID ExpansionFileID = ExpansionLoc.getFileID(); 905224d8a74SDouglas Gregor 906224d8a74SDouglas Gregor while (const FileEntry *ExpansionFile 907224d8a74SDouglas Gregor = SrcMgr.getFileEntryForID(ExpansionFileID)) { 908224d8a74SDouglas Gregor // Find the module that owns this header (if any). 909b53e5483SLawrence Crowl if (Module *Mod = findModuleForHeader(ExpansionFile).getModule()) 910224d8a74SDouglas Gregor return Mod; 911224d8a74SDouglas Gregor 912224d8a74SDouglas Gregor // No module owns this header, so look up the inclusion chain to see if 913224d8a74SDouglas Gregor // any included header has an associated module. 914224d8a74SDouglas Gregor SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); 915224d8a74SDouglas Gregor if (IncludeLoc.isInvalid()) 916d2d442caSCraig Topper return nullptr; 9170093b3c7SDouglas Gregor 918224d8a74SDouglas Gregor ExpansionFileID = SrcMgr.getFileID(IncludeLoc); 919224d8a74SDouglas Gregor } 920224d8a74SDouglas Gregor 921d2d442caSCraig Topper return nullptr; 9220093b3c7SDouglas Gregor } 9230093b3c7SDouglas Gregor 924718292f2SDouglas Gregor //----------------------------------------------------------------------------// 925718292f2SDouglas Gregor // Module map file parser 926718292f2SDouglas Gregor //----------------------------------------------------------------------------// 927718292f2SDouglas Gregor 928718292f2SDouglas Gregor namespace clang { 929718292f2SDouglas Gregor /// \brief A token in a module map file. 930718292f2SDouglas Gregor struct MMToken { 931718292f2SDouglas Gregor enum TokenKind { 9321fb5c3a6SDouglas Gregor Comma, 93335b13eceSDouglas Gregor ConfigMacros, 934fb912657SDouglas Gregor Conflict, 935718292f2SDouglas Gregor EndOfFile, 936718292f2SDouglas Gregor HeaderKeyword, 937718292f2SDouglas Gregor Identifier, 938a3feee2aSRichard Smith Exclaim, 93959527666SDouglas Gregor ExcludeKeyword, 940718292f2SDouglas Gregor ExplicitKeyword, 9412b82c2a5SDouglas Gregor ExportKeyword, 94297292843SDaniel Jasper ExternKeyword, 943755b2055SDouglas Gregor FrameworkKeyword, 9446ddfca91SDouglas Gregor LinkKeyword, 945718292f2SDouglas Gregor ModuleKeyword, 9462b82c2a5SDouglas Gregor Period, 947b53e5483SLawrence Crowl PrivateKeyword, 948718292f2SDouglas Gregor UmbrellaKeyword, 949ba7f2f71SDaniel Jasper UseKeyword, 9501fb5c3a6SDouglas Gregor RequiresKeyword, 9512b82c2a5SDouglas Gregor Star, 952718292f2SDouglas Gregor StringLiteral, 953306d8920SRichard Smith TextualKeyword, 954718292f2SDouglas Gregor LBrace, 955a686e1b0SDouglas Gregor RBrace, 956a686e1b0SDouglas Gregor LSquare, 957a686e1b0SDouglas Gregor RSquare 958718292f2SDouglas Gregor } Kind; 959718292f2SDouglas Gregor 960718292f2SDouglas Gregor unsigned Location; 961718292f2SDouglas Gregor unsigned StringLength; 962718292f2SDouglas Gregor const char *StringData; 963718292f2SDouglas Gregor 964718292f2SDouglas Gregor void clear() { 965718292f2SDouglas Gregor Kind = EndOfFile; 966718292f2SDouglas Gregor Location = 0; 967718292f2SDouglas Gregor StringLength = 0; 968d2d442caSCraig Topper StringData = nullptr; 969718292f2SDouglas Gregor } 970718292f2SDouglas Gregor 971718292f2SDouglas Gregor bool is(TokenKind K) const { return Kind == K; } 972718292f2SDouglas Gregor 973718292f2SDouglas Gregor SourceLocation getLocation() const { 974718292f2SDouglas Gregor return SourceLocation::getFromRawEncoding(Location); 975718292f2SDouglas Gregor } 976718292f2SDouglas Gregor 977718292f2SDouglas Gregor StringRef getString() const { 978718292f2SDouglas Gregor return StringRef(StringData, StringLength); 979718292f2SDouglas Gregor } 980718292f2SDouglas Gregor }; 981718292f2SDouglas Gregor 982718292f2SDouglas Gregor class ModuleMapParser { 983718292f2SDouglas Gregor Lexer &L; 984718292f2SDouglas Gregor SourceManager &SourceMgr; 985bc10b9fbSDouglas Gregor 986bc10b9fbSDouglas Gregor /// \brief Default target information, used only for string literal 987bc10b9fbSDouglas Gregor /// parsing. 988bc10b9fbSDouglas Gregor const TargetInfo *Target; 989bc10b9fbSDouglas Gregor 990718292f2SDouglas Gregor DiagnosticsEngine &Diags; 991718292f2SDouglas Gregor ModuleMap ⤅ 992718292f2SDouglas Gregor 993beee15e7SBen Langmuir /// \brief The current module map file. 994beee15e7SBen Langmuir const FileEntry *ModuleMapFile; 995beee15e7SBen Langmuir 9969acb99e3SRichard Smith /// \brief The directory that file names in this module map file should 9979acb99e3SRichard Smith /// be resolved relative to. 9985257fc63SDouglas Gregor const DirectoryEntry *Directory; 9995257fc63SDouglas Gregor 10003ec6663bSDouglas Gregor /// \brief The directory containing Clang-supplied headers. 10013ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir; 10023ec6663bSDouglas Gregor 1003963c5535SDouglas Gregor /// \brief Whether this module map is in a system header directory. 1004963c5535SDouglas Gregor bool IsSystem; 1005963c5535SDouglas Gregor 1006718292f2SDouglas Gregor /// \brief Whether an error occurred. 1007718292f2SDouglas Gregor bool HadError; 1008718292f2SDouglas Gregor 1009718292f2SDouglas Gregor /// \brief Stores string data for the various string literals referenced 1010718292f2SDouglas Gregor /// during parsing. 1011718292f2SDouglas Gregor llvm::BumpPtrAllocator StringData; 1012718292f2SDouglas Gregor 1013718292f2SDouglas Gregor /// \brief The current token. 1014718292f2SDouglas Gregor MMToken Tok; 1015718292f2SDouglas Gregor 1016718292f2SDouglas Gregor /// \brief The active module. 1017de3ef502SDouglas Gregor Module *ActiveModule; 1018718292f2SDouglas Gregor 1019*7ff29148SBen Langmuir /// \brief Whether a module uses the 'requires excluded' hack to mark its 1020*7ff29148SBen Langmuir /// contents as 'textual'. 1021*7ff29148SBen Langmuir /// 1022*7ff29148SBen Langmuir /// On older Darwin SDK versions, 'requires excluded' is used to mark the 1023*7ff29148SBen Langmuir /// contents of the Darwin.C.excluded (assert.h) and Tcl.Private modules as 1024*7ff29148SBen Langmuir /// non-modular headers. For backwards compatibility, we continue to 1025*7ff29148SBen Langmuir /// support this idiom for just these modules, and map the headers to 1026*7ff29148SBen Langmuir /// 'textual' to match the original intent. 1027*7ff29148SBen Langmuir llvm::SmallPtrSet<Module *, 2> UsesRequiresExcludedHack; 1028*7ff29148SBen Langmuir 1029718292f2SDouglas Gregor /// \brief Consume the current token and return its location. 1030718292f2SDouglas Gregor SourceLocation consumeToken(); 1031718292f2SDouglas Gregor 1032718292f2SDouglas Gregor /// \brief Skip tokens until we reach the a token with the given kind 1033718292f2SDouglas Gregor /// (or the end of the file). 1034718292f2SDouglas Gregor void skipUntil(MMToken::TokenKind K); 1035718292f2SDouglas Gregor 1036f857950dSDmitri Gribenko typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; 1037e7ab3669SDouglas Gregor bool parseModuleId(ModuleId &Id); 1038718292f2SDouglas Gregor void parseModuleDecl(); 103997292843SDaniel Jasper void parseExternModuleDecl(); 10401fb5c3a6SDouglas Gregor void parseRequiresDecl(); 1041b53e5483SLawrence Crowl void parseHeaderDecl(clang::MMToken::TokenKind, 1042b53e5483SLawrence Crowl SourceLocation LeadingLoc); 1043524e33e1SDouglas Gregor void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 10442b82c2a5SDouglas Gregor void parseExportDecl(); 1045ba7f2f71SDaniel Jasper void parseUseDecl(); 10466ddfca91SDouglas Gregor void parseLinkDecl(); 104735b13eceSDouglas Gregor void parseConfigMacros(); 1048fb912657SDouglas Gregor void parseConflict(); 10499194a91dSDouglas Gregor void parseInferredModuleDecl(bool Framework, bool Explicit); 1050c1d88ea5SBen Langmuir 1051c1d88ea5SBen Langmuir typedef ModuleMap::Attributes Attributes; 10524442605fSBill Wendling bool parseOptionalAttributes(Attributes &Attrs); 1053718292f2SDouglas Gregor 1054718292f2SDouglas Gregor public: 1055718292f2SDouglas Gregor explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 1056bc10b9fbSDouglas Gregor const TargetInfo *Target, 1057718292f2SDouglas Gregor DiagnosticsEngine &Diags, 10585257fc63SDouglas Gregor ModuleMap &Map, 1059beee15e7SBen Langmuir const FileEntry *ModuleMapFile, 10603ec6663bSDouglas Gregor const DirectoryEntry *Directory, 1061963c5535SDouglas Gregor const DirectoryEntry *BuiltinIncludeDir, 1062963c5535SDouglas Gregor bool IsSystem) 1063bc10b9fbSDouglas Gregor : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 1064beee15e7SBen Langmuir ModuleMapFile(ModuleMapFile), Directory(Directory), 1065beee15e7SBen Langmuir BuiltinIncludeDir(BuiltinIncludeDir), IsSystem(IsSystem), 1066d2d442caSCraig Topper HadError(false), ActiveModule(nullptr) 1067718292f2SDouglas Gregor { 1068718292f2SDouglas Gregor Tok.clear(); 1069718292f2SDouglas Gregor consumeToken(); 1070718292f2SDouglas Gregor } 1071718292f2SDouglas Gregor 1072718292f2SDouglas Gregor bool parseModuleMapFile(); 1073718292f2SDouglas Gregor }; 1074ab9db510SAlexander Kornienko } 1075718292f2SDouglas Gregor 1076718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() { 1077718292f2SDouglas Gregor retry: 1078718292f2SDouglas Gregor SourceLocation Result = Tok.getLocation(); 1079718292f2SDouglas Gregor Tok.clear(); 1080718292f2SDouglas Gregor 1081718292f2SDouglas Gregor Token LToken; 1082718292f2SDouglas Gregor L.LexFromRawLexer(LToken); 1083718292f2SDouglas Gregor Tok.Location = LToken.getLocation().getRawEncoding(); 1084718292f2SDouglas Gregor switch (LToken.getKind()) { 10852d57cea2SAlp Toker case tok::raw_identifier: { 10862d57cea2SAlp Toker StringRef RI = LToken.getRawIdentifier(); 10872d57cea2SAlp Toker Tok.StringData = RI.data(); 10882d57cea2SAlp Toker Tok.StringLength = RI.size(); 10892d57cea2SAlp Toker Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI) 109035b13eceSDouglas Gregor .Case("config_macros", MMToken::ConfigMacros) 1091fb912657SDouglas Gregor .Case("conflict", MMToken::Conflict) 109259527666SDouglas Gregor .Case("exclude", MMToken::ExcludeKeyword) 1093718292f2SDouglas Gregor .Case("explicit", MMToken::ExplicitKeyword) 10942b82c2a5SDouglas Gregor .Case("export", MMToken::ExportKeyword) 109597292843SDaniel Jasper .Case("extern", MMToken::ExternKeyword) 1096755b2055SDouglas Gregor .Case("framework", MMToken::FrameworkKeyword) 109735b13eceSDouglas Gregor .Case("header", MMToken::HeaderKeyword) 10986ddfca91SDouglas Gregor .Case("link", MMToken::LinkKeyword) 1099718292f2SDouglas Gregor .Case("module", MMToken::ModuleKeyword) 1100b53e5483SLawrence Crowl .Case("private", MMToken::PrivateKeyword) 11011fb5c3a6SDouglas Gregor .Case("requires", MMToken::RequiresKeyword) 1102306d8920SRichard Smith .Case("textual", MMToken::TextualKeyword) 1103718292f2SDouglas Gregor .Case("umbrella", MMToken::UmbrellaKeyword) 1104ba7f2f71SDaniel Jasper .Case("use", MMToken::UseKeyword) 1105718292f2SDouglas Gregor .Default(MMToken::Identifier); 1106718292f2SDouglas Gregor break; 11072d57cea2SAlp Toker } 1108718292f2SDouglas Gregor 11091fb5c3a6SDouglas Gregor case tok::comma: 11101fb5c3a6SDouglas Gregor Tok.Kind = MMToken::Comma; 11111fb5c3a6SDouglas Gregor break; 11121fb5c3a6SDouglas Gregor 1113718292f2SDouglas Gregor case tok::eof: 1114718292f2SDouglas Gregor Tok.Kind = MMToken::EndOfFile; 1115718292f2SDouglas Gregor break; 1116718292f2SDouglas Gregor 1117718292f2SDouglas Gregor case tok::l_brace: 1118718292f2SDouglas Gregor Tok.Kind = MMToken::LBrace; 1119718292f2SDouglas Gregor break; 1120718292f2SDouglas Gregor 1121a686e1b0SDouglas Gregor case tok::l_square: 1122a686e1b0SDouglas Gregor Tok.Kind = MMToken::LSquare; 1123a686e1b0SDouglas Gregor break; 1124a686e1b0SDouglas Gregor 11252b82c2a5SDouglas Gregor case tok::period: 11262b82c2a5SDouglas Gregor Tok.Kind = MMToken::Period; 11272b82c2a5SDouglas Gregor break; 11282b82c2a5SDouglas Gregor 1129718292f2SDouglas Gregor case tok::r_brace: 1130718292f2SDouglas Gregor Tok.Kind = MMToken::RBrace; 1131718292f2SDouglas Gregor break; 1132718292f2SDouglas Gregor 1133a686e1b0SDouglas Gregor case tok::r_square: 1134a686e1b0SDouglas Gregor Tok.Kind = MMToken::RSquare; 1135a686e1b0SDouglas Gregor break; 1136a686e1b0SDouglas Gregor 11372b82c2a5SDouglas Gregor case tok::star: 11382b82c2a5SDouglas Gregor Tok.Kind = MMToken::Star; 11392b82c2a5SDouglas Gregor break; 11402b82c2a5SDouglas Gregor 1141a3feee2aSRichard Smith case tok::exclaim: 1142a3feee2aSRichard Smith Tok.Kind = MMToken::Exclaim; 1143a3feee2aSRichard Smith break; 1144a3feee2aSRichard Smith 1145718292f2SDouglas Gregor case tok::string_literal: { 1146d67aea28SRichard Smith if (LToken.hasUDSuffix()) { 1147d67aea28SRichard Smith Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 1148d67aea28SRichard Smith HadError = true; 1149d67aea28SRichard Smith goto retry; 1150d67aea28SRichard Smith } 1151d67aea28SRichard Smith 1152718292f2SDouglas Gregor // Parse the string literal. 1153718292f2SDouglas Gregor LangOptions LangOpts; 11549d5583efSCraig Topper StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target); 1155718292f2SDouglas Gregor if (StringLiteral.hadError) 1156718292f2SDouglas Gregor goto retry; 1157718292f2SDouglas Gregor 1158718292f2SDouglas Gregor // Copy the string literal into our string data allocator. 1159718292f2SDouglas Gregor unsigned Length = StringLiteral.GetStringLength(); 1160718292f2SDouglas Gregor char *Saved = StringData.Allocate<char>(Length + 1); 1161718292f2SDouglas Gregor memcpy(Saved, StringLiteral.GetString().data(), Length); 1162718292f2SDouglas Gregor Saved[Length] = 0; 1163718292f2SDouglas Gregor 1164718292f2SDouglas Gregor // Form the token. 1165718292f2SDouglas Gregor Tok.Kind = MMToken::StringLiteral; 1166718292f2SDouglas Gregor Tok.StringData = Saved; 1167718292f2SDouglas Gregor Tok.StringLength = Length; 1168718292f2SDouglas Gregor break; 1169718292f2SDouglas Gregor } 1170718292f2SDouglas Gregor 1171718292f2SDouglas Gregor case tok::comment: 1172718292f2SDouglas Gregor goto retry; 1173718292f2SDouglas Gregor 1174718292f2SDouglas Gregor default: 1175718292f2SDouglas Gregor Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 1176718292f2SDouglas Gregor HadError = true; 1177718292f2SDouglas Gregor goto retry; 1178718292f2SDouglas Gregor } 1179718292f2SDouglas Gregor 1180718292f2SDouglas Gregor return Result; 1181718292f2SDouglas Gregor } 1182718292f2SDouglas Gregor 1183718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 1184718292f2SDouglas Gregor unsigned braceDepth = 0; 1185a686e1b0SDouglas Gregor unsigned squareDepth = 0; 1186718292f2SDouglas Gregor do { 1187718292f2SDouglas Gregor switch (Tok.Kind) { 1188718292f2SDouglas Gregor case MMToken::EndOfFile: 1189718292f2SDouglas Gregor return; 1190718292f2SDouglas Gregor 1191718292f2SDouglas Gregor case MMToken::LBrace: 1192a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1193718292f2SDouglas Gregor return; 1194718292f2SDouglas Gregor 1195718292f2SDouglas Gregor ++braceDepth; 1196718292f2SDouglas Gregor break; 1197718292f2SDouglas Gregor 1198a686e1b0SDouglas Gregor case MMToken::LSquare: 1199a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1200a686e1b0SDouglas Gregor return; 1201a686e1b0SDouglas Gregor 1202a686e1b0SDouglas Gregor ++squareDepth; 1203a686e1b0SDouglas Gregor break; 1204a686e1b0SDouglas Gregor 1205718292f2SDouglas Gregor case MMToken::RBrace: 1206718292f2SDouglas Gregor if (braceDepth > 0) 1207718292f2SDouglas Gregor --braceDepth; 1208718292f2SDouglas Gregor else if (Tok.is(K)) 1209718292f2SDouglas Gregor return; 1210718292f2SDouglas Gregor break; 1211718292f2SDouglas Gregor 1212a686e1b0SDouglas Gregor case MMToken::RSquare: 1213a686e1b0SDouglas Gregor if (squareDepth > 0) 1214a686e1b0SDouglas Gregor --squareDepth; 1215a686e1b0SDouglas Gregor else if (Tok.is(K)) 1216a686e1b0SDouglas Gregor return; 1217a686e1b0SDouglas Gregor break; 1218a686e1b0SDouglas Gregor 1219718292f2SDouglas Gregor default: 1220a686e1b0SDouglas Gregor if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 1221718292f2SDouglas Gregor return; 1222718292f2SDouglas Gregor break; 1223718292f2SDouglas Gregor } 1224718292f2SDouglas Gregor 1225718292f2SDouglas Gregor consumeToken(); 1226718292f2SDouglas Gregor } while (true); 1227718292f2SDouglas Gregor } 1228718292f2SDouglas Gregor 1229e7ab3669SDouglas Gregor /// \brief Parse a module-id. 1230e7ab3669SDouglas Gregor /// 1231e7ab3669SDouglas Gregor /// module-id: 1232e7ab3669SDouglas Gregor /// identifier 1233e7ab3669SDouglas Gregor /// identifier '.' module-id 1234e7ab3669SDouglas Gregor /// 1235e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise. 1236e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) { 1237e7ab3669SDouglas Gregor Id.clear(); 1238e7ab3669SDouglas Gregor do { 12393cd34c76SDaniel Jasper if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) { 1240e7ab3669SDouglas Gregor Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 1241e7ab3669SDouglas Gregor consumeToken(); 1242e7ab3669SDouglas Gregor } else { 1243e7ab3669SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 1244e7ab3669SDouglas Gregor return true; 1245e7ab3669SDouglas Gregor } 1246e7ab3669SDouglas Gregor 1247e7ab3669SDouglas Gregor if (!Tok.is(MMToken::Period)) 1248e7ab3669SDouglas Gregor break; 1249e7ab3669SDouglas Gregor 1250e7ab3669SDouglas Gregor consumeToken(); 1251e7ab3669SDouglas Gregor } while (true); 1252e7ab3669SDouglas Gregor 1253e7ab3669SDouglas Gregor return false; 1254e7ab3669SDouglas Gregor } 1255e7ab3669SDouglas Gregor 1256a686e1b0SDouglas Gregor namespace { 1257a686e1b0SDouglas Gregor /// \brief Enumerates the known attributes. 1258a686e1b0SDouglas Gregor enum AttributeKind { 1259a686e1b0SDouglas Gregor /// \brief An unknown attribute. 1260a686e1b0SDouglas Gregor AT_unknown, 1261a686e1b0SDouglas Gregor /// \brief The 'system' attribute. 126235b13eceSDouglas Gregor AT_system, 126377944868SRichard Smith /// \brief The 'extern_c' attribute. 126477944868SRichard Smith AT_extern_c, 126535b13eceSDouglas Gregor /// \brief The 'exhaustive' attribute. 126635b13eceSDouglas Gregor AT_exhaustive 1267a686e1b0SDouglas Gregor }; 1268ab9db510SAlexander Kornienko } 1269a686e1b0SDouglas Gregor 1270718292f2SDouglas Gregor /// \brief Parse a module declaration. 1271718292f2SDouglas Gregor /// 1272718292f2SDouglas Gregor /// module-declaration: 127397292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 1274a686e1b0SDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 1275a686e1b0SDouglas Gregor /// { module-member* } 1276a686e1b0SDouglas Gregor /// 1277718292f2SDouglas Gregor /// module-member: 12781fb5c3a6SDouglas Gregor /// requires-declaration 1279718292f2SDouglas Gregor /// header-declaration 1280e7ab3669SDouglas Gregor /// submodule-declaration 12812b82c2a5SDouglas Gregor /// export-declaration 12826ddfca91SDouglas Gregor /// link-declaration 128373441091SDouglas Gregor /// 128473441091SDouglas Gregor /// submodule-declaration: 128573441091SDouglas Gregor /// module-declaration 128673441091SDouglas Gregor /// inferred-submodule-declaration 1287718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() { 1288755b2055SDouglas Gregor assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 128997292843SDaniel Jasper Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword)); 129097292843SDaniel Jasper if (Tok.is(MMToken::ExternKeyword)) { 129197292843SDaniel Jasper parseExternModuleDecl(); 129297292843SDaniel Jasper return; 129397292843SDaniel Jasper } 129497292843SDaniel Jasper 1295f2161a70SDouglas Gregor // Parse 'explicit' or 'framework' keyword, if present. 1296e7ab3669SDouglas Gregor SourceLocation ExplicitLoc; 1297718292f2SDouglas Gregor bool Explicit = false; 1298f2161a70SDouglas Gregor bool Framework = false; 1299755b2055SDouglas Gregor 1300f2161a70SDouglas Gregor // Parse 'explicit' keyword, if present. 1301f2161a70SDouglas Gregor if (Tok.is(MMToken::ExplicitKeyword)) { 1302e7ab3669SDouglas Gregor ExplicitLoc = consumeToken(); 1303f2161a70SDouglas Gregor Explicit = true; 1304f2161a70SDouglas Gregor } 1305f2161a70SDouglas Gregor 1306f2161a70SDouglas Gregor // Parse 'framework' keyword, if present. 1307755b2055SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 1308755b2055SDouglas Gregor consumeToken(); 1309755b2055SDouglas Gregor Framework = true; 1310755b2055SDouglas Gregor } 1311718292f2SDouglas Gregor 1312718292f2SDouglas Gregor // Parse 'module' keyword. 1313718292f2SDouglas Gregor if (!Tok.is(MMToken::ModuleKeyword)) { 1314d6343c99SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1315718292f2SDouglas Gregor consumeToken(); 1316718292f2SDouglas Gregor HadError = true; 1317718292f2SDouglas Gregor return; 1318718292f2SDouglas Gregor } 1319718292f2SDouglas Gregor consumeToken(); // 'module' keyword 1320718292f2SDouglas Gregor 132173441091SDouglas Gregor // If we have a wildcard for the module name, this is an inferred submodule. 132273441091SDouglas Gregor // Parse it. 132373441091SDouglas Gregor if (Tok.is(MMToken::Star)) 13249194a91dSDouglas Gregor return parseInferredModuleDecl(Framework, Explicit); 132573441091SDouglas Gregor 1326718292f2SDouglas Gregor // Parse the module name. 1327e7ab3669SDouglas Gregor ModuleId Id; 1328e7ab3669SDouglas Gregor if (parseModuleId(Id)) { 1329718292f2SDouglas Gregor HadError = true; 1330718292f2SDouglas Gregor return; 1331718292f2SDouglas Gregor } 1332e7ab3669SDouglas Gregor 1333e7ab3669SDouglas Gregor if (ActiveModule) { 1334e7ab3669SDouglas Gregor if (Id.size() > 1) { 1335e7ab3669SDouglas Gregor Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 1336e7ab3669SDouglas Gregor << SourceRange(Id.front().second, Id.back().second); 1337e7ab3669SDouglas Gregor 1338e7ab3669SDouglas Gregor HadError = true; 1339e7ab3669SDouglas Gregor return; 1340e7ab3669SDouglas Gregor } 1341e7ab3669SDouglas Gregor } else if (Id.size() == 1 && Explicit) { 1342e7ab3669SDouglas Gregor // Top-level modules can't be explicit. 1343e7ab3669SDouglas Gregor Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 1344e7ab3669SDouglas Gregor Explicit = false; 1345e7ab3669SDouglas Gregor ExplicitLoc = SourceLocation(); 1346e7ab3669SDouglas Gregor HadError = true; 1347e7ab3669SDouglas Gregor } 1348e7ab3669SDouglas Gregor 1349e7ab3669SDouglas Gregor Module *PreviousActiveModule = ActiveModule; 1350e7ab3669SDouglas Gregor if (Id.size() > 1) { 1351e7ab3669SDouglas Gregor // This module map defines a submodule. Go find the module of which it 1352e7ab3669SDouglas Gregor // is a submodule. 1353d2d442caSCraig Topper ActiveModule = nullptr; 13544b8a9e95SBen Langmuir const Module *TopLevelModule = nullptr; 1355e7ab3669SDouglas Gregor for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 1356e7ab3669SDouglas Gregor if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 13574b8a9e95SBen Langmuir if (I == 0) 13584b8a9e95SBen Langmuir TopLevelModule = Next; 1359e7ab3669SDouglas Gregor ActiveModule = Next; 1360e7ab3669SDouglas Gregor continue; 1361e7ab3669SDouglas Gregor } 1362e7ab3669SDouglas Gregor 1363e7ab3669SDouglas Gregor if (ActiveModule) { 1364e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 13655b5d21eaSRichard Smith << Id[I].first 13665b5d21eaSRichard Smith << ActiveModule->getTopLevelModule()->getFullModuleName(); 1367e7ab3669SDouglas Gregor } else { 1368e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 1369e7ab3669SDouglas Gregor } 1370e7ab3669SDouglas Gregor HadError = true; 1371e7ab3669SDouglas Gregor return; 1372e7ab3669SDouglas Gregor } 13734b8a9e95SBen Langmuir 13744b8a9e95SBen Langmuir if (ModuleMapFile != Map.getContainingModuleMapFile(TopLevelModule)) { 13754b8a9e95SBen Langmuir assert(ModuleMapFile != Map.getModuleMapFileForUniquing(TopLevelModule) && 13764b8a9e95SBen Langmuir "submodule defined in same file as 'module *' that allowed its " 13774b8a9e95SBen Langmuir "top-level module"); 13784b8a9e95SBen Langmuir Map.addAdditionalModuleMapFile(TopLevelModule, ModuleMapFile); 13794b8a9e95SBen Langmuir } 1380e7ab3669SDouglas Gregor } 1381e7ab3669SDouglas Gregor 1382e7ab3669SDouglas Gregor StringRef ModuleName = Id.back().first; 1383e7ab3669SDouglas Gregor SourceLocation ModuleNameLoc = Id.back().second; 1384718292f2SDouglas Gregor 1385a686e1b0SDouglas Gregor // Parse the optional attribute list. 13864442605fSBill Wendling Attributes Attrs; 13879194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 1388a686e1b0SDouglas Gregor 1389718292f2SDouglas Gregor // Parse the opening brace. 1390718292f2SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 1391718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 1392718292f2SDouglas Gregor << ModuleName; 1393718292f2SDouglas Gregor HadError = true; 1394718292f2SDouglas Gregor return; 1395718292f2SDouglas Gregor } 1396718292f2SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 1397718292f2SDouglas Gregor 1398718292f2SDouglas Gregor // Determine whether this (sub)module has already been defined. 1399eb90e830SDouglas Gregor if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 1400fcc54a3bSDouglas Gregor if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { 1401fcc54a3bSDouglas Gregor // Skip the module definition. 1402fcc54a3bSDouglas Gregor skipUntil(MMToken::RBrace); 1403fcc54a3bSDouglas Gregor if (Tok.is(MMToken::RBrace)) 1404fcc54a3bSDouglas Gregor consumeToken(); 1405fcc54a3bSDouglas Gregor else { 1406fcc54a3bSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1407fcc54a3bSDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1408fcc54a3bSDouglas Gregor HadError = true; 1409fcc54a3bSDouglas Gregor } 1410fcc54a3bSDouglas Gregor return; 1411fcc54a3bSDouglas Gregor } 1412fcc54a3bSDouglas Gregor 1413718292f2SDouglas Gregor Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 1414718292f2SDouglas Gregor << ModuleName; 1415eb90e830SDouglas Gregor Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 1416718292f2SDouglas Gregor 1417718292f2SDouglas Gregor // Skip the module definition. 1418718292f2SDouglas Gregor skipUntil(MMToken::RBrace); 1419718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1420718292f2SDouglas Gregor consumeToken(); 1421718292f2SDouglas Gregor 1422718292f2SDouglas Gregor HadError = true; 1423718292f2SDouglas Gregor return; 1424718292f2SDouglas Gregor } 1425718292f2SDouglas Gregor 1426718292f2SDouglas Gregor // Start defining this module. 14279d6448b1SBen Langmuir ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, 14289d6448b1SBen Langmuir Explicit).first; 1429eb90e830SDouglas Gregor ActiveModule->DefinitionLoc = ModuleNameLoc; 1430963c5535SDouglas Gregor if (Attrs.IsSystem || IsSystem) 1431a686e1b0SDouglas Gregor ActiveModule->IsSystem = true; 143277944868SRichard Smith if (Attrs.IsExternC) 143377944868SRichard Smith ActiveModule->IsExternC = true; 14343c1a41adSRichard Smith ActiveModule->Directory = Directory; 1435718292f2SDouglas Gregor 1436718292f2SDouglas Gregor bool Done = false; 1437718292f2SDouglas Gregor do { 1438718292f2SDouglas Gregor switch (Tok.Kind) { 1439718292f2SDouglas Gregor case MMToken::EndOfFile: 1440718292f2SDouglas Gregor case MMToken::RBrace: 1441718292f2SDouglas Gregor Done = true; 1442718292f2SDouglas Gregor break; 1443718292f2SDouglas Gregor 144435b13eceSDouglas Gregor case MMToken::ConfigMacros: 144535b13eceSDouglas Gregor parseConfigMacros(); 144635b13eceSDouglas Gregor break; 144735b13eceSDouglas Gregor 1448fb912657SDouglas Gregor case MMToken::Conflict: 1449fb912657SDouglas Gregor parseConflict(); 1450fb912657SDouglas Gregor break; 1451fb912657SDouglas Gregor 1452718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 145397292843SDaniel Jasper case MMToken::ExternKeyword: 1454f2161a70SDouglas Gregor case MMToken::FrameworkKeyword: 1455718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1456718292f2SDouglas Gregor parseModuleDecl(); 1457718292f2SDouglas Gregor break; 1458718292f2SDouglas Gregor 14592b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 14602b82c2a5SDouglas Gregor parseExportDecl(); 14612b82c2a5SDouglas Gregor break; 14622b82c2a5SDouglas Gregor 1463ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 1464ba7f2f71SDaniel Jasper parseUseDecl(); 1465ba7f2f71SDaniel Jasper break; 1466ba7f2f71SDaniel Jasper 14671fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 14681fb5c3a6SDouglas Gregor parseRequiresDecl(); 14691fb5c3a6SDouglas Gregor break; 14701fb5c3a6SDouglas Gregor 1471202210b3SRichard Smith case MMToken::TextualKeyword: 1472202210b3SRichard Smith parseHeaderDecl(MMToken::TextualKeyword, consumeToken()); 1473306d8920SRichard Smith break; 1474306d8920SRichard Smith 1475524e33e1SDouglas Gregor case MMToken::UmbrellaKeyword: { 1476524e33e1SDouglas Gregor SourceLocation UmbrellaLoc = consumeToken(); 1477524e33e1SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) 1478b53e5483SLawrence Crowl parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc); 1479524e33e1SDouglas Gregor else 1480524e33e1SDouglas Gregor parseUmbrellaDirDecl(UmbrellaLoc); 1481718292f2SDouglas Gregor break; 1482524e33e1SDouglas Gregor } 1483718292f2SDouglas Gregor 1484202210b3SRichard Smith case MMToken::ExcludeKeyword: 1485202210b3SRichard Smith parseHeaderDecl(MMToken::ExcludeKeyword, consumeToken()); 148659527666SDouglas Gregor break; 148759527666SDouglas Gregor 1488202210b3SRichard Smith case MMToken::PrivateKeyword: 1489202210b3SRichard Smith parseHeaderDecl(MMToken::PrivateKeyword, consumeToken()); 1490b53e5483SLawrence Crowl break; 1491b53e5483SLawrence Crowl 1492322f633cSDouglas Gregor case MMToken::HeaderKeyword: 1493202210b3SRichard Smith parseHeaderDecl(MMToken::HeaderKeyword, consumeToken()); 1494718292f2SDouglas Gregor break; 1495718292f2SDouglas Gregor 14966ddfca91SDouglas Gregor case MMToken::LinkKeyword: 14976ddfca91SDouglas Gregor parseLinkDecl(); 14986ddfca91SDouglas Gregor break; 14996ddfca91SDouglas Gregor 1500718292f2SDouglas Gregor default: 1501718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 1502718292f2SDouglas Gregor consumeToken(); 1503718292f2SDouglas Gregor break; 1504718292f2SDouglas Gregor } 1505718292f2SDouglas Gregor } while (!Done); 1506718292f2SDouglas Gregor 1507718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1508718292f2SDouglas Gregor consumeToken(); 1509718292f2SDouglas Gregor else { 1510718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1511718292f2SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1512718292f2SDouglas Gregor HadError = true; 1513718292f2SDouglas Gregor } 1514718292f2SDouglas Gregor 151511dfe6feSDouglas Gregor // If the active module is a top-level framework, and there are no link 151611dfe6feSDouglas Gregor // libraries, automatically link against the framework. 151711dfe6feSDouglas Gregor if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() && 151811dfe6feSDouglas Gregor ActiveModule->LinkLibraries.empty()) { 151911dfe6feSDouglas Gregor inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager()); 152011dfe6feSDouglas Gregor } 152111dfe6feSDouglas Gregor 1522ec8c9752SBen Langmuir // If the module meets all requirements but is still unavailable, mark the 1523ec8c9752SBen Langmuir // whole tree as unavailable to prevent it from building. 1524ec8c9752SBen Langmuir if (!ActiveModule->IsAvailable && !ActiveModule->IsMissingRequirement && 1525ec8c9752SBen Langmuir ActiveModule->Parent) { 1526ec8c9752SBen Langmuir ActiveModule->getTopLevelModule()->markUnavailable(); 1527ec8c9752SBen Langmuir ActiveModule->getTopLevelModule()->MissingHeaders.append( 1528ec8c9752SBen Langmuir ActiveModule->MissingHeaders.begin(), ActiveModule->MissingHeaders.end()); 1529ec8c9752SBen Langmuir } 1530ec8c9752SBen Langmuir 1531e7ab3669SDouglas Gregor // We're done parsing this module. Pop back to the previous module. 1532e7ab3669SDouglas Gregor ActiveModule = PreviousActiveModule; 1533718292f2SDouglas Gregor } 1534718292f2SDouglas Gregor 153597292843SDaniel Jasper /// \brief Parse an extern module declaration. 153697292843SDaniel Jasper /// 153797292843SDaniel Jasper /// extern module-declaration: 153897292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 153997292843SDaniel Jasper void ModuleMapParser::parseExternModuleDecl() { 154097292843SDaniel Jasper assert(Tok.is(MMToken::ExternKeyword)); 1541ae6df27eSRichard Smith SourceLocation ExternLoc = consumeToken(); // 'extern' keyword 154297292843SDaniel Jasper 154397292843SDaniel Jasper // Parse 'module' keyword. 154497292843SDaniel Jasper if (!Tok.is(MMToken::ModuleKeyword)) { 154597292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 154697292843SDaniel Jasper consumeToken(); 154797292843SDaniel Jasper HadError = true; 154897292843SDaniel Jasper return; 154997292843SDaniel Jasper } 155097292843SDaniel Jasper consumeToken(); // 'module' keyword 155197292843SDaniel Jasper 155297292843SDaniel Jasper // Parse the module name. 155397292843SDaniel Jasper ModuleId Id; 155497292843SDaniel Jasper if (parseModuleId(Id)) { 155597292843SDaniel Jasper HadError = true; 155697292843SDaniel Jasper return; 155797292843SDaniel Jasper } 155897292843SDaniel Jasper 155997292843SDaniel Jasper // Parse the referenced module map file name. 156097292843SDaniel Jasper if (!Tok.is(MMToken::StringLiteral)) { 156197292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file); 156297292843SDaniel Jasper HadError = true; 156397292843SDaniel Jasper return; 156497292843SDaniel Jasper } 156597292843SDaniel Jasper std::string FileName = Tok.getString(); 156697292843SDaniel Jasper consumeToken(); // filename 156797292843SDaniel Jasper 156897292843SDaniel Jasper StringRef FileNameRef = FileName; 156997292843SDaniel Jasper SmallString<128> ModuleMapFileName; 157097292843SDaniel Jasper if (llvm::sys::path::is_relative(FileNameRef)) { 157197292843SDaniel Jasper ModuleMapFileName += Directory->getName(); 157297292843SDaniel Jasper llvm::sys::path::append(ModuleMapFileName, FileName); 157392e1b62dSYaron Keren FileNameRef = ModuleMapFileName; 157497292843SDaniel Jasper } 157597292843SDaniel Jasper if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef)) 15769acb99e3SRichard Smith Map.parseModuleMapFile( 15779acb99e3SRichard Smith File, /*IsSystem=*/false, 15789acb99e3SRichard Smith Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd 15799acb99e3SRichard Smith ? Directory 1580ae6df27eSRichard Smith : File->getDir(), ExternLoc); 158197292843SDaniel Jasper } 158297292843SDaniel Jasper 1583*7ff29148SBen Langmuir /// Whether to add the requirement \p Feature to the module \p M. 1584*7ff29148SBen Langmuir /// 1585*7ff29148SBen Langmuir /// This preserves backwards compatibility for two hacks in the Darwin system 1586*7ff29148SBen Langmuir /// module map files: 1587*7ff29148SBen Langmuir /// 1588*7ff29148SBen Langmuir /// 1. The use of 'requires excluded' to make headers non-modular, which 1589*7ff29148SBen Langmuir /// should really be mapped to 'textual' now that we have this feature. We 1590*7ff29148SBen Langmuir /// drop the 'excluded' requirement, and set \p IsRequiresExcludedHack to 1591*7ff29148SBen Langmuir /// true. Later, this bit will be used to map all the headers inside this 1592*7ff29148SBen Langmuir /// module to 'textual'. 1593*7ff29148SBen Langmuir /// 1594*7ff29148SBen Langmuir /// This affects Darwin.C.excluded (for assert.h) and Tcl.Private. 1595*7ff29148SBen Langmuir /// 1596*7ff29148SBen Langmuir /// 2. Removes a bogus cplusplus requirement from IOKit.avc. This requirement 1597*7ff29148SBen Langmuir /// was never correct and causes issues now that we check it, so drop it. 1598*7ff29148SBen Langmuir static bool shouldAddRequirement(Module *M, StringRef Feature, 1599*7ff29148SBen Langmuir bool &IsRequiresExcludedHack) { 1600*7ff29148SBen Langmuir static const StringRef DarwinCExcluded[] = {"Darwin", "C", "excluded"}; 1601*7ff29148SBen Langmuir static const StringRef TclPrivate[] = {"Tcl", "Private"}; 1602*7ff29148SBen Langmuir static const StringRef IOKitAVC[] = {"IOKit", "avc"}; 1603*7ff29148SBen Langmuir 1604*7ff29148SBen Langmuir if (Feature == "excluded" && (M->fullModuleNameIs(DarwinCExcluded) || 1605*7ff29148SBen Langmuir M->fullModuleNameIs(TclPrivate))) { 1606*7ff29148SBen Langmuir IsRequiresExcludedHack = true; 1607*7ff29148SBen Langmuir return false; 1608*7ff29148SBen Langmuir } else if (Feature == "cplusplus" && M->fullModuleNameIs(IOKitAVC)) { 1609*7ff29148SBen Langmuir return false; 1610*7ff29148SBen Langmuir } 1611*7ff29148SBen Langmuir 1612*7ff29148SBen Langmuir return true; 1613*7ff29148SBen Langmuir } 1614*7ff29148SBen Langmuir 16151fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration. 16161fb5c3a6SDouglas Gregor /// 16171fb5c3a6SDouglas Gregor /// requires-declaration: 16181fb5c3a6SDouglas Gregor /// 'requires' feature-list 16191fb5c3a6SDouglas Gregor /// 16201fb5c3a6SDouglas Gregor /// feature-list: 1621a3feee2aSRichard Smith /// feature ',' feature-list 1622a3feee2aSRichard Smith /// feature 1623a3feee2aSRichard Smith /// 1624a3feee2aSRichard Smith /// feature: 1625a3feee2aSRichard Smith /// '!'[opt] identifier 16261fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() { 16271fb5c3a6SDouglas Gregor assert(Tok.is(MMToken::RequiresKeyword)); 16281fb5c3a6SDouglas Gregor 16291fb5c3a6SDouglas Gregor // Parse 'requires' keyword. 16301fb5c3a6SDouglas Gregor consumeToken(); 16311fb5c3a6SDouglas Gregor 16321fb5c3a6SDouglas Gregor // Parse the feature-list. 16331fb5c3a6SDouglas Gregor do { 1634a3feee2aSRichard Smith bool RequiredState = true; 1635a3feee2aSRichard Smith if (Tok.is(MMToken::Exclaim)) { 1636a3feee2aSRichard Smith RequiredState = false; 1637a3feee2aSRichard Smith consumeToken(); 1638a3feee2aSRichard Smith } 1639a3feee2aSRichard Smith 16401fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 16411fb5c3a6SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 16421fb5c3a6SDouglas Gregor HadError = true; 16431fb5c3a6SDouglas Gregor return; 16441fb5c3a6SDouglas Gregor } 16451fb5c3a6SDouglas Gregor 16461fb5c3a6SDouglas Gregor // Consume the feature name. 16471fb5c3a6SDouglas Gregor std::string Feature = Tok.getString(); 16481fb5c3a6SDouglas Gregor consumeToken(); 16491fb5c3a6SDouglas Gregor 1650*7ff29148SBen Langmuir bool IsRequiresExcludedHack = false; 1651*7ff29148SBen Langmuir bool ShouldAddRequirement = 1652*7ff29148SBen Langmuir shouldAddRequirement(ActiveModule, Feature, IsRequiresExcludedHack); 1653*7ff29148SBen Langmuir 1654*7ff29148SBen Langmuir if (IsRequiresExcludedHack) 1655*7ff29148SBen Langmuir UsesRequiresExcludedHack.insert(ActiveModule); 1656*7ff29148SBen Langmuir 1657*7ff29148SBen Langmuir if (ShouldAddRequirement) { 16581fb5c3a6SDouglas Gregor // Add this feature. 1659*7ff29148SBen Langmuir ActiveModule->addRequirement(Feature, RequiredState, Map.LangOpts, 1660*7ff29148SBen Langmuir *Map.Target); 1661*7ff29148SBen Langmuir } 16621fb5c3a6SDouglas Gregor 16631fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Comma)) 16641fb5c3a6SDouglas Gregor break; 16651fb5c3a6SDouglas Gregor 16661fb5c3a6SDouglas Gregor // Consume the comma. 16671fb5c3a6SDouglas Gregor consumeToken(); 16681fb5c3a6SDouglas Gregor } while (true); 16691fb5c3a6SDouglas Gregor } 16701fb5c3a6SDouglas Gregor 1671f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the 1672f2161a70SDouglas Gregor /// subframework in which the given module lives. 1673bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod, 1674f857950dSDmitri Gribenko SmallVectorImpl<char> &Path) { 1675f2161a70SDouglas Gregor // Collect the framework names from the given module to the top-level module. 1676f857950dSDmitri Gribenko SmallVector<StringRef, 2> Paths; 1677f2161a70SDouglas Gregor for (; Mod; Mod = Mod->Parent) { 1678f2161a70SDouglas Gregor if (Mod->IsFramework) 1679f2161a70SDouglas Gregor Paths.push_back(Mod->Name); 1680f2161a70SDouglas Gregor } 1681f2161a70SDouglas Gregor 1682f2161a70SDouglas Gregor if (Paths.empty()) 1683f2161a70SDouglas Gregor return; 1684f2161a70SDouglas Gregor 1685f2161a70SDouglas Gregor // Add Frameworks/Name.framework for each subframework. 168617381a06SBenjamin Kramer for (unsigned I = Paths.size() - 1; I != 0; --I) 168717381a06SBenjamin Kramer llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework"); 1688f2161a70SDouglas Gregor } 1689f2161a70SDouglas Gregor 1690718292f2SDouglas Gregor /// \brief Parse a header declaration. 1691718292f2SDouglas Gregor /// 1692718292f2SDouglas Gregor /// header-declaration: 1693306d8920SRichard Smith /// 'textual'[opt] 'header' string-literal 1694202210b3SRichard Smith /// 'private' 'textual'[opt] 'header' string-literal 1695202210b3SRichard Smith /// 'exclude' 'header' string-literal 1696202210b3SRichard Smith /// 'umbrella' 'header' string-literal 1697306d8920SRichard Smith /// 1698306d8920SRichard Smith /// FIXME: Support 'private textual header'. 1699b53e5483SLawrence Crowl void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken, 1700b53e5483SLawrence Crowl SourceLocation LeadingLoc) { 1701202210b3SRichard Smith // We've already consumed the first token. 1702202210b3SRichard Smith ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader; 1703202210b3SRichard Smith if (LeadingToken == MMToken::PrivateKeyword) { 1704202210b3SRichard Smith Role = ModuleMap::PrivateHeader; 1705202210b3SRichard Smith // 'private' may optionally be followed by 'textual'. 1706202210b3SRichard Smith if (Tok.is(MMToken::TextualKeyword)) { 1707202210b3SRichard Smith LeadingToken = Tok.Kind; 17081871ed3dSBenjamin Kramer consumeToken(); 1709202210b3SRichard Smith } 1710202210b3SRichard Smith } 1711*7ff29148SBen Langmuir 1712202210b3SRichard Smith if (LeadingToken == MMToken::TextualKeyword) 1713202210b3SRichard Smith Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader); 1714202210b3SRichard Smith 1715*7ff29148SBen Langmuir if (UsesRequiresExcludedHack.count(ActiveModule)) { 1716*7ff29148SBen Langmuir // Mark this header 'textual' (see doc comment for 1717*7ff29148SBen Langmuir // Module::UsesRequiresExcludedHack). 1718*7ff29148SBen Langmuir Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader); 1719*7ff29148SBen Langmuir } 1720*7ff29148SBen Langmuir 1721202210b3SRichard Smith if (LeadingToken != MMToken::HeaderKeyword) { 1722202210b3SRichard Smith if (!Tok.is(MMToken::HeaderKeyword)) { 1723202210b3SRichard Smith Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1724202210b3SRichard Smith << (LeadingToken == MMToken::PrivateKeyword ? "private" : 1725202210b3SRichard Smith LeadingToken == MMToken::ExcludeKeyword ? "exclude" : 1726202210b3SRichard Smith LeadingToken == MMToken::TextualKeyword ? "textual" : "umbrella"); 1727202210b3SRichard Smith return; 1728202210b3SRichard Smith } 1729202210b3SRichard Smith consumeToken(); 1730202210b3SRichard Smith } 1731718292f2SDouglas Gregor 1732718292f2SDouglas Gregor // Parse the header name. 1733718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1734718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1735718292f2SDouglas Gregor << "header"; 1736718292f2SDouglas Gregor HadError = true; 1737718292f2SDouglas Gregor return; 1738718292f2SDouglas Gregor } 17393c1a41adSRichard Smith Module::UnresolvedHeaderDirective Header; 17400761a8a0SDaniel Jasper Header.FileName = Tok.getString(); 17410761a8a0SDaniel Jasper Header.FileNameLoc = consumeToken(); 1742718292f2SDouglas Gregor 1743524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1744b53e5483SLawrence Crowl if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) { 17450761a8a0SDaniel Jasper Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash) 1746524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1747322f633cSDouglas Gregor HadError = true; 1748322f633cSDouglas Gregor return; 1749322f633cSDouglas Gregor } 1750322f633cSDouglas Gregor 17515257fc63SDouglas Gregor // Look for this file. 1752d2d442caSCraig Topper const FileEntry *File = nullptr; 1753d2d442caSCraig Topper const FileEntry *BuiltinFile = nullptr; 17543c1a41adSRichard Smith SmallString<128> RelativePathName; 17550761a8a0SDaniel Jasper if (llvm::sys::path::is_absolute(Header.FileName)) { 17563c1a41adSRichard Smith RelativePathName = Header.FileName; 17573c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(RelativePathName); 1758e7ab3669SDouglas Gregor } else { 1759e7ab3669SDouglas Gregor // Search for the header file within the search directory. 17603c1a41adSRichard Smith SmallString<128> FullPathName(Directory->getName()); 17613c1a41adSRichard Smith unsigned FullPathLength = FullPathName.size(); 1762755b2055SDouglas Gregor 1763f2161a70SDouglas Gregor if (ActiveModule->isPartOfFramework()) { 17643c1a41adSRichard Smith appendSubframeworkPaths(ActiveModule, RelativePathName); 1765755b2055SDouglas Gregor 1766e7ab3669SDouglas Gregor // Check whether this file is in the public headers. 17673c1a41adSRichard Smith llvm::sys::path::append(RelativePathName, "Headers", Header.FileName); 176892e1b62dSYaron Keren llvm::sys::path::append(FullPathName, RelativePathName); 17693c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(FullPathName); 1770e7ab3669SDouglas Gregor 1771e7ab3669SDouglas Gregor if (!File) { 1772e7ab3669SDouglas Gregor // Check whether this file is in the private headers. 17733c1a41adSRichard Smith // FIXME: Should we retain the subframework paths here? 17743c1a41adSRichard Smith RelativePathName.clear(); 17753c1a41adSRichard Smith FullPathName.resize(FullPathLength); 17763c1a41adSRichard Smith llvm::sys::path::append(RelativePathName, "PrivateHeaders", 17773c1a41adSRichard Smith Header.FileName); 177892e1b62dSYaron Keren llvm::sys::path::append(FullPathName, RelativePathName); 17793c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(FullPathName); 1780e7ab3669SDouglas Gregor } 1781e7ab3669SDouglas Gregor } else { 1782e7ab3669SDouglas Gregor // Lookup for normal headers. 17833c1a41adSRichard Smith llvm::sys::path::append(RelativePathName, Header.FileName); 178492e1b62dSYaron Keren llvm::sys::path::append(FullPathName, RelativePathName); 17853c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(FullPathName); 17863ec6663bSDouglas Gregor 17873ec6663bSDouglas Gregor // If this is a system module with a top-level header, this header 17883ec6663bSDouglas Gregor // may have a counterpart (or replacement) in the set of headers 17893ec6663bSDouglas Gregor // supplied by Clang. Find that builtin header. 1790b53e5483SLawrence Crowl if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword && 1791b53e5483SLawrence Crowl BuiltinIncludeDir && BuiltinIncludeDir != Directory && 17920761a8a0SDaniel Jasper isBuiltinHeader(Header.FileName)) { 17932c1dd271SDylan Noblesmith SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); 17940761a8a0SDaniel Jasper llvm::sys::path::append(BuiltinPathName, Header.FileName); 17953ec6663bSDouglas Gregor BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); 17963ec6663bSDouglas Gregor 17973ec6663bSDouglas Gregor // If Clang supplies this header but the underlying system does not, 17983ec6663bSDouglas Gregor // just silently swap in our builtin version. Otherwise, we'll end 17993ec6663bSDouglas Gregor // up adding both (later). 180042413141SRichard Smith // 180142413141SRichard Smith // For local visibility, entirely replace the system file with our 180242413141SRichard Smith // one and textually include the system one. We need to pass macros 180342413141SRichard Smith // from our header to the system one if we #include_next it. 180442413141SRichard Smith // 180542413141SRichard Smith // FIXME: Can we do this in all cases? 180642413141SRichard Smith if (BuiltinFile && (!File || Map.LangOpts.ModulesLocalVisibility)) { 18073ec6663bSDouglas Gregor File = BuiltinFile; 18083c1a41adSRichard Smith RelativePathName = BuiltinPathName; 1809d2d442caSCraig Topper BuiltinFile = nullptr; 18103ec6663bSDouglas Gregor } 18113ec6663bSDouglas Gregor } 1812e7ab3669SDouglas Gregor } 1813e7ab3669SDouglas Gregor } 18145257fc63SDouglas Gregor 18155257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 18165257fc63SDouglas Gregor // Come up with a lazy way to do this. 1817e7ab3669SDouglas Gregor if (File) { 181897da9178SDaniel Jasper if (LeadingToken == MMToken::UmbrellaKeyword) { 1819322f633cSDouglas Gregor const DirectoryEntry *UmbrellaDir = File->getDir(); 182059527666SDouglas Gregor if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { 1821b53e5483SLawrence Crowl Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash) 182259527666SDouglas Gregor << UmbrellaModule->getFullModuleName(); 1823322f633cSDouglas Gregor HadError = true; 18245257fc63SDouglas Gregor } else { 1825322f633cSDouglas Gregor // Record this umbrella header. 18262b63d15fSRichard Smith Map.setUmbrellaHeader(ActiveModule, File, RelativePathName.str()); 1827322f633cSDouglas Gregor } 1828feb54b6dSRichard Smith } else if (LeadingToken == MMToken::ExcludeKeyword) { 18290101b540SHans Wennborg Module::Header H = {RelativePathName.str(), File}; 18300101b540SHans Wennborg Map.excludeHeader(ActiveModule, H); 1831322f633cSDouglas Gregor } else { 183225d50758SRichard Smith // If there is a builtin counterpart to this file, add it now, before 183325d50758SRichard Smith // the "real" header, so we build the built-in one first when building 183425d50758SRichard Smith // the module. 18350101b540SHans Wennborg if (BuiltinFile) { 18363c1a41adSRichard Smith // FIXME: Taking the name from the FileEntry is unstable and can give 18373c1a41adSRichard Smith // different results depending on how we've previously named that file 18383c1a41adSRichard Smith // in this build. 18390101b540SHans Wennborg Module::Header H = { BuiltinFile->getName(), BuiltinFile }; 18400101b540SHans Wennborg Map.addHeader(ActiveModule, H, Role); 18410101b540SHans Wennborg } 184225d50758SRichard Smith 1843202210b3SRichard Smith // Record this header. 18440101b540SHans Wennborg Module::Header H = { RelativePathName.str(), File }; 18450101b540SHans Wennborg Map.addHeader(ActiveModule, H, Role); 18465257fc63SDouglas Gregor } 1847b53e5483SLawrence Crowl } else if (LeadingToken != MMToken::ExcludeKeyword) { 18484b27a64bSDouglas Gregor // Ignore excluded header files. They're optional anyway. 18494b27a64bSDouglas Gregor 18500761a8a0SDaniel Jasper // If we find a module that has a missing header, we mark this module as 18510761a8a0SDaniel Jasper // unavailable and store the header directive for displaying diagnostics. 18520761a8a0SDaniel Jasper Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword; 1853ec8c9752SBen Langmuir ActiveModule->markUnavailable(); 18540761a8a0SDaniel Jasper ActiveModule->MissingHeaders.push_back(Header); 18555257fc63SDouglas Gregor } 1856718292f2SDouglas Gregor } 1857718292f2SDouglas Gregor 1858524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration. 1859524e33e1SDouglas Gregor /// 1860524e33e1SDouglas Gregor /// umbrella-dir-declaration: 1861524e33e1SDouglas Gregor /// umbrella string-literal 1862524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1863524e33e1SDouglas Gregor // Parse the directory name. 1864524e33e1SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1865524e33e1SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1866524e33e1SDouglas Gregor << "umbrella"; 1867524e33e1SDouglas Gregor HadError = true; 1868524e33e1SDouglas Gregor return; 1869524e33e1SDouglas Gregor } 1870524e33e1SDouglas Gregor 1871524e33e1SDouglas Gregor std::string DirName = Tok.getString(); 1872524e33e1SDouglas Gregor SourceLocation DirNameLoc = consumeToken(); 1873524e33e1SDouglas Gregor 1874524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1875524e33e1SDouglas Gregor if (ActiveModule->Umbrella) { 1876524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1877524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1878524e33e1SDouglas Gregor HadError = true; 1879524e33e1SDouglas Gregor return; 1880524e33e1SDouglas Gregor } 1881524e33e1SDouglas Gregor 1882524e33e1SDouglas Gregor // Look for this file. 1883d2d442caSCraig Topper const DirectoryEntry *Dir = nullptr; 1884524e33e1SDouglas Gregor if (llvm::sys::path::is_absolute(DirName)) 1885524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(DirName); 1886524e33e1SDouglas Gregor else { 18872c1dd271SDylan Noblesmith SmallString<128> PathName; 1888524e33e1SDouglas Gregor PathName = Directory->getName(); 1889524e33e1SDouglas Gregor llvm::sys::path::append(PathName, DirName); 1890524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(PathName); 1891524e33e1SDouglas Gregor } 1892524e33e1SDouglas Gregor 1893524e33e1SDouglas Gregor if (!Dir) { 1894524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1895524e33e1SDouglas Gregor << DirName; 1896524e33e1SDouglas Gregor HadError = true; 1897524e33e1SDouglas Gregor return; 1898524e33e1SDouglas Gregor } 1899524e33e1SDouglas Gregor 1900*7ff29148SBen Langmuir if (UsesRequiresExcludedHack.count(ActiveModule)) { 1901*7ff29148SBen Langmuir // Mark this header 'textual' (see doc comment for 1902*7ff29148SBen Langmuir // ModuleMapParser::UsesRequiresExcludedHack). Although iterating over the 1903*7ff29148SBen Langmuir // directory is relatively expensive, in practice this only applies to the 1904*7ff29148SBen Langmuir // uncommonly used Tcl module on Darwin platforms. 1905*7ff29148SBen Langmuir std::error_code EC; 1906*7ff29148SBen Langmuir SmallVector<Module::Header, 6> Headers; 1907*7ff29148SBen Langmuir for (llvm::sys::fs::recursive_directory_iterator I(Dir->getName(), EC), E; 1908*7ff29148SBen Langmuir I != E && !EC; I.increment(EC)) { 1909*7ff29148SBen Langmuir if (const FileEntry *FE = SourceMgr.getFileManager().getFile(I->path())) { 1910*7ff29148SBen Langmuir 1911*7ff29148SBen Langmuir Module::Header Header = {I->path(), FE}; 1912*7ff29148SBen Langmuir Headers.push_back(std::move(Header)); 1913*7ff29148SBen Langmuir } 1914*7ff29148SBen Langmuir } 1915*7ff29148SBen Langmuir 1916*7ff29148SBen Langmuir // Sort header paths so that the pcm doesn't depend on iteration order. 1917*7ff29148SBen Langmuir llvm::array_pod_sort(Headers.begin(), Headers.end(), 1918*7ff29148SBen Langmuir [](const Module::Header *A, const Module::Header *B) { 1919*7ff29148SBen Langmuir return A->NameAsWritten.compare(B->NameAsWritten); 1920*7ff29148SBen Langmuir }); 1921*7ff29148SBen Langmuir for (auto &Header : Headers) 1922*7ff29148SBen Langmuir Map.addHeader(ActiveModule, std::move(Header), ModuleMap::TextualHeader); 1923*7ff29148SBen Langmuir return; 1924*7ff29148SBen Langmuir } 1925*7ff29148SBen Langmuir 1926524e33e1SDouglas Gregor if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1927524e33e1SDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1928524e33e1SDouglas Gregor << OwningModule->getFullModuleName(); 1929524e33e1SDouglas Gregor HadError = true; 1930524e33e1SDouglas Gregor return; 1931524e33e1SDouglas Gregor } 1932524e33e1SDouglas Gregor 1933524e33e1SDouglas Gregor // Record this umbrella directory. 19342b63d15fSRichard Smith Map.setUmbrellaDir(ActiveModule, Dir, DirName); 1935524e33e1SDouglas Gregor } 1936524e33e1SDouglas Gregor 19372b82c2a5SDouglas Gregor /// \brief Parse a module export declaration. 19382b82c2a5SDouglas Gregor /// 19392b82c2a5SDouglas Gregor /// export-declaration: 19402b82c2a5SDouglas Gregor /// 'export' wildcard-module-id 19412b82c2a5SDouglas Gregor /// 19422b82c2a5SDouglas Gregor /// wildcard-module-id: 19432b82c2a5SDouglas Gregor /// identifier 19442b82c2a5SDouglas Gregor /// '*' 19452b82c2a5SDouglas Gregor /// identifier '.' wildcard-module-id 19462b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() { 19472b82c2a5SDouglas Gregor assert(Tok.is(MMToken::ExportKeyword)); 19482b82c2a5SDouglas Gregor SourceLocation ExportLoc = consumeToken(); 19492b82c2a5SDouglas Gregor 19502b82c2a5SDouglas Gregor // Parse the module-id with an optional wildcard at the end. 19512b82c2a5SDouglas Gregor ModuleId ParsedModuleId; 19522b82c2a5SDouglas Gregor bool Wildcard = false; 19532b82c2a5SDouglas Gregor do { 1954306d8920SRichard Smith // FIXME: Support string-literal module names here. 19552b82c2a5SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 19562b82c2a5SDouglas Gregor ParsedModuleId.push_back(std::make_pair(Tok.getString(), 19572b82c2a5SDouglas Gregor Tok.getLocation())); 19582b82c2a5SDouglas Gregor consumeToken(); 19592b82c2a5SDouglas Gregor 19602b82c2a5SDouglas Gregor if (Tok.is(MMToken::Period)) { 19612b82c2a5SDouglas Gregor consumeToken(); 19622b82c2a5SDouglas Gregor continue; 19632b82c2a5SDouglas Gregor } 19642b82c2a5SDouglas Gregor 19652b82c2a5SDouglas Gregor break; 19662b82c2a5SDouglas Gregor } 19672b82c2a5SDouglas Gregor 19682b82c2a5SDouglas Gregor if(Tok.is(MMToken::Star)) { 19692b82c2a5SDouglas Gregor Wildcard = true; 1970f5eedd05SDouglas Gregor consumeToken(); 19712b82c2a5SDouglas Gregor break; 19722b82c2a5SDouglas Gregor } 19732b82c2a5SDouglas Gregor 1974ba7f2f71SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); 19752b82c2a5SDouglas Gregor HadError = true; 19762b82c2a5SDouglas Gregor return; 19772b82c2a5SDouglas Gregor } while (true); 19782b82c2a5SDouglas Gregor 19792b82c2a5SDouglas Gregor Module::UnresolvedExportDecl Unresolved = { 19802b82c2a5SDouglas Gregor ExportLoc, ParsedModuleId, Wildcard 19812b82c2a5SDouglas Gregor }; 19822b82c2a5SDouglas Gregor ActiveModule->UnresolvedExports.push_back(Unresolved); 19832b82c2a5SDouglas Gregor } 19842b82c2a5SDouglas Gregor 19858f4d3ff1SRichard Smith /// \brief Parse a module use declaration. 1986ba7f2f71SDaniel Jasper /// 19878f4d3ff1SRichard Smith /// use-declaration: 19888f4d3ff1SRichard Smith /// 'use' wildcard-module-id 1989ba7f2f71SDaniel Jasper void ModuleMapParser::parseUseDecl() { 1990ba7f2f71SDaniel Jasper assert(Tok.is(MMToken::UseKeyword)); 19918f4d3ff1SRichard Smith auto KWLoc = consumeToken(); 1992ba7f2f71SDaniel Jasper // Parse the module-id. 1993ba7f2f71SDaniel Jasper ModuleId ParsedModuleId; 19943cd34c76SDaniel Jasper parseModuleId(ParsedModuleId); 1995ba7f2f71SDaniel Jasper 19968f4d3ff1SRichard Smith if (ActiveModule->Parent) 19978f4d3ff1SRichard Smith Diags.Report(KWLoc, diag::err_mmap_use_decl_submodule); 19988f4d3ff1SRichard Smith else 1999ba7f2f71SDaniel Jasper ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId); 2000ba7f2f71SDaniel Jasper } 2001ba7f2f71SDaniel Jasper 20026ddfca91SDouglas Gregor /// \brief Parse a link declaration. 20036ddfca91SDouglas Gregor /// 20046ddfca91SDouglas Gregor /// module-declaration: 20056ddfca91SDouglas Gregor /// 'link' 'framework'[opt] string-literal 20066ddfca91SDouglas Gregor void ModuleMapParser::parseLinkDecl() { 20076ddfca91SDouglas Gregor assert(Tok.is(MMToken::LinkKeyword)); 20086ddfca91SDouglas Gregor SourceLocation LinkLoc = consumeToken(); 20096ddfca91SDouglas Gregor 20106ddfca91SDouglas Gregor // Parse the optional 'framework' keyword. 20116ddfca91SDouglas Gregor bool IsFramework = false; 20126ddfca91SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 20136ddfca91SDouglas Gregor consumeToken(); 20146ddfca91SDouglas Gregor IsFramework = true; 20156ddfca91SDouglas Gregor } 20166ddfca91SDouglas Gregor 20176ddfca91SDouglas Gregor // Parse the library name 20186ddfca91SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 20196ddfca91SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) 20206ddfca91SDouglas Gregor << IsFramework << SourceRange(LinkLoc); 20216ddfca91SDouglas Gregor HadError = true; 20226ddfca91SDouglas Gregor return; 20236ddfca91SDouglas Gregor } 20246ddfca91SDouglas Gregor 20256ddfca91SDouglas Gregor std::string LibraryName = Tok.getString(); 20266ddfca91SDouglas Gregor consumeToken(); 20276ddfca91SDouglas Gregor ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, 20286ddfca91SDouglas Gregor IsFramework)); 20296ddfca91SDouglas Gregor } 20306ddfca91SDouglas Gregor 203135b13eceSDouglas Gregor /// \brief Parse a configuration macro declaration. 203235b13eceSDouglas Gregor /// 203335b13eceSDouglas Gregor /// module-declaration: 203435b13eceSDouglas Gregor /// 'config_macros' attributes[opt] config-macro-list? 203535b13eceSDouglas Gregor /// 203635b13eceSDouglas Gregor /// config-macro-list: 203735b13eceSDouglas Gregor /// identifier (',' identifier)? 203835b13eceSDouglas Gregor void ModuleMapParser::parseConfigMacros() { 203935b13eceSDouglas Gregor assert(Tok.is(MMToken::ConfigMacros)); 204035b13eceSDouglas Gregor SourceLocation ConfigMacrosLoc = consumeToken(); 204135b13eceSDouglas Gregor 204235b13eceSDouglas Gregor // Only top-level modules can have configuration macros. 204335b13eceSDouglas Gregor if (ActiveModule->Parent) { 204435b13eceSDouglas Gregor Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule); 204535b13eceSDouglas Gregor } 204635b13eceSDouglas Gregor 204735b13eceSDouglas Gregor // Parse the optional attributes. 204835b13eceSDouglas Gregor Attributes Attrs; 204935b13eceSDouglas Gregor parseOptionalAttributes(Attrs); 205035b13eceSDouglas Gregor if (Attrs.IsExhaustive && !ActiveModule->Parent) { 205135b13eceSDouglas Gregor ActiveModule->ConfigMacrosExhaustive = true; 205235b13eceSDouglas Gregor } 205335b13eceSDouglas Gregor 205435b13eceSDouglas Gregor // If we don't have an identifier, we're done. 2055306d8920SRichard Smith // FIXME: Support macros with the same name as a keyword here. 205635b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) 205735b13eceSDouglas Gregor return; 205835b13eceSDouglas Gregor 205935b13eceSDouglas Gregor // Consume the first identifier. 206035b13eceSDouglas Gregor if (!ActiveModule->Parent) { 206135b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 206235b13eceSDouglas Gregor } 206335b13eceSDouglas Gregor consumeToken(); 206435b13eceSDouglas Gregor 206535b13eceSDouglas Gregor do { 206635b13eceSDouglas Gregor // If there's a comma, consume it. 206735b13eceSDouglas Gregor if (!Tok.is(MMToken::Comma)) 206835b13eceSDouglas Gregor break; 206935b13eceSDouglas Gregor consumeToken(); 207035b13eceSDouglas Gregor 207135b13eceSDouglas Gregor // We expect to see a macro name here. 2072306d8920SRichard Smith // FIXME: Support macros with the same name as a keyword here. 207335b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 207435b13eceSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro); 207535b13eceSDouglas Gregor break; 207635b13eceSDouglas Gregor } 207735b13eceSDouglas Gregor 207835b13eceSDouglas Gregor // Consume the macro name. 207935b13eceSDouglas Gregor if (!ActiveModule->Parent) { 208035b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 208135b13eceSDouglas Gregor } 208235b13eceSDouglas Gregor consumeToken(); 208335b13eceSDouglas Gregor } while (true); 208435b13eceSDouglas Gregor } 208535b13eceSDouglas Gregor 2086fb912657SDouglas Gregor /// \brief Format a module-id into a string. 2087fb912657SDouglas Gregor static std::string formatModuleId(const ModuleId &Id) { 2088fb912657SDouglas Gregor std::string result; 2089fb912657SDouglas Gregor { 2090fb912657SDouglas Gregor llvm::raw_string_ostream OS(result); 2091fb912657SDouglas Gregor 2092fb912657SDouglas Gregor for (unsigned I = 0, N = Id.size(); I != N; ++I) { 2093fb912657SDouglas Gregor if (I) 2094fb912657SDouglas Gregor OS << "."; 2095fb912657SDouglas Gregor OS << Id[I].first; 2096fb912657SDouglas Gregor } 2097fb912657SDouglas Gregor } 2098fb912657SDouglas Gregor 2099fb912657SDouglas Gregor return result; 2100fb912657SDouglas Gregor } 2101fb912657SDouglas Gregor 2102fb912657SDouglas Gregor /// \brief Parse a conflict declaration. 2103fb912657SDouglas Gregor /// 2104fb912657SDouglas Gregor /// module-declaration: 2105fb912657SDouglas Gregor /// 'conflict' module-id ',' string-literal 2106fb912657SDouglas Gregor void ModuleMapParser::parseConflict() { 2107fb912657SDouglas Gregor assert(Tok.is(MMToken::Conflict)); 2108fb912657SDouglas Gregor SourceLocation ConflictLoc = consumeToken(); 2109fb912657SDouglas Gregor Module::UnresolvedConflict Conflict; 2110fb912657SDouglas Gregor 2111fb912657SDouglas Gregor // Parse the module-id. 2112fb912657SDouglas Gregor if (parseModuleId(Conflict.Id)) 2113fb912657SDouglas Gregor return; 2114fb912657SDouglas Gregor 2115fb912657SDouglas Gregor // Parse the ','. 2116fb912657SDouglas Gregor if (!Tok.is(MMToken::Comma)) { 2117fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma) 2118fb912657SDouglas Gregor << SourceRange(ConflictLoc); 2119fb912657SDouglas Gregor return; 2120fb912657SDouglas Gregor } 2121fb912657SDouglas Gregor consumeToken(); 2122fb912657SDouglas Gregor 2123fb912657SDouglas Gregor // Parse the message. 2124fb912657SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 2125fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message) 2126fb912657SDouglas Gregor << formatModuleId(Conflict.Id); 2127fb912657SDouglas Gregor return; 2128fb912657SDouglas Gregor } 2129fb912657SDouglas Gregor Conflict.Message = Tok.getString().str(); 2130fb912657SDouglas Gregor consumeToken(); 2131fb912657SDouglas Gregor 2132fb912657SDouglas Gregor // Add this unresolved conflict. 2133fb912657SDouglas Gregor ActiveModule->UnresolvedConflicts.push_back(Conflict); 2134fb912657SDouglas Gregor } 2135fb912657SDouglas Gregor 21366ddfca91SDouglas Gregor /// \brief Parse an inferred module declaration (wildcard modules). 21379194a91dSDouglas Gregor /// 21389194a91dSDouglas Gregor /// module-declaration: 21399194a91dSDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] 21409194a91dSDouglas Gregor /// { inferred-module-member* } 21419194a91dSDouglas Gregor /// 21429194a91dSDouglas Gregor /// inferred-module-member: 21439194a91dSDouglas Gregor /// 'export' '*' 21449194a91dSDouglas Gregor /// 'exclude' identifier 21459194a91dSDouglas Gregor void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { 214673441091SDouglas Gregor assert(Tok.is(MMToken::Star)); 214773441091SDouglas Gregor SourceLocation StarLoc = consumeToken(); 214873441091SDouglas Gregor bool Failed = false; 214973441091SDouglas Gregor 215073441091SDouglas Gregor // Inferred modules must be submodules. 21519194a91dSDouglas Gregor if (!ActiveModule && !Framework) { 215273441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 215373441091SDouglas Gregor Failed = true; 215473441091SDouglas Gregor } 215573441091SDouglas Gregor 21569194a91dSDouglas Gregor if (ActiveModule) { 2157524e33e1SDouglas Gregor // Inferred modules must have umbrella directories. 21584898cde4SBen Langmuir if (!Failed && ActiveModule->IsAvailable && 21594898cde4SBen Langmuir !ActiveModule->getUmbrellaDir()) { 216073441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 216173441091SDouglas Gregor Failed = true; 216273441091SDouglas Gregor } 216373441091SDouglas Gregor 216473441091SDouglas Gregor // Check for redefinition of an inferred module. 2165dd005f69SDouglas Gregor if (!Failed && ActiveModule->InferSubmodules) { 216673441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 2167dd005f69SDouglas Gregor if (ActiveModule->InferredSubmoduleLoc.isValid()) 2168dd005f69SDouglas Gregor Diags.Report(ActiveModule->InferredSubmoduleLoc, 216973441091SDouglas Gregor diag::note_mmap_prev_definition); 217073441091SDouglas Gregor Failed = true; 217173441091SDouglas Gregor } 217273441091SDouglas Gregor 21739194a91dSDouglas Gregor // Check for the 'framework' keyword, which is not permitted here. 21749194a91dSDouglas Gregor if (Framework) { 21759194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); 21769194a91dSDouglas Gregor Framework = false; 21779194a91dSDouglas Gregor } 21789194a91dSDouglas Gregor } else if (Explicit) { 21799194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); 21809194a91dSDouglas Gregor Explicit = false; 21819194a91dSDouglas Gregor } 21829194a91dSDouglas Gregor 218373441091SDouglas Gregor // If there were any problems with this inferred submodule, skip its body. 218473441091SDouglas Gregor if (Failed) { 218573441091SDouglas Gregor if (Tok.is(MMToken::LBrace)) { 218673441091SDouglas Gregor consumeToken(); 218773441091SDouglas Gregor skipUntil(MMToken::RBrace); 218873441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 218973441091SDouglas Gregor consumeToken(); 219073441091SDouglas Gregor } 219173441091SDouglas Gregor HadError = true; 219273441091SDouglas Gregor return; 219373441091SDouglas Gregor } 219473441091SDouglas Gregor 21959194a91dSDouglas Gregor // Parse optional attributes. 21964442605fSBill Wendling Attributes Attrs; 21979194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 21989194a91dSDouglas Gregor 21999194a91dSDouglas Gregor if (ActiveModule) { 220073441091SDouglas Gregor // Note that we have an inferred submodule. 2201dd005f69SDouglas Gregor ActiveModule->InferSubmodules = true; 2202dd005f69SDouglas Gregor ActiveModule->InferredSubmoduleLoc = StarLoc; 2203dd005f69SDouglas Gregor ActiveModule->InferExplicitSubmodules = Explicit; 22049194a91dSDouglas Gregor } else { 22059194a91dSDouglas Gregor // We'll be inferring framework modules for this directory. 22069194a91dSDouglas Gregor Map.InferredDirectories[Directory].InferModules = true; 2207c1d88ea5SBen Langmuir Map.InferredDirectories[Directory].Attrs = Attrs; 2208beee15e7SBen Langmuir Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile; 2209131daca0SRichard Smith // FIXME: Handle the 'framework' keyword. 22109194a91dSDouglas Gregor } 221173441091SDouglas Gregor 221273441091SDouglas Gregor // Parse the opening brace. 221373441091SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 221473441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 221573441091SDouglas Gregor HadError = true; 221673441091SDouglas Gregor return; 221773441091SDouglas Gregor } 221873441091SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 221973441091SDouglas Gregor 222073441091SDouglas Gregor // Parse the body of the inferred submodule. 222173441091SDouglas Gregor bool Done = false; 222273441091SDouglas Gregor do { 222373441091SDouglas Gregor switch (Tok.Kind) { 222473441091SDouglas Gregor case MMToken::EndOfFile: 222573441091SDouglas Gregor case MMToken::RBrace: 222673441091SDouglas Gregor Done = true; 222773441091SDouglas Gregor break; 222873441091SDouglas Gregor 22299194a91dSDouglas Gregor case MMToken::ExcludeKeyword: { 22309194a91dSDouglas Gregor if (ActiveModule) { 22319194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2232d2d442caSCraig Topper << (ActiveModule != nullptr); 22339194a91dSDouglas Gregor consumeToken(); 22349194a91dSDouglas Gregor break; 22359194a91dSDouglas Gregor } 22369194a91dSDouglas Gregor 22379194a91dSDouglas Gregor consumeToken(); 2238306d8920SRichard Smith // FIXME: Support string-literal module names here. 22399194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 22409194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); 22419194a91dSDouglas Gregor break; 22429194a91dSDouglas Gregor } 22439194a91dSDouglas Gregor 22449194a91dSDouglas Gregor Map.InferredDirectories[Directory].ExcludedModules 22459194a91dSDouglas Gregor .push_back(Tok.getString()); 22469194a91dSDouglas Gregor consumeToken(); 22479194a91dSDouglas Gregor break; 22489194a91dSDouglas Gregor } 22499194a91dSDouglas Gregor 22509194a91dSDouglas Gregor case MMToken::ExportKeyword: 22519194a91dSDouglas Gregor if (!ActiveModule) { 22529194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2253d2d442caSCraig Topper << (ActiveModule != nullptr); 22549194a91dSDouglas Gregor consumeToken(); 22559194a91dSDouglas Gregor break; 22569194a91dSDouglas Gregor } 22579194a91dSDouglas Gregor 225873441091SDouglas Gregor consumeToken(); 225973441091SDouglas Gregor if (Tok.is(MMToken::Star)) 2260dd005f69SDouglas Gregor ActiveModule->InferExportWildcard = true; 226173441091SDouglas Gregor else 226273441091SDouglas Gregor Diags.Report(Tok.getLocation(), 226373441091SDouglas Gregor diag::err_mmap_expected_export_wildcard); 226473441091SDouglas Gregor consumeToken(); 226573441091SDouglas Gregor break; 226673441091SDouglas Gregor 226773441091SDouglas Gregor case MMToken::ExplicitKeyword: 226873441091SDouglas Gregor case MMToken::ModuleKeyword: 226973441091SDouglas Gregor case MMToken::HeaderKeyword: 2270b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 227173441091SDouglas Gregor case MMToken::UmbrellaKeyword: 227273441091SDouglas Gregor default: 22739194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2274d2d442caSCraig Topper << (ActiveModule != nullptr); 227573441091SDouglas Gregor consumeToken(); 227673441091SDouglas Gregor break; 227773441091SDouglas Gregor } 227873441091SDouglas Gregor } while (!Done); 227973441091SDouglas Gregor 228073441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 228173441091SDouglas Gregor consumeToken(); 228273441091SDouglas Gregor else { 228373441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 228473441091SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 228573441091SDouglas Gregor HadError = true; 228673441091SDouglas Gregor } 228773441091SDouglas Gregor } 228873441091SDouglas Gregor 22899194a91dSDouglas Gregor /// \brief Parse optional attributes. 22909194a91dSDouglas Gregor /// 22919194a91dSDouglas Gregor /// attributes: 22929194a91dSDouglas Gregor /// attribute attributes 22939194a91dSDouglas Gregor /// attribute 22949194a91dSDouglas Gregor /// 22959194a91dSDouglas Gregor /// attribute: 22969194a91dSDouglas Gregor /// [ identifier ] 22979194a91dSDouglas Gregor /// 22989194a91dSDouglas Gregor /// \param Attrs Will be filled in with the parsed attributes. 22999194a91dSDouglas Gregor /// 23009194a91dSDouglas Gregor /// \returns true if an error occurred, false otherwise. 23014442605fSBill Wendling bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { 23029194a91dSDouglas Gregor bool HadError = false; 23039194a91dSDouglas Gregor 23049194a91dSDouglas Gregor while (Tok.is(MMToken::LSquare)) { 23059194a91dSDouglas Gregor // Consume the '['. 23069194a91dSDouglas Gregor SourceLocation LSquareLoc = consumeToken(); 23079194a91dSDouglas Gregor 23089194a91dSDouglas Gregor // Check whether we have an attribute name here. 23099194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 23109194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 23119194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 23129194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 23139194a91dSDouglas Gregor consumeToken(); 23149194a91dSDouglas Gregor HadError = true; 23159194a91dSDouglas Gregor } 23169194a91dSDouglas Gregor 23179194a91dSDouglas Gregor // Decode the attribute name. 23189194a91dSDouglas Gregor AttributeKind Attribute 23199194a91dSDouglas Gregor = llvm::StringSwitch<AttributeKind>(Tok.getString()) 232035b13eceSDouglas Gregor .Case("exhaustive", AT_exhaustive) 232177944868SRichard Smith .Case("extern_c", AT_extern_c) 23229194a91dSDouglas Gregor .Case("system", AT_system) 23239194a91dSDouglas Gregor .Default(AT_unknown); 23249194a91dSDouglas Gregor switch (Attribute) { 23259194a91dSDouglas Gregor case AT_unknown: 23269194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 23279194a91dSDouglas Gregor << Tok.getString(); 23289194a91dSDouglas Gregor break; 23299194a91dSDouglas Gregor 23309194a91dSDouglas Gregor case AT_system: 23319194a91dSDouglas Gregor Attrs.IsSystem = true; 23329194a91dSDouglas Gregor break; 233335b13eceSDouglas Gregor 233477944868SRichard Smith case AT_extern_c: 233577944868SRichard Smith Attrs.IsExternC = true; 233677944868SRichard Smith break; 233777944868SRichard Smith 233835b13eceSDouglas Gregor case AT_exhaustive: 233935b13eceSDouglas Gregor Attrs.IsExhaustive = true; 234035b13eceSDouglas Gregor break; 23419194a91dSDouglas Gregor } 23429194a91dSDouglas Gregor consumeToken(); 23439194a91dSDouglas Gregor 23449194a91dSDouglas Gregor // Consume the ']'. 23459194a91dSDouglas Gregor if (!Tok.is(MMToken::RSquare)) { 23469194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 23479194a91dSDouglas Gregor Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 23489194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 23499194a91dSDouglas Gregor HadError = true; 23509194a91dSDouglas Gregor } 23519194a91dSDouglas Gregor 23529194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 23539194a91dSDouglas Gregor consumeToken(); 23549194a91dSDouglas Gregor } 23559194a91dSDouglas Gregor 23569194a91dSDouglas Gregor return HadError; 23579194a91dSDouglas Gregor } 23589194a91dSDouglas Gregor 2359718292f2SDouglas Gregor /// \brief Parse a module map file. 2360718292f2SDouglas Gregor /// 2361718292f2SDouglas Gregor /// module-map-file: 2362718292f2SDouglas Gregor /// module-declaration* 2363718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() { 2364718292f2SDouglas Gregor do { 2365718292f2SDouglas Gregor switch (Tok.Kind) { 2366718292f2SDouglas Gregor case MMToken::EndOfFile: 2367718292f2SDouglas Gregor return HadError; 2368718292f2SDouglas Gregor 2369e7ab3669SDouglas Gregor case MMToken::ExplicitKeyword: 237097292843SDaniel Jasper case MMToken::ExternKeyword: 2371718292f2SDouglas Gregor case MMToken::ModuleKeyword: 2372755b2055SDouglas Gregor case MMToken::FrameworkKeyword: 2373718292f2SDouglas Gregor parseModuleDecl(); 2374718292f2SDouglas Gregor break; 2375718292f2SDouglas Gregor 23761fb5c3a6SDouglas Gregor case MMToken::Comma: 237735b13eceSDouglas Gregor case MMToken::ConfigMacros: 2378fb912657SDouglas Gregor case MMToken::Conflict: 2379a3feee2aSRichard Smith case MMToken::Exclaim: 238059527666SDouglas Gregor case MMToken::ExcludeKeyword: 23812b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 2382718292f2SDouglas Gregor case MMToken::HeaderKeyword: 2383718292f2SDouglas Gregor case MMToken::Identifier: 2384718292f2SDouglas Gregor case MMToken::LBrace: 23856ddfca91SDouglas Gregor case MMToken::LinkKeyword: 2386a686e1b0SDouglas Gregor case MMToken::LSquare: 23872b82c2a5SDouglas Gregor case MMToken::Period: 2388b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 2389718292f2SDouglas Gregor case MMToken::RBrace: 2390a686e1b0SDouglas Gregor case MMToken::RSquare: 23911fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 23922b82c2a5SDouglas Gregor case MMToken::Star: 2393718292f2SDouglas Gregor case MMToken::StringLiteral: 2394b8afebe2SRichard Smith case MMToken::TextualKeyword: 2395718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 2396ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 2397718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 2398718292f2SDouglas Gregor HadError = true; 2399718292f2SDouglas Gregor consumeToken(); 2400718292f2SDouglas Gregor break; 2401718292f2SDouglas Gregor } 2402718292f2SDouglas Gregor } while (true); 2403718292f2SDouglas Gregor } 2404718292f2SDouglas Gregor 24059acb99e3SRichard Smith bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem, 2406ae6df27eSRichard Smith const DirectoryEntry *Dir, 2407ae6df27eSRichard Smith SourceLocation ExternModuleLoc) { 24084ddf2221SDouglas Gregor llvm::DenseMap<const FileEntry *, bool>::iterator Known 24094ddf2221SDouglas Gregor = ParsedModuleMap.find(File); 24104ddf2221SDouglas Gregor if (Known != ParsedModuleMap.end()) 24114ddf2221SDouglas Gregor return Known->second; 24124ddf2221SDouglas Gregor 2413d2d442caSCraig Topper assert(Target && "Missing target information"); 2414cb69b57bSBen Langmuir auto FileCharacter = IsSystem ? SrcMgr::C_System : SrcMgr::C_User; 2415ae6df27eSRichard Smith FileID ID = SourceMgr.createFileID(File, ExternModuleLoc, FileCharacter); 24161f76c4e8SManuel Klimek const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID); 2417718292f2SDouglas Gregor if (!Buffer) 24184ddf2221SDouglas Gregor return ParsedModuleMap[File] = true; 2419718292f2SDouglas Gregor 2420718292f2SDouglas Gregor // Parse this module map file. 24211f76c4e8SManuel Klimek Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts); 24222a6edb30SRichard Smith SourceLocation Start = L.getSourceLocation(); 2423beee15e7SBen Langmuir ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir, 2424963c5535SDouglas Gregor BuiltinIncludeDir, IsSystem); 2425718292f2SDouglas Gregor bool Result = Parser.parseModuleMapFile(); 24264ddf2221SDouglas Gregor ParsedModuleMap[File] = Result; 24272a6edb30SRichard Smith 24282a6edb30SRichard Smith // Notify callbacks that we parsed it. 24292a6edb30SRichard Smith for (const auto &Cb : Callbacks) 24302a6edb30SRichard Smith Cb->moduleMapFileRead(Start, *File, IsSystem); 2431718292f2SDouglas Gregor return Result; 2432718292f2SDouglas Gregor } 2433