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), 92d2d442caSCraig Topper CompilingModule(nullptr), SourceModule(nullptr) {} 93718292f2SDouglas Gregor 94718292f2SDouglas Gregor ModuleMap::~ModuleMap() { 955acdf59eSDouglas Gregor for (llvm::StringMap<Module *>::iterator I = Modules.begin(), 965acdf59eSDouglas Gregor IEnd = Modules.end(); 975acdf59eSDouglas Gregor I != IEnd; ++I) { 985acdf59eSDouglas Gregor delete I->getValue(); 995acdf59eSDouglas Gregor } 100718292f2SDouglas Gregor } 101718292f2SDouglas Gregor 10289929282SDouglas Gregor void ModuleMap::setTarget(const TargetInfo &Target) { 10389929282SDouglas Gregor assert((!this->Target || this->Target == &Target) && 10489929282SDouglas Gregor "Improper target override"); 10589929282SDouglas Gregor this->Target = &Target; 10689929282SDouglas Gregor } 10789929282SDouglas Gregor 108056396aeSDouglas Gregor /// \brief "Sanitize" a filename so that it can be used as an identifier. 109056396aeSDouglas Gregor static StringRef sanitizeFilenameAsIdentifier(StringRef Name, 110056396aeSDouglas Gregor SmallVectorImpl<char> &Buffer) { 111056396aeSDouglas Gregor if (Name.empty()) 112056396aeSDouglas Gregor return Name; 113056396aeSDouglas Gregor 114a7d03840SJordan Rose if (!isValidIdentifier(Name)) { 115056396aeSDouglas Gregor // If we don't already have something with the form of an identifier, 116056396aeSDouglas Gregor // create a buffer with the sanitized name. 117056396aeSDouglas Gregor Buffer.clear(); 118a7d03840SJordan Rose if (isDigit(Name[0])) 119056396aeSDouglas Gregor Buffer.push_back('_'); 120056396aeSDouglas Gregor Buffer.reserve(Buffer.size() + Name.size()); 121056396aeSDouglas Gregor for (unsigned I = 0, N = Name.size(); I != N; ++I) { 122a7d03840SJordan Rose if (isIdentifierBody(Name[I])) 123056396aeSDouglas Gregor Buffer.push_back(Name[I]); 124056396aeSDouglas Gregor else 125056396aeSDouglas Gregor Buffer.push_back('_'); 126056396aeSDouglas Gregor } 127056396aeSDouglas Gregor 128056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 129056396aeSDouglas Gregor } 130056396aeSDouglas Gregor 131056396aeSDouglas Gregor while (llvm::StringSwitch<bool>(Name) 132056396aeSDouglas Gregor #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true) 133056396aeSDouglas Gregor #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true) 134056396aeSDouglas Gregor #include "clang/Basic/TokenKinds.def" 135056396aeSDouglas Gregor .Default(false)) { 136056396aeSDouglas Gregor if (Name.data() != Buffer.data()) 137056396aeSDouglas Gregor Buffer.append(Name.begin(), Name.end()); 138056396aeSDouglas Gregor Buffer.push_back('_'); 139056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 140056396aeSDouglas Gregor } 141056396aeSDouglas Gregor 142056396aeSDouglas Gregor return Name; 143056396aeSDouglas Gregor } 144056396aeSDouglas Gregor 14534d52749SDouglas Gregor /// \brief Determine whether the given file name is the name of a builtin 14634d52749SDouglas Gregor /// header, supplied by Clang to replace, override, or augment existing system 14734d52749SDouglas Gregor /// headers. 14834d52749SDouglas Gregor static bool isBuiltinHeader(StringRef FileName) { 14934d52749SDouglas Gregor return llvm::StringSwitch<bool>(FileName) 15034d52749SDouglas Gregor .Case("float.h", true) 15134d52749SDouglas Gregor .Case("iso646.h", true) 15234d52749SDouglas Gregor .Case("limits.h", true) 15334d52749SDouglas Gregor .Case("stdalign.h", true) 15434d52749SDouglas Gregor .Case("stdarg.h", true) 15534d52749SDouglas Gregor .Case("stdbool.h", true) 15634d52749SDouglas Gregor .Case("stddef.h", true) 15734d52749SDouglas Gregor .Case("stdint.h", true) 15834d52749SDouglas Gregor .Case("tgmath.h", true) 15934d52749SDouglas Gregor .Case("unwind.h", true) 16034d52749SDouglas Gregor .Default(false); 16134d52749SDouglas Gregor } 16234d52749SDouglas Gregor 16392669ee4SDaniel Jasper ModuleMap::HeadersMap::iterator 16492669ee4SDaniel Jasper ModuleMap::findKnownHeader(const FileEntry *File) { 16559527666SDouglas Gregor HeadersMap::iterator Known = Headers.find(File); 1664eaf0a6cSDaniel Jasper if (Known == Headers.end() && File->getDir() == BuiltinIncludeDir && 1674eaf0a6cSDaniel Jasper isBuiltinHeader(llvm::sys::path::filename(File->getName()))) { 1684eaf0a6cSDaniel Jasper HeaderInfo.loadTopLevelSystemModules(); 16992669ee4SDaniel Jasper return Headers.find(File); 1704eaf0a6cSDaniel Jasper } 17192669ee4SDaniel Jasper return Known; 17292669ee4SDaniel Jasper } 17392669ee4SDaniel Jasper 1744469138eSBen Langmuir ModuleMap::KnownHeader 1754469138eSBen Langmuir ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File, 1764469138eSBen Langmuir SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs) { 1774469138eSBen Langmuir const DirectoryEntry *Dir = File->getDir(); 1784469138eSBen Langmuir assert(Dir && "file in no directory"); 1794469138eSBen Langmuir 1804469138eSBen Langmuir // Note: as an egregious but useful hack we use the real path here, because 1814469138eSBen Langmuir // frameworks moving from top-level frameworks to embedded frameworks tend 1824469138eSBen Langmuir // to be symlinked from the top-level location to the embedded location, 1834469138eSBen Langmuir // and we need to resolve lookups as if we had found the embedded location. 1844469138eSBen Langmuir StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir); 1854469138eSBen Langmuir 1864469138eSBen Langmuir // Keep walking up the directory hierarchy, looking for a directory with 1874469138eSBen Langmuir // an umbrella header. 1884469138eSBen Langmuir do { 1894469138eSBen Langmuir auto KnownDir = UmbrellaDirs.find(Dir); 1904469138eSBen Langmuir if (KnownDir != UmbrellaDirs.end()) 1914469138eSBen Langmuir return KnownHeader(KnownDir->second, NormalHeader); 1924469138eSBen Langmuir 1934469138eSBen Langmuir IntermediateDirs.push_back(Dir); 1944469138eSBen Langmuir 1954469138eSBen Langmuir // Retrieve our parent path. 1964469138eSBen Langmuir DirName = llvm::sys::path::parent_path(DirName); 1974469138eSBen Langmuir if (DirName.empty()) 1984469138eSBen Langmuir break; 1994469138eSBen Langmuir 2004469138eSBen Langmuir // Resolve the parent path to a directory entry. 2014469138eSBen Langmuir Dir = SourceMgr.getFileManager().getDirectory(DirName); 2024469138eSBen Langmuir } while (Dir); 2034469138eSBen Langmuir return KnownHeader(); 2044469138eSBen Langmuir } 2054469138eSBen Langmuir 206202210b3SRichard Smith // Returns true if RequestingModule directly uses RequestedModule. 20792669ee4SDaniel Jasper static bool directlyUses(const Module *RequestingModule, 20892669ee4SDaniel Jasper const Module *RequestedModule) { 20992669ee4SDaniel Jasper return std::find(RequestingModule->DirectUses.begin(), 21092669ee4SDaniel Jasper RequestingModule->DirectUses.end(), 21192669ee4SDaniel Jasper RequestedModule) != RequestingModule->DirectUses.end(); 21292669ee4SDaniel Jasper } 21392669ee4SDaniel Jasper 21492669ee4SDaniel Jasper static bool violatesPrivateInclude(Module *RequestingModule, 21592669ee4SDaniel Jasper const FileEntry *IncFileEnt, 21692669ee4SDaniel Jasper ModuleMap::ModuleHeaderRole Role, 21792669ee4SDaniel Jasper Module *RequestedModule) { 218202210b3SRichard Smith bool IsPrivateRole = Role & ModuleMap::PrivateHeader; 21992669ee4SDaniel Jasper #ifndef NDEBUG 22092669ee4SDaniel Jasper // Check for consistency between the module header role 22192669ee4SDaniel Jasper // as obtained from the lookup and as obtained from the module. 22292669ee4SDaniel Jasper // This check is not cheap, so enable it only for debugging. 223202210b3SRichard Smith bool IsPrivate = false; 2243c1a41adSRichard Smith SmallVectorImpl<Module::Header> *HeaderList[] = 2253c1a41adSRichard Smith {&RequestedModule->Headers[Module::HK_Private], 2263c1a41adSRichard Smith &RequestedModule->Headers[Module::HK_PrivateTextual]}; 2270ef0aecaSHans Wennborg for (auto *Hdrs : HeaderList) 228202210b3SRichard Smith IsPrivate |= 2293c1a41adSRichard Smith std::find_if(Hdrs->begin(), Hdrs->end(), [&](const Module::Header &H) { 2303c1a41adSRichard Smith return H.Entry == IncFileEnt; 2313c1a41adSRichard Smith }) != Hdrs->end(); 232202210b3SRichard Smith assert(IsPrivate == IsPrivateRole && "inconsistent headers and roles"); 23392669ee4SDaniel Jasper #endif 234202210b3SRichard Smith return IsPrivateRole && 23592669ee4SDaniel Jasper RequestedModule->getTopLevelModule() != RequestingModule; 23692669ee4SDaniel Jasper } 23792669ee4SDaniel Jasper 23871e1a64fSBen Langmuir static Module *getTopLevelOrNull(Module *M) { 23971e1a64fSBen Langmuir return M ? M->getTopLevelModule() : nullptr; 24071e1a64fSBen Langmuir } 24171e1a64fSBen Langmuir 24292669ee4SDaniel Jasper void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule, 24392669ee4SDaniel Jasper SourceLocation FilenameLoc, 24492669ee4SDaniel Jasper StringRef Filename, 24592669ee4SDaniel Jasper const FileEntry *File) { 24692669ee4SDaniel Jasper // No errors for indirect modules. This may be a bit of a problem for modules 24792669ee4SDaniel Jasper // with no source files. 24871e1a64fSBen Langmuir if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule)) 24992669ee4SDaniel Jasper return; 25092669ee4SDaniel Jasper 25192669ee4SDaniel Jasper if (RequestingModule) 25292669ee4SDaniel Jasper resolveUses(RequestingModule, /*Complain=*/false); 25392669ee4SDaniel Jasper 25471e1a64fSBen Langmuir bool Excluded = false; 255d2d442caSCraig Topper Module *Private = nullptr; 256d2d442caSCraig Topper Module *NotUsed = nullptr; 25771e1a64fSBen Langmuir 25871e1a64fSBen Langmuir HeadersMap::iterator Known = findKnownHeader(File); 25971e1a64fSBen Langmuir if (Known != Headers.end()) { 26071e1a64fSBen Langmuir for (const KnownHeader &Header : Known->second) { 26192669ee4SDaniel Jasper // If 'File' is part of 'RequestingModule' we can definitely include it. 26271e1a64fSBen Langmuir if (Header.getModule() == RequestingModule) 26392669ee4SDaniel Jasper return; 26492669ee4SDaniel Jasper 26592669ee4SDaniel Jasper // Remember private headers for later printing of a diagnostic. 26671e1a64fSBen Langmuir if (violatesPrivateInclude(RequestingModule, File, Header.getRole(), 26771e1a64fSBen Langmuir Header.getModule())) { 26871e1a64fSBen Langmuir Private = Header.getModule(); 26992669ee4SDaniel Jasper continue; 27092669ee4SDaniel Jasper } 27192669ee4SDaniel Jasper 27292669ee4SDaniel Jasper // If uses need to be specified explicitly, we are only allowed to return 27392669ee4SDaniel Jasper // modules that are explicitly used by the requesting module. 27492669ee4SDaniel Jasper if (RequestingModule && LangOpts.ModulesDeclUse && 27571e1a64fSBen Langmuir !directlyUses(RequestingModule, Header.getModule())) { 27671e1a64fSBen Langmuir NotUsed = Header.getModule(); 27792669ee4SDaniel Jasper continue; 27892669ee4SDaniel Jasper } 27992669ee4SDaniel Jasper 28092669ee4SDaniel Jasper // We have found a module that we can happily use. 28192669ee4SDaniel Jasper return; 28292669ee4SDaniel Jasper } 283feb54b6dSRichard Smith 284feb54b6dSRichard Smith Excluded = true; 28571e1a64fSBen Langmuir } 28692669ee4SDaniel Jasper 28792669ee4SDaniel Jasper // We have found a header, but it is private. 288d2d442caSCraig Topper if (Private) { 28992669ee4SDaniel Jasper Diags.Report(FilenameLoc, diag::error_use_of_private_header_outside_module) 29092669ee4SDaniel Jasper << Filename; 29192669ee4SDaniel Jasper return; 29292669ee4SDaniel Jasper } 29392669ee4SDaniel Jasper 29492669ee4SDaniel Jasper // We have found a module, but we don't use it. 295d2d442caSCraig Topper if (NotUsed) { 29692669ee4SDaniel Jasper Diags.Report(FilenameLoc, diag::error_undeclared_use_of_module) 29792669ee4SDaniel Jasper << RequestingModule->getFullModuleName() << Filename; 29892669ee4SDaniel Jasper return; 29992669ee4SDaniel Jasper } 30092669ee4SDaniel Jasper 30171e1a64fSBen Langmuir if (Excluded || isHeaderInUmbrellaDirs(File)) 30271e1a64fSBen Langmuir return; 30371e1a64fSBen Langmuir 30471e1a64fSBen Langmuir // At this point, only non-modular includes remain. 30571e1a64fSBen Langmuir 30671e1a64fSBen Langmuir if (LangOpts.ModulesStrictDeclUse) { 30771e1a64fSBen Langmuir Diags.Report(FilenameLoc, diag::error_undeclared_use_of_module) 30871e1a64fSBen Langmuir << RequestingModule->getFullModuleName() << Filename; 30971e1a64fSBen Langmuir } else if (RequestingModule) { 31071e1a64fSBen Langmuir diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ? 31171e1a64fSBen Langmuir diag::warn_non_modular_include_in_framework_module : 31271e1a64fSBen Langmuir diag::warn_non_modular_include_in_module; 31371e1a64fSBen Langmuir Diags.Report(FilenameLoc, DiagID) << RequestingModule->getFullModuleName(); 31471e1a64fSBen Langmuir } 31592669ee4SDaniel Jasper } 31692669ee4SDaniel Jasper 31792669ee4SDaniel Jasper ModuleMap::KnownHeader 31892669ee4SDaniel Jasper ModuleMap::findModuleForHeader(const FileEntry *File, 319306d8920SRichard Smith Module *RequestingModule, 320306d8920SRichard Smith bool IncludeTextualHeaders) { 32192669ee4SDaniel Jasper HeadersMap::iterator Known = findKnownHeader(File); 3224eaf0a6cSDaniel Jasper 323306d8920SRichard Smith auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader { 324202210b3SRichard Smith if (!IncludeTextualHeaders && (R.getRole() & ModuleMap::TextualHeader)) 325306d8920SRichard Smith return ModuleMap::KnownHeader(); 326306d8920SRichard Smith return R; 327306d8920SRichard Smith }; 328306d8920SRichard Smith 3291fb5c3a6SDouglas Gregor if (Known != Headers.end()) { 330202210b3SRichard Smith ModuleMap::KnownHeader Result; 3311fb5c3a6SDouglas Gregor 33297da9178SDaniel Jasper // Iterate over all modules that 'File' is part of to find the best fit. 33397da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::iterator I = Known->second.begin(), 33497da9178SDaniel Jasper E = Known->second.end(); 33597da9178SDaniel Jasper I != E; ++I) { 3364eaf0a6cSDaniel Jasper // Cannot use a module if it is unavailable. 3374eaf0a6cSDaniel Jasper if (!I->getModule()->isAvailable()) 33897da9178SDaniel Jasper continue; 33997da9178SDaniel Jasper 34097da9178SDaniel Jasper // If 'File' is part of 'RequestingModule', 'RequestingModule' is the 34197da9178SDaniel Jasper // module we are looking for. 34297da9178SDaniel Jasper if (I->getModule() == RequestingModule) 343306d8920SRichard Smith return MakeResult(*I); 34497da9178SDaniel Jasper 34597da9178SDaniel Jasper // If uses need to be specified explicitly, we are only allowed to return 34697da9178SDaniel Jasper // modules that are explicitly used by the requesting module. 34797da9178SDaniel Jasper if (RequestingModule && LangOpts.ModulesDeclUse && 34892669ee4SDaniel Jasper !directlyUses(RequestingModule, I->getModule())) 34997da9178SDaniel Jasper continue; 3504eaf0a6cSDaniel Jasper 351202210b3SRichard Smith // Prefer a public header over a private header. 352202210b3SRichard Smith if (!Result || (Result.getRole() & ModuleMap::PrivateHeader)) 35397da9178SDaniel Jasper Result = *I; 35497da9178SDaniel Jasper } 355306d8920SRichard Smith return MakeResult(Result); 3561fb5c3a6SDouglas Gregor } 357ab0c8a84SDouglas Gregor 358f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 3594469138eSBen Langmuir KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs); 3604469138eSBen Langmuir if (H) { 3614469138eSBen Langmuir Module *Result = H.getModule(); 362930a85ccSDouglas Gregor 363930a85ccSDouglas Gregor // Search up the module stack until we find a module with an umbrella 36473141fa9SDouglas Gregor // directory. 365930a85ccSDouglas Gregor Module *UmbrellaModule = Result; 36673141fa9SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 367930a85ccSDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 368930a85ccSDouglas Gregor 369930a85ccSDouglas Gregor if (UmbrellaModule->InferSubmodules) { 3709d6448b1SBen Langmuir const FileEntry *UmbrellaModuleMap = 3719d6448b1SBen Langmuir getModuleMapFileForUniquing(UmbrellaModule); 3729d6448b1SBen Langmuir 373a89c5ac4SDouglas Gregor // Infer submodules for each of the directories we found between 374a89c5ac4SDouglas Gregor // the directory of the umbrella header and the directory where 375a89c5ac4SDouglas Gregor // the actual header is located. 3769458f82dSDouglas Gregor bool Explicit = UmbrellaModule->InferExplicitSubmodules; 3779458f82dSDouglas Gregor 3787033127bSDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 379a89c5ac4SDouglas Gregor // Find or create the module that corresponds to this directory name. 380056396aeSDouglas Gregor SmallString<32> NameBuf; 381056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 3824469138eSBen Langmuir llvm::sys::path::stem(SkippedDirs[I-1]->getName()), NameBuf); 3839d6448b1SBen Langmuir Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 3849d6448b1SBen Langmuir Explicit).first; 3859d6448b1SBen Langmuir InferredModuleAllowedBy[Result] = UmbrellaModuleMap; 386ffbafa2aSBen Langmuir Result->IsInferred = true; 387a89c5ac4SDouglas Gregor 388a89c5ac4SDouglas Gregor // Associate the module and the directory. 389a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I-1]] = Result; 390a89c5ac4SDouglas Gregor 391a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 392a89c5ac4SDouglas Gregor // wildcard to the set of exports. 393930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 394d2d442caSCraig Topper Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 395a89c5ac4SDouglas Gregor } 396a89c5ac4SDouglas Gregor 397a89c5ac4SDouglas Gregor // Infer a submodule with the same name as this header file. 398056396aeSDouglas Gregor SmallString<32> NameBuf; 399056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 400056396aeSDouglas Gregor llvm::sys::path::stem(File->getName()), NameBuf); 4019d6448b1SBen Langmuir Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 4029d6448b1SBen Langmuir Explicit).first; 4039d6448b1SBen Langmuir InferredModuleAllowedBy[Result] = UmbrellaModuleMap; 404ffbafa2aSBen Langmuir Result->IsInferred = true; 4053c5305c1SArgyrios Kyrtzidis Result->addTopHeader(File); 406a89c5ac4SDouglas Gregor 407a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 408a89c5ac4SDouglas Gregor // wildcard to the set of exports. 409930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 410d2d442caSCraig Topper Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 411a89c5ac4SDouglas Gregor } else { 412a89c5ac4SDouglas Gregor // Record each of the directories we stepped through as being part of 413a89c5ac4SDouglas Gregor // the module we found, since the umbrella header covers them all. 414a89c5ac4SDouglas Gregor for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 415a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I]] = Result; 416a89c5ac4SDouglas Gregor } 417a89c5ac4SDouglas Gregor 41897da9178SDaniel Jasper Headers[File].push_back(KnownHeader(Result, NormalHeader)); 4191fb5c3a6SDouglas Gregor 4201fb5c3a6SDouglas Gregor // If a header corresponds to an unavailable module, don't report 4211fb5c3a6SDouglas Gregor // that it maps to anything. 4221fb5c3a6SDouglas Gregor if (!Result->isAvailable()) 423b53e5483SLawrence Crowl return KnownHeader(); 4241fb5c3a6SDouglas Gregor 425306d8920SRichard Smith return MakeResult(Headers[File].back()); 426a89c5ac4SDouglas Gregor } 427a89c5ac4SDouglas Gregor 428b53e5483SLawrence Crowl return KnownHeader(); 429ab0c8a84SDouglas Gregor } 430ab0c8a84SDouglas Gregor 431e4412640SArgyrios Kyrtzidis bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const { 432d2d442caSCraig Topper return isHeaderUnavailableInModule(Header, nullptr); 43350996ce1SRichard Smith } 43450996ce1SRichard Smith 43562bcd925SDmitri Gribenko bool 43662bcd925SDmitri Gribenko ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header, 43762bcd925SDmitri Gribenko const Module *RequestingModule) const { 438e4412640SArgyrios Kyrtzidis HeadersMap::const_iterator Known = Headers.find(Header); 43997da9178SDaniel Jasper if (Known != Headers.end()) { 44097da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::const_iterator 44197da9178SDaniel Jasper I = Known->second.begin(), 44297da9178SDaniel Jasper E = Known->second.end(); 44397da9178SDaniel Jasper I != E; ++I) { 44450996ce1SRichard Smith if (I->isAvailable() && (!RequestingModule || 44550996ce1SRichard Smith I->getModule()->isSubModuleOf(RequestingModule))) 44697da9178SDaniel Jasper return false; 44797da9178SDaniel Jasper } 44897da9178SDaniel Jasper return true; 44997da9178SDaniel Jasper } 4501fb5c3a6SDouglas Gregor 4511fb5c3a6SDouglas Gregor const DirectoryEntry *Dir = Header->getDir(); 452f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 4531fb5c3a6SDouglas Gregor StringRef DirName = Dir->getName(); 4541fb5c3a6SDouglas Gregor 45550996ce1SRichard Smith auto IsUnavailable = [&](const Module *M) { 45650996ce1SRichard Smith return !M->isAvailable() && (!RequestingModule || 45750996ce1SRichard Smith M->isSubModuleOf(RequestingModule)); 45850996ce1SRichard Smith }; 45950996ce1SRichard Smith 4601fb5c3a6SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 4611fb5c3a6SDouglas Gregor // an umbrella header. 4621fb5c3a6SDouglas Gregor do { 463e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir 4641fb5c3a6SDouglas Gregor = UmbrellaDirs.find(Dir); 4651fb5c3a6SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 4661fb5c3a6SDouglas Gregor Module *Found = KnownDir->second; 46750996ce1SRichard Smith if (IsUnavailable(Found)) 4681fb5c3a6SDouglas Gregor return true; 4691fb5c3a6SDouglas Gregor 4701fb5c3a6SDouglas Gregor // Search up the module stack until we find a module with an umbrella 4711fb5c3a6SDouglas Gregor // directory. 4721fb5c3a6SDouglas Gregor Module *UmbrellaModule = Found; 4731fb5c3a6SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 4741fb5c3a6SDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 4751fb5c3a6SDouglas Gregor 4761fb5c3a6SDouglas Gregor if (UmbrellaModule->InferSubmodules) { 4771fb5c3a6SDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 4781fb5c3a6SDouglas Gregor // Find or create the module that corresponds to this directory name. 479056396aeSDouglas Gregor SmallString<32> NameBuf; 480056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 481056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 482056396aeSDouglas Gregor NameBuf); 4831fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 4841fb5c3a6SDouglas Gregor if (!Found) 4851fb5c3a6SDouglas Gregor return false; 48650996ce1SRichard Smith if (IsUnavailable(Found)) 4871fb5c3a6SDouglas Gregor return true; 4881fb5c3a6SDouglas Gregor } 4891fb5c3a6SDouglas Gregor 4901fb5c3a6SDouglas Gregor // Infer a submodule with the same name as this header file. 491056396aeSDouglas Gregor SmallString<32> NameBuf; 492056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 493056396aeSDouglas Gregor llvm::sys::path::stem(Header->getName()), 494056396aeSDouglas Gregor NameBuf); 4951fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 4961fb5c3a6SDouglas Gregor if (!Found) 4971fb5c3a6SDouglas Gregor return false; 4981fb5c3a6SDouglas Gregor } 4991fb5c3a6SDouglas Gregor 50050996ce1SRichard Smith return IsUnavailable(Found); 5011fb5c3a6SDouglas Gregor } 5021fb5c3a6SDouglas Gregor 5031fb5c3a6SDouglas Gregor SkippedDirs.push_back(Dir); 5041fb5c3a6SDouglas Gregor 5051fb5c3a6SDouglas Gregor // Retrieve our parent path. 5061fb5c3a6SDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 5071fb5c3a6SDouglas Gregor if (DirName.empty()) 5081fb5c3a6SDouglas Gregor break; 5091fb5c3a6SDouglas Gregor 5101fb5c3a6SDouglas Gregor // Resolve the parent path to a directory entry. 5111f76c4e8SManuel Klimek Dir = SourceMgr.getFileManager().getDirectory(DirName); 5121fb5c3a6SDouglas Gregor } while (Dir); 5131fb5c3a6SDouglas Gregor 5141fb5c3a6SDouglas Gregor return false; 5151fb5c3a6SDouglas Gregor } 5161fb5c3a6SDouglas Gregor 517e4412640SArgyrios Kyrtzidis Module *ModuleMap::findModule(StringRef Name) const { 518e4412640SArgyrios Kyrtzidis llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name); 51988bdfb0eSDouglas Gregor if (Known != Modules.end()) 52088bdfb0eSDouglas Gregor return Known->getValue(); 52188bdfb0eSDouglas Gregor 522d2d442caSCraig Topper return nullptr; 52388bdfb0eSDouglas Gregor } 52488bdfb0eSDouglas Gregor 525e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleUnqualified(StringRef Name, 526e4412640SArgyrios Kyrtzidis Module *Context) const { 5272b82c2a5SDouglas Gregor for(; Context; Context = Context->Parent) { 5282b82c2a5SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Context)) 5292b82c2a5SDouglas Gregor return Sub; 5302b82c2a5SDouglas Gregor } 5312b82c2a5SDouglas Gregor 5322b82c2a5SDouglas Gregor return findModule(Name); 5332b82c2a5SDouglas Gregor } 5342b82c2a5SDouglas Gregor 535e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{ 5362b82c2a5SDouglas Gregor if (!Context) 5372b82c2a5SDouglas Gregor return findModule(Name); 5382b82c2a5SDouglas Gregor 539eb90e830SDouglas Gregor return Context->findSubmodule(Name); 5402b82c2a5SDouglas Gregor } 5412b82c2a5SDouglas Gregor 542de3ef502SDouglas Gregor std::pair<Module *, bool> 5439d6448b1SBen Langmuir ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 54469021974SDouglas Gregor bool IsExplicit) { 54569021974SDouglas Gregor // Try to find an existing module with this name. 546eb90e830SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Parent)) 547eb90e830SDouglas Gregor return std::make_pair(Sub, false); 54869021974SDouglas Gregor 54969021974SDouglas Gregor // Create a new module with this name. 5509d6448b1SBen Langmuir Module *Result = new Module(Name, SourceLocation(), Parent, 551beee15e7SBen Langmuir IsFramework, IsExplicit); 552ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == Name) { 553ba7f2f71SDaniel Jasper SourceModule = Result; 554ba7f2f71SDaniel Jasper SourceModuleName = Name; 555ba7f2f71SDaniel Jasper } 5566f722b4eSArgyrios Kyrtzidis if (!Parent) { 55769021974SDouglas Gregor Modules[Name] = Result; 5586f722b4eSArgyrios Kyrtzidis if (!LangOpts.CurrentModule.empty() && !CompilingModule && 5596f722b4eSArgyrios Kyrtzidis Name == LangOpts.CurrentModule) { 5606f722b4eSArgyrios Kyrtzidis CompilingModule = Result; 5616f722b4eSArgyrios Kyrtzidis } 5626f722b4eSArgyrios Kyrtzidis } 56369021974SDouglas Gregor return std::make_pair(Result, true); 56469021974SDouglas Gregor } 56569021974SDouglas Gregor 56611dfe6feSDouglas Gregor /// \brief For a framework module, infer the framework against which we 56711dfe6feSDouglas Gregor /// should link. 56811dfe6feSDouglas Gregor static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir, 56911dfe6feSDouglas Gregor FileManager &FileMgr) { 57011dfe6feSDouglas Gregor assert(Mod->IsFramework && "Can only infer linking for framework modules"); 57111dfe6feSDouglas Gregor assert(!Mod->isSubFramework() && 57211dfe6feSDouglas Gregor "Can only infer linking for top-level frameworks"); 57311dfe6feSDouglas Gregor 57411dfe6feSDouglas Gregor SmallString<128> LibName; 57511dfe6feSDouglas Gregor LibName += FrameworkDir->getName(); 57611dfe6feSDouglas Gregor llvm::sys::path::append(LibName, Mod->Name); 57711dfe6feSDouglas Gregor if (FileMgr.getFile(LibName)) { 57811dfe6feSDouglas Gregor Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name, 57911dfe6feSDouglas Gregor /*IsFramework=*/true)); 58011dfe6feSDouglas Gregor } 58111dfe6feSDouglas Gregor } 58211dfe6feSDouglas Gregor 583de3ef502SDouglas Gregor Module * 58456c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName, 585e89dbc1dSDouglas Gregor const DirectoryEntry *FrameworkDir, 586a686e1b0SDouglas Gregor bool IsSystem, 587e89dbc1dSDouglas Gregor Module *Parent) { 588*c1d88ea5SBen Langmuir Attributes Attrs; 589*c1d88ea5SBen Langmuir Attrs.IsSystem = IsSystem; 590*c1d88ea5SBen Langmuir return inferFrameworkModule(ModuleName, FrameworkDir, Attrs, Parent); 591*c1d88ea5SBen Langmuir } 592*c1d88ea5SBen Langmuir 593*c1d88ea5SBen Langmuir Module *ModuleMap::inferFrameworkModule(StringRef ModuleName, 594*c1d88ea5SBen Langmuir const DirectoryEntry *FrameworkDir, 595*c1d88ea5SBen Langmuir Attributes Attrs, Module *Parent) { 596*c1d88ea5SBen Langmuir 59756c64013SDouglas Gregor // Check whether we've already found this module. 598e89dbc1dSDouglas Gregor if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 599e89dbc1dSDouglas Gregor return Mod; 600e89dbc1dSDouglas Gregor 6011f76c4e8SManuel Klimek FileManager &FileMgr = SourceMgr.getFileManager(); 60256c64013SDouglas Gregor 6039194a91dSDouglas Gregor // If the framework has a parent path from which we're allowed to infer 6049194a91dSDouglas Gregor // a framework module, do so. 605beee15e7SBen Langmuir const FileEntry *ModuleMapFile = nullptr; 6069194a91dSDouglas Gregor if (!Parent) { 6074ddf2221SDouglas Gregor // Determine whether we're allowed to infer a module map. 608e00c8b20SDouglas Gregor 6094ddf2221SDouglas Gregor // Note: as an egregious but useful hack we use the real path here, because 6104ddf2221SDouglas Gregor // we might be looking at an embedded framework that symlinks out to a 6114ddf2221SDouglas Gregor // top-level framework, and we need to infer as if we were naming the 6124ddf2221SDouglas Gregor // top-level framework. 613e00c8b20SDouglas Gregor StringRef FrameworkDirName 6141f76c4e8SManuel Klimek = SourceMgr.getFileManager().getCanonicalName(FrameworkDir); 6154ddf2221SDouglas Gregor 6166b7f7345SBen Langmuir // In case this is a case-insensitive filesystem, make sure the canonical 6176b7f7345SBen Langmuir // directory name matches ModuleName exactly. Modules are case-sensitive. 6186b7f7345SBen Langmuir // FIXME: we should be able to give a fix-it hint for the correct spelling. 6196b7f7345SBen Langmuir if (llvm::sys::path::stem(FrameworkDirName) != ModuleName) 6206b7f7345SBen Langmuir return nullptr; 6216b7f7345SBen Langmuir 6229194a91dSDouglas Gregor bool canInfer = false; 6234ddf2221SDouglas Gregor if (llvm::sys::path::has_parent_path(FrameworkDirName)) { 6249194a91dSDouglas Gregor // Figure out the parent path. 6254ddf2221SDouglas Gregor StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); 6269194a91dSDouglas Gregor if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) { 6279194a91dSDouglas Gregor // Check whether we have already looked into the parent directory 6289194a91dSDouglas Gregor // for a module map. 629e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 6309194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 6319194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) { 6329194a91dSDouglas Gregor // We haven't looked here before. Load a module map, if there is 6339194a91dSDouglas Gregor // one. 634984e1df7SBen Langmuir bool IsFrameworkDir = Parent.endswith(".framework"); 635984e1df7SBen Langmuir if (const FileEntry *ModMapFile = 636984e1df7SBen Langmuir HeaderInfo.lookupModuleMapFile(ParentDir, IsFrameworkDir)) { 637*c1d88ea5SBen Langmuir parseModuleMapFile(ModMapFile, Attrs.IsSystem, ParentDir); 6389194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 6399194a91dSDouglas Gregor } 6409194a91dSDouglas Gregor 6419194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) 6429194a91dSDouglas Gregor inferred = InferredDirectories.insert( 6439194a91dSDouglas Gregor std::make_pair(ParentDir, InferredDirectory())).first; 6449194a91dSDouglas Gregor } 6459194a91dSDouglas Gregor 6469194a91dSDouglas Gregor if (inferred->second.InferModules) { 6479194a91dSDouglas Gregor // We're allowed to infer for this directory, but make sure it's okay 6489194a91dSDouglas Gregor // to infer this particular module. 6494ddf2221SDouglas Gregor StringRef Name = llvm::sys::path::stem(FrameworkDirName); 6509194a91dSDouglas Gregor canInfer = std::find(inferred->second.ExcludedModules.begin(), 6519194a91dSDouglas Gregor inferred->second.ExcludedModules.end(), 6529194a91dSDouglas Gregor Name) == inferred->second.ExcludedModules.end(); 6539194a91dSDouglas Gregor 654*c1d88ea5SBen Langmuir Attrs.IsSystem |= inferred->second.Attrs.IsSystem; 655*c1d88ea5SBen Langmuir Attrs.IsExternC |= inferred->second.Attrs.IsExternC; 656*c1d88ea5SBen Langmuir Attrs.IsExhaustive |= inferred->second.Attrs.IsExhaustive; 657beee15e7SBen Langmuir ModuleMapFile = inferred->second.ModuleMapFile; 6589194a91dSDouglas Gregor } 6599194a91dSDouglas Gregor } 6609194a91dSDouglas Gregor } 6619194a91dSDouglas Gregor 6629194a91dSDouglas Gregor // If we're not allowed to infer a framework module, don't. 6639194a91dSDouglas Gregor if (!canInfer) 664d2d442caSCraig Topper return nullptr; 665beee15e7SBen Langmuir } else 6669d6448b1SBen Langmuir ModuleMapFile = getModuleMapFileForUniquing(Parent); 6679194a91dSDouglas Gregor 6689194a91dSDouglas Gregor 66956c64013SDouglas Gregor // Look for an umbrella header. 6702c1dd271SDylan Noblesmith SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 67117381a06SBenjamin Kramer llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h"); 672e89dbc1dSDouglas Gregor const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 67356c64013SDouglas Gregor 67456c64013SDouglas Gregor // FIXME: If there's no umbrella header, we could probably scan the 67556c64013SDouglas Gregor // framework to load *everything*. But, it's not clear that this is a good 67656c64013SDouglas Gregor // idea. 67756c64013SDouglas Gregor if (!UmbrellaHeader) 678d2d442caSCraig Topper return nullptr; 67956c64013SDouglas Gregor 6809d6448b1SBen Langmuir Module *Result = new Module(ModuleName, SourceLocation(), Parent, 681e89dbc1dSDouglas Gregor /*IsFramework=*/true, /*IsExplicit=*/false); 6829d6448b1SBen Langmuir InferredModuleAllowedBy[Result] = ModuleMapFile; 6839d6448b1SBen Langmuir Result->IsInferred = true; 684ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == ModuleName) { 685ba7f2f71SDaniel Jasper SourceModule = Result; 686ba7f2f71SDaniel Jasper SourceModuleName = ModuleName; 687ba7f2f71SDaniel Jasper } 688*c1d88ea5SBen Langmuir 689*c1d88ea5SBen Langmuir Result->IsSystem |= Attrs.IsSystem; 690*c1d88ea5SBen Langmuir Result->IsExternC |= Attrs.IsExternC; 691*c1d88ea5SBen Langmuir Result->ConfigMacrosExhaustive |= Attrs.IsExhaustive; 692a686e1b0SDouglas Gregor 693eb90e830SDouglas Gregor if (!Parent) 694e89dbc1dSDouglas Gregor Modules[ModuleName] = Result; 695e89dbc1dSDouglas Gregor 696322f633cSDouglas Gregor // umbrella header "umbrella-header-name" 69773141fa9SDouglas Gregor Result->Umbrella = UmbrellaHeader; 69897da9178SDaniel Jasper Headers[UmbrellaHeader].push_back(KnownHeader(Result, NormalHeader)); 6994dc71835SDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Result; 700d8bd7537SDouglas Gregor 701d8bd7537SDouglas Gregor // export * 702d2d442caSCraig Topper Result->Exports.push_back(Module::ExportDecl(nullptr, true)); 703d8bd7537SDouglas Gregor 704a89c5ac4SDouglas Gregor // module * { export * } 705a89c5ac4SDouglas Gregor Result->InferSubmodules = true; 706a89c5ac4SDouglas Gregor Result->InferExportWildcard = true; 707a89c5ac4SDouglas Gregor 708e89dbc1dSDouglas Gregor // Look for subframeworks. 709c080917eSRafael Espindola std::error_code EC; 7102c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirName 711ddaa69cbSDouglas Gregor = StringRef(FrameworkDir->getName()); 712e89dbc1dSDouglas Gregor llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 7132d4d8cb3SBenjamin Kramer llvm::sys::path::native(SubframeworksDirName); 714ddaa69cbSDouglas Gregor for (llvm::sys::fs::directory_iterator 7152d4d8cb3SBenjamin Kramer Dir(SubframeworksDirName.str(), EC), DirEnd; 716e89dbc1dSDouglas Gregor Dir != DirEnd && !EC; Dir.increment(EC)) { 717e89dbc1dSDouglas Gregor if (!StringRef(Dir->path()).endswith(".framework")) 718e89dbc1dSDouglas Gregor continue; 719f2161a70SDouglas Gregor 720e89dbc1dSDouglas Gregor if (const DirectoryEntry *SubframeworkDir 721e89dbc1dSDouglas Gregor = FileMgr.getDirectory(Dir->path())) { 72207c22b78SDouglas Gregor // Note: as an egregious but useful hack, we use the real path here and 72307c22b78SDouglas Gregor // check whether it is actually a subdirectory of the parent directory. 72407c22b78SDouglas Gregor // This will not be the case if the 'subframework' is actually a symlink 72507c22b78SDouglas Gregor // out to a top-level framework. 726e00c8b20SDouglas Gregor StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir); 72707c22b78SDouglas Gregor bool FoundParent = false; 72807c22b78SDouglas Gregor do { 72907c22b78SDouglas Gregor // Get the parent directory name. 73007c22b78SDouglas Gregor SubframeworkDirName 73107c22b78SDouglas Gregor = llvm::sys::path::parent_path(SubframeworkDirName); 73207c22b78SDouglas Gregor if (SubframeworkDirName.empty()) 73307c22b78SDouglas Gregor break; 73407c22b78SDouglas Gregor 73507c22b78SDouglas Gregor if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { 73607c22b78SDouglas Gregor FoundParent = true; 73707c22b78SDouglas Gregor break; 73807c22b78SDouglas Gregor } 73907c22b78SDouglas Gregor } while (true); 74007c22b78SDouglas Gregor 74107c22b78SDouglas Gregor if (!FoundParent) 74207c22b78SDouglas Gregor continue; 74307c22b78SDouglas Gregor 744e89dbc1dSDouglas Gregor // FIXME: Do we want to warn about subframeworks without umbrella headers? 745056396aeSDouglas Gregor SmallString<32> NameBuf; 746056396aeSDouglas Gregor inferFrameworkModule(sanitizeFilenameAsIdentifier( 747056396aeSDouglas Gregor llvm::sys::path::stem(Dir->path()), NameBuf), 748*c1d88ea5SBen Langmuir SubframeworkDir, Attrs, Result); 749e89dbc1dSDouglas Gregor } 750e89dbc1dSDouglas Gregor } 751e89dbc1dSDouglas Gregor 75211dfe6feSDouglas Gregor // If the module is a top-level framework, automatically link against the 75311dfe6feSDouglas Gregor // framework. 75411dfe6feSDouglas Gregor if (!Result->isSubFramework()) { 75511dfe6feSDouglas Gregor inferFrameworkLink(Result, FrameworkDir, FileMgr); 75611dfe6feSDouglas Gregor } 75711dfe6feSDouglas Gregor 75856c64013SDouglas Gregor return Result; 75956c64013SDouglas Gregor } 76056c64013SDouglas Gregor 761a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ 76297da9178SDaniel Jasper Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader)); 76373141fa9SDouglas Gregor Mod->Umbrella = UmbrellaHeader; 7647033127bSDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; 765a89c5ac4SDouglas Gregor } 766a89c5ac4SDouglas Gregor 767524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) { 768524e33e1SDouglas Gregor Mod->Umbrella = UmbrellaDir; 769524e33e1SDouglas Gregor UmbrellaDirs[UmbrellaDir] = Mod; 770524e33e1SDouglas Gregor } 771524e33e1SDouglas Gregor 7723c1a41adSRichard Smith static Module::HeaderKind headerRoleToKind(ModuleMap::ModuleHeaderRole Role) { 7730e98d938SNAKAMURA Takumi switch ((int)Role) { 7743c1a41adSRichard Smith default: llvm_unreachable("unknown header role"); 7753c1a41adSRichard Smith case ModuleMap::NormalHeader: 7763c1a41adSRichard Smith return Module::HK_Normal; 7773c1a41adSRichard Smith case ModuleMap::PrivateHeader: 7783c1a41adSRichard Smith return Module::HK_Private; 7793c1a41adSRichard Smith case ModuleMap::TextualHeader: 7803c1a41adSRichard Smith return Module::HK_Textual; 7813c1a41adSRichard Smith case ModuleMap::PrivateHeader | ModuleMap::TextualHeader: 7823c1a41adSRichard Smith return Module::HK_PrivateTextual; 7833c1a41adSRichard Smith } 7840e98d938SNAKAMURA Takumi } 785202210b3SRichard Smith 7863c1a41adSRichard Smith void ModuleMap::addHeader(Module *Mod, Module::Header Header, 7873c1a41adSRichard Smith ModuleHeaderRole Role) { 788202210b3SRichard Smith if (!(Role & TextualHeader)) { 7896f722b4eSArgyrios Kyrtzidis bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule; 7903c1a41adSRichard Smith HeaderInfo.MarkFileModuleHeader(Header.Entry, Role, 7913c1a41adSRichard Smith isCompilingModuleHeader); 792b146baabSArgyrios Kyrtzidis } 7933c1a41adSRichard Smith Headers[Header.Entry].push_back(KnownHeader(Mod, Role)); 7943c1a41adSRichard Smith 7953c1a41adSRichard Smith Mod->Headers[headerRoleToKind(Role)].push_back(std::move(Header)); 796a89c5ac4SDouglas Gregor } 797a89c5ac4SDouglas Gregor 7983c1a41adSRichard Smith void ModuleMap::excludeHeader(Module *Mod, Module::Header Header) { 799feb54b6dSRichard Smith // Add this as a known header so we won't implicitly add it to any 800feb54b6dSRichard Smith // umbrella directory module. 801feb54b6dSRichard Smith // FIXME: Should we only exclude it from umbrella modules within the 802feb54b6dSRichard Smith // specified module? 8033c1a41adSRichard Smith (void) Headers[Header.Entry]; 8043c1a41adSRichard Smith 8053c1a41adSRichard Smith Mod->Headers[Module::HK_Excluded].push_back(std::move(Header)); 806feb54b6dSRichard Smith } 807feb54b6dSRichard Smith 808514b636aSDouglas Gregor const FileEntry * 8094b8a9e95SBen Langmuir ModuleMap::getContainingModuleMapFile(const Module *Module) const { 8101f76c4e8SManuel Klimek if (Module->DefinitionLoc.isInvalid()) 811d2d442caSCraig Topper return nullptr; 812514b636aSDouglas Gregor 8131f76c4e8SManuel Klimek return SourceMgr.getFileEntryForID( 8141f76c4e8SManuel Klimek SourceMgr.getFileID(Module->DefinitionLoc)); 815514b636aSDouglas Gregor } 816514b636aSDouglas Gregor 8174b8a9e95SBen Langmuir const FileEntry *ModuleMap::getModuleMapFileForUniquing(const Module *M) const { 8189d6448b1SBen Langmuir if (M->IsInferred) { 8199d6448b1SBen Langmuir assert(InferredModuleAllowedBy.count(M) && "missing inferred module map"); 8209d6448b1SBen Langmuir return InferredModuleAllowedBy.find(M)->second; 8219d6448b1SBen Langmuir } 8229d6448b1SBen Langmuir return getContainingModuleMapFile(M); 8239d6448b1SBen Langmuir } 8249d6448b1SBen Langmuir 8259d6448b1SBen Langmuir void ModuleMap::setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap) { 8269d6448b1SBen Langmuir assert(M->IsInferred && "module not inferred"); 8279d6448b1SBen Langmuir InferredModuleAllowedBy[M] = ModMap; 8289d6448b1SBen Langmuir } 8299d6448b1SBen Langmuir 830718292f2SDouglas Gregor void ModuleMap::dump() { 831718292f2SDouglas Gregor llvm::errs() << "Modules:"; 832718292f2SDouglas Gregor for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 833718292f2SDouglas Gregor MEnd = Modules.end(); 834718292f2SDouglas Gregor M != MEnd; ++M) 835d28d1b8dSDouglas Gregor M->getValue()->print(llvm::errs(), 2); 836718292f2SDouglas Gregor 837718292f2SDouglas Gregor llvm::errs() << "Headers:"; 83859527666SDouglas Gregor for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 839718292f2SDouglas Gregor H != HEnd; ++H) { 84097da9178SDaniel Jasper llvm::errs() << " \"" << H->first->getName() << "\" -> "; 84197da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(), 84297da9178SDaniel Jasper E = H->second.end(); 84397da9178SDaniel Jasper I != E; ++I) { 84497da9178SDaniel Jasper if (I != H->second.begin()) 84597da9178SDaniel Jasper llvm::errs() << ","; 84697da9178SDaniel Jasper llvm::errs() << I->getModule()->getFullModuleName(); 84797da9178SDaniel Jasper } 84897da9178SDaniel Jasper llvm::errs() << "\n"; 849718292f2SDouglas Gregor } 850718292f2SDouglas Gregor } 851718292f2SDouglas Gregor 8522b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 8532b82c2a5SDouglas Gregor bool HadError = false; 8542b82c2a5SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { 8552b82c2a5SDouglas Gregor Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 8562b82c2a5SDouglas Gregor Complain); 857f5eedd05SDouglas Gregor if (Export.getPointer() || Export.getInt()) 8582b82c2a5SDouglas Gregor Mod->Exports.push_back(Export); 8592b82c2a5SDouglas Gregor else 8602b82c2a5SDouglas Gregor HadError = true; 8612b82c2a5SDouglas Gregor } 8622b82c2a5SDouglas Gregor Mod->UnresolvedExports.clear(); 8632b82c2a5SDouglas Gregor return HadError; 8642b82c2a5SDouglas Gregor } 8652b82c2a5SDouglas Gregor 866ba7f2f71SDaniel Jasper bool ModuleMap::resolveUses(Module *Mod, bool Complain) { 867ba7f2f71SDaniel Jasper bool HadError = false; 868ba7f2f71SDaniel Jasper for (unsigned I = 0, N = Mod->UnresolvedDirectUses.size(); I != N; ++I) { 869ba7f2f71SDaniel Jasper Module *DirectUse = 870ba7f2f71SDaniel Jasper resolveModuleId(Mod->UnresolvedDirectUses[I], Mod, Complain); 871ba7f2f71SDaniel Jasper if (DirectUse) 872ba7f2f71SDaniel Jasper Mod->DirectUses.push_back(DirectUse); 873ba7f2f71SDaniel Jasper else 874ba7f2f71SDaniel Jasper HadError = true; 875ba7f2f71SDaniel Jasper } 876ba7f2f71SDaniel Jasper Mod->UnresolvedDirectUses.clear(); 877ba7f2f71SDaniel Jasper return HadError; 878ba7f2f71SDaniel Jasper } 879ba7f2f71SDaniel Jasper 880fb912657SDouglas Gregor bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) { 881fb912657SDouglas Gregor bool HadError = false; 882fb912657SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) { 883fb912657SDouglas Gregor Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id, 884fb912657SDouglas Gregor Mod, Complain); 885fb912657SDouglas Gregor if (!OtherMod) { 886fb912657SDouglas Gregor HadError = true; 887fb912657SDouglas Gregor continue; 888fb912657SDouglas Gregor } 889fb912657SDouglas Gregor 890fb912657SDouglas Gregor Module::Conflict Conflict; 891fb912657SDouglas Gregor Conflict.Other = OtherMod; 892fb912657SDouglas Gregor Conflict.Message = Mod->UnresolvedConflicts[I].Message; 893fb912657SDouglas Gregor Mod->Conflicts.push_back(Conflict); 894fb912657SDouglas Gregor } 895fb912657SDouglas Gregor Mod->UnresolvedConflicts.clear(); 896fb912657SDouglas Gregor return HadError; 897fb912657SDouglas Gregor } 898fb912657SDouglas Gregor 8990093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 9000093b3c7SDouglas Gregor if (Loc.isInvalid()) 901d2d442caSCraig Topper return nullptr; 9020093b3c7SDouglas Gregor 9030093b3c7SDouglas Gregor // Use the expansion location to determine which module we're in. 9040093b3c7SDouglas Gregor FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 9050093b3c7SDouglas Gregor if (!ExpansionLoc.isFileID()) 906d2d442caSCraig Topper return nullptr; 9070093b3c7SDouglas Gregor 9080093b3c7SDouglas Gregor const SourceManager &SrcMgr = Loc.getManager(); 9090093b3c7SDouglas Gregor FileID ExpansionFileID = ExpansionLoc.getFileID(); 910224d8a74SDouglas Gregor 911224d8a74SDouglas Gregor while (const FileEntry *ExpansionFile 912224d8a74SDouglas Gregor = SrcMgr.getFileEntryForID(ExpansionFileID)) { 913224d8a74SDouglas Gregor // Find the module that owns this header (if any). 914b53e5483SLawrence Crowl if (Module *Mod = findModuleForHeader(ExpansionFile).getModule()) 915224d8a74SDouglas Gregor return Mod; 916224d8a74SDouglas Gregor 917224d8a74SDouglas Gregor // No module owns this header, so look up the inclusion chain to see if 918224d8a74SDouglas Gregor // any included header has an associated module. 919224d8a74SDouglas Gregor SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); 920224d8a74SDouglas Gregor if (IncludeLoc.isInvalid()) 921d2d442caSCraig Topper return nullptr; 9220093b3c7SDouglas Gregor 923224d8a74SDouglas Gregor ExpansionFileID = SrcMgr.getFileID(IncludeLoc); 924224d8a74SDouglas Gregor } 925224d8a74SDouglas Gregor 926d2d442caSCraig Topper return nullptr; 9270093b3c7SDouglas Gregor } 9280093b3c7SDouglas Gregor 929718292f2SDouglas Gregor //----------------------------------------------------------------------------// 930718292f2SDouglas Gregor // Module map file parser 931718292f2SDouglas Gregor //----------------------------------------------------------------------------// 932718292f2SDouglas Gregor 933718292f2SDouglas Gregor namespace clang { 934718292f2SDouglas Gregor /// \brief A token in a module map file. 935718292f2SDouglas Gregor struct MMToken { 936718292f2SDouglas Gregor enum TokenKind { 9371fb5c3a6SDouglas Gregor Comma, 93835b13eceSDouglas Gregor ConfigMacros, 939fb912657SDouglas Gregor Conflict, 940718292f2SDouglas Gregor EndOfFile, 941718292f2SDouglas Gregor HeaderKeyword, 942718292f2SDouglas Gregor Identifier, 943a3feee2aSRichard Smith Exclaim, 94459527666SDouglas Gregor ExcludeKeyword, 945718292f2SDouglas Gregor ExplicitKeyword, 9462b82c2a5SDouglas Gregor ExportKeyword, 94797292843SDaniel Jasper ExternKeyword, 948755b2055SDouglas Gregor FrameworkKeyword, 9496ddfca91SDouglas Gregor LinkKeyword, 950718292f2SDouglas Gregor ModuleKeyword, 9512b82c2a5SDouglas Gregor Period, 952b53e5483SLawrence Crowl PrivateKeyword, 953718292f2SDouglas Gregor UmbrellaKeyword, 954ba7f2f71SDaniel Jasper UseKeyword, 9551fb5c3a6SDouglas Gregor RequiresKeyword, 9562b82c2a5SDouglas Gregor Star, 957718292f2SDouglas Gregor StringLiteral, 958306d8920SRichard Smith TextualKeyword, 959718292f2SDouglas Gregor LBrace, 960a686e1b0SDouglas Gregor RBrace, 961a686e1b0SDouglas Gregor LSquare, 962a686e1b0SDouglas Gregor RSquare 963718292f2SDouglas Gregor } Kind; 964718292f2SDouglas Gregor 965718292f2SDouglas Gregor unsigned Location; 966718292f2SDouglas Gregor unsigned StringLength; 967718292f2SDouglas Gregor const char *StringData; 968718292f2SDouglas Gregor 969718292f2SDouglas Gregor void clear() { 970718292f2SDouglas Gregor Kind = EndOfFile; 971718292f2SDouglas Gregor Location = 0; 972718292f2SDouglas Gregor StringLength = 0; 973d2d442caSCraig Topper StringData = nullptr; 974718292f2SDouglas Gregor } 975718292f2SDouglas Gregor 976718292f2SDouglas Gregor bool is(TokenKind K) const { return Kind == K; } 977718292f2SDouglas Gregor 978718292f2SDouglas Gregor SourceLocation getLocation() const { 979718292f2SDouglas Gregor return SourceLocation::getFromRawEncoding(Location); 980718292f2SDouglas Gregor } 981718292f2SDouglas Gregor 982718292f2SDouglas Gregor StringRef getString() const { 983718292f2SDouglas Gregor return StringRef(StringData, StringLength); 984718292f2SDouglas Gregor } 985718292f2SDouglas Gregor }; 986718292f2SDouglas Gregor 987718292f2SDouglas Gregor class ModuleMapParser { 988718292f2SDouglas Gregor Lexer &L; 989718292f2SDouglas Gregor SourceManager &SourceMgr; 990bc10b9fbSDouglas Gregor 991bc10b9fbSDouglas Gregor /// \brief Default target information, used only for string literal 992bc10b9fbSDouglas Gregor /// parsing. 993bc10b9fbSDouglas Gregor const TargetInfo *Target; 994bc10b9fbSDouglas Gregor 995718292f2SDouglas Gregor DiagnosticsEngine &Diags; 996718292f2SDouglas Gregor ModuleMap ⤅ 997718292f2SDouglas Gregor 998beee15e7SBen Langmuir /// \brief The current module map file. 999beee15e7SBen Langmuir const FileEntry *ModuleMapFile; 1000beee15e7SBen Langmuir 10019acb99e3SRichard Smith /// \brief The directory that file names in this module map file should 10029acb99e3SRichard Smith /// be resolved relative to. 10035257fc63SDouglas Gregor const DirectoryEntry *Directory; 10045257fc63SDouglas Gregor 10053ec6663bSDouglas Gregor /// \brief The directory containing Clang-supplied headers. 10063ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir; 10073ec6663bSDouglas Gregor 1008963c5535SDouglas Gregor /// \brief Whether this module map is in a system header directory. 1009963c5535SDouglas Gregor bool IsSystem; 1010963c5535SDouglas Gregor 1011718292f2SDouglas Gregor /// \brief Whether an error occurred. 1012718292f2SDouglas Gregor bool HadError; 1013718292f2SDouglas Gregor 1014718292f2SDouglas Gregor /// \brief Stores string data for the various string literals referenced 1015718292f2SDouglas Gregor /// during parsing. 1016718292f2SDouglas Gregor llvm::BumpPtrAllocator StringData; 1017718292f2SDouglas Gregor 1018718292f2SDouglas Gregor /// \brief The current token. 1019718292f2SDouglas Gregor MMToken Tok; 1020718292f2SDouglas Gregor 1021718292f2SDouglas Gregor /// \brief The active module. 1022de3ef502SDouglas Gregor Module *ActiveModule; 1023718292f2SDouglas Gregor 1024718292f2SDouglas Gregor /// \brief Consume the current token and return its location. 1025718292f2SDouglas Gregor SourceLocation consumeToken(); 1026718292f2SDouglas Gregor 1027718292f2SDouglas Gregor /// \brief Skip tokens until we reach the a token with the given kind 1028718292f2SDouglas Gregor /// (or the end of the file). 1029718292f2SDouglas Gregor void skipUntil(MMToken::TokenKind K); 1030718292f2SDouglas Gregor 1031f857950dSDmitri Gribenko typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; 1032e7ab3669SDouglas Gregor bool parseModuleId(ModuleId &Id); 1033718292f2SDouglas Gregor void parseModuleDecl(); 103497292843SDaniel Jasper void parseExternModuleDecl(); 10351fb5c3a6SDouglas Gregor void parseRequiresDecl(); 1036b53e5483SLawrence Crowl void parseHeaderDecl(clang::MMToken::TokenKind, 1037b53e5483SLawrence Crowl SourceLocation LeadingLoc); 1038524e33e1SDouglas Gregor void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 10392b82c2a5SDouglas Gregor void parseExportDecl(); 1040ba7f2f71SDaniel Jasper void parseUseDecl(); 10416ddfca91SDouglas Gregor void parseLinkDecl(); 104235b13eceSDouglas Gregor void parseConfigMacros(); 1043fb912657SDouglas Gregor void parseConflict(); 10449194a91dSDouglas Gregor void parseInferredModuleDecl(bool Framework, bool Explicit); 1045*c1d88ea5SBen Langmuir 1046*c1d88ea5SBen Langmuir typedef ModuleMap::Attributes Attributes; 10474442605fSBill Wendling bool parseOptionalAttributes(Attributes &Attrs); 1048718292f2SDouglas Gregor 1049718292f2SDouglas Gregor public: 1050718292f2SDouglas Gregor explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 1051bc10b9fbSDouglas Gregor const TargetInfo *Target, 1052718292f2SDouglas Gregor DiagnosticsEngine &Diags, 10535257fc63SDouglas Gregor ModuleMap &Map, 1054beee15e7SBen Langmuir const FileEntry *ModuleMapFile, 10553ec6663bSDouglas Gregor const DirectoryEntry *Directory, 1056963c5535SDouglas Gregor const DirectoryEntry *BuiltinIncludeDir, 1057963c5535SDouglas Gregor bool IsSystem) 1058bc10b9fbSDouglas Gregor : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 1059beee15e7SBen Langmuir ModuleMapFile(ModuleMapFile), Directory(Directory), 1060beee15e7SBen Langmuir BuiltinIncludeDir(BuiltinIncludeDir), IsSystem(IsSystem), 1061d2d442caSCraig Topper HadError(false), ActiveModule(nullptr) 1062718292f2SDouglas Gregor { 1063718292f2SDouglas Gregor Tok.clear(); 1064718292f2SDouglas Gregor consumeToken(); 1065718292f2SDouglas Gregor } 1066718292f2SDouglas Gregor 1067718292f2SDouglas Gregor bool parseModuleMapFile(); 1068718292f2SDouglas Gregor }; 1069718292f2SDouglas Gregor } 1070718292f2SDouglas Gregor 1071718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() { 1072718292f2SDouglas Gregor retry: 1073718292f2SDouglas Gregor SourceLocation Result = Tok.getLocation(); 1074718292f2SDouglas Gregor Tok.clear(); 1075718292f2SDouglas Gregor 1076718292f2SDouglas Gregor Token LToken; 1077718292f2SDouglas Gregor L.LexFromRawLexer(LToken); 1078718292f2SDouglas Gregor Tok.Location = LToken.getLocation().getRawEncoding(); 1079718292f2SDouglas Gregor switch (LToken.getKind()) { 10802d57cea2SAlp Toker case tok::raw_identifier: { 10812d57cea2SAlp Toker StringRef RI = LToken.getRawIdentifier(); 10822d57cea2SAlp Toker Tok.StringData = RI.data(); 10832d57cea2SAlp Toker Tok.StringLength = RI.size(); 10842d57cea2SAlp Toker Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI) 108535b13eceSDouglas Gregor .Case("config_macros", MMToken::ConfigMacros) 1086fb912657SDouglas Gregor .Case("conflict", MMToken::Conflict) 108759527666SDouglas Gregor .Case("exclude", MMToken::ExcludeKeyword) 1088718292f2SDouglas Gregor .Case("explicit", MMToken::ExplicitKeyword) 10892b82c2a5SDouglas Gregor .Case("export", MMToken::ExportKeyword) 109097292843SDaniel Jasper .Case("extern", MMToken::ExternKeyword) 1091755b2055SDouglas Gregor .Case("framework", MMToken::FrameworkKeyword) 109235b13eceSDouglas Gregor .Case("header", MMToken::HeaderKeyword) 10936ddfca91SDouglas Gregor .Case("link", MMToken::LinkKeyword) 1094718292f2SDouglas Gregor .Case("module", MMToken::ModuleKeyword) 1095b53e5483SLawrence Crowl .Case("private", MMToken::PrivateKeyword) 10961fb5c3a6SDouglas Gregor .Case("requires", MMToken::RequiresKeyword) 1097306d8920SRichard Smith .Case("textual", MMToken::TextualKeyword) 1098718292f2SDouglas Gregor .Case("umbrella", MMToken::UmbrellaKeyword) 1099ba7f2f71SDaniel Jasper .Case("use", MMToken::UseKeyword) 1100718292f2SDouglas Gregor .Default(MMToken::Identifier); 1101718292f2SDouglas Gregor break; 11022d57cea2SAlp Toker } 1103718292f2SDouglas Gregor 11041fb5c3a6SDouglas Gregor case tok::comma: 11051fb5c3a6SDouglas Gregor Tok.Kind = MMToken::Comma; 11061fb5c3a6SDouglas Gregor break; 11071fb5c3a6SDouglas Gregor 1108718292f2SDouglas Gregor case tok::eof: 1109718292f2SDouglas Gregor Tok.Kind = MMToken::EndOfFile; 1110718292f2SDouglas Gregor break; 1111718292f2SDouglas Gregor 1112718292f2SDouglas Gregor case tok::l_brace: 1113718292f2SDouglas Gregor Tok.Kind = MMToken::LBrace; 1114718292f2SDouglas Gregor break; 1115718292f2SDouglas Gregor 1116a686e1b0SDouglas Gregor case tok::l_square: 1117a686e1b0SDouglas Gregor Tok.Kind = MMToken::LSquare; 1118a686e1b0SDouglas Gregor break; 1119a686e1b0SDouglas Gregor 11202b82c2a5SDouglas Gregor case tok::period: 11212b82c2a5SDouglas Gregor Tok.Kind = MMToken::Period; 11222b82c2a5SDouglas Gregor break; 11232b82c2a5SDouglas Gregor 1124718292f2SDouglas Gregor case tok::r_brace: 1125718292f2SDouglas Gregor Tok.Kind = MMToken::RBrace; 1126718292f2SDouglas Gregor break; 1127718292f2SDouglas Gregor 1128a686e1b0SDouglas Gregor case tok::r_square: 1129a686e1b0SDouglas Gregor Tok.Kind = MMToken::RSquare; 1130a686e1b0SDouglas Gregor break; 1131a686e1b0SDouglas Gregor 11322b82c2a5SDouglas Gregor case tok::star: 11332b82c2a5SDouglas Gregor Tok.Kind = MMToken::Star; 11342b82c2a5SDouglas Gregor break; 11352b82c2a5SDouglas Gregor 1136a3feee2aSRichard Smith case tok::exclaim: 1137a3feee2aSRichard Smith Tok.Kind = MMToken::Exclaim; 1138a3feee2aSRichard Smith break; 1139a3feee2aSRichard Smith 1140718292f2SDouglas Gregor case tok::string_literal: { 1141d67aea28SRichard Smith if (LToken.hasUDSuffix()) { 1142d67aea28SRichard Smith Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 1143d67aea28SRichard Smith HadError = true; 1144d67aea28SRichard Smith goto retry; 1145d67aea28SRichard Smith } 1146d67aea28SRichard Smith 1147718292f2SDouglas Gregor // Parse the string literal. 1148718292f2SDouglas Gregor LangOptions LangOpts; 11499d5583efSCraig Topper StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target); 1150718292f2SDouglas Gregor if (StringLiteral.hadError) 1151718292f2SDouglas Gregor goto retry; 1152718292f2SDouglas Gregor 1153718292f2SDouglas Gregor // Copy the string literal into our string data allocator. 1154718292f2SDouglas Gregor unsigned Length = StringLiteral.GetStringLength(); 1155718292f2SDouglas Gregor char *Saved = StringData.Allocate<char>(Length + 1); 1156718292f2SDouglas Gregor memcpy(Saved, StringLiteral.GetString().data(), Length); 1157718292f2SDouglas Gregor Saved[Length] = 0; 1158718292f2SDouglas Gregor 1159718292f2SDouglas Gregor // Form the token. 1160718292f2SDouglas Gregor Tok.Kind = MMToken::StringLiteral; 1161718292f2SDouglas Gregor Tok.StringData = Saved; 1162718292f2SDouglas Gregor Tok.StringLength = Length; 1163718292f2SDouglas Gregor break; 1164718292f2SDouglas Gregor } 1165718292f2SDouglas Gregor 1166718292f2SDouglas Gregor case tok::comment: 1167718292f2SDouglas Gregor goto retry; 1168718292f2SDouglas Gregor 1169718292f2SDouglas Gregor default: 1170718292f2SDouglas Gregor Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 1171718292f2SDouglas Gregor HadError = true; 1172718292f2SDouglas Gregor goto retry; 1173718292f2SDouglas Gregor } 1174718292f2SDouglas Gregor 1175718292f2SDouglas Gregor return Result; 1176718292f2SDouglas Gregor } 1177718292f2SDouglas Gregor 1178718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 1179718292f2SDouglas Gregor unsigned braceDepth = 0; 1180a686e1b0SDouglas Gregor unsigned squareDepth = 0; 1181718292f2SDouglas Gregor do { 1182718292f2SDouglas Gregor switch (Tok.Kind) { 1183718292f2SDouglas Gregor case MMToken::EndOfFile: 1184718292f2SDouglas Gregor return; 1185718292f2SDouglas Gregor 1186718292f2SDouglas Gregor case MMToken::LBrace: 1187a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1188718292f2SDouglas Gregor return; 1189718292f2SDouglas Gregor 1190718292f2SDouglas Gregor ++braceDepth; 1191718292f2SDouglas Gregor break; 1192718292f2SDouglas Gregor 1193a686e1b0SDouglas Gregor case MMToken::LSquare: 1194a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1195a686e1b0SDouglas Gregor return; 1196a686e1b0SDouglas Gregor 1197a686e1b0SDouglas Gregor ++squareDepth; 1198a686e1b0SDouglas Gregor break; 1199a686e1b0SDouglas Gregor 1200718292f2SDouglas Gregor case MMToken::RBrace: 1201718292f2SDouglas Gregor if (braceDepth > 0) 1202718292f2SDouglas Gregor --braceDepth; 1203718292f2SDouglas Gregor else if (Tok.is(K)) 1204718292f2SDouglas Gregor return; 1205718292f2SDouglas Gregor break; 1206718292f2SDouglas Gregor 1207a686e1b0SDouglas Gregor case MMToken::RSquare: 1208a686e1b0SDouglas Gregor if (squareDepth > 0) 1209a686e1b0SDouglas Gregor --squareDepth; 1210a686e1b0SDouglas Gregor else if (Tok.is(K)) 1211a686e1b0SDouglas Gregor return; 1212a686e1b0SDouglas Gregor break; 1213a686e1b0SDouglas Gregor 1214718292f2SDouglas Gregor default: 1215a686e1b0SDouglas Gregor if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 1216718292f2SDouglas Gregor return; 1217718292f2SDouglas Gregor break; 1218718292f2SDouglas Gregor } 1219718292f2SDouglas Gregor 1220718292f2SDouglas Gregor consumeToken(); 1221718292f2SDouglas Gregor } while (true); 1222718292f2SDouglas Gregor } 1223718292f2SDouglas Gregor 1224e7ab3669SDouglas Gregor /// \brief Parse a module-id. 1225e7ab3669SDouglas Gregor /// 1226e7ab3669SDouglas Gregor /// module-id: 1227e7ab3669SDouglas Gregor /// identifier 1228e7ab3669SDouglas Gregor /// identifier '.' module-id 1229e7ab3669SDouglas Gregor /// 1230e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise. 1231e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) { 1232e7ab3669SDouglas Gregor Id.clear(); 1233e7ab3669SDouglas Gregor do { 12343cd34c76SDaniel Jasper if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) { 1235e7ab3669SDouglas Gregor Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 1236e7ab3669SDouglas Gregor consumeToken(); 1237e7ab3669SDouglas Gregor } else { 1238e7ab3669SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 1239e7ab3669SDouglas Gregor return true; 1240e7ab3669SDouglas Gregor } 1241e7ab3669SDouglas Gregor 1242e7ab3669SDouglas Gregor if (!Tok.is(MMToken::Period)) 1243e7ab3669SDouglas Gregor break; 1244e7ab3669SDouglas Gregor 1245e7ab3669SDouglas Gregor consumeToken(); 1246e7ab3669SDouglas Gregor } while (true); 1247e7ab3669SDouglas Gregor 1248e7ab3669SDouglas Gregor return false; 1249e7ab3669SDouglas Gregor } 1250e7ab3669SDouglas Gregor 1251a686e1b0SDouglas Gregor namespace { 1252a686e1b0SDouglas Gregor /// \brief Enumerates the known attributes. 1253a686e1b0SDouglas Gregor enum AttributeKind { 1254a686e1b0SDouglas Gregor /// \brief An unknown attribute. 1255a686e1b0SDouglas Gregor AT_unknown, 1256a686e1b0SDouglas Gregor /// \brief The 'system' attribute. 125735b13eceSDouglas Gregor AT_system, 125877944868SRichard Smith /// \brief The 'extern_c' attribute. 125977944868SRichard Smith AT_extern_c, 126035b13eceSDouglas Gregor /// \brief The 'exhaustive' attribute. 126135b13eceSDouglas Gregor AT_exhaustive 1262a686e1b0SDouglas Gregor }; 1263a686e1b0SDouglas Gregor } 1264a686e1b0SDouglas Gregor 1265718292f2SDouglas Gregor /// \brief Parse a module declaration. 1266718292f2SDouglas Gregor /// 1267718292f2SDouglas Gregor /// module-declaration: 126897292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 1269a686e1b0SDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 1270a686e1b0SDouglas Gregor /// { module-member* } 1271a686e1b0SDouglas Gregor /// 1272718292f2SDouglas Gregor /// module-member: 12731fb5c3a6SDouglas Gregor /// requires-declaration 1274718292f2SDouglas Gregor /// header-declaration 1275e7ab3669SDouglas Gregor /// submodule-declaration 12762b82c2a5SDouglas Gregor /// export-declaration 12776ddfca91SDouglas Gregor /// link-declaration 127873441091SDouglas Gregor /// 127973441091SDouglas Gregor /// submodule-declaration: 128073441091SDouglas Gregor /// module-declaration 128173441091SDouglas Gregor /// inferred-submodule-declaration 1282718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() { 1283755b2055SDouglas Gregor assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 128497292843SDaniel Jasper Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword)); 128597292843SDaniel Jasper if (Tok.is(MMToken::ExternKeyword)) { 128697292843SDaniel Jasper parseExternModuleDecl(); 128797292843SDaniel Jasper return; 128897292843SDaniel Jasper } 128997292843SDaniel Jasper 1290f2161a70SDouglas Gregor // Parse 'explicit' or 'framework' keyword, if present. 1291e7ab3669SDouglas Gregor SourceLocation ExplicitLoc; 1292718292f2SDouglas Gregor bool Explicit = false; 1293f2161a70SDouglas Gregor bool Framework = false; 1294755b2055SDouglas Gregor 1295f2161a70SDouglas Gregor // Parse 'explicit' keyword, if present. 1296f2161a70SDouglas Gregor if (Tok.is(MMToken::ExplicitKeyword)) { 1297e7ab3669SDouglas Gregor ExplicitLoc = consumeToken(); 1298f2161a70SDouglas Gregor Explicit = true; 1299f2161a70SDouglas Gregor } 1300f2161a70SDouglas Gregor 1301f2161a70SDouglas Gregor // Parse 'framework' keyword, if present. 1302755b2055SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 1303755b2055SDouglas Gregor consumeToken(); 1304755b2055SDouglas Gregor Framework = true; 1305755b2055SDouglas Gregor } 1306718292f2SDouglas Gregor 1307718292f2SDouglas Gregor // Parse 'module' keyword. 1308718292f2SDouglas Gregor if (!Tok.is(MMToken::ModuleKeyword)) { 1309d6343c99SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1310718292f2SDouglas Gregor consumeToken(); 1311718292f2SDouglas Gregor HadError = true; 1312718292f2SDouglas Gregor return; 1313718292f2SDouglas Gregor } 1314718292f2SDouglas Gregor consumeToken(); // 'module' keyword 1315718292f2SDouglas Gregor 131673441091SDouglas Gregor // If we have a wildcard for the module name, this is an inferred submodule. 131773441091SDouglas Gregor // Parse it. 131873441091SDouglas Gregor if (Tok.is(MMToken::Star)) 13199194a91dSDouglas Gregor return parseInferredModuleDecl(Framework, Explicit); 132073441091SDouglas Gregor 1321718292f2SDouglas Gregor // Parse the module name. 1322e7ab3669SDouglas Gregor ModuleId Id; 1323e7ab3669SDouglas Gregor if (parseModuleId(Id)) { 1324718292f2SDouglas Gregor HadError = true; 1325718292f2SDouglas Gregor return; 1326718292f2SDouglas Gregor } 1327e7ab3669SDouglas Gregor 1328e7ab3669SDouglas Gregor if (ActiveModule) { 1329e7ab3669SDouglas Gregor if (Id.size() > 1) { 1330e7ab3669SDouglas Gregor Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 1331e7ab3669SDouglas Gregor << SourceRange(Id.front().second, Id.back().second); 1332e7ab3669SDouglas Gregor 1333e7ab3669SDouglas Gregor HadError = true; 1334e7ab3669SDouglas Gregor return; 1335e7ab3669SDouglas Gregor } 1336e7ab3669SDouglas Gregor } else if (Id.size() == 1 && Explicit) { 1337e7ab3669SDouglas Gregor // Top-level modules can't be explicit. 1338e7ab3669SDouglas Gregor Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 1339e7ab3669SDouglas Gregor Explicit = false; 1340e7ab3669SDouglas Gregor ExplicitLoc = SourceLocation(); 1341e7ab3669SDouglas Gregor HadError = true; 1342e7ab3669SDouglas Gregor } 1343e7ab3669SDouglas Gregor 1344e7ab3669SDouglas Gregor Module *PreviousActiveModule = ActiveModule; 1345e7ab3669SDouglas Gregor if (Id.size() > 1) { 1346e7ab3669SDouglas Gregor // This module map defines a submodule. Go find the module of which it 1347e7ab3669SDouglas Gregor // is a submodule. 1348d2d442caSCraig Topper ActiveModule = nullptr; 13494b8a9e95SBen Langmuir const Module *TopLevelModule = nullptr; 1350e7ab3669SDouglas Gregor for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 1351e7ab3669SDouglas Gregor if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 13524b8a9e95SBen Langmuir if (I == 0) 13534b8a9e95SBen Langmuir TopLevelModule = Next; 1354e7ab3669SDouglas Gregor ActiveModule = Next; 1355e7ab3669SDouglas Gregor continue; 1356e7ab3669SDouglas Gregor } 1357e7ab3669SDouglas Gregor 1358e7ab3669SDouglas Gregor if (ActiveModule) { 1359e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 13605b5d21eaSRichard Smith << Id[I].first 13615b5d21eaSRichard Smith << ActiveModule->getTopLevelModule()->getFullModuleName(); 1362e7ab3669SDouglas Gregor } else { 1363e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 1364e7ab3669SDouglas Gregor } 1365e7ab3669SDouglas Gregor HadError = true; 1366e7ab3669SDouglas Gregor return; 1367e7ab3669SDouglas Gregor } 13684b8a9e95SBen Langmuir 13694b8a9e95SBen Langmuir if (ModuleMapFile != Map.getContainingModuleMapFile(TopLevelModule)) { 13704b8a9e95SBen Langmuir assert(ModuleMapFile != Map.getModuleMapFileForUniquing(TopLevelModule) && 13714b8a9e95SBen Langmuir "submodule defined in same file as 'module *' that allowed its " 13724b8a9e95SBen Langmuir "top-level module"); 13734b8a9e95SBen Langmuir Map.addAdditionalModuleMapFile(TopLevelModule, ModuleMapFile); 13744b8a9e95SBen Langmuir } 1375e7ab3669SDouglas Gregor } 1376e7ab3669SDouglas Gregor 1377e7ab3669SDouglas Gregor StringRef ModuleName = Id.back().first; 1378e7ab3669SDouglas Gregor SourceLocation ModuleNameLoc = Id.back().second; 1379718292f2SDouglas Gregor 1380a686e1b0SDouglas Gregor // Parse the optional attribute list. 13814442605fSBill Wendling Attributes Attrs; 13829194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 1383a686e1b0SDouglas Gregor 1384718292f2SDouglas Gregor // Parse the opening brace. 1385718292f2SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 1386718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 1387718292f2SDouglas Gregor << ModuleName; 1388718292f2SDouglas Gregor HadError = true; 1389718292f2SDouglas Gregor return; 1390718292f2SDouglas Gregor } 1391718292f2SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 1392718292f2SDouglas Gregor 1393718292f2SDouglas Gregor // Determine whether this (sub)module has already been defined. 1394eb90e830SDouglas Gregor if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 1395fcc54a3bSDouglas Gregor if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { 1396fcc54a3bSDouglas Gregor // Skip the module definition. 1397fcc54a3bSDouglas Gregor skipUntil(MMToken::RBrace); 1398fcc54a3bSDouglas Gregor if (Tok.is(MMToken::RBrace)) 1399fcc54a3bSDouglas Gregor consumeToken(); 1400fcc54a3bSDouglas Gregor else { 1401fcc54a3bSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1402fcc54a3bSDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1403fcc54a3bSDouglas Gregor HadError = true; 1404fcc54a3bSDouglas Gregor } 1405fcc54a3bSDouglas Gregor return; 1406fcc54a3bSDouglas Gregor } 1407fcc54a3bSDouglas Gregor 1408718292f2SDouglas Gregor Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 1409718292f2SDouglas Gregor << ModuleName; 1410eb90e830SDouglas Gregor Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 1411718292f2SDouglas Gregor 1412718292f2SDouglas Gregor // Skip the module definition. 1413718292f2SDouglas Gregor skipUntil(MMToken::RBrace); 1414718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1415718292f2SDouglas Gregor consumeToken(); 1416718292f2SDouglas Gregor 1417718292f2SDouglas Gregor HadError = true; 1418718292f2SDouglas Gregor return; 1419718292f2SDouglas Gregor } 1420718292f2SDouglas Gregor 1421718292f2SDouglas Gregor // Start defining this module. 14229d6448b1SBen Langmuir ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, 14239d6448b1SBen Langmuir Explicit).first; 1424eb90e830SDouglas Gregor ActiveModule->DefinitionLoc = ModuleNameLoc; 1425963c5535SDouglas Gregor if (Attrs.IsSystem || IsSystem) 1426a686e1b0SDouglas Gregor ActiveModule->IsSystem = true; 142777944868SRichard Smith if (Attrs.IsExternC) 142877944868SRichard Smith ActiveModule->IsExternC = true; 14293c1a41adSRichard Smith ActiveModule->Directory = Directory; 1430718292f2SDouglas Gregor 1431718292f2SDouglas Gregor bool Done = false; 1432718292f2SDouglas Gregor do { 1433718292f2SDouglas Gregor switch (Tok.Kind) { 1434718292f2SDouglas Gregor case MMToken::EndOfFile: 1435718292f2SDouglas Gregor case MMToken::RBrace: 1436718292f2SDouglas Gregor Done = true; 1437718292f2SDouglas Gregor break; 1438718292f2SDouglas Gregor 143935b13eceSDouglas Gregor case MMToken::ConfigMacros: 144035b13eceSDouglas Gregor parseConfigMacros(); 144135b13eceSDouglas Gregor break; 144235b13eceSDouglas Gregor 1443fb912657SDouglas Gregor case MMToken::Conflict: 1444fb912657SDouglas Gregor parseConflict(); 1445fb912657SDouglas Gregor break; 1446fb912657SDouglas Gregor 1447718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 144897292843SDaniel Jasper case MMToken::ExternKeyword: 1449f2161a70SDouglas Gregor case MMToken::FrameworkKeyword: 1450718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1451718292f2SDouglas Gregor parseModuleDecl(); 1452718292f2SDouglas Gregor break; 1453718292f2SDouglas Gregor 14542b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 14552b82c2a5SDouglas Gregor parseExportDecl(); 14562b82c2a5SDouglas Gregor break; 14572b82c2a5SDouglas Gregor 1458ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 1459ba7f2f71SDaniel Jasper parseUseDecl(); 1460ba7f2f71SDaniel Jasper break; 1461ba7f2f71SDaniel Jasper 14621fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 14631fb5c3a6SDouglas Gregor parseRequiresDecl(); 14641fb5c3a6SDouglas Gregor break; 14651fb5c3a6SDouglas Gregor 1466202210b3SRichard Smith case MMToken::TextualKeyword: 1467202210b3SRichard Smith parseHeaderDecl(MMToken::TextualKeyword, consumeToken()); 1468306d8920SRichard Smith break; 1469306d8920SRichard Smith 1470524e33e1SDouglas Gregor case MMToken::UmbrellaKeyword: { 1471524e33e1SDouglas Gregor SourceLocation UmbrellaLoc = consumeToken(); 1472524e33e1SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) 1473b53e5483SLawrence Crowl parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc); 1474524e33e1SDouglas Gregor else 1475524e33e1SDouglas Gregor parseUmbrellaDirDecl(UmbrellaLoc); 1476718292f2SDouglas Gregor break; 1477524e33e1SDouglas Gregor } 1478718292f2SDouglas Gregor 1479202210b3SRichard Smith case MMToken::ExcludeKeyword: 1480202210b3SRichard Smith parseHeaderDecl(MMToken::ExcludeKeyword, consumeToken()); 148159527666SDouglas Gregor break; 148259527666SDouglas Gregor 1483202210b3SRichard Smith case MMToken::PrivateKeyword: 1484202210b3SRichard Smith parseHeaderDecl(MMToken::PrivateKeyword, consumeToken()); 1485b53e5483SLawrence Crowl break; 1486b53e5483SLawrence Crowl 1487322f633cSDouglas Gregor case MMToken::HeaderKeyword: 1488202210b3SRichard Smith parseHeaderDecl(MMToken::HeaderKeyword, consumeToken()); 1489718292f2SDouglas Gregor break; 1490718292f2SDouglas Gregor 14916ddfca91SDouglas Gregor case MMToken::LinkKeyword: 14926ddfca91SDouglas Gregor parseLinkDecl(); 14936ddfca91SDouglas Gregor break; 14946ddfca91SDouglas Gregor 1495718292f2SDouglas Gregor default: 1496718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 1497718292f2SDouglas Gregor consumeToken(); 1498718292f2SDouglas Gregor break; 1499718292f2SDouglas Gregor } 1500718292f2SDouglas Gregor } while (!Done); 1501718292f2SDouglas Gregor 1502718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1503718292f2SDouglas Gregor consumeToken(); 1504718292f2SDouglas Gregor else { 1505718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1506718292f2SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1507718292f2SDouglas Gregor HadError = true; 1508718292f2SDouglas Gregor } 1509718292f2SDouglas Gregor 151011dfe6feSDouglas Gregor // If the active module is a top-level framework, and there are no link 151111dfe6feSDouglas Gregor // libraries, automatically link against the framework. 151211dfe6feSDouglas Gregor if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() && 151311dfe6feSDouglas Gregor ActiveModule->LinkLibraries.empty()) { 151411dfe6feSDouglas Gregor inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager()); 151511dfe6feSDouglas Gregor } 151611dfe6feSDouglas Gregor 1517ec8c9752SBen Langmuir // If the module meets all requirements but is still unavailable, mark the 1518ec8c9752SBen Langmuir // whole tree as unavailable to prevent it from building. 1519ec8c9752SBen Langmuir if (!ActiveModule->IsAvailable && !ActiveModule->IsMissingRequirement && 1520ec8c9752SBen Langmuir ActiveModule->Parent) { 1521ec8c9752SBen Langmuir ActiveModule->getTopLevelModule()->markUnavailable(); 1522ec8c9752SBen Langmuir ActiveModule->getTopLevelModule()->MissingHeaders.append( 1523ec8c9752SBen Langmuir ActiveModule->MissingHeaders.begin(), ActiveModule->MissingHeaders.end()); 1524ec8c9752SBen Langmuir } 1525ec8c9752SBen Langmuir 1526e7ab3669SDouglas Gregor // We're done parsing this module. Pop back to the previous module. 1527e7ab3669SDouglas Gregor ActiveModule = PreviousActiveModule; 1528718292f2SDouglas Gregor } 1529718292f2SDouglas Gregor 153097292843SDaniel Jasper /// \brief Parse an extern module declaration. 153197292843SDaniel Jasper /// 153297292843SDaniel Jasper /// extern module-declaration: 153397292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 153497292843SDaniel Jasper void ModuleMapParser::parseExternModuleDecl() { 153597292843SDaniel Jasper assert(Tok.is(MMToken::ExternKeyword)); 153697292843SDaniel Jasper consumeToken(); // 'extern' keyword 153797292843SDaniel Jasper 153897292843SDaniel Jasper // Parse 'module' keyword. 153997292843SDaniel Jasper if (!Tok.is(MMToken::ModuleKeyword)) { 154097292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 154197292843SDaniel Jasper consumeToken(); 154297292843SDaniel Jasper HadError = true; 154397292843SDaniel Jasper return; 154497292843SDaniel Jasper } 154597292843SDaniel Jasper consumeToken(); // 'module' keyword 154697292843SDaniel Jasper 154797292843SDaniel Jasper // Parse the module name. 154897292843SDaniel Jasper ModuleId Id; 154997292843SDaniel Jasper if (parseModuleId(Id)) { 155097292843SDaniel Jasper HadError = true; 155197292843SDaniel Jasper return; 155297292843SDaniel Jasper } 155397292843SDaniel Jasper 155497292843SDaniel Jasper // Parse the referenced module map file name. 155597292843SDaniel Jasper if (!Tok.is(MMToken::StringLiteral)) { 155697292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file); 155797292843SDaniel Jasper HadError = true; 155897292843SDaniel Jasper return; 155997292843SDaniel Jasper } 156097292843SDaniel Jasper std::string FileName = Tok.getString(); 156197292843SDaniel Jasper consumeToken(); // filename 156297292843SDaniel Jasper 156397292843SDaniel Jasper StringRef FileNameRef = FileName; 156497292843SDaniel Jasper SmallString<128> ModuleMapFileName; 156597292843SDaniel Jasper if (llvm::sys::path::is_relative(FileNameRef)) { 156697292843SDaniel Jasper ModuleMapFileName += Directory->getName(); 156797292843SDaniel Jasper llvm::sys::path::append(ModuleMapFileName, FileName); 156897292843SDaniel Jasper FileNameRef = ModuleMapFileName.str(); 156997292843SDaniel Jasper } 157097292843SDaniel Jasper if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef)) 15719acb99e3SRichard Smith Map.parseModuleMapFile( 15729acb99e3SRichard Smith File, /*IsSystem=*/false, 15739acb99e3SRichard Smith Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd 15749acb99e3SRichard Smith ? Directory 15759acb99e3SRichard Smith : File->getDir()); 157697292843SDaniel Jasper } 157797292843SDaniel Jasper 15781fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration. 15791fb5c3a6SDouglas Gregor /// 15801fb5c3a6SDouglas Gregor /// requires-declaration: 15811fb5c3a6SDouglas Gregor /// 'requires' feature-list 15821fb5c3a6SDouglas Gregor /// 15831fb5c3a6SDouglas Gregor /// feature-list: 1584a3feee2aSRichard Smith /// feature ',' feature-list 1585a3feee2aSRichard Smith /// feature 1586a3feee2aSRichard Smith /// 1587a3feee2aSRichard Smith /// feature: 1588a3feee2aSRichard Smith /// '!'[opt] identifier 15891fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() { 15901fb5c3a6SDouglas Gregor assert(Tok.is(MMToken::RequiresKeyword)); 15911fb5c3a6SDouglas Gregor 15921fb5c3a6SDouglas Gregor // Parse 'requires' keyword. 15931fb5c3a6SDouglas Gregor consumeToken(); 15941fb5c3a6SDouglas Gregor 15951fb5c3a6SDouglas Gregor // Parse the feature-list. 15961fb5c3a6SDouglas Gregor do { 1597a3feee2aSRichard Smith bool RequiredState = true; 1598a3feee2aSRichard Smith if (Tok.is(MMToken::Exclaim)) { 1599a3feee2aSRichard Smith RequiredState = false; 1600a3feee2aSRichard Smith consumeToken(); 1601a3feee2aSRichard Smith } 1602a3feee2aSRichard Smith 16031fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 16041fb5c3a6SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 16051fb5c3a6SDouglas Gregor HadError = true; 16061fb5c3a6SDouglas Gregor return; 16071fb5c3a6SDouglas Gregor } 16081fb5c3a6SDouglas Gregor 16091fb5c3a6SDouglas Gregor // Consume the feature name. 16101fb5c3a6SDouglas Gregor std::string Feature = Tok.getString(); 16111fb5c3a6SDouglas Gregor consumeToken(); 16121fb5c3a6SDouglas Gregor 16131fb5c3a6SDouglas Gregor // Add this feature. 1614a3feee2aSRichard Smith ActiveModule->addRequirement(Feature, RequiredState, 1615a3feee2aSRichard Smith Map.LangOpts, *Map.Target); 16161fb5c3a6SDouglas Gregor 16171fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Comma)) 16181fb5c3a6SDouglas Gregor break; 16191fb5c3a6SDouglas Gregor 16201fb5c3a6SDouglas Gregor // Consume the comma. 16211fb5c3a6SDouglas Gregor consumeToken(); 16221fb5c3a6SDouglas Gregor } while (true); 16231fb5c3a6SDouglas Gregor } 16241fb5c3a6SDouglas Gregor 1625f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the 1626f2161a70SDouglas Gregor /// subframework in which the given module lives. 1627bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod, 1628f857950dSDmitri Gribenko SmallVectorImpl<char> &Path) { 1629f2161a70SDouglas Gregor // Collect the framework names from the given module to the top-level module. 1630f857950dSDmitri Gribenko SmallVector<StringRef, 2> Paths; 1631f2161a70SDouglas Gregor for (; Mod; Mod = Mod->Parent) { 1632f2161a70SDouglas Gregor if (Mod->IsFramework) 1633f2161a70SDouglas Gregor Paths.push_back(Mod->Name); 1634f2161a70SDouglas Gregor } 1635f2161a70SDouglas Gregor 1636f2161a70SDouglas Gregor if (Paths.empty()) 1637f2161a70SDouglas Gregor return; 1638f2161a70SDouglas Gregor 1639f2161a70SDouglas Gregor // Add Frameworks/Name.framework for each subframework. 164017381a06SBenjamin Kramer for (unsigned I = Paths.size() - 1; I != 0; --I) 164117381a06SBenjamin Kramer llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework"); 1642f2161a70SDouglas Gregor } 1643f2161a70SDouglas Gregor 1644718292f2SDouglas Gregor /// \brief Parse a header declaration. 1645718292f2SDouglas Gregor /// 1646718292f2SDouglas Gregor /// header-declaration: 1647306d8920SRichard Smith /// 'textual'[opt] 'header' string-literal 1648202210b3SRichard Smith /// 'private' 'textual'[opt] 'header' string-literal 1649202210b3SRichard Smith /// 'exclude' 'header' string-literal 1650202210b3SRichard Smith /// 'umbrella' 'header' string-literal 1651306d8920SRichard Smith /// 1652306d8920SRichard Smith /// FIXME: Support 'private textual header'. 1653b53e5483SLawrence Crowl void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken, 1654b53e5483SLawrence Crowl SourceLocation LeadingLoc) { 1655202210b3SRichard Smith // We've already consumed the first token. 1656202210b3SRichard Smith ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader; 1657202210b3SRichard Smith if (LeadingToken == MMToken::PrivateKeyword) { 1658202210b3SRichard Smith Role = ModuleMap::PrivateHeader; 1659202210b3SRichard Smith // 'private' may optionally be followed by 'textual'. 1660202210b3SRichard Smith if (Tok.is(MMToken::TextualKeyword)) { 1661202210b3SRichard Smith LeadingToken = Tok.Kind; 16621871ed3dSBenjamin Kramer consumeToken(); 1663202210b3SRichard Smith } 1664202210b3SRichard Smith } 1665202210b3SRichard Smith if (LeadingToken == MMToken::TextualKeyword) 1666202210b3SRichard Smith Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader); 1667202210b3SRichard Smith 1668202210b3SRichard Smith if (LeadingToken != MMToken::HeaderKeyword) { 1669202210b3SRichard Smith if (!Tok.is(MMToken::HeaderKeyword)) { 1670202210b3SRichard Smith Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1671202210b3SRichard Smith << (LeadingToken == MMToken::PrivateKeyword ? "private" : 1672202210b3SRichard Smith LeadingToken == MMToken::ExcludeKeyword ? "exclude" : 1673202210b3SRichard Smith LeadingToken == MMToken::TextualKeyword ? "textual" : "umbrella"); 1674202210b3SRichard Smith return; 1675202210b3SRichard Smith } 1676202210b3SRichard Smith consumeToken(); 1677202210b3SRichard Smith } 1678718292f2SDouglas Gregor 1679718292f2SDouglas Gregor // Parse the header name. 1680718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1681718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1682718292f2SDouglas Gregor << "header"; 1683718292f2SDouglas Gregor HadError = true; 1684718292f2SDouglas Gregor return; 1685718292f2SDouglas Gregor } 16863c1a41adSRichard Smith Module::UnresolvedHeaderDirective Header; 16870761a8a0SDaniel Jasper Header.FileName = Tok.getString(); 16880761a8a0SDaniel Jasper Header.FileNameLoc = consumeToken(); 1689718292f2SDouglas Gregor 1690524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1691b53e5483SLawrence Crowl if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) { 16920761a8a0SDaniel Jasper Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash) 1693524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1694322f633cSDouglas Gregor HadError = true; 1695322f633cSDouglas Gregor return; 1696322f633cSDouglas Gregor } 1697322f633cSDouglas Gregor 16985257fc63SDouglas Gregor // Look for this file. 1699d2d442caSCraig Topper const FileEntry *File = nullptr; 1700d2d442caSCraig Topper const FileEntry *BuiltinFile = nullptr; 17013c1a41adSRichard Smith SmallString<128> RelativePathName; 17020761a8a0SDaniel Jasper if (llvm::sys::path::is_absolute(Header.FileName)) { 17033c1a41adSRichard Smith RelativePathName = Header.FileName; 17043c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(RelativePathName); 1705e7ab3669SDouglas Gregor } else { 1706e7ab3669SDouglas Gregor // Search for the header file within the search directory. 17073c1a41adSRichard Smith SmallString<128> FullPathName(Directory->getName()); 17083c1a41adSRichard Smith unsigned FullPathLength = FullPathName.size(); 1709755b2055SDouglas Gregor 1710f2161a70SDouglas Gregor if (ActiveModule->isPartOfFramework()) { 17113c1a41adSRichard Smith appendSubframeworkPaths(ActiveModule, RelativePathName); 1712755b2055SDouglas Gregor 1713e7ab3669SDouglas Gregor // Check whether this file is in the public headers. 17143c1a41adSRichard Smith llvm::sys::path::append(RelativePathName, "Headers", Header.FileName); 17153c1a41adSRichard Smith llvm::sys::path::append(FullPathName, RelativePathName.str()); 17163c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(FullPathName); 1717e7ab3669SDouglas Gregor 1718e7ab3669SDouglas Gregor if (!File) { 1719e7ab3669SDouglas Gregor // Check whether this file is in the private headers. 17203c1a41adSRichard Smith // FIXME: Should we retain the subframework paths here? 17213c1a41adSRichard Smith RelativePathName.clear(); 17223c1a41adSRichard Smith FullPathName.resize(FullPathLength); 17233c1a41adSRichard Smith llvm::sys::path::append(RelativePathName, "PrivateHeaders", 17243c1a41adSRichard Smith Header.FileName); 17253c1a41adSRichard Smith llvm::sys::path::append(FullPathName, RelativePathName.str()); 17263c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(FullPathName); 1727e7ab3669SDouglas Gregor } 1728e7ab3669SDouglas Gregor } else { 1729e7ab3669SDouglas Gregor // Lookup for normal headers. 17303c1a41adSRichard Smith llvm::sys::path::append(RelativePathName, Header.FileName); 17313c1a41adSRichard Smith llvm::sys::path::append(FullPathName, RelativePathName.str()); 17323c1a41adSRichard Smith File = SourceMgr.getFileManager().getFile(FullPathName); 17333ec6663bSDouglas Gregor 17343ec6663bSDouglas Gregor // If this is a system module with a top-level header, this header 17353ec6663bSDouglas Gregor // may have a counterpart (or replacement) in the set of headers 17363ec6663bSDouglas Gregor // supplied by Clang. Find that builtin header. 1737b53e5483SLawrence Crowl if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword && 1738b53e5483SLawrence Crowl BuiltinIncludeDir && BuiltinIncludeDir != Directory && 17390761a8a0SDaniel Jasper isBuiltinHeader(Header.FileName)) { 17402c1dd271SDylan Noblesmith SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); 17410761a8a0SDaniel Jasper llvm::sys::path::append(BuiltinPathName, Header.FileName); 17423ec6663bSDouglas Gregor BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); 17433ec6663bSDouglas Gregor 17443ec6663bSDouglas Gregor // If Clang supplies this header but the underlying system does not, 17453ec6663bSDouglas Gregor // just silently swap in our builtin version. Otherwise, we'll end 17463ec6663bSDouglas Gregor // up adding both (later). 17473ec6663bSDouglas Gregor if (!File && BuiltinFile) { 17483ec6663bSDouglas Gregor File = BuiltinFile; 17493c1a41adSRichard Smith RelativePathName = BuiltinPathName; 1750d2d442caSCraig Topper BuiltinFile = nullptr; 17513ec6663bSDouglas Gregor } 17523ec6663bSDouglas Gregor } 1753e7ab3669SDouglas Gregor } 1754e7ab3669SDouglas Gregor } 17555257fc63SDouglas Gregor 17565257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 17575257fc63SDouglas Gregor // Come up with a lazy way to do this. 1758e7ab3669SDouglas Gregor if (File) { 175997da9178SDaniel Jasper if (LeadingToken == MMToken::UmbrellaKeyword) { 1760322f633cSDouglas Gregor const DirectoryEntry *UmbrellaDir = File->getDir(); 176159527666SDouglas Gregor if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { 1762b53e5483SLawrence Crowl Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash) 176359527666SDouglas Gregor << UmbrellaModule->getFullModuleName(); 1764322f633cSDouglas Gregor HadError = true; 17655257fc63SDouglas Gregor } else { 1766322f633cSDouglas Gregor // Record this umbrella header. 1767322f633cSDouglas Gregor Map.setUmbrellaHeader(ActiveModule, File); 1768322f633cSDouglas Gregor } 1769feb54b6dSRichard Smith } else if (LeadingToken == MMToken::ExcludeKeyword) { 17700101b540SHans Wennborg Module::Header H = {RelativePathName.str(), File}; 17710101b540SHans Wennborg Map.excludeHeader(ActiveModule, H); 1772322f633cSDouglas Gregor } else { 177325d50758SRichard Smith // If there is a builtin counterpart to this file, add it now, before 177425d50758SRichard Smith // the "real" header, so we build the built-in one first when building 177525d50758SRichard Smith // the module. 17760101b540SHans Wennborg if (BuiltinFile) { 17773c1a41adSRichard Smith // FIXME: Taking the name from the FileEntry is unstable and can give 17783c1a41adSRichard Smith // different results depending on how we've previously named that file 17793c1a41adSRichard Smith // in this build. 17800101b540SHans Wennborg Module::Header H = { BuiltinFile->getName(), BuiltinFile }; 17810101b540SHans Wennborg Map.addHeader(ActiveModule, H, Role); 17820101b540SHans Wennborg } 178325d50758SRichard Smith 1784202210b3SRichard Smith // Record this header. 17850101b540SHans Wennborg Module::Header H = { RelativePathName.str(), File }; 17860101b540SHans Wennborg Map.addHeader(ActiveModule, H, Role); 17875257fc63SDouglas Gregor } 1788b53e5483SLawrence Crowl } else if (LeadingToken != MMToken::ExcludeKeyword) { 17894b27a64bSDouglas Gregor // Ignore excluded header files. They're optional anyway. 17904b27a64bSDouglas Gregor 17910761a8a0SDaniel Jasper // If we find a module that has a missing header, we mark this module as 17920761a8a0SDaniel Jasper // unavailable and store the header directive for displaying diagnostics. 17930761a8a0SDaniel Jasper Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword; 1794ec8c9752SBen Langmuir ActiveModule->markUnavailable(); 17950761a8a0SDaniel Jasper ActiveModule->MissingHeaders.push_back(Header); 17965257fc63SDouglas Gregor } 1797718292f2SDouglas Gregor } 1798718292f2SDouglas Gregor 1799524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration. 1800524e33e1SDouglas Gregor /// 1801524e33e1SDouglas Gregor /// umbrella-dir-declaration: 1802524e33e1SDouglas Gregor /// umbrella string-literal 1803524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1804524e33e1SDouglas Gregor // Parse the directory name. 1805524e33e1SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1806524e33e1SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1807524e33e1SDouglas Gregor << "umbrella"; 1808524e33e1SDouglas Gregor HadError = true; 1809524e33e1SDouglas Gregor return; 1810524e33e1SDouglas Gregor } 1811524e33e1SDouglas Gregor 1812524e33e1SDouglas Gregor std::string DirName = Tok.getString(); 1813524e33e1SDouglas Gregor SourceLocation DirNameLoc = consumeToken(); 1814524e33e1SDouglas Gregor 1815524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1816524e33e1SDouglas Gregor if (ActiveModule->Umbrella) { 1817524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1818524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1819524e33e1SDouglas Gregor HadError = true; 1820524e33e1SDouglas Gregor return; 1821524e33e1SDouglas Gregor } 1822524e33e1SDouglas Gregor 1823524e33e1SDouglas Gregor // Look for this file. 1824d2d442caSCraig Topper const DirectoryEntry *Dir = nullptr; 1825524e33e1SDouglas Gregor if (llvm::sys::path::is_absolute(DirName)) 1826524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(DirName); 1827524e33e1SDouglas Gregor else { 18282c1dd271SDylan Noblesmith SmallString<128> PathName; 1829524e33e1SDouglas Gregor PathName = Directory->getName(); 1830524e33e1SDouglas Gregor llvm::sys::path::append(PathName, DirName); 1831524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(PathName); 1832524e33e1SDouglas Gregor } 1833524e33e1SDouglas Gregor 1834524e33e1SDouglas Gregor if (!Dir) { 1835524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1836524e33e1SDouglas Gregor << DirName; 1837524e33e1SDouglas Gregor HadError = true; 1838524e33e1SDouglas Gregor return; 1839524e33e1SDouglas Gregor } 1840524e33e1SDouglas Gregor 1841524e33e1SDouglas Gregor if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1842524e33e1SDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1843524e33e1SDouglas Gregor << OwningModule->getFullModuleName(); 1844524e33e1SDouglas Gregor HadError = true; 1845524e33e1SDouglas Gregor return; 1846524e33e1SDouglas Gregor } 1847524e33e1SDouglas Gregor 1848524e33e1SDouglas Gregor // Record this umbrella directory. 1849524e33e1SDouglas Gregor Map.setUmbrellaDir(ActiveModule, Dir); 1850524e33e1SDouglas Gregor } 1851524e33e1SDouglas Gregor 18522b82c2a5SDouglas Gregor /// \brief Parse a module export declaration. 18532b82c2a5SDouglas Gregor /// 18542b82c2a5SDouglas Gregor /// export-declaration: 18552b82c2a5SDouglas Gregor /// 'export' wildcard-module-id 18562b82c2a5SDouglas Gregor /// 18572b82c2a5SDouglas Gregor /// wildcard-module-id: 18582b82c2a5SDouglas Gregor /// identifier 18592b82c2a5SDouglas Gregor /// '*' 18602b82c2a5SDouglas Gregor /// identifier '.' wildcard-module-id 18612b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() { 18622b82c2a5SDouglas Gregor assert(Tok.is(MMToken::ExportKeyword)); 18632b82c2a5SDouglas Gregor SourceLocation ExportLoc = consumeToken(); 18642b82c2a5SDouglas Gregor 18652b82c2a5SDouglas Gregor // Parse the module-id with an optional wildcard at the end. 18662b82c2a5SDouglas Gregor ModuleId ParsedModuleId; 18672b82c2a5SDouglas Gregor bool Wildcard = false; 18682b82c2a5SDouglas Gregor do { 1869306d8920SRichard Smith // FIXME: Support string-literal module names here. 18702b82c2a5SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 18712b82c2a5SDouglas Gregor ParsedModuleId.push_back(std::make_pair(Tok.getString(), 18722b82c2a5SDouglas Gregor Tok.getLocation())); 18732b82c2a5SDouglas Gregor consumeToken(); 18742b82c2a5SDouglas Gregor 18752b82c2a5SDouglas Gregor if (Tok.is(MMToken::Period)) { 18762b82c2a5SDouglas Gregor consumeToken(); 18772b82c2a5SDouglas Gregor continue; 18782b82c2a5SDouglas Gregor } 18792b82c2a5SDouglas Gregor 18802b82c2a5SDouglas Gregor break; 18812b82c2a5SDouglas Gregor } 18822b82c2a5SDouglas Gregor 18832b82c2a5SDouglas Gregor if(Tok.is(MMToken::Star)) { 18842b82c2a5SDouglas Gregor Wildcard = true; 1885f5eedd05SDouglas Gregor consumeToken(); 18862b82c2a5SDouglas Gregor break; 18872b82c2a5SDouglas Gregor } 18882b82c2a5SDouglas Gregor 1889ba7f2f71SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); 18902b82c2a5SDouglas Gregor HadError = true; 18912b82c2a5SDouglas Gregor return; 18922b82c2a5SDouglas Gregor } while (true); 18932b82c2a5SDouglas Gregor 18942b82c2a5SDouglas Gregor Module::UnresolvedExportDecl Unresolved = { 18952b82c2a5SDouglas Gregor ExportLoc, ParsedModuleId, Wildcard 18962b82c2a5SDouglas Gregor }; 18972b82c2a5SDouglas Gregor ActiveModule->UnresolvedExports.push_back(Unresolved); 18982b82c2a5SDouglas Gregor } 18992b82c2a5SDouglas Gregor 1900ba7f2f71SDaniel Jasper /// \brief Parse a module uses declaration. 1901ba7f2f71SDaniel Jasper /// 1902ba7f2f71SDaniel Jasper /// uses-declaration: 1903ba7f2f71SDaniel Jasper /// 'uses' wildcard-module-id 1904ba7f2f71SDaniel Jasper void ModuleMapParser::parseUseDecl() { 1905ba7f2f71SDaniel Jasper assert(Tok.is(MMToken::UseKeyword)); 1906ba7f2f71SDaniel Jasper consumeToken(); 1907ba7f2f71SDaniel Jasper // Parse the module-id. 1908ba7f2f71SDaniel Jasper ModuleId ParsedModuleId; 19093cd34c76SDaniel Jasper parseModuleId(ParsedModuleId); 1910ba7f2f71SDaniel Jasper 1911ba7f2f71SDaniel Jasper ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId); 1912ba7f2f71SDaniel Jasper } 1913ba7f2f71SDaniel Jasper 19146ddfca91SDouglas Gregor /// \brief Parse a link declaration. 19156ddfca91SDouglas Gregor /// 19166ddfca91SDouglas Gregor /// module-declaration: 19176ddfca91SDouglas Gregor /// 'link' 'framework'[opt] string-literal 19186ddfca91SDouglas Gregor void ModuleMapParser::parseLinkDecl() { 19196ddfca91SDouglas Gregor assert(Tok.is(MMToken::LinkKeyword)); 19206ddfca91SDouglas Gregor SourceLocation LinkLoc = consumeToken(); 19216ddfca91SDouglas Gregor 19226ddfca91SDouglas Gregor // Parse the optional 'framework' keyword. 19236ddfca91SDouglas Gregor bool IsFramework = false; 19246ddfca91SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 19256ddfca91SDouglas Gregor consumeToken(); 19266ddfca91SDouglas Gregor IsFramework = true; 19276ddfca91SDouglas Gregor } 19286ddfca91SDouglas Gregor 19296ddfca91SDouglas Gregor // Parse the library name 19306ddfca91SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 19316ddfca91SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) 19326ddfca91SDouglas Gregor << IsFramework << SourceRange(LinkLoc); 19336ddfca91SDouglas Gregor HadError = true; 19346ddfca91SDouglas Gregor return; 19356ddfca91SDouglas Gregor } 19366ddfca91SDouglas Gregor 19376ddfca91SDouglas Gregor std::string LibraryName = Tok.getString(); 19386ddfca91SDouglas Gregor consumeToken(); 19396ddfca91SDouglas Gregor ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, 19406ddfca91SDouglas Gregor IsFramework)); 19416ddfca91SDouglas Gregor } 19426ddfca91SDouglas Gregor 194335b13eceSDouglas Gregor /// \brief Parse a configuration macro declaration. 194435b13eceSDouglas Gregor /// 194535b13eceSDouglas Gregor /// module-declaration: 194635b13eceSDouglas Gregor /// 'config_macros' attributes[opt] config-macro-list? 194735b13eceSDouglas Gregor /// 194835b13eceSDouglas Gregor /// config-macro-list: 194935b13eceSDouglas Gregor /// identifier (',' identifier)? 195035b13eceSDouglas Gregor void ModuleMapParser::parseConfigMacros() { 195135b13eceSDouglas Gregor assert(Tok.is(MMToken::ConfigMacros)); 195235b13eceSDouglas Gregor SourceLocation ConfigMacrosLoc = consumeToken(); 195335b13eceSDouglas Gregor 195435b13eceSDouglas Gregor // Only top-level modules can have configuration macros. 195535b13eceSDouglas Gregor if (ActiveModule->Parent) { 195635b13eceSDouglas Gregor Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule); 195735b13eceSDouglas Gregor } 195835b13eceSDouglas Gregor 195935b13eceSDouglas Gregor // Parse the optional attributes. 196035b13eceSDouglas Gregor Attributes Attrs; 196135b13eceSDouglas Gregor parseOptionalAttributes(Attrs); 196235b13eceSDouglas Gregor if (Attrs.IsExhaustive && !ActiveModule->Parent) { 196335b13eceSDouglas Gregor ActiveModule->ConfigMacrosExhaustive = true; 196435b13eceSDouglas Gregor } 196535b13eceSDouglas Gregor 196635b13eceSDouglas Gregor // If we don't have an identifier, we're done. 1967306d8920SRichard Smith // FIXME: Support macros with the same name as a keyword here. 196835b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) 196935b13eceSDouglas Gregor return; 197035b13eceSDouglas Gregor 197135b13eceSDouglas Gregor // Consume the first identifier. 197235b13eceSDouglas Gregor if (!ActiveModule->Parent) { 197335b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 197435b13eceSDouglas Gregor } 197535b13eceSDouglas Gregor consumeToken(); 197635b13eceSDouglas Gregor 197735b13eceSDouglas Gregor do { 197835b13eceSDouglas Gregor // If there's a comma, consume it. 197935b13eceSDouglas Gregor if (!Tok.is(MMToken::Comma)) 198035b13eceSDouglas Gregor break; 198135b13eceSDouglas Gregor consumeToken(); 198235b13eceSDouglas Gregor 198335b13eceSDouglas Gregor // We expect to see a macro name here. 1984306d8920SRichard Smith // FIXME: Support macros with the same name as a keyword here. 198535b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 198635b13eceSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro); 198735b13eceSDouglas Gregor break; 198835b13eceSDouglas Gregor } 198935b13eceSDouglas Gregor 199035b13eceSDouglas Gregor // Consume the macro name. 199135b13eceSDouglas Gregor if (!ActiveModule->Parent) { 199235b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 199335b13eceSDouglas Gregor } 199435b13eceSDouglas Gregor consumeToken(); 199535b13eceSDouglas Gregor } while (true); 199635b13eceSDouglas Gregor } 199735b13eceSDouglas Gregor 1998fb912657SDouglas Gregor /// \brief Format a module-id into a string. 1999fb912657SDouglas Gregor static std::string formatModuleId(const ModuleId &Id) { 2000fb912657SDouglas Gregor std::string result; 2001fb912657SDouglas Gregor { 2002fb912657SDouglas Gregor llvm::raw_string_ostream OS(result); 2003fb912657SDouglas Gregor 2004fb912657SDouglas Gregor for (unsigned I = 0, N = Id.size(); I != N; ++I) { 2005fb912657SDouglas Gregor if (I) 2006fb912657SDouglas Gregor OS << "."; 2007fb912657SDouglas Gregor OS << Id[I].first; 2008fb912657SDouglas Gregor } 2009fb912657SDouglas Gregor } 2010fb912657SDouglas Gregor 2011fb912657SDouglas Gregor return result; 2012fb912657SDouglas Gregor } 2013fb912657SDouglas Gregor 2014fb912657SDouglas Gregor /// \brief Parse a conflict declaration. 2015fb912657SDouglas Gregor /// 2016fb912657SDouglas Gregor /// module-declaration: 2017fb912657SDouglas Gregor /// 'conflict' module-id ',' string-literal 2018fb912657SDouglas Gregor void ModuleMapParser::parseConflict() { 2019fb912657SDouglas Gregor assert(Tok.is(MMToken::Conflict)); 2020fb912657SDouglas Gregor SourceLocation ConflictLoc = consumeToken(); 2021fb912657SDouglas Gregor Module::UnresolvedConflict Conflict; 2022fb912657SDouglas Gregor 2023fb912657SDouglas Gregor // Parse the module-id. 2024fb912657SDouglas Gregor if (parseModuleId(Conflict.Id)) 2025fb912657SDouglas Gregor return; 2026fb912657SDouglas Gregor 2027fb912657SDouglas Gregor // Parse the ','. 2028fb912657SDouglas Gregor if (!Tok.is(MMToken::Comma)) { 2029fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma) 2030fb912657SDouglas Gregor << SourceRange(ConflictLoc); 2031fb912657SDouglas Gregor return; 2032fb912657SDouglas Gregor } 2033fb912657SDouglas Gregor consumeToken(); 2034fb912657SDouglas Gregor 2035fb912657SDouglas Gregor // Parse the message. 2036fb912657SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 2037fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message) 2038fb912657SDouglas Gregor << formatModuleId(Conflict.Id); 2039fb912657SDouglas Gregor return; 2040fb912657SDouglas Gregor } 2041fb912657SDouglas Gregor Conflict.Message = Tok.getString().str(); 2042fb912657SDouglas Gregor consumeToken(); 2043fb912657SDouglas Gregor 2044fb912657SDouglas Gregor // Add this unresolved conflict. 2045fb912657SDouglas Gregor ActiveModule->UnresolvedConflicts.push_back(Conflict); 2046fb912657SDouglas Gregor } 2047fb912657SDouglas Gregor 20486ddfca91SDouglas Gregor /// \brief Parse an inferred module declaration (wildcard modules). 20499194a91dSDouglas Gregor /// 20509194a91dSDouglas Gregor /// module-declaration: 20519194a91dSDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] 20529194a91dSDouglas Gregor /// { inferred-module-member* } 20539194a91dSDouglas Gregor /// 20549194a91dSDouglas Gregor /// inferred-module-member: 20559194a91dSDouglas Gregor /// 'export' '*' 20569194a91dSDouglas Gregor /// 'exclude' identifier 20579194a91dSDouglas Gregor void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { 205873441091SDouglas Gregor assert(Tok.is(MMToken::Star)); 205973441091SDouglas Gregor SourceLocation StarLoc = consumeToken(); 206073441091SDouglas Gregor bool Failed = false; 206173441091SDouglas Gregor 206273441091SDouglas Gregor // Inferred modules must be submodules. 20639194a91dSDouglas Gregor if (!ActiveModule && !Framework) { 206473441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 206573441091SDouglas Gregor Failed = true; 206673441091SDouglas Gregor } 206773441091SDouglas Gregor 20689194a91dSDouglas Gregor if (ActiveModule) { 2069524e33e1SDouglas Gregor // Inferred modules must have umbrella directories. 20704898cde4SBen Langmuir if (!Failed && ActiveModule->IsAvailable && 20714898cde4SBen Langmuir !ActiveModule->getUmbrellaDir()) { 207273441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 207373441091SDouglas Gregor Failed = true; 207473441091SDouglas Gregor } 207573441091SDouglas Gregor 207673441091SDouglas Gregor // Check for redefinition of an inferred module. 2077dd005f69SDouglas Gregor if (!Failed && ActiveModule->InferSubmodules) { 207873441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 2079dd005f69SDouglas Gregor if (ActiveModule->InferredSubmoduleLoc.isValid()) 2080dd005f69SDouglas Gregor Diags.Report(ActiveModule->InferredSubmoduleLoc, 208173441091SDouglas Gregor diag::note_mmap_prev_definition); 208273441091SDouglas Gregor Failed = true; 208373441091SDouglas Gregor } 208473441091SDouglas Gregor 20859194a91dSDouglas Gregor // Check for the 'framework' keyword, which is not permitted here. 20869194a91dSDouglas Gregor if (Framework) { 20879194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); 20889194a91dSDouglas Gregor Framework = false; 20899194a91dSDouglas Gregor } 20909194a91dSDouglas Gregor } else if (Explicit) { 20919194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); 20929194a91dSDouglas Gregor Explicit = false; 20939194a91dSDouglas Gregor } 20949194a91dSDouglas Gregor 209573441091SDouglas Gregor // If there were any problems with this inferred submodule, skip its body. 209673441091SDouglas Gregor if (Failed) { 209773441091SDouglas Gregor if (Tok.is(MMToken::LBrace)) { 209873441091SDouglas Gregor consumeToken(); 209973441091SDouglas Gregor skipUntil(MMToken::RBrace); 210073441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 210173441091SDouglas Gregor consumeToken(); 210273441091SDouglas Gregor } 210373441091SDouglas Gregor HadError = true; 210473441091SDouglas Gregor return; 210573441091SDouglas Gregor } 210673441091SDouglas Gregor 21079194a91dSDouglas Gregor // Parse optional attributes. 21084442605fSBill Wendling Attributes Attrs; 21099194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 21109194a91dSDouglas Gregor 21119194a91dSDouglas Gregor if (ActiveModule) { 211273441091SDouglas Gregor // Note that we have an inferred submodule. 2113dd005f69SDouglas Gregor ActiveModule->InferSubmodules = true; 2114dd005f69SDouglas Gregor ActiveModule->InferredSubmoduleLoc = StarLoc; 2115dd005f69SDouglas Gregor ActiveModule->InferExplicitSubmodules = Explicit; 21169194a91dSDouglas Gregor } else { 21179194a91dSDouglas Gregor // We'll be inferring framework modules for this directory. 21189194a91dSDouglas Gregor Map.InferredDirectories[Directory].InferModules = true; 2119*c1d88ea5SBen Langmuir Map.InferredDirectories[Directory].Attrs = Attrs; 2120beee15e7SBen Langmuir Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile; 2121131daca0SRichard Smith // FIXME: Handle the 'framework' keyword. 21229194a91dSDouglas Gregor } 212373441091SDouglas Gregor 212473441091SDouglas Gregor // Parse the opening brace. 212573441091SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 212673441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 212773441091SDouglas Gregor HadError = true; 212873441091SDouglas Gregor return; 212973441091SDouglas Gregor } 213073441091SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 213173441091SDouglas Gregor 213273441091SDouglas Gregor // Parse the body of the inferred submodule. 213373441091SDouglas Gregor bool Done = false; 213473441091SDouglas Gregor do { 213573441091SDouglas Gregor switch (Tok.Kind) { 213673441091SDouglas Gregor case MMToken::EndOfFile: 213773441091SDouglas Gregor case MMToken::RBrace: 213873441091SDouglas Gregor Done = true; 213973441091SDouglas Gregor break; 214073441091SDouglas Gregor 21419194a91dSDouglas Gregor case MMToken::ExcludeKeyword: { 21429194a91dSDouglas Gregor if (ActiveModule) { 21439194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2144d2d442caSCraig Topper << (ActiveModule != nullptr); 21459194a91dSDouglas Gregor consumeToken(); 21469194a91dSDouglas Gregor break; 21479194a91dSDouglas Gregor } 21489194a91dSDouglas Gregor 21499194a91dSDouglas Gregor consumeToken(); 2150306d8920SRichard Smith // FIXME: Support string-literal module names here. 21519194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 21529194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); 21539194a91dSDouglas Gregor break; 21549194a91dSDouglas Gregor } 21559194a91dSDouglas Gregor 21569194a91dSDouglas Gregor Map.InferredDirectories[Directory].ExcludedModules 21579194a91dSDouglas Gregor .push_back(Tok.getString()); 21589194a91dSDouglas Gregor consumeToken(); 21599194a91dSDouglas Gregor break; 21609194a91dSDouglas Gregor } 21619194a91dSDouglas Gregor 21629194a91dSDouglas Gregor case MMToken::ExportKeyword: 21639194a91dSDouglas Gregor if (!ActiveModule) { 21649194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2165d2d442caSCraig Topper << (ActiveModule != nullptr); 21669194a91dSDouglas Gregor consumeToken(); 21679194a91dSDouglas Gregor break; 21689194a91dSDouglas Gregor } 21699194a91dSDouglas Gregor 217073441091SDouglas Gregor consumeToken(); 217173441091SDouglas Gregor if (Tok.is(MMToken::Star)) 2172dd005f69SDouglas Gregor ActiveModule->InferExportWildcard = true; 217373441091SDouglas Gregor else 217473441091SDouglas Gregor Diags.Report(Tok.getLocation(), 217573441091SDouglas Gregor diag::err_mmap_expected_export_wildcard); 217673441091SDouglas Gregor consumeToken(); 217773441091SDouglas Gregor break; 217873441091SDouglas Gregor 217973441091SDouglas Gregor case MMToken::ExplicitKeyword: 218073441091SDouglas Gregor case MMToken::ModuleKeyword: 218173441091SDouglas Gregor case MMToken::HeaderKeyword: 2182b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 218373441091SDouglas Gregor case MMToken::UmbrellaKeyword: 218473441091SDouglas Gregor default: 21859194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2186d2d442caSCraig Topper << (ActiveModule != nullptr); 218773441091SDouglas Gregor consumeToken(); 218873441091SDouglas Gregor break; 218973441091SDouglas Gregor } 219073441091SDouglas Gregor } while (!Done); 219173441091SDouglas Gregor 219273441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 219373441091SDouglas Gregor consumeToken(); 219473441091SDouglas Gregor else { 219573441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 219673441091SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 219773441091SDouglas Gregor HadError = true; 219873441091SDouglas Gregor } 219973441091SDouglas Gregor } 220073441091SDouglas Gregor 22019194a91dSDouglas Gregor /// \brief Parse optional attributes. 22029194a91dSDouglas Gregor /// 22039194a91dSDouglas Gregor /// attributes: 22049194a91dSDouglas Gregor /// attribute attributes 22059194a91dSDouglas Gregor /// attribute 22069194a91dSDouglas Gregor /// 22079194a91dSDouglas Gregor /// attribute: 22089194a91dSDouglas Gregor /// [ identifier ] 22099194a91dSDouglas Gregor /// 22109194a91dSDouglas Gregor /// \param Attrs Will be filled in with the parsed attributes. 22119194a91dSDouglas Gregor /// 22129194a91dSDouglas Gregor /// \returns true if an error occurred, false otherwise. 22134442605fSBill Wendling bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { 22149194a91dSDouglas Gregor bool HadError = false; 22159194a91dSDouglas Gregor 22169194a91dSDouglas Gregor while (Tok.is(MMToken::LSquare)) { 22179194a91dSDouglas Gregor // Consume the '['. 22189194a91dSDouglas Gregor SourceLocation LSquareLoc = consumeToken(); 22199194a91dSDouglas Gregor 22209194a91dSDouglas Gregor // Check whether we have an attribute name here. 22219194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 22229194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 22239194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 22249194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 22259194a91dSDouglas Gregor consumeToken(); 22269194a91dSDouglas Gregor HadError = true; 22279194a91dSDouglas Gregor } 22289194a91dSDouglas Gregor 22299194a91dSDouglas Gregor // Decode the attribute name. 22309194a91dSDouglas Gregor AttributeKind Attribute 22319194a91dSDouglas Gregor = llvm::StringSwitch<AttributeKind>(Tok.getString()) 223235b13eceSDouglas Gregor .Case("exhaustive", AT_exhaustive) 223377944868SRichard Smith .Case("extern_c", AT_extern_c) 22349194a91dSDouglas Gregor .Case("system", AT_system) 22359194a91dSDouglas Gregor .Default(AT_unknown); 22369194a91dSDouglas Gregor switch (Attribute) { 22379194a91dSDouglas Gregor case AT_unknown: 22389194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 22399194a91dSDouglas Gregor << Tok.getString(); 22409194a91dSDouglas Gregor break; 22419194a91dSDouglas Gregor 22429194a91dSDouglas Gregor case AT_system: 22439194a91dSDouglas Gregor Attrs.IsSystem = true; 22449194a91dSDouglas Gregor break; 224535b13eceSDouglas Gregor 224677944868SRichard Smith case AT_extern_c: 224777944868SRichard Smith Attrs.IsExternC = true; 224877944868SRichard Smith break; 224977944868SRichard Smith 225035b13eceSDouglas Gregor case AT_exhaustive: 225135b13eceSDouglas Gregor Attrs.IsExhaustive = true; 225235b13eceSDouglas Gregor break; 22539194a91dSDouglas Gregor } 22549194a91dSDouglas Gregor consumeToken(); 22559194a91dSDouglas Gregor 22569194a91dSDouglas Gregor // Consume the ']'. 22579194a91dSDouglas Gregor if (!Tok.is(MMToken::RSquare)) { 22589194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 22599194a91dSDouglas Gregor Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 22609194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 22619194a91dSDouglas Gregor HadError = true; 22629194a91dSDouglas Gregor } 22639194a91dSDouglas Gregor 22649194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 22659194a91dSDouglas Gregor consumeToken(); 22669194a91dSDouglas Gregor } 22679194a91dSDouglas Gregor 22689194a91dSDouglas Gregor return HadError; 22699194a91dSDouglas Gregor } 22709194a91dSDouglas Gregor 2271718292f2SDouglas Gregor /// \brief Parse a module map file. 2272718292f2SDouglas Gregor /// 2273718292f2SDouglas Gregor /// module-map-file: 2274718292f2SDouglas Gregor /// module-declaration* 2275718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() { 2276718292f2SDouglas Gregor do { 2277718292f2SDouglas Gregor switch (Tok.Kind) { 2278718292f2SDouglas Gregor case MMToken::EndOfFile: 2279718292f2SDouglas Gregor return HadError; 2280718292f2SDouglas Gregor 2281e7ab3669SDouglas Gregor case MMToken::ExplicitKeyword: 228297292843SDaniel Jasper case MMToken::ExternKeyword: 2283718292f2SDouglas Gregor case MMToken::ModuleKeyword: 2284755b2055SDouglas Gregor case MMToken::FrameworkKeyword: 2285718292f2SDouglas Gregor parseModuleDecl(); 2286718292f2SDouglas Gregor break; 2287718292f2SDouglas Gregor 22881fb5c3a6SDouglas Gregor case MMToken::Comma: 228935b13eceSDouglas Gregor case MMToken::ConfigMacros: 2290fb912657SDouglas Gregor case MMToken::Conflict: 2291a3feee2aSRichard Smith case MMToken::Exclaim: 229259527666SDouglas Gregor case MMToken::ExcludeKeyword: 22932b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 2294718292f2SDouglas Gregor case MMToken::HeaderKeyword: 2295718292f2SDouglas Gregor case MMToken::Identifier: 2296718292f2SDouglas Gregor case MMToken::LBrace: 22976ddfca91SDouglas Gregor case MMToken::LinkKeyword: 2298a686e1b0SDouglas Gregor case MMToken::LSquare: 22992b82c2a5SDouglas Gregor case MMToken::Period: 2300b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 2301718292f2SDouglas Gregor case MMToken::RBrace: 2302a686e1b0SDouglas Gregor case MMToken::RSquare: 23031fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 23042b82c2a5SDouglas Gregor case MMToken::Star: 2305718292f2SDouglas Gregor case MMToken::StringLiteral: 2306b8afebe2SRichard Smith case MMToken::TextualKeyword: 2307718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 2308ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 2309718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 2310718292f2SDouglas Gregor HadError = true; 2311718292f2SDouglas Gregor consumeToken(); 2312718292f2SDouglas Gregor break; 2313718292f2SDouglas Gregor } 2314718292f2SDouglas Gregor } while (true); 2315718292f2SDouglas Gregor } 2316718292f2SDouglas Gregor 23179acb99e3SRichard Smith bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem, 23189acb99e3SRichard Smith const DirectoryEntry *Dir) { 23194ddf2221SDouglas Gregor llvm::DenseMap<const FileEntry *, bool>::iterator Known 23204ddf2221SDouglas Gregor = ParsedModuleMap.find(File); 23214ddf2221SDouglas Gregor if (Known != ParsedModuleMap.end()) 23224ddf2221SDouglas Gregor return Known->second; 23234ddf2221SDouglas Gregor 2324d2d442caSCraig Topper assert(Target && "Missing target information"); 2325cb69b57bSBen Langmuir auto FileCharacter = IsSystem ? SrcMgr::C_System : SrcMgr::C_User; 2326cb69b57bSBen Langmuir FileID ID = SourceMgr.createFileID(File, SourceLocation(), FileCharacter); 23271f76c4e8SManuel Klimek const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID); 2328718292f2SDouglas Gregor if (!Buffer) 23294ddf2221SDouglas Gregor return ParsedModuleMap[File] = true; 2330718292f2SDouglas Gregor 2331718292f2SDouglas Gregor // Parse this module map file. 23321f76c4e8SManuel Klimek Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts); 2333beee15e7SBen Langmuir ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir, 2334963c5535SDouglas Gregor BuiltinIncludeDir, IsSystem); 2335718292f2SDouglas Gregor bool Result = Parser.parseModuleMapFile(); 23364ddf2221SDouglas Gregor ParsedModuleMap[File] = Result; 2337718292f2SDouglas Gregor return Result; 2338718292f2SDouglas Gregor } 2339