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), 920414b857SRichard Smith CompilingModule(nullptr), SourceModule(nullptr) { 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); 1684eaf0a6cSDaniel Jasper if (Known == Headers.end() && File->getDir() == BuiltinIncludeDir && 1694eaf0a6cSDaniel Jasper isBuiltinHeader(llvm::sys::path::filename(File->getName()))) { 1704eaf0a6cSDaniel Jasper HeaderInfo.loadTopLevelSystemModules(); 17192669ee4SDaniel Jasper return Headers.find(File); 1724eaf0a6cSDaniel Jasper } 17392669ee4SDaniel Jasper return Known; 17492669ee4SDaniel Jasper } 17592669ee4SDaniel Jasper 1764469138eSBen Langmuir ModuleMap::KnownHeader 1774469138eSBen Langmuir ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File, 1784469138eSBen Langmuir SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs) { 1794469138eSBen Langmuir const DirectoryEntry *Dir = File->getDir(); 1804469138eSBen Langmuir assert(Dir && "file in no directory"); 1814469138eSBen Langmuir 1824469138eSBen Langmuir // Note: as an egregious but useful hack we use the real path here, because 1834469138eSBen Langmuir // frameworks moving from top-level frameworks to embedded frameworks tend 1844469138eSBen Langmuir // to be symlinked from the top-level location to the embedded location, 1854469138eSBen Langmuir // and we need to resolve lookups as if we had found the embedded location. 1864469138eSBen Langmuir StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir); 1874469138eSBen Langmuir 1884469138eSBen Langmuir // Keep walking up the directory hierarchy, looking for a directory with 1894469138eSBen Langmuir // an umbrella header. 1904469138eSBen Langmuir do { 1914469138eSBen Langmuir auto KnownDir = UmbrellaDirs.find(Dir); 1924469138eSBen Langmuir if (KnownDir != UmbrellaDirs.end()) 1934469138eSBen Langmuir return KnownHeader(KnownDir->second, NormalHeader); 1944469138eSBen Langmuir 1954469138eSBen Langmuir IntermediateDirs.push_back(Dir); 1964469138eSBen Langmuir 1974469138eSBen Langmuir // Retrieve our parent path. 1984469138eSBen Langmuir DirName = llvm::sys::path::parent_path(DirName); 1994469138eSBen Langmuir if (DirName.empty()) 2004469138eSBen Langmuir break; 2014469138eSBen Langmuir 2024469138eSBen Langmuir // Resolve the parent path to a directory entry. 2034469138eSBen Langmuir Dir = SourceMgr.getFileManager().getDirectory(DirName); 2044469138eSBen Langmuir } while (Dir); 2054469138eSBen Langmuir return KnownHeader(); 2064469138eSBen Langmuir } 2074469138eSBen Langmuir 208202210b3SRichard Smith // Returns true if RequestingModule directly uses RequestedModule. 20992669ee4SDaniel Jasper static bool directlyUses(const Module *RequestingModule, 21092669ee4SDaniel Jasper const Module *RequestedModule) { 211*b6ba46efSDaniel Jasper for (const Module* DirectUse : RequestingModule->DirectUses) { 212*b6ba46efSDaniel Jasper if (RequestedModule->isSubModuleOf(DirectUse)) 213*b6ba46efSDaniel Jasper return true; 214*b6ba46efSDaniel Jasper } 215*b6ba46efSDaniel Jasper return false; 21692669ee4SDaniel Jasper } 21792669ee4SDaniel Jasper 21892669ee4SDaniel Jasper static bool violatesPrivateInclude(Module *RequestingModule, 21992669ee4SDaniel Jasper const FileEntry *IncFileEnt, 22092669ee4SDaniel Jasper ModuleMap::ModuleHeaderRole Role, 22192669ee4SDaniel Jasper Module *RequestedModule) { 222202210b3SRichard Smith bool IsPrivateRole = Role & ModuleMap::PrivateHeader; 22392669ee4SDaniel Jasper #ifndef NDEBUG 2242708e520SRichard Smith if (IsPrivateRole) { 22592669ee4SDaniel Jasper // Check for consistency between the module header role 22692669ee4SDaniel Jasper // as obtained from the lookup and as obtained from the module. 22792669ee4SDaniel Jasper // This check is not cheap, so enable it only for debugging. 2282708e520SRichard Smith bool IsPrivate = false; 2292708e520SRichard Smith SmallVectorImpl<Module::Header> *HeaderList[] = { 2302708e520SRichard Smith &RequestedModule->Headers[Module::HK_Private], 2312708e520SRichard Smith &RequestedModule->Headers[Module::HK_PrivateTextual]}; 2322708e520SRichard Smith for (auto *Hs : HeaderList) 2332708e520SRichard Smith IsPrivate |= 2342708e520SRichard Smith std::find_if(Hs->begin(), Hs->end(), [&](const Module::Header &H) { 2353c1a41adSRichard Smith return H.Entry == IncFileEnt; 2362708e520SRichard Smith }) != Hs->end(); 2372708e520SRichard Smith assert((!IsPrivateRole || IsPrivate) && "inconsistent headers and roles"); 23800bc95ecSRichard Smith } 23992669ee4SDaniel Jasper #endif 240202210b3SRichard Smith return IsPrivateRole && 24192669ee4SDaniel Jasper RequestedModule->getTopLevelModule() != RequestingModule; 24292669ee4SDaniel Jasper } 24392669ee4SDaniel Jasper 24471e1a64fSBen Langmuir static Module *getTopLevelOrNull(Module *M) { 24571e1a64fSBen Langmuir return M ? M->getTopLevelModule() : nullptr; 24671e1a64fSBen Langmuir } 24771e1a64fSBen Langmuir 24892669ee4SDaniel Jasper void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule, 24992669ee4SDaniel Jasper SourceLocation FilenameLoc, 25092669ee4SDaniel Jasper StringRef Filename, 25192669ee4SDaniel Jasper const FileEntry *File) { 25292669ee4SDaniel Jasper // No errors for indirect modules. This may be a bit of a problem for modules 25392669ee4SDaniel Jasper // with no source files. 25471e1a64fSBen Langmuir if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule)) 25592669ee4SDaniel Jasper return; 25692669ee4SDaniel Jasper 25792669ee4SDaniel Jasper if (RequestingModule) 25892669ee4SDaniel Jasper resolveUses(RequestingModule, /*Complain=*/false); 25992669ee4SDaniel Jasper 26071e1a64fSBen Langmuir bool Excluded = false; 261d2d442caSCraig Topper Module *Private = nullptr; 262d2d442caSCraig Topper Module *NotUsed = nullptr; 26371e1a64fSBen Langmuir 26471e1a64fSBen Langmuir HeadersMap::iterator Known = findKnownHeader(File); 26571e1a64fSBen Langmuir if (Known != Headers.end()) { 26671e1a64fSBen Langmuir for (const KnownHeader &Header : Known->second) { 26792669ee4SDaniel Jasper // If 'File' is part of 'RequestingModule' we can definitely include it. 26871e1a64fSBen Langmuir if (Header.getModule() == RequestingModule) 26992669ee4SDaniel Jasper return; 27092669ee4SDaniel Jasper 27192669ee4SDaniel Jasper // Remember private headers for later printing of a diagnostic. 27271e1a64fSBen Langmuir if (violatesPrivateInclude(RequestingModule, File, Header.getRole(), 27371e1a64fSBen Langmuir Header.getModule())) { 27471e1a64fSBen Langmuir Private = Header.getModule(); 27592669ee4SDaniel Jasper continue; 27692669ee4SDaniel Jasper } 27792669ee4SDaniel Jasper 27892669ee4SDaniel Jasper // If uses need to be specified explicitly, we are only allowed to return 27992669ee4SDaniel Jasper // modules that are explicitly used by the requesting module. 28092669ee4SDaniel Jasper if (RequestingModule && LangOpts.ModulesDeclUse && 28171e1a64fSBen Langmuir !directlyUses(RequestingModule, Header.getModule())) { 28271e1a64fSBen Langmuir NotUsed = Header.getModule(); 28392669ee4SDaniel Jasper continue; 28492669ee4SDaniel Jasper } 28592669ee4SDaniel Jasper 28692669ee4SDaniel Jasper // We have found a module that we can happily use. 28792669ee4SDaniel Jasper return; 28892669ee4SDaniel Jasper } 289feb54b6dSRichard Smith 290feb54b6dSRichard Smith Excluded = true; 29171e1a64fSBen Langmuir } 29292669ee4SDaniel Jasper 29392669ee4SDaniel Jasper // We have found a header, but it is private. 294d2d442caSCraig Topper if (Private) { 29511152dd5SRichard Smith Diags.Report(FilenameLoc, diag::warn_use_of_private_header_outside_module) 29692669ee4SDaniel Jasper << Filename; 29792669ee4SDaniel Jasper return; 29892669ee4SDaniel Jasper } 29992669ee4SDaniel Jasper 30092669ee4SDaniel Jasper // We have found a module, but we don't use it. 301d2d442caSCraig Topper if (NotUsed) { 30211152dd5SRichard Smith Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module) 30392669ee4SDaniel Jasper << RequestingModule->getFullModuleName() << Filename; 30492669ee4SDaniel Jasper return; 30592669ee4SDaniel Jasper } 30692669ee4SDaniel Jasper 30771e1a64fSBen Langmuir if (Excluded || isHeaderInUmbrellaDirs(File)) 30871e1a64fSBen Langmuir return; 30971e1a64fSBen Langmuir 31071e1a64fSBen Langmuir // At this point, only non-modular includes remain. 31171e1a64fSBen Langmuir 31271e1a64fSBen Langmuir if (LangOpts.ModulesStrictDeclUse) { 31311152dd5SRichard Smith Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module) 31471e1a64fSBen Langmuir << RequestingModule->getFullModuleName() << Filename; 31571e1a64fSBen Langmuir } else if (RequestingModule) { 31671e1a64fSBen Langmuir diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ? 31771e1a64fSBen Langmuir diag::warn_non_modular_include_in_framework_module : 31871e1a64fSBen Langmuir diag::warn_non_modular_include_in_module; 31971e1a64fSBen Langmuir Diags.Report(FilenameLoc, DiagID) << RequestingModule->getFullModuleName(); 32071e1a64fSBen Langmuir } 32192669ee4SDaniel Jasper } 32292669ee4SDaniel Jasper 323ec87a50aSRichard Smith static bool isBetterKnownHeader(const ModuleMap::KnownHeader &New, 324ec87a50aSRichard Smith const ModuleMap::KnownHeader &Old) { 325ec87a50aSRichard Smith // Prefer a public header over a private header. 326ec87a50aSRichard Smith if ((New.getRole() & ModuleMap::PrivateHeader) != 327ec87a50aSRichard Smith (Old.getRole() & ModuleMap::PrivateHeader)) 328ec87a50aSRichard Smith return !(New.getRole() & ModuleMap::PrivateHeader); 329ec87a50aSRichard Smith 330ec87a50aSRichard Smith // Prefer a non-textual header over a textual header. 331ec87a50aSRichard Smith if ((New.getRole() & ModuleMap::TextualHeader) != 332ec87a50aSRichard Smith (Old.getRole() & ModuleMap::TextualHeader)) 333ec87a50aSRichard Smith return !(New.getRole() & ModuleMap::TextualHeader); 334ec87a50aSRichard Smith 335ec87a50aSRichard Smith // Don't have a reason to choose between these. Just keep the first one. 336ec87a50aSRichard Smith return false; 337ec87a50aSRichard Smith } 338ec87a50aSRichard Smith 33992669ee4SDaniel Jasper ModuleMap::KnownHeader 34092669ee4SDaniel Jasper ModuleMap::findModuleForHeader(const FileEntry *File, 341306d8920SRichard Smith Module *RequestingModule, 342306d8920SRichard Smith bool IncludeTextualHeaders) { 34392669ee4SDaniel Jasper HeadersMap::iterator Known = findKnownHeader(File); 3444eaf0a6cSDaniel Jasper 345306d8920SRichard Smith auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader { 346202210b3SRichard Smith if (!IncludeTextualHeaders && (R.getRole() & ModuleMap::TextualHeader)) 347306d8920SRichard Smith return ModuleMap::KnownHeader(); 348306d8920SRichard Smith return R; 349306d8920SRichard Smith }; 350306d8920SRichard Smith 3511fb5c3a6SDouglas Gregor if (Known != Headers.end()) { 352202210b3SRichard Smith ModuleMap::KnownHeader Result; 3531fb5c3a6SDouglas Gregor 35497da9178SDaniel Jasper // Iterate over all modules that 'File' is part of to find the best fit. 35597da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::iterator I = Known->second.begin(), 35697da9178SDaniel Jasper E = Known->second.end(); 35797da9178SDaniel Jasper I != E; ++I) { 3584eaf0a6cSDaniel Jasper // Cannot use a module if it is unavailable. 3594eaf0a6cSDaniel Jasper if (!I->getModule()->isAvailable()) 36097da9178SDaniel Jasper continue; 36197da9178SDaniel Jasper 36297da9178SDaniel Jasper // If 'File' is part of 'RequestingModule', 'RequestingModule' is the 36397da9178SDaniel Jasper // module we are looking for. 36497da9178SDaniel Jasper if (I->getModule() == RequestingModule) 365306d8920SRichard Smith return MakeResult(*I); 36697da9178SDaniel Jasper 36797da9178SDaniel Jasper // If uses need to be specified explicitly, we are only allowed to return 36897da9178SDaniel Jasper // modules that are explicitly used by the requesting module. 36997da9178SDaniel Jasper if (RequestingModule && LangOpts.ModulesDeclUse && 37092669ee4SDaniel Jasper !directlyUses(RequestingModule, I->getModule())) 37197da9178SDaniel Jasper continue; 3724eaf0a6cSDaniel Jasper 373ec87a50aSRichard Smith if (!Result || isBetterKnownHeader(*I, Result)) 37497da9178SDaniel Jasper Result = *I; 37597da9178SDaniel Jasper } 376306d8920SRichard Smith return MakeResult(Result); 3771fb5c3a6SDouglas Gregor } 378ab0c8a84SDouglas Gregor 379f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 3804469138eSBen Langmuir KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs); 3814469138eSBen Langmuir if (H) { 3824469138eSBen Langmuir Module *Result = H.getModule(); 383930a85ccSDouglas Gregor 384930a85ccSDouglas Gregor // Search up the module stack until we find a module with an umbrella 38573141fa9SDouglas Gregor // directory. 386930a85ccSDouglas Gregor Module *UmbrellaModule = Result; 38773141fa9SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 388930a85ccSDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 389930a85ccSDouglas Gregor 390930a85ccSDouglas Gregor if (UmbrellaModule->InferSubmodules) { 3919d6448b1SBen Langmuir const FileEntry *UmbrellaModuleMap = 3929d6448b1SBen Langmuir getModuleMapFileForUniquing(UmbrellaModule); 3939d6448b1SBen Langmuir 394a89c5ac4SDouglas Gregor // Infer submodules for each of the directories we found between 395a89c5ac4SDouglas Gregor // the directory of the umbrella header and the directory where 396a89c5ac4SDouglas Gregor // the actual header is located. 3979458f82dSDouglas Gregor bool Explicit = UmbrellaModule->InferExplicitSubmodules; 3989458f82dSDouglas Gregor 3997033127bSDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 400a89c5ac4SDouglas Gregor // Find or create the module that corresponds to this directory name. 401056396aeSDouglas Gregor SmallString<32> NameBuf; 402056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 4034469138eSBen Langmuir llvm::sys::path::stem(SkippedDirs[I-1]->getName()), NameBuf); 4049d6448b1SBen Langmuir Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 4059d6448b1SBen Langmuir Explicit).first; 4069d6448b1SBen Langmuir InferredModuleAllowedBy[Result] = UmbrellaModuleMap; 407ffbafa2aSBen Langmuir Result->IsInferred = true; 408a89c5ac4SDouglas Gregor 409a89c5ac4SDouglas Gregor // Associate the module and the directory. 410a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I-1]] = Result; 411a89c5ac4SDouglas Gregor 412a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 413a89c5ac4SDouglas Gregor // wildcard to the set of exports. 414930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 415d2d442caSCraig Topper Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 416a89c5ac4SDouglas Gregor } 417a89c5ac4SDouglas Gregor 418a89c5ac4SDouglas Gregor // Infer a submodule with the same name as this header file. 419056396aeSDouglas Gregor SmallString<32> NameBuf; 420056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 421056396aeSDouglas Gregor llvm::sys::path::stem(File->getName()), NameBuf); 4229d6448b1SBen Langmuir Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 4239d6448b1SBen Langmuir Explicit).first; 4249d6448b1SBen Langmuir InferredModuleAllowedBy[Result] = UmbrellaModuleMap; 425ffbafa2aSBen Langmuir Result->IsInferred = true; 4263c5305c1SArgyrios Kyrtzidis Result->addTopHeader(File); 427a89c5ac4SDouglas Gregor 428a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 429a89c5ac4SDouglas Gregor // wildcard to the set of exports. 430930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 431d2d442caSCraig Topper Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 432a89c5ac4SDouglas Gregor } else { 433a89c5ac4SDouglas Gregor // Record each of the directories we stepped through as being part of 434a89c5ac4SDouglas Gregor // the module we found, since the umbrella header covers them all. 435a89c5ac4SDouglas Gregor for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 436a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I]] = Result; 437a89c5ac4SDouglas Gregor } 438a89c5ac4SDouglas Gregor 43997da9178SDaniel Jasper Headers[File].push_back(KnownHeader(Result, NormalHeader)); 4401fb5c3a6SDouglas Gregor 4411fb5c3a6SDouglas Gregor // If a header corresponds to an unavailable module, don't report 4421fb5c3a6SDouglas Gregor // that it maps to anything. 4431fb5c3a6SDouglas Gregor if (!Result->isAvailable()) 444b53e5483SLawrence Crowl return KnownHeader(); 4451fb5c3a6SDouglas Gregor 446306d8920SRichard Smith return MakeResult(Headers[File].back()); 447a89c5ac4SDouglas Gregor } 448a89c5ac4SDouglas Gregor 449b53e5483SLawrence Crowl return KnownHeader(); 450ab0c8a84SDouglas Gregor } 451ab0c8a84SDouglas Gregor 452e4412640SArgyrios Kyrtzidis bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const { 453d2d442caSCraig Topper return isHeaderUnavailableInModule(Header, nullptr); 45450996ce1SRichard Smith } 45550996ce1SRichard Smith 45662bcd925SDmitri Gribenko bool 45762bcd925SDmitri Gribenko ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header, 45862bcd925SDmitri Gribenko const Module *RequestingModule) const { 459e4412640SArgyrios Kyrtzidis HeadersMap::const_iterator Known = Headers.find(Header); 46097da9178SDaniel Jasper if (Known != Headers.end()) { 46197da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::const_iterator 46297da9178SDaniel Jasper I = Known->second.begin(), 46397da9178SDaniel Jasper E = Known->second.end(); 46497da9178SDaniel Jasper I != E; ++I) { 46550996ce1SRichard Smith if (I->isAvailable() && (!RequestingModule || 46650996ce1SRichard Smith I->getModule()->isSubModuleOf(RequestingModule))) 46797da9178SDaniel Jasper return false; 46897da9178SDaniel Jasper } 46997da9178SDaniel Jasper return true; 47097da9178SDaniel Jasper } 4711fb5c3a6SDouglas Gregor 4721fb5c3a6SDouglas Gregor const DirectoryEntry *Dir = Header->getDir(); 473f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 4741fb5c3a6SDouglas Gregor StringRef DirName = Dir->getName(); 4751fb5c3a6SDouglas Gregor 47650996ce1SRichard Smith auto IsUnavailable = [&](const Module *M) { 47750996ce1SRichard Smith return !M->isAvailable() && (!RequestingModule || 47850996ce1SRichard Smith M->isSubModuleOf(RequestingModule)); 47950996ce1SRichard Smith }; 48050996ce1SRichard Smith 4811fb5c3a6SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 4821fb5c3a6SDouglas Gregor // an umbrella header. 4831fb5c3a6SDouglas Gregor do { 484e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir 4851fb5c3a6SDouglas Gregor = UmbrellaDirs.find(Dir); 4861fb5c3a6SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 4871fb5c3a6SDouglas Gregor Module *Found = KnownDir->second; 48850996ce1SRichard Smith if (IsUnavailable(Found)) 4891fb5c3a6SDouglas Gregor return true; 4901fb5c3a6SDouglas Gregor 4911fb5c3a6SDouglas Gregor // Search up the module stack until we find a module with an umbrella 4921fb5c3a6SDouglas Gregor // directory. 4931fb5c3a6SDouglas Gregor Module *UmbrellaModule = Found; 4941fb5c3a6SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 4951fb5c3a6SDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 4961fb5c3a6SDouglas Gregor 4971fb5c3a6SDouglas Gregor if (UmbrellaModule->InferSubmodules) { 4981fb5c3a6SDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 4991fb5c3a6SDouglas Gregor // Find or create the module that corresponds to this directory name. 500056396aeSDouglas Gregor SmallString<32> NameBuf; 501056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 502056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 503056396aeSDouglas Gregor NameBuf); 5041fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 5051fb5c3a6SDouglas Gregor if (!Found) 5061fb5c3a6SDouglas Gregor return false; 50750996ce1SRichard Smith if (IsUnavailable(Found)) 5081fb5c3a6SDouglas Gregor return true; 5091fb5c3a6SDouglas Gregor } 5101fb5c3a6SDouglas Gregor 5111fb5c3a6SDouglas Gregor // Infer a submodule with the same name as this header file. 512056396aeSDouglas Gregor SmallString<32> NameBuf; 513056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 514056396aeSDouglas Gregor llvm::sys::path::stem(Header->getName()), 515056396aeSDouglas Gregor NameBuf); 5161fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 5171fb5c3a6SDouglas Gregor if (!Found) 5181fb5c3a6SDouglas Gregor return false; 5191fb5c3a6SDouglas Gregor } 5201fb5c3a6SDouglas Gregor 52150996ce1SRichard Smith return IsUnavailable(Found); 5221fb5c3a6SDouglas Gregor } 5231fb5c3a6SDouglas Gregor 5241fb5c3a6SDouglas Gregor SkippedDirs.push_back(Dir); 5251fb5c3a6SDouglas Gregor 5261fb5c3a6SDouglas Gregor // Retrieve our parent path. 5271fb5c3a6SDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 5281fb5c3a6SDouglas Gregor if (DirName.empty()) 5291fb5c3a6SDouglas Gregor break; 5301fb5c3a6SDouglas Gregor 5311fb5c3a6SDouglas Gregor // Resolve the parent path to a directory entry. 5321f76c4e8SManuel Klimek Dir = SourceMgr.getFileManager().getDirectory(DirName); 5331fb5c3a6SDouglas Gregor } while (Dir); 5341fb5c3a6SDouglas Gregor 5351fb5c3a6SDouglas Gregor return false; 5361fb5c3a6SDouglas Gregor } 5371fb5c3a6SDouglas Gregor 538e4412640SArgyrios Kyrtzidis Module *ModuleMap::findModule(StringRef Name) const { 539e4412640SArgyrios Kyrtzidis llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name); 54088bdfb0eSDouglas Gregor if (Known != Modules.end()) 54188bdfb0eSDouglas Gregor return Known->getValue(); 54288bdfb0eSDouglas Gregor 543d2d442caSCraig Topper return nullptr; 54488bdfb0eSDouglas Gregor } 54588bdfb0eSDouglas Gregor 546e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleUnqualified(StringRef Name, 547e4412640SArgyrios Kyrtzidis Module *Context) const { 5482b82c2a5SDouglas Gregor for(; Context; Context = Context->Parent) { 5492b82c2a5SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Context)) 5502b82c2a5SDouglas Gregor return Sub; 5512b82c2a5SDouglas Gregor } 5522b82c2a5SDouglas Gregor 5532b82c2a5SDouglas Gregor return findModule(Name); 5542b82c2a5SDouglas Gregor } 5552b82c2a5SDouglas Gregor 556e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{ 5572b82c2a5SDouglas Gregor if (!Context) 5582b82c2a5SDouglas Gregor return findModule(Name); 5592b82c2a5SDouglas Gregor 560eb90e830SDouglas Gregor return Context->findSubmodule(Name); 5612b82c2a5SDouglas Gregor } 5622b82c2a5SDouglas Gregor 563de3ef502SDouglas Gregor std::pair<Module *, bool> 5649d6448b1SBen Langmuir ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 56569021974SDouglas Gregor bool IsExplicit) { 56669021974SDouglas Gregor // Try to find an existing module with this name. 567eb90e830SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Parent)) 568eb90e830SDouglas Gregor return std::make_pair(Sub, false); 56969021974SDouglas Gregor 57069021974SDouglas Gregor // Create a new module with this name. 5719d6448b1SBen Langmuir Module *Result = new Module(Name, SourceLocation(), Parent, 572beee15e7SBen Langmuir IsFramework, IsExplicit); 573ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == Name) { 574ba7f2f71SDaniel Jasper SourceModule = Result; 575ba7f2f71SDaniel Jasper SourceModuleName = Name; 576ba7f2f71SDaniel Jasper } 5776f722b4eSArgyrios Kyrtzidis if (!Parent) { 57869021974SDouglas Gregor Modules[Name] = Result; 5796f722b4eSArgyrios Kyrtzidis if (!LangOpts.CurrentModule.empty() && !CompilingModule && 5806f722b4eSArgyrios Kyrtzidis Name == LangOpts.CurrentModule) { 5816f722b4eSArgyrios Kyrtzidis CompilingModule = Result; 5826f722b4eSArgyrios Kyrtzidis } 5836f722b4eSArgyrios Kyrtzidis } 58469021974SDouglas Gregor return std::make_pair(Result, true); 58569021974SDouglas Gregor } 58669021974SDouglas Gregor 58711dfe6feSDouglas Gregor /// \brief For a framework module, infer the framework against which we 58811dfe6feSDouglas Gregor /// should link. 58911dfe6feSDouglas Gregor static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir, 59011dfe6feSDouglas Gregor FileManager &FileMgr) { 59111dfe6feSDouglas Gregor assert(Mod->IsFramework && "Can only infer linking for framework modules"); 59211dfe6feSDouglas Gregor assert(!Mod->isSubFramework() && 59311dfe6feSDouglas Gregor "Can only infer linking for top-level frameworks"); 59411dfe6feSDouglas Gregor 59511dfe6feSDouglas Gregor SmallString<128> LibName; 59611dfe6feSDouglas Gregor LibName += FrameworkDir->getName(); 59711dfe6feSDouglas Gregor llvm::sys::path::append(LibName, Mod->Name); 59811dfe6feSDouglas Gregor if (FileMgr.getFile(LibName)) { 59911dfe6feSDouglas Gregor Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name, 60011dfe6feSDouglas Gregor /*IsFramework=*/true)); 60111dfe6feSDouglas Gregor } 60211dfe6feSDouglas Gregor } 60311dfe6feSDouglas Gregor 604de3ef502SDouglas Gregor Module * 60556c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName, 606e89dbc1dSDouglas Gregor const DirectoryEntry *FrameworkDir, 607a686e1b0SDouglas Gregor bool IsSystem, 608e89dbc1dSDouglas Gregor Module *Parent) { 609c1d88ea5SBen Langmuir Attributes Attrs; 610c1d88ea5SBen Langmuir Attrs.IsSystem = IsSystem; 611c1d88ea5SBen Langmuir return inferFrameworkModule(ModuleName, FrameworkDir, Attrs, Parent); 612c1d88ea5SBen Langmuir } 613c1d88ea5SBen Langmuir 614c1d88ea5SBen Langmuir Module *ModuleMap::inferFrameworkModule(StringRef ModuleName, 615c1d88ea5SBen Langmuir const DirectoryEntry *FrameworkDir, 616c1d88ea5SBen Langmuir Attributes Attrs, Module *Parent) { 617c1d88ea5SBen Langmuir 61856c64013SDouglas Gregor // Check whether we've already found this module. 619e89dbc1dSDouglas Gregor if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 620e89dbc1dSDouglas Gregor return Mod; 621e89dbc1dSDouglas Gregor 6221f76c4e8SManuel Klimek FileManager &FileMgr = SourceMgr.getFileManager(); 62356c64013SDouglas Gregor 6249194a91dSDouglas Gregor // If the framework has a parent path from which we're allowed to infer 6259194a91dSDouglas Gregor // a framework module, do so. 626beee15e7SBen Langmuir const FileEntry *ModuleMapFile = nullptr; 6279194a91dSDouglas Gregor if (!Parent) { 6284ddf2221SDouglas Gregor // Determine whether we're allowed to infer a module map. 629e00c8b20SDouglas Gregor 6304ddf2221SDouglas Gregor // Note: as an egregious but useful hack we use the real path here, because 6314ddf2221SDouglas Gregor // we might be looking at an embedded framework that symlinks out to a 6324ddf2221SDouglas Gregor // top-level framework, and we need to infer as if we were naming the 6334ddf2221SDouglas Gregor // top-level framework. 634e00c8b20SDouglas Gregor StringRef FrameworkDirName 6351f76c4e8SManuel Klimek = SourceMgr.getFileManager().getCanonicalName(FrameworkDir); 6364ddf2221SDouglas Gregor 6376b7f7345SBen Langmuir // In case this is a case-insensitive filesystem, make sure the canonical 6386b7f7345SBen Langmuir // directory name matches ModuleName exactly. Modules are case-sensitive. 6396b7f7345SBen Langmuir // FIXME: we should be able to give a fix-it hint for the correct spelling. 6406b7f7345SBen Langmuir if (llvm::sys::path::stem(FrameworkDirName) != ModuleName) 6416b7f7345SBen Langmuir return nullptr; 6426b7f7345SBen Langmuir 6439194a91dSDouglas Gregor bool canInfer = false; 6444ddf2221SDouglas Gregor if (llvm::sys::path::has_parent_path(FrameworkDirName)) { 6459194a91dSDouglas Gregor // Figure out the parent path. 6464ddf2221SDouglas Gregor StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); 6479194a91dSDouglas Gregor if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) { 6489194a91dSDouglas Gregor // Check whether we have already looked into the parent directory 6499194a91dSDouglas Gregor // for a module map. 650e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 6519194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 6529194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) { 6539194a91dSDouglas Gregor // We haven't looked here before. Load a module map, if there is 6549194a91dSDouglas Gregor // one. 655984e1df7SBen Langmuir bool IsFrameworkDir = Parent.endswith(".framework"); 656984e1df7SBen Langmuir if (const FileEntry *ModMapFile = 657984e1df7SBen Langmuir HeaderInfo.lookupModuleMapFile(ParentDir, IsFrameworkDir)) { 658c1d88ea5SBen Langmuir parseModuleMapFile(ModMapFile, Attrs.IsSystem, ParentDir); 6599194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 6609194a91dSDouglas Gregor } 6619194a91dSDouglas Gregor 6629194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) 6639194a91dSDouglas Gregor inferred = InferredDirectories.insert( 6649194a91dSDouglas Gregor std::make_pair(ParentDir, InferredDirectory())).first; 6659194a91dSDouglas Gregor } 6669194a91dSDouglas Gregor 6679194a91dSDouglas Gregor if (inferred->second.InferModules) { 6689194a91dSDouglas Gregor // We're allowed to infer for this directory, but make sure it's okay 6699194a91dSDouglas Gregor // to infer this particular module. 6704ddf2221SDouglas Gregor StringRef Name = llvm::sys::path::stem(FrameworkDirName); 6719194a91dSDouglas Gregor canInfer = std::find(inferred->second.ExcludedModules.begin(), 6729194a91dSDouglas Gregor inferred->second.ExcludedModules.end(), 6739194a91dSDouglas Gregor Name) == inferred->second.ExcludedModules.end(); 6749194a91dSDouglas Gregor 675c1d88ea5SBen Langmuir Attrs.IsSystem |= inferred->second.Attrs.IsSystem; 676c1d88ea5SBen Langmuir Attrs.IsExternC |= inferred->second.Attrs.IsExternC; 677c1d88ea5SBen Langmuir Attrs.IsExhaustive |= inferred->second.Attrs.IsExhaustive; 678beee15e7SBen Langmuir ModuleMapFile = inferred->second.ModuleMapFile; 6799194a91dSDouglas Gregor } 6809194a91dSDouglas Gregor } 6819194a91dSDouglas Gregor } 6829194a91dSDouglas Gregor 6839194a91dSDouglas Gregor // If we're not allowed to infer a framework module, don't. 6849194a91dSDouglas Gregor if (!canInfer) 685d2d442caSCraig Topper return nullptr; 686beee15e7SBen Langmuir } else 6879d6448b1SBen Langmuir ModuleMapFile = getModuleMapFileForUniquing(Parent); 6889194a91dSDouglas Gregor 6899194a91dSDouglas Gregor 69056c64013SDouglas Gregor // Look for an umbrella header. 6912c1dd271SDylan Noblesmith SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 69217381a06SBenjamin Kramer llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h"); 693e89dbc1dSDouglas Gregor const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 69456c64013SDouglas Gregor 69556c64013SDouglas Gregor // FIXME: If there's no umbrella header, we could probably scan the 69656c64013SDouglas Gregor // framework to load *everything*. But, it's not clear that this is a good 69756c64013SDouglas Gregor // idea. 69856c64013SDouglas Gregor if (!UmbrellaHeader) 699d2d442caSCraig Topper return nullptr; 70056c64013SDouglas Gregor 7019d6448b1SBen Langmuir Module *Result = new Module(ModuleName, SourceLocation(), Parent, 702e89dbc1dSDouglas Gregor /*IsFramework=*/true, /*IsExplicit=*/false); 7039d6448b1SBen Langmuir InferredModuleAllowedBy[Result] = ModuleMapFile; 7049d6448b1SBen Langmuir Result->IsInferred = true; 705ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == ModuleName) { 706ba7f2f71SDaniel Jasper SourceModule = Result; 707ba7f2f71SDaniel Jasper SourceModuleName = ModuleName; 708ba7f2f71SDaniel Jasper } 709c1d88ea5SBen Langmuir 710c1d88ea5SBen Langmuir Result->IsSystem |= Attrs.IsSystem; 711c1d88ea5SBen Langmuir Result->IsExternC |= Attrs.IsExternC; 712c1d88ea5SBen Langmuir Result->ConfigMacrosExhaustive |= Attrs.IsExhaustive; 713a686e1b0SDouglas Gregor 714eb90e830SDouglas Gregor if (!Parent) 715e89dbc1dSDouglas Gregor Modules[ModuleName] = Result; 716e89dbc1dSDouglas Gregor 717322f633cSDouglas Gregor // umbrella header "umbrella-header-name" 71873141fa9SDouglas Gregor Result->Umbrella = UmbrellaHeader; 71997da9178SDaniel Jasper Headers[UmbrellaHeader].push_back(KnownHeader(Result, NormalHeader)); 7204dc71835SDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Result; 721d8bd7537SDouglas Gregor 722d8bd7537SDouglas Gregor // export * 723d2d442caSCraig Topper Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 724d8bd7537SDouglas Gregor 725a89c5ac4SDouglas Gregor // module * { export * } 726a89c5ac4SDouglas Gregor Result->InferSubmodules = true; 727a89c5ac4SDouglas Gregor Result->InferExportWildcard = true; 728a89c5ac4SDouglas Gregor 729e89dbc1dSDouglas Gregor // Look for subframeworks. 730c080917eSRafael Espindola std::error_code EC; 7312c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirName 732ddaa69cbSDouglas Gregor = StringRef(FrameworkDir->getName()); 733e89dbc1dSDouglas Gregor llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 7342d4d8cb3SBenjamin Kramer llvm::sys::path::native(SubframeworksDirName); 735ddaa69cbSDouglas Gregor for (llvm::sys::fs::directory_iterator 7362d4d8cb3SBenjamin Kramer Dir(SubframeworksDirName.str(), EC), DirEnd; 737e89dbc1dSDouglas Gregor Dir != DirEnd && !EC; Dir.increment(EC)) { 738e89dbc1dSDouglas Gregor if (!StringRef(Dir->path()).endswith(".framework")) 739e89dbc1dSDouglas Gregor continue; 740f2161a70SDouglas Gregor 741e89dbc1dSDouglas Gregor if (const DirectoryEntry *SubframeworkDir 742e89dbc1dSDouglas Gregor = FileMgr.getDirectory(Dir->path())) { 74307c22b78SDouglas Gregor // Note: as an egregious but useful hack, we use the real path here and 74407c22b78SDouglas Gregor // check whether it is actually a subdirectory of the parent directory. 74507c22b78SDouglas Gregor // This will not be the case if the 'subframework' is actually a symlink 74607c22b78SDouglas Gregor // out to a top-level framework. 747e00c8b20SDouglas Gregor StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir); 74807c22b78SDouglas Gregor bool FoundParent = false; 74907c22b78SDouglas Gregor do { 75007c22b78SDouglas Gregor // Get the parent directory name. 75107c22b78SDouglas Gregor SubframeworkDirName 75207c22b78SDouglas Gregor = llvm::sys::path::parent_path(SubframeworkDirName); 75307c22b78SDouglas Gregor if (SubframeworkDirName.empty()) 75407c22b78SDouglas Gregor break; 75507c22b78SDouglas Gregor 75607c22b78SDouglas Gregor if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { 75707c22b78SDouglas Gregor FoundParent = true; 75807c22b78SDouglas Gregor break; 75907c22b78SDouglas Gregor } 76007c22b78SDouglas Gregor } while (true); 76107c22b78SDouglas Gregor 76207c22b78SDouglas Gregor if (!FoundParent) 76307c22b78SDouglas Gregor continue; 76407c22b78SDouglas Gregor 765e89dbc1dSDouglas Gregor // FIXME: Do we want to warn about subframeworks without umbrella headers? 766056396aeSDouglas Gregor SmallString<32> NameBuf; 767056396aeSDouglas Gregor inferFrameworkModule(sanitizeFilenameAsIdentifier( 768056396aeSDouglas Gregor llvm::sys::path::stem(Dir->path()), NameBuf), 769c1d88ea5SBen Langmuir SubframeworkDir, Attrs, Result); 770e89dbc1dSDouglas Gregor } 771e89dbc1dSDouglas Gregor } 772e89dbc1dSDouglas Gregor 77311dfe6feSDouglas Gregor // If the module is a top-level framework, automatically link against the 77411dfe6feSDouglas Gregor // framework. 77511dfe6feSDouglas Gregor if (!Result->isSubFramework()) { 77611dfe6feSDouglas Gregor inferFrameworkLink(Result, FrameworkDir, FileMgr); 77711dfe6feSDouglas Gregor } 77811dfe6feSDouglas Gregor 77956c64013SDouglas Gregor return Result; 78056c64013SDouglas Gregor } 78156c64013SDouglas Gregor 782a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ 78397da9178SDaniel Jasper Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader)); 78473141fa9SDouglas Gregor Mod->Umbrella = UmbrellaHeader; 7857033127bSDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; 786a89c5ac4SDouglas Gregor } 787a89c5ac4SDouglas Gregor 788524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) { 789524e33e1SDouglas Gregor Mod->Umbrella = UmbrellaDir; 790524e33e1SDouglas Gregor UmbrellaDirs[UmbrellaDir] = Mod; 791524e33e1SDouglas Gregor } 792524e33e1SDouglas Gregor 7933c1a41adSRichard Smith static Module::HeaderKind headerRoleToKind(ModuleMap::ModuleHeaderRole Role) { 7940e98d938SNAKAMURA Takumi switch ((int)Role) { 7953c1a41adSRichard Smith default: llvm_unreachable("unknown header role"); 7963c1a41adSRichard Smith case ModuleMap::NormalHeader: 7973c1a41adSRichard Smith return Module::HK_Normal; 7983c1a41adSRichard Smith case ModuleMap::PrivateHeader: 7993c1a41adSRichard Smith return Module::HK_Private; 8003c1a41adSRichard Smith case ModuleMap::TextualHeader: 8013c1a41adSRichard Smith return Module::HK_Textual; 8023c1a41adSRichard Smith case ModuleMap::PrivateHeader | ModuleMap::TextualHeader: 8033c1a41adSRichard Smith return Module::HK_PrivateTextual; 8043c1a41adSRichard Smith } 8050e98d938SNAKAMURA Takumi } 806202210b3SRichard Smith 8073c1a41adSRichard Smith void ModuleMap::addHeader(Module *Mod, Module::Header Header, 8083c1a41adSRichard Smith ModuleHeaderRole Role) { 809202210b3SRichard Smith if (!(Role & TextualHeader)) { 8106f722b4eSArgyrios Kyrtzidis bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule; 8113c1a41adSRichard Smith HeaderInfo.MarkFileModuleHeader(Header.Entry, Role, 8123c1a41adSRichard Smith isCompilingModuleHeader); 813b146baabSArgyrios Kyrtzidis } 8143c1a41adSRichard Smith Headers[Header.Entry].push_back(KnownHeader(Mod, Role)); 8153c1a41adSRichard Smith 8163c1a41adSRichard Smith Mod->Headers[headerRoleToKind(Role)].push_back(std::move(Header)); 817a89c5ac4SDouglas Gregor } 818a89c5ac4SDouglas Gregor 8193c1a41adSRichard Smith void ModuleMap::excludeHeader(Module *Mod, Module::Header Header) { 820feb54b6dSRichard Smith // Add this as a known header so we won't implicitly add it to any 821feb54b6dSRichard Smith // umbrella directory module. 822feb54b6dSRichard Smith // FIXME: Should we only exclude it from umbrella modules within the 823feb54b6dSRichard Smith // specified module? 8243c1a41adSRichard Smith (void) Headers[Header.Entry]; 8253c1a41adSRichard Smith 8263c1a41adSRichard Smith Mod->Headers[Module::HK_Excluded].push_back(std::move(Header)); 827feb54b6dSRichard Smith } 828feb54b6dSRichard Smith 829514b636aSDouglas Gregor const FileEntry * 8304b8a9e95SBen Langmuir ModuleMap::getContainingModuleMapFile(const Module *Module) const { 8311f76c4e8SManuel Klimek if (Module->DefinitionLoc.isInvalid()) 832d2d442caSCraig Topper return nullptr; 833514b636aSDouglas Gregor 8341f76c4e8SManuel Klimek return SourceMgr.getFileEntryForID( 8351f76c4e8SManuel Klimek SourceMgr.getFileID(Module->DefinitionLoc)); 836514b636aSDouglas Gregor } 837514b636aSDouglas Gregor 8384b8a9e95SBen Langmuir const FileEntry *ModuleMap::getModuleMapFileForUniquing(const Module *M) const { 8399d6448b1SBen Langmuir if (M->IsInferred) { 8409d6448b1SBen Langmuir assert(InferredModuleAllowedBy.count(M) && "missing inferred module map"); 8419d6448b1SBen Langmuir return InferredModuleAllowedBy.find(M)->second; 8429d6448b1SBen Langmuir } 8439d6448b1SBen Langmuir return getContainingModuleMapFile(M); 8449d6448b1SBen Langmuir } 8459d6448b1SBen Langmuir 8469d6448b1SBen Langmuir void ModuleMap::setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap) { 8479d6448b1SBen Langmuir assert(M->IsInferred && "module not inferred"); 8489d6448b1SBen Langmuir InferredModuleAllowedBy[M] = ModMap; 8499d6448b1SBen Langmuir } 8509d6448b1SBen Langmuir 851718292f2SDouglas Gregor void ModuleMap::dump() { 852718292f2SDouglas Gregor llvm::errs() << "Modules:"; 853718292f2SDouglas Gregor for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 854718292f2SDouglas Gregor MEnd = Modules.end(); 855718292f2SDouglas Gregor M != MEnd; ++M) 856d28d1b8dSDouglas Gregor M->getValue()->print(llvm::errs(), 2); 857718292f2SDouglas Gregor 858718292f2SDouglas Gregor llvm::errs() << "Headers:"; 85959527666SDouglas Gregor for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 860718292f2SDouglas Gregor H != HEnd; ++H) { 86197da9178SDaniel Jasper llvm::errs() << " \"" << H->first->getName() << "\" -> "; 86297da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(), 86397da9178SDaniel Jasper E = H->second.end(); 86497da9178SDaniel Jasper I != E; ++I) { 86597da9178SDaniel Jasper if (I != H->second.begin()) 86697da9178SDaniel Jasper llvm::errs() << ","; 86797da9178SDaniel Jasper llvm::errs() << I->getModule()->getFullModuleName(); 86897da9178SDaniel Jasper } 86997da9178SDaniel Jasper llvm::errs() << "\n"; 870718292f2SDouglas Gregor } 871718292f2SDouglas Gregor } 872718292f2SDouglas Gregor 8732b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 8742b82c2a5SDouglas Gregor bool HadError = false; 8752b82c2a5SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { 8762b82c2a5SDouglas Gregor Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 8772b82c2a5SDouglas Gregor Complain); 878f5eedd05SDouglas Gregor if (Export.getPointer() || Export.getInt()) 8792b82c2a5SDouglas Gregor Mod->Exports.push_back(Export); 8802b82c2a5SDouglas Gregor else 8812b82c2a5SDouglas Gregor HadError = true; 8822b82c2a5SDouglas Gregor } 8832b82c2a5SDouglas Gregor Mod->UnresolvedExports.clear(); 8842b82c2a5SDouglas Gregor return HadError; 8852b82c2a5SDouglas Gregor } 8862b82c2a5SDouglas Gregor 887ba7f2f71SDaniel Jasper bool ModuleMap::resolveUses(Module *Mod, bool Complain) { 888ba7f2f71SDaniel Jasper bool HadError = false; 889ba7f2f71SDaniel Jasper for (unsigned I = 0, N = Mod->UnresolvedDirectUses.size(); I != N; ++I) { 890ba7f2f71SDaniel Jasper Module *DirectUse = 891ba7f2f71SDaniel Jasper resolveModuleId(Mod->UnresolvedDirectUses[I], Mod, Complain); 892ba7f2f71SDaniel Jasper if (DirectUse) 893ba7f2f71SDaniel Jasper Mod->DirectUses.push_back(DirectUse); 894ba7f2f71SDaniel Jasper else 895ba7f2f71SDaniel Jasper HadError = true; 896ba7f2f71SDaniel Jasper } 897ba7f2f71SDaniel Jasper Mod->UnresolvedDirectUses.clear(); 898ba7f2f71SDaniel Jasper return HadError; 899ba7f2f71SDaniel Jasper } 900ba7f2f71SDaniel Jasper 901fb912657SDouglas Gregor bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) { 902fb912657SDouglas Gregor bool HadError = false; 903fb912657SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) { 904fb912657SDouglas Gregor Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id, 905fb912657SDouglas Gregor Mod, Complain); 906fb912657SDouglas Gregor if (!OtherMod) { 907fb912657SDouglas Gregor HadError = true; 908fb912657SDouglas Gregor continue; 909fb912657SDouglas Gregor } 910fb912657SDouglas Gregor 911fb912657SDouglas Gregor Module::Conflict Conflict; 912fb912657SDouglas Gregor Conflict.Other = OtherMod; 913fb912657SDouglas Gregor Conflict.Message = Mod->UnresolvedConflicts[I].Message; 914fb912657SDouglas Gregor Mod->Conflicts.push_back(Conflict); 915fb912657SDouglas Gregor } 916fb912657SDouglas Gregor Mod->UnresolvedConflicts.clear(); 917fb912657SDouglas Gregor return HadError; 918fb912657SDouglas Gregor } 919fb912657SDouglas Gregor 9200093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 9210093b3c7SDouglas Gregor if (Loc.isInvalid()) 922d2d442caSCraig Topper return nullptr; 9230093b3c7SDouglas Gregor 9240093b3c7SDouglas Gregor // Use the expansion location to determine which module we're in. 9250093b3c7SDouglas Gregor FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 9260093b3c7SDouglas Gregor if (!ExpansionLoc.isFileID()) 927d2d442caSCraig Topper return nullptr; 9280093b3c7SDouglas Gregor 9290093b3c7SDouglas Gregor const SourceManager &SrcMgr = Loc.getManager(); 9300093b3c7SDouglas Gregor FileID ExpansionFileID = ExpansionLoc.getFileID(); 931224d8a74SDouglas Gregor 932224d8a74SDouglas Gregor while (const FileEntry *ExpansionFile 933224d8a74SDouglas Gregor = SrcMgr.getFileEntryForID(ExpansionFileID)) { 934224d8a74SDouglas Gregor // Find the module that owns this header (if any). 935b53e5483SLawrence Crowl if (Module *Mod = findModuleForHeader(ExpansionFile).getModule()) 936224d8a74SDouglas Gregor return Mod; 937224d8a74SDouglas Gregor 938224d8a74SDouglas Gregor // No module owns this header, so look up the inclusion chain to see if 939224d8a74SDouglas Gregor // any included header has an associated module. 940224d8a74SDouglas Gregor SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); 941224d8a74SDouglas Gregor if (IncludeLoc.isInvalid()) 942d2d442caSCraig Topper return nullptr; 9430093b3c7SDouglas Gregor 944224d8a74SDouglas Gregor ExpansionFileID = SrcMgr.getFileID(IncludeLoc); 945224d8a74SDouglas Gregor } 946224d8a74SDouglas Gregor 947d2d442caSCraig Topper return nullptr; 9480093b3c7SDouglas Gregor } 9490093b3c7SDouglas Gregor 950718292f2SDouglas Gregor //----------------------------------------------------------------------------// 951718292f2SDouglas Gregor // Module map file parser 952718292f2SDouglas Gregor //----------------------------------------------------------------------------// 953718292f2SDouglas Gregor 954718292f2SDouglas Gregor namespace clang { 955718292f2SDouglas Gregor /// \brief A token in a module map file. 956718292f2SDouglas Gregor struct MMToken { 957718292f2SDouglas Gregor enum TokenKind { 9581fb5c3a6SDouglas Gregor Comma, 95935b13eceSDouglas Gregor ConfigMacros, 960fb912657SDouglas Gregor Conflict, 961718292f2SDouglas Gregor EndOfFile, 962718292f2SDouglas Gregor HeaderKeyword, 963718292f2SDouglas Gregor Identifier, 964a3feee2aSRichard Smith Exclaim, 96559527666SDouglas Gregor ExcludeKeyword, 966718292f2SDouglas Gregor ExplicitKeyword, 9672b82c2a5SDouglas Gregor ExportKeyword, 96897292843SDaniel Jasper ExternKeyword, 969755b2055SDouglas Gregor FrameworkKeyword, 9706ddfca91SDouglas Gregor LinkKeyword, 971718292f2SDouglas Gregor ModuleKeyword, 9722b82c2a5SDouglas Gregor Period, 973b53e5483SLawrence Crowl PrivateKeyword, 974718292f2SDouglas Gregor UmbrellaKeyword, 975ba7f2f71SDaniel Jasper UseKeyword, 9761fb5c3a6SDouglas Gregor RequiresKeyword, 9772b82c2a5SDouglas Gregor Star, 978718292f2SDouglas Gregor StringLiteral, 979306d8920SRichard Smith TextualKeyword, 980718292f2SDouglas Gregor LBrace, 981a686e1b0SDouglas Gregor RBrace, 982a686e1b0SDouglas Gregor LSquare, 983a686e1b0SDouglas Gregor RSquare 984718292f2SDouglas Gregor } Kind; 985718292f2SDouglas Gregor 986718292f2SDouglas Gregor unsigned Location; 987718292f2SDouglas Gregor unsigned StringLength; 988718292f2SDouglas Gregor const char *StringData; 989718292f2SDouglas Gregor 990718292f2SDouglas Gregor void clear() { 991718292f2SDouglas Gregor Kind = EndOfFile; 992718292f2SDouglas Gregor Location = 0; 993718292f2SDouglas Gregor StringLength = 0; 994d2d442caSCraig Topper StringData = nullptr; 995718292f2SDouglas Gregor } 996718292f2SDouglas Gregor 997718292f2SDouglas Gregor bool is(TokenKind K) const { return Kind == K; } 998718292f2SDouglas Gregor 999718292f2SDouglas Gregor SourceLocation getLocation() const { 1000718292f2SDouglas Gregor return SourceLocation::getFromRawEncoding(Location); 1001718292f2SDouglas Gregor } 1002718292f2SDouglas Gregor 1003718292f2SDouglas Gregor StringRef getString() const { 1004718292f2SDouglas Gregor return StringRef(StringData, StringLength); 1005718292f2SDouglas Gregor } 1006718292f2SDouglas Gregor }; 1007718292f2SDouglas Gregor 1008718292f2SDouglas Gregor class ModuleMapParser { 1009718292f2SDouglas Gregor Lexer &L; 1010718292f2SDouglas Gregor SourceManager &SourceMgr; 1011bc10b9fbSDouglas Gregor 1012bc10b9fbSDouglas Gregor /// \brief Default target information, used only for string literal 1013bc10b9fbSDouglas Gregor /// parsing. 1014bc10b9fbSDouglas Gregor const TargetInfo *Target; 1015bc10b9fbSDouglas Gregor 1016718292f2SDouglas Gregor DiagnosticsEngine &Diags; 1017718292f2SDouglas Gregor ModuleMap ⤅ 1018718292f2SDouglas Gregor 1019beee15e7SBen Langmuir /// \brief The current module map file. 1020beee15e7SBen Langmuir const FileEntry *ModuleMapFile; 1021beee15e7SBen Langmuir 10229acb99e3SRichard Smith /// \brief The directory that file names in this module map file should 10239acb99e3SRichard Smith /// be resolved relative to. 10245257fc63SDouglas Gregor const DirectoryEntry *Directory; 10255257fc63SDouglas Gregor 10263ec6663bSDouglas Gregor /// \brief The directory containing Clang-supplied headers. 10273ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir; 10283ec6663bSDouglas Gregor 1029963c5535SDouglas Gregor /// \brief Whether this module map is in a system header directory. 1030963c5535SDouglas Gregor bool IsSystem; 1031963c5535SDouglas Gregor 1032718292f2SDouglas Gregor /// \brief Whether an error occurred. 1033718292f2SDouglas Gregor bool HadError; 1034718292f2SDouglas Gregor 1035718292f2SDouglas Gregor /// \brief Stores string data for the various string literals referenced 1036718292f2SDouglas Gregor /// during parsing. 1037718292f2SDouglas Gregor llvm::BumpPtrAllocator StringData; 1038718292f2SDouglas Gregor 1039718292f2SDouglas Gregor /// \brief The current token. 1040718292f2SDouglas Gregor MMToken Tok; 1041718292f2SDouglas Gregor 1042718292f2SDouglas Gregor /// \brief The active module. 1043de3ef502SDouglas Gregor Module *ActiveModule; 1044718292f2SDouglas Gregor 1045718292f2SDouglas Gregor /// \brief Consume the current token and return its location. 1046718292f2SDouglas Gregor SourceLocation consumeToken(); 1047718292f2SDouglas Gregor 1048718292f2SDouglas Gregor /// \brief Skip tokens until we reach the a token with the given kind 1049718292f2SDouglas Gregor /// (or the end of the file). 1050718292f2SDouglas Gregor void skipUntil(MMToken::TokenKind K); 1051718292f2SDouglas Gregor 1052f857950dSDmitri Gribenko typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; 1053e7ab3669SDouglas Gregor bool parseModuleId(ModuleId &Id); 1054718292f2SDouglas Gregor void parseModuleDecl(); 105597292843SDaniel Jasper void parseExternModuleDecl(); 10561fb5c3a6SDouglas Gregor void parseRequiresDecl(); 1057b53e5483SLawrence Crowl void parseHeaderDecl(clang::MMToken::TokenKind, 1058b53e5483SLawrence Crowl SourceLocation LeadingLoc); 1059524e33e1SDouglas Gregor void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 10602b82c2a5SDouglas Gregor void parseExportDecl(); 1061ba7f2f71SDaniel Jasper void parseUseDecl(); 10626ddfca91SDouglas Gregor void parseLinkDecl(); 106335b13eceSDouglas Gregor void parseConfigMacros(); 1064fb912657SDouglas Gregor void parseConflict(); 10659194a91dSDouglas Gregor void parseInferredModuleDecl(bool Framework, bool Explicit); 1066c1d88ea5SBen Langmuir 1067c1d88ea5SBen Langmuir typedef ModuleMap::Attributes Attributes; 10684442605fSBill Wendling bool parseOptionalAttributes(Attributes &Attrs); 1069718292f2SDouglas Gregor 1070718292f2SDouglas Gregor public: 1071718292f2SDouglas Gregor explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 1072bc10b9fbSDouglas Gregor const TargetInfo *Target, 1073718292f2SDouglas Gregor DiagnosticsEngine &Diags, 10745257fc63SDouglas Gregor ModuleMap &Map, 1075beee15e7SBen Langmuir const FileEntry *ModuleMapFile, 10763ec6663bSDouglas Gregor const DirectoryEntry *Directory, 1077963c5535SDouglas Gregor const DirectoryEntry *BuiltinIncludeDir, 1078963c5535SDouglas Gregor bool IsSystem) 1079bc10b9fbSDouglas Gregor : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 1080beee15e7SBen Langmuir ModuleMapFile(ModuleMapFile), Directory(Directory), 1081beee15e7SBen Langmuir BuiltinIncludeDir(BuiltinIncludeDir), IsSystem(IsSystem), 1082d2d442caSCraig Topper HadError(false), ActiveModule(nullptr) 1083718292f2SDouglas Gregor { 1084718292f2SDouglas Gregor Tok.clear(); 1085718292f2SDouglas Gregor consumeToken(); 1086718292f2SDouglas Gregor } 1087718292f2SDouglas Gregor 1088718292f2SDouglas Gregor bool parseModuleMapFile(); 1089718292f2SDouglas Gregor }; 1090718292f2SDouglas Gregor } 1091718292f2SDouglas Gregor 1092718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() { 1093718292f2SDouglas Gregor retry: 1094718292f2SDouglas Gregor SourceLocation Result = Tok.getLocation(); 1095718292f2SDouglas Gregor Tok.clear(); 1096718292f2SDouglas Gregor 1097718292f2SDouglas Gregor Token LToken; 1098718292f2SDouglas Gregor L.LexFromRawLexer(LToken); 1099718292f2SDouglas Gregor Tok.Location = LToken.getLocation().getRawEncoding(); 1100718292f2SDouglas Gregor switch (LToken.getKind()) { 11012d57cea2SAlp Toker case tok::raw_identifier: { 11022d57cea2SAlp Toker StringRef RI = LToken.getRawIdentifier(); 11032d57cea2SAlp Toker Tok.StringData = RI.data(); 11042d57cea2SAlp Toker Tok.StringLength = RI.size(); 11052d57cea2SAlp Toker Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI) 110635b13eceSDouglas Gregor .Case("config_macros", MMToken::ConfigMacros) 1107fb912657SDouglas Gregor .Case("conflict", MMToken::Conflict) 110859527666SDouglas Gregor .Case("exclude", MMToken::ExcludeKeyword) 1109718292f2SDouglas Gregor .Case("explicit", MMToken::ExplicitKeyword) 11102b82c2a5SDouglas Gregor .Case("export", MMToken::ExportKeyword) 111197292843SDaniel Jasper .Case("extern", MMToken::ExternKeyword) 1112755b2055SDouglas Gregor .Case("framework", MMToken::FrameworkKeyword) 111335b13eceSDouglas Gregor .Case("header", MMToken::HeaderKeyword) 11146ddfca91SDouglas Gregor .Case("link", MMToken::LinkKeyword) 1115718292f2SDouglas Gregor .Case("module", MMToken::ModuleKeyword) 1116b53e5483SLawrence Crowl .Case("private", MMToken::PrivateKeyword) 11171fb5c3a6SDouglas Gregor .Case("requires", MMToken::RequiresKeyword) 1118306d8920SRichard Smith .Case("textual", MMToken::TextualKeyword) 1119718292f2SDouglas Gregor .Case("umbrella", MMToken::UmbrellaKeyword) 1120ba7f2f71SDaniel Jasper .Case("use", MMToken::UseKeyword) 1121718292f2SDouglas Gregor .Default(MMToken::Identifier); 1122718292f2SDouglas Gregor break; 11232d57cea2SAlp Toker } 1124718292f2SDouglas Gregor 11251fb5c3a6SDouglas Gregor case tok::comma: 11261fb5c3a6SDouglas Gregor Tok.Kind = MMToken::Comma; 11271fb5c3a6SDouglas Gregor break; 11281fb5c3a6SDouglas Gregor 1129718292f2SDouglas Gregor case tok::eof: 1130718292f2SDouglas Gregor Tok.Kind = MMToken::EndOfFile; 1131718292f2SDouglas Gregor break; 1132718292f2SDouglas Gregor 1133718292f2SDouglas Gregor case tok::l_brace: 1134718292f2SDouglas Gregor Tok.Kind = MMToken::LBrace; 1135718292f2SDouglas Gregor break; 1136718292f2SDouglas Gregor 1137a686e1b0SDouglas Gregor case tok::l_square: 1138a686e1b0SDouglas Gregor Tok.Kind = MMToken::LSquare; 1139a686e1b0SDouglas Gregor break; 1140a686e1b0SDouglas Gregor 11412b82c2a5SDouglas Gregor case tok::period: 11422b82c2a5SDouglas Gregor Tok.Kind = MMToken::Period; 11432b82c2a5SDouglas Gregor break; 11442b82c2a5SDouglas Gregor 1145718292f2SDouglas Gregor case tok::r_brace: 1146718292f2SDouglas Gregor Tok.Kind = MMToken::RBrace; 1147718292f2SDouglas Gregor break; 1148718292f2SDouglas Gregor 1149a686e1b0SDouglas Gregor case tok::r_square: 1150a686e1b0SDouglas Gregor Tok.Kind = MMToken::RSquare; 1151a686e1b0SDouglas Gregor break; 1152a686e1b0SDouglas Gregor 11532b82c2a5SDouglas Gregor case tok::star: 11542b82c2a5SDouglas Gregor Tok.Kind = MMToken::Star; 11552b82c2a5SDouglas Gregor break; 11562b82c2a5SDouglas Gregor 1157a3feee2aSRichard Smith case tok::exclaim: 1158a3feee2aSRichard Smith Tok.Kind = MMToken::Exclaim; 1159a3feee2aSRichard Smith break; 1160a3feee2aSRichard Smith 1161718292f2SDouglas Gregor case tok::string_literal: { 1162d67aea28SRichard Smith if (LToken.hasUDSuffix()) { 1163d67aea28SRichard Smith Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 1164d67aea28SRichard Smith HadError = true; 1165d67aea28SRichard Smith goto retry; 1166d67aea28SRichard Smith } 1167d67aea28SRichard Smith 1168718292f2SDouglas Gregor // Parse the string literal. 1169718292f2SDouglas Gregor LangOptions LangOpts; 11709d5583efSCraig Topper StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target); 1171718292f2SDouglas Gregor if (StringLiteral.hadError) 1172718292f2SDouglas Gregor goto retry; 1173718292f2SDouglas Gregor 1174718292f2SDouglas Gregor // Copy the string literal into our string data allocator. 1175718292f2SDouglas Gregor unsigned Length = StringLiteral.GetStringLength(); 1176718292f2SDouglas Gregor char *Saved = StringData.Allocate<char>(Length + 1); 1177718292f2SDouglas Gregor memcpy(Saved, StringLiteral.GetString().data(), Length); 1178718292f2SDouglas Gregor Saved[Length] = 0; 1179718292f2SDouglas Gregor 1180718292f2SDouglas Gregor // Form the token. 1181718292f2SDouglas Gregor Tok.Kind = MMToken::StringLiteral; 1182718292f2SDouglas Gregor Tok.StringData = Saved; 1183718292f2SDouglas Gregor Tok.StringLength = Length; 1184718292f2SDouglas Gregor break; 1185718292f2SDouglas Gregor } 1186718292f2SDouglas Gregor 1187718292f2SDouglas Gregor case tok::comment: 1188718292f2SDouglas Gregor goto retry; 1189718292f2SDouglas Gregor 1190718292f2SDouglas Gregor default: 1191718292f2SDouglas Gregor Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 1192718292f2SDouglas Gregor HadError = true; 1193718292f2SDouglas Gregor goto retry; 1194718292f2SDouglas Gregor } 1195718292f2SDouglas Gregor 1196718292f2SDouglas Gregor return Result; 1197718292f2SDouglas Gregor } 1198718292f2SDouglas Gregor 1199718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 1200718292f2SDouglas Gregor unsigned braceDepth = 0; 1201a686e1b0SDouglas Gregor unsigned squareDepth = 0; 1202718292f2SDouglas Gregor do { 1203718292f2SDouglas Gregor switch (Tok.Kind) { 1204718292f2SDouglas Gregor case MMToken::EndOfFile: 1205718292f2SDouglas Gregor return; 1206718292f2SDouglas Gregor 1207718292f2SDouglas Gregor case MMToken::LBrace: 1208a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1209718292f2SDouglas Gregor return; 1210718292f2SDouglas Gregor 1211718292f2SDouglas Gregor ++braceDepth; 1212718292f2SDouglas Gregor break; 1213718292f2SDouglas Gregor 1214a686e1b0SDouglas Gregor case MMToken::LSquare: 1215a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1216a686e1b0SDouglas Gregor return; 1217a686e1b0SDouglas Gregor 1218a686e1b0SDouglas Gregor ++squareDepth; 1219a686e1b0SDouglas Gregor break; 1220a686e1b0SDouglas Gregor 1221718292f2SDouglas Gregor case MMToken::RBrace: 1222718292f2SDouglas Gregor if (braceDepth > 0) 1223718292f2SDouglas Gregor --braceDepth; 1224718292f2SDouglas Gregor else if (Tok.is(K)) 1225718292f2SDouglas Gregor return; 1226718292f2SDouglas Gregor break; 1227718292f2SDouglas Gregor 1228a686e1b0SDouglas Gregor case MMToken::RSquare: 1229a686e1b0SDouglas Gregor if (squareDepth > 0) 1230a686e1b0SDouglas Gregor --squareDepth; 1231a686e1b0SDouglas Gregor else if (Tok.is(K)) 1232a686e1b0SDouglas Gregor return; 1233a686e1b0SDouglas Gregor break; 1234a686e1b0SDouglas Gregor 1235718292f2SDouglas Gregor default: 1236a686e1b0SDouglas Gregor if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 1237718292f2SDouglas Gregor return; 1238718292f2SDouglas Gregor break; 1239718292f2SDouglas Gregor } 1240718292f2SDouglas Gregor 1241718292f2SDouglas Gregor consumeToken(); 1242718292f2SDouglas Gregor } while (true); 1243718292f2SDouglas Gregor } 1244718292f2SDouglas Gregor 1245e7ab3669SDouglas Gregor /// \brief Parse a module-id. 1246e7ab3669SDouglas Gregor /// 1247e7ab3669SDouglas Gregor /// module-id: 1248e7ab3669SDouglas Gregor /// identifier 1249e7ab3669SDouglas Gregor /// identifier '.' module-id 1250e7ab3669SDouglas Gregor /// 1251e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise. 1252e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) { 1253e7ab3669SDouglas Gregor Id.clear(); 1254e7ab3669SDouglas Gregor do { 12553cd34c76SDaniel Jasper if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) { 1256e7ab3669SDouglas Gregor Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 1257e7ab3669SDouglas Gregor consumeToken(); 1258e7ab3669SDouglas Gregor } else { 1259e7ab3669SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 1260e7ab3669SDouglas Gregor return true; 1261e7ab3669SDouglas Gregor } 1262e7ab3669SDouglas Gregor 1263e7ab3669SDouglas Gregor if (!Tok.is(MMToken::Period)) 1264e7ab3669SDouglas Gregor break; 1265e7ab3669SDouglas Gregor 1266e7ab3669SDouglas Gregor consumeToken(); 1267e7ab3669SDouglas Gregor } while (true); 1268e7ab3669SDouglas Gregor 1269e7ab3669SDouglas Gregor return false; 1270e7ab3669SDouglas Gregor } 1271e7ab3669SDouglas Gregor 1272a686e1b0SDouglas Gregor namespace { 1273a686e1b0SDouglas Gregor /// \brief Enumerates the known attributes. 1274a686e1b0SDouglas Gregor enum AttributeKind { 1275a686e1b0SDouglas Gregor /// \brief An unknown attribute. 1276a686e1b0SDouglas Gregor AT_unknown, 1277a686e1b0SDouglas Gregor /// \brief The 'system' attribute. 127835b13eceSDouglas Gregor AT_system, 127977944868SRichard Smith /// \brief The 'extern_c' attribute. 128077944868SRichard Smith AT_extern_c, 128135b13eceSDouglas Gregor /// \brief The 'exhaustive' attribute. 128235b13eceSDouglas Gregor AT_exhaustive 1283a686e1b0SDouglas Gregor }; 1284a686e1b0SDouglas Gregor } 1285a686e1b0SDouglas Gregor 1286718292f2SDouglas Gregor /// \brief Parse a module declaration. 1287718292f2SDouglas Gregor /// 1288718292f2SDouglas Gregor /// module-declaration: 128997292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 1290a686e1b0SDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 1291a686e1b0SDouglas Gregor /// { module-member* } 1292a686e1b0SDouglas Gregor /// 1293718292f2SDouglas Gregor /// module-member: 12941fb5c3a6SDouglas Gregor /// requires-declaration 1295718292f2SDouglas Gregor /// header-declaration 1296e7ab3669SDouglas Gregor /// submodule-declaration 12972b82c2a5SDouglas Gregor /// export-declaration 12986ddfca91SDouglas Gregor /// link-declaration 129973441091SDouglas Gregor /// 130073441091SDouglas Gregor /// submodule-declaration: 130173441091SDouglas Gregor /// module-declaration 130273441091SDouglas Gregor /// inferred-submodule-declaration 1303718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() { 1304755b2055SDouglas Gregor assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 130597292843SDaniel Jasper Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword)); 130697292843SDaniel Jasper if (Tok.is(MMToken::ExternKeyword)) { 130797292843SDaniel Jasper parseExternModuleDecl(); 130897292843SDaniel Jasper return; 130997292843SDaniel Jasper } 131097292843SDaniel Jasper 1311f2161a70SDouglas Gregor // Parse 'explicit' or 'framework' keyword, if present. 1312e7ab3669SDouglas Gregor SourceLocation ExplicitLoc; 1313718292f2SDouglas Gregor bool Explicit = false; 1314f2161a70SDouglas Gregor bool Framework = false; 1315755b2055SDouglas Gregor 1316f2161a70SDouglas Gregor // Parse 'explicit' keyword, if present. 1317f2161a70SDouglas Gregor if (Tok.is(MMToken::ExplicitKeyword)) { 1318e7ab3669SDouglas Gregor ExplicitLoc = consumeToken(); 1319f2161a70SDouglas Gregor Explicit = true; 1320f2161a70SDouglas Gregor } 1321f2161a70SDouglas Gregor 1322f2161a70SDouglas Gregor // Parse 'framework' keyword, if present. 1323755b2055SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 1324755b2055SDouglas Gregor consumeToken(); 1325755b2055SDouglas Gregor Framework = true; 1326755b2055SDouglas Gregor } 1327718292f2SDouglas Gregor 1328718292f2SDouglas Gregor // Parse 'module' keyword. 1329718292f2SDouglas Gregor if (!Tok.is(MMToken::ModuleKeyword)) { 1330d6343c99SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1331718292f2SDouglas Gregor consumeToken(); 1332718292f2SDouglas Gregor HadError = true; 1333718292f2SDouglas Gregor return; 1334718292f2SDouglas Gregor } 1335718292f2SDouglas Gregor consumeToken(); // 'module' keyword 1336718292f2SDouglas Gregor 133773441091SDouglas Gregor // If we have a wildcard for the module name, this is an inferred submodule. 133873441091SDouglas Gregor // Parse it. 133973441091SDouglas Gregor if (Tok.is(MMToken::Star)) 13409194a91dSDouglas Gregor return parseInferredModuleDecl(Framework, Explicit); 134173441091SDouglas Gregor 1342718292f2SDouglas Gregor // Parse the module name. 1343e7ab3669SDouglas Gregor ModuleId Id; 1344e7ab3669SDouglas Gregor if (parseModuleId(Id)) { 1345718292f2SDouglas Gregor HadError = true; 1346718292f2SDouglas Gregor return; 1347718292f2SDouglas Gregor } 1348e7ab3669SDouglas Gregor 1349e7ab3669SDouglas Gregor if (ActiveModule) { 1350e7ab3669SDouglas Gregor if (Id.size() > 1) { 1351e7ab3669SDouglas Gregor Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 1352e7ab3669SDouglas Gregor << SourceRange(Id.front().second, Id.back().second); 1353e7ab3669SDouglas Gregor 1354e7ab3669SDouglas Gregor HadError = true; 1355e7ab3669SDouglas Gregor return; 1356e7ab3669SDouglas Gregor } 1357e7ab3669SDouglas Gregor } else if (Id.size() == 1 && Explicit) { 1358e7ab3669SDouglas Gregor // Top-level modules can't be explicit. 1359e7ab3669SDouglas Gregor Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 1360e7ab3669SDouglas Gregor Explicit = false; 1361e7ab3669SDouglas Gregor ExplicitLoc = SourceLocation(); 1362e7ab3669SDouglas Gregor HadError = true; 1363e7ab3669SDouglas Gregor } 1364e7ab3669SDouglas Gregor 1365e7ab3669SDouglas Gregor Module *PreviousActiveModule = ActiveModule; 1366e7ab3669SDouglas Gregor if (Id.size() > 1) { 1367e7ab3669SDouglas Gregor // This module map defines a submodule. Go find the module of which it 1368e7ab3669SDouglas Gregor // is a submodule. 1369d2d442caSCraig Topper ActiveModule = nullptr; 13704b8a9e95SBen Langmuir const Module *TopLevelModule = nullptr; 1371e7ab3669SDouglas Gregor for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 1372e7ab3669SDouglas Gregor if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 13734b8a9e95SBen Langmuir if (I == 0) 13744b8a9e95SBen Langmuir TopLevelModule = Next; 1375e7ab3669SDouglas Gregor ActiveModule = Next; 1376e7ab3669SDouglas Gregor continue; 1377e7ab3669SDouglas Gregor } 1378e7ab3669SDouglas Gregor 1379e7ab3669SDouglas Gregor if (ActiveModule) { 1380e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 13815b5d21eaSRichard Smith << Id[I].first 13825b5d21eaSRichard Smith << ActiveModule->getTopLevelModule()->getFullModuleName(); 1383e7ab3669SDouglas Gregor } else { 1384e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 1385e7ab3669SDouglas Gregor } 1386e7ab3669SDouglas Gregor HadError = true; 1387e7ab3669SDouglas Gregor return; 1388e7ab3669SDouglas Gregor } 13894b8a9e95SBen Langmuir 13904b8a9e95SBen Langmuir if (ModuleMapFile != Map.getContainingModuleMapFile(TopLevelModule)) { 13914b8a9e95SBen Langmuir assert(ModuleMapFile != Map.getModuleMapFileForUniquing(TopLevelModule) && 13924b8a9e95SBen Langmuir "submodule defined in same file as 'module *' that allowed its " 13934b8a9e95SBen Langmuir "top-level module"); 13944b8a9e95SBen Langmuir Map.addAdditionalModuleMapFile(TopLevelModule, ModuleMapFile); 13954b8a9e95SBen Langmuir } 1396e7ab3669SDouglas Gregor } 1397e7ab3669SDouglas Gregor 1398e7ab3669SDouglas Gregor StringRef ModuleName = Id.back().first; 1399e7ab3669SDouglas Gregor SourceLocation ModuleNameLoc = Id.back().second; 1400718292f2SDouglas Gregor 1401a686e1b0SDouglas Gregor // Parse the optional attribute list. 14024442605fSBill Wendling Attributes Attrs; 14039194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 1404a686e1b0SDouglas Gregor 1405718292f2SDouglas Gregor // Parse the opening brace. 1406718292f2SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 1407718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 1408718292f2SDouglas Gregor << ModuleName; 1409718292f2SDouglas Gregor HadError = true; 1410718292f2SDouglas Gregor return; 1411718292f2SDouglas Gregor } 1412718292f2SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 1413718292f2SDouglas Gregor 1414718292f2SDouglas Gregor // Determine whether this (sub)module has already been defined. 1415eb90e830SDouglas Gregor if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 1416fcc54a3bSDouglas Gregor if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { 1417fcc54a3bSDouglas Gregor // Skip the module definition. 1418fcc54a3bSDouglas Gregor skipUntil(MMToken::RBrace); 1419fcc54a3bSDouglas Gregor if (Tok.is(MMToken::RBrace)) 1420fcc54a3bSDouglas Gregor consumeToken(); 1421fcc54a3bSDouglas Gregor else { 1422fcc54a3bSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1423fcc54a3bSDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1424fcc54a3bSDouglas Gregor HadError = true; 1425fcc54a3bSDouglas Gregor } 1426fcc54a3bSDouglas Gregor return; 1427fcc54a3bSDouglas Gregor } 1428fcc54a3bSDouglas Gregor 1429718292f2SDouglas Gregor Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 1430718292f2SDouglas Gregor << ModuleName; 1431eb90e830SDouglas Gregor Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 1432718292f2SDouglas Gregor 1433718292f2SDouglas Gregor // Skip the module definition. 1434718292f2SDouglas Gregor skipUntil(MMToken::RBrace); 1435718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1436718292f2SDouglas Gregor consumeToken(); 1437718292f2SDouglas Gregor 1438718292f2SDouglas Gregor HadError = true; 1439718292f2SDouglas Gregor return; 1440718292f2SDouglas Gregor } 1441718292f2SDouglas Gregor 1442718292f2SDouglas Gregor // Start defining this module. 14439d6448b1SBen Langmuir ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, 14449d6448b1SBen Langmuir Explicit).first; 1445eb90e830SDouglas Gregor ActiveModule->DefinitionLoc = ModuleNameLoc; 1446963c5535SDouglas Gregor if (Attrs.IsSystem || IsSystem) 1447a686e1b0SDouglas Gregor ActiveModule->IsSystem = true; 144877944868SRichard Smith if (Attrs.IsExternC) 144977944868SRichard Smith ActiveModule->IsExternC = true; 14503c1a41adSRichard Smith ActiveModule->Directory = Directory; 1451718292f2SDouglas Gregor 1452718292f2SDouglas Gregor bool Done = false; 1453718292f2SDouglas Gregor do { 1454718292f2SDouglas Gregor switch (Tok.Kind) { 1455718292f2SDouglas Gregor case MMToken::EndOfFile: 1456718292f2SDouglas Gregor case MMToken::RBrace: 1457718292f2SDouglas Gregor Done = true; 1458718292f2SDouglas Gregor break; 1459718292f2SDouglas Gregor 146035b13eceSDouglas Gregor case MMToken::ConfigMacros: 146135b13eceSDouglas Gregor parseConfigMacros(); 146235b13eceSDouglas Gregor break; 146335b13eceSDouglas Gregor 1464fb912657SDouglas Gregor case MMToken::Conflict: 1465fb912657SDouglas Gregor parseConflict(); 1466fb912657SDouglas Gregor break; 1467fb912657SDouglas Gregor 1468718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 146997292843SDaniel Jasper case MMToken::ExternKeyword: 1470f2161a70SDouglas Gregor case MMToken::FrameworkKeyword: 1471718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1472718292f2SDouglas Gregor parseModuleDecl(); 1473718292f2SDouglas Gregor break; 1474718292f2SDouglas Gregor 14752b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 14762b82c2a5SDouglas Gregor parseExportDecl(); 14772b82c2a5SDouglas Gregor break; 14782b82c2a5SDouglas Gregor 1479ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 1480ba7f2f71SDaniel Jasper parseUseDecl(); 1481ba7f2f71SDaniel Jasper break; 1482ba7f2f71SDaniel Jasper 14831fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 14841fb5c3a6SDouglas Gregor parseRequiresDecl(); 14851fb5c3a6SDouglas Gregor break; 14861fb5c3a6SDouglas Gregor 1487202210b3SRichard Smith case MMToken::TextualKeyword: 1488202210b3SRichard Smith parseHeaderDecl(MMToken::TextualKeyword, consumeToken()); 1489306d8920SRichard Smith break; 1490306d8920SRichard Smith 1491524e33e1SDouglas Gregor case MMToken::UmbrellaKeyword: { 1492524e33e1SDouglas Gregor SourceLocation UmbrellaLoc = consumeToken(); 1493524e33e1SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) 1494b53e5483SLawrence Crowl parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc); 1495524e33e1SDouglas Gregor else 1496524e33e1SDouglas Gregor parseUmbrellaDirDecl(UmbrellaLoc); 1497718292f2SDouglas Gregor break; 1498524e33e1SDouglas Gregor } 1499718292f2SDouglas Gregor 1500202210b3SRichard Smith case MMToken::ExcludeKeyword: 1501202210b3SRichard Smith parseHeaderDecl(MMToken::ExcludeKeyword, consumeToken()); 150259527666SDouglas Gregor break; 150359527666SDouglas Gregor 1504202210b3SRichard Smith case MMToken::PrivateKeyword: 1505202210b3SRichard Smith parseHeaderDecl(MMToken::PrivateKeyword, consumeToken()); 1506b53e5483SLawrence Crowl break; 1507b53e5483SLawrence Crowl 1508322f633cSDouglas Gregor case MMToken::HeaderKeyword: 1509202210b3SRichard Smith parseHeaderDecl(MMToken::HeaderKeyword, consumeToken()); 1510718292f2SDouglas Gregor break; 1511718292f2SDouglas Gregor 15126ddfca91SDouglas Gregor case MMToken::LinkKeyword: 15136ddfca91SDouglas Gregor parseLinkDecl(); 15146ddfca91SDouglas Gregor break; 15156ddfca91SDouglas Gregor 1516718292f2SDouglas Gregor default: 1517718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 1518718292f2SDouglas Gregor consumeToken(); 1519718292f2SDouglas Gregor break; 1520718292f2SDouglas Gregor } 1521718292f2SDouglas Gregor } while (!Done); 1522718292f2SDouglas Gregor 1523718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1524718292f2SDouglas Gregor consumeToken(); 1525718292f2SDouglas Gregor else { 1526718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1527718292f2SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1528718292f2SDouglas Gregor HadError = true; 1529718292f2SDouglas Gregor } 1530718292f2SDouglas Gregor 153111dfe6feSDouglas Gregor // If the active module is a top-level framework, and there are no link 153211dfe6feSDouglas Gregor // libraries, automatically link against the framework. 153311dfe6feSDouglas Gregor if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() && 153411dfe6feSDouglas Gregor ActiveModule->LinkLibraries.empty()) { 153511dfe6feSDouglas Gregor inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager()); 153611dfe6feSDouglas Gregor } 153711dfe6feSDouglas Gregor 1538ec8c9752SBen Langmuir // If the module meets all requirements but is still unavailable, mark the 1539ec8c9752SBen Langmuir // whole tree as unavailable to prevent it from building. 1540ec8c9752SBen Langmuir if (!ActiveModule->IsAvailable && !ActiveModule->IsMissingRequirement && 1541ec8c9752SBen Langmuir ActiveModule->Parent) { 1542ec8c9752SBen Langmuir ActiveModule->getTopLevelModule()->markUnavailable(); 1543ec8c9752SBen Langmuir ActiveModule->getTopLevelModule()->MissingHeaders.append( 1544ec8c9752SBen Langmuir ActiveModule->MissingHeaders.begin(), ActiveModule->MissingHeaders.end()); 1545ec8c9752SBen Langmuir } 1546ec8c9752SBen Langmuir 1547e7ab3669SDouglas Gregor // We're done parsing this module. Pop back to the previous module. 1548e7ab3669SDouglas Gregor ActiveModule = PreviousActiveModule; 1549718292f2SDouglas Gregor } 1550718292f2SDouglas Gregor 155197292843SDaniel Jasper /// \brief Parse an extern module declaration. 155297292843SDaniel Jasper /// 155397292843SDaniel Jasper /// extern module-declaration: 155497292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 155597292843SDaniel Jasper void ModuleMapParser::parseExternModuleDecl() { 155697292843SDaniel Jasper assert(Tok.is(MMToken::ExternKeyword)); 155797292843SDaniel Jasper consumeToken(); // 'extern' keyword 155897292843SDaniel Jasper 155997292843SDaniel Jasper // Parse 'module' keyword. 156097292843SDaniel Jasper if (!Tok.is(MMToken::ModuleKeyword)) { 156197292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 156297292843SDaniel Jasper consumeToken(); 156397292843SDaniel Jasper HadError = true; 156497292843SDaniel Jasper return; 156597292843SDaniel Jasper } 156697292843SDaniel Jasper consumeToken(); // 'module' keyword 156797292843SDaniel Jasper 156897292843SDaniel Jasper // Parse the module name. 156997292843SDaniel Jasper ModuleId Id; 157097292843SDaniel Jasper if (parseModuleId(Id)) { 157197292843SDaniel Jasper HadError = true; 157297292843SDaniel Jasper return; 157397292843SDaniel Jasper } 157497292843SDaniel Jasper 157597292843SDaniel Jasper // Parse the referenced module map file name. 157697292843SDaniel Jasper if (!Tok.is(MMToken::StringLiteral)) { 157797292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file); 157897292843SDaniel Jasper HadError = true; 157997292843SDaniel Jasper return; 158097292843SDaniel Jasper } 158197292843SDaniel Jasper std::string FileName = Tok.getString(); 158297292843SDaniel Jasper consumeToken(); // filename 158397292843SDaniel Jasper 158497292843SDaniel Jasper StringRef FileNameRef = FileName; 158597292843SDaniel Jasper SmallString<128> ModuleMapFileName; 158697292843SDaniel Jasper if (llvm::sys::path::is_relative(FileNameRef)) { 158797292843SDaniel Jasper ModuleMapFileName += Directory->getName(); 158897292843SDaniel Jasper llvm::sys::path::append(ModuleMapFileName, FileName); 158997292843SDaniel Jasper FileNameRef = ModuleMapFileName.str(); 159097292843SDaniel Jasper } 159197292843SDaniel Jasper if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef)) 15929acb99e3SRichard Smith Map.parseModuleMapFile( 15939acb99e3SRichard Smith File, /*IsSystem=*/false, 15949acb99e3SRichard Smith Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd 15959acb99e3SRichard Smith ? Directory 15969acb99e3SRichard Smith : File->getDir()); 159797292843SDaniel Jasper } 159897292843SDaniel Jasper 15991fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration. 16001fb5c3a6SDouglas Gregor /// 16011fb5c3a6SDouglas Gregor /// requires-declaration: 16021fb5c3a6SDouglas Gregor /// 'requires' feature-list 16031fb5c3a6SDouglas Gregor /// 16041fb5c3a6SDouglas Gregor /// feature-list: 1605a3feee2aSRichard Smith /// feature ',' feature-list 1606a3feee2aSRichard Smith /// feature 1607a3feee2aSRichard Smith /// 1608a3feee2aSRichard Smith /// feature: 1609a3feee2aSRichard Smith /// '!'[opt] identifier 16101fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() { 16111fb5c3a6SDouglas Gregor assert(Tok.is(MMToken::RequiresKeyword)); 16121fb5c3a6SDouglas Gregor 16131fb5c3a6SDouglas Gregor // Parse 'requires' keyword. 16141fb5c3a6SDouglas Gregor consumeToken(); 16151fb5c3a6SDouglas Gregor 16161fb5c3a6SDouglas Gregor // Parse the feature-list. 16171fb5c3a6SDouglas Gregor do { 1618a3feee2aSRichard Smith bool RequiredState = true; 1619a3feee2aSRichard Smith if (Tok.is(MMToken::Exclaim)) { 1620a3feee2aSRichard Smith RequiredState = false; 1621a3feee2aSRichard Smith consumeToken(); 1622a3feee2aSRichard Smith } 1623a3feee2aSRichard Smith 16241fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 16251fb5c3a6SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 16261fb5c3a6SDouglas Gregor HadError = true; 16271fb5c3a6SDouglas Gregor return; 16281fb5c3a6SDouglas Gregor } 16291fb5c3a6SDouglas Gregor 16301fb5c3a6SDouglas Gregor // Consume the feature name. 16311fb5c3a6SDouglas Gregor std::string Feature = Tok.getString(); 16321fb5c3a6SDouglas Gregor consumeToken(); 16331fb5c3a6SDouglas Gregor 16341fb5c3a6SDouglas Gregor // Add this feature. 1635a3feee2aSRichard Smith ActiveModule->addRequirement(Feature, RequiredState, 1636a3feee2aSRichard Smith Map.LangOpts, *Map.Target); 16371fb5c3a6SDouglas Gregor 16381fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Comma)) 16391fb5c3a6SDouglas Gregor break; 16401fb5c3a6SDouglas Gregor 16411fb5c3a6SDouglas Gregor // Consume the comma. 16421fb5c3a6SDouglas Gregor consumeToken(); 16431fb5c3a6SDouglas Gregor } while (true); 16441fb5c3a6SDouglas Gregor } 16451fb5c3a6SDouglas Gregor 1646f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the 1647f2161a70SDouglas Gregor /// subframework in which the given module lives. 1648bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod, 1649f857950dSDmitri Gribenko SmallVectorImpl<char> &Path) { 1650f2161a70SDouglas Gregor // Collect the framework names from the given module to the top-level module. 1651f857950dSDmitri Gribenko SmallVector<StringRef, 2> Paths; 1652f2161a70SDouglas Gregor for (; Mod; Mod = Mod->Parent) { 1653f2161a70SDouglas Gregor if (Mod->IsFramework) 1654f2161a70SDouglas Gregor Paths.push_back(Mod->Name); 1655f2161a70SDouglas Gregor } 1656f2161a70SDouglas Gregor 1657f2161a70SDouglas Gregor if (Paths.empty()) 1658f2161a70SDouglas Gregor return; 1659f2161a70SDouglas Gregor 1660f2161a70SDouglas Gregor // Add Frameworks/Name.framework for each subframework. 166117381a06SBenjamin Kramer for (unsigned I = Paths.size() - 1; I != 0; --I) 166217381a06SBenjamin Kramer llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework"); 1663f2161a70SDouglas Gregor } 1664f2161a70SDouglas Gregor 1665718292f2SDouglas Gregor /// \brief Parse a header declaration. 1666718292f2SDouglas Gregor /// 1667718292f2SDouglas Gregor /// header-declaration: 1668306d8920SRichard Smith /// 'textual'[opt] 'header' string-literal 1669202210b3SRichard Smith /// 'private' 'textual'[opt] 'header' string-literal 1670202210b3SRichard Smith /// 'exclude' 'header' string-literal 1671202210b3SRichard Smith /// 'umbrella' 'header' string-literal 1672306d8920SRichard Smith /// 1673306d8920SRichard Smith /// FIXME: Support 'private textual header'. 1674b53e5483SLawrence Crowl void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken, 1675b53e5483SLawrence Crowl SourceLocation LeadingLoc) { 1676202210b3SRichard Smith // We've already consumed the first token. 1677202210b3SRichard Smith ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader; 1678202210b3SRichard Smith if (LeadingToken == MMToken::PrivateKeyword) { 1679202210b3SRichard Smith Role = ModuleMap::PrivateHeader; 1680202210b3SRichard Smith // 'private' may optionally be followed by 'textual'. 1681202210b3SRichard Smith if (Tok.is(MMToken::TextualKeyword)) { 1682202210b3SRichard Smith LeadingToken = Tok.Kind; 16831871ed3dSBenjamin Kramer consumeToken(); 1684202210b3SRichard Smith } 1685202210b3SRichard Smith } 1686202210b3SRichard Smith if (LeadingToken == MMToken::TextualKeyword) 1687202210b3SRichard Smith Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader); 1688202210b3SRichard Smith 1689202210b3SRichard Smith if (LeadingToken != MMToken::HeaderKeyword) { 1690202210b3SRichard Smith if (!Tok.is(MMToken::HeaderKeyword)) { 1691202210b3SRichard Smith Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1692202210b3SRichard Smith << (LeadingToken == MMToken::PrivateKeyword ? "private" : 1693202210b3SRichard Smith LeadingToken == MMToken::ExcludeKeyword ? "exclude" : 1694202210b3SRichard Smith LeadingToken == MMToken::TextualKeyword ? "textual" : "umbrella"); 1695202210b3SRichard Smith return; 1696202210b3SRichard Smith } 1697202210b3SRichard Smith consumeToken(); 1698202210b3SRichard Smith } 1699718292f2SDouglas Gregor 1700718292f2SDouglas Gregor // Parse the header name. 1701718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1702718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1703718292f2SDouglas Gregor << "header"; 1704718292f2SDouglas Gregor HadError = true; 1705718292f2SDouglas Gregor return; 1706718292f2SDouglas Gregor } 17073c1a41adSRichard Smith Module::UnresolvedHeaderDirective Header; 17080761a8a0SDaniel Jasper Header.FileName = Tok.getString(); 17090761a8a0SDaniel Jasper Header.FileNameLoc = consumeToken(); 1710718292f2SDouglas Gregor 1711524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1712b53e5483SLawrence Crowl if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) { 17130761a8a0SDaniel Jasper Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash) 1714524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1715322f633cSDouglas Gregor HadError = true; 1716322f633cSDouglas Gregor return; 1717322f633cSDouglas Gregor } 1718322f633cSDouglas Gregor 17195257fc63SDouglas Gregor // Look for this file. 1720d2d442caSCraig Topper const FileEntry *File = nullptr; 1721d2d442caSCraig Topper const FileEntry *BuiltinFile = nullptr; 17223c1a41adSRichard Smith SmallString<128> RelativePathName; 17230761a8a0SDaniel Jasper if (llvm::sys::path::is_absolute(Header.FileName)) { 17243c1a41adSRichard Smith RelativePathName = Header.FileName; 17253c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(RelativePathName); 1726e7ab3669SDouglas Gregor } else { 1727e7ab3669SDouglas Gregor // Search for the header file within the search directory. 17283c1a41adSRichard Smith SmallString<128> FullPathName(Directory->getName()); 17293c1a41adSRichard Smith unsigned FullPathLength = FullPathName.size(); 1730755b2055SDouglas Gregor 1731f2161a70SDouglas Gregor if (ActiveModule->isPartOfFramework()) { 17323c1a41adSRichard Smith appendSubframeworkPaths(ActiveModule, RelativePathName); 1733755b2055SDouglas Gregor 1734e7ab3669SDouglas Gregor // Check whether this file is in the public headers. 17353c1a41adSRichard Smith llvm::sys::path::append(RelativePathName, "Headers", Header.FileName); 17363c1a41adSRichard Smith llvm::sys::path::append(FullPathName, RelativePathName.str()); 17373c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(FullPathName); 1738e7ab3669SDouglas Gregor 1739e7ab3669SDouglas Gregor if (!File) { 1740e7ab3669SDouglas Gregor // Check whether this file is in the private headers. 17413c1a41adSRichard Smith // FIXME: Should we retain the subframework paths here? 17423c1a41adSRichard Smith RelativePathName.clear(); 17433c1a41adSRichard Smith FullPathName.resize(FullPathLength); 17443c1a41adSRichard Smith llvm::sys::path::append(RelativePathName, "PrivateHeaders", 17453c1a41adSRichard Smith Header.FileName); 17463c1a41adSRichard Smith llvm::sys::path::append(FullPathName, RelativePathName.str()); 17473c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(FullPathName); 1748e7ab3669SDouglas Gregor } 1749e7ab3669SDouglas Gregor } else { 1750e7ab3669SDouglas Gregor // Lookup for normal headers. 17513c1a41adSRichard Smith llvm::sys::path::append(RelativePathName, Header.FileName); 17523c1a41adSRichard Smith llvm::sys::path::append(FullPathName, RelativePathName.str()); 17533c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(FullPathName); 17543ec6663bSDouglas Gregor 17553ec6663bSDouglas Gregor // If this is a system module with a top-level header, this header 17563ec6663bSDouglas Gregor // may have a counterpart (or replacement) in the set of headers 17573ec6663bSDouglas Gregor // supplied by Clang. Find that builtin header. 1758b53e5483SLawrence Crowl if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword && 1759b53e5483SLawrence Crowl BuiltinIncludeDir && BuiltinIncludeDir != Directory && 17600761a8a0SDaniel Jasper isBuiltinHeader(Header.FileName)) { 17612c1dd271SDylan Noblesmith SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); 17620761a8a0SDaniel Jasper llvm::sys::path::append(BuiltinPathName, Header.FileName); 17633ec6663bSDouglas Gregor BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); 17643ec6663bSDouglas Gregor 17653ec6663bSDouglas Gregor // If Clang supplies this header but the underlying system does not, 17663ec6663bSDouglas Gregor // just silently swap in our builtin version. Otherwise, we'll end 17673ec6663bSDouglas Gregor // up adding both (later). 17683ec6663bSDouglas Gregor if (!File && BuiltinFile) { 17693ec6663bSDouglas Gregor File = BuiltinFile; 17703c1a41adSRichard Smith RelativePathName = BuiltinPathName; 1771d2d442caSCraig Topper BuiltinFile = nullptr; 17723ec6663bSDouglas Gregor } 17733ec6663bSDouglas Gregor } 1774e7ab3669SDouglas Gregor } 1775e7ab3669SDouglas Gregor } 17765257fc63SDouglas Gregor 17775257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 17785257fc63SDouglas Gregor // Come up with a lazy way to do this. 1779e7ab3669SDouglas Gregor if (File) { 178097da9178SDaniel Jasper if (LeadingToken == MMToken::UmbrellaKeyword) { 1781322f633cSDouglas Gregor const DirectoryEntry *UmbrellaDir = File->getDir(); 178259527666SDouglas Gregor if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { 1783b53e5483SLawrence Crowl Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash) 178459527666SDouglas Gregor << UmbrellaModule->getFullModuleName(); 1785322f633cSDouglas Gregor HadError = true; 17865257fc63SDouglas Gregor } else { 1787322f633cSDouglas Gregor // Record this umbrella header. 1788322f633cSDouglas Gregor Map.setUmbrellaHeader(ActiveModule, File); 1789322f633cSDouglas Gregor } 1790feb54b6dSRichard Smith } else if (LeadingToken == MMToken::ExcludeKeyword) { 17910101b540SHans Wennborg Module::Header H = {RelativePathName.str(), File}; 17920101b540SHans Wennborg Map.excludeHeader(ActiveModule, H); 1793322f633cSDouglas Gregor } else { 179425d50758SRichard Smith // If there is a builtin counterpart to this file, add it now, before 179525d50758SRichard Smith // the "real" header, so we build the built-in one first when building 179625d50758SRichard Smith // the module. 17970101b540SHans Wennborg if (BuiltinFile) { 17983c1a41adSRichard Smith // FIXME: Taking the name from the FileEntry is unstable and can give 17993c1a41adSRichard Smith // different results depending on how we've previously named that file 18003c1a41adSRichard Smith // in this build. 18010101b540SHans Wennborg Module::Header H = { BuiltinFile->getName(), BuiltinFile }; 18020101b540SHans Wennborg Map.addHeader(ActiveModule, H, Role); 18030101b540SHans Wennborg } 180425d50758SRichard Smith 1805202210b3SRichard Smith // Record this header. 18060101b540SHans Wennborg Module::Header H = { RelativePathName.str(), File }; 18070101b540SHans Wennborg Map.addHeader(ActiveModule, H, Role); 18085257fc63SDouglas Gregor } 1809b53e5483SLawrence Crowl } else if (LeadingToken != MMToken::ExcludeKeyword) { 18104b27a64bSDouglas Gregor // Ignore excluded header files. They're optional anyway. 18114b27a64bSDouglas Gregor 18120761a8a0SDaniel Jasper // If we find a module that has a missing header, we mark this module as 18130761a8a0SDaniel Jasper // unavailable and store the header directive for displaying diagnostics. 18140761a8a0SDaniel Jasper Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword; 1815ec8c9752SBen Langmuir ActiveModule->markUnavailable(); 18160761a8a0SDaniel Jasper ActiveModule->MissingHeaders.push_back(Header); 18175257fc63SDouglas Gregor } 1818718292f2SDouglas Gregor } 1819718292f2SDouglas Gregor 1820524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration. 1821524e33e1SDouglas Gregor /// 1822524e33e1SDouglas Gregor /// umbrella-dir-declaration: 1823524e33e1SDouglas Gregor /// umbrella string-literal 1824524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1825524e33e1SDouglas Gregor // Parse the directory name. 1826524e33e1SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1827524e33e1SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1828524e33e1SDouglas Gregor << "umbrella"; 1829524e33e1SDouglas Gregor HadError = true; 1830524e33e1SDouglas Gregor return; 1831524e33e1SDouglas Gregor } 1832524e33e1SDouglas Gregor 1833524e33e1SDouglas Gregor std::string DirName = Tok.getString(); 1834524e33e1SDouglas Gregor SourceLocation DirNameLoc = consumeToken(); 1835524e33e1SDouglas Gregor 1836524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1837524e33e1SDouglas Gregor if (ActiveModule->Umbrella) { 1838524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1839524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1840524e33e1SDouglas Gregor HadError = true; 1841524e33e1SDouglas Gregor return; 1842524e33e1SDouglas Gregor } 1843524e33e1SDouglas Gregor 1844524e33e1SDouglas Gregor // Look for this file. 1845d2d442caSCraig Topper const DirectoryEntry *Dir = nullptr; 1846524e33e1SDouglas Gregor if (llvm::sys::path::is_absolute(DirName)) 1847524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(DirName); 1848524e33e1SDouglas Gregor else { 18492c1dd271SDylan Noblesmith SmallString<128> PathName; 1850524e33e1SDouglas Gregor PathName = Directory->getName(); 1851524e33e1SDouglas Gregor llvm::sys::path::append(PathName, DirName); 1852524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(PathName); 1853524e33e1SDouglas Gregor } 1854524e33e1SDouglas Gregor 1855524e33e1SDouglas Gregor if (!Dir) { 1856524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1857524e33e1SDouglas Gregor << DirName; 1858524e33e1SDouglas Gregor HadError = true; 1859524e33e1SDouglas Gregor return; 1860524e33e1SDouglas Gregor } 1861524e33e1SDouglas Gregor 1862524e33e1SDouglas Gregor if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1863524e33e1SDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1864524e33e1SDouglas Gregor << OwningModule->getFullModuleName(); 1865524e33e1SDouglas Gregor HadError = true; 1866524e33e1SDouglas Gregor return; 1867524e33e1SDouglas Gregor } 1868524e33e1SDouglas Gregor 1869524e33e1SDouglas Gregor // Record this umbrella directory. 1870524e33e1SDouglas Gregor Map.setUmbrellaDir(ActiveModule, Dir); 1871524e33e1SDouglas Gregor } 1872524e33e1SDouglas Gregor 18732b82c2a5SDouglas Gregor /// \brief Parse a module export declaration. 18742b82c2a5SDouglas Gregor /// 18752b82c2a5SDouglas Gregor /// export-declaration: 18762b82c2a5SDouglas Gregor /// 'export' wildcard-module-id 18772b82c2a5SDouglas Gregor /// 18782b82c2a5SDouglas Gregor /// wildcard-module-id: 18792b82c2a5SDouglas Gregor /// identifier 18802b82c2a5SDouglas Gregor /// '*' 18812b82c2a5SDouglas Gregor /// identifier '.' wildcard-module-id 18822b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() { 18832b82c2a5SDouglas Gregor assert(Tok.is(MMToken::ExportKeyword)); 18842b82c2a5SDouglas Gregor SourceLocation ExportLoc = consumeToken(); 18852b82c2a5SDouglas Gregor 18862b82c2a5SDouglas Gregor // Parse the module-id with an optional wildcard at the end. 18872b82c2a5SDouglas Gregor ModuleId ParsedModuleId; 18882b82c2a5SDouglas Gregor bool Wildcard = false; 18892b82c2a5SDouglas Gregor do { 1890306d8920SRichard Smith // FIXME: Support string-literal module names here. 18912b82c2a5SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 18922b82c2a5SDouglas Gregor ParsedModuleId.push_back(std::make_pair(Tok.getString(), 18932b82c2a5SDouglas Gregor Tok.getLocation())); 18942b82c2a5SDouglas Gregor consumeToken(); 18952b82c2a5SDouglas Gregor 18962b82c2a5SDouglas Gregor if (Tok.is(MMToken::Period)) { 18972b82c2a5SDouglas Gregor consumeToken(); 18982b82c2a5SDouglas Gregor continue; 18992b82c2a5SDouglas Gregor } 19002b82c2a5SDouglas Gregor 19012b82c2a5SDouglas Gregor break; 19022b82c2a5SDouglas Gregor } 19032b82c2a5SDouglas Gregor 19042b82c2a5SDouglas Gregor if(Tok.is(MMToken::Star)) { 19052b82c2a5SDouglas Gregor Wildcard = true; 1906f5eedd05SDouglas Gregor consumeToken(); 19072b82c2a5SDouglas Gregor break; 19082b82c2a5SDouglas Gregor } 19092b82c2a5SDouglas Gregor 1910ba7f2f71SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); 19112b82c2a5SDouglas Gregor HadError = true; 19122b82c2a5SDouglas Gregor return; 19132b82c2a5SDouglas Gregor } while (true); 19142b82c2a5SDouglas Gregor 19152b82c2a5SDouglas Gregor Module::UnresolvedExportDecl Unresolved = { 19162b82c2a5SDouglas Gregor ExportLoc, ParsedModuleId, Wildcard 19172b82c2a5SDouglas Gregor }; 19182b82c2a5SDouglas Gregor ActiveModule->UnresolvedExports.push_back(Unresolved); 19192b82c2a5SDouglas Gregor } 19202b82c2a5SDouglas Gregor 1921ba7f2f71SDaniel Jasper /// \brief Parse a module uses declaration. 1922ba7f2f71SDaniel Jasper /// 1923ba7f2f71SDaniel Jasper /// uses-declaration: 1924ba7f2f71SDaniel Jasper /// 'uses' wildcard-module-id 1925ba7f2f71SDaniel Jasper void ModuleMapParser::parseUseDecl() { 1926ba7f2f71SDaniel Jasper assert(Tok.is(MMToken::UseKeyword)); 1927ba7f2f71SDaniel Jasper consumeToken(); 1928ba7f2f71SDaniel Jasper // Parse the module-id. 1929ba7f2f71SDaniel Jasper ModuleId ParsedModuleId; 19303cd34c76SDaniel Jasper parseModuleId(ParsedModuleId); 1931ba7f2f71SDaniel Jasper 1932ba7f2f71SDaniel Jasper ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId); 1933ba7f2f71SDaniel Jasper } 1934ba7f2f71SDaniel Jasper 19356ddfca91SDouglas Gregor /// \brief Parse a link declaration. 19366ddfca91SDouglas Gregor /// 19376ddfca91SDouglas Gregor /// module-declaration: 19386ddfca91SDouglas Gregor /// 'link' 'framework'[opt] string-literal 19396ddfca91SDouglas Gregor void ModuleMapParser::parseLinkDecl() { 19406ddfca91SDouglas Gregor assert(Tok.is(MMToken::LinkKeyword)); 19416ddfca91SDouglas Gregor SourceLocation LinkLoc = consumeToken(); 19426ddfca91SDouglas Gregor 19436ddfca91SDouglas Gregor // Parse the optional 'framework' keyword. 19446ddfca91SDouglas Gregor bool IsFramework = false; 19456ddfca91SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 19466ddfca91SDouglas Gregor consumeToken(); 19476ddfca91SDouglas Gregor IsFramework = true; 19486ddfca91SDouglas Gregor } 19496ddfca91SDouglas Gregor 19506ddfca91SDouglas Gregor // Parse the library name 19516ddfca91SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 19526ddfca91SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) 19536ddfca91SDouglas Gregor << IsFramework << SourceRange(LinkLoc); 19546ddfca91SDouglas Gregor HadError = true; 19556ddfca91SDouglas Gregor return; 19566ddfca91SDouglas Gregor } 19576ddfca91SDouglas Gregor 19586ddfca91SDouglas Gregor std::string LibraryName = Tok.getString(); 19596ddfca91SDouglas Gregor consumeToken(); 19606ddfca91SDouglas Gregor ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, 19616ddfca91SDouglas Gregor IsFramework)); 19626ddfca91SDouglas Gregor } 19636ddfca91SDouglas Gregor 196435b13eceSDouglas Gregor /// \brief Parse a configuration macro declaration. 196535b13eceSDouglas Gregor /// 196635b13eceSDouglas Gregor /// module-declaration: 196735b13eceSDouglas Gregor /// 'config_macros' attributes[opt] config-macro-list? 196835b13eceSDouglas Gregor /// 196935b13eceSDouglas Gregor /// config-macro-list: 197035b13eceSDouglas Gregor /// identifier (',' identifier)? 197135b13eceSDouglas Gregor void ModuleMapParser::parseConfigMacros() { 197235b13eceSDouglas Gregor assert(Tok.is(MMToken::ConfigMacros)); 197335b13eceSDouglas Gregor SourceLocation ConfigMacrosLoc = consumeToken(); 197435b13eceSDouglas Gregor 197535b13eceSDouglas Gregor // Only top-level modules can have configuration macros. 197635b13eceSDouglas Gregor if (ActiveModule->Parent) { 197735b13eceSDouglas Gregor Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule); 197835b13eceSDouglas Gregor } 197935b13eceSDouglas Gregor 198035b13eceSDouglas Gregor // Parse the optional attributes. 198135b13eceSDouglas Gregor Attributes Attrs; 198235b13eceSDouglas Gregor parseOptionalAttributes(Attrs); 198335b13eceSDouglas Gregor if (Attrs.IsExhaustive && !ActiveModule->Parent) { 198435b13eceSDouglas Gregor ActiveModule->ConfigMacrosExhaustive = true; 198535b13eceSDouglas Gregor } 198635b13eceSDouglas Gregor 198735b13eceSDouglas Gregor // If we don't have an identifier, we're done. 1988306d8920SRichard Smith // FIXME: Support macros with the same name as a keyword here. 198935b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) 199035b13eceSDouglas Gregor return; 199135b13eceSDouglas Gregor 199235b13eceSDouglas Gregor // Consume the first identifier. 199335b13eceSDouglas Gregor if (!ActiveModule->Parent) { 199435b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 199535b13eceSDouglas Gregor } 199635b13eceSDouglas Gregor consumeToken(); 199735b13eceSDouglas Gregor 199835b13eceSDouglas Gregor do { 199935b13eceSDouglas Gregor // If there's a comma, consume it. 200035b13eceSDouglas Gregor if (!Tok.is(MMToken::Comma)) 200135b13eceSDouglas Gregor break; 200235b13eceSDouglas Gregor consumeToken(); 200335b13eceSDouglas Gregor 200435b13eceSDouglas Gregor // We expect to see a macro name here. 2005306d8920SRichard Smith // FIXME: Support macros with the same name as a keyword here. 200635b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 200735b13eceSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro); 200835b13eceSDouglas Gregor break; 200935b13eceSDouglas Gregor } 201035b13eceSDouglas Gregor 201135b13eceSDouglas Gregor // Consume the macro name. 201235b13eceSDouglas Gregor if (!ActiveModule->Parent) { 201335b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 201435b13eceSDouglas Gregor } 201535b13eceSDouglas Gregor consumeToken(); 201635b13eceSDouglas Gregor } while (true); 201735b13eceSDouglas Gregor } 201835b13eceSDouglas Gregor 2019fb912657SDouglas Gregor /// \brief Format a module-id into a string. 2020fb912657SDouglas Gregor static std::string formatModuleId(const ModuleId &Id) { 2021fb912657SDouglas Gregor std::string result; 2022fb912657SDouglas Gregor { 2023fb912657SDouglas Gregor llvm::raw_string_ostream OS(result); 2024fb912657SDouglas Gregor 2025fb912657SDouglas Gregor for (unsigned I = 0, N = Id.size(); I != N; ++I) { 2026fb912657SDouglas Gregor if (I) 2027fb912657SDouglas Gregor OS << "."; 2028fb912657SDouglas Gregor OS << Id[I].first; 2029fb912657SDouglas Gregor } 2030fb912657SDouglas Gregor } 2031fb912657SDouglas Gregor 2032fb912657SDouglas Gregor return result; 2033fb912657SDouglas Gregor } 2034fb912657SDouglas Gregor 2035fb912657SDouglas Gregor /// \brief Parse a conflict declaration. 2036fb912657SDouglas Gregor /// 2037fb912657SDouglas Gregor /// module-declaration: 2038fb912657SDouglas Gregor /// 'conflict' module-id ',' string-literal 2039fb912657SDouglas Gregor void ModuleMapParser::parseConflict() { 2040fb912657SDouglas Gregor assert(Tok.is(MMToken::Conflict)); 2041fb912657SDouglas Gregor SourceLocation ConflictLoc = consumeToken(); 2042fb912657SDouglas Gregor Module::UnresolvedConflict Conflict; 2043fb912657SDouglas Gregor 2044fb912657SDouglas Gregor // Parse the module-id. 2045fb912657SDouglas Gregor if (parseModuleId(Conflict.Id)) 2046fb912657SDouglas Gregor return; 2047fb912657SDouglas Gregor 2048fb912657SDouglas Gregor // Parse the ','. 2049fb912657SDouglas Gregor if (!Tok.is(MMToken::Comma)) { 2050fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma) 2051fb912657SDouglas Gregor << SourceRange(ConflictLoc); 2052fb912657SDouglas Gregor return; 2053fb912657SDouglas Gregor } 2054fb912657SDouglas Gregor consumeToken(); 2055fb912657SDouglas Gregor 2056fb912657SDouglas Gregor // Parse the message. 2057fb912657SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 2058fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message) 2059fb912657SDouglas Gregor << formatModuleId(Conflict.Id); 2060fb912657SDouglas Gregor return; 2061fb912657SDouglas Gregor } 2062fb912657SDouglas Gregor Conflict.Message = Tok.getString().str(); 2063fb912657SDouglas Gregor consumeToken(); 2064fb912657SDouglas Gregor 2065fb912657SDouglas Gregor // Add this unresolved conflict. 2066fb912657SDouglas Gregor ActiveModule->UnresolvedConflicts.push_back(Conflict); 2067fb912657SDouglas Gregor } 2068fb912657SDouglas Gregor 20696ddfca91SDouglas Gregor /// \brief Parse an inferred module declaration (wildcard modules). 20709194a91dSDouglas Gregor /// 20719194a91dSDouglas Gregor /// module-declaration: 20729194a91dSDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] 20739194a91dSDouglas Gregor /// { inferred-module-member* } 20749194a91dSDouglas Gregor /// 20759194a91dSDouglas Gregor /// inferred-module-member: 20769194a91dSDouglas Gregor /// 'export' '*' 20779194a91dSDouglas Gregor /// 'exclude' identifier 20789194a91dSDouglas Gregor void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { 207973441091SDouglas Gregor assert(Tok.is(MMToken::Star)); 208073441091SDouglas Gregor SourceLocation StarLoc = consumeToken(); 208173441091SDouglas Gregor bool Failed = false; 208273441091SDouglas Gregor 208373441091SDouglas Gregor // Inferred modules must be submodules. 20849194a91dSDouglas Gregor if (!ActiveModule && !Framework) { 208573441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 208673441091SDouglas Gregor Failed = true; 208773441091SDouglas Gregor } 208873441091SDouglas Gregor 20899194a91dSDouglas Gregor if (ActiveModule) { 2090524e33e1SDouglas Gregor // Inferred modules must have umbrella directories. 20914898cde4SBen Langmuir if (!Failed && ActiveModule->IsAvailable && 20924898cde4SBen Langmuir !ActiveModule->getUmbrellaDir()) { 209373441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 209473441091SDouglas Gregor Failed = true; 209573441091SDouglas Gregor } 209673441091SDouglas Gregor 209773441091SDouglas Gregor // Check for redefinition of an inferred module. 2098dd005f69SDouglas Gregor if (!Failed && ActiveModule->InferSubmodules) { 209973441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 2100dd005f69SDouglas Gregor if (ActiveModule->InferredSubmoduleLoc.isValid()) 2101dd005f69SDouglas Gregor Diags.Report(ActiveModule->InferredSubmoduleLoc, 210273441091SDouglas Gregor diag::note_mmap_prev_definition); 210373441091SDouglas Gregor Failed = true; 210473441091SDouglas Gregor } 210573441091SDouglas Gregor 21069194a91dSDouglas Gregor // Check for the 'framework' keyword, which is not permitted here. 21079194a91dSDouglas Gregor if (Framework) { 21089194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); 21099194a91dSDouglas Gregor Framework = false; 21109194a91dSDouglas Gregor } 21119194a91dSDouglas Gregor } else if (Explicit) { 21129194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); 21139194a91dSDouglas Gregor Explicit = false; 21149194a91dSDouglas Gregor } 21159194a91dSDouglas Gregor 211673441091SDouglas Gregor // If there were any problems with this inferred submodule, skip its body. 211773441091SDouglas Gregor if (Failed) { 211873441091SDouglas Gregor if (Tok.is(MMToken::LBrace)) { 211973441091SDouglas Gregor consumeToken(); 212073441091SDouglas Gregor skipUntil(MMToken::RBrace); 212173441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 212273441091SDouglas Gregor consumeToken(); 212373441091SDouglas Gregor } 212473441091SDouglas Gregor HadError = true; 212573441091SDouglas Gregor return; 212673441091SDouglas Gregor } 212773441091SDouglas Gregor 21289194a91dSDouglas Gregor // Parse optional attributes. 21294442605fSBill Wendling Attributes Attrs; 21309194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 21319194a91dSDouglas Gregor 21329194a91dSDouglas Gregor if (ActiveModule) { 213373441091SDouglas Gregor // Note that we have an inferred submodule. 2134dd005f69SDouglas Gregor ActiveModule->InferSubmodules = true; 2135dd005f69SDouglas Gregor ActiveModule->InferredSubmoduleLoc = StarLoc; 2136dd005f69SDouglas Gregor ActiveModule->InferExplicitSubmodules = Explicit; 21379194a91dSDouglas Gregor } else { 21389194a91dSDouglas Gregor // We'll be inferring framework modules for this directory. 21399194a91dSDouglas Gregor Map.InferredDirectories[Directory].InferModules = true; 2140c1d88ea5SBen Langmuir Map.InferredDirectories[Directory].Attrs = Attrs; 2141beee15e7SBen Langmuir Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile; 2142131daca0SRichard Smith // FIXME: Handle the 'framework' keyword. 21439194a91dSDouglas Gregor } 214473441091SDouglas Gregor 214573441091SDouglas Gregor // Parse the opening brace. 214673441091SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 214773441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 214873441091SDouglas Gregor HadError = true; 214973441091SDouglas Gregor return; 215073441091SDouglas Gregor } 215173441091SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 215273441091SDouglas Gregor 215373441091SDouglas Gregor // Parse the body of the inferred submodule. 215473441091SDouglas Gregor bool Done = false; 215573441091SDouglas Gregor do { 215673441091SDouglas Gregor switch (Tok.Kind) { 215773441091SDouglas Gregor case MMToken::EndOfFile: 215873441091SDouglas Gregor case MMToken::RBrace: 215973441091SDouglas Gregor Done = true; 216073441091SDouglas Gregor break; 216173441091SDouglas Gregor 21629194a91dSDouglas Gregor case MMToken::ExcludeKeyword: { 21639194a91dSDouglas Gregor if (ActiveModule) { 21649194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2165d2d442caSCraig Topper << (ActiveModule != nullptr); 21669194a91dSDouglas Gregor consumeToken(); 21679194a91dSDouglas Gregor break; 21689194a91dSDouglas Gregor } 21699194a91dSDouglas Gregor 21709194a91dSDouglas Gregor consumeToken(); 2171306d8920SRichard Smith // FIXME: Support string-literal module names here. 21729194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 21739194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); 21749194a91dSDouglas Gregor break; 21759194a91dSDouglas Gregor } 21769194a91dSDouglas Gregor 21779194a91dSDouglas Gregor Map.InferredDirectories[Directory].ExcludedModules 21789194a91dSDouglas Gregor .push_back(Tok.getString()); 21799194a91dSDouglas Gregor consumeToken(); 21809194a91dSDouglas Gregor break; 21819194a91dSDouglas Gregor } 21829194a91dSDouglas Gregor 21839194a91dSDouglas Gregor case MMToken::ExportKeyword: 21849194a91dSDouglas Gregor if (!ActiveModule) { 21859194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2186d2d442caSCraig Topper << (ActiveModule != nullptr); 21879194a91dSDouglas Gregor consumeToken(); 21889194a91dSDouglas Gregor break; 21899194a91dSDouglas Gregor } 21909194a91dSDouglas Gregor 219173441091SDouglas Gregor consumeToken(); 219273441091SDouglas Gregor if (Tok.is(MMToken::Star)) 2193dd005f69SDouglas Gregor ActiveModule->InferExportWildcard = true; 219473441091SDouglas Gregor else 219573441091SDouglas Gregor Diags.Report(Tok.getLocation(), 219673441091SDouglas Gregor diag::err_mmap_expected_export_wildcard); 219773441091SDouglas Gregor consumeToken(); 219873441091SDouglas Gregor break; 219973441091SDouglas Gregor 220073441091SDouglas Gregor case MMToken::ExplicitKeyword: 220173441091SDouglas Gregor case MMToken::ModuleKeyword: 220273441091SDouglas Gregor case MMToken::HeaderKeyword: 2203b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 220473441091SDouglas Gregor case MMToken::UmbrellaKeyword: 220573441091SDouglas Gregor default: 22069194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2207d2d442caSCraig Topper << (ActiveModule != nullptr); 220873441091SDouglas Gregor consumeToken(); 220973441091SDouglas Gregor break; 221073441091SDouglas Gregor } 221173441091SDouglas Gregor } while (!Done); 221273441091SDouglas Gregor 221373441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 221473441091SDouglas Gregor consumeToken(); 221573441091SDouglas Gregor else { 221673441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 221773441091SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 221873441091SDouglas Gregor HadError = true; 221973441091SDouglas Gregor } 222073441091SDouglas Gregor } 222173441091SDouglas Gregor 22229194a91dSDouglas Gregor /// \brief Parse optional attributes. 22239194a91dSDouglas Gregor /// 22249194a91dSDouglas Gregor /// attributes: 22259194a91dSDouglas Gregor /// attribute attributes 22269194a91dSDouglas Gregor /// attribute 22279194a91dSDouglas Gregor /// 22289194a91dSDouglas Gregor /// attribute: 22299194a91dSDouglas Gregor /// [ identifier ] 22309194a91dSDouglas Gregor /// 22319194a91dSDouglas Gregor /// \param Attrs Will be filled in with the parsed attributes. 22329194a91dSDouglas Gregor /// 22339194a91dSDouglas Gregor /// \returns true if an error occurred, false otherwise. 22344442605fSBill Wendling bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { 22359194a91dSDouglas Gregor bool HadError = false; 22369194a91dSDouglas Gregor 22379194a91dSDouglas Gregor while (Tok.is(MMToken::LSquare)) { 22389194a91dSDouglas Gregor // Consume the '['. 22399194a91dSDouglas Gregor SourceLocation LSquareLoc = consumeToken(); 22409194a91dSDouglas Gregor 22419194a91dSDouglas Gregor // Check whether we have an attribute name here. 22429194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 22439194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 22449194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 22459194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 22469194a91dSDouglas Gregor consumeToken(); 22479194a91dSDouglas Gregor HadError = true; 22489194a91dSDouglas Gregor } 22499194a91dSDouglas Gregor 22509194a91dSDouglas Gregor // Decode the attribute name. 22519194a91dSDouglas Gregor AttributeKind Attribute 22529194a91dSDouglas Gregor = llvm::StringSwitch<AttributeKind>(Tok.getString()) 225335b13eceSDouglas Gregor .Case("exhaustive", AT_exhaustive) 225477944868SRichard Smith .Case("extern_c", AT_extern_c) 22559194a91dSDouglas Gregor .Case("system", AT_system) 22569194a91dSDouglas Gregor .Default(AT_unknown); 22579194a91dSDouglas Gregor switch (Attribute) { 22589194a91dSDouglas Gregor case AT_unknown: 22599194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 22609194a91dSDouglas Gregor << Tok.getString(); 22619194a91dSDouglas Gregor break; 22629194a91dSDouglas Gregor 22639194a91dSDouglas Gregor case AT_system: 22649194a91dSDouglas Gregor Attrs.IsSystem = true; 22659194a91dSDouglas Gregor break; 226635b13eceSDouglas Gregor 226777944868SRichard Smith case AT_extern_c: 226877944868SRichard Smith Attrs.IsExternC = true; 226977944868SRichard Smith break; 227077944868SRichard Smith 227135b13eceSDouglas Gregor case AT_exhaustive: 227235b13eceSDouglas Gregor Attrs.IsExhaustive = true; 227335b13eceSDouglas Gregor break; 22749194a91dSDouglas Gregor } 22759194a91dSDouglas Gregor consumeToken(); 22769194a91dSDouglas Gregor 22779194a91dSDouglas Gregor // Consume the ']'. 22789194a91dSDouglas Gregor if (!Tok.is(MMToken::RSquare)) { 22799194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 22809194a91dSDouglas Gregor Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 22819194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 22829194a91dSDouglas Gregor HadError = true; 22839194a91dSDouglas Gregor } 22849194a91dSDouglas Gregor 22859194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 22869194a91dSDouglas Gregor consumeToken(); 22879194a91dSDouglas Gregor } 22889194a91dSDouglas Gregor 22899194a91dSDouglas Gregor return HadError; 22909194a91dSDouglas Gregor } 22919194a91dSDouglas Gregor 2292718292f2SDouglas Gregor /// \brief Parse a module map file. 2293718292f2SDouglas Gregor /// 2294718292f2SDouglas Gregor /// module-map-file: 2295718292f2SDouglas Gregor /// module-declaration* 2296718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() { 2297718292f2SDouglas Gregor do { 2298718292f2SDouglas Gregor switch (Tok.Kind) { 2299718292f2SDouglas Gregor case MMToken::EndOfFile: 2300718292f2SDouglas Gregor return HadError; 2301718292f2SDouglas Gregor 2302e7ab3669SDouglas Gregor case MMToken::ExplicitKeyword: 230397292843SDaniel Jasper case MMToken::ExternKeyword: 2304718292f2SDouglas Gregor case MMToken::ModuleKeyword: 2305755b2055SDouglas Gregor case MMToken::FrameworkKeyword: 2306718292f2SDouglas Gregor parseModuleDecl(); 2307718292f2SDouglas Gregor break; 2308718292f2SDouglas Gregor 23091fb5c3a6SDouglas Gregor case MMToken::Comma: 231035b13eceSDouglas Gregor case MMToken::ConfigMacros: 2311fb912657SDouglas Gregor case MMToken::Conflict: 2312a3feee2aSRichard Smith case MMToken::Exclaim: 231359527666SDouglas Gregor case MMToken::ExcludeKeyword: 23142b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 2315718292f2SDouglas Gregor case MMToken::HeaderKeyword: 2316718292f2SDouglas Gregor case MMToken::Identifier: 2317718292f2SDouglas Gregor case MMToken::LBrace: 23186ddfca91SDouglas Gregor case MMToken::LinkKeyword: 2319a686e1b0SDouglas Gregor case MMToken::LSquare: 23202b82c2a5SDouglas Gregor case MMToken::Period: 2321b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 2322718292f2SDouglas Gregor case MMToken::RBrace: 2323a686e1b0SDouglas Gregor case MMToken::RSquare: 23241fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 23252b82c2a5SDouglas Gregor case MMToken::Star: 2326718292f2SDouglas Gregor case MMToken::StringLiteral: 2327b8afebe2SRichard Smith case MMToken::TextualKeyword: 2328718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 2329ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 2330718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 2331718292f2SDouglas Gregor HadError = true; 2332718292f2SDouglas Gregor consumeToken(); 2333718292f2SDouglas Gregor break; 2334718292f2SDouglas Gregor } 2335718292f2SDouglas Gregor } while (true); 2336718292f2SDouglas Gregor } 2337718292f2SDouglas Gregor 23389acb99e3SRichard Smith bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem, 23399acb99e3SRichard Smith const DirectoryEntry *Dir) { 23404ddf2221SDouglas Gregor llvm::DenseMap<const FileEntry *, bool>::iterator Known 23414ddf2221SDouglas Gregor = ParsedModuleMap.find(File); 23424ddf2221SDouglas Gregor if (Known != ParsedModuleMap.end()) 23434ddf2221SDouglas Gregor return Known->second; 23444ddf2221SDouglas Gregor 2345d2d442caSCraig Topper assert(Target && "Missing target information"); 2346cb69b57bSBen Langmuir auto FileCharacter = IsSystem ? SrcMgr::C_System : SrcMgr::C_User; 2347cb69b57bSBen Langmuir FileID ID = SourceMgr.createFileID(File, SourceLocation(), FileCharacter); 23481f76c4e8SManuel Klimek const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID); 2349718292f2SDouglas Gregor if (!Buffer) 23504ddf2221SDouglas Gregor return ParsedModuleMap[File] = true; 2351718292f2SDouglas Gregor 2352718292f2SDouglas Gregor // Parse this module map file. 23531f76c4e8SManuel Klimek Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts); 2354beee15e7SBen Langmuir ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir, 2355963c5535SDouglas Gregor BuiltinIncludeDir, IsSystem); 2356718292f2SDouglas Gregor bool Result = Parser.parseModuleMapFile(); 23574ddf2221SDouglas Gregor ParsedModuleMap[File] = Result; 2358718292f2SDouglas Gregor return Result; 2359718292f2SDouglas Gregor } 2360