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" 223a02247dSChandler Carruth #include "clang/Lex/LexDiagnostic.h" 233a02247dSChandler Carruth #include "clang/Lex/Lexer.h" 243a02247dSChandler Carruth #include "clang/Lex/LiteralSupport.h" 253a02247dSChandler Carruth #include "llvm/ADT/StringRef.h" 263a02247dSChandler Carruth #include "llvm/ADT/StringSwitch.h" 27718292f2SDouglas Gregor #include "llvm/Support/Allocator.h" 28e89dbc1dSDouglas Gregor #include "llvm/Support/FileSystem.h" 29718292f2SDouglas Gregor #include "llvm/Support/Host.h" 30552c169eSRafael Espindola #include "llvm/Support/Path.h" 31718292f2SDouglas Gregor #include "llvm/Support/raw_ostream.h" 3207c22b78SDouglas Gregor #include <stdlib.h> 3301c7cfa2SDouglas Gregor #if defined(LLVM_ON_UNIX) 34eadae014SDmitri Gribenko #include <limits.h> 3501c7cfa2SDouglas Gregor #endif 36718292f2SDouglas Gregor using namespace clang; 37718292f2SDouglas Gregor 382b82c2a5SDouglas Gregor Module::ExportDecl 392b82c2a5SDouglas Gregor ModuleMap::resolveExport(Module *Mod, 402b82c2a5SDouglas Gregor const Module::UnresolvedExportDecl &Unresolved, 41e4412640SArgyrios Kyrtzidis bool Complain) const { 42f5eedd05SDouglas Gregor // We may have just a wildcard. 43f5eedd05SDouglas Gregor if (Unresolved.Id.empty()) { 44f5eedd05SDouglas Gregor assert(Unresolved.Wildcard && "Invalid unresolved export"); 45f5eedd05SDouglas Gregor return Module::ExportDecl(0, true); 46f5eedd05SDouglas Gregor } 47f5eedd05SDouglas Gregor 48fb912657SDouglas Gregor // Resolve the module-id. 49fb912657SDouglas Gregor Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain); 50fb912657SDouglas Gregor if (!Context) 51fb912657SDouglas Gregor return Module::ExportDecl(); 52fb912657SDouglas Gregor 53fb912657SDouglas Gregor return Module::ExportDecl(Context, Unresolved.Wildcard); 54fb912657SDouglas Gregor } 55fb912657SDouglas Gregor 56fb912657SDouglas Gregor Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod, 57fb912657SDouglas Gregor bool Complain) const { 582b82c2a5SDouglas Gregor // Find the starting module. 59fb912657SDouglas Gregor Module *Context = lookupModuleUnqualified(Id[0].first, Mod); 602b82c2a5SDouglas Gregor if (!Context) { 612b82c2a5SDouglas Gregor if (Complain) 620761a8a0SDaniel Jasper Diags.Report(Id[0].second, diag::err_mmap_missing_module_unqualified) 63fb912657SDouglas Gregor << Id[0].first << Mod->getFullModuleName(); 642b82c2a5SDouglas Gregor 65fb912657SDouglas Gregor return 0; 662b82c2a5SDouglas Gregor } 672b82c2a5SDouglas Gregor 682b82c2a5SDouglas Gregor // Dig into the module path. 69fb912657SDouglas Gregor for (unsigned I = 1, N = Id.size(); I != N; ++I) { 70fb912657SDouglas Gregor Module *Sub = lookupModuleQualified(Id[I].first, Context); 712b82c2a5SDouglas Gregor if (!Sub) { 722b82c2a5SDouglas Gregor if (Complain) 730761a8a0SDaniel Jasper Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 74fb912657SDouglas Gregor << Id[I].first << Context->getFullModuleName() 75fb912657SDouglas Gregor << SourceRange(Id[0].second, Id[I-1].second); 762b82c2a5SDouglas Gregor 77fb912657SDouglas Gregor return 0; 782b82c2a5SDouglas Gregor } 792b82c2a5SDouglas Gregor 802b82c2a5SDouglas Gregor Context = Sub; 812b82c2a5SDouglas Gregor } 822b82c2a5SDouglas Gregor 83fb912657SDouglas Gregor return Context; 842b82c2a5SDouglas Gregor } 852b82c2a5SDouglas Gregor 860761a8a0SDaniel Jasper ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags, 87b146baabSArgyrios Kyrtzidis const LangOptions &LangOpts, const TargetInfo *Target, 88b146baabSArgyrios Kyrtzidis HeaderSearch &HeaderInfo) 890761a8a0SDaniel Jasper : SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target), 901f76c4e8SManuel Klimek HeaderInfo(HeaderInfo), BuiltinIncludeDir(0), CompilingModule(0), 910761a8a0SDaniel Jasper SourceModule(0) {} 92718292f2SDouglas Gregor 93718292f2SDouglas Gregor ModuleMap::~ModuleMap() { 945acdf59eSDouglas Gregor for (llvm::StringMap<Module *>::iterator I = Modules.begin(), 955acdf59eSDouglas Gregor IEnd = Modules.end(); 965acdf59eSDouglas Gregor I != IEnd; ++I) { 975acdf59eSDouglas Gregor delete I->getValue(); 985acdf59eSDouglas Gregor } 99718292f2SDouglas Gregor } 100718292f2SDouglas Gregor 10189929282SDouglas Gregor void ModuleMap::setTarget(const TargetInfo &Target) { 10289929282SDouglas Gregor assert((!this->Target || this->Target == &Target) && 10389929282SDouglas Gregor "Improper target override"); 10489929282SDouglas Gregor this->Target = &Target; 10589929282SDouglas Gregor } 10689929282SDouglas Gregor 107056396aeSDouglas Gregor /// \brief "Sanitize" a filename so that it can be used as an identifier. 108056396aeSDouglas Gregor static StringRef sanitizeFilenameAsIdentifier(StringRef Name, 109056396aeSDouglas Gregor SmallVectorImpl<char> &Buffer) { 110056396aeSDouglas Gregor if (Name.empty()) 111056396aeSDouglas Gregor return Name; 112056396aeSDouglas Gregor 113a7d03840SJordan Rose if (!isValidIdentifier(Name)) { 114056396aeSDouglas Gregor // If we don't already have something with the form of an identifier, 115056396aeSDouglas Gregor // create a buffer with the sanitized name. 116056396aeSDouglas Gregor Buffer.clear(); 117a7d03840SJordan Rose if (isDigit(Name[0])) 118056396aeSDouglas Gregor Buffer.push_back('_'); 119056396aeSDouglas Gregor Buffer.reserve(Buffer.size() + Name.size()); 120056396aeSDouglas Gregor for (unsigned I = 0, N = Name.size(); I != N; ++I) { 121a7d03840SJordan Rose if (isIdentifierBody(Name[I])) 122056396aeSDouglas Gregor Buffer.push_back(Name[I]); 123056396aeSDouglas Gregor else 124056396aeSDouglas Gregor Buffer.push_back('_'); 125056396aeSDouglas Gregor } 126056396aeSDouglas Gregor 127056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 128056396aeSDouglas Gregor } 129056396aeSDouglas Gregor 130056396aeSDouglas Gregor while (llvm::StringSwitch<bool>(Name) 131056396aeSDouglas Gregor #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true) 132056396aeSDouglas Gregor #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true) 133056396aeSDouglas Gregor #include "clang/Basic/TokenKinds.def" 134056396aeSDouglas Gregor .Default(false)) { 135056396aeSDouglas Gregor if (Name.data() != Buffer.data()) 136056396aeSDouglas Gregor Buffer.append(Name.begin(), Name.end()); 137056396aeSDouglas Gregor Buffer.push_back('_'); 138056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 139056396aeSDouglas Gregor } 140056396aeSDouglas Gregor 141056396aeSDouglas Gregor return Name; 142056396aeSDouglas Gregor } 143056396aeSDouglas Gregor 14434d52749SDouglas Gregor /// \brief Determine whether the given file name is the name of a builtin 14534d52749SDouglas Gregor /// header, supplied by Clang to replace, override, or augment existing system 14634d52749SDouglas Gregor /// headers. 14734d52749SDouglas Gregor static bool isBuiltinHeader(StringRef FileName) { 14834d52749SDouglas Gregor return llvm::StringSwitch<bool>(FileName) 14934d52749SDouglas Gregor .Case("float.h", true) 15034d52749SDouglas Gregor .Case("iso646.h", true) 15134d52749SDouglas Gregor .Case("limits.h", true) 15234d52749SDouglas Gregor .Case("stdalign.h", true) 15334d52749SDouglas Gregor .Case("stdarg.h", true) 15434d52749SDouglas Gregor .Case("stdbool.h", true) 15534d52749SDouglas Gregor .Case("stddef.h", true) 15634d52749SDouglas Gregor .Case("stdint.h", true) 15734d52749SDouglas Gregor .Case("tgmath.h", true) 15834d52749SDouglas Gregor .Case("unwind.h", true) 15934d52749SDouglas Gregor .Default(false); 16034d52749SDouglas Gregor } 16134d52749SDouglas Gregor 16292669ee4SDaniel Jasper ModuleMap::HeadersMap::iterator 16392669ee4SDaniel Jasper ModuleMap::findKnownHeader(const FileEntry *File) { 16459527666SDouglas Gregor HeadersMap::iterator Known = Headers.find(File); 1654eaf0a6cSDaniel Jasper if (Known == Headers.end() && File->getDir() == BuiltinIncludeDir && 1664eaf0a6cSDaniel Jasper isBuiltinHeader(llvm::sys::path::filename(File->getName()))) { 1674eaf0a6cSDaniel Jasper HeaderInfo.loadTopLevelSystemModules(); 16892669ee4SDaniel Jasper return Headers.find(File); 1694eaf0a6cSDaniel Jasper } 17092669ee4SDaniel Jasper return Known; 17192669ee4SDaniel Jasper } 17292669ee4SDaniel Jasper 173*4469138eSBen Langmuir ModuleMap::KnownHeader 174*4469138eSBen Langmuir ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File, 175*4469138eSBen Langmuir SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs) { 176*4469138eSBen Langmuir const DirectoryEntry *Dir = File->getDir(); 177*4469138eSBen Langmuir assert(Dir && "file in no directory"); 178*4469138eSBen Langmuir 179*4469138eSBen Langmuir // Note: as an egregious but useful hack we use the real path here, because 180*4469138eSBen Langmuir // frameworks moving from top-level frameworks to embedded frameworks tend 181*4469138eSBen Langmuir // to be symlinked from the top-level location to the embedded location, 182*4469138eSBen Langmuir // and we need to resolve lookups as if we had found the embedded location. 183*4469138eSBen Langmuir StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir); 184*4469138eSBen Langmuir 185*4469138eSBen Langmuir // Keep walking up the directory hierarchy, looking for a directory with 186*4469138eSBen Langmuir // an umbrella header. 187*4469138eSBen Langmuir do { 188*4469138eSBen Langmuir auto KnownDir = UmbrellaDirs.find(Dir); 189*4469138eSBen Langmuir if (KnownDir != UmbrellaDirs.end()) 190*4469138eSBen Langmuir return KnownHeader(KnownDir->second, NormalHeader); 191*4469138eSBen Langmuir 192*4469138eSBen Langmuir IntermediateDirs.push_back(Dir); 193*4469138eSBen Langmuir 194*4469138eSBen Langmuir // Retrieve our parent path. 195*4469138eSBen Langmuir DirName = llvm::sys::path::parent_path(DirName); 196*4469138eSBen Langmuir if (DirName.empty()) 197*4469138eSBen Langmuir break; 198*4469138eSBen Langmuir 199*4469138eSBen Langmuir // Resolve the parent path to a directory entry. 200*4469138eSBen Langmuir Dir = SourceMgr.getFileManager().getDirectory(DirName); 201*4469138eSBen Langmuir } while (Dir); 202*4469138eSBen Langmuir return KnownHeader(); 203*4469138eSBen Langmuir } 204*4469138eSBen Langmuir 20592669ee4SDaniel Jasper // Returns 'true' if 'RequestingModule directly uses 'RequestedModule'. 20692669ee4SDaniel Jasper static bool directlyUses(const Module *RequestingModule, 20792669ee4SDaniel Jasper const Module *RequestedModule) { 20892669ee4SDaniel Jasper return std::find(RequestingModule->DirectUses.begin(), 20992669ee4SDaniel Jasper RequestingModule->DirectUses.end(), 21092669ee4SDaniel Jasper RequestedModule) != RequestingModule->DirectUses.end(); 21192669ee4SDaniel Jasper } 21292669ee4SDaniel Jasper 21392669ee4SDaniel Jasper static bool violatesPrivateInclude(Module *RequestingModule, 21492669ee4SDaniel Jasper const FileEntry *IncFileEnt, 21592669ee4SDaniel Jasper ModuleMap::ModuleHeaderRole Role, 21692669ee4SDaniel Jasper Module *RequestedModule) { 21792669ee4SDaniel Jasper #ifndef NDEBUG 21892669ee4SDaniel Jasper // Check for consistency between the module header role 21992669ee4SDaniel Jasper // as obtained from the lookup and as obtained from the module. 22092669ee4SDaniel Jasper // This check is not cheap, so enable it only for debugging. 22192669ee4SDaniel Jasper SmallVectorImpl<const FileEntry *> &PvtHdrs 22292669ee4SDaniel Jasper = RequestedModule->PrivateHeaders; 22392669ee4SDaniel Jasper SmallVectorImpl<const FileEntry *>::iterator Look 22492669ee4SDaniel Jasper = std::find(PvtHdrs.begin(), PvtHdrs.end(), IncFileEnt); 22592669ee4SDaniel Jasper bool IsPrivate = Look != PvtHdrs.end(); 22692669ee4SDaniel Jasper assert((IsPrivate && Role == ModuleMap::PrivateHeader) 22792669ee4SDaniel Jasper || (!IsPrivate && Role != ModuleMap::PrivateHeader)); 22892669ee4SDaniel Jasper #endif 22992669ee4SDaniel Jasper return Role == ModuleMap::PrivateHeader && 23092669ee4SDaniel Jasper RequestedModule->getTopLevelModule() != RequestingModule; 23192669ee4SDaniel Jasper } 23292669ee4SDaniel Jasper 23392669ee4SDaniel Jasper void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule, 23492669ee4SDaniel Jasper SourceLocation FilenameLoc, 23592669ee4SDaniel Jasper StringRef Filename, 23692669ee4SDaniel Jasper const FileEntry *File) { 23792669ee4SDaniel Jasper // No errors for indirect modules. This may be a bit of a problem for modules 23892669ee4SDaniel Jasper // with no source files. 23992669ee4SDaniel Jasper if (RequestingModule != SourceModule) 24092669ee4SDaniel Jasper return; 24192669ee4SDaniel Jasper 24292669ee4SDaniel Jasper if (RequestingModule) 24392669ee4SDaniel Jasper resolveUses(RequestingModule, /*Complain=*/false); 24492669ee4SDaniel Jasper 24592669ee4SDaniel Jasper HeadersMap::iterator Known = findKnownHeader(File); 24692669ee4SDaniel Jasper if (Known == Headers.end()) 24792669ee4SDaniel Jasper return; 24892669ee4SDaniel Jasper 24992669ee4SDaniel Jasper Module *Private = NULL; 25092669ee4SDaniel Jasper Module *NotUsed = NULL; 25192669ee4SDaniel Jasper for (SmallVectorImpl<KnownHeader>::iterator I = Known->second.begin(), 25292669ee4SDaniel Jasper E = Known->second.end(); 25392669ee4SDaniel Jasper I != E; ++I) { 25492669ee4SDaniel Jasper // Excluded headers don't really belong to a module. 25592669ee4SDaniel Jasper if (I->getRole() == ModuleMap::ExcludedHeader) 25692669ee4SDaniel Jasper continue; 25792669ee4SDaniel Jasper 25892669ee4SDaniel Jasper // If 'File' is part of 'RequestingModule' we can definitely include it. 25992669ee4SDaniel Jasper if (I->getModule() == RequestingModule) 26092669ee4SDaniel Jasper return; 26192669ee4SDaniel Jasper 26292669ee4SDaniel Jasper // Remember private headers for later printing of a diagnostic. 26392669ee4SDaniel Jasper if (violatesPrivateInclude(RequestingModule, File, I->getRole(), 26492669ee4SDaniel Jasper I->getModule())) { 26592669ee4SDaniel Jasper Private = I->getModule(); 26692669ee4SDaniel Jasper continue; 26792669ee4SDaniel Jasper } 26892669ee4SDaniel Jasper 26992669ee4SDaniel Jasper // If uses need to be specified explicitly, we are only allowed to return 27092669ee4SDaniel Jasper // modules that are explicitly used by the requesting module. 27192669ee4SDaniel Jasper if (RequestingModule && LangOpts.ModulesDeclUse && 27292669ee4SDaniel Jasper !directlyUses(RequestingModule, I->getModule())) { 27392669ee4SDaniel Jasper NotUsed = I->getModule(); 27492669ee4SDaniel Jasper continue; 27592669ee4SDaniel Jasper } 27692669ee4SDaniel Jasper 27792669ee4SDaniel Jasper // We have found a module that we can happily use. 27892669ee4SDaniel Jasper return; 27992669ee4SDaniel Jasper } 28092669ee4SDaniel Jasper 28192669ee4SDaniel Jasper // We have found a header, but it is private. 28292669ee4SDaniel Jasper if (Private != NULL) { 28392669ee4SDaniel Jasper Diags.Report(FilenameLoc, diag::error_use_of_private_header_outside_module) 28492669ee4SDaniel Jasper << Filename; 28592669ee4SDaniel Jasper return; 28692669ee4SDaniel Jasper } 28792669ee4SDaniel Jasper 28892669ee4SDaniel Jasper // We have found a module, but we don't use it. 28992669ee4SDaniel Jasper if (NotUsed != NULL) { 29092669ee4SDaniel Jasper Diags.Report(FilenameLoc, diag::error_undeclared_use_of_module) 29192669ee4SDaniel Jasper << RequestingModule->getFullModuleName() << Filename; 29292669ee4SDaniel Jasper return; 29392669ee4SDaniel Jasper } 29492669ee4SDaniel Jasper 29592669ee4SDaniel Jasper // Headers for which we have not found a module are fine to include. 29692669ee4SDaniel Jasper } 29792669ee4SDaniel Jasper 29892669ee4SDaniel Jasper ModuleMap::KnownHeader 29992669ee4SDaniel Jasper ModuleMap::findModuleForHeader(const FileEntry *File, 30092669ee4SDaniel Jasper Module *RequestingModule) { 30192669ee4SDaniel Jasper HeadersMap::iterator Known = findKnownHeader(File); 3024eaf0a6cSDaniel Jasper 3031fb5c3a6SDouglas Gregor if (Known != Headers.end()) { 30497da9178SDaniel Jasper ModuleMap::KnownHeader Result = KnownHeader(); 3051fb5c3a6SDouglas Gregor 30697da9178SDaniel Jasper // Iterate over all modules that 'File' is part of to find the best fit. 30797da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::iterator I = Known->second.begin(), 30897da9178SDaniel Jasper E = Known->second.end(); 30997da9178SDaniel Jasper I != E; ++I) { 3104eaf0a6cSDaniel Jasper // Cannot use a module if the header is excluded in it. 3114eaf0a6cSDaniel Jasper if (I->getRole() == ModuleMap::ExcludedHeader) 3124eaf0a6cSDaniel Jasper continue; 3134eaf0a6cSDaniel Jasper 3144eaf0a6cSDaniel Jasper // Cannot use a module if it is unavailable. 3154eaf0a6cSDaniel Jasper if (!I->getModule()->isAvailable()) 31697da9178SDaniel Jasper continue; 31797da9178SDaniel Jasper 31897da9178SDaniel Jasper // If 'File' is part of 'RequestingModule', 'RequestingModule' is the 31997da9178SDaniel Jasper // module we are looking for. 32097da9178SDaniel Jasper if (I->getModule() == RequestingModule) 32197da9178SDaniel Jasper return *I; 32297da9178SDaniel Jasper 32397da9178SDaniel Jasper // If uses need to be specified explicitly, we are only allowed to return 32497da9178SDaniel Jasper // modules that are explicitly used by the requesting module. 32597da9178SDaniel Jasper if (RequestingModule && LangOpts.ModulesDeclUse && 32692669ee4SDaniel Jasper !directlyUses(RequestingModule, I->getModule())) 32797da9178SDaniel Jasper continue; 3284eaf0a6cSDaniel Jasper 32997da9178SDaniel Jasper Result = *I; 33097da9178SDaniel Jasper // If 'File' is a public header of this module, this is as good as we 33197da9178SDaniel Jasper // are going to get. 3328c71eba1SRichard Smith // FIXME: If we have a RequestingModule, we should prefer the header from 3338c71eba1SRichard Smith // that module. 33497da9178SDaniel Jasper if (I->getRole() == ModuleMap::NormalHeader) 33597da9178SDaniel Jasper break; 33697da9178SDaniel Jasper } 33797da9178SDaniel Jasper return Result; 3381fb5c3a6SDouglas Gregor } 339ab0c8a84SDouglas Gregor 340f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 341*4469138eSBen Langmuir KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs); 342*4469138eSBen Langmuir if (H) { 343*4469138eSBen Langmuir Module *Result = H.getModule(); 344930a85ccSDouglas Gregor 345930a85ccSDouglas Gregor // Search up the module stack until we find a module with an umbrella 34673141fa9SDouglas Gregor // directory. 347930a85ccSDouglas Gregor Module *UmbrellaModule = Result; 34873141fa9SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 349930a85ccSDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 350930a85ccSDouglas Gregor 351930a85ccSDouglas Gregor if (UmbrellaModule->InferSubmodules) { 352a89c5ac4SDouglas Gregor // Infer submodules for each of the directories we found between 353a89c5ac4SDouglas Gregor // the directory of the umbrella header and the directory where 354a89c5ac4SDouglas Gregor // the actual header is located. 3559458f82dSDouglas Gregor bool Explicit = UmbrellaModule->InferExplicitSubmodules; 3569458f82dSDouglas Gregor 3577033127bSDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 358a89c5ac4SDouglas Gregor // Find or create the module that corresponds to this directory name. 359056396aeSDouglas Gregor SmallString<32> NameBuf; 360056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 361*4469138eSBen Langmuir llvm::sys::path::stem(SkippedDirs[I-1]->getName()), NameBuf); 362a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 3639458f82dSDouglas Gregor Explicit).first; 364a89c5ac4SDouglas Gregor 365a89c5ac4SDouglas Gregor // Associate the module and the directory. 366a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I-1]] = Result; 367a89c5ac4SDouglas Gregor 368a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 369a89c5ac4SDouglas Gregor // wildcard to the set of exports. 370930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 371a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 372a89c5ac4SDouglas Gregor } 373a89c5ac4SDouglas Gregor 374a89c5ac4SDouglas Gregor // Infer a submodule with the same name as this header file. 375056396aeSDouglas Gregor SmallString<32> NameBuf; 376056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 377056396aeSDouglas Gregor llvm::sys::path::stem(File->getName()), NameBuf); 378a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 3799458f82dSDouglas Gregor Explicit).first; 3803c5305c1SArgyrios Kyrtzidis Result->addTopHeader(File); 381a89c5ac4SDouglas Gregor 382a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 383a89c5ac4SDouglas Gregor // wildcard to the set of exports. 384930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 385a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 386a89c5ac4SDouglas Gregor } else { 387a89c5ac4SDouglas Gregor // Record each of the directories we stepped through as being part of 388a89c5ac4SDouglas Gregor // the module we found, since the umbrella header covers them all. 389a89c5ac4SDouglas Gregor for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 390a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I]] = Result; 391a89c5ac4SDouglas Gregor } 392a89c5ac4SDouglas Gregor 39397da9178SDaniel Jasper Headers[File].push_back(KnownHeader(Result, NormalHeader)); 3941fb5c3a6SDouglas Gregor 3951fb5c3a6SDouglas Gregor // If a header corresponds to an unavailable module, don't report 3961fb5c3a6SDouglas Gregor // that it maps to anything. 3971fb5c3a6SDouglas Gregor if (!Result->isAvailable()) 398b53e5483SLawrence Crowl return KnownHeader(); 3991fb5c3a6SDouglas Gregor 40097da9178SDaniel Jasper return Headers[File].back(); 401a89c5ac4SDouglas Gregor } 402a89c5ac4SDouglas Gregor 403b53e5483SLawrence Crowl return KnownHeader(); 404ab0c8a84SDouglas Gregor } 405ab0c8a84SDouglas Gregor 406e4412640SArgyrios Kyrtzidis bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const { 40750996ce1SRichard Smith return isHeaderUnavailableInModule(Header, 0); 40850996ce1SRichard Smith } 40950996ce1SRichard Smith 41050996ce1SRichard Smith bool ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header, 41150996ce1SRichard Smith Module *RequestingModule) const { 412e4412640SArgyrios Kyrtzidis HeadersMap::const_iterator Known = Headers.find(Header); 41397da9178SDaniel Jasper if (Known != Headers.end()) { 41497da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::const_iterator 41597da9178SDaniel Jasper I = Known->second.begin(), 41697da9178SDaniel Jasper E = Known->second.end(); 41797da9178SDaniel Jasper I != E; ++I) { 41850996ce1SRichard Smith if (I->isAvailable() && (!RequestingModule || 41950996ce1SRichard Smith I->getModule()->isSubModuleOf(RequestingModule))) 42097da9178SDaniel Jasper return false; 42197da9178SDaniel Jasper } 42297da9178SDaniel Jasper return true; 42397da9178SDaniel Jasper } 4241fb5c3a6SDouglas Gregor 4251fb5c3a6SDouglas Gregor const DirectoryEntry *Dir = Header->getDir(); 426f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 4271fb5c3a6SDouglas Gregor StringRef DirName = Dir->getName(); 4281fb5c3a6SDouglas Gregor 42950996ce1SRichard Smith auto IsUnavailable = [&](const Module *M) { 43050996ce1SRichard Smith return !M->isAvailable() && (!RequestingModule || 43150996ce1SRichard Smith M->isSubModuleOf(RequestingModule)); 43250996ce1SRichard Smith }; 43350996ce1SRichard Smith 4341fb5c3a6SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 4351fb5c3a6SDouglas Gregor // an umbrella header. 4361fb5c3a6SDouglas Gregor do { 437e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir 4381fb5c3a6SDouglas Gregor = UmbrellaDirs.find(Dir); 4391fb5c3a6SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 4401fb5c3a6SDouglas Gregor Module *Found = KnownDir->second; 44150996ce1SRichard Smith if (IsUnavailable(Found)) 4421fb5c3a6SDouglas Gregor return true; 4431fb5c3a6SDouglas Gregor 4441fb5c3a6SDouglas Gregor // Search up the module stack until we find a module with an umbrella 4451fb5c3a6SDouglas Gregor // directory. 4461fb5c3a6SDouglas Gregor Module *UmbrellaModule = Found; 4471fb5c3a6SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 4481fb5c3a6SDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 4491fb5c3a6SDouglas Gregor 4501fb5c3a6SDouglas Gregor if (UmbrellaModule->InferSubmodules) { 4511fb5c3a6SDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 4521fb5c3a6SDouglas Gregor // Find or create the module that corresponds to this directory name. 453056396aeSDouglas Gregor SmallString<32> NameBuf; 454056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 455056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 456056396aeSDouglas Gregor NameBuf); 4571fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 4581fb5c3a6SDouglas Gregor if (!Found) 4591fb5c3a6SDouglas Gregor return false; 46050996ce1SRichard Smith if (IsUnavailable(Found)) 4611fb5c3a6SDouglas Gregor return true; 4621fb5c3a6SDouglas Gregor } 4631fb5c3a6SDouglas Gregor 4641fb5c3a6SDouglas Gregor // Infer a submodule with the same name as this header file. 465056396aeSDouglas Gregor SmallString<32> NameBuf; 466056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 467056396aeSDouglas Gregor llvm::sys::path::stem(Header->getName()), 468056396aeSDouglas Gregor NameBuf); 4691fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 4701fb5c3a6SDouglas Gregor if (!Found) 4711fb5c3a6SDouglas Gregor return false; 4721fb5c3a6SDouglas Gregor } 4731fb5c3a6SDouglas Gregor 47450996ce1SRichard Smith return IsUnavailable(Found); 4751fb5c3a6SDouglas Gregor } 4761fb5c3a6SDouglas Gregor 4771fb5c3a6SDouglas Gregor SkippedDirs.push_back(Dir); 4781fb5c3a6SDouglas Gregor 4791fb5c3a6SDouglas Gregor // Retrieve our parent path. 4801fb5c3a6SDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 4811fb5c3a6SDouglas Gregor if (DirName.empty()) 4821fb5c3a6SDouglas Gregor break; 4831fb5c3a6SDouglas Gregor 4841fb5c3a6SDouglas Gregor // Resolve the parent path to a directory entry. 4851f76c4e8SManuel Klimek Dir = SourceMgr.getFileManager().getDirectory(DirName); 4861fb5c3a6SDouglas Gregor } while (Dir); 4871fb5c3a6SDouglas Gregor 4881fb5c3a6SDouglas Gregor return false; 4891fb5c3a6SDouglas Gregor } 4901fb5c3a6SDouglas Gregor 491e4412640SArgyrios Kyrtzidis Module *ModuleMap::findModule(StringRef Name) const { 492e4412640SArgyrios Kyrtzidis llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name); 49388bdfb0eSDouglas Gregor if (Known != Modules.end()) 49488bdfb0eSDouglas Gregor return Known->getValue(); 49588bdfb0eSDouglas Gregor 49688bdfb0eSDouglas Gregor return 0; 49788bdfb0eSDouglas Gregor } 49888bdfb0eSDouglas Gregor 499e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleUnqualified(StringRef Name, 500e4412640SArgyrios Kyrtzidis Module *Context) const { 5012b82c2a5SDouglas Gregor for(; Context; Context = Context->Parent) { 5022b82c2a5SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Context)) 5032b82c2a5SDouglas Gregor return Sub; 5042b82c2a5SDouglas Gregor } 5052b82c2a5SDouglas Gregor 5062b82c2a5SDouglas Gregor return findModule(Name); 5072b82c2a5SDouglas Gregor } 5082b82c2a5SDouglas Gregor 509e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{ 5102b82c2a5SDouglas Gregor if (!Context) 5112b82c2a5SDouglas Gregor return findModule(Name); 5122b82c2a5SDouglas Gregor 513eb90e830SDouglas Gregor return Context->findSubmodule(Name); 5142b82c2a5SDouglas Gregor } 5152b82c2a5SDouglas Gregor 516de3ef502SDouglas Gregor std::pair<Module *, bool> 51769021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 51869021974SDouglas Gregor bool IsExplicit) { 51969021974SDouglas Gregor // Try to find an existing module with this name. 520eb90e830SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Parent)) 521eb90e830SDouglas Gregor return std::make_pair(Sub, false); 52269021974SDouglas Gregor 52369021974SDouglas Gregor // Create a new module with this name. 52469021974SDouglas Gregor Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework, 52569021974SDouglas Gregor IsExplicit); 526ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == Name) { 527ba7f2f71SDaniel Jasper SourceModule = Result; 528ba7f2f71SDaniel Jasper SourceModuleName = Name; 529ba7f2f71SDaniel Jasper } 5306f722b4eSArgyrios Kyrtzidis if (!Parent) { 53169021974SDouglas Gregor Modules[Name] = Result; 5326f722b4eSArgyrios Kyrtzidis if (!LangOpts.CurrentModule.empty() && !CompilingModule && 5336f722b4eSArgyrios Kyrtzidis Name == LangOpts.CurrentModule) { 5346f722b4eSArgyrios Kyrtzidis CompilingModule = Result; 5356f722b4eSArgyrios Kyrtzidis } 5366f722b4eSArgyrios Kyrtzidis } 53769021974SDouglas Gregor return std::make_pair(Result, true); 53869021974SDouglas Gregor } 53969021974SDouglas Gregor 5409194a91dSDouglas Gregor bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir, 541e4412640SArgyrios Kyrtzidis StringRef Name, bool &IsSystem) const { 5429194a91dSDouglas Gregor // Check whether we have already looked into the parent directory 5439194a91dSDouglas Gregor // for a module map. 544e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 5459194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 5469194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) 5479194a91dSDouglas Gregor return false; 5489194a91dSDouglas Gregor 5499194a91dSDouglas Gregor if (!inferred->second.InferModules) 5509194a91dSDouglas Gregor return false; 5519194a91dSDouglas Gregor 5529194a91dSDouglas Gregor // We're allowed to infer for this directory, but make sure it's okay 5539194a91dSDouglas Gregor // to infer this particular module. 5549194a91dSDouglas Gregor bool canInfer = std::find(inferred->second.ExcludedModules.begin(), 5559194a91dSDouglas Gregor inferred->second.ExcludedModules.end(), 5569194a91dSDouglas Gregor Name) == inferred->second.ExcludedModules.end(); 5579194a91dSDouglas Gregor 5589194a91dSDouglas Gregor if (canInfer && inferred->second.InferSystemModules) 5599194a91dSDouglas Gregor IsSystem = true; 5609194a91dSDouglas Gregor 5619194a91dSDouglas Gregor return canInfer; 5629194a91dSDouglas Gregor } 5639194a91dSDouglas Gregor 56411dfe6feSDouglas Gregor /// \brief For a framework module, infer the framework against which we 56511dfe6feSDouglas Gregor /// should link. 56611dfe6feSDouglas Gregor static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir, 56711dfe6feSDouglas Gregor FileManager &FileMgr) { 56811dfe6feSDouglas Gregor assert(Mod->IsFramework && "Can only infer linking for framework modules"); 56911dfe6feSDouglas Gregor assert(!Mod->isSubFramework() && 57011dfe6feSDouglas Gregor "Can only infer linking for top-level frameworks"); 57111dfe6feSDouglas Gregor 57211dfe6feSDouglas Gregor SmallString<128> LibName; 57311dfe6feSDouglas Gregor LibName += FrameworkDir->getName(); 57411dfe6feSDouglas Gregor llvm::sys::path::append(LibName, Mod->Name); 57511dfe6feSDouglas Gregor if (FileMgr.getFile(LibName)) { 57611dfe6feSDouglas Gregor Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name, 57711dfe6feSDouglas Gregor /*IsFramework=*/true)); 57811dfe6feSDouglas Gregor } 57911dfe6feSDouglas Gregor } 58011dfe6feSDouglas Gregor 581de3ef502SDouglas Gregor Module * 58256c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName, 583e89dbc1dSDouglas Gregor const DirectoryEntry *FrameworkDir, 584a686e1b0SDouglas Gregor bool IsSystem, 585e89dbc1dSDouglas Gregor Module *Parent) { 58656c64013SDouglas Gregor // Check whether we've already found this module. 587e89dbc1dSDouglas Gregor if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 588e89dbc1dSDouglas Gregor return Mod; 589e89dbc1dSDouglas Gregor 5901f76c4e8SManuel Klimek FileManager &FileMgr = SourceMgr.getFileManager(); 59156c64013SDouglas Gregor 5929194a91dSDouglas Gregor // If the framework has a parent path from which we're allowed to infer 5939194a91dSDouglas Gregor // a framework module, do so. 5949194a91dSDouglas Gregor if (!Parent) { 5954ddf2221SDouglas Gregor // Determine whether we're allowed to infer a module map. 596e00c8b20SDouglas Gregor 5974ddf2221SDouglas Gregor // Note: as an egregious but useful hack we use the real path here, because 5984ddf2221SDouglas Gregor // we might be looking at an embedded framework that symlinks out to a 5994ddf2221SDouglas Gregor // top-level framework, and we need to infer as if we were naming the 6004ddf2221SDouglas Gregor // top-level framework. 601e00c8b20SDouglas Gregor StringRef FrameworkDirName 6021f76c4e8SManuel Klimek = SourceMgr.getFileManager().getCanonicalName(FrameworkDir); 6034ddf2221SDouglas Gregor 6049194a91dSDouglas Gregor bool canInfer = false; 6054ddf2221SDouglas Gregor if (llvm::sys::path::has_parent_path(FrameworkDirName)) { 6069194a91dSDouglas Gregor // Figure out the parent path. 6074ddf2221SDouglas Gregor StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); 6089194a91dSDouglas Gregor if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) { 6099194a91dSDouglas Gregor // Check whether we have already looked into the parent directory 6109194a91dSDouglas Gregor // for a module map. 611e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 6129194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 6139194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) { 6149194a91dSDouglas Gregor // We haven't looked here before. Load a module map, if there is 6159194a91dSDouglas Gregor // one. 616984e1df7SBen Langmuir bool IsFrameworkDir = Parent.endswith(".framework"); 617984e1df7SBen Langmuir if (const FileEntry *ModMapFile = 618984e1df7SBen Langmuir HeaderInfo.lookupModuleMapFile(ParentDir, IsFrameworkDir)) { 619963c5535SDouglas Gregor parseModuleMapFile(ModMapFile, IsSystem); 6209194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 6219194a91dSDouglas Gregor } 6229194a91dSDouglas Gregor 6239194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) 6249194a91dSDouglas Gregor inferred = InferredDirectories.insert( 6259194a91dSDouglas Gregor std::make_pair(ParentDir, InferredDirectory())).first; 6269194a91dSDouglas Gregor } 6279194a91dSDouglas Gregor 6289194a91dSDouglas Gregor if (inferred->second.InferModules) { 6299194a91dSDouglas Gregor // We're allowed to infer for this directory, but make sure it's okay 6309194a91dSDouglas Gregor // to infer this particular module. 6314ddf2221SDouglas Gregor StringRef Name = llvm::sys::path::stem(FrameworkDirName); 6329194a91dSDouglas Gregor canInfer = std::find(inferred->second.ExcludedModules.begin(), 6339194a91dSDouglas Gregor inferred->second.ExcludedModules.end(), 6349194a91dSDouglas Gregor Name) == inferred->second.ExcludedModules.end(); 6359194a91dSDouglas Gregor 6369194a91dSDouglas Gregor if (inferred->second.InferSystemModules) 6379194a91dSDouglas Gregor IsSystem = true; 6389194a91dSDouglas Gregor } 6399194a91dSDouglas Gregor } 6409194a91dSDouglas Gregor } 6419194a91dSDouglas Gregor 6429194a91dSDouglas Gregor // If we're not allowed to infer a framework module, don't. 6439194a91dSDouglas Gregor if (!canInfer) 6449194a91dSDouglas Gregor return 0; 6459194a91dSDouglas Gregor } 6469194a91dSDouglas Gregor 6479194a91dSDouglas Gregor 64856c64013SDouglas Gregor // Look for an umbrella header. 6492c1dd271SDylan Noblesmith SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 65017381a06SBenjamin Kramer llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h"); 651e89dbc1dSDouglas Gregor const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 65256c64013SDouglas Gregor 65356c64013SDouglas Gregor // FIXME: If there's no umbrella header, we could probably scan the 65456c64013SDouglas Gregor // framework to load *everything*. But, it's not clear that this is a good 65556c64013SDouglas Gregor // idea. 65656c64013SDouglas Gregor if (!UmbrellaHeader) 65756c64013SDouglas Gregor return 0; 65856c64013SDouglas Gregor 659e89dbc1dSDouglas Gregor Module *Result = new Module(ModuleName, SourceLocation(), Parent, 660e89dbc1dSDouglas Gregor /*IsFramework=*/true, /*IsExplicit=*/false); 661ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == ModuleName) { 662ba7f2f71SDaniel Jasper SourceModule = Result; 663ba7f2f71SDaniel Jasper SourceModuleName = ModuleName; 664ba7f2f71SDaniel Jasper } 665a686e1b0SDouglas Gregor if (IsSystem) 666a686e1b0SDouglas Gregor Result->IsSystem = IsSystem; 667a686e1b0SDouglas Gregor 668eb90e830SDouglas Gregor if (!Parent) 669e89dbc1dSDouglas Gregor Modules[ModuleName] = Result; 670e89dbc1dSDouglas Gregor 671322f633cSDouglas Gregor // umbrella header "umbrella-header-name" 67273141fa9SDouglas Gregor Result->Umbrella = UmbrellaHeader; 67397da9178SDaniel Jasper Headers[UmbrellaHeader].push_back(KnownHeader(Result, NormalHeader)); 6744dc71835SDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Result; 675d8bd7537SDouglas Gregor 676d8bd7537SDouglas Gregor // export * 677d8bd7537SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 678d8bd7537SDouglas Gregor 679a89c5ac4SDouglas Gregor // module * { export * } 680a89c5ac4SDouglas Gregor Result->InferSubmodules = true; 681a89c5ac4SDouglas Gregor Result->InferExportWildcard = true; 682a89c5ac4SDouglas Gregor 683e89dbc1dSDouglas Gregor // Look for subframeworks. 684e89dbc1dSDouglas Gregor llvm::error_code EC; 6852c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirName 686ddaa69cbSDouglas Gregor = StringRef(FrameworkDir->getName()); 687e89dbc1dSDouglas Gregor llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 6882d4d8cb3SBenjamin Kramer llvm::sys::path::native(SubframeworksDirName); 689ddaa69cbSDouglas Gregor for (llvm::sys::fs::directory_iterator 6902d4d8cb3SBenjamin Kramer Dir(SubframeworksDirName.str(), EC), DirEnd; 691e89dbc1dSDouglas Gregor Dir != DirEnd && !EC; Dir.increment(EC)) { 692e89dbc1dSDouglas Gregor if (!StringRef(Dir->path()).endswith(".framework")) 693e89dbc1dSDouglas Gregor continue; 694f2161a70SDouglas Gregor 695e89dbc1dSDouglas Gregor if (const DirectoryEntry *SubframeworkDir 696e89dbc1dSDouglas Gregor = FileMgr.getDirectory(Dir->path())) { 69707c22b78SDouglas Gregor // Note: as an egregious but useful hack, we use the real path here and 69807c22b78SDouglas Gregor // check whether it is actually a subdirectory of the parent directory. 69907c22b78SDouglas Gregor // This will not be the case if the 'subframework' is actually a symlink 70007c22b78SDouglas Gregor // out to a top-level framework. 701e00c8b20SDouglas Gregor StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir); 70207c22b78SDouglas Gregor bool FoundParent = false; 70307c22b78SDouglas Gregor do { 70407c22b78SDouglas Gregor // Get the parent directory name. 70507c22b78SDouglas Gregor SubframeworkDirName 70607c22b78SDouglas Gregor = llvm::sys::path::parent_path(SubframeworkDirName); 70707c22b78SDouglas Gregor if (SubframeworkDirName.empty()) 70807c22b78SDouglas Gregor break; 70907c22b78SDouglas Gregor 71007c22b78SDouglas Gregor if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { 71107c22b78SDouglas Gregor FoundParent = true; 71207c22b78SDouglas Gregor break; 71307c22b78SDouglas Gregor } 71407c22b78SDouglas Gregor } while (true); 71507c22b78SDouglas Gregor 71607c22b78SDouglas Gregor if (!FoundParent) 71707c22b78SDouglas Gregor continue; 71807c22b78SDouglas Gregor 719e89dbc1dSDouglas Gregor // FIXME: Do we want to warn about subframeworks without umbrella headers? 720056396aeSDouglas Gregor SmallString<32> NameBuf; 721056396aeSDouglas Gregor inferFrameworkModule(sanitizeFilenameAsIdentifier( 722056396aeSDouglas Gregor llvm::sys::path::stem(Dir->path()), NameBuf), 723056396aeSDouglas Gregor SubframeworkDir, IsSystem, Result); 724e89dbc1dSDouglas Gregor } 725e89dbc1dSDouglas Gregor } 726e89dbc1dSDouglas Gregor 72711dfe6feSDouglas Gregor // If the module is a top-level framework, automatically link against the 72811dfe6feSDouglas Gregor // framework. 72911dfe6feSDouglas Gregor if (!Result->isSubFramework()) { 73011dfe6feSDouglas Gregor inferFrameworkLink(Result, FrameworkDir, FileMgr); 73111dfe6feSDouglas Gregor } 73211dfe6feSDouglas Gregor 73356c64013SDouglas Gregor return Result; 73456c64013SDouglas Gregor } 73556c64013SDouglas Gregor 736a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ 73797da9178SDaniel Jasper Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader)); 73873141fa9SDouglas Gregor Mod->Umbrella = UmbrellaHeader; 7397033127bSDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; 740a89c5ac4SDouglas Gregor } 741a89c5ac4SDouglas Gregor 742524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) { 743524e33e1SDouglas Gregor Mod->Umbrella = UmbrellaDir; 744524e33e1SDouglas Gregor UmbrellaDirs[UmbrellaDir] = Mod; 745524e33e1SDouglas Gregor } 746524e33e1SDouglas Gregor 74759527666SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header, 748b53e5483SLawrence Crowl ModuleHeaderRole Role) { 749b53e5483SLawrence Crowl if (Role == ExcludedHeader) { 75059527666SDouglas Gregor Mod->ExcludedHeaders.push_back(Header); 751b146baabSArgyrios Kyrtzidis } else { 752b53e5483SLawrence Crowl if (Role == PrivateHeader) 753b53e5483SLawrence Crowl Mod->PrivateHeaders.push_back(Header); 754b53e5483SLawrence Crowl else 755b53e5483SLawrence Crowl Mod->NormalHeaders.push_back(Header); 7566f722b4eSArgyrios Kyrtzidis bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule; 757b53e5483SLawrence Crowl HeaderInfo.MarkFileModuleHeader(Header, Role, isCompilingModuleHeader); 758b146baabSArgyrios Kyrtzidis } 75997da9178SDaniel Jasper Headers[Header].push_back(KnownHeader(Mod, Role)); 760a89c5ac4SDouglas Gregor } 761a89c5ac4SDouglas Gregor 762514b636aSDouglas Gregor const FileEntry * 763e4412640SArgyrios Kyrtzidis ModuleMap::getContainingModuleMapFile(Module *Module) const { 7641f76c4e8SManuel Klimek if (Module->DefinitionLoc.isInvalid()) 765514b636aSDouglas Gregor return 0; 766514b636aSDouglas Gregor 7671f76c4e8SManuel Klimek return SourceMgr.getFileEntryForID( 7681f76c4e8SManuel Klimek SourceMgr.getFileID(Module->DefinitionLoc)); 769514b636aSDouglas Gregor } 770514b636aSDouglas Gregor 771718292f2SDouglas Gregor void ModuleMap::dump() { 772718292f2SDouglas Gregor llvm::errs() << "Modules:"; 773718292f2SDouglas Gregor for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 774718292f2SDouglas Gregor MEnd = Modules.end(); 775718292f2SDouglas Gregor M != MEnd; ++M) 776d28d1b8dSDouglas Gregor M->getValue()->print(llvm::errs(), 2); 777718292f2SDouglas Gregor 778718292f2SDouglas Gregor llvm::errs() << "Headers:"; 77959527666SDouglas Gregor for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 780718292f2SDouglas Gregor H != HEnd; ++H) { 78197da9178SDaniel Jasper llvm::errs() << " \"" << H->first->getName() << "\" -> "; 78297da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(), 78397da9178SDaniel Jasper E = H->second.end(); 78497da9178SDaniel Jasper I != E; ++I) { 78597da9178SDaniel Jasper if (I != H->second.begin()) 78697da9178SDaniel Jasper llvm::errs() << ","; 78797da9178SDaniel Jasper llvm::errs() << I->getModule()->getFullModuleName(); 78897da9178SDaniel Jasper } 78997da9178SDaniel Jasper llvm::errs() << "\n"; 790718292f2SDouglas Gregor } 791718292f2SDouglas Gregor } 792718292f2SDouglas Gregor 7932b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 7942b82c2a5SDouglas Gregor bool HadError = false; 7952b82c2a5SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { 7962b82c2a5SDouglas Gregor Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 7972b82c2a5SDouglas Gregor Complain); 798f5eedd05SDouglas Gregor if (Export.getPointer() || Export.getInt()) 7992b82c2a5SDouglas Gregor Mod->Exports.push_back(Export); 8002b82c2a5SDouglas Gregor else 8012b82c2a5SDouglas Gregor HadError = true; 8022b82c2a5SDouglas Gregor } 8032b82c2a5SDouglas Gregor Mod->UnresolvedExports.clear(); 8042b82c2a5SDouglas Gregor return HadError; 8052b82c2a5SDouglas Gregor } 8062b82c2a5SDouglas Gregor 807ba7f2f71SDaniel Jasper bool ModuleMap::resolveUses(Module *Mod, bool Complain) { 808ba7f2f71SDaniel Jasper bool HadError = false; 809ba7f2f71SDaniel Jasper for (unsigned I = 0, N = Mod->UnresolvedDirectUses.size(); I != N; ++I) { 810ba7f2f71SDaniel Jasper Module *DirectUse = 811ba7f2f71SDaniel Jasper resolveModuleId(Mod->UnresolvedDirectUses[I], Mod, Complain); 812ba7f2f71SDaniel Jasper if (DirectUse) 813ba7f2f71SDaniel Jasper Mod->DirectUses.push_back(DirectUse); 814ba7f2f71SDaniel Jasper else 815ba7f2f71SDaniel Jasper HadError = true; 816ba7f2f71SDaniel Jasper } 817ba7f2f71SDaniel Jasper Mod->UnresolvedDirectUses.clear(); 818ba7f2f71SDaniel Jasper return HadError; 819ba7f2f71SDaniel Jasper } 820ba7f2f71SDaniel Jasper 821fb912657SDouglas Gregor bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) { 822fb912657SDouglas Gregor bool HadError = false; 823fb912657SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) { 824fb912657SDouglas Gregor Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id, 825fb912657SDouglas Gregor Mod, Complain); 826fb912657SDouglas Gregor if (!OtherMod) { 827fb912657SDouglas Gregor HadError = true; 828fb912657SDouglas Gregor continue; 829fb912657SDouglas Gregor } 830fb912657SDouglas Gregor 831fb912657SDouglas Gregor Module::Conflict Conflict; 832fb912657SDouglas Gregor Conflict.Other = OtherMod; 833fb912657SDouglas Gregor Conflict.Message = Mod->UnresolvedConflicts[I].Message; 834fb912657SDouglas Gregor Mod->Conflicts.push_back(Conflict); 835fb912657SDouglas Gregor } 836fb912657SDouglas Gregor Mod->UnresolvedConflicts.clear(); 837fb912657SDouglas Gregor return HadError; 838fb912657SDouglas Gregor } 839fb912657SDouglas Gregor 8400093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 8410093b3c7SDouglas Gregor if (Loc.isInvalid()) 8420093b3c7SDouglas Gregor return 0; 8430093b3c7SDouglas Gregor 8440093b3c7SDouglas Gregor // Use the expansion location to determine which module we're in. 8450093b3c7SDouglas Gregor FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 8460093b3c7SDouglas Gregor if (!ExpansionLoc.isFileID()) 8470093b3c7SDouglas Gregor return 0; 8480093b3c7SDouglas Gregor 8490093b3c7SDouglas Gregor 8500093b3c7SDouglas Gregor const SourceManager &SrcMgr = Loc.getManager(); 8510093b3c7SDouglas Gregor FileID ExpansionFileID = ExpansionLoc.getFileID(); 852224d8a74SDouglas Gregor 853224d8a74SDouglas Gregor while (const FileEntry *ExpansionFile 854224d8a74SDouglas Gregor = SrcMgr.getFileEntryForID(ExpansionFileID)) { 855224d8a74SDouglas Gregor // Find the module that owns this header (if any). 856b53e5483SLawrence Crowl if (Module *Mod = findModuleForHeader(ExpansionFile).getModule()) 857224d8a74SDouglas Gregor return Mod; 858224d8a74SDouglas Gregor 859224d8a74SDouglas Gregor // No module owns this header, so look up the inclusion chain to see if 860224d8a74SDouglas Gregor // any included header has an associated module. 861224d8a74SDouglas Gregor SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); 862224d8a74SDouglas Gregor if (IncludeLoc.isInvalid()) 8630093b3c7SDouglas Gregor return 0; 8640093b3c7SDouglas Gregor 865224d8a74SDouglas Gregor ExpansionFileID = SrcMgr.getFileID(IncludeLoc); 866224d8a74SDouglas Gregor } 867224d8a74SDouglas Gregor 868224d8a74SDouglas Gregor return 0; 8690093b3c7SDouglas Gregor } 8700093b3c7SDouglas Gregor 871718292f2SDouglas Gregor //----------------------------------------------------------------------------// 872718292f2SDouglas Gregor // Module map file parser 873718292f2SDouglas Gregor //----------------------------------------------------------------------------// 874718292f2SDouglas Gregor 875718292f2SDouglas Gregor namespace clang { 876718292f2SDouglas Gregor /// \brief A token in a module map file. 877718292f2SDouglas Gregor struct MMToken { 878718292f2SDouglas Gregor enum TokenKind { 8791fb5c3a6SDouglas Gregor Comma, 88035b13eceSDouglas Gregor ConfigMacros, 881fb912657SDouglas Gregor Conflict, 882718292f2SDouglas Gregor EndOfFile, 883718292f2SDouglas Gregor HeaderKeyword, 884718292f2SDouglas Gregor Identifier, 885a3feee2aSRichard Smith Exclaim, 88659527666SDouglas Gregor ExcludeKeyword, 887718292f2SDouglas Gregor ExplicitKeyword, 8882b82c2a5SDouglas Gregor ExportKeyword, 88997292843SDaniel Jasper ExternKeyword, 890755b2055SDouglas Gregor FrameworkKeyword, 8916ddfca91SDouglas Gregor LinkKeyword, 892718292f2SDouglas Gregor ModuleKeyword, 8932b82c2a5SDouglas Gregor Period, 894b53e5483SLawrence Crowl PrivateKeyword, 895718292f2SDouglas Gregor UmbrellaKeyword, 896ba7f2f71SDaniel Jasper UseKeyword, 8971fb5c3a6SDouglas Gregor RequiresKeyword, 8982b82c2a5SDouglas Gregor Star, 899718292f2SDouglas Gregor StringLiteral, 900718292f2SDouglas Gregor LBrace, 901a686e1b0SDouglas Gregor RBrace, 902a686e1b0SDouglas Gregor LSquare, 903a686e1b0SDouglas Gregor RSquare 904718292f2SDouglas Gregor } Kind; 905718292f2SDouglas Gregor 906718292f2SDouglas Gregor unsigned Location; 907718292f2SDouglas Gregor unsigned StringLength; 908718292f2SDouglas Gregor const char *StringData; 909718292f2SDouglas Gregor 910718292f2SDouglas Gregor void clear() { 911718292f2SDouglas Gregor Kind = EndOfFile; 912718292f2SDouglas Gregor Location = 0; 913718292f2SDouglas Gregor StringLength = 0; 914718292f2SDouglas Gregor StringData = 0; 915718292f2SDouglas Gregor } 916718292f2SDouglas Gregor 917718292f2SDouglas Gregor bool is(TokenKind K) const { return Kind == K; } 918718292f2SDouglas Gregor 919718292f2SDouglas Gregor SourceLocation getLocation() const { 920718292f2SDouglas Gregor return SourceLocation::getFromRawEncoding(Location); 921718292f2SDouglas Gregor } 922718292f2SDouglas Gregor 923718292f2SDouglas Gregor StringRef getString() const { 924718292f2SDouglas Gregor return StringRef(StringData, StringLength); 925718292f2SDouglas Gregor } 926718292f2SDouglas Gregor }; 927718292f2SDouglas Gregor 9289194a91dSDouglas Gregor /// \brief The set of attributes that can be attached to a module. 9294442605fSBill Wendling struct Attributes { 93077944868SRichard Smith Attributes() : IsSystem(), IsExternC(), IsExhaustive() { } 9319194a91dSDouglas Gregor 9329194a91dSDouglas Gregor /// \brief Whether this is a system module. 9339194a91dSDouglas Gregor unsigned IsSystem : 1; 93435b13eceSDouglas Gregor 93577944868SRichard Smith /// \brief Whether this is an extern "C" module. 93677944868SRichard Smith unsigned IsExternC : 1; 93777944868SRichard Smith 93835b13eceSDouglas Gregor /// \brief Whether this is an exhaustive set of configuration macros. 93935b13eceSDouglas Gregor unsigned IsExhaustive : 1; 9409194a91dSDouglas Gregor }; 9419194a91dSDouglas Gregor 9429194a91dSDouglas Gregor 943718292f2SDouglas Gregor class ModuleMapParser { 944718292f2SDouglas Gregor Lexer &L; 945718292f2SDouglas Gregor SourceManager &SourceMgr; 946bc10b9fbSDouglas Gregor 947bc10b9fbSDouglas Gregor /// \brief Default target information, used only for string literal 948bc10b9fbSDouglas Gregor /// parsing. 949bc10b9fbSDouglas Gregor const TargetInfo *Target; 950bc10b9fbSDouglas Gregor 951718292f2SDouglas Gregor DiagnosticsEngine &Diags; 952718292f2SDouglas Gregor ModuleMap ⤅ 953718292f2SDouglas Gregor 9545257fc63SDouglas Gregor /// \brief The directory that this module map resides in. 9555257fc63SDouglas Gregor const DirectoryEntry *Directory; 9565257fc63SDouglas Gregor 9573ec6663bSDouglas Gregor /// \brief The directory containing Clang-supplied headers. 9583ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir; 9593ec6663bSDouglas Gregor 960963c5535SDouglas Gregor /// \brief Whether this module map is in a system header directory. 961963c5535SDouglas Gregor bool IsSystem; 962963c5535SDouglas Gregor 963718292f2SDouglas Gregor /// \brief Whether an error occurred. 964718292f2SDouglas Gregor bool HadError; 965718292f2SDouglas Gregor 966718292f2SDouglas Gregor /// \brief Stores string data for the various string literals referenced 967718292f2SDouglas Gregor /// during parsing. 968718292f2SDouglas Gregor llvm::BumpPtrAllocator StringData; 969718292f2SDouglas Gregor 970718292f2SDouglas Gregor /// \brief The current token. 971718292f2SDouglas Gregor MMToken Tok; 972718292f2SDouglas Gregor 973718292f2SDouglas Gregor /// \brief The active module. 974de3ef502SDouglas Gregor Module *ActiveModule; 975718292f2SDouglas Gregor 976718292f2SDouglas Gregor /// \brief Consume the current token and return its location. 977718292f2SDouglas Gregor SourceLocation consumeToken(); 978718292f2SDouglas Gregor 979718292f2SDouglas Gregor /// \brief Skip tokens until we reach the a token with the given kind 980718292f2SDouglas Gregor /// (or the end of the file). 981718292f2SDouglas Gregor void skipUntil(MMToken::TokenKind K); 982718292f2SDouglas Gregor 983f857950dSDmitri Gribenko typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; 984e7ab3669SDouglas Gregor bool parseModuleId(ModuleId &Id); 985718292f2SDouglas Gregor void parseModuleDecl(); 98697292843SDaniel Jasper void parseExternModuleDecl(); 9871fb5c3a6SDouglas Gregor void parseRequiresDecl(); 988b53e5483SLawrence Crowl void parseHeaderDecl(clang::MMToken::TokenKind, 989b53e5483SLawrence Crowl SourceLocation LeadingLoc); 990524e33e1SDouglas Gregor void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 9912b82c2a5SDouglas Gregor void parseExportDecl(); 992ba7f2f71SDaniel Jasper void parseUseDecl(); 9936ddfca91SDouglas Gregor void parseLinkDecl(); 99435b13eceSDouglas Gregor void parseConfigMacros(); 995fb912657SDouglas Gregor void parseConflict(); 9969194a91dSDouglas Gregor void parseInferredModuleDecl(bool Framework, bool Explicit); 9974442605fSBill Wendling bool parseOptionalAttributes(Attributes &Attrs); 998718292f2SDouglas Gregor 9997033127bSDouglas Gregor const DirectoryEntry *getOverriddenHeaderSearchDir(); 10007033127bSDouglas Gregor 1001718292f2SDouglas Gregor public: 1002718292f2SDouglas Gregor explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 1003bc10b9fbSDouglas Gregor const TargetInfo *Target, 1004718292f2SDouglas Gregor DiagnosticsEngine &Diags, 10055257fc63SDouglas Gregor ModuleMap &Map, 10063ec6663bSDouglas Gregor const DirectoryEntry *Directory, 1007963c5535SDouglas Gregor const DirectoryEntry *BuiltinIncludeDir, 1008963c5535SDouglas Gregor bool IsSystem) 1009bc10b9fbSDouglas Gregor : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 10103ec6663bSDouglas Gregor Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir), 1011963c5535SDouglas Gregor IsSystem(IsSystem), HadError(false), ActiveModule(0) 1012718292f2SDouglas Gregor { 1013718292f2SDouglas Gregor Tok.clear(); 1014718292f2SDouglas Gregor consumeToken(); 1015718292f2SDouglas Gregor } 1016718292f2SDouglas Gregor 1017718292f2SDouglas Gregor bool parseModuleMapFile(); 1018718292f2SDouglas Gregor }; 1019718292f2SDouglas Gregor } 1020718292f2SDouglas Gregor 1021718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() { 1022718292f2SDouglas Gregor retry: 1023718292f2SDouglas Gregor SourceLocation Result = Tok.getLocation(); 1024718292f2SDouglas Gregor Tok.clear(); 1025718292f2SDouglas Gregor 1026718292f2SDouglas Gregor Token LToken; 1027718292f2SDouglas Gregor L.LexFromRawLexer(LToken); 1028718292f2SDouglas Gregor Tok.Location = LToken.getLocation().getRawEncoding(); 1029718292f2SDouglas Gregor switch (LToken.getKind()) { 1030718292f2SDouglas Gregor case tok::raw_identifier: 1031718292f2SDouglas Gregor Tok.StringData = LToken.getRawIdentifierData(); 1032718292f2SDouglas Gregor Tok.StringLength = LToken.getLength(); 1033718292f2SDouglas Gregor Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString()) 103435b13eceSDouglas Gregor .Case("config_macros", MMToken::ConfigMacros) 1035fb912657SDouglas Gregor .Case("conflict", MMToken::Conflict) 103659527666SDouglas Gregor .Case("exclude", MMToken::ExcludeKeyword) 1037718292f2SDouglas Gregor .Case("explicit", MMToken::ExplicitKeyword) 10382b82c2a5SDouglas Gregor .Case("export", MMToken::ExportKeyword) 103997292843SDaniel Jasper .Case("extern", MMToken::ExternKeyword) 1040755b2055SDouglas Gregor .Case("framework", MMToken::FrameworkKeyword) 104135b13eceSDouglas Gregor .Case("header", MMToken::HeaderKeyword) 10426ddfca91SDouglas Gregor .Case("link", MMToken::LinkKeyword) 1043718292f2SDouglas Gregor .Case("module", MMToken::ModuleKeyword) 1044b53e5483SLawrence Crowl .Case("private", MMToken::PrivateKeyword) 10451fb5c3a6SDouglas Gregor .Case("requires", MMToken::RequiresKeyword) 1046718292f2SDouglas Gregor .Case("umbrella", MMToken::UmbrellaKeyword) 1047ba7f2f71SDaniel Jasper .Case("use", MMToken::UseKeyword) 1048718292f2SDouglas Gregor .Default(MMToken::Identifier); 1049718292f2SDouglas Gregor break; 1050718292f2SDouglas Gregor 10511fb5c3a6SDouglas Gregor case tok::comma: 10521fb5c3a6SDouglas Gregor Tok.Kind = MMToken::Comma; 10531fb5c3a6SDouglas Gregor break; 10541fb5c3a6SDouglas Gregor 1055718292f2SDouglas Gregor case tok::eof: 1056718292f2SDouglas Gregor Tok.Kind = MMToken::EndOfFile; 1057718292f2SDouglas Gregor break; 1058718292f2SDouglas Gregor 1059718292f2SDouglas Gregor case tok::l_brace: 1060718292f2SDouglas Gregor Tok.Kind = MMToken::LBrace; 1061718292f2SDouglas Gregor break; 1062718292f2SDouglas Gregor 1063a686e1b0SDouglas Gregor case tok::l_square: 1064a686e1b0SDouglas Gregor Tok.Kind = MMToken::LSquare; 1065a686e1b0SDouglas Gregor break; 1066a686e1b0SDouglas Gregor 10672b82c2a5SDouglas Gregor case tok::period: 10682b82c2a5SDouglas Gregor Tok.Kind = MMToken::Period; 10692b82c2a5SDouglas Gregor break; 10702b82c2a5SDouglas Gregor 1071718292f2SDouglas Gregor case tok::r_brace: 1072718292f2SDouglas Gregor Tok.Kind = MMToken::RBrace; 1073718292f2SDouglas Gregor break; 1074718292f2SDouglas Gregor 1075a686e1b0SDouglas Gregor case tok::r_square: 1076a686e1b0SDouglas Gregor Tok.Kind = MMToken::RSquare; 1077a686e1b0SDouglas Gregor break; 1078a686e1b0SDouglas Gregor 10792b82c2a5SDouglas Gregor case tok::star: 10802b82c2a5SDouglas Gregor Tok.Kind = MMToken::Star; 10812b82c2a5SDouglas Gregor break; 10822b82c2a5SDouglas Gregor 1083a3feee2aSRichard Smith case tok::exclaim: 1084a3feee2aSRichard Smith Tok.Kind = MMToken::Exclaim; 1085a3feee2aSRichard Smith break; 1086a3feee2aSRichard Smith 1087718292f2SDouglas Gregor case tok::string_literal: { 1088d67aea28SRichard Smith if (LToken.hasUDSuffix()) { 1089d67aea28SRichard Smith Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 1090d67aea28SRichard Smith HadError = true; 1091d67aea28SRichard Smith goto retry; 1092d67aea28SRichard Smith } 1093d67aea28SRichard Smith 1094718292f2SDouglas Gregor // Parse the string literal. 1095718292f2SDouglas Gregor LangOptions LangOpts; 1096718292f2SDouglas Gregor StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target); 1097718292f2SDouglas Gregor if (StringLiteral.hadError) 1098718292f2SDouglas Gregor goto retry; 1099718292f2SDouglas Gregor 1100718292f2SDouglas Gregor // Copy the string literal into our string data allocator. 1101718292f2SDouglas Gregor unsigned Length = StringLiteral.GetStringLength(); 1102718292f2SDouglas Gregor char *Saved = StringData.Allocate<char>(Length + 1); 1103718292f2SDouglas Gregor memcpy(Saved, StringLiteral.GetString().data(), Length); 1104718292f2SDouglas Gregor Saved[Length] = 0; 1105718292f2SDouglas Gregor 1106718292f2SDouglas Gregor // Form the token. 1107718292f2SDouglas Gregor Tok.Kind = MMToken::StringLiteral; 1108718292f2SDouglas Gregor Tok.StringData = Saved; 1109718292f2SDouglas Gregor Tok.StringLength = Length; 1110718292f2SDouglas Gregor break; 1111718292f2SDouglas Gregor } 1112718292f2SDouglas Gregor 1113718292f2SDouglas Gregor case tok::comment: 1114718292f2SDouglas Gregor goto retry; 1115718292f2SDouglas Gregor 1116718292f2SDouglas Gregor default: 1117718292f2SDouglas Gregor Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 1118718292f2SDouglas Gregor HadError = true; 1119718292f2SDouglas Gregor goto retry; 1120718292f2SDouglas Gregor } 1121718292f2SDouglas Gregor 1122718292f2SDouglas Gregor return Result; 1123718292f2SDouglas Gregor } 1124718292f2SDouglas Gregor 1125718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 1126718292f2SDouglas Gregor unsigned braceDepth = 0; 1127a686e1b0SDouglas Gregor unsigned squareDepth = 0; 1128718292f2SDouglas Gregor do { 1129718292f2SDouglas Gregor switch (Tok.Kind) { 1130718292f2SDouglas Gregor case MMToken::EndOfFile: 1131718292f2SDouglas Gregor return; 1132718292f2SDouglas Gregor 1133718292f2SDouglas Gregor case MMToken::LBrace: 1134a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1135718292f2SDouglas Gregor return; 1136718292f2SDouglas Gregor 1137718292f2SDouglas Gregor ++braceDepth; 1138718292f2SDouglas Gregor break; 1139718292f2SDouglas Gregor 1140a686e1b0SDouglas Gregor case MMToken::LSquare: 1141a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1142a686e1b0SDouglas Gregor return; 1143a686e1b0SDouglas Gregor 1144a686e1b0SDouglas Gregor ++squareDepth; 1145a686e1b0SDouglas Gregor break; 1146a686e1b0SDouglas Gregor 1147718292f2SDouglas Gregor case MMToken::RBrace: 1148718292f2SDouglas Gregor if (braceDepth > 0) 1149718292f2SDouglas Gregor --braceDepth; 1150718292f2SDouglas Gregor else if (Tok.is(K)) 1151718292f2SDouglas Gregor return; 1152718292f2SDouglas Gregor break; 1153718292f2SDouglas Gregor 1154a686e1b0SDouglas Gregor case MMToken::RSquare: 1155a686e1b0SDouglas Gregor if (squareDepth > 0) 1156a686e1b0SDouglas Gregor --squareDepth; 1157a686e1b0SDouglas Gregor else if (Tok.is(K)) 1158a686e1b0SDouglas Gregor return; 1159a686e1b0SDouglas Gregor break; 1160a686e1b0SDouglas Gregor 1161718292f2SDouglas Gregor default: 1162a686e1b0SDouglas Gregor if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 1163718292f2SDouglas Gregor return; 1164718292f2SDouglas Gregor break; 1165718292f2SDouglas Gregor } 1166718292f2SDouglas Gregor 1167718292f2SDouglas Gregor consumeToken(); 1168718292f2SDouglas Gregor } while (true); 1169718292f2SDouglas Gregor } 1170718292f2SDouglas Gregor 1171e7ab3669SDouglas Gregor /// \brief Parse a module-id. 1172e7ab3669SDouglas Gregor /// 1173e7ab3669SDouglas Gregor /// module-id: 1174e7ab3669SDouglas Gregor /// identifier 1175e7ab3669SDouglas Gregor /// identifier '.' module-id 1176e7ab3669SDouglas Gregor /// 1177e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise. 1178e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) { 1179e7ab3669SDouglas Gregor Id.clear(); 1180e7ab3669SDouglas Gregor do { 11813cd34c76SDaniel Jasper if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) { 1182e7ab3669SDouglas Gregor Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 1183e7ab3669SDouglas Gregor consumeToken(); 1184e7ab3669SDouglas Gregor } else { 1185e7ab3669SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 1186e7ab3669SDouglas Gregor return true; 1187e7ab3669SDouglas Gregor } 1188e7ab3669SDouglas Gregor 1189e7ab3669SDouglas Gregor if (!Tok.is(MMToken::Period)) 1190e7ab3669SDouglas Gregor break; 1191e7ab3669SDouglas Gregor 1192e7ab3669SDouglas Gregor consumeToken(); 1193e7ab3669SDouglas Gregor } while (true); 1194e7ab3669SDouglas Gregor 1195e7ab3669SDouglas Gregor return false; 1196e7ab3669SDouglas Gregor } 1197e7ab3669SDouglas Gregor 1198a686e1b0SDouglas Gregor namespace { 1199a686e1b0SDouglas Gregor /// \brief Enumerates the known attributes. 1200a686e1b0SDouglas Gregor enum AttributeKind { 1201a686e1b0SDouglas Gregor /// \brief An unknown attribute. 1202a686e1b0SDouglas Gregor AT_unknown, 1203a686e1b0SDouglas Gregor /// \brief The 'system' attribute. 120435b13eceSDouglas Gregor AT_system, 120577944868SRichard Smith /// \brief The 'extern_c' attribute. 120677944868SRichard Smith AT_extern_c, 120735b13eceSDouglas Gregor /// \brief The 'exhaustive' attribute. 120835b13eceSDouglas Gregor AT_exhaustive 1209a686e1b0SDouglas Gregor }; 1210a686e1b0SDouglas Gregor } 1211a686e1b0SDouglas Gregor 1212718292f2SDouglas Gregor /// \brief Parse a module declaration. 1213718292f2SDouglas Gregor /// 1214718292f2SDouglas Gregor /// module-declaration: 121597292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 1216a686e1b0SDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 1217a686e1b0SDouglas Gregor /// { module-member* } 1218a686e1b0SDouglas Gregor /// 1219718292f2SDouglas Gregor /// module-member: 12201fb5c3a6SDouglas Gregor /// requires-declaration 1221718292f2SDouglas Gregor /// header-declaration 1222e7ab3669SDouglas Gregor /// submodule-declaration 12232b82c2a5SDouglas Gregor /// export-declaration 12246ddfca91SDouglas Gregor /// link-declaration 122573441091SDouglas Gregor /// 122673441091SDouglas Gregor /// submodule-declaration: 122773441091SDouglas Gregor /// module-declaration 122873441091SDouglas Gregor /// inferred-submodule-declaration 1229718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() { 1230755b2055SDouglas Gregor assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 123197292843SDaniel Jasper Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword)); 123297292843SDaniel Jasper if (Tok.is(MMToken::ExternKeyword)) { 123397292843SDaniel Jasper parseExternModuleDecl(); 123497292843SDaniel Jasper return; 123597292843SDaniel Jasper } 123697292843SDaniel Jasper 1237f2161a70SDouglas Gregor // Parse 'explicit' or 'framework' keyword, if present. 1238e7ab3669SDouglas Gregor SourceLocation ExplicitLoc; 1239718292f2SDouglas Gregor bool Explicit = false; 1240f2161a70SDouglas Gregor bool Framework = false; 1241755b2055SDouglas Gregor 1242f2161a70SDouglas Gregor // Parse 'explicit' keyword, if present. 1243f2161a70SDouglas Gregor if (Tok.is(MMToken::ExplicitKeyword)) { 1244e7ab3669SDouglas Gregor ExplicitLoc = consumeToken(); 1245f2161a70SDouglas Gregor Explicit = true; 1246f2161a70SDouglas Gregor } 1247f2161a70SDouglas Gregor 1248f2161a70SDouglas Gregor // Parse 'framework' keyword, if present. 1249755b2055SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 1250755b2055SDouglas Gregor consumeToken(); 1251755b2055SDouglas Gregor Framework = true; 1252755b2055SDouglas Gregor } 1253718292f2SDouglas Gregor 1254718292f2SDouglas Gregor // Parse 'module' keyword. 1255718292f2SDouglas Gregor if (!Tok.is(MMToken::ModuleKeyword)) { 1256d6343c99SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1257718292f2SDouglas Gregor consumeToken(); 1258718292f2SDouglas Gregor HadError = true; 1259718292f2SDouglas Gregor return; 1260718292f2SDouglas Gregor } 1261718292f2SDouglas Gregor consumeToken(); // 'module' keyword 1262718292f2SDouglas Gregor 126373441091SDouglas Gregor // If we have a wildcard for the module name, this is an inferred submodule. 126473441091SDouglas Gregor // Parse it. 126573441091SDouglas Gregor if (Tok.is(MMToken::Star)) 12669194a91dSDouglas Gregor return parseInferredModuleDecl(Framework, Explicit); 126773441091SDouglas Gregor 1268718292f2SDouglas Gregor // Parse the module name. 1269e7ab3669SDouglas Gregor ModuleId Id; 1270e7ab3669SDouglas Gregor if (parseModuleId(Id)) { 1271718292f2SDouglas Gregor HadError = true; 1272718292f2SDouglas Gregor return; 1273718292f2SDouglas Gregor } 1274e7ab3669SDouglas Gregor 1275e7ab3669SDouglas Gregor if (ActiveModule) { 1276e7ab3669SDouglas Gregor if (Id.size() > 1) { 1277e7ab3669SDouglas Gregor Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 1278e7ab3669SDouglas Gregor << SourceRange(Id.front().second, Id.back().second); 1279e7ab3669SDouglas Gregor 1280e7ab3669SDouglas Gregor HadError = true; 1281e7ab3669SDouglas Gregor return; 1282e7ab3669SDouglas Gregor } 1283e7ab3669SDouglas Gregor } else if (Id.size() == 1 && Explicit) { 1284e7ab3669SDouglas Gregor // Top-level modules can't be explicit. 1285e7ab3669SDouglas Gregor Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 1286e7ab3669SDouglas Gregor Explicit = false; 1287e7ab3669SDouglas Gregor ExplicitLoc = SourceLocation(); 1288e7ab3669SDouglas Gregor HadError = true; 1289e7ab3669SDouglas Gregor } 1290e7ab3669SDouglas Gregor 1291e7ab3669SDouglas Gregor Module *PreviousActiveModule = ActiveModule; 1292e7ab3669SDouglas Gregor if (Id.size() > 1) { 1293e7ab3669SDouglas Gregor // This module map defines a submodule. Go find the module of which it 1294e7ab3669SDouglas Gregor // is a submodule. 1295e7ab3669SDouglas Gregor ActiveModule = 0; 1296e7ab3669SDouglas Gregor for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 1297e7ab3669SDouglas Gregor if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 1298e7ab3669SDouglas Gregor ActiveModule = Next; 1299e7ab3669SDouglas Gregor continue; 1300e7ab3669SDouglas Gregor } 1301e7ab3669SDouglas Gregor 1302e7ab3669SDouglas Gregor if (ActiveModule) { 1303e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 13045b5d21eaSRichard Smith << Id[I].first 13055b5d21eaSRichard Smith << ActiveModule->getTopLevelModule()->getFullModuleName(); 1306e7ab3669SDouglas Gregor } else { 1307e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 1308e7ab3669SDouglas Gregor } 1309e7ab3669SDouglas Gregor HadError = true; 1310e7ab3669SDouglas Gregor return; 1311e7ab3669SDouglas Gregor } 1312e7ab3669SDouglas Gregor } 1313e7ab3669SDouglas Gregor 1314e7ab3669SDouglas Gregor StringRef ModuleName = Id.back().first; 1315e7ab3669SDouglas Gregor SourceLocation ModuleNameLoc = Id.back().second; 1316718292f2SDouglas Gregor 1317a686e1b0SDouglas Gregor // Parse the optional attribute list. 13184442605fSBill Wendling Attributes Attrs; 13199194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 1320a686e1b0SDouglas Gregor 1321718292f2SDouglas Gregor // Parse the opening brace. 1322718292f2SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 1323718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 1324718292f2SDouglas Gregor << ModuleName; 1325718292f2SDouglas Gregor HadError = true; 1326718292f2SDouglas Gregor return; 1327718292f2SDouglas Gregor } 1328718292f2SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 1329718292f2SDouglas Gregor 1330718292f2SDouglas Gregor // Determine whether this (sub)module has already been defined. 1331eb90e830SDouglas Gregor if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 1332fcc54a3bSDouglas Gregor if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { 1333fcc54a3bSDouglas Gregor // Skip the module definition. 1334fcc54a3bSDouglas Gregor skipUntil(MMToken::RBrace); 1335fcc54a3bSDouglas Gregor if (Tok.is(MMToken::RBrace)) 1336fcc54a3bSDouglas Gregor consumeToken(); 1337fcc54a3bSDouglas Gregor else { 1338fcc54a3bSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1339fcc54a3bSDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1340fcc54a3bSDouglas Gregor HadError = true; 1341fcc54a3bSDouglas Gregor } 1342fcc54a3bSDouglas Gregor return; 1343fcc54a3bSDouglas Gregor } 1344fcc54a3bSDouglas Gregor 1345718292f2SDouglas Gregor Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 1346718292f2SDouglas Gregor << ModuleName; 1347eb90e830SDouglas Gregor Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 1348718292f2SDouglas Gregor 1349718292f2SDouglas Gregor // Skip the module definition. 1350718292f2SDouglas Gregor skipUntil(MMToken::RBrace); 1351718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1352718292f2SDouglas Gregor consumeToken(); 1353718292f2SDouglas Gregor 1354718292f2SDouglas Gregor HadError = true; 1355718292f2SDouglas Gregor return; 1356718292f2SDouglas Gregor } 1357718292f2SDouglas Gregor 1358718292f2SDouglas Gregor // Start defining this module. 1359eb90e830SDouglas Gregor ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, 1360eb90e830SDouglas Gregor Explicit).first; 1361eb90e830SDouglas Gregor ActiveModule->DefinitionLoc = ModuleNameLoc; 1362963c5535SDouglas Gregor if (Attrs.IsSystem || IsSystem) 1363a686e1b0SDouglas Gregor ActiveModule->IsSystem = true; 136477944868SRichard Smith if (Attrs.IsExternC) 136577944868SRichard Smith ActiveModule->IsExternC = true; 1366718292f2SDouglas Gregor 1367718292f2SDouglas Gregor bool Done = false; 1368718292f2SDouglas Gregor do { 1369718292f2SDouglas Gregor switch (Tok.Kind) { 1370718292f2SDouglas Gregor case MMToken::EndOfFile: 1371718292f2SDouglas Gregor case MMToken::RBrace: 1372718292f2SDouglas Gregor Done = true; 1373718292f2SDouglas Gregor break; 1374718292f2SDouglas Gregor 137535b13eceSDouglas Gregor case MMToken::ConfigMacros: 137635b13eceSDouglas Gregor parseConfigMacros(); 137735b13eceSDouglas Gregor break; 137835b13eceSDouglas Gregor 1379fb912657SDouglas Gregor case MMToken::Conflict: 1380fb912657SDouglas Gregor parseConflict(); 1381fb912657SDouglas Gregor break; 1382fb912657SDouglas Gregor 1383718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 138497292843SDaniel Jasper case MMToken::ExternKeyword: 1385f2161a70SDouglas Gregor case MMToken::FrameworkKeyword: 1386718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1387718292f2SDouglas Gregor parseModuleDecl(); 1388718292f2SDouglas Gregor break; 1389718292f2SDouglas Gregor 13902b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 13912b82c2a5SDouglas Gregor parseExportDecl(); 13922b82c2a5SDouglas Gregor break; 13932b82c2a5SDouglas Gregor 1394ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 1395ba7f2f71SDaniel Jasper parseUseDecl(); 1396ba7f2f71SDaniel Jasper break; 1397ba7f2f71SDaniel Jasper 13981fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 13991fb5c3a6SDouglas Gregor parseRequiresDecl(); 14001fb5c3a6SDouglas Gregor break; 14011fb5c3a6SDouglas Gregor 1402524e33e1SDouglas Gregor case MMToken::UmbrellaKeyword: { 1403524e33e1SDouglas Gregor SourceLocation UmbrellaLoc = consumeToken(); 1404524e33e1SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) 1405b53e5483SLawrence Crowl parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc); 1406524e33e1SDouglas Gregor else 1407524e33e1SDouglas Gregor parseUmbrellaDirDecl(UmbrellaLoc); 1408718292f2SDouglas Gregor break; 1409524e33e1SDouglas Gregor } 1410718292f2SDouglas Gregor 141159527666SDouglas Gregor case MMToken::ExcludeKeyword: { 141259527666SDouglas Gregor SourceLocation ExcludeLoc = consumeToken(); 141359527666SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) { 1414b53e5483SLawrence Crowl parseHeaderDecl(MMToken::ExcludeKeyword, ExcludeLoc); 141559527666SDouglas Gregor } else { 141659527666SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 141759527666SDouglas Gregor << "exclude"; 141859527666SDouglas Gregor } 141959527666SDouglas Gregor break; 142059527666SDouglas Gregor } 142159527666SDouglas Gregor 1422b53e5483SLawrence Crowl case MMToken::PrivateKeyword: { 1423b53e5483SLawrence Crowl SourceLocation PrivateLoc = consumeToken(); 1424b53e5483SLawrence Crowl if (Tok.is(MMToken::HeaderKeyword)) { 1425b53e5483SLawrence Crowl parseHeaderDecl(MMToken::PrivateKeyword, PrivateLoc); 1426b53e5483SLawrence Crowl } else { 1427b53e5483SLawrence Crowl Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1428b53e5483SLawrence Crowl << "private"; 1429b53e5483SLawrence Crowl } 1430b53e5483SLawrence Crowl break; 1431b53e5483SLawrence Crowl } 1432b53e5483SLawrence Crowl 1433322f633cSDouglas Gregor case MMToken::HeaderKeyword: 1434b53e5483SLawrence Crowl parseHeaderDecl(MMToken::HeaderKeyword, SourceLocation()); 1435718292f2SDouglas Gregor break; 1436718292f2SDouglas Gregor 14376ddfca91SDouglas Gregor case MMToken::LinkKeyword: 14386ddfca91SDouglas Gregor parseLinkDecl(); 14396ddfca91SDouglas Gregor break; 14406ddfca91SDouglas Gregor 1441718292f2SDouglas Gregor default: 1442718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 1443718292f2SDouglas Gregor consumeToken(); 1444718292f2SDouglas Gregor break; 1445718292f2SDouglas Gregor } 1446718292f2SDouglas Gregor } while (!Done); 1447718292f2SDouglas Gregor 1448718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1449718292f2SDouglas Gregor consumeToken(); 1450718292f2SDouglas Gregor else { 1451718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1452718292f2SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1453718292f2SDouglas Gregor HadError = true; 1454718292f2SDouglas Gregor } 1455718292f2SDouglas Gregor 145611dfe6feSDouglas Gregor // If the active module is a top-level framework, and there are no link 145711dfe6feSDouglas Gregor // libraries, automatically link against the framework. 145811dfe6feSDouglas Gregor if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() && 145911dfe6feSDouglas Gregor ActiveModule->LinkLibraries.empty()) { 146011dfe6feSDouglas Gregor inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager()); 146111dfe6feSDouglas Gregor } 146211dfe6feSDouglas Gregor 1463e7ab3669SDouglas Gregor // We're done parsing this module. Pop back to the previous module. 1464e7ab3669SDouglas Gregor ActiveModule = PreviousActiveModule; 1465718292f2SDouglas Gregor } 1466718292f2SDouglas Gregor 146797292843SDaniel Jasper /// \brief Parse an extern module declaration. 146897292843SDaniel Jasper /// 146997292843SDaniel Jasper /// extern module-declaration: 147097292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 147197292843SDaniel Jasper void ModuleMapParser::parseExternModuleDecl() { 147297292843SDaniel Jasper assert(Tok.is(MMToken::ExternKeyword)); 147397292843SDaniel Jasper consumeToken(); // 'extern' keyword 147497292843SDaniel Jasper 147597292843SDaniel Jasper // Parse 'module' keyword. 147697292843SDaniel Jasper if (!Tok.is(MMToken::ModuleKeyword)) { 147797292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 147897292843SDaniel Jasper consumeToken(); 147997292843SDaniel Jasper HadError = true; 148097292843SDaniel Jasper return; 148197292843SDaniel Jasper } 148297292843SDaniel Jasper consumeToken(); // 'module' keyword 148397292843SDaniel Jasper 148497292843SDaniel Jasper // Parse the module name. 148597292843SDaniel Jasper ModuleId Id; 148697292843SDaniel Jasper if (parseModuleId(Id)) { 148797292843SDaniel Jasper HadError = true; 148897292843SDaniel Jasper return; 148997292843SDaniel Jasper } 149097292843SDaniel Jasper 149197292843SDaniel Jasper // Parse the referenced module map file name. 149297292843SDaniel Jasper if (!Tok.is(MMToken::StringLiteral)) { 149397292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file); 149497292843SDaniel Jasper HadError = true; 149597292843SDaniel Jasper return; 149697292843SDaniel Jasper } 149797292843SDaniel Jasper std::string FileName = Tok.getString(); 149897292843SDaniel Jasper consumeToken(); // filename 149997292843SDaniel Jasper 150097292843SDaniel Jasper StringRef FileNameRef = FileName; 150197292843SDaniel Jasper SmallString<128> ModuleMapFileName; 150297292843SDaniel Jasper if (llvm::sys::path::is_relative(FileNameRef)) { 150397292843SDaniel Jasper ModuleMapFileName += Directory->getName(); 150497292843SDaniel Jasper llvm::sys::path::append(ModuleMapFileName, FileName); 150597292843SDaniel Jasper FileNameRef = ModuleMapFileName.str(); 150697292843SDaniel Jasper } 150797292843SDaniel Jasper if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef)) 150897292843SDaniel Jasper Map.parseModuleMapFile(File, /*IsSystem=*/false); 150997292843SDaniel Jasper } 151097292843SDaniel Jasper 15111fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration. 15121fb5c3a6SDouglas Gregor /// 15131fb5c3a6SDouglas Gregor /// requires-declaration: 15141fb5c3a6SDouglas Gregor /// 'requires' feature-list 15151fb5c3a6SDouglas Gregor /// 15161fb5c3a6SDouglas Gregor /// feature-list: 1517a3feee2aSRichard Smith /// feature ',' feature-list 1518a3feee2aSRichard Smith /// feature 1519a3feee2aSRichard Smith /// 1520a3feee2aSRichard Smith /// feature: 1521a3feee2aSRichard Smith /// '!'[opt] identifier 15221fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() { 15231fb5c3a6SDouglas Gregor assert(Tok.is(MMToken::RequiresKeyword)); 15241fb5c3a6SDouglas Gregor 15251fb5c3a6SDouglas Gregor // Parse 'requires' keyword. 15261fb5c3a6SDouglas Gregor consumeToken(); 15271fb5c3a6SDouglas Gregor 15281fb5c3a6SDouglas Gregor // Parse the feature-list. 15291fb5c3a6SDouglas Gregor do { 1530a3feee2aSRichard Smith bool RequiredState = true; 1531a3feee2aSRichard Smith if (Tok.is(MMToken::Exclaim)) { 1532a3feee2aSRichard Smith RequiredState = false; 1533a3feee2aSRichard Smith consumeToken(); 1534a3feee2aSRichard Smith } 1535a3feee2aSRichard Smith 15361fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 15371fb5c3a6SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 15381fb5c3a6SDouglas Gregor HadError = true; 15391fb5c3a6SDouglas Gregor return; 15401fb5c3a6SDouglas Gregor } 15411fb5c3a6SDouglas Gregor 15421fb5c3a6SDouglas Gregor // Consume the feature name. 15431fb5c3a6SDouglas Gregor std::string Feature = Tok.getString(); 15441fb5c3a6SDouglas Gregor consumeToken(); 15451fb5c3a6SDouglas Gregor 15461fb5c3a6SDouglas Gregor // Add this feature. 1547a3feee2aSRichard Smith ActiveModule->addRequirement(Feature, RequiredState, 1548a3feee2aSRichard Smith Map.LangOpts, *Map.Target); 15491fb5c3a6SDouglas Gregor 15501fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Comma)) 15511fb5c3a6SDouglas Gregor break; 15521fb5c3a6SDouglas Gregor 15531fb5c3a6SDouglas Gregor // Consume the comma. 15541fb5c3a6SDouglas Gregor consumeToken(); 15551fb5c3a6SDouglas Gregor } while (true); 15561fb5c3a6SDouglas Gregor } 15571fb5c3a6SDouglas Gregor 1558f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the 1559f2161a70SDouglas Gregor /// subframework in which the given module lives. 1560bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod, 1561f857950dSDmitri Gribenko SmallVectorImpl<char> &Path) { 1562f2161a70SDouglas Gregor // Collect the framework names from the given module to the top-level module. 1563f857950dSDmitri Gribenko SmallVector<StringRef, 2> Paths; 1564f2161a70SDouglas Gregor for (; Mod; Mod = Mod->Parent) { 1565f2161a70SDouglas Gregor if (Mod->IsFramework) 1566f2161a70SDouglas Gregor Paths.push_back(Mod->Name); 1567f2161a70SDouglas Gregor } 1568f2161a70SDouglas Gregor 1569f2161a70SDouglas Gregor if (Paths.empty()) 1570f2161a70SDouglas Gregor return; 1571f2161a70SDouglas Gregor 1572f2161a70SDouglas Gregor // Add Frameworks/Name.framework for each subframework. 157317381a06SBenjamin Kramer for (unsigned I = Paths.size() - 1; I != 0; --I) 157417381a06SBenjamin Kramer llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework"); 1575f2161a70SDouglas Gregor } 1576f2161a70SDouglas Gregor 1577718292f2SDouglas Gregor /// \brief Parse a header declaration. 1578718292f2SDouglas Gregor /// 1579718292f2SDouglas Gregor /// header-declaration: 1580322f633cSDouglas Gregor /// 'umbrella'[opt] 'header' string-literal 158159527666SDouglas Gregor /// 'exclude'[opt] 'header' string-literal 1582b53e5483SLawrence Crowl void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken, 1583b53e5483SLawrence Crowl SourceLocation LeadingLoc) { 1584718292f2SDouglas Gregor assert(Tok.is(MMToken::HeaderKeyword)); 15851871ed3dSBenjamin Kramer consumeToken(); 1586718292f2SDouglas Gregor 1587718292f2SDouglas Gregor // Parse the header name. 1588718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1589718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1590718292f2SDouglas Gregor << "header"; 1591718292f2SDouglas Gregor HadError = true; 1592718292f2SDouglas Gregor return; 1593718292f2SDouglas Gregor } 15940761a8a0SDaniel Jasper Module::HeaderDirective Header; 15950761a8a0SDaniel Jasper Header.FileName = Tok.getString(); 15960761a8a0SDaniel Jasper Header.FileNameLoc = consumeToken(); 1597718292f2SDouglas Gregor 1598524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1599b53e5483SLawrence Crowl if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) { 16000761a8a0SDaniel Jasper Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash) 1601524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1602322f633cSDouglas Gregor HadError = true; 1603322f633cSDouglas Gregor return; 1604322f633cSDouglas Gregor } 1605322f633cSDouglas Gregor 16065257fc63SDouglas Gregor // Look for this file. 1607e7ab3669SDouglas Gregor const FileEntry *File = 0; 16083ec6663bSDouglas Gregor const FileEntry *BuiltinFile = 0; 16092c1dd271SDylan Noblesmith SmallString<128> PathName; 16100761a8a0SDaniel Jasper if (llvm::sys::path::is_absolute(Header.FileName)) { 16110761a8a0SDaniel Jasper PathName = Header.FileName; 1612e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 16137033127bSDouglas Gregor } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) { 16147033127bSDouglas Gregor PathName = Dir->getName(); 16150761a8a0SDaniel Jasper llvm::sys::path::append(PathName, Header.FileName); 16167033127bSDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1617e7ab3669SDouglas Gregor } else { 1618e7ab3669SDouglas Gregor // Search for the header file within the search directory. 16197033127bSDouglas Gregor PathName = Directory->getName(); 1620e7ab3669SDouglas Gregor unsigned PathLength = PathName.size(); 1621755b2055SDouglas Gregor 1622f2161a70SDouglas Gregor if (ActiveModule->isPartOfFramework()) { 1623f2161a70SDouglas Gregor appendSubframeworkPaths(ActiveModule, PathName); 1624755b2055SDouglas Gregor 1625e7ab3669SDouglas Gregor // Check whether this file is in the public headers. 16260761a8a0SDaniel Jasper llvm::sys::path::append(PathName, "Headers", Header.FileName); 1627e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1628e7ab3669SDouglas Gregor 1629e7ab3669SDouglas Gregor if (!File) { 1630e7ab3669SDouglas Gregor // Check whether this file is in the private headers. 1631e7ab3669SDouglas Gregor PathName.resize(PathLength); 16320761a8a0SDaniel Jasper llvm::sys::path::append(PathName, "PrivateHeaders", Header.FileName); 1633e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1634e7ab3669SDouglas Gregor } 1635e7ab3669SDouglas Gregor } else { 1636e7ab3669SDouglas Gregor // Lookup for normal headers. 16370761a8a0SDaniel Jasper llvm::sys::path::append(PathName, Header.FileName); 1638e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 16393ec6663bSDouglas Gregor 16403ec6663bSDouglas Gregor // If this is a system module with a top-level header, this header 16413ec6663bSDouglas Gregor // may have a counterpart (or replacement) in the set of headers 16423ec6663bSDouglas Gregor // supplied by Clang. Find that builtin header. 1643b53e5483SLawrence Crowl if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword && 1644b53e5483SLawrence Crowl BuiltinIncludeDir && BuiltinIncludeDir != Directory && 16450761a8a0SDaniel Jasper isBuiltinHeader(Header.FileName)) { 16462c1dd271SDylan Noblesmith SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); 16470761a8a0SDaniel Jasper llvm::sys::path::append(BuiltinPathName, Header.FileName); 16483ec6663bSDouglas Gregor BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); 16493ec6663bSDouglas Gregor 16503ec6663bSDouglas Gregor // If Clang supplies this header but the underlying system does not, 16513ec6663bSDouglas Gregor // just silently swap in our builtin version. Otherwise, we'll end 16523ec6663bSDouglas Gregor // up adding both (later). 16533ec6663bSDouglas Gregor if (!File && BuiltinFile) { 16543ec6663bSDouglas Gregor File = BuiltinFile; 16553ec6663bSDouglas Gregor BuiltinFile = 0; 16563ec6663bSDouglas Gregor } 16573ec6663bSDouglas Gregor } 1658e7ab3669SDouglas Gregor } 1659e7ab3669SDouglas Gregor } 16605257fc63SDouglas Gregor 16615257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 16625257fc63SDouglas Gregor // Come up with a lazy way to do this. 1663e7ab3669SDouglas Gregor if (File) { 166497da9178SDaniel Jasper if (LeadingToken == MMToken::UmbrellaKeyword) { 1665322f633cSDouglas Gregor const DirectoryEntry *UmbrellaDir = File->getDir(); 166659527666SDouglas Gregor if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { 1667b53e5483SLawrence Crowl Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash) 166859527666SDouglas Gregor << UmbrellaModule->getFullModuleName(); 1669322f633cSDouglas Gregor HadError = true; 16705257fc63SDouglas Gregor } else { 1671322f633cSDouglas Gregor // Record this umbrella header. 1672322f633cSDouglas Gregor Map.setUmbrellaHeader(ActiveModule, File); 1673322f633cSDouglas Gregor } 1674322f633cSDouglas Gregor } else { 1675322f633cSDouglas Gregor // Record this header. 1676b53e5483SLawrence Crowl ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader; 1677b53e5483SLawrence Crowl if (LeadingToken == MMToken::ExcludeKeyword) 1678b53e5483SLawrence Crowl Role = ModuleMap::ExcludedHeader; 1679b53e5483SLawrence Crowl else if (LeadingToken == MMToken::PrivateKeyword) 1680b53e5483SLawrence Crowl Role = ModuleMap::PrivateHeader; 1681b53e5483SLawrence Crowl else 1682b53e5483SLawrence Crowl assert(LeadingToken == MMToken::HeaderKeyword); 1683b53e5483SLawrence Crowl 1684b53e5483SLawrence Crowl Map.addHeader(ActiveModule, File, Role); 16853ec6663bSDouglas Gregor 16863ec6663bSDouglas Gregor // If there is a builtin counterpart to this file, add it now. 16873ec6663bSDouglas Gregor if (BuiltinFile) 1688b53e5483SLawrence Crowl Map.addHeader(ActiveModule, BuiltinFile, Role); 16895257fc63SDouglas Gregor } 1690b53e5483SLawrence Crowl } else if (LeadingToken != MMToken::ExcludeKeyword) { 16914b27a64bSDouglas Gregor // Ignore excluded header files. They're optional anyway. 16924b27a64bSDouglas Gregor 16930761a8a0SDaniel Jasper // If we find a module that has a missing header, we mark this module as 16940761a8a0SDaniel Jasper // unavailable and store the header directive for displaying diagnostics. 16950761a8a0SDaniel Jasper // Other submodules in the same module can still be used. 16960761a8a0SDaniel Jasper Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword; 16970761a8a0SDaniel Jasper ActiveModule->IsAvailable = false; 16980761a8a0SDaniel Jasper ActiveModule->MissingHeaders.push_back(Header); 16995257fc63SDouglas Gregor } 1700718292f2SDouglas Gregor } 1701718292f2SDouglas Gregor 1702524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration. 1703524e33e1SDouglas Gregor /// 1704524e33e1SDouglas Gregor /// umbrella-dir-declaration: 1705524e33e1SDouglas Gregor /// umbrella string-literal 1706524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1707524e33e1SDouglas Gregor // Parse the directory name. 1708524e33e1SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1709524e33e1SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1710524e33e1SDouglas Gregor << "umbrella"; 1711524e33e1SDouglas Gregor HadError = true; 1712524e33e1SDouglas Gregor return; 1713524e33e1SDouglas Gregor } 1714524e33e1SDouglas Gregor 1715524e33e1SDouglas Gregor std::string DirName = Tok.getString(); 1716524e33e1SDouglas Gregor SourceLocation DirNameLoc = consumeToken(); 1717524e33e1SDouglas Gregor 1718524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1719524e33e1SDouglas Gregor if (ActiveModule->Umbrella) { 1720524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1721524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1722524e33e1SDouglas Gregor HadError = true; 1723524e33e1SDouglas Gregor return; 1724524e33e1SDouglas Gregor } 1725524e33e1SDouglas Gregor 1726524e33e1SDouglas Gregor // Look for this file. 1727524e33e1SDouglas Gregor const DirectoryEntry *Dir = 0; 1728524e33e1SDouglas Gregor if (llvm::sys::path::is_absolute(DirName)) 1729524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(DirName); 1730524e33e1SDouglas Gregor else { 17312c1dd271SDylan Noblesmith SmallString<128> PathName; 1732524e33e1SDouglas Gregor PathName = Directory->getName(); 1733524e33e1SDouglas Gregor llvm::sys::path::append(PathName, DirName); 1734524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(PathName); 1735524e33e1SDouglas Gregor } 1736524e33e1SDouglas Gregor 1737524e33e1SDouglas Gregor if (!Dir) { 1738524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1739524e33e1SDouglas Gregor << DirName; 1740524e33e1SDouglas Gregor HadError = true; 1741524e33e1SDouglas Gregor return; 1742524e33e1SDouglas Gregor } 1743524e33e1SDouglas Gregor 1744524e33e1SDouglas Gregor if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1745524e33e1SDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1746524e33e1SDouglas Gregor << OwningModule->getFullModuleName(); 1747524e33e1SDouglas Gregor HadError = true; 1748524e33e1SDouglas Gregor return; 1749524e33e1SDouglas Gregor } 1750524e33e1SDouglas Gregor 1751524e33e1SDouglas Gregor // Record this umbrella directory. 1752524e33e1SDouglas Gregor Map.setUmbrellaDir(ActiveModule, Dir); 1753524e33e1SDouglas Gregor } 1754524e33e1SDouglas Gregor 17552b82c2a5SDouglas Gregor /// \brief Parse a module export declaration. 17562b82c2a5SDouglas Gregor /// 17572b82c2a5SDouglas Gregor /// export-declaration: 17582b82c2a5SDouglas Gregor /// 'export' wildcard-module-id 17592b82c2a5SDouglas Gregor /// 17602b82c2a5SDouglas Gregor /// wildcard-module-id: 17612b82c2a5SDouglas Gregor /// identifier 17622b82c2a5SDouglas Gregor /// '*' 17632b82c2a5SDouglas Gregor /// identifier '.' wildcard-module-id 17642b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() { 17652b82c2a5SDouglas Gregor assert(Tok.is(MMToken::ExportKeyword)); 17662b82c2a5SDouglas Gregor SourceLocation ExportLoc = consumeToken(); 17672b82c2a5SDouglas Gregor 17682b82c2a5SDouglas Gregor // Parse the module-id with an optional wildcard at the end. 17692b82c2a5SDouglas Gregor ModuleId ParsedModuleId; 17702b82c2a5SDouglas Gregor bool Wildcard = false; 17712b82c2a5SDouglas Gregor do { 17722b82c2a5SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 17732b82c2a5SDouglas Gregor ParsedModuleId.push_back(std::make_pair(Tok.getString(), 17742b82c2a5SDouglas Gregor Tok.getLocation())); 17752b82c2a5SDouglas Gregor consumeToken(); 17762b82c2a5SDouglas Gregor 17772b82c2a5SDouglas Gregor if (Tok.is(MMToken::Period)) { 17782b82c2a5SDouglas Gregor consumeToken(); 17792b82c2a5SDouglas Gregor continue; 17802b82c2a5SDouglas Gregor } 17812b82c2a5SDouglas Gregor 17822b82c2a5SDouglas Gregor break; 17832b82c2a5SDouglas Gregor } 17842b82c2a5SDouglas Gregor 17852b82c2a5SDouglas Gregor if(Tok.is(MMToken::Star)) { 17862b82c2a5SDouglas Gregor Wildcard = true; 1787f5eedd05SDouglas Gregor consumeToken(); 17882b82c2a5SDouglas Gregor break; 17892b82c2a5SDouglas Gregor } 17902b82c2a5SDouglas Gregor 1791ba7f2f71SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); 17922b82c2a5SDouglas Gregor HadError = true; 17932b82c2a5SDouglas Gregor return; 17942b82c2a5SDouglas Gregor } while (true); 17952b82c2a5SDouglas Gregor 17962b82c2a5SDouglas Gregor Module::UnresolvedExportDecl Unresolved = { 17972b82c2a5SDouglas Gregor ExportLoc, ParsedModuleId, Wildcard 17982b82c2a5SDouglas Gregor }; 17992b82c2a5SDouglas Gregor ActiveModule->UnresolvedExports.push_back(Unresolved); 18002b82c2a5SDouglas Gregor } 18012b82c2a5SDouglas Gregor 1802ba7f2f71SDaniel Jasper /// \brief Parse a module uses declaration. 1803ba7f2f71SDaniel Jasper /// 1804ba7f2f71SDaniel Jasper /// uses-declaration: 1805ba7f2f71SDaniel Jasper /// 'uses' wildcard-module-id 1806ba7f2f71SDaniel Jasper void ModuleMapParser::parseUseDecl() { 1807ba7f2f71SDaniel Jasper assert(Tok.is(MMToken::UseKeyword)); 1808ba7f2f71SDaniel Jasper consumeToken(); 1809ba7f2f71SDaniel Jasper // Parse the module-id. 1810ba7f2f71SDaniel Jasper ModuleId ParsedModuleId; 18113cd34c76SDaniel Jasper parseModuleId(ParsedModuleId); 1812ba7f2f71SDaniel Jasper 1813ba7f2f71SDaniel Jasper ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId); 1814ba7f2f71SDaniel Jasper } 1815ba7f2f71SDaniel Jasper 18166ddfca91SDouglas Gregor /// \brief Parse a link declaration. 18176ddfca91SDouglas Gregor /// 18186ddfca91SDouglas Gregor /// module-declaration: 18196ddfca91SDouglas Gregor /// 'link' 'framework'[opt] string-literal 18206ddfca91SDouglas Gregor void ModuleMapParser::parseLinkDecl() { 18216ddfca91SDouglas Gregor assert(Tok.is(MMToken::LinkKeyword)); 18226ddfca91SDouglas Gregor SourceLocation LinkLoc = consumeToken(); 18236ddfca91SDouglas Gregor 18246ddfca91SDouglas Gregor // Parse the optional 'framework' keyword. 18256ddfca91SDouglas Gregor bool IsFramework = false; 18266ddfca91SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 18276ddfca91SDouglas Gregor consumeToken(); 18286ddfca91SDouglas Gregor IsFramework = true; 18296ddfca91SDouglas Gregor } 18306ddfca91SDouglas Gregor 18316ddfca91SDouglas Gregor // Parse the library name 18326ddfca91SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 18336ddfca91SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) 18346ddfca91SDouglas Gregor << IsFramework << SourceRange(LinkLoc); 18356ddfca91SDouglas Gregor HadError = true; 18366ddfca91SDouglas Gregor return; 18376ddfca91SDouglas Gregor } 18386ddfca91SDouglas Gregor 18396ddfca91SDouglas Gregor std::string LibraryName = Tok.getString(); 18406ddfca91SDouglas Gregor consumeToken(); 18416ddfca91SDouglas Gregor ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, 18426ddfca91SDouglas Gregor IsFramework)); 18436ddfca91SDouglas Gregor } 18446ddfca91SDouglas Gregor 184535b13eceSDouglas Gregor /// \brief Parse a configuration macro declaration. 184635b13eceSDouglas Gregor /// 184735b13eceSDouglas Gregor /// module-declaration: 184835b13eceSDouglas Gregor /// 'config_macros' attributes[opt] config-macro-list? 184935b13eceSDouglas Gregor /// 185035b13eceSDouglas Gregor /// config-macro-list: 185135b13eceSDouglas Gregor /// identifier (',' identifier)? 185235b13eceSDouglas Gregor void ModuleMapParser::parseConfigMacros() { 185335b13eceSDouglas Gregor assert(Tok.is(MMToken::ConfigMacros)); 185435b13eceSDouglas Gregor SourceLocation ConfigMacrosLoc = consumeToken(); 185535b13eceSDouglas Gregor 185635b13eceSDouglas Gregor // Only top-level modules can have configuration macros. 185735b13eceSDouglas Gregor if (ActiveModule->Parent) { 185835b13eceSDouglas Gregor Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule); 185935b13eceSDouglas Gregor } 186035b13eceSDouglas Gregor 186135b13eceSDouglas Gregor // Parse the optional attributes. 186235b13eceSDouglas Gregor Attributes Attrs; 186335b13eceSDouglas Gregor parseOptionalAttributes(Attrs); 186435b13eceSDouglas Gregor if (Attrs.IsExhaustive && !ActiveModule->Parent) { 186535b13eceSDouglas Gregor ActiveModule->ConfigMacrosExhaustive = true; 186635b13eceSDouglas Gregor } 186735b13eceSDouglas Gregor 186835b13eceSDouglas Gregor // If we don't have an identifier, we're done. 186935b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) 187035b13eceSDouglas Gregor return; 187135b13eceSDouglas Gregor 187235b13eceSDouglas Gregor // Consume the first identifier. 187335b13eceSDouglas Gregor if (!ActiveModule->Parent) { 187435b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 187535b13eceSDouglas Gregor } 187635b13eceSDouglas Gregor consumeToken(); 187735b13eceSDouglas Gregor 187835b13eceSDouglas Gregor do { 187935b13eceSDouglas Gregor // If there's a comma, consume it. 188035b13eceSDouglas Gregor if (!Tok.is(MMToken::Comma)) 188135b13eceSDouglas Gregor break; 188235b13eceSDouglas Gregor consumeToken(); 188335b13eceSDouglas Gregor 188435b13eceSDouglas Gregor // We expect to see a macro name here. 188535b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 188635b13eceSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro); 188735b13eceSDouglas Gregor break; 188835b13eceSDouglas Gregor } 188935b13eceSDouglas Gregor 189035b13eceSDouglas Gregor // Consume the macro name. 189135b13eceSDouglas Gregor if (!ActiveModule->Parent) { 189235b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 189335b13eceSDouglas Gregor } 189435b13eceSDouglas Gregor consumeToken(); 189535b13eceSDouglas Gregor } while (true); 189635b13eceSDouglas Gregor } 189735b13eceSDouglas Gregor 1898fb912657SDouglas Gregor /// \brief Format a module-id into a string. 1899fb912657SDouglas Gregor static std::string formatModuleId(const ModuleId &Id) { 1900fb912657SDouglas Gregor std::string result; 1901fb912657SDouglas Gregor { 1902fb912657SDouglas Gregor llvm::raw_string_ostream OS(result); 1903fb912657SDouglas Gregor 1904fb912657SDouglas Gregor for (unsigned I = 0, N = Id.size(); I != N; ++I) { 1905fb912657SDouglas Gregor if (I) 1906fb912657SDouglas Gregor OS << "."; 1907fb912657SDouglas Gregor OS << Id[I].first; 1908fb912657SDouglas Gregor } 1909fb912657SDouglas Gregor } 1910fb912657SDouglas Gregor 1911fb912657SDouglas Gregor return result; 1912fb912657SDouglas Gregor } 1913fb912657SDouglas Gregor 1914fb912657SDouglas Gregor /// \brief Parse a conflict declaration. 1915fb912657SDouglas Gregor /// 1916fb912657SDouglas Gregor /// module-declaration: 1917fb912657SDouglas Gregor /// 'conflict' module-id ',' string-literal 1918fb912657SDouglas Gregor void ModuleMapParser::parseConflict() { 1919fb912657SDouglas Gregor assert(Tok.is(MMToken::Conflict)); 1920fb912657SDouglas Gregor SourceLocation ConflictLoc = consumeToken(); 1921fb912657SDouglas Gregor Module::UnresolvedConflict Conflict; 1922fb912657SDouglas Gregor 1923fb912657SDouglas Gregor // Parse the module-id. 1924fb912657SDouglas Gregor if (parseModuleId(Conflict.Id)) 1925fb912657SDouglas Gregor return; 1926fb912657SDouglas Gregor 1927fb912657SDouglas Gregor // Parse the ','. 1928fb912657SDouglas Gregor if (!Tok.is(MMToken::Comma)) { 1929fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma) 1930fb912657SDouglas Gregor << SourceRange(ConflictLoc); 1931fb912657SDouglas Gregor return; 1932fb912657SDouglas Gregor } 1933fb912657SDouglas Gregor consumeToken(); 1934fb912657SDouglas Gregor 1935fb912657SDouglas Gregor // Parse the message. 1936fb912657SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1937fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message) 1938fb912657SDouglas Gregor << formatModuleId(Conflict.Id); 1939fb912657SDouglas Gregor return; 1940fb912657SDouglas Gregor } 1941fb912657SDouglas Gregor Conflict.Message = Tok.getString().str(); 1942fb912657SDouglas Gregor consumeToken(); 1943fb912657SDouglas Gregor 1944fb912657SDouglas Gregor // Add this unresolved conflict. 1945fb912657SDouglas Gregor ActiveModule->UnresolvedConflicts.push_back(Conflict); 1946fb912657SDouglas Gregor } 1947fb912657SDouglas Gregor 19486ddfca91SDouglas Gregor /// \brief Parse an inferred module declaration (wildcard modules). 19499194a91dSDouglas Gregor /// 19509194a91dSDouglas Gregor /// module-declaration: 19519194a91dSDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] 19529194a91dSDouglas Gregor /// { inferred-module-member* } 19539194a91dSDouglas Gregor /// 19549194a91dSDouglas Gregor /// inferred-module-member: 19559194a91dSDouglas Gregor /// 'export' '*' 19569194a91dSDouglas Gregor /// 'exclude' identifier 19579194a91dSDouglas Gregor void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { 195873441091SDouglas Gregor assert(Tok.is(MMToken::Star)); 195973441091SDouglas Gregor SourceLocation StarLoc = consumeToken(); 196073441091SDouglas Gregor bool Failed = false; 196173441091SDouglas Gregor 196273441091SDouglas Gregor // Inferred modules must be submodules. 19639194a91dSDouglas Gregor if (!ActiveModule && !Framework) { 196473441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 196573441091SDouglas Gregor Failed = true; 196673441091SDouglas Gregor } 196773441091SDouglas Gregor 19689194a91dSDouglas Gregor if (ActiveModule) { 1969524e33e1SDouglas Gregor // Inferred modules must have umbrella directories. 1970524e33e1SDouglas Gregor if (!Failed && !ActiveModule->getUmbrellaDir()) { 197173441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 197273441091SDouglas Gregor Failed = true; 197373441091SDouglas Gregor } 197473441091SDouglas Gregor 197573441091SDouglas Gregor // Check for redefinition of an inferred module. 1976dd005f69SDouglas Gregor if (!Failed && ActiveModule->InferSubmodules) { 197773441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 1978dd005f69SDouglas Gregor if (ActiveModule->InferredSubmoduleLoc.isValid()) 1979dd005f69SDouglas Gregor Diags.Report(ActiveModule->InferredSubmoduleLoc, 198073441091SDouglas Gregor diag::note_mmap_prev_definition); 198173441091SDouglas Gregor Failed = true; 198273441091SDouglas Gregor } 198373441091SDouglas Gregor 19849194a91dSDouglas Gregor // Check for the 'framework' keyword, which is not permitted here. 19859194a91dSDouglas Gregor if (Framework) { 19869194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); 19879194a91dSDouglas Gregor Framework = false; 19889194a91dSDouglas Gregor } 19899194a91dSDouglas Gregor } else if (Explicit) { 19909194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); 19919194a91dSDouglas Gregor Explicit = false; 19929194a91dSDouglas Gregor } 19939194a91dSDouglas Gregor 199473441091SDouglas Gregor // If there were any problems with this inferred submodule, skip its body. 199573441091SDouglas Gregor if (Failed) { 199673441091SDouglas Gregor if (Tok.is(MMToken::LBrace)) { 199773441091SDouglas Gregor consumeToken(); 199873441091SDouglas Gregor skipUntil(MMToken::RBrace); 199973441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 200073441091SDouglas Gregor consumeToken(); 200173441091SDouglas Gregor } 200273441091SDouglas Gregor HadError = true; 200373441091SDouglas Gregor return; 200473441091SDouglas Gregor } 200573441091SDouglas Gregor 20069194a91dSDouglas Gregor // Parse optional attributes. 20074442605fSBill Wendling Attributes Attrs; 20089194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 20099194a91dSDouglas Gregor 20109194a91dSDouglas Gregor if (ActiveModule) { 201173441091SDouglas Gregor // Note that we have an inferred submodule. 2012dd005f69SDouglas Gregor ActiveModule->InferSubmodules = true; 2013dd005f69SDouglas Gregor ActiveModule->InferredSubmoduleLoc = StarLoc; 2014dd005f69SDouglas Gregor ActiveModule->InferExplicitSubmodules = Explicit; 20159194a91dSDouglas Gregor } else { 20169194a91dSDouglas Gregor // We'll be inferring framework modules for this directory. 20179194a91dSDouglas Gregor Map.InferredDirectories[Directory].InferModules = true; 20189194a91dSDouglas Gregor Map.InferredDirectories[Directory].InferSystemModules = Attrs.IsSystem; 2019131daca0SRichard Smith // FIXME: Handle the 'framework' keyword. 20209194a91dSDouglas Gregor } 202173441091SDouglas Gregor 202273441091SDouglas Gregor // Parse the opening brace. 202373441091SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 202473441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 202573441091SDouglas Gregor HadError = true; 202673441091SDouglas Gregor return; 202773441091SDouglas Gregor } 202873441091SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 202973441091SDouglas Gregor 203073441091SDouglas Gregor // Parse the body of the inferred submodule. 203173441091SDouglas Gregor bool Done = false; 203273441091SDouglas Gregor do { 203373441091SDouglas Gregor switch (Tok.Kind) { 203473441091SDouglas Gregor case MMToken::EndOfFile: 203573441091SDouglas Gregor case MMToken::RBrace: 203673441091SDouglas Gregor Done = true; 203773441091SDouglas Gregor break; 203873441091SDouglas Gregor 20399194a91dSDouglas Gregor case MMToken::ExcludeKeyword: { 20409194a91dSDouglas Gregor if (ActiveModule) { 20419194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2042162405daSDouglas Gregor << (ActiveModule != 0); 20439194a91dSDouglas Gregor consumeToken(); 20449194a91dSDouglas Gregor break; 20459194a91dSDouglas Gregor } 20469194a91dSDouglas Gregor 20479194a91dSDouglas Gregor consumeToken(); 20489194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 20499194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); 20509194a91dSDouglas Gregor break; 20519194a91dSDouglas Gregor } 20529194a91dSDouglas Gregor 20539194a91dSDouglas Gregor Map.InferredDirectories[Directory].ExcludedModules 20549194a91dSDouglas Gregor .push_back(Tok.getString()); 20559194a91dSDouglas Gregor consumeToken(); 20569194a91dSDouglas Gregor break; 20579194a91dSDouglas Gregor } 20589194a91dSDouglas Gregor 20599194a91dSDouglas Gregor case MMToken::ExportKeyword: 20609194a91dSDouglas Gregor if (!ActiveModule) { 20619194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2062162405daSDouglas Gregor << (ActiveModule != 0); 20639194a91dSDouglas Gregor consumeToken(); 20649194a91dSDouglas Gregor break; 20659194a91dSDouglas Gregor } 20669194a91dSDouglas Gregor 206773441091SDouglas Gregor consumeToken(); 206873441091SDouglas Gregor if (Tok.is(MMToken::Star)) 2069dd005f69SDouglas Gregor ActiveModule->InferExportWildcard = true; 207073441091SDouglas Gregor else 207173441091SDouglas Gregor Diags.Report(Tok.getLocation(), 207273441091SDouglas Gregor diag::err_mmap_expected_export_wildcard); 207373441091SDouglas Gregor consumeToken(); 207473441091SDouglas Gregor break; 207573441091SDouglas Gregor 207673441091SDouglas Gregor case MMToken::ExplicitKeyword: 207773441091SDouglas Gregor case MMToken::ModuleKeyword: 207873441091SDouglas Gregor case MMToken::HeaderKeyword: 2079b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 208073441091SDouglas Gregor case MMToken::UmbrellaKeyword: 208173441091SDouglas Gregor default: 20829194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2083162405daSDouglas Gregor << (ActiveModule != 0); 208473441091SDouglas Gregor consumeToken(); 208573441091SDouglas Gregor break; 208673441091SDouglas Gregor } 208773441091SDouglas Gregor } while (!Done); 208873441091SDouglas Gregor 208973441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 209073441091SDouglas Gregor consumeToken(); 209173441091SDouglas Gregor else { 209273441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 209373441091SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 209473441091SDouglas Gregor HadError = true; 209573441091SDouglas Gregor } 209673441091SDouglas Gregor } 209773441091SDouglas Gregor 20989194a91dSDouglas Gregor /// \brief Parse optional attributes. 20999194a91dSDouglas Gregor /// 21009194a91dSDouglas Gregor /// attributes: 21019194a91dSDouglas Gregor /// attribute attributes 21029194a91dSDouglas Gregor /// attribute 21039194a91dSDouglas Gregor /// 21049194a91dSDouglas Gregor /// attribute: 21059194a91dSDouglas Gregor /// [ identifier ] 21069194a91dSDouglas Gregor /// 21079194a91dSDouglas Gregor /// \param Attrs Will be filled in with the parsed attributes. 21089194a91dSDouglas Gregor /// 21099194a91dSDouglas Gregor /// \returns true if an error occurred, false otherwise. 21104442605fSBill Wendling bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { 21119194a91dSDouglas Gregor bool HadError = false; 21129194a91dSDouglas Gregor 21139194a91dSDouglas Gregor while (Tok.is(MMToken::LSquare)) { 21149194a91dSDouglas Gregor // Consume the '['. 21159194a91dSDouglas Gregor SourceLocation LSquareLoc = consumeToken(); 21169194a91dSDouglas Gregor 21179194a91dSDouglas Gregor // Check whether we have an attribute name here. 21189194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 21199194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 21209194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 21219194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 21229194a91dSDouglas Gregor consumeToken(); 21239194a91dSDouglas Gregor HadError = true; 21249194a91dSDouglas Gregor } 21259194a91dSDouglas Gregor 21269194a91dSDouglas Gregor // Decode the attribute name. 21279194a91dSDouglas Gregor AttributeKind Attribute 21289194a91dSDouglas Gregor = llvm::StringSwitch<AttributeKind>(Tok.getString()) 212935b13eceSDouglas Gregor .Case("exhaustive", AT_exhaustive) 213077944868SRichard Smith .Case("extern_c", AT_extern_c) 21319194a91dSDouglas Gregor .Case("system", AT_system) 21329194a91dSDouglas Gregor .Default(AT_unknown); 21339194a91dSDouglas Gregor switch (Attribute) { 21349194a91dSDouglas Gregor case AT_unknown: 21359194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 21369194a91dSDouglas Gregor << Tok.getString(); 21379194a91dSDouglas Gregor break; 21389194a91dSDouglas Gregor 21399194a91dSDouglas Gregor case AT_system: 21409194a91dSDouglas Gregor Attrs.IsSystem = true; 21419194a91dSDouglas Gregor break; 214235b13eceSDouglas Gregor 214377944868SRichard Smith case AT_extern_c: 214477944868SRichard Smith Attrs.IsExternC = true; 214577944868SRichard Smith break; 214677944868SRichard Smith 214735b13eceSDouglas Gregor case AT_exhaustive: 214835b13eceSDouglas Gregor Attrs.IsExhaustive = true; 214935b13eceSDouglas Gregor break; 21509194a91dSDouglas Gregor } 21519194a91dSDouglas Gregor consumeToken(); 21529194a91dSDouglas Gregor 21539194a91dSDouglas Gregor // Consume the ']'. 21549194a91dSDouglas Gregor if (!Tok.is(MMToken::RSquare)) { 21559194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 21569194a91dSDouglas Gregor Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 21579194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 21589194a91dSDouglas Gregor HadError = true; 21599194a91dSDouglas Gregor } 21609194a91dSDouglas Gregor 21619194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 21629194a91dSDouglas Gregor consumeToken(); 21639194a91dSDouglas Gregor } 21649194a91dSDouglas Gregor 21659194a91dSDouglas Gregor return HadError; 21669194a91dSDouglas Gregor } 21679194a91dSDouglas Gregor 21687033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence 21697033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null. 21707033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() { 21717033127bSDouglas Gregor for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) { 21727033127bSDouglas Gregor // If we have an umbrella directory, use that. 21737033127bSDouglas Gregor if (Mod->hasUmbrellaDir()) 21747033127bSDouglas Gregor return Mod->getUmbrellaDir(); 21757033127bSDouglas Gregor 21767033127bSDouglas Gregor // If we have a framework directory, stop looking. 21777033127bSDouglas Gregor if (Mod->IsFramework) 21787033127bSDouglas Gregor return 0; 21797033127bSDouglas Gregor } 21807033127bSDouglas Gregor 21817033127bSDouglas Gregor return 0; 21827033127bSDouglas Gregor } 21837033127bSDouglas Gregor 2184718292f2SDouglas Gregor /// \brief Parse a module map file. 2185718292f2SDouglas Gregor /// 2186718292f2SDouglas Gregor /// module-map-file: 2187718292f2SDouglas Gregor /// module-declaration* 2188718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() { 2189718292f2SDouglas Gregor do { 2190718292f2SDouglas Gregor switch (Tok.Kind) { 2191718292f2SDouglas Gregor case MMToken::EndOfFile: 2192718292f2SDouglas Gregor return HadError; 2193718292f2SDouglas Gregor 2194e7ab3669SDouglas Gregor case MMToken::ExplicitKeyword: 219597292843SDaniel Jasper case MMToken::ExternKeyword: 2196718292f2SDouglas Gregor case MMToken::ModuleKeyword: 2197755b2055SDouglas Gregor case MMToken::FrameworkKeyword: 2198718292f2SDouglas Gregor parseModuleDecl(); 2199718292f2SDouglas Gregor break; 2200718292f2SDouglas Gregor 22011fb5c3a6SDouglas Gregor case MMToken::Comma: 220235b13eceSDouglas Gregor case MMToken::ConfigMacros: 2203fb912657SDouglas Gregor case MMToken::Conflict: 2204a3feee2aSRichard Smith case MMToken::Exclaim: 220559527666SDouglas Gregor case MMToken::ExcludeKeyword: 22062b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 2207718292f2SDouglas Gregor case MMToken::HeaderKeyword: 2208718292f2SDouglas Gregor case MMToken::Identifier: 2209718292f2SDouglas Gregor case MMToken::LBrace: 22106ddfca91SDouglas Gregor case MMToken::LinkKeyword: 2211a686e1b0SDouglas Gregor case MMToken::LSquare: 22122b82c2a5SDouglas Gregor case MMToken::Period: 2213b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 2214718292f2SDouglas Gregor case MMToken::RBrace: 2215a686e1b0SDouglas Gregor case MMToken::RSquare: 22161fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 22172b82c2a5SDouglas Gregor case MMToken::Star: 2218718292f2SDouglas Gregor case MMToken::StringLiteral: 2219718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 2220ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 2221718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 2222718292f2SDouglas Gregor HadError = true; 2223718292f2SDouglas Gregor consumeToken(); 2224718292f2SDouglas Gregor break; 2225718292f2SDouglas Gregor } 2226718292f2SDouglas Gregor } while (true); 2227718292f2SDouglas Gregor } 2228718292f2SDouglas Gregor 2229963c5535SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem) { 22304ddf2221SDouglas Gregor llvm::DenseMap<const FileEntry *, bool>::iterator Known 22314ddf2221SDouglas Gregor = ParsedModuleMap.find(File); 22324ddf2221SDouglas Gregor if (Known != ParsedModuleMap.end()) 22334ddf2221SDouglas Gregor return Known->second; 22344ddf2221SDouglas Gregor 223589929282SDouglas Gregor assert(Target != 0 && "Missing target information"); 2236cb69b57bSBen Langmuir auto FileCharacter = IsSystem ? SrcMgr::C_System : SrcMgr::C_User; 2237cb69b57bSBen Langmuir FileID ID = SourceMgr.createFileID(File, SourceLocation(), FileCharacter); 22381f76c4e8SManuel Klimek const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID); 2239718292f2SDouglas Gregor if (!Buffer) 22404ddf2221SDouglas Gregor return ParsedModuleMap[File] = true; 2241718292f2SDouglas Gregor 2242984e1df7SBen Langmuir // Find the directory for the module. For frameworks, that may require going 2243984e1df7SBen Langmuir // up from the 'Modules' directory. 2244984e1df7SBen Langmuir const DirectoryEntry *Dir = File->getDir(); 2245984e1df7SBen Langmuir StringRef DirName(Dir->getName()); 2246984e1df7SBen Langmuir if (llvm::sys::path::filename(DirName) == "Modules") { 2247984e1df7SBen Langmuir DirName = llvm::sys::path::parent_path(DirName); 2248984e1df7SBen Langmuir if (DirName.endswith(".framework")) 2249984e1df7SBen Langmuir Dir = SourceMgr.getFileManager().getDirectory(DirName); 2250984e1df7SBen Langmuir assert(Dir && "parent must exist"); 2251984e1df7SBen Langmuir } 2252984e1df7SBen Langmuir 2253718292f2SDouglas Gregor // Parse this module map file. 22541f76c4e8SManuel Klimek Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts); 2255984e1df7SBen Langmuir ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, Dir, 2256963c5535SDouglas Gregor BuiltinIncludeDir, IsSystem); 2257718292f2SDouglas Gregor bool Result = Parser.parseModuleMapFile(); 22584ddf2221SDouglas Gregor ParsedModuleMap[File] = Result; 2259718292f2SDouglas Gregor return Result; 2260718292f2SDouglas Gregor } 2261