1718292f2SDouglas Gregor //===--- ModuleMap.cpp - Describe the layout of modules ---------*- C++ -*-===// 2718292f2SDouglas Gregor // 3718292f2SDouglas Gregor // The LLVM Compiler Infrastructure 4718292f2SDouglas Gregor // 5718292f2SDouglas Gregor // This file is distributed under the University of Illinois Open Source 6718292f2SDouglas Gregor // License. See LICENSE.TXT for details. 7718292f2SDouglas Gregor // 8718292f2SDouglas Gregor //===----------------------------------------------------------------------===// 9718292f2SDouglas Gregor // 10718292f2SDouglas Gregor // This file defines the ModuleMap implementation, which describes the layout 11718292f2SDouglas Gregor // of a module as it relates to headers. 12718292f2SDouglas Gregor // 13718292f2SDouglas Gregor //===----------------------------------------------------------------------===// 14718292f2SDouglas Gregor #include "clang/Lex/ModuleMap.h" 15a7d03840SJordan Rose #include "clang/Basic/CharInfo.h" 16718292f2SDouglas Gregor #include "clang/Basic/Diagnostic.h" 17811db4eaSDouglas Gregor #include "clang/Basic/DiagnosticOptions.h" 18718292f2SDouglas Gregor #include "clang/Basic/FileManager.h" 19718292f2SDouglas Gregor #include "clang/Basic/TargetInfo.h" 20718292f2SDouglas Gregor #include "clang/Basic/TargetOptions.h" 21b146baabSArgyrios Kyrtzidis #include "clang/Lex/HeaderSearch.h" 229acb99e3SRichard Smith #include "clang/Lex/HeaderSearchOptions.h" 233a02247dSChandler Carruth #include "clang/Lex/LexDiagnostic.h" 243a02247dSChandler Carruth #include "clang/Lex/Lexer.h" 253a02247dSChandler Carruth #include "clang/Lex/LiteralSupport.h" 263a02247dSChandler Carruth #include "llvm/ADT/StringRef.h" 273a02247dSChandler Carruth #include "llvm/ADT/StringSwitch.h" 28718292f2SDouglas Gregor #include "llvm/Support/Allocator.h" 29e89dbc1dSDouglas Gregor #include "llvm/Support/FileSystem.h" 30718292f2SDouglas Gregor #include "llvm/Support/Host.h" 31552c169eSRafael Espindola #include "llvm/Support/Path.h" 32718292f2SDouglas Gregor #include "llvm/Support/raw_ostream.h" 3307c22b78SDouglas Gregor #include <stdlib.h> 3401c7cfa2SDouglas Gregor #if defined(LLVM_ON_UNIX) 35eadae014SDmitri Gribenko #include <limits.h> 3601c7cfa2SDouglas Gregor #endif 37718292f2SDouglas Gregor using namespace clang; 38718292f2SDouglas Gregor 392b82c2a5SDouglas Gregor Module::ExportDecl 402b82c2a5SDouglas Gregor ModuleMap::resolveExport(Module *Mod, 412b82c2a5SDouglas Gregor const Module::UnresolvedExportDecl &Unresolved, 42e4412640SArgyrios Kyrtzidis bool Complain) const { 43f5eedd05SDouglas Gregor // We may have just a wildcard. 44f5eedd05SDouglas Gregor if (Unresolved.Id.empty()) { 45f5eedd05SDouglas Gregor assert(Unresolved.Wildcard && "Invalid unresolved export"); 46d2d442caSCraig Topper return Module::ExportDecl(nullptr, true); 47f5eedd05SDouglas Gregor } 48f5eedd05SDouglas Gregor 49fb912657SDouglas Gregor // Resolve the module-id. 50fb912657SDouglas Gregor Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain); 51fb912657SDouglas Gregor if (!Context) 52fb912657SDouglas Gregor return Module::ExportDecl(); 53fb912657SDouglas Gregor 54fb912657SDouglas Gregor return Module::ExportDecl(Context, Unresolved.Wildcard); 55fb912657SDouglas Gregor } 56fb912657SDouglas Gregor 57fb912657SDouglas Gregor Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod, 58fb912657SDouglas Gregor bool Complain) const { 592b82c2a5SDouglas Gregor // Find the starting module. 60fb912657SDouglas Gregor Module *Context = lookupModuleUnqualified(Id[0].first, Mod); 612b82c2a5SDouglas Gregor if (!Context) { 622b82c2a5SDouglas Gregor if (Complain) 630761a8a0SDaniel Jasper Diags.Report(Id[0].second, diag::err_mmap_missing_module_unqualified) 64fb912657SDouglas Gregor << Id[0].first << Mod->getFullModuleName(); 652b82c2a5SDouglas Gregor 66d2d442caSCraig Topper return nullptr; 672b82c2a5SDouglas Gregor } 682b82c2a5SDouglas Gregor 692b82c2a5SDouglas Gregor // Dig into the module path. 70fb912657SDouglas Gregor for (unsigned I = 1, N = Id.size(); I != N; ++I) { 71fb912657SDouglas Gregor Module *Sub = lookupModuleQualified(Id[I].first, Context); 722b82c2a5SDouglas Gregor if (!Sub) { 732b82c2a5SDouglas Gregor if (Complain) 740761a8a0SDaniel Jasper Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 75fb912657SDouglas Gregor << Id[I].first << Context->getFullModuleName() 76fb912657SDouglas Gregor << SourceRange(Id[0].second, Id[I-1].second); 772b82c2a5SDouglas Gregor 78d2d442caSCraig Topper return nullptr; 792b82c2a5SDouglas Gregor } 802b82c2a5SDouglas Gregor 812b82c2a5SDouglas Gregor Context = Sub; 822b82c2a5SDouglas Gregor } 832b82c2a5SDouglas Gregor 84fb912657SDouglas Gregor return Context; 852b82c2a5SDouglas Gregor } 862b82c2a5SDouglas Gregor 870761a8a0SDaniel Jasper ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags, 88b146baabSArgyrios Kyrtzidis const LangOptions &LangOpts, const TargetInfo *Target, 89b146baabSArgyrios Kyrtzidis HeaderSearch &HeaderInfo) 900761a8a0SDaniel Jasper : SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target), 91d2d442caSCraig Topper HeaderInfo(HeaderInfo), BuiltinIncludeDir(nullptr), 92a7e2cc68SRichard Smith CompilingModule(nullptr), SourceModule(nullptr), NumCreatedModules(0) { 930414b857SRichard Smith MMapLangOpts.LineComment = true; 940414b857SRichard Smith } 95718292f2SDouglas Gregor 96718292f2SDouglas Gregor ModuleMap::~ModuleMap() { 975acdf59eSDouglas Gregor for (llvm::StringMap<Module *>::iterator I = Modules.begin(), 985acdf59eSDouglas Gregor IEnd = Modules.end(); 995acdf59eSDouglas Gregor I != IEnd; ++I) { 1005acdf59eSDouglas Gregor delete I->getValue(); 1015acdf59eSDouglas Gregor } 102718292f2SDouglas Gregor } 103718292f2SDouglas Gregor 10489929282SDouglas Gregor void ModuleMap::setTarget(const TargetInfo &Target) { 10589929282SDouglas Gregor assert((!this->Target || this->Target == &Target) && 10689929282SDouglas Gregor "Improper target override"); 10789929282SDouglas Gregor this->Target = &Target; 10889929282SDouglas Gregor } 10989929282SDouglas Gregor 110056396aeSDouglas Gregor /// \brief "Sanitize" a filename so that it can be used as an identifier. 111056396aeSDouglas Gregor static StringRef sanitizeFilenameAsIdentifier(StringRef Name, 112056396aeSDouglas Gregor SmallVectorImpl<char> &Buffer) { 113056396aeSDouglas Gregor if (Name.empty()) 114056396aeSDouglas Gregor return Name; 115056396aeSDouglas Gregor 116a7d03840SJordan Rose if (!isValidIdentifier(Name)) { 117056396aeSDouglas Gregor // If we don't already have something with the form of an identifier, 118056396aeSDouglas Gregor // create a buffer with the sanitized name. 119056396aeSDouglas Gregor Buffer.clear(); 120a7d03840SJordan Rose if (isDigit(Name[0])) 121056396aeSDouglas Gregor Buffer.push_back('_'); 122056396aeSDouglas Gregor Buffer.reserve(Buffer.size() + Name.size()); 123056396aeSDouglas Gregor for (unsigned I = 0, N = Name.size(); I != N; ++I) { 124a7d03840SJordan Rose if (isIdentifierBody(Name[I])) 125056396aeSDouglas Gregor Buffer.push_back(Name[I]); 126056396aeSDouglas Gregor else 127056396aeSDouglas Gregor Buffer.push_back('_'); 128056396aeSDouglas Gregor } 129056396aeSDouglas Gregor 130056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 131056396aeSDouglas Gregor } 132056396aeSDouglas Gregor 133056396aeSDouglas Gregor while (llvm::StringSwitch<bool>(Name) 134056396aeSDouglas Gregor #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true) 135056396aeSDouglas Gregor #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true) 136056396aeSDouglas Gregor #include "clang/Basic/TokenKinds.def" 137056396aeSDouglas Gregor .Default(false)) { 138056396aeSDouglas Gregor if (Name.data() != Buffer.data()) 139056396aeSDouglas Gregor Buffer.append(Name.begin(), Name.end()); 140056396aeSDouglas Gregor Buffer.push_back('_'); 141056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 142056396aeSDouglas Gregor } 143056396aeSDouglas Gregor 144056396aeSDouglas Gregor return Name; 145056396aeSDouglas Gregor } 146056396aeSDouglas Gregor 14734d52749SDouglas Gregor /// \brief Determine whether the given file name is the name of a builtin 14834d52749SDouglas Gregor /// header, supplied by Clang to replace, override, or augment existing system 14934d52749SDouglas Gregor /// headers. 15034d52749SDouglas Gregor static bool isBuiltinHeader(StringRef FileName) { 15134d52749SDouglas Gregor return llvm::StringSwitch<bool>(FileName) 15234d52749SDouglas Gregor .Case("float.h", true) 15334d52749SDouglas Gregor .Case("iso646.h", true) 15434d52749SDouglas Gregor .Case("limits.h", true) 15534d52749SDouglas Gregor .Case("stdalign.h", true) 15634d52749SDouglas Gregor .Case("stdarg.h", true) 15734d52749SDouglas Gregor .Case("stdbool.h", true) 15834d52749SDouglas Gregor .Case("stddef.h", true) 15934d52749SDouglas Gregor .Case("stdint.h", true) 16034d52749SDouglas Gregor .Case("tgmath.h", true) 16134d52749SDouglas Gregor .Case("unwind.h", true) 16234d52749SDouglas Gregor .Default(false); 16334d52749SDouglas Gregor } 16434d52749SDouglas Gregor 16592669ee4SDaniel Jasper ModuleMap::HeadersMap::iterator 16692669ee4SDaniel Jasper ModuleMap::findKnownHeader(const FileEntry *File) { 16759527666SDouglas Gregor HeadersMap::iterator Known = Headers.find(File); 16847972afdSRichard Smith if (HeaderInfo.getHeaderSearchOpts().ImplicitModuleMaps && 16947972afdSRichard Smith Known == Headers.end() && File->getDir() == BuiltinIncludeDir && 1704eaf0a6cSDaniel Jasper isBuiltinHeader(llvm::sys::path::filename(File->getName()))) { 1714eaf0a6cSDaniel Jasper HeaderInfo.loadTopLevelSystemModules(); 17292669ee4SDaniel Jasper return Headers.find(File); 1734eaf0a6cSDaniel Jasper } 17492669ee4SDaniel Jasper return Known; 17592669ee4SDaniel Jasper } 17692669ee4SDaniel Jasper 1774469138eSBen Langmuir ModuleMap::KnownHeader 1784469138eSBen Langmuir ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File, 1794469138eSBen Langmuir SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs) { 18047972afdSRichard Smith if (UmbrellaDirs.empty()) 18147972afdSRichard Smith return KnownHeader(); 18247972afdSRichard Smith 1834469138eSBen Langmuir const DirectoryEntry *Dir = File->getDir(); 1844469138eSBen Langmuir assert(Dir && "file in no directory"); 1854469138eSBen Langmuir 1864469138eSBen Langmuir // Note: as an egregious but useful hack we use the real path here, because 1874469138eSBen Langmuir // frameworks moving from top-level frameworks to embedded frameworks tend 1884469138eSBen Langmuir // to be symlinked from the top-level location to the embedded location, 1894469138eSBen Langmuir // and we need to resolve lookups as if we had found the embedded location. 1904469138eSBen Langmuir StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir); 1914469138eSBen Langmuir 1924469138eSBen Langmuir // Keep walking up the directory hierarchy, looking for a directory with 1934469138eSBen Langmuir // an umbrella header. 1944469138eSBen Langmuir do { 1954469138eSBen Langmuir auto KnownDir = UmbrellaDirs.find(Dir); 1964469138eSBen Langmuir if (KnownDir != UmbrellaDirs.end()) 1974469138eSBen Langmuir return KnownHeader(KnownDir->second, NormalHeader); 1984469138eSBen Langmuir 1994469138eSBen Langmuir IntermediateDirs.push_back(Dir); 2004469138eSBen Langmuir 2014469138eSBen Langmuir // Retrieve our parent path. 2024469138eSBen Langmuir DirName = llvm::sys::path::parent_path(DirName); 2034469138eSBen Langmuir if (DirName.empty()) 2044469138eSBen Langmuir break; 2054469138eSBen Langmuir 2064469138eSBen Langmuir // Resolve the parent path to a directory entry. 2074469138eSBen Langmuir Dir = SourceMgr.getFileManager().getDirectory(DirName); 2084469138eSBen Langmuir } while (Dir); 2094469138eSBen Langmuir return KnownHeader(); 2104469138eSBen Langmuir } 2114469138eSBen Langmuir 21292669ee4SDaniel Jasper static bool violatesPrivateInclude(Module *RequestingModule, 21392669ee4SDaniel Jasper const FileEntry *IncFileEnt, 21492669ee4SDaniel Jasper ModuleMap::ModuleHeaderRole Role, 21592669ee4SDaniel Jasper Module *RequestedModule) { 216202210b3SRichard Smith bool IsPrivateRole = Role & ModuleMap::PrivateHeader; 21792669ee4SDaniel Jasper #ifndef NDEBUG 2182708e520SRichard Smith if (IsPrivateRole) { 21992669ee4SDaniel Jasper // Check for consistency between the module header role 22092669ee4SDaniel Jasper // as obtained from the lookup and as obtained from the module. 22192669ee4SDaniel Jasper // This check is not cheap, so enable it only for debugging. 2222708e520SRichard Smith bool IsPrivate = false; 2232708e520SRichard Smith SmallVectorImpl<Module::Header> *HeaderList[] = { 2242708e520SRichard Smith &RequestedModule->Headers[Module::HK_Private], 2252708e520SRichard Smith &RequestedModule->Headers[Module::HK_PrivateTextual]}; 2262708e520SRichard Smith for (auto *Hs : HeaderList) 2272708e520SRichard Smith IsPrivate |= 2282708e520SRichard Smith std::find_if(Hs->begin(), Hs->end(), [&](const Module::Header &H) { 2293c1a41adSRichard Smith return H.Entry == IncFileEnt; 2302708e520SRichard Smith }) != Hs->end(); 2312708e520SRichard Smith assert((!IsPrivateRole || IsPrivate) && "inconsistent headers and roles"); 23200bc95ecSRichard Smith } 23392669ee4SDaniel Jasper #endif 234e8bd0db6SManuel Klimek return IsPrivateRole && (!RequestingModule || 235e8bd0db6SManuel Klimek RequestedModule->getTopLevelModule() != 236e8bd0db6SManuel Klimek RequestingModule->getTopLevelModule()); 23792669ee4SDaniel Jasper } 23892669ee4SDaniel Jasper 23971e1a64fSBen Langmuir static Module *getTopLevelOrNull(Module *M) { 24071e1a64fSBen Langmuir return M ? M->getTopLevelModule() : nullptr; 24171e1a64fSBen Langmuir } 24271e1a64fSBen Langmuir 24392669ee4SDaniel Jasper void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule, 24492669ee4SDaniel Jasper SourceLocation FilenameLoc, 24592669ee4SDaniel Jasper StringRef Filename, 24692669ee4SDaniel Jasper const FileEntry *File) { 24792669ee4SDaniel Jasper // No errors for indirect modules. This may be a bit of a problem for modules 24892669ee4SDaniel Jasper // with no source files. 24971e1a64fSBen Langmuir if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule)) 25092669ee4SDaniel Jasper return; 25192669ee4SDaniel Jasper 25292669ee4SDaniel Jasper if (RequestingModule) 25392669ee4SDaniel Jasper resolveUses(RequestingModule, /*Complain=*/false); 25492669ee4SDaniel Jasper 25571e1a64fSBen Langmuir bool Excluded = false; 256d2d442caSCraig Topper Module *Private = nullptr; 257d2d442caSCraig Topper Module *NotUsed = nullptr; 25871e1a64fSBen Langmuir 25971e1a64fSBen Langmuir HeadersMap::iterator Known = findKnownHeader(File); 26071e1a64fSBen Langmuir if (Known != Headers.end()) { 26171e1a64fSBen Langmuir for (const KnownHeader &Header : Known->second) { 26292669ee4SDaniel Jasper // Remember private headers for later printing of a diagnostic. 26371e1a64fSBen Langmuir if (violatesPrivateInclude(RequestingModule, File, Header.getRole(), 26471e1a64fSBen Langmuir Header.getModule())) { 26571e1a64fSBen Langmuir Private = Header.getModule(); 26692669ee4SDaniel Jasper continue; 26792669ee4SDaniel Jasper } 26892669ee4SDaniel Jasper 26992669ee4SDaniel Jasper // If uses need to be specified explicitly, we are only allowed to return 27092669ee4SDaniel Jasper // modules that are explicitly used by the requesting module. 27192669ee4SDaniel Jasper if (RequestingModule && LangOpts.ModulesDeclUse && 2728f4d3ff1SRichard Smith !RequestingModule->directlyUses(Header.getModule())) { 27371e1a64fSBen Langmuir NotUsed = Header.getModule(); 27492669ee4SDaniel Jasper continue; 27592669ee4SDaniel Jasper } 27692669ee4SDaniel Jasper 27792669ee4SDaniel Jasper // We have found a module that we can happily use. 27892669ee4SDaniel Jasper return; 27992669ee4SDaniel Jasper } 280feb54b6dSRichard Smith 281feb54b6dSRichard Smith Excluded = true; 28271e1a64fSBen Langmuir } 28392669ee4SDaniel Jasper 28492669ee4SDaniel Jasper // We have found a header, but it is private. 285d2d442caSCraig Topper if (Private) { 28611152dd5SRichard Smith Diags.Report(FilenameLoc, diag::warn_use_of_private_header_outside_module) 28792669ee4SDaniel Jasper << Filename; 28892669ee4SDaniel Jasper return; 28992669ee4SDaniel Jasper } 29092669ee4SDaniel Jasper 29192669ee4SDaniel Jasper // We have found a module, but we don't use it. 292d2d442caSCraig Topper if (NotUsed) { 29311152dd5SRichard Smith Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module) 29492669ee4SDaniel Jasper << RequestingModule->getFullModuleName() << Filename; 29592669ee4SDaniel Jasper return; 29692669ee4SDaniel Jasper } 29792669ee4SDaniel Jasper 29871e1a64fSBen Langmuir if (Excluded || isHeaderInUmbrellaDirs(File)) 29971e1a64fSBen Langmuir return; 30071e1a64fSBen Langmuir 30171e1a64fSBen Langmuir // At this point, only non-modular includes remain. 30271e1a64fSBen Langmuir 30371e1a64fSBen Langmuir if (LangOpts.ModulesStrictDeclUse) { 30411152dd5SRichard Smith Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module) 30571e1a64fSBen Langmuir << RequestingModule->getFullModuleName() << Filename; 30671e1a64fSBen Langmuir } else if (RequestingModule) { 30771e1a64fSBen Langmuir diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ? 30871e1a64fSBen Langmuir diag::warn_non_modular_include_in_framework_module : 30971e1a64fSBen Langmuir diag::warn_non_modular_include_in_module; 31071e1a64fSBen Langmuir Diags.Report(FilenameLoc, DiagID) << RequestingModule->getFullModuleName(); 31171e1a64fSBen Langmuir } 31292669ee4SDaniel Jasper } 31392669ee4SDaniel Jasper 314ec87a50aSRichard Smith static bool isBetterKnownHeader(const ModuleMap::KnownHeader &New, 315ec87a50aSRichard Smith const ModuleMap::KnownHeader &Old) { 3168b7c0398SSean Silva // Prefer available modules. 3178b7c0398SSean Silva if (New.getModule()->isAvailable() && !Old.getModule()->isAvailable()) 3188b7c0398SSean Silva return true; 3198b7c0398SSean Silva 320ec87a50aSRichard Smith // Prefer a public header over a private header. 321ec87a50aSRichard Smith if ((New.getRole() & ModuleMap::PrivateHeader) != 322ec87a50aSRichard Smith (Old.getRole() & ModuleMap::PrivateHeader)) 323ec87a50aSRichard Smith return !(New.getRole() & ModuleMap::PrivateHeader); 324ec87a50aSRichard Smith 325ec87a50aSRichard Smith // Prefer a non-textual header over a textual header. 326ec87a50aSRichard Smith if ((New.getRole() & ModuleMap::TextualHeader) != 327ec87a50aSRichard Smith (Old.getRole() & ModuleMap::TextualHeader)) 328ec87a50aSRichard Smith return !(New.getRole() & ModuleMap::TextualHeader); 329ec87a50aSRichard Smith 330ec87a50aSRichard Smith // Don't have a reason to choose between these. Just keep the first one. 331ec87a50aSRichard Smith return false; 332ec87a50aSRichard Smith } 333ec87a50aSRichard Smith 3344881e8b2SSean Silva ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File) { 335306d8920SRichard Smith auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader { 3368230e5eeSSean Silva if (R.getRole() & ModuleMap::TextualHeader) 337306d8920SRichard Smith return ModuleMap::KnownHeader(); 338306d8920SRichard Smith return R; 339306d8920SRichard Smith }; 340306d8920SRichard Smith 3414881e8b2SSean Silva HeadersMap::iterator Known = findKnownHeader(File); 3421fb5c3a6SDouglas Gregor if (Known != Headers.end()) { 343202210b3SRichard Smith ModuleMap::KnownHeader Result; 34497da9178SDaniel Jasper // Iterate over all modules that 'File' is part of to find the best fit. 3454881e8b2SSean Silva for (KnownHeader &H : Known->second) { 3462f633e7cSRichard Smith // Prefer a header from the current module over all others. 3478692a4d1SRichard Smith if (H.getModule()->getTopLevelModule() == CompilingModule) 3482f633e7cSRichard Smith return MakeResult(H); 3494881e8b2SSean Silva if (!Result || isBetterKnownHeader(H, Result)) 3504881e8b2SSean Silva Result = H; 35197da9178SDaniel Jasper } 352306d8920SRichard Smith return MakeResult(Result); 3531fb5c3a6SDouglas Gregor } 354ab0c8a84SDouglas Gregor 355386bb073SRichard Smith return MakeResult(findOrCreateModuleForHeaderInUmbrellaDir(File)); 356386bb073SRichard Smith } 357386bb073SRichard Smith 358386bb073SRichard Smith ModuleMap::KnownHeader 359386bb073SRichard Smith ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File) { 360386bb073SRichard Smith assert(!Headers.count(File) && "already have a module for this header"); 361386bb073SRichard Smith 362f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 3634469138eSBen Langmuir KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs); 3644469138eSBen Langmuir if (H) { 3654469138eSBen Langmuir Module *Result = H.getModule(); 366930a85ccSDouglas Gregor 367930a85ccSDouglas Gregor // Search up the module stack until we find a module with an umbrella 36873141fa9SDouglas Gregor // directory. 369930a85ccSDouglas Gregor Module *UmbrellaModule = Result; 37073141fa9SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 371930a85ccSDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 372930a85ccSDouglas Gregor 373930a85ccSDouglas Gregor if (UmbrellaModule->InferSubmodules) { 3749d6448b1SBen Langmuir const FileEntry *UmbrellaModuleMap = 3759d6448b1SBen Langmuir getModuleMapFileForUniquing(UmbrellaModule); 3769d6448b1SBen Langmuir 377a89c5ac4SDouglas Gregor // Infer submodules for each of the directories we found between 378a89c5ac4SDouglas Gregor // the directory of the umbrella header and the directory where 379a89c5ac4SDouglas Gregor // the actual header is located. 3809458f82dSDouglas Gregor bool Explicit = UmbrellaModule->InferExplicitSubmodules; 3819458f82dSDouglas Gregor 3827033127bSDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 383a89c5ac4SDouglas Gregor // Find or create the module that corresponds to this directory name. 384056396aeSDouglas Gregor SmallString<32> NameBuf; 385056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 3864469138eSBen Langmuir llvm::sys::path::stem(SkippedDirs[I-1]->getName()), NameBuf); 3879d6448b1SBen Langmuir Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 3889d6448b1SBen Langmuir Explicit).first; 3899d6448b1SBen Langmuir InferredModuleAllowedBy[Result] = UmbrellaModuleMap; 390ffbafa2aSBen Langmuir Result->IsInferred = true; 391a89c5ac4SDouglas Gregor 392a89c5ac4SDouglas Gregor // Associate the module and the directory. 393a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I-1]] = Result; 394a89c5ac4SDouglas Gregor 395a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 396a89c5ac4SDouglas Gregor // wildcard to the set of exports. 397930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 398d2d442caSCraig Topper Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 399a89c5ac4SDouglas Gregor } 400a89c5ac4SDouglas Gregor 401a89c5ac4SDouglas Gregor // Infer a submodule with the same name as this header file. 402056396aeSDouglas Gregor SmallString<32> NameBuf; 403056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 404056396aeSDouglas Gregor llvm::sys::path::stem(File->getName()), NameBuf); 4059d6448b1SBen Langmuir Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 4069d6448b1SBen Langmuir Explicit).first; 4079d6448b1SBen Langmuir InferredModuleAllowedBy[Result] = UmbrellaModuleMap; 408ffbafa2aSBen Langmuir Result->IsInferred = true; 4093c5305c1SArgyrios Kyrtzidis Result->addTopHeader(File); 410a89c5ac4SDouglas Gregor 411a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 412a89c5ac4SDouglas Gregor // wildcard to the set of exports. 413930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 414d2d442caSCraig Topper Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 415a89c5ac4SDouglas Gregor } else { 416a89c5ac4SDouglas Gregor // Record each of the directories we stepped through as being part of 417a89c5ac4SDouglas Gregor // the module we found, since the umbrella header covers them all. 418a89c5ac4SDouglas Gregor for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 419a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I]] = Result; 420a89c5ac4SDouglas Gregor } 421a89c5ac4SDouglas Gregor 422386bb073SRichard Smith KnownHeader Header(Result, NormalHeader); 423386bb073SRichard Smith Headers[File].push_back(Header); 424386bb073SRichard Smith return Header; 425a89c5ac4SDouglas Gregor } 426a89c5ac4SDouglas Gregor 427b53e5483SLawrence Crowl return KnownHeader(); 428ab0c8a84SDouglas Gregor } 429ab0c8a84SDouglas Gregor 430386bb073SRichard Smith ArrayRef<ModuleMap::KnownHeader> 431386bb073SRichard Smith ModuleMap::findAllModulesForHeader(const FileEntry *File) const { 432386bb073SRichard Smith auto It = Headers.find(File); 433386bb073SRichard Smith if (It == Headers.end()) 434386bb073SRichard Smith return None; 435386bb073SRichard Smith return It->second; 436386bb073SRichard Smith } 437386bb073SRichard Smith 438e4412640SArgyrios Kyrtzidis bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const { 439d2d442caSCraig Topper return isHeaderUnavailableInModule(Header, nullptr); 44050996ce1SRichard Smith } 44150996ce1SRichard Smith 44262bcd925SDmitri Gribenko bool 44362bcd925SDmitri Gribenko ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header, 44462bcd925SDmitri Gribenko const Module *RequestingModule) const { 445e4412640SArgyrios Kyrtzidis HeadersMap::const_iterator Known = Headers.find(Header); 44697da9178SDaniel Jasper if (Known != Headers.end()) { 44797da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::const_iterator 44897da9178SDaniel Jasper I = Known->second.begin(), 44997da9178SDaniel Jasper E = Known->second.end(); 45097da9178SDaniel Jasper I != E; ++I) { 45150996ce1SRichard Smith if (I->isAvailable() && (!RequestingModule || 45250996ce1SRichard Smith I->getModule()->isSubModuleOf(RequestingModule))) 45397da9178SDaniel Jasper return false; 45497da9178SDaniel Jasper } 45597da9178SDaniel Jasper return true; 45697da9178SDaniel Jasper } 4571fb5c3a6SDouglas Gregor 4581fb5c3a6SDouglas Gregor const DirectoryEntry *Dir = Header->getDir(); 459f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 4601fb5c3a6SDouglas Gregor StringRef DirName = Dir->getName(); 4611fb5c3a6SDouglas Gregor 46250996ce1SRichard Smith auto IsUnavailable = [&](const Module *M) { 46350996ce1SRichard Smith return !M->isAvailable() && (!RequestingModule || 46450996ce1SRichard Smith M->isSubModuleOf(RequestingModule)); 46550996ce1SRichard Smith }; 46650996ce1SRichard Smith 4671fb5c3a6SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 4681fb5c3a6SDouglas Gregor // an umbrella header. 4691fb5c3a6SDouglas Gregor do { 470e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir 4711fb5c3a6SDouglas Gregor = UmbrellaDirs.find(Dir); 4721fb5c3a6SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 4731fb5c3a6SDouglas Gregor Module *Found = KnownDir->second; 47450996ce1SRichard Smith if (IsUnavailable(Found)) 4751fb5c3a6SDouglas Gregor return true; 4761fb5c3a6SDouglas Gregor 4771fb5c3a6SDouglas Gregor // Search up the module stack until we find a module with an umbrella 4781fb5c3a6SDouglas Gregor // directory. 4791fb5c3a6SDouglas Gregor Module *UmbrellaModule = Found; 4801fb5c3a6SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 4811fb5c3a6SDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 4821fb5c3a6SDouglas Gregor 4831fb5c3a6SDouglas Gregor if (UmbrellaModule->InferSubmodules) { 4841fb5c3a6SDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 4851fb5c3a6SDouglas Gregor // Find or create the module that corresponds to this directory name. 486056396aeSDouglas Gregor SmallString<32> NameBuf; 487056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 488056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 489056396aeSDouglas Gregor NameBuf); 4901fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 4911fb5c3a6SDouglas Gregor if (!Found) 4921fb5c3a6SDouglas Gregor return false; 49350996ce1SRichard Smith if (IsUnavailable(Found)) 4941fb5c3a6SDouglas Gregor return true; 4951fb5c3a6SDouglas Gregor } 4961fb5c3a6SDouglas Gregor 4971fb5c3a6SDouglas Gregor // Infer a submodule with the same name as this header file. 498056396aeSDouglas Gregor SmallString<32> NameBuf; 499056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 500056396aeSDouglas Gregor llvm::sys::path::stem(Header->getName()), 501056396aeSDouglas Gregor NameBuf); 5021fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 5031fb5c3a6SDouglas Gregor if (!Found) 5041fb5c3a6SDouglas Gregor return false; 5051fb5c3a6SDouglas Gregor } 5061fb5c3a6SDouglas Gregor 50750996ce1SRichard Smith return IsUnavailable(Found); 5081fb5c3a6SDouglas Gregor } 5091fb5c3a6SDouglas Gregor 5101fb5c3a6SDouglas Gregor SkippedDirs.push_back(Dir); 5111fb5c3a6SDouglas Gregor 5121fb5c3a6SDouglas Gregor // Retrieve our parent path. 5131fb5c3a6SDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 5141fb5c3a6SDouglas Gregor if (DirName.empty()) 5151fb5c3a6SDouglas Gregor break; 5161fb5c3a6SDouglas Gregor 5171fb5c3a6SDouglas Gregor // Resolve the parent path to a directory entry. 5181f76c4e8SManuel Klimek Dir = SourceMgr.getFileManager().getDirectory(DirName); 5191fb5c3a6SDouglas Gregor } while (Dir); 5201fb5c3a6SDouglas Gregor 5211fb5c3a6SDouglas Gregor return false; 5221fb5c3a6SDouglas Gregor } 5231fb5c3a6SDouglas Gregor 524e4412640SArgyrios Kyrtzidis Module *ModuleMap::findModule(StringRef Name) const { 525e4412640SArgyrios Kyrtzidis llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name); 52688bdfb0eSDouglas Gregor if (Known != Modules.end()) 52788bdfb0eSDouglas Gregor return Known->getValue(); 52888bdfb0eSDouglas Gregor 529d2d442caSCraig Topper return nullptr; 53088bdfb0eSDouglas Gregor } 53188bdfb0eSDouglas Gregor 532e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleUnqualified(StringRef Name, 533e4412640SArgyrios Kyrtzidis Module *Context) const { 5342b82c2a5SDouglas Gregor for(; Context; Context = Context->Parent) { 5352b82c2a5SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Context)) 5362b82c2a5SDouglas Gregor return Sub; 5372b82c2a5SDouglas Gregor } 5382b82c2a5SDouglas Gregor 5392b82c2a5SDouglas Gregor return findModule(Name); 5402b82c2a5SDouglas Gregor } 5412b82c2a5SDouglas Gregor 542e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{ 5432b82c2a5SDouglas Gregor if (!Context) 5442b82c2a5SDouglas Gregor return findModule(Name); 5452b82c2a5SDouglas Gregor 546eb90e830SDouglas Gregor return Context->findSubmodule(Name); 5472b82c2a5SDouglas Gregor } 5482b82c2a5SDouglas Gregor 549de3ef502SDouglas Gregor std::pair<Module *, bool> 5509d6448b1SBen Langmuir ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 55169021974SDouglas Gregor bool IsExplicit) { 55269021974SDouglas Gregor // Try to find an existing module with this name. 553eb90e830SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Parent)) 554eb90e830SDouglas Gregor return std::make_pair(Sub, false); 55569021974SDouglas Gregor 55669021974SDouglas Gregor // Create a new module with this name. 5579d6448b1SBen Langmuir Module *Result = new Module(Name, SourceLocation(), Parent, 558a7e2cc68SRichard Smith IsFramework, IsExplicit, NumCreatedModules++); 559ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == Name) { 560ba7f2f71SDaniel Jasper SourceModule = Result; 561ba7f2f71SDaniel Jasper SourceModuleName = Name; 562ba7f2f71SDaniel Jasper } 5636f722b4eSArgyrios Kyrtzidis if (!Parent) { 56469021974SDouglas Gregor Modules[Name] = Result; 5656f722b4eSArgyrios Kyrtzidis if (!LangOpts.CurrentModule.empty() && !CompilingModule && 5666f722b4eSArgyrios Kyrtzidis Name == LangOpts.CurrentModule) { 5676f722b4eSArgyrios Kyrtzidis CompilingModule = Result; 5686f722b4eSArgyrios Kyrtzidis } 5696f722b4eSArgyrios Kyrtzidis } 57069021974SDouglas Gregor return std::make_pair(Result, true); 57169021974SDouglas Gregor } 57269021974SDouglas Gregor 57311dfe6feSDouglas Gregor /// \brief For a framework module, infer the framework against which we 57411dfe6feSDouglas Gregor /// should link. 57511dfe6feSDouglas Gregor static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir, 57611dfe6feSDouglas Gregor FileManager &FileMgr) { 57711dfe6feSDouglas Gregor assert(Mod->IsFramework && "Can only infer linking for framework modules"); 57811dfe6feSDouglas Gregor assert(!Mod->isSubFramework() && 57911dfe6feSDouglas Gregor "Can only infer linking for top-level frameworks"); 58011dfe6feSDouglas Gregor 58111dfe6feSDouglas Gregor SmallString<128> LibName; 58211dfe6feSDouglas Gregor LibName += FrameworkDir->getName(); 58311dfe6feSDouglas Gregor llvm::sys::path::append(LibName, Mod->Name); 5848aaae5a9SJuergen Ributzka 5858aaae5a9SJuergen Ributzka // The library name of a framework has more than one possible extension since 5868aaae5a9SJuergen Ributzka // the introduction of the text-based dynamic library format. We need to check 5878aaae5a9SJuergen Ributzka // for both before we give up. 5888aaae5a9SJuergen Ributzka static const char *frameworkExtensions[] = {"", ".tbd"}; 5898aaae5a9SJuergen Ributzka for (const auto *extension : frameworkExtensions) { 5908aaae5a9SJuergen Ributzka llvm::sys::path::replace_extension(LibName, extension); 59111dfe6feSDouglas Gregor if (FileMgr.getFile(LibName)) { 59211dfe6feSDouglas Gregor Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name, 59311dfe6feSDouglas Gregor /*IsFramework=*/true)); 5948aaae5a9SJuergen Ributzka return; 5958aaae5a9SJuergen Ributzka } 59611dfe6feSDouglas Gregor } 59711dfe6feSDouglas Gregor } 59811dfe6feSDouglas Gregor 599a525400dSBen Langmuir Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir, 600a525400dSBen Langmuir bool IsSystem, Module *Parent) { 601c1d88ea5SBen Langmuir Attributes Attrs; 602c1d88ea5SBen Langmuir Attrs.IsSystem = IsSystem; 603a525400dSBen Langmuir return inferFrameworkModule(FrameworkDir, Attrs, Parent); 604c1d88ea5SBen Langmuir } 605c1d88ea5SBen Langmuir 606a525400dSBen Langmuir Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir, 607c1d88ea5SBen Langmuir Attributes Attrs, Module *Parent) { 608a525400dSBen Langmuir // Note: as an egregious but useful hack we use the real path here, because 609a525400dSBen Langmuir // we might be looking at an embedded framework that symlinks out to a 610a525400dSBen Langmuir // top-level framework, and we need to infer as if we were naming the 611a525400dSBen Langmuir // top-level framework. 612a525400dSBen Langmuir StringRef FrameworkDirName = 613a525400dSBen Langmuir SourceMgr.getFileManager().getCanonicalName(FrameworkDir); 614a525400dSBen Langmuir 615a525400dSBen Langmuir // In case this is a case-insensitive filesystem, use the canonical 616a525400dSBen Langmuir // directory name as the ModuleName, since modules are case-sensitive. 617a525400dSBen Langmuir // FIXME: we should be able to give a fix-it hint for the correct spelling. 618a525400dSBen Langmuir SmallString<32> ModuleNameStorage; 619a525400dSBen Langmuir StringRef ModuleName = sanitizeFilenameAsIdentifier( 620a525400dSBen Langmuir llvm::sys::path::stem(FrameworkDirName), ModuleNameStorage); 621c1d88ea5SBen Langmuir 62256c64013SDouglas Gregor // Check whether we've already found this module. 623e89dbc1dSDouglas Gregor if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 624e89dbc1dSDouglas Gregor return Mod; 625e89dbc1dSDouglas Gregor 6261f76c4e8SManuel Klimek FileManager &FileMgr = SourceMgr.getFileManager(); 62756c64013SDouglas Gregor 6289194a91dSDouglas Gregor // If the framework has a parent path from which we're allowed to infer 6299194a91dSDouglas Gregor // a framework module, do so. 630beee15e7SBen Langmuir const FileEntry *ModuleMapFile = nullptr; 6319194a91dSDouglas Gregor if (!Parent) { 6324ddf2221SDouglas Gregor // Determine whether we're allowed to infer a module map. 6339194a91dSDouglas Gregor bool canInfer = false; 6344ddf2221SDouglas Gregor if (llvm::sys::path::has_parent_path(FrameworkDirName)) { 6359194a91dSDouglas Gregor // Figure out the parent path. 6364ddf2221SDouglas Gregor StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); 6379194a91dSDouglas Gregor if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) { 6389194a91dSDouglas Gregor // Check whether we have already looked into the parent directory 6399194a91dSDouglas Gregor // for a module map. 640e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 6419194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 6429194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) { 6439194a91dSDouglas Gregor // We haven't looked here before. Load a module map, if there is 6449194a91dSDouglas Gregor // one. 645984e1df7SBen Langmuir bool IsFrameworkDir = Parent.endswith(".framework"); 646984e1df7SBen Langmuir if (const FileEntry *ModMapFile = 647984e1df7SBen Langmuir HeaderInfo.lookupModuleMapFile(ParentDir, IsFrameworkDir)) { 648c1d88ea5SBen Langmuir parseModuleMapFile(ModMapFile, Attrs.IsSystem, ParentDir); 6499194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 6509194a91dSDouglas Gregor } 6519194a91dSDouglas Gregor 6529194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) 6539194a91dSDouglas Gregor inferred = InferredDirectories.insert( 6549194a91dSDouglas Gregor std::make_pair(ParentDir, InferredDirectory())).first; 6559194a91dSDouglas Gregor } 6569194a91dSDouglas Gregor 6579194a91dSDouglas Gregor if (inferred->second.InferModules) { 6589194a91dSDouglas Gregor // We're allowed to infer for this directory, but make sure it's okay 6599194a91dSDouglas Gregor // to infer this particular module. 6604ddf2221SDouglas Gregor StringRef Name = llvm::sys::path::stem(FrameworkDirName); 6619194a91dSDouglas Gregor canInfer = std::find(inferred->second.ExcludedModules.begin(), 6629194a91dSDouglas Gregor inferred->second.ExcludedModules.end(), 6639194a91dSDouglas Gregor Name) == inferred->second.ExcludedModules.end(); 6649194a91dSDouglas Gregor 665c1d88ea5SBen Langmuir Attrs.IsSystem |= inferred->second.Attrs.IsSystem; 666c1d88ea5SBen Langmuir Attrs.IsExternC |= inferred->second.Attrs.IsExternC; 667c1d88ea5SBen Langmuir Attrs.IsExhaustive |= inferred->second.Attrs.IsExhaustive; 668beee15e7SBen Langmuir ModuleMapFile = inferred->second.ModuleMapFile; 6699194a91dSDouglas Gregor } 6709194a91dSDouglas Gregor } 6719194a91dSDouglas Gregor } 6729194a91dSDouglas Gregor 6739194a91dSDouglas Gregor // If we're not allowed to infer a framework module, don't. 6749194a91dSDouglas Gregor if (!canInfer) 675d2d442caSCraig Topper return nullptr; 676beee15e7SBen Langmuir } else 6779d6448b1SBen Langmuir ModuleMapFile = getModuleMapFileForUniquing(Parent); 6789194a91dSDouglas Gregor 6799194a91dSDouglas Gregor 68056c64013SDouglas Gregor // Look for an umbrella header. 6812c1dd271SDylan Noblesmith SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 68217381a06SBenjamin Kramer llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h"); 683e89dbc1dSDouglas Gregor const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 68456c64013SDouglas Gregor 68556c64013SDouglas Gregor // FIXME: If there's no umbrella header, we could probably scan the 68656c64013SDouglas Gregor // framework to load *everything*. But, it's not clear that this is a good 68756c64013SDouglas Gregor // idea. 68856c64013SDouglas Gregor if (!UmbrellaHeader) 689d2d442caSCraig Topper return nullptr; 69056c64013SDouglas Gregor 6919d6448b1SBen Langmuir Module *Result = new Module(ModuleName, SourceLocation(), Parent, 692a7e2cc68SRichard Smith /*IsFramework=*/true, /*IsExplicit=*/false, 693a7e2cc68SRichard Smith NumCreatedModules++); 6949d6448b1SBen Langmuir InferredModuleAllowedBy[Result] = ModuleMapFile; 6959d6448b1SBen Langmuir Result->IsInferred = true; 696ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == ModuleName) { 697ba7f2f71SDaniel Jasper SourceModule = Result; 698ba7f2f71SDaniel Jasper SourceModuleName = ModuleName; 699ba7f2f71SDaniel Jasper } 700c1d88ea5SBen Langmuir 701c1d88ea5SBen Langmuir Result->IsSystem |= Attrs.IsSystem; 702c1d88ea5SBen Langmuir Result->IsExternC |= Attrs.IsExternC; 703c1d88ea5SBen Langmuir Result->ConfigMacrosExhaustive |= Attrs.IsExhaustive; 7042b63d15fSRichard Smith Result->Directory = FrameworkDir; 705a686e1b0SDouglas Gregor 706eb90e830SDouglas Gregor if (!Parent) 707e89dbc1dSDouglas Gregor Modules[ModuleName] = Result; 708e89dbc1dSDouglas Gregor 709322f633cSDouglas Gregor // umbrella header "umbrella-header-name" 7102b63d15fSRichard Smith // 7112b63d15fSRichard Smith // The "Headers/" component of the name is implied because this is 7122b63d15fSRichard Smith // a framework module. 7132b63d15fSRichard Smith setUmbrellaHeader(Result, UmbrellaHeader, ModuleName + ".h"); 714d8bd7537SDouglas Gregor 715d8bd7537SDouglas Gregor // export * 716d2d442caSCraig Topper Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 717d8bd7537SDouglas Gregor 718a89c5ac4SDouglas Gregor // module * { export * } 719a89c5ac4SDouglas Gregor Result->InferSubmodules = true; 720a89c5ac4SDouglas Gregor Result->InferExportWildcard = true; 721a89c5ac4SDouglas Gregor 722e89dbc1dSDouglas Gregor // Look for subframeworks. 723c080917eSRafael Espindola std::error_code EC; 7242c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirName 725ddaa69cbSDouglas Gregor = StringRef(FrameworkDir->getName()); 726e89dbc1dSDouglas Gregor llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 7272d4d8cb3SBenjamin Kramer llvm::sys::path::native(SubframeworksDirName); 72892e1b62dSYaron Keren for (llvm::sys::fs::directory_iterator Dir(SubframeworksDirName, EC), DirEnd; 729e89dbc1dSDouglas Gregor Dir != DirEnd && !EC; Dir.increment(EC)) { 730e89dbc1dSDouglas Gregor if (!StringRef(Dir->path()).endswith(".framework")) 731e89dbc1dSDouglas Gregor continue; 732f2161a70SDouglas Gregor 733e89dbc1dSDouglas Gregor if (const DirectoryEntry *SubframeworkDir 734e89dbc1dSDouglas Gregor = FileMgr.getDirectory(Dir->path())) { 73507c22b78SDouglas Gregor // Note: as an egregious but useful hack, we use the real path here and 73607c22b78SDouglas Gregor // check whether it is actually a subdirectory of the parent directory. 73707c22b78SDouglas Gregor // This will not be the case if the 'subframework' is actually a symlink 73807c22b78SDouglas Gregor // out to a top-level framework. 739e00c8b20SDouglas Gregor StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir); 74007c22b78SDouglas Gregor bool FoundParent = false; 74107c22b78SDouglas Gregor do { 74207c22b78SDouglas Gregor // Get the parent directory name. 74307c22b78SDouglas Gregor SubframeworkDirName 74407c22b78SDouglas Gregor = llvm::sys::path::parent_path(SubframeworkDirName); 74507c22b78SDouglas Gregor if (SubframeworkDirName.empty()) 74607c22b78SDouglas Gregor break; 74707c22b78SDouglas Gregor 74807c22b78SDouglas Gregor if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { 74907c22b78SDouglas Gregor FoundParent = true; 75007c22b78SDouglas Gregor break; 75107c22b78SDouglas Gregor } 75207c22b78SDouglas Gregor } while (true); 75307c22b78SDouglas Gregor 75407c22b78SDouglas Gregor if (!FoundParent) 75507c22b78SDouglas Gregor continue; 75607c22b78SDouglas Gregor 757e89dbc1dSDouglas Gregor // FIXME: Do we want to warn about subframeworks without umbrella headers? 758a525400dSBen Langmuir inferFrameworkModule(SubframeworkDir, Attrs, Result); 759e89dbc1dSDouglas Gregor } 760e89dbc1dSDouglas Gregor } 761e89dbc1dSDouglas Gregor 76211dfe6feSDouglas Gregor // If the module is a top-level framework, automatically link against the 76311dfe6feSDouglas Gregor // framework. 76411dfe6feSDouglas Gregor if (!Result->isSubFramework()) { 76511dfe6feSDouglas Gregor inferFrameworkLink(Result, FrameworkDir, FileMgr); 76611dfe6feSDouglas Gregor } 76711dfe6feSDouglas Gregor 76856c64013SDouglas Gregor return Result; 76956c64013SDouglas Gregor } 77056c64013SDouglas Gregor 7712b63d15fSRichard Smith void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader, 7722b63d15fSRichard Smith Twine NameAsWritten) { 77397da9178SDaniel Jasper Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader)); 77473141fa9SDouglas Gregor Mod->Umbrella = UmbrellaHeader; 7752b63d15fSRichard Smith Mod->UmbrellaAsWritten = NameAsWritten.str(); 7767033127bSDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; 777a89c5ac4SDouglas Gregor } 778a89c5ac4SDouglas Gregor 7792b63d15fSRichard Smith void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir, 7802b63d15fSRichard Smith Twine NameAsWritten) { 781524e33e1SDouglas Gregor Mod->Umbrella = UmbrellaDir; 7822b63d15fSRichard Smith Mod->UmbrellaAsWritten = NameAsWritten.str(); 783524e33e1SDouglas Gregor UmbrellaDirs[UmbrellaDir] = Mod; 784524e33e1SDouglas Gregor } 785524e33e1SDouglas Gregor 7863c1a41adSRichard Smith static Module::HeaderKind headerRoleToKind(ModuleMap::ModuleHeaderRole Role) { 7870e98d938SNAKAMURA Takumi switch ((int)Role) { 7883c1a41adSRichard Smith default: llvm_unreachable("unknown header role"); 7893c1a41adSRichard Smith case ModuleMap::NormalHeader: 7903c1a41adSRichard Smith return Module::HK_Normal; 7913c1a41adSRichard Smith case ModuleMap::PrivateHeader: 7923c1a41adSRichard Smith return Module::HK_Private; 7933c1a41adSRichard Smith case ModuleMap::TextualHeader: 7943c1a41adSRichard Smith return Module::HK_Textual; 7953c1a41adSRichard Smith case ModuleMap::PrivateHeader | ModuleMap::TextualHeader: 7963c1a41adSRichard Smith return Module::HK_PrivateTextual; 7973c1a41adSRichard Smith } 7980e98d938SNAKAMURA Takumi } 799202210b3SRichard Smith 8003c1a41adSRichard Smith void ModuleMap::addHeader(Module *Mod, Module::Header Header, 801d8879c85SRichard Smith ModuleHeaderRole Role, bool Imported) { 802386bb073SRichard Smith KnownHeader KH(Mod, Role); 8033c1a41adSRichard Smith 804386bb073SRichard Smith // Only add each header to the headers list once. 805386bb073SRichard Smith // FIXME: Should we diagnose if a header is listed twice in the 806386bb073SRichard Smith // same module definition? 807386bb073SRichard Smith auto &HeaderList = Headers[Header.Entry]; 808386bb073SRichard Smith for (auto H : HeaderList) 809386bb073SRichard Smith if (H == KH) 810386bb073SRichard Smith return; 811386bb073SRichard Smith 812386bb073SRichard Smith HeaderList.push_back(KH); 8133c1a41adSRichard Smith Mod->Headers[headerRoleToKind(Role)].push_back(std::move(Header)); 814386bb073SRichard Smith 815386bb073SRichard Smith bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule; 816d8879c85SRichard Smith if (!Imported || isCompilingModuleHeader) { 817d8879c85SRichard Smith // When we import HeaderFileInfo, the external source is expected to 818d8879c85SRichard Smith // set the isModuleHeader flag itself. 819d8879c85SRichard Smith HeaderInfo.MarkFileModuleHeader(Header.Entry, Role, 820d8879c85SRichard Smith isCompilingModuleHeader); 821d8879c85SRichard Smith } 822a89c5ac4SDouglas Gregor } 823a89c5ac4SDouglas Gregor 8243c1a41adSRichard Smith void ModuleMap::excludeHeader(Module *Mod, Module::Header Header) { 825feb54b6dSRichard Smith // Add this as a known header so we won't implicitly add it to any 826feb54b6dSRichard Smith // umbrella directory module. 827feb54b6dSRichard Smith // FIXME: Should we only exclude it from umbrella modules within the 828feb54b6dSRichard Smith // specified module? 8293c1a41adSRichard Smith (void) Headers[Header.Entry]; 8303c1a41adSRichard Smith 8313c1a41adSRichard Smith Mod->Headers[Module::HK_Excluded].push_back(std::move(Header)); 832feb54b6dSRichard Smith } 833feb54b6dSRichard Smith 834514b636aSDouglas Gregor const FileEntry * 8354b8a9e95SBen Langmuir ModuleMap::getContainingModuleMapFile(const Module *Module) const { 8361f76c4e8SManuel Klimek if (Module->DefinitionLoc.isInvalid()) 837d2d442caSCraig Topper return nullptr; 838514b636aSDouglas Gregor 8391f76c4e8SManuel Klimek return SourceMgr.getFileEntryForID( 8401f76c4e8SManuel Klimek SourceMgr.getFileID(Module->DefinitionLoc)); 841514b636aSDouglas Gregor } 842514b636aSDouglas Gregor 8434b8a9e95SBen Langmuir const FileEntry *ModuleMap::getModuleMapFileForUniquing(const Module *M) const { 8449d6448b1SBen Langmuir if (M->IsInferred) { 8459d6448b1SBen Langmuir assert(InferredModuleAllowedBy.count(M) && "missing inferred module map"); 8469d6448b1SBen Langmuir return InferredModuleAllowedBy.find(M)->second; 8479d6448b1SBen Langmuir } 8489d6448b1SBen Langmuir return getContainingModuleMapFile(M); 8499d6448b1SBen Langmuir } 8509d6448b1SBen Langmuir 8519d6448b1SBen Langmuir void ModuleMap::setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap) { 8529d6448b1SBen Langmuir assert(M->IsInferred && "module not inferred"); 8539d6448b1SBen Langmuir InferredModuleAllowedBy[M] = ModMap; 8549d6448b1SBen Langmuir } 8559d6448b1SBen Langmuir 856*cdae941eSYaron Keren LLVM_DUMP_METHOD void ModuleMap::dump() { 857718292f2SDouglas Gregor llvm::errs() << "Modules:"; 858718292f2SDouglas Gregor for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 859718292f2SDouglas Gregor MEnd = Modules.end(); 860718292f2SDouglas Gregor M != MEnd; ++M) 861d28d1b8dSDouglas Gregor M->getValue()->print(llvm::errs(), 2); 862718292f2SDouglas Gregor 863718292f2SDouglas Gregor llvm::errs() << "Headers:"; 86459527666SDouglas Gregor for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 865718292f2SDouglas Gregor H != HEnd; ++H) { 86697da9178SDaniel Jasper llvm::errs() << " \"" << H->first->getName() << "\" -> "; 86797da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(), 86897da9178SDaniel Jasper E = H->second.end(); 86997da9178SDaniel Jasper I != E; ++I) { 87097da9178SDaniel Jasper if (I != H->second.begin()) 87197da9178SDaniel Jasper llvm::errs() << ","; 87297da9178SDaniel Jasper llvm::errs() << I->getModule()->getFullModuleName(); 87397da9178SDaniel Jasper } 87497da9178SDaniel Jasper llvm::errs() << "\n"; 875718292f2SDouglas Gregor } 876718292f2SDouglas Gregor } 877718292f2SDouglas Gregor 8782b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 87942413141SRichard Smith auto Unresolved = std::move(Mod->UnresolvedExports); 88042413141SRichard Smith Mod->UnresolvedExports.clear(); 88142413141SRichard Smith for (auto &UE : Unresolved) { 88242413141SRichard Smith Module::ExportDecl Export = resolveExport(Mod, UE, Complain); 883f5eedd05SDouglas Gregor if (Export.getPointer() || Export.getInt()) 8842b82c2a5SDouglas Gregor Mod->Exports.push_back(Export); 8852b82c2a5SDouglas Gregor else 88642413141SRichard Smith Mod->UnresolvedExports.push_back(UE); 8872b82c2a5SDouglas Gregor } 88842413141SRichard Smith return !Mod->UnresolvedExports.empty(); 8892b82c2a5SDouglas Gregor } 8902b82c2a5SDouglas Gregor 891ba7f2f71SDaniel Jasper bool ModuleMap::resolveUses(Module *Mod, bool Complain) { 89242413141SRichard Smith auto Unresolved = std::move(Mod->UnresolvedDirectUses); 89342413141SRichard Smith Mod->UnresolvedDirectUses.clear(); 89442413141SRichard Smith for (auto &UDU : Unresolved) { 89542413141SRichard Smith Module *DirectUse = resolveModuleId(UDU, Mod, Complain); 896ba7f2f71SDaniel Jasper if (DirectUse) 897ba7f2f71SDaniel Jasper Mod->DirectUses.push_back(DirectUse); 898ba7f2f71SDaniel Jasper else 89942413141SRichard Smith Mod->UnresolvedDirectUses.push_back(UDU); 900ba7f2f71SDaniel Jasper } 90142413141SRichard Smith return !Mod->UnresolvedDirectUses.empty(); 902ba7f2f71SDaniel Jasper } 903ba7f2f71SDaniel Jasper 904fb912657SDouglas Gregor bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) { 90542413141SRichard Smith auto Unresolved = std::move(Mod->UnresolvedConflicts); 90642413141SRichard Smith Mod->UnresolvedConflicts.clear(); 90742413141SRichard Smith for (auto &UC : Unresolved) { 90842413141SRichard Smith if (Module *OtherMod = resolveModuleId(UC.Id, Mod, Complain)) { 909fb912657SDouglas Gregor Module::Conflict Conflict; 910fb912657SDouglas Gregor Conflict.Other = OtherMod; 91142413141SRichard Smith Conflict.Message = UC.Message; 912fb912657SDouglas Gregor Mod->Conflicts.push_back(Conflict); 91342413141SRichard Smith } else 91442413141SRichard Smith Mod->UnresolvedConflicts.push_back(UC); 915fb912657SDouglas Gregor } 91642413141SRichard Smith return !Mod->UnresolvedConflicts.empty(); 917fb912657SDouglas Gregor } 918fb912657SDouglas Gregor 9190093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 9200093b3c7SDouglas Gregor if (Loc.isInvalid()) 921d2d442caSCraig Topper return nullptr; 9220093b3c7SDouglas Gregor 9230093b3c7SDouglas Gregor // Use the expansion location to determine which module we're in. 9240093b3c7SDouglas Gregor FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 9250093b3c7SDouglas Gregor if (!ExpansionLoc.isFileID()) 926d2d442caSCraig Topper return nullptr; 9270093b3c7SDouglas Gregor 9280093b3c7SDouglas Gregor const SourceManager &SrcMgr = Loc.getManager(); 9290093b3c7SDouglas Gregor FileID ExpansionFileID = ExpansionLoc.getFileID(); 930224d8a74SDouglas Gregor 931224d8a74SDouglas Gregor while (const FileEntry *ExpansionFile 932224d8a74SDouglas Gregor = SrcMgr.getFileEntryForID(ExpansionFileID)) { 933224d8a74SDouglas Gregor // Find the module that owns this header (if any). 934b53e5483SLawrence Crowl if (Module *Mod = findModuleForHeader(ExpansionFile).getModule()) 935224d8a74SDouglas Gregor return Mod; 936224d8a74SDouglas Gregor 937224d8a74SDouglas Gregor // No module owns this header, so look up the inclusion chain to see if 938224d8a74SDouglas Gregor // any included header has an associated module. 939224d8a74SDouglas Gregor SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); 940224d8a74SDouglas Gregor if (IncludeLoc.isInvalid()) 941d2d442caSCraig Topper return nullptr; 9420093b3c7SDouglas Gregor 943224d8a74SDouglas Gregor ExpansionFileID = SrcMgr.getFileID(IncludeLoc); 944224d8a74SDouglas Gregor } 945224d8a74SDouglas Gregor 946d2d442caSCraig Topper return nullptr; 9470093b3c7SDouglas Gregor } 9480093b3c7SDouglas Gregor 949718292f2SDouglas Gregor //----------------------------------------------------------------------------// 950718292f2SDouglas Gregor // Module map file parser 951718292f2SDouglas Gregor //----------------------------------------------------------------------------// 952718292f2SDouglas Gregor 953718292f2SDouglas Gregor namespace clang { 954718292f2SDouglas Gregor /// \brief A token in a module map file. 955718292f2SDouglas Gregor struct MMToken { 956718292f2SDouglas Gregor enum TokenKind { 9571fb5c3a6SDouglas Gregor Comma, 95835b13eceSDouglas Gregor ConfigMacros, 959fb912657SDouglas Gregor Conflict, 960718292f2SDouglas Gregor EndOfFile, 961718292f2SDouglas Gregor HeaderKeyword, 962718292f2SDouglas Gregor Identifier, 963a3feee2aSRichard Smith Exclaim, 96459527666SDouglas Gregor ExcludeKeyword, 965718292f2SDouglas Gregor ExplicitKeyword, 9662b82c2a5SDouglas Gregor ExportKeyword, 96797292843SDaniel Jasper ExternKeyword, 968755b2055SDouglas Gregor FrameworkKeyword, 9696ddfca91SDouglas Gregor LinkKeyword, 970718292f2SDouglas Gregor ModuleKeyword, 9712b82c2a5SDouglas Gregor Period, 972b53e5483SLawrence Crowl PrivateKeyword, 973718292f2SDouglas Gregor UmbrellaKeyword, 974ba7f2f71SDaniel Jasper UseKeyword, 9751fb5c3a6SDouglas Gregor RequiresKeyword, 9762b82c2a5SDouglas Gregor Star, 977718292f2SDouglas Gregor StringLiteral, 978306d8920SRichard Smith TextualKeyword, 979718292f2SDouglas Gregor LBrace, 980a686e1b0SDouglas Gregor RBrace, 981a686e1b0SDouglas Gregor LSquare, 982a686e1b0SDouglas Gregor RSquare 983718292f2SDouglas Gregor } Kind; 984718292f2SDouglas Gregor 985718292f2SDouglas Gregor unsigned Location; 986718292f2SDouglas Gregor unsigned StringLength; 987718292f2SDouglas Gregor const char *StringData; 988718292f2SDouglas Gregor 989718292f2SDouglas Gregor void clear() { 990718292f2SDouglas Gregor Kind = EndOfFile; 991718292f2SDouglas Gregor Location = 0; 992718292f2SDouglas Gregor StringLength = 0; 993d2d442caSCraig Topper StringData = nullptr; 994718292f2SDouglas Gregor } 995718292f2SDouglas Gregor 996718292f2SDouglas Gregor bool is(TokenKind K) const { return Kind == K; } 997718292f2SDouglas Gregor 998718292f2SDouglas Gregor SourceLocation getLocation() const { 999718292f2SDouglas Gregor return SourceLocation::getFromRawEncoding(Location); 1000718292f2SDouglas Gregor } 1001718292f2SDouglas Gregor 1002718292f2SDouglas Gregor StringRef getString() const { 1003718292f2SDouglas Gregor return StringRef(StringData, StringLength); 1004718292f2SDouglas Gregor } 1005718292f2SDouglas Gregor }; 1006718292f2SDouglas Gregor 1007718292f2SDouglas Gregor class ModuleMapParser { 1008718292f2SDouglas Gregor Lexer &L; 1009718292f2SDouglas Gregor SourceManager &SourceMgr; 1010bc10b9fbSDouglas Gregor 1011bc10b9fbSDouglas Gregor /// \brief Default target information, used only for string literal 1012bc10b9fbSDouglas Gregor /// parsing. 1013bc10b9fbSDouglas Gregor const TargetInfo *Target; 1014bc10b9fbSDouglas Gregor 1015718292f2SDouglas Gregor DiagnosticsEngine &Diags; 1016718292f2SDouglas Gregor ModuleMap ⤅ 1017718292f2SDouglas Gregor 1018beee15e7SBen Langmuir /// \brief The current module map file. 1019beee15e7SBen Langmuir const FileEntry *ModuleMapFile; 1020beee15e7SBen Langmuir 10219acb99e3SRichard Smith /// \brief The directory that file names in this module map file should 10229acb99e3SRichard Smith /// be resolved relative to. 10235257fc63SDouglas Gregor const DirectoryEntry *Directory; 10245257fc63SDouglas Gregor 10253ec6663bSDouglas Gregor /// \brief The directory containing Clang-supplied headers. 10263ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir; 10273ec6663bSDouglas Gregor 1028963c5535SDouglas Gregor /// \brief Whether this module map is in a system header directory. 1029963c5535SDouglas Gregor bool IsSystem; 1030963c5535SDouglas Gregor 1031718292f2SDouglas Gregor /// \brief Whether an error occurred. 1032718292f2SDouglas Gregor bool HadError; 1033718292f2SDouglas Gregor 1034718292f2SDouglas Gregor /// \brief Stores string data for the various string literals referenced 1035718292f2SDouglas Gregor /// during parsing. 1036718292f2SDouglas Gregor llvm::BumpPtrAllocator StringData; 1037718292f2SDouglas Gregor 1038718292f2SDouglas Gregor /// \brief The current token. 1039718292f2SDouglas Gregor MMToken Tok; 1040718292f2SDouglas Gregor 1041718292f2SDouglas Gregor /// \brief The active module. 1042de3ef502SDouglas Gregor Module *ActiveModule; 1043718292f2SDouglas Gregor 10447ff29148SBen Langmuir /// \brief Whether a module uses the 'requires excluded' hack to mark its 10457ff29148SBen Langmuir /// contents as 'textual'. 10467ff29148SBen Langmuir /// 10477ff29148SBen Langmuir /// On older Darwin SDK versions, 'requires excluded' is used to mark the 10487ff29148SBen Langmuir /// contents of the Darwin.C.excluded (assert.h) and Tcl.Private modules as 10497ff29148SBen Langmuir /// non-modular headers. For backwards compatibility, we continue to 10507ff29148SBen Langmuir /// support this idiom for just these modules, and map the headers to 10517ff29148SBen Langmuir /// 'textual' to match the original intent. 10527ff29148SBen Langmuir llvm::SmallPtrSet<Module *, 2> UsesRequiresExcludedHack; 10537ff29148SBen Langmuir 1054718292f2SDouglas Gregor /// \brief Consume the current token and return its location. 1055718292f2SDouglas Gregor SourceLocation consumeToken(); 1056718292f2SDouglas Gregor 1057718292f2SDouglas Gregor /// \brief Skip tokens until we reach the a token with the given kind 1058718292f2SDouglas Gregor /// (or the end of the file). 1059718292f2SDouglas Gregor void skipUntil(MMToken::TokenKind K); 1060718292f2SDouglas Gregor 1061f857950dSDmitri Gribenko typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; 1062e7ab3669SDouglas Gregor bool parseModuleId(ModuleId &Id); 1063718292f2SDouglas Gregor void parseModuleDecl(); 106497292843SDaniel Jasper void parseExternModuleDecl(); 10651fb5c3a6SDouglas Gregor void parseRequiresDecl(); 1066b53e5483SLawrence Crowl void parseHeaderDecl(clang::MMToken::TokenKind, 1067b53e5483SLawrence Crowl SourceLocation LeadingLoc); 1068524e33e1SDouglas Gregor void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 10692b82c2a5SDouglas Gregor void parseExportDecl(); 1070ba7f2f71SDaniel Jasper void parseUseDecl(); 10716ddfca91SDouglas Gregor void parseLinkDecl(); 107235b13eceSDouglas Gregor void parseConfigMacros(); 1073fb912657SDouglas Gregor void parseConflict(); 10749194a91dSDouglas Gregor void parseInferredModuleDecl(bool Framework, bool Explicit); 1075c1d88ea5SBen Langmuir 1076c1d88ea5SBen Langmuir typedef ModuleMap::Attributes Attributes; 10774442605fSBill Wendling bool parseOptionalAttributes(Attributes &Attrs); 1078718292f2SDouglas Gregor 1079718292f2SDouglas Gregor public: 1080718292f2SDouglas Gregor explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 1081bc10b9fbSDouglas Gregor const TargetInfo *Target, 1082718292f2SDouglas Gregor DiagnosticsEngine &Diags, 10835257fc63SDouglas Gregor ModuleMap &Map, 1084beee15e7SBen Langmuir const FileEntry *ModuleMapFile, 10853ec6663bSDouglas Gregor const DirectoryEntry *Directory, 1086963c5535SDouglas Gregor const DirectoryEntry *BuiltinIncludeDir, 1087963c5535SDouglas Gregor bool IsSystem) 1088bc10b9fbSDouglas Gregor : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 1089beee15e7SBen Langmuir ModuleMapFile(ModuleMapFile), Directory(Directory), 1090beee15e7SBen Langmuir BuiltinIncludeDir(BuiltinIncludeDir), IsSystem(IsSystem), 1091d2d442caSCraig Topper HadError(false), ActiveModule(nullptr) 1092718292f2SDouglas Gregor { 1093718292f2SDouglas Gregor Tok.clear(); 1094718292f2SDouglas Gregor consumeToken(); 1095718292f2SDouglas Gregor } 1096718292f2SDouglas Gregor 1097718292f2SDouglas Gregor bool parseModuleMapFile(); 1098718292f2SDouglas Gregor }; 1099ab9db510SAlexander Kornienko } 1100718292f2SDouglas Gregor 1101718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() { 1102718292f2SDouglas Gregor retry: 1103718292f2SDouglas Gregor SourceLocation Result = Tok.getLocation(); 1104718292f2SDouglas Gregor Tok.clear(); 1105718292f2SDouglas Gregor 1106718292f2SDouglas Gregor Token LToken; 1107718292f2SDouglas Gregor L.LexFromRawLexer(LToken); 1108718292f2SDouglas Gregor Tok.Location = LToken.getLocation().getRawEncoding(); 1109718292f2SDouglas Gregor switch (LToken.getKind()) { 11102d57cea2SAlp Toker case tok::raw_identifier: { 11112d57cea2SAlp Toker StringRef RI = LToken.getRawIdentifier(); 11122d57cea2SAlp Toker Tok.StringData = RI.data(); 11132d57cea2SAlp Toker Tok.StringLength = RI.size(); 11142d57cea2SAlp Toker Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI) 111535b13eceSDouglas Gregor .Case("config_macros", MMToken::ConfigMacros) 1116fb912657SDouglas Gregor .Case("conflict", MMToken::Conflict) 111759527666SDouglas Gregor .Case("exclude", MMToken::ExcludeKeyword) 1118718292f2SDouglas Gregor .Case("explicit", MMToken::ExplicitKeyword) 11192b82c2a5SDouglas Gregor .Case("export", MMToken::ExportKeyword) 112097292843SDaniel Jasper .Case("extern", MMToken::ExternKeyword) 1121755b2055SDouglas Gregor .Case("framework", MMToken::FrameworkKeyword) 112235b13eceSDouglas Gregor .Case("header", MMToken::HeaderKeyword) 11236ddfca91SDouglas Gregor .Case("link", MMToken::LinkKeyword) 1124718292f2SDouglas Gregor .Case("module", MMToken::ModuleKeyword) 1125b53e5483SLawrence Crowl .Case("private", MMToken::PrivateKeyword) 11261fb5c3a6SDouglas Gregor .Case("requires", MMToken::RequiresKeyword) 1127306d8920SRichard Smith .Case("textual", MMToken::TextualKeyword) 1128718292f2SDouglas Gregor .Case("umbrella", MMToken::UmbrellaKeyword) 1129ba7f2f71SDaniel Jasper .Case("use", MMToken::UseKeyword) 1130718292f2SDouglas Gregor .Default(MMToken::Identifier); 1131718292f2SDouglas Gregor break; 11322d57cea2SAlp Toker } 1133718292f2SDouglas Gregor 11341fb5c3a6SDouglas Gregor case tok::comma: 11351fb5c3a6SDouglas Gregor Tok.Kind = MMToken::Comma; 11361fb5c3a6SDouglas Gregor break; 11371fb5c3a6SDouglas Gregor 1138718292f2SDouglas Gregor case tok::eof: 1139718292f2SDouglas Gregor Tok.Kind = MMToken::EndOfFile; 1140718292f2SDouglas Gregor break; 1141718292f2SDouglas Gregor 1142718292f2SDouglas Gregor case tok::l_brace: 1143718292f2SDouglas Gregor Tok.Kind = MMToken::LBrace; 1144718292f2SDouglas Gregor break; 1145718292f2SDouglas Gregor 1146a686e1b0SDouglas Gregor case tok::l_square: 1147a686e1b0SDouglas Gregor Tok.Kind = MMToken::LSquare; 1148a686e1b0SDouglas Gregor break; 1149a686e1b0SDouglas Gregor 11502b82c2a5SDouglas Gregor case tok::period: 11512b82c2a5SDouglas Gregor Tok.Kind = MMToken::Period; 11522b82c2a5SDouglas Gregor break; 11532b82c2a5SDouglas Gregor 1154718292f2SDouglas Gregor case tok::r_brace: 1155718292f2SDouglas Gregor Tok.Kind = MMToken::RBrace; 1156718292f2SDouglas Gregor break; 1157718292f2SDouglas Gregor 1158a686e1b0SDouglas Gregor case tok::r_square: 1159a686e1b0SDouglas Gregor Tok.Kind = MMToken::RSquare; 1160a686e1b0SDouglas Gregor break; 1161a686e1b0SDouglas Gregor 11622b82c2a5SDouglas Gregor case tok::star: 11632b82c2a5SDouglas Gregor Tok.Kind = MMToken::Star; 11642b82c2a5SDouglas Gregor break; 11652b82c2a5SDouglas Gregor 1166a3feee2aSRichard Smith case tok::exclaim: 1167a3feee2aSRichard Smith Tok.Kind = MMToken::Exclaim; 1168a3feee2aSRichard Smith break; 1169a3feee2aSRichard Smith 1170718292f2SDouglas Gregor case tok::string_literal: { 1171d67aea28SRichard Smith if (LToken.hasUDSuffix()) { 1172d67aea28SRichard Smith Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 1173d67aea28SRichard Smith HadError = true; 1174d67aea28SRichard Smith goto retry; 1175d67aea28SRichard Smith } 1176d67aea28SRichard Smith 1177718292f2SDouglas Gregor // Parse the string literal. 1178718292f2SDouglas Gregor LangOptions LangOpts; 11799d5583efSCraig Topper StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target); 1180718292f2SDouglas Gregor if (StringLiteral.hadError) 1181718292f2SDouglas Gregor goto retry; 1182718292f2SDouglas Gregor 1183718292f2SDouglas Gregor // Copy the string literal into our string data allocator. 1184718292f2SDouglas Gregor unsigned Length = StringLiteral.GetStringLength(); 1185718292f2SDouglas Gregor char *Saved = StringData.Allocate<char>(Length + 1); 1186718292f2SDouglas Gregor memcpy(Saved, StringLiteral.GetString().data(), Length); 1187718292f2SDouglas Gregor Saved[Length] = 0; 1188718292f2SDouglas Gregor 1189718292f2SDouglas Gregor // Form the token. 1190718292f2SDouglas Gregor Tok.Kind = MMToken::StringLiteral; 1191718292f2SDouglas Gregor Tok.StringData = Saved; 1192718292f2SDouglas Gregor Tok.StringLength = Length; 1193718292f2SDouglas Gregor break; 1194718292f2SDouglas Gregor } 1195718292f2SDouglas Gregor 1196718292f2SDouglas Gregor case tok::comment: 1197718292f2SDouglas Gregor goto retry; 1198718292f2SDouglas Gregor 1199718292f2SDouglas Gregor default: 1200718292f2SDouglas Gregor Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 1201718292f2SDouglas Gregor HadError = true; 1202718292f2SDouglas Gregor goto retry; 1203718292f2SDouglas Gregor } 1204718292f2SDouglas Gregor 1205718292f2SDouglas Gregor return Result; 1206718292f2SDouglas Gregor } 1207718292f2SDouglas Gregor 1208718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 1209718292f2SDouglas Gregor unsigned braceDepth = 0; 1210a686e1b0SDouglas Gregor unsigned squareDepth = 0; 1211718292f2SDouglas Gregor do { 1212718292f2SDouglas Gregor switch (Tok.Kind) { 1213718292f2SDouglas Gregor case MMToken::EndOfFile: 1214718292f2SDouglas Gregor return; 1215718292f2SDouglas Gregor 1216718292f2SDouglas Gregor case MMToken::LBrace: 1217a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1218718292f2SDouglas Gregor return; 1219718292f2SDouglas Gregor 1220718292f2SDouglas Gregor ++braceDepth; 1221718292f2SDouglas Gregor break; 1222718292f2SDouglas Gregor 1223a686e1b0SDouglas Gregor case MMToken::LSquare: 1224a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1225a686e1b0SDouglas Gregor return; 1226a686e1b0SDouglas Gregor 1227a686e1b0SDouglas Gregor ++squareDepth; 1228a686e1b0SDouglas Gregor break; 1229a686e1b0SDouglas Gregor 1230718292f2SDouglas Gregor case MMToken::RBrace: 1231718292f2SDouglas Gregor if (braceDepth > 0) 1232718292f2SDouglas Gregor --braceDepth; 1233718292f2SDouglas Gregor else if (Tok.is(K)) 1234718292f2SDouglas Gregor return; 1235718292f2SDouglas Gregor break; 1236718292f2SDouglas Gregor 1237a686e1b0SDouglas Gregor case MMToken::RSquare: 1238a686e1b0SDouglas Gregor if (squareDepth > 0) 1239a686e1b0SDouglas Gregor --squareDepth; 1240a686e1b0SDouglas Gregor else if (Tok.is(K)) 1241a686e1b0SDouglas Gregor return; 1242a686e1b0SDouglas Gregor break; 1243a686e1b0SDouglas Gregor 1244718292f2SDouglas Gregor default: 1245a686e1b0SDouglas Gregor if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 1246718292f2SDouglas Gregor return; 1247718292f2SDouglas Gregor break; 1248718292f2SDouglas Gregor } 1249718292f2SDouglas Gregor 1250718292f2SDouglas Gregor consumeToken(); 1251718292f2SDouglas Gregor } while (true); 1252718292f2SDouglas Gregor } 1253718292f2SDouglas Gregor 1254e7ab3669SDouglas Gregor /// \brief Parse a module-id. 1255e7ab3669SDouglas Gregor /// 1256e7ab3669SDouglas Gregor /// module-id: 1257e7ab3669SDouglas Gregor /// identifier 1258e7ab3669SDouglas Gregor /// identifier '.' module-id 1259e7ab3669SDouglas Gregor /// 1260e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise. 1261e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) { 1262e7ab3669SDouglas Gregor Id.clear(); 1263e7ab3669SDouglas Gregor do { 12643cd34c76SDaniel Jasper if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) { 1265e7ab3669SDouglas Gregor Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 1266e7ab3669SDouglas Gregor consumeToken(); 1267e7ab3669SDouglas Gregor } else { 1268e7ab3669SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 1269e7ab3669SDouglas Gregor return true; 1270e7ab3669SDouglas Gregor } 1271e7ab3669SDouglas Gregor 1272e7ab3669SDouglas Gregor if (!Tok.is(MMToken::Period)) 1273e7ab3669SDouglas Gregor break; 1274e7ab3669SDouglas Gregor 1275e7ab3669SDouglas Gregor consumeToken(); 1276e7ab3669SDouglas Gregor } while (true); 1277e7ab3669SDouglas Gregor 1278e7ab3669SDouglas Gregor return false; 1279e7ab3669SDouglas Gregor } 1280e7ab3669SDouglas Gregor 1281a686e1b0SDouglas Gregor namespace { 1282a686e1b0SDouglas Gregor /// \brief Enumerates the known attributes. 1283a686e1b0SDouglas Gregor enum AttributeKind { 1284a686e1b0SDouglas Gregor /// \brief An unknown attribute. 1285a686e1b0SDouglas Gregor AT_unknown, 1286a686e1b0SDouglas Gregor /// \brief The 'system' attribute. 128735b13eceSDouglas Gregor AT_system, 128877944868SRichard Smith /// \brief The 'extern_c' attribute. 128977944868SRichard Smith AT_extern_c, 129035b13eceSDouglas Gregor /// \brief The 'exhaustive' attribute. 129135b13eceSDouglas Gregor AT_exhaustive 1292a686e1b0SDouglas Gregor }; 1293ab9db510SAlexander Kornienko } 1294a686e1b0SDouglas Gregor 1295718292f2SDouglas Gregor /// \brief Parse a module declaration. 1296718292f2SDouglas Gregor /// 1297718292f2SDouglas Gregor /// module-declaration: 129897292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 1299a686e1b0SDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 1300a686e1b0SDouglas Gregor /// { module-member* } 1301a686e1b0SDouglas Gregor /// 1302718292f2SDouglas Gregor /// module-member: 13031fb5c3a6SDouglas Gregor /// requires-declaration 1304718292f2SDouglas Gregor /// header-declaration 1305e7ab3669SDouglas Gregor /// submodule-declaration 13062b82c2a5SDouglas Gregor /// export-declaration 13076ddfca91SDouglas Gregor /// link-declaration 130873441091SDouglas Gregor /// 130973441091SDouglas Gregor /// submodule-declaration: 131073441091SDouglas Gregor /// module-declaration 131173441091SDouglas Gregor /// inferred-submodule-declaration 1312718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() { 1313755b2055SDouglas Gregor assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 131497292843SDaniel Jasper Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword)); 131597292843SDaniel Jasper if (Tok.is(MMToken::ExternKeyword)) { 131697292843SDaniel Jasper parseExternModuleDecl(); 131797292843SDaniel Jasper return; 131897292843SDaniel Jasper } 131997292843SDaniel Jasper 1320f2161a70SDouglas Gregor // Parse 'explicit' or 'framework' keyword, if present. 1321e7ab3669SDouglas Gregor SourceLocation ExplicitLoc; 1322718292f2SDouglas Gregor bool Explicit = false; 1323f2161a70SDouglas Gregor bool Framework = false; 1324755b2055SDouglas Gregor 1325f2161a70SDouglas Gregor // Parse 'explicit' keyword, if present. 1326f2161a70SDouglas Gregor if (Tok.is(MMToken::ExplicitKeyword)) { 1327e7ab3669SDouglas Gregor ExplicitLoc = consumeToken(); 1328f2161a70SDouglas Gregor Explicit = true; 1329f2161a70SDouglas Gregor } 1330f2161a70SDouglas Gregor 1331f2161a70SDouglas Gregor // Parse 'framework' keyword, if present. 1332755b2055SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 1333755b2055SDouglas Gregor consumeToken(); 1334755b2055SDouglas Gregor Framework = true; 1335755b2055SDouglas Gregor } 1336718292f2SDouglas Gregor 1337718292f2SDouglas Gregor // Parse 'module' keyword. 1338718292f2SDouglas Gregor if (!Tok.is(MMToken::ModuleKeyword)) { 1339d6343c99SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1340718292f2SDouglas Gregor consumeToken(); 1341718292f2SDouglas Gregor HadError = true; 1342718292f2SDouglas Gregor return; 1343718292f2SDouglas Gregor } 1344718292f2SDouglas Gregor consumeToken(); // 'module' keyword 1345718292f2SDouglas Gregor 134673441091SDouglas Gregor // If we have a wildcard for the module name, this is an inferred submodule. 134773441091SDouglas Gregor // Parse it. 134873441091SDouglas Gregor if (Tok.is(MMToken::Star)) 13499194a91dSDouglas Gregor return parseInferredModuleDecl(Framework, Explicit); 135073441091SDouglas Gregor 1351718292f2SDouglas Gregor // Parse the module name. 1352e7ab3669SDouglas Gregor ModuleId Id; 1353e7ab3669SDouglas Gregor if (parseModuleId(Id)) { 1354718292f2SDouglas Gregor HadError = true; 1355718292f2SDouglas Gregor return; 1356718292f2SDouglas Gregor } 1357e7ab3669SDouglas Gregor 1358e7ab3669SDouglas Gregor if (ActiveModule) { 1359e7ab3669SDouglas Gregor if (Id.size() > 1) { 1360e7ab3669SDouglas Gregor Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 1361e7ab3669SDouglas Gregor << SourceRange(Id.front().second, Id.back().second); 1362e7ab3669SDouglas Gregor 1363e7ab3669SDouglas Gregor HadError = true; 1364e7ab3669SDouglas Gregor return; 1365e7ab3669SDouglas Gregor } 1366e7ab3669SDouglas Gregor } else if (Id.size() == 1 && Explicit) { 1367e7ab3669SDouglas Gregor // Top-level modules can't be explicit. 1368e7ab3669SDouglas Gregor Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 1369e7ab3669SDouglas Gregor Explicit = false; 1370e7ab3669SDouglas Gregor ExplicitLoc = SourceLocation(); 1371e7ab3669SDouglas Gregor HadError = true; 1372e7ab3669SDouglas Gregor } 1373e7ab3669SDouglas Gregor 1374e7ab3669SDouglas Gregor Module *PreviousActiveModule = ActiveModule; 1375e7ab3669SDouglas Gregor if (Id.size() > 1) { 1376e7ab3669SDouglas Gregor // This module map defines a submodule. Go find the module of which it 1377e7ab3669SDouglas Gregor // is a submodule. 1378d2d442caSCraig Topper ActiveModule = nullptr; 13794b8a9e95SBen Langmuir const Module *TopLevelModule = nullptr; 1380e7ab3669SDouglas Gregor for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 1381e7ab3669SDouglas Gregor if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 13824b8a9e95SBen Langmuir if (I == 0) 13834b8a9e95SBen Langmuir TopLevelModule = Next; 1384e7ab3669SDouglas Gregor ActiveModule = Next; 1385e7ab3669SDouglas Gregor continue; 1386e7ab3669SDouglas Gregor } 1387e7ab3669SDouglas Gregor 1388e7ab3669SDouglas Gregor if (ActiveModule) { 1389e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 13905b5d21eaSRichard Smith << Id[I].first 13915b5d21eaSRichard Smith << ActiveModule->getTopLevelModule()->getFullModuleName(); 1392e7ab3669SDouglas Gregor } else { 1393e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 1394e7ab3669SDouglas Gregor } 1395e7ab3669SDouglas Gregor HadError = true; 1396e7ab3669SDouglas Gregor return; 1397e7ab3669SDouglas Gregor } 13984b8a9e95SBen Langmuir 13994b8a9e95SBen Langmuir if (ModuleMapFile != Map.getContainingModuleMapFile(TopLevelModule)) { 14004b8a9e95SBen Langmuir assert(ModuleMapFile != Map.getModuleMapFileForUniquing(TopLevelModule) && 14014b8a9e95SBen Langmuir "submodule defined in same file as 'module *' that allowed its " 14024b8a9e95SBen Langmuir "top-level module"); 14034b8a9e95SBen Langmuir Map.addAdditionalModuleMapFile(TopLevelModule, ModuleMapFile); 14044b8a9e95SBen Langmuir } 1405e7ab3669SDouglas Gregor } 1406e7ab3669SDouglas Gregor 1407e7ab3669SDouglas Gregor StringRef ModuleName = Id.back().first; 1408e7ab3669SDouglas Gregor SourceLocation ModuleNameLoc = Id.back().second; 1409718292f2SDouglas Gregor 1410a686e1b0SDouglas Gregor // Parse the optional attribute list. 14114442605fSBill Wendling Attributes Attrs; 14129194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 1413a686e1b0SDouglas Gregor 1414718292f2SDouglas Gregor // Parse the opening brace. 1415718292f2SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 1416718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 1417718292f2SDouglas Gregor << ModuleName; 1418718292f2SDouglas Gregor HadError = true; 1419718292f2SDouglas Gregor return; 1420718292f2SDouglas Gregor } 1421718292f2SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 1422718292f2SDouglas Gregor 1423718292f2SDouglas Gregor // Determine whether this (sub)module has already been defined. 1424eb90e830SDouglas Gregor if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 1425fcc54a3bSDouglas Gregor if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { 1426fcc54a3bSDouglas Gregor // Skip the module definition. 1427fcc54a3bSDouglas Gregor skipUntil(MMToken::RBrace); 1428fcc54a3bSDouglas Gregor if (Tok.is(MMToken::RBrace)) 1429fcc54a3bSDouglas Gregor consumeToken(); 1430fcc54a3bSDouglas Gregor else { 1431fcc54a3bSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1432fcc54a3bSDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1433fcc54a3bSDouglas Gregor HadError = true; 1434fcc54a3bSDouglas Gregor } 1435fcc54a3bSDouglas Gregor return; 1436fcc54a3bSDouglas Gregor } 1437fcc54a3bSDouglas Gregor 1438718292f2SDouglas Gregor Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 1439718292f2SDouglas Gregor << ModuleName; 1440eb90e830SDouglas Gregor Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 1441718292f2SDouglas Gregor 1442718292f2SDouglas Gregor // Skip the module definition. 1443718292f2SDouglas Gregor skipUntil(MMToken::RBrace); 1444718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1445718292f2SDouglas Gregor consumeToken(); 1446718292f2SDouglas Gregor 1447718292f2SDouglas Gregor HadError = true; 1448718292f2SDouglas Gregor return; 1449718292f2SDouglas Gregor } 1450718292f2SDouglas Gregor 1451718292f2SDouglas Gregor // Start defining this module. 14529d6448b1SBen Langmuir ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, 14539d6448b1SBen Langmuir Explicit).first; 1454eb90e830SDouglas Gregor ActiveModule->DefinitionLoc = ModuleNameLoc; 1455963c5535SDouglas Gregor if (Attrs.IsSystem || IsSystem) 1456a686e1b0SDouglas Gregor ActiveModule->IsSystem = true; 145777944868SRichard Smith if (Attrs.IsExternC) 145877944868SRichard Smith ActiveModule->IsExternC = true; 14593c1a41adSRichard Smith ActiveModule->Directory = Directory; 1460718292f2SDouglas Gregor 1461718292f2SDouglas Gregor bool Done = false; 1462718292f2SDouglas Gregor do { 1463718292f2SDouglas Gregor switch (Tok.Kind) { 1464718292f2SDouglas Gregor case MMToken::EndOfFile: 1465718292f2SDouglas Gregor case MMToken::RBrace: 1466718292f2SDouglas Gregor Done = true; 1467718292f2SDouglas Gregor break; 1468718292f2SDouglas Gregor 146935b13eceSDouglas Gregor case MMToken::ConfigMacros: 147035b13eceSDouglas Gregor parseConfigMacros(); 147135b13eceSDouglas Gregor break; 147235b13eceSDouglas Gregor 1473fb912657SDouglas Gregor case MMToken::Conflict: 1474fb912657SDouglas Gregor parseConflict(); 1475fb912657SDouglas Gregor break; 1476fb912657SDouglas Gregor 1477718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 147897292843SDaniel Jasper case MMToken::ExternKeyword: 1479f2161a70SDouglas Gregor case MMToken::FrameworkKeyword: 1480718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1481718292f2SDouglas Gregor parseModuleDecl(); 1482718292f2SDouglas Gregor break; 1483718292f2SDouglas Gregor 14842b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 14852b82c2a5SDouglas Gregor parseExportDecl(); 14862b82c2a5SDouglas Gregor break; 14872b82c2a5SDouglas Gregor 1488ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 1489ba7f2f71SDaniel Jasper parseUseDecl(); 1490ba7f2f71SDaniel Jasper break; 1491ba7f2f71SDaniel Jasper 14921fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 14931fb5c3a6SDouglas Gregor parseRequiresDecl(); 14941fb5c3a6SDouglas Gregor break; 14951fb5c3a6SDouglas Gregor 1496202210b3SRichard Smith case MMToken::TextualKeyword: 1497202210b3SRichard Smith parseHeaderDecl(MMToken::TextualKeyword, consumeToken()); 1498306d8920SRichard Smith break; 1499306d8920SRichard Smith 1500524e33e1SDouglas Gregor case MMToken::UmbrellaKeyword: { 1501524e33e1SDouglas Gregor SourceLocation UmbrellaLoc = consumeToken(); 1502524e33e1SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) 1503b53e5483SLawrence Crowl parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc); 1504524e33e1SDouglas Gregor else 1505524e33e1SDouglas Gregor parseUmbrellaDirDecl(UmbrellaLoc); 1506718292f2SDouglas Gregor break; 1507524e33e1SDouglas Gregor } 1508718292f2SDouglas Gregor 1509202210b3SRichard Smith case MMToken::ExcludeKeyword: 1510202210b3SRichard Smith parseHeaderDecl(MMToken::ExcludeKeyword, consumeToken()); 151159527666SDouglas Gregor break; 151259527666SDouglas Gregor 1513202210b3SRichard Smith case MMToken::PrivateKeyword: 1514202210b3SRichard Smith parseHeaderDecl(MMToken::PrivateKeyword, consumeToken()); 1515b53e5483SLawrence Crowl break; 1516b53e5483SLawrence Crowl 1517322f633cSDouglas Gregor case MMToken::HeaderKeyword: 1518202210b3SRichard Smith parseHeaderDecl(MMToken::HeaderKeyword, consumeToken()); 1519718292f2SDouglas Gregor break; 1520718292f2SDouglas Gregor 15216ddfca91SDouglas Gregor case MMToken::LinkKeyword: 15226ddfca91SDouglas Gregor parseLinkDecl(); 15236ddfca91SDouglas Gregor break; 15246ddfca91SDouglas Gregor 1525718292f2SDouglas Gregor default: 1526718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 1527718292f2SDouglas Gregor consumeToken(); 1528718292f2SDouglas Gregor break; 1529718292f2SDouglas Gregor } 1530718292f2SDouglas Gregor } while (!Done); 1531718292f2SDouglas Gregor 1532718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1533718292f2SDouglas Gregor consumeToken(); 1534718292f2SDouglas Gregor else { 1535718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1536718292f2SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1537718292f2SDouglas Gregor HadError = true; 1538718292f2SDouglas Gregor } 1539718292f2SDouglas Gregor 154011dfe6feSDouglas Gregor // If the active module is a top-level framework, and there are no link 154111dfe6feSDouglas Gregor // libraries, automatically link against the framework. 154211dfe6feSDouglas Gregor if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() && 154311dfe6feSDouglas Gregor ActiveModule->LinkLibraries.empty()) { 154411dfe6feSDouglas Gregor inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager()); 154511dfe6feSDouglas Gregor } 154611dfe6feSDouglas Gregor 1547ec8c9752SBen Langmuir // If the module meets all requirements but is still unavailable, mark the 1548ec8c9752SBen Langmuir // whole tree as unavailable to prevent it from building. 1549ec8c9752SBen Langmuir if (!ActiveModule->IsAvailable && !ActiveModule->IsMissingRequirement && 1550ec8c9752SBen Langmuir ActiveModule->Parent) { 1551ec8c9752SBen Langmuir ActiveModule->getTopLevelModule()->markUnavailable(); 1552ec8c9752SBen Langmuir ActiveModule->getTopLevelModule()->MissingHeaders.append( 1553ec8c9752SBen Langmuir ActiveModule->MissingHeaders.begin(), ActiveModule->MissingHeaders.end()); 1554ec8c9752SBen Langmuir } 1555ec8c9752SBen Langmuir 1556e7ab3669SDouglas Gregor // We're done parsing this module. Pop back to the previous module. 1557e7ab3669SDouglas Gregor ActiveModule = PreviousActiveModule; 1558718292f2SDouglas Gregor } 1559718292f2SDouglas Gregor 156097292843SDaniel Jasper /// \brief Parse an extern module declaration. 156197292843SDaniel Jasper /// 156297292843SDaniel Jasper /// extern module-declaration: 156397292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 156497292843SDaniel Jasper void ModuleMapParser::parseExternModuleDecl() { 156597292843SDaniel Jasper assert(Tok.is(MMToken::ExternKeyword)); 1566ae6df27eSRichard Smith SourceLocation ExternLoc = consumeToken(); // 'extern' keyword 156797292843SDaniel Jasper 156897292843SDaniel Jasper // Parse 'module' keyword. 156997292843SDaniel Jasper if (!Tok.is(MMToken::ModuleKeyword)) { 157097292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 157197292843SDaniel Jasper consumeToken(); 157297292843SDaniel Jasper HadError = true; 157397292843SDaniel Jasper return; 157497292843SDaniel Jasper } 157597292843SDaniel Jasper consumeToken(); // 'module' keyword 157697292843SDaniel Jasper 157797292843SDaniel Jasper // Parse the module name. 157897292843SDaniel Jasper ModuleId Id; 157997292843SDaniel Jasper if (parseModuleId(Id)) { 158097292843SDaniel Jasper HadError = true; 158197292843SDaniel Jasper return; 158297292843SDaniel Jasper } 158397292843SDaniel Jasper 158497292843SDaniel Jasper // Parse the referenced module map file name. 158597292843SDaniel Jasper if (!Tok.is(MMToken::StringLiteral)) { 158697292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file); 158797292843SDaniel Jasper HadError = true; 158897292843SDaniel Jasper return; 158997292843SDaniel Jasper } 159097292843SDaniel Jasper std::string FileName = Tok.getString(); 159197292843SDaniel Jasper consumeToken(); // filename 159297292843SDaniel Jasper 159397292843SDaniel Jasper StringRef FileNameRef = FileName; 159497292843SDaniel Jasper SmallString<128> ModuleMapFileName; 159597292843SDaniel Jasper if (llvm::sys::path::is_relative(FileNameRef)) { 159697292843SDaniel Jasper ModuleMapFileName += Directory->getName(); 159797292843SDaniel Jasper llvm::sys::path::append(ModuleMapFileName, FileName); 159892e1b62dSYaron Keren FileNameRef = ModuleMapFileName; 159997292843SDaniel Jasper } 160097292843SDaniel Jasper if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef)) 16019acb99e3SRichard Smith Map.parseModuleMapFile( 16029acb99e3SRichard Smith File, /*IsSystem=*/false, 16039acb99e3SRichard Smith Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd 16049acb99e3SRichard Smith ? Directory 1605ae6df27eSRichard Smith : File->getDir(), ExternLoc); 160697292843SDaniel Jasper } 160797292843SDaniel Jasper 16087ff29148SBen Langmuir /// Whether to add the requirement \p Feature to the module \p M. 16097ff29148SBen Langmuir /// 16107ff29148SBen Langmuir /// This preserves backwards compatibility for two hacks in the Darwin system 16117ff29148SBen Langmuir /// module map files: 16127ff29148SBen Langmuir /// 16137ff29148SBen Langmuir /// 1. The use of 'requires excluded' to make headers non-modular, which 16147ff29148SBen Langmuir /// should really be mapped to 'textual' now that we have this feature. We 16157ff29148SBen Langmuir /// drop the 'excluded' requirement, and set \p IsRequiresExcludedHack to 16167ff29148SBen Langmuir /// true. Later, this bit will be used to map all the headers inside this 16177ff29148SBen Langmuir /// module to 'textual'. 16187ff29148SBen Langmuir /// 16197ff29148SBen Langmuir /// This affects Darwin.C.excluded (for assert.h) and Tcl.Private. 16207ff29148SBen Langmuir /// 16217ff29148SBen Langmuir /// 2. Removes a bogus cplusplus requirement from IOKit.avc. This requirement 16227ff29148SBen Langmuir /// was never correct and causes issues now that we check it, so drop it. 16237ff29148SBen Langmuir static bool shouldAddRequirement(Module *M, StringRef Feature, 16247ff29148SBen Langmuir bool &IsRequiresExcludedHack) { 16257ff29148SBen Langmuir static const StringRef DarwinCExcluded[] = {"Darwin", "C", "excluded"}; 16267ff29148SBen Langmuir static const StringRef TclPrivate[] = {"Tcl", "Private"}; 16277ff29148SBen Langmuir static const StringRef IOKitAVC[] = {"IOKit", "avc"}; 16287ff29148SBen Langmuir 16297ff29148SBen Langmuir if (Feature == "excluded" && (M->fullModuleNameIs(DarwinCExcluded) || 16307ff29148SBen Langmuir M->fullModuleNameIs(TclPrivate))) { 16317ff29148SBen Langmuir IsRequiresExcludedHack = true; 16327ff29148SBen Langmuir return false; 16337ff29148SBen Langmuir } else if (Feature == "cplusplus" && M->fullModuleNameIs(IOKitAVC)) { 16347ff29148SBen Langmuir return false; 16357ff29148SBen Langmuir } 16367ff29148SBen Langmuir 16377ff29148SBen Langmuir return true; 16387ff29148SBen Langmuir } 16397ff29148SBen Langmuir 16401fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration. 16411fb5c3a6SDouglas Gregor /// 16421fb5c3a6SDouglas Gregor /// requires-declaration: 16431fb5c3a6SDouglas Gregor /// 'requires' feature-list 16441fb5c3a6SDouglas Gregor /// 16451fb5c3a6SDouglas Gregor /// feature-list: 1646a3feee2aSRichard Smith /// feature ',' feature-list 1647a3feee2aSRichard Smith /// feature 1648a3feee2aSRichard Smith /// 1649a3feee2aSRichard Smith /// feature: 1650a3feee2aSRichard Smith /// '!'[opt] identifier 16511fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() { 16521fb5c3a6SDouglas Gregor assert(Tok.is(MMToken::RequiresKeyword)); 16531fb5c3a6SDouglas Gregor 16541fb5c3a6SDouglas Gregor // Parse 'requires' keyword. 16551fb5c3a6SDouglas Gregor consumeToken(); 16561fb5c3a6SDouglas Gregor 16571fb5c3a6SDouglas Gregor // Parse the feature-list. 16581fb5c3a6SDouglas Gregor do { 1659a3feee2aSRichard Smith bool RequiredState = true; 1660a3feee2aSRichard Smith if (Tok.is(MMToken::Exclaim)) { 1661a3feee2aSRichard Smith RequiredState = false; 1662a3feee2aSRichard Smith consumeToken(); 1663a3feee2aSRichard Smith } 1664a3feee2aSRichard Smith 16651fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 16661fb5c3a6SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 16671fb5c3a6SDouglas Gregor HadError = true; 16681fb5c3a6SDouglas Gregor return; 16691fb5c3a6SDouglas Gregor } 16701fb5c3a6SDouglas Gregor 16711fb5c3a6SDouglas Gregor // Consume the feature name. 16721fb5c3a6SDouglas Gregor std::string Feature = Tok.getString(); 16731fb5c3a6SDouglas Gregor consumeToken(); 16741fb5c3a6SDouglas Gregor 16757ff29148SBen Langmuir bool IsRequiresExcludedHack = false; 16767ff29148SBen Langmuir bool ShouldAddRequirement = 16777ff29148SBen Langmuir shouldAddRequirement(ActiveModule, Feature, IsRequiresExcludedHack); 16787ff29148SBen Langmuir 16797ff29148SBen Langmuir if (IsRequiresExcludedHack) 16807ff29148SBen Langmuir UsesRequiresExcludedHack.insert(ActiveModule); 16817ff29148SBen Langmuir 16827ff29148SBen Langmuir if (ShouldAddRequirement) { 16831fb5c3a6SDouglas Gregor // Add this feature. 16847ff29148SBen Langmuir ActiveModule->addRequirement(Feature, RequiredState, Map.LangOpts, 16857ff29148SBen Langmuir *Map.Target); 16867ff29148SBen Langmuir } 16871fb5c3a6SDouglas Gregor 16881fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Comma)) 16891fb5c3a6SDouglas Gregor break; 16901fb5c3a6SDouglas Gregor 16911fb5c3a6SDouglas Gregor // Consume the comma. 16921fb5c3a6SDouglas Gregor consumeToken(); 16931fb5c3a6SDouglas Gregor } while (true); 16941fb5c3a6SDouglas Gregor } 16951fb5c3a6SDouglas Gregor 1696f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the 1697f2161a70SDouglas Gregor /// subframework in which the given module lives. 1698bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod, 1699f857950dSDmitri Gribenko SmallVectorImpl<char> &Path) { 1700f2161a70SDouglas Gregor // Collect the framework names from the given module to the top-level module. 1701f857950dSDmitri Gribenko SmallVector<StringRef, 2> Paths; 1702f2161a70SDouglas Gregor for (; Mod; Mod = Mod->Parent) { 1703f2161a70SDouglas Gregor if (Mod->IsFramework) 1704f2161a70SDouglas Gregor Paths.push_back(Mod->Name); 1705f2161a70SDouglas Gregor } 1706f2161a70SDouglas Gregor 1707f2161a70SDouglas Gregor if (Paths.empty()) 1708f2161a70SDouglas Gregor return; 1709f2161a70SDouglas Gregor 1710f2161a70SDouglas Gregor // Add Frameworks/Name.framework for each subframework. 171117381a06SBenjamin Kramer for (unsigned I = Paths.size() - 1; I != 0; --I) 171217381a06SBenjamin Kramer llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework"); 1713f2161a70SDouglas Gregor } 1714f2161a70SDouglas Gregor 1715718292f2SDouglas Gregor /// \brief Parse a header declaration. 1716718292f2SDouglas Gregor /// 1717718292f2SDouglas Gregor /// header-declaration: 1718306d8920SRichard Smith /// 'textual'[opt] 'header' string-literal 1719202210b3SRichard Smith /// 'private' 'textual'[opt] 'header' string-literal 1720202210b3SRichard Smith /// 'exclude' 'header' string-literal 1721202210b3SRichard Smith /// 'umbrella' 'header' string-literal 1722306d8920SRichard Smith /// 1723306d8920SRichard Smith /// FIXME: Support 'private textual header'. 1724b53e5483SLawrence Crowl void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken, 1725b53e5483SLawrence Crowl SourceLocation LeadingLoc) { 1726202210b3SRichard Smith // We've already consumed the first token. 1727202210b3SRichard Smith ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader; 1728202210b3SRichard Smith if (LeadingToken == MMToken::PrivateKeyword) { 1729202210b3SRichard Smith Role = ModuleMap::PrivateHeader; 1730202210b3SRichard Smith // 'private' may optionally be followed by 'textual'. 1731202210b3SRichard Smith if (Tok.is(MMToken::TextualKeyword)) { 1732202210b3SRichard Smith LeadingToken = Tok.Kind; 17331871ed3dSBenjamin Kramer consumeToken(); 1734202210b3SRichard Smith } 1735202210b3SRichard Smith } 17367ff29148SBen Langmuir 1737202210b3SRichard Smith if (LeadingToken == MMToken::TextualKeyword) 1738202210b3SRichard Smith Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader); 1739202210b3SRichard Smith 17407ff29148SBen Langmuir if (UsesRequiresExcludedHack.count(ActiveModule)) { 17417ff29148SBen Langmuir // Mark this header 'textual' (see doc comment for 17427ff29148SBen Langmuir // Module::UsesRequiresExcludedHack). 17437ff29148SBen Langmuir Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader); 17447ff29148SBen Langmuir } 17457ff29148SBen Langmuir 1746202210b3SRichard Smith if (LeadingToken != MMToken::HeaderKeyword) { 1747202210b3SRichard Smith if (!Tok.is(MMToken::HeaderKeyword)) { 1748202210b3SRichard Smith Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1749202210b3SRichard Smith << (LeadingToken == MMToken::PrivateKeyword ? "private" : 1750202210b3SRichard Smith LeadingToken == MMToken::ExcludeKeyword ? "exclude" : 1751202210b3SRichard Smith LeadingToken == MMToken::TextualKeyword ? "textual" : "umbrella"); 1752202210b3SRichard Smith return; 1753202210b3SRichard Smith } 1754202210b3SRichard Smith consumeToken(); 1755202210b3SRichard Smith } 1756718292f2SDouglas Gregor 1757718292f2SDouglas Gregor // Parse the header name. 1758718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1759718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1760718292f2SDouglas Gregor << "header"; 1761718292f2SDouglas Gregor HadError = true; 1762718292f2SDouglas Gregor return; 1763718292f2SDouglas Gregor } 17643c1a41adSRichard Smith Module::UnresolvedHeaderDirective Header; 17650761a8a0SDaniel Jasper Header.FileName = Tok.getString(); 17660761a8a0SDaniel Jasper Header.FileNameLoc = consumeToken(); 1767718292f2SDouglas Gregor 1768524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1769b53e5483SLawrence Crowl if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) { 17700761a8a0SDaniel Jasper Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash) 1771524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1772322f633cSDouglas Gregor HadError = true; 1773322f633cSDouglas Gregor return; 1774322f633cSDouglas Gregor } 1775322f633cSDouglas Gregor 17765257fc63SDouglas Gregor // Look for this file. 1777d2d442caSCraig Topper const FileEntry *File = nullptr; 1778d2d442caSCraig Topper const FileEntry *BuiltinFile = nullptr; 17793c1a41adSRichard Smith SmallString<128> RelativePathName; 17800761a8a0SDaniel Jasper if (llvm::sys::path::is_absolute(Header.FileName)) { 17813c1a41adSRichard Smith RelativePathName = Header.FileName; 17823c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(RelativePathName); 1783e7ab3669SDouglas Gregor } else { 1784e7ab3669SDouglas Gregor // Search for the header file within the search directory. 17853c1a41adSRichard Smith SmallString<128> FullPathName(Directory->getName()); 17863c1a41adSRichard Smith unsigned FullPathLength = FullPathName.size(); 1787755b2055SDouglas Gregor 1788f2161a70SDouglas Gregor if (ActiveModule->isPartOfFramework()) { 17893c1a41adSRichard Smith appendSubframeworkPaths(ActiveModule, RelativePathName); 1790755b2055SDouglas Gregor 1791e7ab3669SDouglas Gregor // Check whether this file is in the public headers. 17923c1a41adSRichard Smith llvm::sys::path::append(RelativePathName, "Headers", Header.FileName); 179392e1b62dSYaron Keren llvm::sys::path::append(FullPathName, RelativePathName); 17943c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(FullPathName); 1795e7ab3669SDouglas Gregor 1796e7ab3669SDouglas Gregor if (!File) { 1797e7ab3669SDouglas Gregor // Check whether this file is in the private headers. 17983c1a41adSRichard Smith // FIXME: Should we retain the subframework paths here? 17993c1a41adSRichard Smith RelativePathName.clear(); 18003c1a41adSRichard Smith FullPathName.resize(FullPathLength); 18013c1a41adSRichard Smith llvm::sys::path::append(RelativePathName, "PrivateHeaders", 18023c1a41adSRichard Smith Header.FileName); 180392e1b62dSYaron Keren llvm::sys::path::append(FullPathName, RelativePathName); 18043c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(FullPathName); 1805e7ab3669SDouglas Gregor } 1806e7ab3669SDouglas Gregor } else { 1807e7ab3669SDouglas Gregor // Lookup for normal headers. 18083c1a41adSRichard Smith llvm::sys::path::append(RelativePathName, Header.FileName); 180992e1b62dSYaron Keren llvm::sys::path::append(FullPathName, RelativePathName); 18103c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(FullPathName); 18113ec6663bSDouglas Gregor 18123ec6663bSDouglas Gregor // If this is a system module with a top-level header, this header 18133ec6663bSDouglas Gregor // may have a counterpart (or replacement) in the set of headers 18143ec6663bSDouglas Gregor // supplied by Clang. Find that builtin header. 1815b53e5483SLawrence Crowl if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword && 1816b53e5483SLawrence Crowl BuiltinIncludeDir && BuiltinIncludeDir != Directory && 18170761a8a0SDaniel Jasper isBuiltinHeader(Header.FileName)) { 18182c1dd271SDylan Noblesmith SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); 18190761a8a0SDaniel Jasper llvm::sys::path::append(BuiltinPathName, Header.FileName); 18203ec6663bSDouglas Gregor BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); 18213ec6663bSDouglas Gregor 18223ec6663bSDouglas Gregor // If Clang supplies this header but the underlying system does not, 18233ec6663bSDouglas Gregor // just silently swap in our builtin version. Otherwise, we'll end 18243ec6663bSDouglas Gregor // up adding both (later). 182542413141SRichard Smith // 182642413141SRichard Smith // For local visibility, entirely replace the system file with our 182742413141SRichard Smith // one and textually include the system one. We need to pass macros 182842413141SRichard Smith // from our header to the system one if we #include_next it. 182942413141SRichard Smith // 183042413141SRichard Smith // FIXME: Can we do this in all cases? 183142413141SRichard Smith if (BuiltinFile && (!File || Map.LangOpts.ModulesLocalVisibility)) { 18323ec6663bSDouglas Gregor File = BuiltinFile; 18333c1a41adSRichard Smith RelativePathName = BuiltinPathName; 1834d2d442caSCraig Topper BuiltinFile = nullptr; 18353ec6663bSDouglas Gregor } 18363ec6663bSDouglas Gregor } 1837e7ab3669SDouglas Gregor } 1838e7ab3669SDouglas Gregor } 18395257fc63SDouglas Gregor 18405257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 18415257fc63SDouglas Gregor // Come up with a lazy way to do this. 1842e7ab3669SDouglas Gregor if (File) { 184397da9178SDaniel Jasper if (LeadingToken == MMToken::UmbrellaKeyword) { 1844322f633cSDouglas Gregor const DirectoryEntry *UmbrellaDir = File->getDir(); 184559527666SDouglas Gregor if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { 1846b53e5483SLawrence Crowl Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash) 184759527666SDouglas Gregor << UmbrellaModule->getFullModuleName(); 1848322f633cSDouglas Gregor HadError = true; 18495257fc63SDouglas Gregor } else { 1850322f633cSDouglas Gregor // Record this umbrella header. 18512b63d15fSRichard Smith Map.setUmbrellaHeader(ActiveModule, File, RelativePathName.str()); 1852322f633cSDouglas Gregor } 1853feb54b6dSRichard Smith } else if (LeadingToken == MMToken::ExcludeKeyword) { 18540101b540SHans Wennborg Module::Header H = {RelativePathName.str(), File}; 18550101b540SHans Wennborg Map.excludeHeader(ActiveModule, H); 1856322f633cSDouglas Gregor } else { 185725d50758SRichard Smith // If there is a builtin counterpart to this file, add it now, before 185825d50758SRichard Smith // the "real" header, so we build the built-in one first when building 185925d50758SRichard Smith // the module. 18600101b540SHans Wennborg if (BuiltinFile) { 18613c1a41adSRichard Smith // FIXME: Taking the name from the FileEntry is unstable and can give 18623c1a41adSRichard Smith // different results depending on how we've previously named that file 18633c1a41adSRichard Smith // in this build. 18640101b540SHans Wennborg Module::Header H = { BuiltinFile->getName(), BuiltinFile }; 18650101b540SHans Wennborg Map.addHeader(ActiveModule, H, Role); 18660101b540SHans Wennborg } 186725d50758SRichard Smith 1868202210b3SRichard Smith // Record this header. 18690101b540SHans Wennborg Module::Header H = { RelativePathName.str(), File }; 18700101b540SHans Wennborg Map.addHeader(ActiveModule, H, Role); 18715257fc63SDouglas Gregor } 1872b53e5483SLawrence Crowl } else if (LeadingToken != MMToken::ExcludeKeyword) { 18734b27a64bSDouglas Gregor // Ignore excluded header files. They're optional anyway. 18744b27a64bSDouglas Gregor 18750761a8a0SDaniel Jasper // If we find a module that has a missing header, we mark this module as 18760761a8a0SDaniel Jasper // unavailable and store the header directive for displaying diagnostics. 18770761a8a0SDaniel Jasper Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword; 1878ec8c9752SBen Langmuir ActiveModule->markUnavailable(); 18790761a8a0SDaniel Jasper ActiveModule->MissingHeaders.push_back(Header); 18805257fc63SDouglas Gregor } 1881718292f2SDouglas Gregor } 1882718292f2SDouglas Gregor 188341f81994SBen Langmuir static int compareModuleHeaders(const Module::Header *A, 188441f81994SBen Langmuir const Module::Header *B) { 188541f81994SBen Langmuir return A->NameAsWritten.compare(B->NameAsWritten); 188641f81994SBen Langmuir } 188741f81994SBen Langmuir 1888524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration. 1889524e33e1SDouglas Gregor /// 1890524e33e1SDouglas Gregor /// umbrella-dir-declaration: 1891524e33e1SDouglas Gregor /// umbrella string-literal 1892524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1893524e33e1SDouglas Gregor // Parse the directory name. 1894524e33e1SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1895524e33e1SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1896524e33e1SDouglas Gregor << "umbrella"; 1897524e33e1SDouglas Gregor HadError = true; 1898524e33e1SDouglas Gregor return; 1899524e33e1SDouglas Gregor } 1900524e33e1SDouglas Gregor 1901524e33e1SDouglas Gregor std::string DirName = Tok.getString(); 1902524e33e1SDouglas Gregor SourceLocation DirNameLoc = consumeToken(); 1903524e33e1SDouglas Gregor 1904524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1905524e33e1SDouglas Gregor if (ActiveModule->Umbrella) { 1906524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1907524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1908524e33e1SDouglas Gregor HadError = true; 1909524e33e1SDouglas Gregor return; 1910524e33e1SDouglas Gregor } 1911524e33e1SDouglas Gregor 1912524e33e1SDouglas Gregor // Look for this file. 1913d2d442caSCraig Topper const DirectoryEntry *Dir = nullptr; 1914524e33e1SDouglas Gregor if (llvm::sys::path::is_absolute(DirName)) 1915524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(DirName); 1916524e33e1SDouglas Gregor else { 19172c1dd271SDylan Noblesmith SmallString<128> PathName; 1918524e33e1SDouglas Gregor PathName = Directory->getName(); 1919524e33e1SDouglas Gregor llvm::sys::path::append(PathName, DirName); 1920524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(PathName); 1921524e33e1SDouglas Gregor } 1922524e33e1SDouglas Gregor 1923524e33e1SDouglas Gregor if (!Dir) { 1924524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1925524e33e1SDouglas Gregor << DirName; 1926524e33e1SDouglas Gregor HadError = true; 1927524e33e1SDouglas Gregor return; 1928524e33e1SDouglas Gregor } 1929524e33e1SDouglas Gregor 19307ff29148SBen Langmuir if (UsesRequiresExcludedHack.count(ActiveModule)) { 19317ff29148SBen Langmuir // Mark this header 'textual' (see doc comment for 19327ff29148SBen Langmuir // ModuleMapParser::UsesRequiresExcludedHack). Although iterating over the 19337ff29148SBen Langmuir // directory is relatively expensive, in practice this only applies to the 19347ff29148SBen Langmuir // uncommonly used Tcl module on Darwin platforms. 19357ff29148SBen Langmuir std::error_code EC; 19367ff29148SBen Langmuir SmallVector<Module::Header, 6> Headers; 19377ff29148SBen Langmuir for (llvm::sys::fs::recursive_directory_iterator I(Dir->getName(), EC), E; 19387ff29148SBen Langmuir I != E && !EC; I.increment(EC)) { 19397ff29148SBen Langmuir if (const FileEntry *FE = SourceMgr.getFileManager().getFile(I->path())) { 19407ff29148SBen Langmuir 19417ff29148SBen Langmuir Module::Header Header = {I->path(), FE}; 19427ff29148SBen Langmuir Headers.push_back(std::move(Header)); 19437ff29148SBen Langmuir } 19447ff29148SBen Langmuir } 19457ff29148SBen Langmuir 19467ff29148SBen Langmuir // Sort header paths so that the pcm doesn't depend on iteration order. 194741f81994SBen Langmuir llvm::array_pod_sort(Headers.begin(), Headers.end(), compareModuleHeaders); 194841f81994SBen Langmuir 19497ff29148SBen Langmuir for (auto &Header : Headers) 19507ff29148SBen Langmuir Map.addHeader(ActiveModule, std::move(Header), ModuleMap::TextualHeader); 19517ff29148SBen Langmuir return; 19527ff29148SBen Langmuir } 19537ff29148SBen Langmuir 1954524e33e1SDouglas Gregor if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1955524e33e1SDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1956524e33e1SDouglas Gregor << OwningModule->getFullModuleName(); 1957524e33e1SDouglas Gregor HadError = true; 1958524e33e1SDouglas Gregor return; 1959524e33e1SDouglas Gregor } 1960524e33e1SDouglas Gregor 1961524e33e1SDouglas Gregor // Record this umbrella directory. 19622b63d15fSRichard Smith Map.setUmbrellaDir(ActiveModule, Dir, DirName); 1963524e33e1SDouglas Gregor } 1964524e33e1SDouglas Gregor 19652b82c2a5SDouglas Gregor /// \brief Parse a module export declaration. 19662b82c2a5SDouglas Gregor /// 19672b82c2a5SDouglas Gregor /// export-declaration: 19682b82c2a5SDouglas Gregor /// 'export' wildcard-module-id 19692b82c2a5SDouglas Gregor /// 19702b82c2a5SDouglas Gregor /// wildcard-module-id: 19712b82c2a5SDouglas Gregor /// identifier 19722b82c2a5SDouglas Gregor /// '*' 19732b82c2a5SDouglas Gregor /// identifier '.' wildcard-module-id 19742b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() { 19752b82c2a5SDouglas Gregor assert(Tok.is(MMToken::ExportKeyword)); 19762b82c2a5SDouglas Gregor SourceLocation ExportLoc = consumeToken(); 19772b82c2a5SDouglas Gregor 19782b82c2a5SDouglas Gregor // Parse the module-id with an optional wildcard at the end. 19792b82c2a5SDouglas Gregor ModuleId ParsedModuleId; 19802b82c2a5SDouglas Gregor bool Wildcard = false; 19812b82c2a5SDouglas Gregor do { 1982306d8920SRichard Smith // FIXME: Support string-literal module names here. 19832b82c2a5SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 19842b82c2a5SDouglas Gregor ParsedModuleId.push_back(std::make_pair(Tok.getString(), 19852b82c2a5SDouglas Gregor Tok.getLocation())); 19862b82c2a5SDouglas Gregor consumeToken(); 19872b82c2a5SDouglas Gregor 19882b82c2a5SDouglas Gregor if (Tok.is(MMToken::Period)) { 19892b82c2a5SDouglas Gregor consumeToken(); 19902b82c2a5SDouglas Gregor continue; 19912b82c2a5SDouglas Gregor } 19922b82c2a5SDouglas Gregor 19932b82c2a5SDouglas Gregor break; 19942b82c2a5SDouglas Gregor } 19952b82c2a5SDouglas Gregor 19962b82c2a5SDouglas Gregor if(Tok.is(MMToken::Star)) { 19972b82c2a5SDouglas Gregor Wildcard = true; 1998f5eedd05SDouglas Gregor consumeToken(); 19992b82c2a5SDouglas Gregor break; 20002b82c2a5SDouglas Gregor } 20012b82c2a5SDouglas Gregor 2002ba7f2f71SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); 20032b82c2a5SDouglas Gregor HadError = true; 20042b82c2a5SDouglas Gregor return; 20052b82c2a5SDouglas Gregor } while (true); 20062b82c2a5SDouglas Gregor 20072b82c2a5SDouglas Gregor Module::UnresolvedExportDecl Unresolved = { 20082b82c2a5SDouglas Gregor ExportLoc, ParsedModuleId, Wildcard 20092b82c2a5SDouglas Gregor }; 20102b82c2a5SDouglas Gregor ActiveModule->UnresolvedExports.push_back(Unresolved); 20112b82c2a5SDouglas Gregor } 20122b82c2a5SDouglas Gregor 20138f4d3ff1SRichard Smith /// \brief Parse a module use declaration. 2014ba7f2f71SDaniel Jasper /// 20158f4d3ff1SRichard Smith /// use-declaration: 20168f4d3ff1SRichard Smith /// 'use' wildcard-module-id 2017ba7f2f71SDaniel Jasper void ModuleMapParser::parseUseDecl() { 2018ba7f2f71SDaniel Jasper assert(Tok.is(MMToken::UseKeyword)); 20198f4d3ff1SRichard Smith auto KWLoc = consumeToken(); 2020ba7f2f71SDaniel Jasper // Parse the module-id. 2021ba7f2f71SDaniel Jasper ModuleId ParsedModuleId; 20223cd34c76SDaniel Jasper parseModuleId(ParsedModuleId); 2023ba7f2f71SDaniel Jasper 20248f4d3ff1SRichard Smith if (ActiveModule->Parent) 20258f4d3ff1SRichard Smith Diags.Report(KWLoc, diag::err_mmap_use_decl_submodule); 20268f4d3ff1SRichard Smith else 2027ba7f2f71SDaniel Jasper ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId); 2028ba7f2f71SDaniel Jasper } 2029ba7f2f71SDaniel Jasper 20306ddfca91SDouglas Gregor /// \brief Parse a link declaration. 20316ddfca91SDouglas Gregor /// 20326ddfca91SDouglas Gregor /// module-declaration: 20336ddfca91SDouglas Gregor /// 'link' 'framework'[opt] string-literal 20346ddfca91SDouglas Gregor void ModuleMapParser::parseLinkDecl() { 20356ddfca91SDouglas Gregor assert(Tok.is(MMToken::LinkKeyword)); 20366ddfca91SDouglas Gregor SourceLocation LinkLoc = consumeToken(); 20376ddfca91SDouglas Gregor 20386ddfca91SDouglas Gregor // Parse the optional 'framework' keyword. 20396ddfca91SDouglas Gregor bool IsFramework = false; 20406ddfca91SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 20416ddfca91SDouglas Gregor consumeToken(); 20426ddfca91SDouglas Gregor IsFramework = true; 20436ddfca91SDouglas Gregor } 20446ddfca91SDouglas Gregor 20456ddfca91SDouglas Gregor // Parse the library name 20466ddfca91SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 20476ddfca91SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) 20486ddfca91SDouglas Gregor << IsFramework << SourceRange(LinkLoc); 20496ddfca91SDouglas Gregor HadError = true; 20506ddfca91SDouglas Gregor return; 20516ddfca91SDouglas Gregor } 20526ddfca91SDouglas Gregor 20536ddfca91SDouglas Gregor std::string LibraryName = Tok.getString(); 20546ddfca91SDouglas Gregor consumeToken(); 20556ddfca91SDouglas Gregor ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, 20566ddfca91SDouglas Gregor IsFramework)); 20576ddfca91SDouglas Gregor } 20586ddfca91SDouglas Gregor 205935b13eceSDouglas Gregor /// \brief Parse a configuration macro declaration. 206035b13eceSDouglas Gregor /// 206135b13eceSDouglas Gregor /// module-declaration: 206235b13eceSDouglas Gregor /// 'config_macros' attributes[opt] config-macro-list? 206335b13eceSDouglas Gregor /// 206435b13eceSDouglas Gregor /// config-macro-list: 206535b13eceSDouglas Gregor /// identifier (',' identifier)? 206635b13eceSDouglas Gregor void ModuleMapParser::parseConfigMacros() { 206735b13eceSDouglas Gregor assert(Tok.is(MMToken::ConfigMacros)); 206835b13eceSDouglas Gregor SourceLocation ConfigMacrosLoc = consumeToken(); 206935b13eceSDouglas Gregor 207035b13eceSDouglas Gregor // Only top-level modules can have configuration macros. 207135b13eceSDouglas Gregor if (ActiveModule->Parent) { 207235b13eceSDouglas Gregor Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule); 207335b13eceSDouglas Gregor } 207435b13eceSDouglas Gregor 207535b13eceSDouglas Gregor // Parse the optional attributes. 207635b13eceSDouglas Gregor Attributes Attrs; 207735b13eceSDouglas Gregor parseOptionalAttributes(Attrs); 207835b13eceSDouglas Gregor if (Attrs.IsExhaustive && !ActiveModule->Parent) { 207935b13eceSDouglas Gregor ActiveModule->ConfigMacrosExhaustive = true; 208035b13eceSDouglas Gregor } 208135b13eceSDouglas Gregor 208235b13eceSDouglas Gregor // If we don't have an identifier, we're done. 2083306d8920SRichard Smith // FIXME: Support macros with the same name as a keyword here. 208435b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) 208535b13eceSDouglas Gregor return; 208635b13eceSDouglas Gregor 208735b13eceSDouglas Gregor // Consume the first identifier. 208835b13eceSDouglas Gregor if (!ActiveModule->Parent) { 208935b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 209035b13eceSDouglas Gregor } 209135b13eceSDouglas Gregor consumeToken(); 209235b13eceSDouglas Gregor 209335b13eceSDouglas Gregor do { 209435b13eceSDouglas Gregor // If there's a comma, consume it. 209535b13eceSDouglas Gregor if (!Tok.is(MMToken::Comma)) 209635b13eceSDouglas Gregor break; 209735b13eceSDouglas Gregor consumeToken(); 209835b13eceSDouglas Gregor 209935b13eceSDouglas Gregor // We expect to see a macro name here. 2100306d8920SRichard Smith // FIXME: Support macros with the same name as a keyword here. 210135b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 210235b13eceSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro); 210335b13eceSDouglas Gregor break; 210435b13eceSDouglas Gregor } 210535b13eceSDouglas Gregor 210635b13eceSDouglas Gregor // Consume the macro name. 210735b13eceSDouglas Gregor if (!ActiveModule->Parent) { 210835b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 210935b13eceSDouglas Gregor } 211035b13eceSDouglas Gregor consumeToken(); 211135b13eceSDouglas Gregor } while (true); 211235b13eceSDouglas Gregor } 211335b13eceSDouglas Gregor 2114fb912657SDouglas Gregor /// \brief Format a module-id into a string. 2115fb912657SDouglas Gregor static std::string formatModuleId(const ModuleId &Id) { 2116fb912657SDouglas Gregor std::string result; 2117fb912657SDouglas Gregor { 2118fb912657SDouglas Gregor llvm::raw_string_ostream OS(result); 2119fb912657SDouglas Gregor 2120fb912657SDouglas Gregor for (unsigned I = 0, N = Id.size(); I != N; ++I) { 2121fb912657SDouglas Gregor if (I) 2122fb912657SDouglas Gregor OS << "."; 2123fb912657SDouglas Gregor OS << Id[I].first; 2124fb912657SDouglas Gregor } 2125fb912657SDouglas Gregor } 2126fb912657SDouglas Gregor 2127fb912657SDouglas Gregor return result; 2128fb912657SDouglas Gregor } 2129fb912657SDouglas Gregor 2130fb912657SDouglas Gregor /// \brief Parse a conflict declaration. 2131fb912657SDouglas Gregor /// 2132fb912657SDouglas Gregor /// module-declaration: 2133fb912657SDouglas Gregor /// 'conflict' module-id ',' string-literal 2134fb912657SDouglas Gregor void ModuleMapParser::parseConflict() { 2135fb912657SDouglas Gregor assert(Tok.is(MMToken::Conflict)); 2136fb912657SDouglas Gregor SourceLocation ConflictLoc = consumeToken(); 2137fb912657SDouglas Gregor Module::UnresolvedConflict Conflict; 2138fb912657SDouglas Gregor 2139fb912657SDouglas Gregor // Parse the module-id. 2140fb912657SDouglas Gregor if (parseModuleId(Conflict.Id)) 2141fb912657SDouglas Gregor return; 2142fb912657SDouglas Gregor 2143fb912657SDouglas Gregor // Parse the ','. 2144fb912657SDouglas Gregor if (!Tok.is(MMToken::Comma)) { 2145fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma) 2146fb912657SDouglas Gregor << SourceRange(ConflictLoc); 2147fb912657SDouglas Gregor return; 2148fb912657SDouglas Gregor } 2149fb912657SDouglas Gregor consumeToken(); 2150fb912657SDouglas Gregor 2151fb912657SDouglas Gregor // Parse the message. 2152fb912657SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 2153fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message) 2154fb912657SDouglas Gregor << formatModuleId(Conflict.Id); 2155fb912657SDouglas Gregor return; 2156fb912657SDouglas Gregor } 2157fb912657SDouglas Gregor Conflict.Message = Tok.getString().str(); 2158fb912657SDouglas Gregor consumeToken(); 2159fb912657SDouglas Gregor 2160fb912657SDouglas Gregor // Add this unresolved conflict. 2161fb912657SDouglas Gregor ActiveModule->UnresolvedConflicts.push_back(Conflict); 2162fb912657SDouglas Gregor } 2163fb912657SDouglas Gregor 21646ddfca91SDouglas Gregor /// \brief Parse an inferred module declaration (wildcard modules). 21659194a91dSDouglas Gregor /// 21669194a91dSDouglas Gregor /// module-declaration: 21679194a91dSDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] 21689194a91dSDouglas Gregor /// { inferred-module-member* } 21699194a91dSDouglas Gregor /// 21709194a91dSDouglas Gregor /// inferred-module-member: 21719194a91dSDouglas Gregor /// 'export' '*' 21729194a91dSDouglas Gregor /// 'exclude' identifier 21739194a91dSDouglas Gregor void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { 217473441091SDouglas Gregor assert(Tok.is(MMToken::Star)); 217573441091SDouglas Gregor SourceLocation StarLoc = consumeToken(); 217673441091SDouglas Gregor bool Failed = false; 217773441091SDouglas Gregor 217873441091SDouglas Gregor // Inferred modules must be submodules. 21799194a91dSDouglas Gregor if (!ActiveModule && !Framework) { 218073441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 218173441091SDouglas Gregor Failed = true; 218273441091SDouglas Gregor } 218373441091SDouglas Gregor 21849194a91dSDouglas Gregor if (ActiveModule) { 2185524e33e1SDouglas Gregor // Inferred modules must have umbrella directories. 21864898cde4SBen Langmuir if (!Failed && ActiveModule->IsAvailable && 21874898cde4SBen Langmuir !ActiveModule->getUmbrellaDir()) { 218873441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 218973441091SDouglas Gregor Failed = true; 219073441091SDouglas Gregor } 219173441091SDouglas Gregor 219273441091SDouglas Gregor // Check for redefinition of an inferred module. 2193dd005f69SDouglas Gregor if (!Failed && ActiveModule->InferSubmodules) { 219473441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 2195dd005f69SDouglas Gregor if (ActiveModule->InferredSubmoduleLoc.isValid()) 2196dd005f69SDouglas Gregor Diags.Report(ActiveModule->InferredSubmoduleLoc, 219773441091SDouglas Gregor diag::note_mmap_prev_definition); 219873441091SDouglas Gregor Failed = true; 219973441091SDouglas Gregor } 220073441091SDouglas Gregor 22019194a91dSDouglas Gregor // Check for the 'framework' keyword, which is not permitted here. 22029194a91dSDouglas Gregor if (Framework) { 22039194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); 22049194a91dSDouglas Gregor Framework = false; 22059194a91dSDouglas Gregor } 22069194a91dSDouglas Gregor } else if (Explicit) { 22079194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); 22089194a91dSDouglas Gregor Explicit = false; 22099194a91dSDouglas Gregor } 22109194a91dSDouglas Gregor 221173441091SDouglas Gregor // If there were any problems with this inferred submodule, skip its body. 221273441091SDouglas Gregor if (Failed) { 221373441091SDouglas Gregor if (Tok.is(MMToken::LBrace)) { 221473441091SDouglas Gregor consumeToken(); 221573441091SDouglas Gregor skipUntil(MMToken::RBrace); 221673441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 221773441091SDouglas Gregor consumeToken(); 221873441091SDouglas Gregor } 221973441091SDouglas Gregor HadError = true; 222073441091SDouglas Gregor return; 222173441091SDouglas Gregor } 222273441091SDouglas Gregor 22239194a91dSDouglas Gregor // Parse optional attributes. 22244442605fSBill Wendling Attributes Attrs; 22259194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 22269194a91dSDouglas Gregor 22279194a91dSDouglas Gregor if (ActiveModule) { 222873441091SDouglas Gregor // Note that we have an inferred submodule. 2229dd005f69SDouglas Gregor ActiveModule->InferSubmodules = true; 2230dd005f69SDouglas Gregor ActiveModule->InferredSubmoduleLoc = StarLoc; 2231dd005f69SDouglas Gregor ActiveModule->InferExplicitSubmodules = Explicit; 22329194a91dSDouglas Gregor } else { 22339194a91dSDouglas Gregor // We'll be inferring framework modules for this directory. 22349194a91dSDouglas Gregor Map.InferredDirectories[Directory].InferModules = true; 2235c1d88ea5SBen Langmuir Map.InferredDirectories[Directory].Attrs = Attrs; 2236beee15e7SBen Langmuir Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile; 2237131daca0SRichard Smith // FIXME: Handle the 'framework' keyword. 22389194a91dSDouglas Gregor } 223973441091SDouglas Gregor 224073441091SDouglas Gregor // Parse the opening brace. 224173441091SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 224273441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 224373441091SDouglas Gregor HadError = true; 224473441091SDouglas Gregor return; 224573441091SDouglas Gregor } 224673441091SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 224773441091SDouglas Gregor 224873441091SDouglas Gregor // Parse the body of the inferred submodule. 224973441091SDouglas Gregor bool Done = false; 225073441091SDouglas Gregor do { 225173441091SDouglas Gregor switch (Tok.Kind) { 225273441091SDouglas Gregor case MMToken::EndOfFile: 225373441091SDouglas Gregor case MMToken::RBrace: 225473441091SDouglas Gregor Done = true; 225573441091SDouglas Gregor break; 225673441091SDouglas Gregor 22579194a91dSDouglas Gregor case MMToken::ExcludeKeyword: { 22589194a91dSDouglas Gregor if (ActiveModule) { 22599194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2260d2d442caSCraig Topper << (ActiveModule != nullptr); 22619194a91dSDouglas Gregor consumeToken(); 22629194a91dSDouglas Gregor break; 22639194a91dSDouglas Gregor } 22649194a91dSDouglas Gregor 22659194a91dSDouglas Gregor consumeToken(); 2266306d8920SRichard Smith // FIXME: Support string-literal module names here. 22679194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 22689194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); 22699194a91dSDouglas Gregor break; 22709194a91dSDouglas Gregor } 22719194a91dSDouglas Gregor 22729194a91dSDouglas Gregor Map.InferredDirectories[Directory].ExcludedModules 22739194a91dSDouglas Gregor .push_back(Tok.getString()); 22749194a91dSDouglas Gregor consumeToken(); 22759194a91dSDouglas Gregor break; 22769194a91dSDouglas Gregor } 22779194a91dSDouglas Gregor 22789194a91dSDouglas Gregor case MMToken::ExportKeyword: 22799194a91dSDouglas Gregor if (!ActiveModule) { 22809194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2281d2d442caSCraig Topper << (ActiveModule != nullptr); 22829194a91dSDouglas Gregor consumeToken(); 22839194a91dSDouglas Gregor break; 22849194a91dSDouglas Gregor } 22859194a91dSDouglas Gregor 228673441091SDouglas Gregor consumeToken(); 228773441091SDouglas Gregor if (Tok.is(MMToken::Star)) 2288dd005f69SDouglas Gregor ActiveModule->InferExportWildcard = true; 228973441091SDouglas Gregor else 229073441091SDouglas Gregor Diags.Report(Tok.getLocation(), 229173441091SDouglas Gregor diag::err_mmap_expected_export_wildcard); 229273441091SDouglas Gregor consumeToken(); 229373441091SDouglas Gregor break; 229473441091SDouglas Gregor 229573441091SDouglas Gregor case MMToken::ExplicitKeyword: 229673441091SDouglas Gregor case MMToken::ModuleKeyword: 229773441091SDouglas Gregor case MMToken::HeaderKeyword: 2298b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 229973441091SDouglas Gregor case MMToken::UmbrellaKeyword: 230073441091SDouglas Gregor default: 23019194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2302d2d442caSCraig Topper << (ActiveModule != nullptr); 230373441091SDouglas Gregor consumeToken(); 230473441091SDouglas Gregor break; 230573441091SDouglas Gregor } 230673441091SDouglas Gregor } while (!Done); 230773441091SDouglas Gregor 230873441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 230973441091SDouglas Gregor consumeToken(); 231073441091SDouglas Gregor else { 231173441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 231273441091SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 231373441091SDouglas Gregor HadError = true; 231473441091SDouglas Gregor } 231573441091SDouglas Gregor } 231673441091SDouglas Gregor 23179194a91dSDouglas Gregor /// \brief Parse optional attributes. 23189194a91dSDouglas Gregor /// 23199194a91dSDouglas Gregor /// attributes: 23209194a91dSDouglas Gregor /// attribute attributes 23219194a91dSDouglas Gregor /// attribute 23229194a91dSDouglas Gregor /// 23239194a91dSDouglas Gregor /// attribute: 23249194a91dSDouglas Gregor /// [ identifier ] 23259194a91dSDouglas Gregor /// 23269194a91dSDouglas Gregor /// \param Attrs Will be filled in with the parsed attributes. 23279194a91dSDouglas Gregor /// 23289194a91dSDouglas Gregor /// \returns true if an error occurred, false otherwise. 23294442605fSBill Wendling bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { 23309194a91dSDouglas Gregor bool HadError = false; 23319194a91dSDouglas Gregor 23329194a91dSDouglas Gregor while (Tok.is(MMToken::LSquare)) { 23339194a91dSDouglas Gregor // Consume the '['. 23349194a91dSDouglas Gregor SourceLocation LSquareLoc = consumeToken(); 23359194a91dSDouglas Gregor 23369194a91dSDouglas Gregor // Check whether we have an attribute name here. 23379194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 23389194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 23399194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 23409194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 23419194a91dSDouglas Gregor consumeToken(); 23429194a91dSDouglas Gregor HadError = true; 23439194a91dSDouglas Gregor } 23449194a91dSDouglas Gregor 23459194a91dSDouglas Gregor // Decode the attribute name. 23469194a91dSDouglas Gregor AttributeKind Attribute 23479194a91dSDouglas Gregor = llvm::StringSwitch<AttributeKind>(Tok.getString()) 234835b13eceSDouglas Gregor .Case("exhaustive", AT_exhaustive) 234977944868SRichard Smith .Case("extern_c", AT_extern_c) 23509194a91dSDouglas Gregor .Case("system", AT_system) 23519194a91dSDouglas Gregor .Default(AT_unknown); 23529194a91dSDouglas Gregor switch (Attribute) { 23539194a91dSDouglas Gregor case AT_unknown: 23549194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 23559194a91dSDouglas Gregor << Tok.getString(); 23569194a91dSDouglas Gregor break; 23579194a91dSDouglas Gregor 23589194a91dSDouglas Gregor case AT_system: 23599194a91dSDouglas Gregor Attrs.IsSystem = true; 23609194a91dSDouglas Gregor break; 236135b13eceSDouglas Gregor 236277944868SRichard Smith case AT_extern_c: 236377944868SRichard Smith Attrs.IsExternC = true; 236477944868SRichard Smith break; 236577944868SRichard Smith 236635b13eceSDouglas Gregor case AT_exhaustive: 236735b13eceSDouglas Gregor Attrs.IsExhaustive = true; 236835b13eceSDouglas Gregor break; 23699194a91dSDouglas Gregor } 23709194a91dSDouglas Gregor consumeToken(); 23719194a91dSDouglas Gregor 23729194a91dSDouglas Gregor // Consume the ']'. 23739194a91dSDouglas Gregor if (!Tok.is(MMToken::RSquare)) { 23749194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 23759194a91dSDouglas Gregor Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 23769194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 23779194a91dSDouglas Gregor HadError = true; 23789194a91dSDouglas Gregor } 23799194a91dSDouglas Gregor 23809194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 23819194a91dSDouglas Gregor consumeToken(); 23829194a91dSDouglas Gregor } 23839194a91dSDouglas Gregor 23849194a91dSDouglas Gregor return HadError; 23859194a91dSDouglas Gregor } 23869194a91dSDouglas Gregor 2387718292f2SDouglas Gregor /// \brief Parse a module map file. 2388718292f2SDouglas Gregor /// 2389718292f2SDouglas Gregor /// module-map-file: 2390718292f2SDouglas Gregor /// module-declaration* 2391718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() { 2392718292f2SDouglas Gregor do { 2393718292f2SDouglas Gregor switch (Tok.Kind) { 2394718292f2SDouglas Gregor case MMToken::EndOfFile: 2395718292f2SDouglas Gregor return HadError; 2396718292f2SDouglas Gregor 2397e7ab3669SDouglas Gregor case MMToken::ExplicitKeyword: 239897292843SDaniel Jasper case MMToken::ExternKeyword: 2399718292f2SDouglas Gregor case MMToken::ModuleKeyword: 2400755b2055SDouglas Gregor case MMToken::FrameworkKeyword: 2401718292f2SDouglas Gregor parseModuleDecl(); 2402718292f2SDouglas Gregor break; 2403718292f2SDouglas Gregor 24041fb5c3a6SDouglas Gregor case MMToken::Comma: 240535b13eceSDouglas Gregor case MMToken::ConfigMacros: 2406fb912657SDouglas Gregor case MMToken::Conflict: 2407a3feee2aSRichard Smith case MMToken::Exclaim: 240859527666SDouglas Gregor case MMToken::ExcludeKeyword: 24092b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 2410718292f2SDouglas Gregor case MMToken::HeaderKeyword: 2411718292f2SDouglas Gregor case MMToken::Identifier: 2412718292f2SDouglas Gregor case MMToken::LBrace: 24136ddfca91SDouglas Gregor case MMToken::LinkKeyword: 2414a686e1b0SDouglas Gregor case MMToken::LSquare: 24152b82c2a5SDouglas Gregor case MMToken::Period: 2416b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 2417718292f2SDouglas Gregor case MMToken::RBrace: 2418a686e1b0SDouglas Gregor case MMToken::RSquare: 24191fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 24202b82c2a5SDouglas Gregor case MMToken::Star: 2421718292f2SDouglas Gregor case MMToken::StringLiteral: 2422b8afebe2SRichard Smith case MMToken::TextualKeyword: 2423718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 2424ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 2425718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 2426718292f2SDouglas Gregor HadError = true; 2427718292f2SDouglas Gregor consumeToken(); 2428718292f2SDouglas Gregor break; 2429718292f2SDouglas Gregor } 2430718292f2SDouglas Gregor } while (true); 2431718292f2SDouglas Gregor } 2432718292f2SDouglas Gregor 24339acb99e3SRichard Smith bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem, 2434ae6df27eSRichard Smith const DirectoryEntry *Dir, 2435ae6df27eSRichard Smith SourceLocation ExternModuleLoc) { 24364ddf2221SDouglas Gregor llvm::DenseMap<const FileEntry *, bool>::iterator Known 24374ddf2221SDouglas Gregor = ParsedModuleMap.find(File); 24384ddf2221SDouglas Gregor if (Known != ParsedModuleMap.end()) 24394ddf2221SDouglas Gregor return Known->second; 24404ddf2221SDouglas Gregor 2441d2d442caSCraig Topper assert(Target && "Missing target information"); 2442cb69b57bSBen Langmuir auto FileCharacter = IsSystem ? SrcMgr::C_System : SrcMgr::C_User; 2443ae6df27eSRichard Smith FileID ID = SourceMgr.createFileID(File, ExternModuleLoc, FileCharacter); 24441f76c4e8SManuel Klimek const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID); 2445718292f2SDouglas Gregor if (!Buffer) 24464ddf2221SDouglas Gregor return ParsedModuleMap[File] = true; 2447718292f2SDouglas Gregor 2448718292f2SDouglas Gregor // Parse this module map file. 24491f76c4e8SManuel Klimek Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts); 24502a6edb30SRichard Smith SourceLocation Start = L.getSourceLocation(); 2451beee15e7SBen Langmuir ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir, 2452963c5535SDouglas Gregor BuiltinIncludeDir, IsSystem); 2453718292f2SDouglas Gregor bool Result = Parser.parseModuleMapFile(); 24544ddf2221SDouglas Gregor ParsedModuleMap[File] = Result; 24552a6edb30SRichard Smith 24562a6edb30SRichard Smith // Notify callbacks that we parsed it. 24572a6edb30SRichard Smith for (const auto &Cb : Callbacks) 24582a6edb30SRichard Smith Cb->moduleMapFileRead(Start, *File, IsSystem); 2459718292f2SDouglas Gregor return Result; 2460718292f2SDouglas Gregor } 2461