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 17392669ee4SDaniel Jasper // Returns 'true' if 'RequestingModule directly uses 'RequestedModule'. 17492669ee4SDaniel Jasper static bool directlyUses(const Module *RequestingModule, 17592669ee4SDaniel Jasper const Module *RequestedModule) { 17692669ee4SDaniel Jasper return std::find(RequestingModule->DirectUses.begin(), 17792669ee4SDaniel Jasper RequestingModule->DirectUses.end(), 17892669ee4SDaniel Jasper RequestedModule) != RequestingModule->DirectUses.end(); 17992669ee4SDaniel Jasper } 18092669ee4SDaniel Jasper 18192669ee4SDaniel Jasper static bool violatesPrivateInclude(Module *RequestingModule, 18292669ee4SDaniel Jasper const FileEntry *IncFileEnt, 18392669ee4SDaniel Jasper ModuleMap::ModuleHeaderRole Role, 18492669ee4SDaniel Jasper Module *RequestedModule) { 18592669ee4SDaniel Jasper #ifndef NDEBUG 18692669ee4SDaniel Jasper // Check for consistency between the module header role 18792669ee4SDaniel Jasper // as obtained from the lookup and as obtained from the module. 18892669ee4SDaniel Jasper // This check is not cheap, so enable it only for debugging. 18992669ee4SDaniel Jasper SmallVectorImpl<const FileEntry *> &PvtHdrs 19092669ee4SDaniel Jasper = RequestedModule->PrivateHeaders; 19192669ee4SDaniel Jasper SmallVectorImpl<const FileEntry *>::iterator Look 19292669ee4SDaniel Jasper = std::find(PvtHdrs.begin(), PvtHdrs.end(), IncFileEnt); 19392669ee4SDaniel Jasper bool IsPrivate = Look != PvtHdrs.end(); 19492669ee4SDaniel Jasper assert((IsPrivate && Role == ModuleMap::PrivateHeader) 19592669ee4SDaniel Jasper || (!IsPrivate && Role != ModuleMap::PrivateHeader)); 19692669ee4SDaniel Jasper #endif 19792669ee4SDaniel Jasper return Role == ModuleMap::PrivateHeader && 19892669ee4SDaniel Jasper RequestedModule->getTopLevelModule() != RequestingModule; 19992669ee4SDaniel Jasper } 20092669ee4SDaniel Jasper 20192669ee4SDaniel Jasper void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule, 20292669ee4SDaniel Jasper SourceLocation FilenameLoc, 20392669ee4SDaniel Jasper StringRef Filename, 20492669ee4SDaniel Jasper const FileEntry *File) { 20592669ee4SDaniel Jasper // No errors for indirect modules. This may be a bit of a problem for modules 20692669ee4SDaniel Jasper // with no source files. 20792669ee4SDaniel Jasper if (RequestingModule != SourceModule) 20892669ee4SDaniel Jasper return; 20992669ee4SDaniel Jasper 21092669ee4SDaniel Jasper if (RequestingModule) 21192669ee4SDaniel Jasper resolveUses(RequestingModule, /*Complain=*/false); 21292669ee4SDaniel Jasper 21392669ee4SDaniel Jasper HeadersMap::iterator Known = findKnownHeader(File); 21492669ee4SDaniel Jasper if (Known == Headers.end()) 21592669ee4SDaniel Jasper return; 21692669ee4SDaniel Jasper 21792669ee4SDaniel Jasper Module *Private = NULL; 21892669ee4SDaniel Jasper Module *NotUsed = NULL; 21992669ee4SDaniel Jasper for (SmallVectorImpl<KnownHeader>::iterator I = Known->second.begin(), 22092669ee4SDaniel Jasper E = Known->second.end(); 22192669ee4SDaniel Jasper I != E; ++I) { 22292669ee4SDaniel Jasper // Excluded headers don't really belong to a module. 22392669ee4SDaniel Jasper if (I->getRole() == ModuleMap::ExcludedHeader) 22492669ee4SDaniel Jasper continue; 22592669ee4SDaniel Jasper 22692669ee4SDaniel Jasper // If 'File' is part of 'RequestingModule' we can definitely include it. 22792669ee4SDaniel Jasper if (I->getModule() == RequestingModule) 22892669ee4SDaniel Jasper return; 22992669ee4SDaniel Jasper 23092669ee4SDaniel Jasper // Remember private headers for later printing of a diagnostic. 23192669ee4SDaniel Jasper if (violatesPrivateInclude(RequestingModule, File, I->getRole(), 23292669ee4SDaniel Jasper I->getModule())) { 23392669ee4SDaniel Jasper Private = I->getModule(); 23492669ee4SDaniel Jasper continue; 23592669ee4SDaniel Jasper } 23692669ee4SDaniel Jasper 23792669ee4SDaniel Jasper // If uses need to be specified explicitly, we are only allowed to return 23892669ee4SDaniel Jasper // modules that are explicitly used by the requesting module. 23992669ee4SDaniel Jasper if (RequestingModule && LangOpts.ModulesDeclUse && 24092669ee4SDaniel Jasper !directlyUses(RequestingModule, I->getModule())) { 24192669ee4SDaniel Jasper NotUsed = I->getModule(); 24292669ee4SDaniel Jasper continue; 24392669ee4SDaniel Jasper } 24492669ee4SDaniel Jasper 24592669ee4SDaniel Jasper // We have found a module that we can happily use. 24692669ee4SDaniel Jasper return; 24792669ee4SDaniel Jasper } 24892669ee4SDaniel Jasper 24992669ee4SDaniel Jasper // We have found a header, but it is private. 25092669ee4SDaniel Jasper if (Private != NULL) { 25192669ee4SDaniel Jasper Diags.Report(FilenameLoc, diag::error_use_of_private_header_outside_module) 25292669ee4SDaniel Jasper << Filename; 25392669ee4SDaniel Jasper return; 25492669ee4SDaniel Jasper } 25592669ee4SDaniel Jasper 25692669ee4SDaniel Jasper // We have found a module, but we don't use it. 25792669ee4SDaniel Jasper if (NotUsed != NULL) { 25892669ee4SDaniel Jasper Diags.Report(FilenameLoc, diag::error_undeclared_use_of_module) 25992669ee4SDaniel Jasper << RequestingModule->getFullModuleName() << Filename; 26092669ee4SDaniel Jasper return; 26192669ee4SDaniel Jasper } 26292669ee4SDaniel Jasper 26392669ee4SDaniel Jasper // Headers for which we have not found a module are fine to include. 26492669ee4SDaniel Jasper } 26592669ee4SDaniel Jasper 26692669ee4SDaniel Jasper ModuleMap::KnownHeader 26792669ee4SDaniel Jasper ModuleMap::findModuleForHeader(const FileEntry *File, 26892669ee4SDaniel Jasper Module *RequestingModule) { 26992669ee4SDaniel Jasper HeadersMap::iterator Known = findKnownHeader(File); 2704eaf0a6cSDaniel Jasper 2711fb5c3a6SDouglas Gregor if (Known != Headers.end()) { 27297da9178SDaniel Jasper ModuleMap::KnownHeader Result = KnownHeader(); 2731fb5c3a6SDouglas Gregor 27497da9178SDaniel Jasper // Iterate over all modules that 'File' is part of to find the best fit. 27597da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::iterator I = Known->second.begin(), 27697da9178SDaniel Jasper E = Known->second.end(); 27797da9178SDaniel Jasper I != E; ++I) { 2784eaf0a6cSDaniel Jasper // Cannot use a module if the header is excluded in it. 2794eaf0a6cSDaniel Jasper if (I->getRole() == ModuleMap::ExcludedHeader) 2804eaf0a6cSDaniel Jasper continue; 2814eaf0a6cSDaniel Jasper 2824eaf0a6cSDaniel Jasper // Cannot use a module if it is unavailable. 2834eaf0a6cSDaniel Jasper if (!I->getModule()->isAvailable()) 28497da9178SDaniel Jasper continue; 28597da9178SDaniel Jasper 28697da9178SDaniel Jasper // If 'File' is part of 'RequestingModule', 'RequestingModule' is the 28797da9178SDaniel Jasper // module we are looking for. 28897da9178SDaniel Jasper if (I->getModule() == RequestingModule) 28997da9178SDaniel Jasper return *I; 29097da9178SDaniel Jasper 29197da9178SDaniel Jasper // If uses need to be specified explicitly, we are only allowed to return 29297da9178SDaniel Jasper // modules that are explicitly used by the requesting module. 29397da9178SDaniel Jasper if (RequestingModule && LangOpts.ModulesDeclUse && 29492669ee4SDaniel Jasper !directlyUses(RequestingModule, I->getModule())) 29597da9178SDaniel Jasper continue; 2964eaf0a6cSDaniel Jasper 29797da9178SDaniel Jasper Result = *I; 29897da9178SDaniel Jasper // If 'File' is a public header of this module, this is as good as we 29997da9178SDaniel Jasper // are going to get. 3008c71eba1SRichard Smith // FIXME: If we have a RequestingModule, we should prefer the header from 3018c71eba1SRichard Smith // that module. 30297da9178SDaniel Jasper if (I->getRole() == ModuleMap::NormalHeader) 30397da9178SDaniel Jasper break; 30497da9178SDaniel Jasper } 30597da9178SDaniel Jasper return Result; 3061fb5c3a6SDouglas Gregor } 307ab0c8a84SDouglas Gregor 308b65dbfffSDouglas Gregor const DirectoryEntry *Dir = File->getDir(); 309f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 310e00c8b20SDouglas Gregor 31174260502SDouglas Gregor // Note: as an egregious but useful hack we use the real path here, because 31274260502SDouglas Gregor // frameworks moving from top-level frameworks to embedded frameworks tend 31374260502SDouglas Gregor // to be symlinked from the top-level location to the embedded location, 31474260502SDouglas Gregor // and we need to resolve lookups as if we had found the embedded location. 3151f76c4e8SManuel Klimek StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir); 316a89c5ac4SDouglas Gregor 317a89c5ac4SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 318a89c5ac4SDouglas Gregor // an umbrella header. 319b65dbfffSDouglas Gregor do { 320a89c5ac4SDouglas Gregor llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir 321a89c5ac4SDouglas Gregor = UmbrellaDirs.find(Dir); 322a89c5ac4SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 323a89c5ac4SDouglas Gregor Module *Result = KnownDir->second; 324930a85ccSDouglas Gregor 325930a85ccSDouglas Gregor // Search up the module stack until we find a module with an umbrella 32673141fa9SDouglas Gregor // directory. 327930a85ccSDouglas Gregor Module *UmbrellaModule = Result; 32873141fa9SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 329930a85ccSDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 330930a85ccSDouglas Gregor 331930a85ccSDouglas Gregor if (UmbrellaModule->InferSubmodules) { 332a89c5ac4SDouglas Gregor // Infer submodules for each of the directories we found between 333a89c5ac4SDouglas Gregor // the directory of the umbrella header and the directory where 334a89c5ac4SDouglas Gregor // the actual header is located. 3359458f82dSDouglas Gregor bool Explicit = UmbrellaModule->InferExplicitSubmodules; 3369458f82dSDouglas Gregor 3377033127bSDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 338a89c5ac4SDouglas Gregor // Find or create the module that corresponds to this directory name. 339056396aeSDouglas Gregor SmallString<32> NameBuf; 340056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 341056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 342056396aeSDouglas Gregor NameBuf); 343a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 3449458f82dSDouglas Gregor Explicit).first; 345a89c5ac4SDouglas Gregor 346a89c5ac4SDouglas Gregor // Associate the module and the directory. 347a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I-1]] = Result; 348a89c5ac4SDouglas Gregor 349a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 350a89c5ac4SDouglas Gregor // wildcard to the set of exports. 351930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 352a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 353a89c5ac4SDouglas Gregor } 354a89c5ac4SDouglas Gregor 355a89c5ac4SDouglas Gregor // Infer a submodule with the same name as this header file. 356056396aeSDouglas Gregor SmallString<32> NameBuf; 357056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 358056396aeSDouglas Gregor llvm::sys::path::stem(File->getName()), NameBuf); 359a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 3609458f82dSDouglas Gregor Explicit).first; 3613c5305c1SArgyrios Kyrtzidis Result->addTopHeader(File); 362a89c5ac4SDouglas Gregor 363a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 364a89c5ac4SDouglas Gregor // wildcard to the set of exports. 365930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 366a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 367a89c5ac4SDouglas Gregor } else { 368a89c5ac4SDouglas Gregor // Record each of the directories we stepped through as being part of 369a89c5ac4SDouglas Gregor // the module we found, since the umbrella header covers them all. 370a89c5ac4SDouglas Gregor for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 371a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I]] = Result; 372a89c5ac4SDouglas Gregor } 373a89c5ac4SDouglas Gregor 37497da9178SDaniel Jasper Headers[File].push_back(KnownHeader(Result, NormalHeader)); 3751fb5c3a6SDouglas Gregor 3761fb5c3a6SDouglas Gregor // If a header corresponds to an unavailable module, don't report 3771fb5c3a6SDouglas Gregor // that it maps to anything. 3781fb5c3a6SDouglas Gregor if (!Result->isAvailable()) 379b53e5483SLawrence Crowl return KnownHeader(); 3801fb5c3a6SDouglas Gregor 38197da9178SDaniel Jasper return Headers[File].back(); 382a89c5ac4SDouglas Gregor } 383a89c5ac4SDouglas Gregor 384a89c5ac4SDouglas Gregor SkippedDirs.push_back(Dir); 385a89c5ac4SDouglas Gregor 386b65dbfffSDouglas Gregor // Retrieve our parent path. 387b65dbfffSDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 388b65dbfffSDouglas Gregor if (DirName.empty()) 389b65dbfffSDouglas Gregor break; 390b65dbfffSDouglas Gregor 391b65dbfffSDouglas Gregor // Resolve the parent path to a directory entry. 3921f76c4e8SManuel Klimek Dir = SourceMgr.getFileManager().getDirectory(DirName); 393a89c5ac4SDouglas Gregor } while (Dir); 394b65dbfffSDouglas Gregor 395b53e5483SLawrence Crowl return KnownHeader(); 396ab0c8a84SDouglas Gregor } 397ab0c8a84SDouglas Gregor 398e4412640SArgyrios Kyrtzidis bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const { 399e4412640SArgyrios Kyrtzidis HeadersMap::const_iterator Known = Headers.find(Header); 40097da9178SDaniel Jasper if (Known != Headers.end()) { 40197da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::const_iterator 40297da9178SDaniel Jasper I = Known->second.begin(), 40397da9178SDaniel Jasper E = Known->second.end(); 40497da9178SDaniel Jasper I != E; ++I) { 40597da9178SDaniel Jasper if (I->isAvailable()) 40697da9178SDaniel Jasper return false; 40797da9178SDaniel Jasper } 40897da9178SDaniel Jasper return true; 40997da9178SDaniel Jasper } 4101fb5c3a6SDouglas Gregor 4111fb5c3a6SDouglas Gregor const DirectoryEntry *Dir = Header->getDir(); 412f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 4131fb5c3a6SDouglas Gregor StringRef DirName = Dir->getName(); 4141fb5c3a6SDouglas Gregor 4151fb5c3a6SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 4161fb5c3a6SDouglas Gregor // an umbrella header. 4171fb5c3a6SDouglas Gregor do { 418e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir 4191fb5c3a6SDouglas Gregor = UmbrellaDirs.find(Dir); 4201fb5c3a6SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 4211fb5c3a6SDouglas Gregor Module *Found = KnownDir->second; 4221fb5c3a6SDouglas Gregor if (!Found->isAvailable()) 4231fb5c3a6SDouglas Gregor return true; 4241fb5c3a6SDouglas Gregor 4251fb5c3a6SDouglas Gregor // Search up the module stack until we find a module with an umbrella 4261fb5c3a6SDouglas Gregor // directory. 4271fb5c3a6SDouglas Gregor Module *UmbrellaModule = Found; 4281fb5c3a6SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 4291fb5c3a6SDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 4301fb5c3a6SDouglas Gregor 4311fb5c3a6SDouglas Gregor if (UmbrellaModule->InferSubmodules) { 4321fb5c3a6SDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 4331fb5c3a6SDouglas Gregor // Find or create the module that corresponds to this directory name. 434056396aeSDouglas Gregor SmallString<32> NameBuf; 435056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 436056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 437056396aeSDouglas Gregor NameBuf); 4381fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 4391fb5c3a6SDouglas Gregor if (!Found) 4401fb5c3a6SDouglas Gregor return false; 4411fb5c3a6SDouglas Gregor if (!Found->isAvailable()) 4421fb5c3a6SDouglas Gregor return true; 4431fb5c3a6SDouglas Gregor } 4441fb5c3a6SDouglas Gregor 4451fb5c3a6SDouglas Gregor // Infer a submodule with the same name as this header file. 446056396aeSDouglas Gregor SmallString<32> NameBuf; 447056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 448056396aeSDouglas Gregor llvm::sys::path::stem(Header->getName()), 449056396aeSDouglas Gregor NameBuf); 4501fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 4511fb5c3a6SDouglas Gregor if (!Found) 4521fb5c3a6SDouglas Gregor return false; 4531fb5c3a6SDouglas Gregor } 4541fb5c3a6SDouglas Gregor 4551fb5c3a6SDouglas Gregor return !Found->isAvailable(); 4561fb5c3a6SDouglas Gregor } 4571fb5c3a6SDouglas Gregor 4581fb5c3a6SDouglas Gregor SkippedDirs.push_back(Dir); 4591fb5c3a6SDouglas Gregor 4601fb5c3a6SDouglas Gregor // Retrieve our parent path. 4611fb5c3a6SDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 4621fb5c3a6SDouglas Gregor if (DirName.empty()) 4631fb5c3a6SDouglas Gregor break; 4641fb5c3a6SDouglas Gregor 4651fb5c3a6SDouglas Gregor // Resolve the parent path to a directory entry. 4661f76c4e8SManuel Klimek Dir = SourceMgr.getFileManager().getDirectory(DirName); 4671fb5c3a6SDouglas Gregor } while (Dir); 4681fb5c3a6SDouglas Gregor 4691fb5c3a6SDouglas Gregor return false; 4701fb5c3a6SDouglas Gregor } 4711fb5c3a6SDouglas Gregor 472e4412640SArgyrios Kyrtzidis Module *ModuleMap::findModule(StringRef Name) const { 473e4412640SArgyrios Kyrtzidis llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name); 47488bdfb0eSDouglas Gregor if (Known != Modules.end()) 47588bdfb0eSDouglas Gregor return Known->getValue(); 47688bdfb0eSDouglas Gregor 47788bdfb0eSDouglas Gregor return 0; 47888bdfb0eSDouglas Gregor } 47988bdfb0eSDouglas Gregor 480e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleUnqualified(StringRef Name, 481e4412640SArgyrios Kyrtzidis Module *Context) const { 4822b82c2a5SDouglas Gregor for(; Context; Context = Context->Parent) { 4832b82c2a5SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Context)) 4842b82c2a5SDouglas Gregor return Sub; 4852b82c2a5SDouglas Gregor } 4862b82c2a5SDouglas Gregor 4872b82c2a5SDouglas Gregor return findModule(Name); 4882b82c2a5SDouglas Gregor } 4892b82c2a5SDouglas Gregor 490e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{ 4912b82c2a5SDouglas Gregor if (!Context) 4922b82c2a5SDouglas Gregor return findModule(Name); 4932b82c2a5SDouglas Gregor 494eb90e830SDouglas Gregor return Context->findSubmodule(Name); 4952b82c2a5SDouglas Gregor } 4962b82c2a5SDouglas Gregor 497de3ef502SDouglas Gregor std::pair<Module *, bool> 49869021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 49969021974SDouglas Gregor bool IsExplicit) { 50069021974SDouglas Gregor // Try to find an existing module with this name. 501eb90e830SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Parent)) 502eb90e830SDouglas Gregor return std::make_pair(Sub, false); 50369021974SDouglas Gregor 50469021974SDouglas Gregor // Create a new module with this name. 50569021974SDouglas Gregor Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework, 50669021974SDouglas Gregor IsExplicit); 507ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == Name) { 508ba7f2f71SDaniel Jasper SourceModule = Result; 509ba7f2f71SDaniel Jasper SourceModuleName = Name; 510ba7f2f71SDaniel Jasper } 5116f722b4eSArgyrios Kyrtzidis if (!Parent) { 51269021974SDouglas Gregor Modules[Name] = Result; 5136f722b4eSArgyrios Kyrtzidis if (!LangOpts.CurrentModule.empty() && !CompilingModule && 5146f722b4eSArgyrios Kyrtzidis Name == LangOpts.CurrentModule) { 5156f722b4eSArgyrios Kyrtzidis CompilingModule = Result; 5166f722b4eSArgyrios Kyrtzidis } 5176f722b4eSArgyrios Kyrtzidis } 51869021974SDouglas Gregor return std::make_pair(Result, true); 51969021974SDouglas Gregor } 52069021974SDouglas Gregor 5219194a91dSDouglas Gregor bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir, 522e4412640SArgyrios Kyrtzidis StringRef Name, bool &IsSystem) const { 5239194a91dSDouglas Gregor // Check whether we have already looked into the parent directory 5249194a91dSDouglas Gregor // for a module map. 525e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 5269194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 5279194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) 5289194a91dSDouglas Gregor return false; 5299194a91dSDouglas Gregor 5309194a91dSDouglas Gregor if (!inferred->second.InferModules) 5319194a91dSDouglas Gregor return false; 5329194a91dSDouglas Gregor 5339194a91dSDouglas Gregor // We're allowed to infer for this directory, but make sure it's okay 5349194a91dSDouglas Gregor // to infer this particular module. 5359194a91dSDouglas Gregor bool canInfer = std::find(inferred->second.ExcludedModules.begin(), 5369194a91dSDouglas Gregor inferred->second.ExcludedModules.end(), 5379194a91dSDouglas Gregor Name) == inferred->second.ExcludedModules.end(); 5389194a91dSDouglas Gregor 5399194a91dSDouglas Gregor if (canInfer && inferred->second.InferSystemModules) 5409194a91dSDouglas Gregor IsSystem = true; 5419194a91dSDouglas Gregor 5429194a91dSDouglas Gregor return canInfer; 5439194a91dSDouglas Gregor } 5449194a91dSDouglas Gregor 54511dfe6feSDouglas Gregor /// \brief For a framework module, infer the framework against which we 54611dfe6feSDouglas Gregor /// should link. 54711dfe6feSDouglas Gregor static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir, 54811dfe6feSDouglas Gregor FileManager &FileMgr) { 54911dfe6feSDouglas Gregor assert(Mod->IsFramework && "Can only infer linking for framework modules"); 55011dfe6feSDouglas Gregor assert(!Mod->isSubFramework() && 55111dfe6feSDouglas Gregor "Can only infer linking for top-level frameworks"); 55211dfe6feSDouglas Gregor 55311dfe6feSDouglas Gregor SmallString<128> LibName; 55411dfe6feSDouglas Gregor LibName += FrameworkDir->getName(); 55511dfe6feSDouglas Gregor llvm::sys::path::append(LibName, Mod->Name); 55611dfe6feSDouglas Gregor if (FileMgr.getFile(LibName)) { 55711dfe6feSDouglas Gregor Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name, 55811dfe6feSDouglas Gregor /*IsFramework=*/true)); 55911dfe6feSDouglas Gregor } 56011dfe6feSDouglas Gregor } 56111dfe6feSDouglas Gregor 562de3ef502SDouglas Gregor Module * 56356c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName, 564e89dbc1dSDouglas Gregor const DirectoryEntry *FrameworkDir, 565a686e1b0SDouglas Gregor bool IsSystem, 566e89dbc1dSDouglas Gregor Module *Parent) { 56756c64013SDouglas Gregor // Check whether we've already found this module. 568e89dbc1dSDouglas Gregor if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 569e89dbc1dSDouglas Gregor return Mod; 570e89dbc1dSDouglas Gregor 5711f76c4e8SManuel Klimek FileManager &FileMgr = SourceMgr.getFileManager(); 57256c64013SDouglas Gregor 5739194a91dSDouglas Gregor // If the framework has a parent path from which we're allowed to infer 5749194a91dSDouglas Gregor // a framework module, do so. 5759194a91dSDouglas Gregor if (!Parent) { 5764ddf2221SDouglas Gregor // Determine whether we're allowed to infer a module map. 577e00c8b20SDouglas Gregor 5784ddf2221SDouglas Gregor // Note: as an egregious but useful hack we use the real path here, because 5794ddf2221SDouglas Gregor // we might be looking at an embedded framework that symlinks out to a 5804ddf2221SDouglas Gregor // top-level framework, and we need to infer as if we were naming the 5814ddf2221SDouglas Gregor // top-level framework. 582e00c8b20SDouglas Gregor StringRef FrameworkDirName 5831f76c4e8SManuel Klimek = SourceMgr.getFileManager().getCanonicalName(FrameworkDir); 5844ddf2221SDouglas Gregor 5859194a91dSDouglas Gregor bool canInfer = false; 5864ddf2221SDouglas Gregor if (llvm::sys::path::has_parent_path(FrameworkDirName)) { 5879194a91dSDouglas Gregor // Figure out the parent path. 5884ddf2221SDouglas Gregor StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); 5899194a91dSDouglas Gregor if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) { 5909194a91dSDouglas Gregor // Check whether we have already looked into the parent directory 5919194a91dSDouglas Gregor // for a module map. 592e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 5939194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 5949194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) { 5959194a91dSDouglas Gregor // We haven't looked here before. Load a module map, if there is 5969194a91dSDouglas Gregor // one. 597*984e1df7SBen Langmuir bool IsFrameworkDir = Parent.endswith(".framework"); 598*984e1df7SBen Langmuir if (const FileEntry *ModMapFile = 599*984e1df7SBen Langmuir HeaderInfo.lookupModuleMapFile(ParentDir, IsFrameworkDir)) { 600963c5535SDouglas Gregor parseModuleMapFile(ModMapFile, IsSystem); 6019194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 6029194a91dSDouglas Gregor } 6039194a91dSDouglas Gregor 6049194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) 6059194a91dSDouglas Gregor inferred = InferredDirectories.insert( 6069194a91dSDouglas Gregor std::make_pair(ParentDir, InferredDirectory())).first; 6079194a91dSDouglas Gregor } 6089194a91dSDouglas Gregor 6099194a91dSDouglas Gregor if (inferred->second.InferModules) { 6109194a91dSDouglas Gregor // We're allowed to infer for this directory, but make sure it's okay 6119194a91dSDouglas Gregor // to infer this particular module. 6124ddf2221SDouglas Gregor StringRef Name = llvm::sys::path::stem(FrameworkDirName); 6139194a91dSDouglas Gregor canInfer = std::find(inferred->second.ExcludedModules.begin(), 6149194a91dSDouglas Gregor inferred->second.ExcludedModules.end(), 6159194a91dSDouglas Gregor Name) == inferred->second.ExcludedModules.end(); 6169194a91dSDouglas Gregor 6179194a91dSDouglas Gregor if (inferred->second.InferSystemModules) 6189194a91dSDouglas Gregor IsSystem = true; 6199194a91dSDouglas Gregor } 6209194a91dSDouglas Gregor } 6219194a91dSDouglas Gregor } 6229194a91dSDouglas Gregor 6239194a91dSDouglas Gregor // If we're not allowed to infer a framework module, don't. 6249194a91dSDouglas Gregor if (!canInfer) 6259194a91dSDouglas Gregor return 0; 6269194a91dSDouglas Gregor } 6279194a91dSDouglas Gregor 6289194a91dSDouglas Gregor 62956c64013SDouglas Gregor // Look for an umbrella header. 6302c1dd271SDylan Noblesmith SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 63117381a06SBenjamin Kramer llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h"); 632e89dbc1dSDouglas Gregor const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 63356c64013SDouglas Gregor 63456c64013SDouglas Gregor // FIXME: If there's no umbrella header, we could probably scan the 63556c64013SDouglas Gregor // framework to load *everything*. But, it's not clear that this is a good 63656c64013SDouglas Gregor // idea. 63756c64013SDouglas Gregor if (!UmbrellaHeader) 63856c64013SDouglas Gregor return 0; 63956c64013SDouglas Gregor 640e89dbc1dSDouglas Gregor Module *Result = new Module(ModuleName, SourceLocation(), Parent, 641e89dbc1dSDouglas Gregor /*IsFramework=*/true, /*IsExplicit=*/false); 642ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == ModuleName) { 643ba7f2f71SDaniel Jasper SourceModule = Result; 644ba7f2f71SDaniel Jasper SourceModuleName = ModuleName; 645ba7f2f71SDaniel Jasper } 646a686e1b0SDouglas Gregor if (IsSystem) 647a686e1b0SDouglas Gregor Result->IsSystem = IsSystem; 648a686e1b0SDouglas Gregor 649eb90e830SDouglas Gregor if (!Parent) 650e89dbc1dSDouglas Gregor Modules[ModuleName] = Result; 651e89dbc1dSDouglas Gregor 652322f633cSDouglas Gregor // umbrella header "umbrella-header-name" 65373141fa9SDouglas Gregor Result->Umbrella = UmbrellaHeader; 65497da9178SDaniel Jasper Headers[UmbrellaHeader].push_back(KnownHeader(Result, NormalHeader)); 6554dc71835SDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Result; 656d8bd7537SDouglas Gregor 657d8bd7537SDouglas Gregor // export * 658d8bd7537SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 659d8bd7537SDouglas Gregor 660a89c5ac4SDouglas Gregor // module * { export * } 661a89c5ac4SDouglas Gregor Result->InferSubmodules = true; 662a89c5ac4SDouglas Gregor Result->InferExportWildcard = true; 663a89c5ac4SDouglas Gregor 664e89dbc1dSDouglas Gregor // Look for subframeworks. 665e89dbc1dSDouglas Gregor llvm::error_code EC; 6662c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirName 667ddaa69cbSDouglas Gregor = StringRef(FrameworkDir->getName()); 668e89dbc1dSDouglas Gregor llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 6692d4d8cb3SBenjamin Kramer llvm::sys::path::native(SubframeworksDirName); 670ddaa69cbSDouglas Gregor for (llvm::sys::fs::directory_iterator 6712d4d8cb3SBenjamin Kramer Dir(SubframeworksDirName.str(), EC), DirEnd; 672e89dbc1dSDouglas Gregor Dir != DirEnd && !EC; Dir.increment(EC)) { 673e89dbc1dSDouglas Gregor if (!StringRef(Dir->path()).endswith(".framework")) 674e89dbc1dSDouglas Gregor continue; 675f2161a70SDouglas Gregor 676e89dbc1dSDouglas Gregor if (const DirectoryEntry *SubframeworkDir 677e89dbc1dSDouglas Gregor = FileMgr.getDirectory(Dir->path())) { 67807c22b78SDouglas Gregor // Note: as an egregious but useful hack, we use the real path here and 67907c22b78SDouglas Gregor // check whether it is actually a subdirectory of the parent directory. 68007c22b78SDouglas Gregor // This will not be the case if the 'subframework' is actually a symlink 68107c22b78SDouglas Gregor // out to a top-level framework. 682e00c8b20SDouglas Gregor StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir); 68307c22b78SDouglas Gregor bool FoundParent = false; 68407c22b78SDouglas Gregor do { 68507c22b78SDouglas Gregor // Get the parent directory name. 68607c22b78SDouglas Gregor SubframeworkDirName 68707c22b78SDouglas Gregor = llvm::sys::path::parent_path(SubframeworkDirName); 68807c22b78SDouglas Gregor if (SubframeworkDirName.empty()) 68907c22b78SDouglas Gregor break; 69007c22b78SDouglas Gregor 69107c22b78SDouglas Gregor if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { 69207c22b78SDouglas Gregor FoundParent = true; 69307c22b78SDouglas Gregor break; 69407c22b78SDouglas Gregor } 69507c22b78SDouglas Gregor } while (true); 69607c22b78SDouglas Gregor 69707c22b78SDouglas Gregor if (!FoundParent) 69807c22b78SDouglas Gregor continue; 69907c22b78SDouglas Gregor 700e89dbc1dSDouglas Gregor // FIXME: Do we want to warn about subframeworks without umbrella headers? 701056396aeSDouglas Gregor SmallString<32> NameBuf; 702056396aeSDouglas Gregor inferFrameworkModule(sanitizeFilenameAsIdentifier( 703056396aeSDouglas Gregor llvm::sys::path::stem(Dir->path()), NameBuf), 704056396aeSDouglas Gregor SubframeworkDir, IsSystem, Result); 705e89dbc1dSDouglas Gregor } 706e89dbc1dSDouglas Gregor } 707e89dbc1dSDouglas Gregor 70811dfe6feSDouglas Gregor // If the module is a top-level framework, automatically link against the 70911dfe6feSDouglas Gregor // framework. 71011dfe6feSDouglas Gregor if (!Result->isSubFramework()) { 71111dfe6feSDouglas Gregor inferFrameworkLink(Result, FrameworkDir, FileMgr); 71211dfe6feSDouglas Gregor } 71311dfe6feSDouglas Gregor 71456c64013SDouglas Gregor return Result; 71556c64013SDouglas Gregor } 71656c64013SDouglas Gregor 717a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ 71897da9178SDaniel Jasper Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader)); 71973141fa9SDouglas Gregor Mod->Umbrella = UmbrellaHeader; 7207033127bSDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; 721a89c5ac4SDouglas Gregor } 722a89c5ac4SDouglas Gregor 723524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) { 724524e33e1SDouglas Gregor Mod->Umbrella = UmbrellaDir; 725524e33e1SDouglas Gregor UmbrellaDirs[UmbrellaDir] = Mod; 726524e33e1SDouglas Gregor } 727524e33e1SDouglas Gregor 72859527666SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header, 729b53e5483SLawrence Crowl ModuleHeaderRole Role) { 730b53e5483SLawrence Crowl if (Role == ExcludedHeader) { 73159527666SDouglas Gregor Mod->ExcludedHeaders.push_back(Header); 732b146baabSArgyrios Kyrtzidis } else { 733b53e5483SLawrence Crowl if (Role == PrivateHeader) 734b53e5483SLawrence Crowl Mod->PrivateHeaders.push_back(Header); 735b53e5483SLawrence Crowl else 736b53e5483SLawrence Crowl Mod->NormalHeaders.push_back(Header); 7376f722b4eSArgyrios Kyrtzidis bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule; 738b53e5483SLawrence Crowl HeaderInfo.MarkFileModuleHeader(Header, Role, isCompilingModuleHeader); 739b146baabSArgyrios Kyrtzidis } 74097da9178SDaniel Jasper Headers[Header].push_back(KnownHeader(Mod, Role)); 741a89c5ac4SDouglas Gregor } 742a89c5ac4SDouglas Gregor 743514b636aSDouglas Gregor const FileEntry * 744e4412640SArgyrios Kyrtzidis ModuleMap::getContainingModuleMapFile(Module *Module) const { 7451f76c4e8SManuel Klimek if (Module->DefinitionLoc.isInvalid()) 746514b636aSDouglas Gregor return 0; 747514b636aSDouglas Gregor 7481f76c4e8SManuel Klimek return SourceMgr.getFileEntryForID( 7491f76c4e8SManuel Klimek SourceMgr.getFileID(Module->DefinitionLoc)); 750514b636aSDouglas Gregor } 751514b636aSDouglas Gregor 752718292f2SDouglas Gregor void ModuleMap::dump() { 753718292f2SDouglas Gregor llvm::errs() << "Modules:"; 754718292f2SDouglas Gregor for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 755718292f2SDouglas Gregor MEnd = Modules.end(); 756718292f2SDouglas Gregor M != MEnd; ++M) 757d28d1b8dSDouglas Gregor M->getValue()->print(llvm::errs(), 2); 758718292f2SDouglas Gregor 759718292f2SDouglas Gregor llvm::errs() << "Headers:"; 76059527666SDouglas Gregor for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 761718292f2SDouglas Gregor H != HEnd; ++H) { 76297da9178SDaniel Jasper llvm::errs() << " \"" << H->first->getName() << "\" -> "; 76397da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(), 76497da9178SDaniel Jasper E = H->second.end(); 76597da9178SDaniel Jasper I != E; ++I) { 76697da9178SDaniel Jasper if (I != H->second.begin()) 76797da9178SDaniel Jasper llvm::errs() << ","; 76897da9178SDaniel Jasper llvm::errs() << I->getModule()->getFullModuleName(); 76997da9178SDaniel Jasper } 77097da9178SDaniel Jasper llvm::errs() << "\n"; 771718292f2SDouglas Gregor } 772718292f2SDouglas Gregor } 773718292f2SDouglas Gregor 7742b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 7752b82c2a5SDouglas Gregor bool HadError = false; 7762b82c2a5SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { 7772b82c2a5SDouglas Gregor Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 7782b82c2a5SDouglas Gregor Complain); 779f5eedd05SDouglas Gregor if (Export.getPointer() || Export.getInt()) 7802b82c2a5SDouglas Gregor Mod->Exports.push_back(Export); 7812b82c2a5SDouglas Gregor else 7822b82c2a5SDouglas Gregor HadError = true; 7832b82c2a5SDouglas Gregor } 7842b82c2a5SDouglas Gregor Mod->UnresolvedExports.clear(); 7852b82c2a5SDouglas Gregor return HadError; 7862b82c2a5SDouglas Gregor } 7872b82c2a5SDouglas Gregor 788ba7f2f71SDaniel Jasper bool ModuleMap::resolveUses(Module *Mod, bool Complain) { 789ba7f2f71SDaniel Jasper bool HadError = false; 790ba7f2f71SDaniel Jasper for (unsigned I = 0, N = Mod->UnresolvedDirectUses.size(); I != N; ++I) { 791ba7f2f71SDaniel Jasper Module *DirectUse = 792ba7f2f71SDaniel Jasper resolveModuleId(Mod->UnresolvedDirectUses[I], Mod, Complain); 793ba7f2f71SDaniel Jasper if (DirectUse) 794ba7f2f71SDaniel Jasper Mod->DirectUses.push_back(DirectUse); 795ba7f2f71SDaniel Jasper else 796ba7f2f71SDaniel Jasper HadError = true; 797ba7f2f71SDaniel Jasper } 798ba7f2f71SDaniel Jasper Mod->UnresolvedDirectUses.clear(); 799ba7f2f71SDaniel Jasper return HadError; 800ba7f2f71SDaniel Jasper } 801ba7f2f71SDaniel Jasper 802fb912657SDouglas Gregor bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) { 803fb912657SDouglas Gregor bool HadError = false; 804fb912657SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) { 805fb912657SDouglas Gregor Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id, 806fb912657SDouglas Gregor Mod, Complain); 807fb912657SDouglas Gregor if (!OtherMod) { 808fb912657SDouglas Gregor HadError = true; 809fb912657SDouglas Gregor continue; 810fb912657SDouglas Gregor } 811fb912657SDouglas Gregor 812fb912657SDouglas Gregor Module::Conflict Conflict; 813fb912657SDouglas Gregor Conflict.Other = OtherMod; 814fb912657SDouglas Gregor Conflict.Message = Mod->UnresolvedConflicts[I].Message; 815fb912657SDouglas Gregor Mod->Conflicts.push_back(Conflict); 816fb912657SDouglas Gregor } 817fb912657SDouglas Gregor Mod->UnresolvedConflicts.clear(); 818fb912657SDouglas Gregor return HadError; 819fb912657SDouglas Gregor } 820fb912657SDouglas Gregor 8210093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 8220093b3c7SDouglas Gregor if (Loc.isInvalid()) 8230093b3c7SDouglas Gregor return 0; 8240093b3c7SDouglas Gregor 8250093b3c7SDouglas Gregor // Use the expansion location to determine which module we're in. 8260093b3c7SDouglas Gregor FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 8270093b3c7SDouglas Gregor if (!ExpansionLoc.isFileID()) 8280093b3c7SDouglas Gregor return 0; 8290093b3c7SDouglas Gregor 8300093b3c7SDouglas Gregor 8310093b3c7SDouglas Gregor const SourceManager &SrcMgr = Loc.getManager(); 8320093b3c7SDouglas Gregor FileID ExpansionFileID = ExpansionLoc.getFileID(); 833224d8a74SDouglas Gregor 834224d8a74SDouglas Gregor while (const FileEntry *ExpansionFile 835224d8a74SDouglas Gregor = SrcMgr.getFileEntryForID(ExpansionFileID)) { 836224d8a74SDouglas Gregor // Find the module that owns this header (if any). 837b53e5483SLawrence Crowl if (Module *Mod = findModuleForHeader(ExpansionFile).getModule()) 838224d8a74SDouglas Gregor return Mod; 839224d8a74SDouglas Gregor 840224d8a74SDouglas Gregor // No module owns this header, so look up the inclusion chain to see if 841224d8a74SDouglas Gregor // any included header has an associated module. 842224d8a74SDouglas Gregor SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); 843224d8a74SDouglas Gregor if (IncludeLoc.isInvalid()) 8440093b3c7SDouglas Gregor return 0; 8450093b3c7SDouglas Gregor 846224d8a74SDouglas Gregor ExpansionFileID = SrcMgr.getFileID(IncludeLoc); 847224d8a74SDouglas Gregor } 848224d8a74SDouglas Gregor 849224d8a74SDouglas Gregor return 0; 8500093b3c7SDouglas Gregor } 8510093b3c7SDouglas Gregor 852718292f2SDouglas Gregor //----------------------------------------------------------------------------// 853718292f2SDouglas Gregor // Module map file parser 854718292f2SDouglas Gregor //----------------------------------------------------------------------------// 855718292f2SDouglas Gregor 856718292f2SDouglas Gregor namespace clang { 857718292f2SDouglas Gregor /// \brief A token in a module map file. 858718292f2SDouglas Gregor struct MMToken { 859718292f2SDouglas Gregor enum TokenKind { 8601fb5c3a6SDouglas Gregor Comma, 86135b13eceSDouglas Gregor ConfigMacros, 862fb912657SDouglas Gregor Conflict, 863718292f2SDouglas Gregor EndOfFile, 864718292f2SDouglas Gregor HeaderKeyword, 865718292f2SDouglas Gregor Identifier, 866a3feee2aSRichard Smith Exclaim, 86759527666SDouglas Gregor ExcludeKeyword, 868718292f2SDouglas Gregor ExplicitKeyword, 8692b82c2a5SDouglas Gregor ExportKeyword, 87097292843SDaniel Jasper ExternKeyword, 871755b2055SDouglas Gregor FrameworkKeyword, 8726ddfca91SDouglas Gregor LinkKeyword, 873718292f2SDouglas Gregor ModuleKeyword, 8742b82c2a5SDouglas Gregor Period, 875b53e5483SLawrence Crowl PrivateKeyword, 876718292f2SDouglas Gregor UmbrellaKeyword, 877ba7f2f71SDaniel Jasper UseKeyword, 8781fb5c3a6SDouglas Gregor RequiresKeyword, 8792b82c2a5SDouglas Gregor Star, 880718292f2SDouglas Gregor StringLiteral, 881718292f2SDouglas Gregor LBrace, 882a686e1b0SDouglas Gregor RBrace, 883a686e1b0SDouglas Gregor LSquare, 884a686e1b0SDouglas Gregor RSquare 885718292f2SDouglas Gregor } Kind; 886718292f2SDouglas Gregor 887718292f2SDouglas Gregor unsigned Location; 888718292f2SDouglas Gregor unsigned StringLength; 889718292f2SDouglas Gregor const char *StringData; 890718292f2SDouglas Gregor 891718292f2SDouglas Gregor void clear() { 892718292f2SDouglas Gregor Kind = EndOfFile; 893718292f2SDouglas Gregor Location = 0; 894718292f2SDouglas Gregor StringLength = 0; 895718292f2SDouglas Gregor StringData = 0; 896718292f2SDouglas Gregor } 897718292f2SDouglas Gregor 898718292f2SDouglas Gregor bool is(TokenKind K) const { return Kind == K; } 899718292f2SDouglas Gregor 900718292f2SDouglas Gregor SourceLocation getLocation() const { 901718292f2SDouglas Gregor return SourceLocation::getFromRawEncoding(Location); 902718292f2SDouglas Gregor } 903718292f2SDouglas Gregor 904718292f2SDouglas Gregor StringRef getString() const { 905718292f2SDouglas Gregor return StringRef(StringData, StringLength); 906718292f2SDouglas Gregor } 907718292f2SDouglas Gregor }; 908718292f2SDouglas Gregor 9099194a91dSDouglas Gregor /// \brief The set of attributes that can be attached to a module. 9104442605fSBill Wendling struct Attributes { 91177944868SRichard Smith Attributes() : IsSystem(), IsExternC(), IsExhaustive() { } 9129194a91dSDouglas Gregor 9139194a91dSDouglas Gregor /// \brief Whether this is a system module. 9149194a91dSDouglas Gregor unsigned IsSystem : 1; 91535b13eceSDouglas Gregor 91677944868SRichard Smith /// \brief Whether this is an extern "C" module. 91777944868SRichard Smith unsigned IsExternC : 1; 91877944868SRichard Smith 91935b13eceSDouglas Gregor /// \brief Whether this is an exhaustive set of configuration macros. 92035b13eceSDouglas Gregor unsigned IsExhaustive : 1; 9219194a91dSDouglas Gregor }; 9229194a91dSDouglas Gregor 9239194a91dSDouglas Gregor 924718292f2SDouglas Gregor class ModuleMapParser { 925718292f2SDouglas Gregor Lexer &L; 926718292f2SDouglas Gregor SourceManager &SourceMgr; 927bc10b9fbSDouglas Gregor 928bc10b9fbSDouglas Gregor /// \brief Default target information, used only for string literal 929bc10b9fbSDouglas Gregor /// parsing. 930bc10b9fbSDouglas Gregor const TargetInfo *Target; 931bc10b9fbSDouglas Gregor 932718292f2SDouglas Gregor DiagnosticsEngine &Diags; 933718292f2SDouglas Gregor ModuleMap ⤅ 934718292f2SDouglas Gregor 9355257fc63SDouglas Gregor /// \brief The directory that this module map resides in. 9365257fc63SDouglas Gregor const DirectoryEntry *Directory; 9375257fc63SDouglas Gregor 9383ec6663bSDouglas Gregor /// \brief The directory containing Clang-supplied headers. 9393ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir; 9403ec6663bSDouglas Gregor 941963c5535SDouglas Gregor /// \brief Whether this module map is in a system header directory. 942963c5535SDouglas Gregor bool IsSystem; 943963c5535SDouglas Gregor 944718292f2SDouglas Gregor /// \brief Whether an error occurred. 945718292f2SDouglas Gregor bool HadError; 946718292f2SDouglas Gregor 947718292f2SDouglas Gregor /// \brief Stores string data for the various string literals referenced 948718292f2SDouglas Gregor /// during parsing. 949718292f2SDouglas Gregor llvm::BumpPtrAllocator StringData; 950718292f2SDouglas Gregor 951718292f2SDouglas Gregor /// \brief The current token. 952718292f2SDouglas Gregor MMToken Tok; 953718292f2SDouglas Gregor 954718292f2SDouglas Gregor /// \brief The active module. 955de3ef502SDouglas Gregor Module *ActiveModule; 956718292f2SDouglas Gregor 957718292f2SDouglas Gregor /// \brief Consume the current token and return its location. 958718292f2SDouglas Gregor SourceLocation consumeToken(); 959718292f2SDouglas Gregor 960718292f2SDouglas Gregor /// \brief Skip tokens until we reach the a token with the given kind 961718292f2SDouglas Gregor /// (or the end of the file). 962718292f2SDouglas Gregor void skipUntil(MMToken::TokenKind K); 963718292f2SDouglas Gregor 964f857950dSDmitri Gribenko typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; 965e7ab3669SDouglas Gregor bool parseModuleId(ModuleId &Id); 966718292f2SDouglas Gregor void parseModuleDecl(); 96797292843SDaniel Jasper void parseExternModuleDecl(); 9681fb5c3a6SDouglas Gregor void parseRequiresDecl(); 969b53e5483SLawrence Crowl void parseHeaderDecl(clang::MMToken::TokenKind, 970b53e5483SLawrence Crowl SourceLocation LeadingLoc); 971524e33e1SDouglas Gregor void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 9722b82c2a5SDouglas Gregor void parseExportDecl(); 973ba7f2f71SDaniel Jasper void parseUseDecl(); 9746ddfca91SDouglas Gregor void parseLinkDecl(); 97535b13eceSDouglas Gregor void parseConfigMacros(); 976fb912657SDouglas Gregor void parseConflict(); 9779194a91dSDouglas Gregor void parseInferredModuleDecl(bool Framework, bool Explicit); 9784442605fSBill Wendling bool parseOptionalAttributes(Attributes &Attrs); 979718292f2SDouglas Gregor 9807033127bSDouglas Gregor const DirectoryEntry *getOverriddenHeaderSearchDir(); 9817033127bSDouglas Gregor 982718292f2SDouglas Gregor public: 983718292f2SDouglas Gregor explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 984bc10b9fbSDouglas Gregor const TargetInfo *Target, 985718292f2SDouglas Gregor DiagnosticsEngine &Diags, 9865257fc63SDouglas Gregor ModuleMap &Map, 9873ec6663bSDouglas Gregor const DirectoryEntry *Directory, 988963c5535SDouglas Gregor const DirectoryEntry *BuiltinIncludeDir, 989963c5535SDouglas Gregor bool IsSystem) 990bc10b9fbSDouglas Gregor : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 9913ec6663bSDouglas Gregor Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir), 992963c5535SDouglas Gregor IsSystem(IsSystem), HadError(false), ActiveModule(0) 993718292f2SDouglas Gregor { 994718292f2SDouglas Gregor Tok.clear(); 995718292f2SDouglas Gregor consumeToken(); 996718292f2SDouglas Gregor } 997718292f2SDouglas Gregor 998718292f2SDouglas Gregor bool parseModuleMapFile(); 999718292f2SDouglas Gregor }; 1000718292f2SDouglas Gregor } 1001718292f2SDouglas Gregor 1002718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() { 1003718292f2SDouglas Gregor retry: 1004718292f2SDouglas Gregor SourceLocation Result = Tok.getLocation(); 1005718292f2SDouglas Gregor Tok.clear(); 1006718292f2SDouglas Gregor 1007718292f2SDouglas Gregor Token LToken; 1008718292f2SDouglas Gregor L.LexFromRawLexer(LToken); 1009718292f2SDouglas Gregor Tok.Location = LToken.getLocation().getRawEncoding(); 1010718292f2SDouglas Gregor switch (LToken.getKind()) { 1011718292f2SDouglas Gregor case tok::raw_identifier: 1012718292f2SDouglas Gregor Tok.StringData = LToken.getRawIdentifierData(); 1013718292f2SDouglas Gregor Tok.StringLength = LToken.getLength(); 1014718292f2SDouglas Gregor Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString()) 101535b13eceSDouglas Gregor .Case("config_macros", MMToken::ConfigMacros) 1016fb912657SDouglas Gregor .Case("conflict", MMToken::Conflict) 101759527666SDouglas Gregor .Case("exclude", MMToken::ExcludeKeyword) 1018718292f2SDouglas Gregor .Case("explicit", MMToken::ExplicitKeyword) 10192b82c2a5SDouglas Gregor .Case("export", MMToken::ExportKeyword) 102097292843SDaniel Jasper .Case("extern", MMToken::ExternKeyword) 1021755b2055SDouglas Gregor .Case("framework", MMToken::FrameworkKeyword) 102235b13eceSDouglas Gregor .Case("header", MMToken::HeaderKeyword) 10236ddfca91SDouglas Gregor .Case("link", MMToken::LinkKeyword) 1024718292f2SDouglas Gregor .Case("module", MMToken::ModuleKeyword) 1025b53e5483SLawrence Crowl .Case("private", MMToken::PrivateKeyword) 10261fb5c3a6SDouglas Gregor .Case("requires", MMToken::RequiresKeyword) 1027718292f2SDouglas Gregor .Case("umbrella", MMToken::UmbrellaKeyword) 1028ba7f2f71SDaniel Jasper .Case("use", MMToken::UseKeyword) 1029718292f2SDouglas Gregor .Default(MMToken::Identifier); 1030718292f2SDouglas Gregor break; 1031718292f2SDouglas Gregor 10321fb5c3a6SDouglas Gregor case tok::comma: 10331fb5c3a6SDouglas Gregor Tok.Kind = MMToken::Comma; 10341fb5c3a6SDouglas Gregor break; 10351fb5c3a6SDouglas Gregor 1036718292f2SDouglas Gregor case tok::eof: 1037718292f2SDouglas Gregor Tok.Kind = MMToken::EndOfFile; 1038718292f2SDouglas Gregor break; 1039718292f2SDouglas Gregor 1040718292f2SDouglas Gregor case tok::l_brace: 1041718292f2SDouglas Gregor Tok.Kind = MMToken::LBrace; 1042718292f2SDouglas Gregor break; 1043718292f2SDouglas Gregor 1044a686e1b0SDouglas Gregor case tok::l_square: 1045a686e1b0SDouglas Gregor Tok.Kind = MMToken::LSquare; 1046a686e1b0SDouglas Gregor break; 1047a686e1b0SDouglas Gregor 10482b82c2a5SDouglas Gregor case tok::period: 10492b82c2a5SDouglas Gregor Tok.Kind = MMToken::Period; 10502b82c2a5SDouglas Gregor break; 10512b82c2a5SDouglas Gregor 1052718292f2SDouglas Gregor case tok::r_brace: 1053718292f2SDouglas Gregor Tok.Kind = MMToken::RBrace; 1054718292f2SDouglas Gregor break; 1055718292f2SDouglas Gregor 1056a686e1b0SDouglas Gregor case tok::r_square: 1057a686e1b0SDouglas Gregor Tok.Kind = MMToken::RSquare; 1058a686e1b0SDouglas Gregor break; 1059a686e1b0SDouglas Gregor 10602b82c2a5SDouglas Gregor case tok::star: 10612b82c2a5SDouglas Gregor Tok.Kind = MMToken::Star; 10622b82c2a5SDouglas Gregor break; 10632b82c2a5SDouglas Gregor 1064a3feee2aSRichard Smith case tok::exclaim: 1065a3feee2aSRichard Smith Tok.Kind = MMToken::Exclaim; 1066a3feee2aSRichard Smith break; 1067a3feee2aSRichard Smith 1068718292f2SDouglas Gregor case tok::string_literal: { 1069d67aea28SRichard Smith if (LToken.hasUDSuffix()) { 1070d67aea28SRichard Smith Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 1071d67aea28SRichard Smith HadError = true; 1072d67aea28SRichard Smith goto retry; 1073d67aea28SRichard Smith } 1074d67aea28SRichard Smith 1075718292f2SDouglas Gregor // Parse the string literal. 1076718292f2SDouglas Gregor LangOptions LangOpts; 1077718292f2SDouglas Gregor StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target); 1078718292f2SDouglas Gregor if (StringLiteral.hadError) 1079718292f2SDouglas Gregor goto retry; 1080718292f2SDouglas Gregor 1081718292f2SDouglas Gregor // Copy the string literal into our string data allocator. 1082718292f2SDouglas Gregor unsigned Length = StringLiteral.GetStringLength(); 1083718292f2SDouglas Gregor char *Saved = StringData.Allocate<char>(Length + 1); 1084718292f2SDouglas Gregor memcpy(Saved, StringLiteral.GetString().data(), Length); 1085718292f2SDouglas Gregor Saved[Length] = 0; 1086718292f2SDouglas Gregor 1087718292f2SDouglas Gregor // Form the token. 1088718292f2SDouglas Gregor Tok.Kind = MMToken::StringLiteral; 1089718292f2SDouglas Gregor Tok.StringData = Saved; 1090718292f2SDouglas Gregor Tok.StringLength = Length; 1091718292f2SDouglas Gregor break; 1092718292f2SDouglas Gregor } 1093718292f2SDouglas Gregor 1094718292f2SDouglas Gregor case tok::comment: 1095718292f2SDouglas Gregor goto retry; 1096718292f2SDouglas Gregor 1097718292f2SDouglas Gregor default: 1098718292f2SDouglas Gregor Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 1099718292f2SDouglas Gregor HadError = true; 1100718292f2SDouglas Gregor goto retry; 1101718292f2SDouglas Gregor } 1102718292f2SDouglas Gregor 1103718292f2SDouglas Gregor return Result; 1104718292f2SDouglas Gregor } 1105718292f2SDouglas Gregor 1106718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 1107718292f2SDouglas Gregor unsigned braceDepth = 0; 1108a686e1b0SDouglas Gregor unsigned squareDepth = 0; 1109718292f2SDouglas Gregor do { 1110718292f2SDouglas Gregor switch (Tok.Kind) { 1111718292f2SDouglas Gregor case MMToken::EndOfFile: 1112718292f2SDouglas Gregor return; 1113718292f2SDouglas Gregor 1114718292f2SDouglas Gregor case MMToken::LBrace: 1115a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1116718292f2SDouglas Gregor return; 1117718292f2SDouglas Gregor 1118718292f2SDouglas Gregor ++braceDepth; 1119718292f2SDouglas Gregor break; 1120718292f2SDouglas Gregor 1121a686e1b0SDouglas Gregor case MMToken::LSquare: 1122a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1123a686e1b0SDouglas Gregor return; 1124a686e1b0SDouglas Gregor 1125a686e1b0SDouglas Gregor ++squareDepth; 1126a686e1b0SDouglas Gregor break; 1127a686e1b0SDouglas Gregor 1128718292f2SDouglas Gregor case MMToken::RBrace: 1129718292f2SDouglas Gregor if (braceDepth > 0) 1130718292f2SDouglas Gregor --braceDepth; 1131718292f2SDouglas Gregor else if (Tok.is(K)) 1132718292f2SDouglas Gregor return; 1133718292f2SDouglas Gregor break; 1134718292f2SDouglas Gregor 1135a686e1b0SDouglas Gregor case MMToken::RSquare: 1136a686e1b0SDouglas Gregor if (squareDepth > 0) 1137a686e1b0SDouglas Gregor --squareDepth; 1138a686e1b0SDouglas Gregor else if (Tok.is(K)) 1139a686e1b0SDouglas Gregor return; 1140a686e1b0SDouglas Gregor break; 1141a686e1b0SDouglas Gregor 1142718292f2SDouglas Gregor default: 1143a686e1b0SDouglas Gregor if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 1144718292f2SDouglas Gregor return; 1145718292f2SDouglas Gregor break; 1146718292f2SDouglas Gregor } 1147718292f2SDouglas Gregor 1148718292f2SDouglas Gregor consumeToken(); 1149718292f2SDouglas Gregor } while (true); 1150718292f2SDouglas Gregor } 1151718292f2SDouglas Gregor 1152e7ab3669SDouglas Gregor /// \brief Parse a module-id. 1153e7ab3669SDouglas Gregor /// 1154e7ab3669SDouglas Gregor /// module-id: 1155e7ab3669SDouglas Gregor /// identifier 1156e7ab3669SDouglas Gregor /// identifier '.' module-id 1157e7ab3669SDouglas Gregor /// 1158e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise. 1159e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) { 1160e7ab3669SDouglas Gregor Id.clear(); 1161e7ab3669SDouglas Gregor do { 11623cd34c76SDaniel Jasper if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) { 1163e7ab3669SDouglas Gregor Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 1164e7ab3669SDouglas Gregor consumeToken(); 1165e7ab3669SDouglas Gregor } else { 1166e7ab3669SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 1167e7ab3669SDouglas Gregor return true; 1168e7ab3669SDouglas Gregor } 1169e7ab3669SDouglas Gregor 1170e7ab3669SDouglas Gregor if (!Tok.is(MMToken::Period)) 1171e7ab3669SDouglas Gregor break; 1172e7ab3669SDouglas Gregor 1173e7ab3669SDouglas Gregor consumeToken(); 1174e7ab3669SDouglas Gregor } while (true); 1175e7ab3669SDouglas Gregor 1176e7ab3669SDouglas Gregor return false; 1177e7ab3669SDouglas Gregor } 1178e7ab3669SDouglas Gregor 1179a686e1b0SDouglas Gregor namespace { 1180a686e1b0SDouglas Gregor /// \brief Enumerates the known attributes. 1181a686e1b0SDouglas Gregor enum AttributeKind { 1182a686e1b0SDouglas Gregor /// \brief An unknown attribute. 1183a686e1b0SDouglas Gregor AT_unknown, 1184a686e1b0SDouglas Gregor /// \brief The 'system' attribute. 118535b13eceSDouglas Gregor AT_system, 118677944868SRichard Smith /// \brief The 'extern_c' attribute. 118777944868SRichard Smith AT_extern_c, 118835b13eceSDouglas Gregor /// \brief The 'exhaustive' attribute. 118935b13eceSDouglas Gregor AT_exhaustive 1190a686e1b0SDouglas Gregor }; 1191a686e1b0SDouglas Gregor } 1192a686e1b0SDouglas Gregor 1193718292f2SDouglas Gregor /// \brief Parse a module declaration. 1194718292f2SDouglas Gregor /// 1195718292f2SDouglas Gregor /// module-declaration: 119697292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 1197a686e1b0SDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 1198a686e1b0SDouglas Gregor /// { module-member* } 1199a686e1b0SDouglas Gregor /// 1200718292f2SDouglas Gregor /// module-member: 12011fb5c3a6SDouglas Gregor /// requires-declaration 1202718292f2SDouglas Gregor /// header-declaration 1203e7ab3669SDouglas Gregor /// submodule-declaration 12042b82c2a5SDouglas Gregor /// export-declaration 12056ddfca91SDouglas Gregor /// link-declaration 120673441091SDouglas Gregor /// 120773441091SDouglas Gregor /// submodule-declaration: 120873441091SDouglas Gregor /// module-declaration 120973441091SDouglas Gregor /// inferred-submodule-declaration 1210718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() { 1211755b2055SDouglas Gregor assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 121297292843SDaniel Jasper Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword)); 121397292843SDaniel Jasper if (Tok.is(MMToken::ExternKeyword)) { 121497292843SDaniel Jasper parseExternModuleDecl(); 121597292843SDaniel Jasper return; 121697292843SDaniel Jasper } 121797292843SDaniel Jasper 1218f2161a70SDouglas Gregor // Parse 'explicit' or 'framework' keyword, if present. 1219e7ab3669SDouglas Gregor SourceLocation ExplicitLoc; 1220718292f2SDouglas Gregor bool Explicit = false; 1221f2161a70SDouglas Gregor bool Framework = false; 1222755b2055SDouglas Gregor 1223f2161a70SDouglas Gregor // Parse 'explicit' keyword, if present. 1224f2161a70SDouglas Gregor if (Tok.is(MMToken::ExplicitKeyword)) { 1225e7ab3669SDouglas Gregor ExplicitLoc = consumeToken(); 1226f2161a70SDouglas Gregor Explicit = true; 1227f2161a70SDouglas Gregor } 1228f2161a70SDouglas Gregor 1229f2161a70SDouglas Gregor // Parse 'framework' keyword, if present. 1230755b2055SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 1231755b2055SDouglas Gregor consumeToken(); 1232755b2055SDouglas Gregor Framework = true; 1233755b2055SDouglas Gregor } 1234718292f2SDouglas Gregor 1235718292f2SDouglas Gregor // Parse 'module' keyword. 1236718292f2SDouglas Gregor if (!Tok.is(MMToken::ModuleKeyword)) { 1237d6343c99SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1238718292f2SDouglas Gregor consumeToken(); 1239718292f2SDouglas Gregor HadError = true; 1240718292f2SDouglas Gregor return; 1241718292f2SDouglas Gregor } 1242718292f2SDouglas Gregor consumeToken(); // 'module' keyword 1243718292f2SDouglas Gregor 124473441091SDouglas Gregor // If we have a wildcard for the module name, this is an inferred submodule. 124573441091SDouglas Gregor // Parse it. 124673441091SDouglas Gregor if (Tok.is(MMToken::Star)) 12479194a91dSDouglas Gregor return parseInferredModuleDecl(Framework, Explicit); 124873441091SDouglas Gregor 1249718292f2SDouglas Gregor // Parse the module name. 1250e7ab3669SDouglas Gregor ModuleId Id; 1251e7ab3669SDouglas Gregor if (parseModuleId(Id)) { 1252718292f2SDouglas Gregor HadError = true; 1253718292f2SDouglas Gregor return; 1254718292f2SDouglas Gregor } 1255e7ab3669SDouglas Gregor 1256e7ab3669SDouglas Gregor if (ActiveModule) { 1257e7ab3669SDouglas Gregor if (Id.size() > 1) { 1258e7ab3669SDouglas Gregor Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 1259e7ab3669SDouglas Gregor << SourceRange(Id.front().second, Id.back().second); 1260e7ab3669SDouglas Gregor 1261e7ab3669SDouglas Gregor HadError = true; 1262e7ab3669SDouglas Gregor return; 1263e7ab3669SDouglas Gregor } 1264e7ab3669SDouglas Gregor } else if (Id.size() == 1 && Explicit) { 1265e7ab3669SDouglas Gregor // Top-level modules can't be explicit. 1266e7ab3669SDouglas Gregor Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 1267e7ab3669SDouglas Gregor Explicit = false; 1268e7ab3669SDouglas Gregor ExplicitLoc = SourceLocation(); 1269e7ab3669SDouglas Gregor HadError = true; 1270e7ab3669SDouglas Gregor } 1271e7ab3669SDouglas Gregor 1272e7ab3669SDouglas Gregor Module *PreviousActiveModule = ActiveModule; 1273e7ab3669SDouglas Gregor if (Id.size() > 1) { 1274e7ab3669SDouglas Gregor // This module map defines a submodule. Go find the module of which it 1275e7ab3669SDouglas Gregor // is a submodule. 1276e7ab3669SDouglas Gregor ActiveModule = 0; 1277e7ab3669SDouglas Gregor for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 1278e7ab3669SDouglas Gregor if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 1279e7ab3669SDouglas Gregor ActiveModule = Next; 1280e7ab3669SDouglas Gregor continue; 1281e7ab3669SDouglas Gregor } 1282e7ab3669SDouglas Gregor 1283e7ab3669SDouglas Gregor if (ActiveModule) { 1284e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 12855b5d21eaSRichard Smith << Id[I].first 12865b5d21eaSRichard Smith << ActiveModule->getTopLevelModule()->getFullModuleName(); 1287e7ab3669SDouglas Gregor } else { 1288e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 1289e7ab3669SDouglas Gregor } 1290e7ab3669SDouglas Gregor HadError = true; 1291e7ab3669SDouglas Gregor return; 1292e7ab3669SDouglas Gregor } 1293e7ab3669SDouglas Gregor } 1294e7ab3669SDouglas Gregor 1295e7ab3669SDouglas Gregor StringRef ModuleName = Id.back().first; 1296e7ab3669SDouglas Gregor SourceLocation ModuleNameLoc = Id.back().second; 1297718292f2SDouglas Gregor 1298a686e1b0SDouglas Gregor // Parse the optional attribute list. 12994442605fSBill Wendling Attributes Attrs; 13009194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 1301a686e1b0SDouglas Gregor 1302718292f2SDouglas Gregor // Parse the opening brace. 1303718292f2SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 1304718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 1305718292f2SDouglas Gregor << ModuleName; 1306718292f2SDouglas Gregor HadError = true; 1307718292f2SDouglas Gregor return; 1308718292f2SDouglas Gregor } 1309718292f2SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 1310718292f2SDouglas Gregor 1311718292f2SDouglas Gregor // Determine whether this (sub)module has already been defined. 1312eb90e830SDouglas Gregor if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 1313fcc54a3bSDouglas Gregor if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { 1314fcc54a3bSDouglas Gregor // Skip the module definition. 1315fcc54a3bSDouglas Gregor skipUntil(MMToken::RBrace); 1316fcc54a3bSDouglas Gregor if (Tok.is(MMToken::RBrace)) 1317fcc54a3bSDouglas Gregor consumeToken(); 1318fcc54a3bSDouglas Gregor else { 1319fcc54a3bSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1320fcc54a3bSDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1321fcc54a3bSDouglas Gregor HadError = true; 1322fcc54a3bSDouglas Gregor } 1323fcc54a3bSDouglas Gregor return; 1324fcc54a3bSDouglas Gregor } 1325fcc54a3bSDouglas Gregor 1326718292f2SDouglas Gregor Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 1327718292f2SDouglas Gregor << ModuleName; 1328eb90e830SDouglas Gregor Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 1329718292f2SDouglas Gregor 1330718292f2SDouglas Gregor // Skip the module definition. 1331718292f2SDouglas Gregor skipUntil(MMToken::RBrace); 1332718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1333718292f2SDouglas Gregor consumeToken(); 1334718292f2SDouglas Gregor 1335718292f2SDouglas Gregor HadError = true; 1336718292f2SDouglas Gregor return; 1337718292f2SDouglas Gregor } 1338718292f2SDouglas Gregor 1339718292f2SDouglas Gregor // Start defining this module. 1340eb90e830SDouglas Gregor ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, 1341eb90e830SDouglas Gregor Explicit).first; 1342eb90e830SDouglas Gregor ActiveModule->DefinitionLoc = ModuleNameLoc; 1343963c5535SDouglas Gregor if (Attrs.IsSystem || IsSystem) 1344a686e1b0SDouglas Gregor ActiveModule->IsSystem = true; 134577944868SRichard Smith if (Attrs.IsExternC) 134677944868SRichard Smith ActiveModule->IsExternC = true; 1347718292f2SDouglas Gregor 1348718292f2SDouglas Gregor bool Done = false; 1349718292f2SDouglas Gregor do { 1350718292f2SDouglas Gregor switch (Tok.Kind) { 1351718292f2SDouglas Gregor case MMToken::EndOfFile: 1352718292f2SDouglas Gregor case MMToken::RBrace: 1353718292f2SDouglas Gregor Done = true; 1354718292f2SDouglas Gregor break; 1355718292f2SDouglas Gregor 135635b13eceSDouglas Gregor case MMToken::ConfigMacros: 135735b13eceSDouglas Gregor parseConfigMacros(); 135835b13eceSDouglas Gregor break; 135935b13eceSDouglas Gregor 1360fb912657SDouglas Gregor case MMToken::Conflict: 1361fb912657SDouglas Gregor parseConflict(); 1362fb912657SDouglas Gregor break; 1363fb912657SDouglas Gregor 1364718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 136597292843SDaniel Jasper case MMToken::ExternKeyword: 1366f2161a70SDouglas Gregor case MMToken::FrameworkKeyword: 1367718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1368718292f2SDouglas Gregor parseModuleDecl(); 1369718292f2SDouglas Gregor break; 1370718292f2SDouglas Gregor 13712b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 13722b82c2a5SDouglas Gregor parseExportDecl(); 13732b82c2a5SDouglas Gregor break; 13742b82c2a5SDouglas Gregor 1375ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 1376ba7f2f71SDaniel Jasper parseUseDecl(); 1377ba7f2f71SDaniel Jasper break; 1378ba7f2f71SDaniel Jasper 13791fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 13801fb5c3a6SDouglas Gregor parseRequiresDecl(); 13811fb5c3a6SDouglas Gregor break; 13821fb5c3a6SDouglas Gregor 1383524e33e1SDouglas Gregor case MMToken::UmbrellaKeyword: { 1384524e33e1SDouglas Gregor SourceLocation UmbrellaLoc = consumeToken(); 1385524e33e1SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) 1386b53e5483SLawrence Crowl parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc); 1387524e33e1SDouglas Gregor else 1388524e33e1SDouglas Gregor parseUmbrellaDirDecl(UmbrellaLoc); 1389718292f2SDouglas Gregor break; 1390524e33e1SDouglas Gregor } 1391718292f2SDouglas Gregor 139259527666SDouglas Gregor case MMToken::ExcludeKeyword: { 139359527666SDouglas Gregor SourceLocation ExcludeLoc = consumeToken(); 139459527666SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) { 1395b53e5483SLawrence Crowl parseHeaderDecl(MMToken::ExcludeKeyword, ExcludeLoc); 139659527666SDouglas Gregor } else { 139759527666SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 139859527666SDouglas Gregor << "exclude"; 139959527666SDouglas Gregor } 140059527666SDouglas Gregor break; 140159527666SDouglas Gregor } 140259527666SDouglas Gregor 1403b53e5483SLawrence Crowl case MMToken::PrivateKeyword: { 1404b53e5483SLawrence Crowl SourceLocation PrivateLoc = consumeToken(); 1405b53e5483SLawrence Crowl if (Tok.is(MMToken::HeaderKeyword)) { 1406b53e5483SLawrence Crowl parseHeaderDecl(MMToken::PrivateKeyword, PrivateLoc); 1407b53e5483SLawrence Crowl } else { 1408b53e5483SLawrence Crowl Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1409b53e5483SLawrence Crowl << "private"; 1410b53e5483SLawrence Crowl } 1411b53e5483SLawrence Crowl break; 1412b53e5483SLawrence Crowl } 1413b53e5483SLawrence Crowl 1414322f633cSDouglas Gregor case MMToken::HeaderKeyword: 1415b53e5483SLawrence Crowl parseHeaderDecl(MMToken::HeaderKeyword, SourceLocation()); 1416718292f2SDouglas Gregor break; 1417718292f2SDouglas Gregor 14186ddfca91SDouglas Gregor case MMToken::LinkKeyword: 14196ddfca91SDouglas Gregor parseLinkDecl(); 14206ddfca91SDouglas Gregor break; 14216ddfca91SDouglas Gregor 1422718292f2SDouglas Gregor default: 1423718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 1424718292f2SDouglas Gregor consumeToken(); 1425718292f2SDouglas Gregor break; 1426718292f2SDouglas Gregor } 1427718292f2SDouglas Gregor } while (!Done); 1428718292f2SDouglas Gregor 1429718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1430718292f2SDouglas Gregor consumeToken(); 1431718292f2SDouglas Gregor else { 1432718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1433718292f2SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1434718292f2SDouglas Gregor HadError = true; 1435718292f2SDouglas Gregor } 1436718292f2SDouglas Gregor 143711dfe6feSDouglas Gregor // If the active module is a top-level framework, and there are no link 143811dfe6feSDouglas Gregor // libraries, automatically link against the framework. 143911dfe6feSDouglas Gregor if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() && 144011dfe6feSDouglas Gregor ActiveModule->LinkLibraries.empty()) { 144111dfe6feSDouglas Gregor inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager()); 144211dfe6feSDouglas Gregor } 144311dfe6feSDouglas Gregor 1444e7ab3669SDouglas Gregor // We're done parsing this module. Pop back to the previous module. 1445e7ab3669SDouglas Gregor ActiveModule = PreviousActiveModule; 1446718292f2SDouglas Gregor } 1447718292f2SDouglas Gregor 144897292843SDaniel Jasper /// \brief Parse an extern module declaration. 144997292843SDaniel Jasper /// 145097292843SDaniel Jasper /// extern module-declaration: 145197292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 145297292843SDaniel Jasper void ModuleMapParser::parseExternModuleDecl() { 145397292843SDaniel Jasper assert(Tok.is(MMToken::ExternKeyword)); 145497292843SDaniel Jasper consumeToken(); // 'extern' keyword 145597292843SDaniel Jasper 145697292843SDaniel Jasper // Parse 'module' keyword. 145797292843SDaniel Jasper if (!Tok.is(MMToken::ModuleKeyword)) { 145897292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 145997292843SDaniel Jasper consumeToken(); 146097292843SDaniel Jasper HadError = true; 146197292843SDaniel Jasper return; 146297292843SDaniel Jasper } 146397292843SDaniel Jasper consumeToken(); // 'module' keyword 146497292843SDaniel Jasper 146597292843SDaniel Jasper // Parse the module name. 146697292843SDaniel Jasper ModuleId Id; 146797292843SDaniel Jasper if (parseModuleId(Id)) { 146897292843SDaniel Jasper HadError = true; 146997292843SDaniel Jasper return; 147097292843SDaniel Jasper } 147197292843SDaniel Jasper 147297292843SDaniel Jasper // Parse the referenced module map file name. 147397292843SDaniel Jasper if (!Tok.is(MMToken::StringLiteral)) { 147497292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file); 147597292843SDaniel Jasper HadError = true; 147697292843SDaniel Jasper return; 147797292843SDaniel Jasper } 147897292843SDaniel Jasper std::string FileName = Tok.getString(); 147997292843SDaniel Jasper consumeToken(); // filename 148097292843SDaniel Jasper 148197292843SDaniel Jasper StringRef FileNameRef = FileName; 148297292843SDaniel Jasper SmallString<128> ModuleMapFileName; 148397292843SDaniel Jasper if (llvm::sys::path::is_relative(FileNameRef)) { 148497292843SDaniel Jasper ModuleMapFileName += Directory->getName(); 148597292843SDaniel Jasper llvm::sys::path::append(ModuleMapFileName, FileName); 148697292843SDaniel Jasper FileNameRef = ModuleMapFileName.str(); 148797292843SDaniel Jasper } 148897292843SDaniel Jasper if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef)) 148997292843SDaniel Jasper Map.parseModuleMapFile(File, /*IsSystem=*/false); 149097292843SDaniel Jasper } 149197292843SDaniel Jasper 14921fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration. 14931fb5c3a6SDouglas Gregor /// 14941fb5c3a6SDouglas Gregor /// requires-declaration: 14951fb5c3a6SDouglas Gregor /// 'requires' feature-list 14961fb5c3a6SDouglas Gregor /// 14971fb5c3a6SDouglas Gregor /// feature-list: 1498a3feee2aSRichard Smith /// feature ',' feature-list 1499a3feee2aSRichard Smith /// feature 1500a3feee2aSRichard Smith /// 1501a3feee2aSRichard Smith /// feature: 1502a3feee2aSRichard Smith /// '!'[opt] identifier 15031fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() { 15041fb5c3a6SDouglas Gregor assert(Tok.is(MMToken::RequiresKeyword)); 15051fb5c3a6SDouglas Gregor 15061fb5c3a6SDouglas Gregor // Parse 'requires' keyword. 15071fb5c3a6SDouglas Gregor consumeToken(); 15081fb5c3a6SDouglas Gregor 15091fb5c3a6SDouglas Gregor // Parse the feature-list. 15101fb5c3a6SDouglas Gregor do { 1511a3feee2aSRichard Smith bool RequiredState = true; 1512a3feee2aSRichard Smith if (Tok.is(MMToken::Exclaim)) { 1513a3feee2aSRichard Smith RequiredState = false; 1514a3feee2aSRichard Smith consumeToken(); 1515a3feee2aSRichard Smith } 1516a3feee2aSRichard Smith 15171fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 15181fb5c3a6SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 15191fb5c3a6SDouglas Gregor HadError = true; 15201fb5c3a6SDouglas Gregor return; 15211fb5c3a6SDouglas Gregor } 15221fb5c3a6SDouglas Gregor 15231fb5c3a6SDouglas Gregor // Consume the feature name. 15241fb5c3a6SDouglas Gregor std::string Feature = Tok.getString(); 15251fb5c3a6SDouglas Gregor consumeToken(); 15261fb5c3a6SDouglas Gregor 15271fb5c3a6SDouglas Gregor // Add this feature. 1528a3feee2aSRichard Smith ActiveModule->addRequirement(Feature, RequiredState, 1529a3feee2aSRichard Smith Map.LangOpts, *Map.Target); 15301fb5c3a6SDouglas Gregor 15311fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Comma)) 15321fb5c3a6SDouglas Gregor break; 15331fb5c3a6SDouglas Gregor 15341fb5c3a6SDouglas Gregor // Consume the comma. 15351fb5c3a6SDouglas Gregor consumeToken(); 15361fb5c3a6SDouglas Gregor } while (true); 15371fb5c3a6SDouglas Gregor } 15381fb5c3a6SDouglas Gregor 1539f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the 1540f2161a70SDouglas Gregor /// subframework in which the given module lives. 1541bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod, 1542f857950dSDmitri Gribenko SmallVectorImpl<char> &Path) { 1543f2161a70SDouglas Gregor // Collect the framework names from the given module to the top-level module. 1544f857950dSDmitri Gribenko SmallVector<StringRef, 2> Paths; 1545f2161a70SDouglas Gregor for (; Mod; Mod = Mod->Parent) { 1546f2161a70SDouglas Gregor if (Mod->IsFramework) 1547f2161a70SDouglas Gregor Paths.push_back(Mod->Name); 1548f2161a70SDouglas Gregor } 1549f2161a70SDouglas Gregor 1550f2161a70SDouglas Gregor if (Paths.empty()) 1551f2161a70SDouglas Gregor return; 1552f2161a70SDouglas Gregor 1553f2161a70SDouglas Gregor // Add Frameworks/Name.framework for each subframework. 155417381a06SBenjamin Kramer for (unsigned I = Paths.size() - 1; I != 0; --I) 155517381a06SBenjamin Kramer llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework"); 1556f2161a70SDouglas Gregor } 1557f2161a70SDouglas Gregor 1558718292f2SDouglas Gregor /// \brief Parse a header declaration. 1559718292f2SDouglas Gregor /// 1560718292f2SDouglas Gregor /// header-declaration: 1561322f633cSDouglas Gregor /// 'umbrella'[opt] 'header' string-literal 156259527666SDouglas Gregor /// 'exclude'[opt] 'header' string-literal 1563b53e5483SLawrence Crowl void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken, 1564b53e5483SLawrence Crowl SourceLocation LeadingLoc) { 1565718292f2SDouglas Gregor assert(Tok.is(MMToken::HeaderKeyword)); 15661871ed3dSBenjamin Kramer consumeToken(); 1567718292f2SDouglas Gregor 1568718292f2SDouglas Gregor // Parse the header name. 1569718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1570718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1571718292f2SDouglas Gregor << "header"; 1572718292f2SDouglas Gregor HadError = true; 1573718292f2SDouglas Gregor return; 1574718292f2SDouglas Gregor } 15750761a8a0SDaniel Jasper Module::HeaderDirective Header; 15760761a8a0SDaniel Jasper Header.FileName = Tok.getString(); 15770761a8a0SDaniel Jasper Header.FileNameLoc = consumeToken(); 1578718292f2SDouglas Gregor 1579524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1580b53e5483SLawrence Crowl if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) { 15810761a8a0SDaniel Jasper Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash) 1582524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1583322f633cSDouglas Gregor HadError = true; 1584322f633cSDouglas Gregor return; 1585322f633cSDouglas Gregor } 1586322f633cSDouglas Gregor 15875257fc63SDouglas Gregor // Look for this file. 1588e7ab3669SDouglas Gregor const FileEntry *File = 0; 15893ec6663bSDouglas Gregor const FileEntry *BuiltinFile = 0; 15902c1dd271SDylan Noblesmith SmallString<128> PathName; 15910761a8a0SDaniel Jasper if (llvm::sys::path::is_absolute(Header.FileName)) { 15920761a8a0SDaniel Jasper PathName = Header.FileName; 1593e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 15947033127bSDouglas Gregor } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) { 15957033127bSDouglas Gregor PathName = Dir->getName(); 15960761a8a0SDaniel Jasper llvm::sys::path::append(PathName, Header.FileName); 15977033127bSDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1598e7ab3669SDouglas Gregor } else { 1599e7ab3669SDouglas Gregor // Search for the header file within the search directory. 16007033127bSDouglas Gregor PathName = Directory->getName(); 1601e7ab3669SDouglas Gregor unsigned PathLength = PathName.size(); 1602755b2055SDouglas Gregor 1603f2161a70SDouglas Gregor if (ActiveModule->isPartOfFramework()) { 1604f2161a70SDouglas Gregor appendSubframeworkPaths(ActiveModule, PathName); 1605755b2055SDouglas Gregor 1606e7ab3669SDouglas Gregor // Check whether this file is in the public headers. 16070761a8a0SDaniel Jasper llvm::sys::path::append(PathName, "Headers", Header.FileName); 1608e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1609e7ab3669SDouglas Gregor 1610e7ab3669SDouglas Gregor if (!File) { 1611e7ab3669SDouglas Gregor // Check whether this file is in the private headers. 1612e7ab3669SDouglas Gregor PathName.resize(PathLength); 16130761a8a0SDaniel Jasper llvm::sys::path::append(PathName, "PrivateHeaders", Header.FileName); 1614e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1615e7ab3669SDouglas Gregor } 1616e7ab3669SDouglas Gregor } else { 1617e7ab3669SDouglas Gregor // Lookup for normal headers. 16180761a8a0SDaniel Jasper llvm::sys::path::append(PathName, Header.FileName); 1619e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 16203ec6663bSDouglas Gregor 16213ec6663bSDouglas Gregor // If this is a system module with a top-level header, this header 16223ec6663bSDouglas Gregor // may have a counterpart (or replacement) in the set of headers 16233ec6663bSDouglas Gregor // supplied by Clang. Find that builtin header. 1624b53e5483SLawrence Crowl if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword && 1625b53e5483SLawrence Crowl BuiltinIncludeDir && BuiltinIncludeDir != Directory && 16260761a8a0SDaniel Jasper isBuiltinHeader(Header.FileName)) { 16272c1dd271SDylan Noblesmith SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); 16280761a8a0SDaniel Jasper llvm::sys::path::append(BuiltinPathName, Header.FileName); 16293ec6663bSDouglas Gregor BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); 16303ec6663bSDouglas Gregor 16313ec6663bSDouglas Gregor // If Clang supplies this header but the underlying system does not, 16323ec6663bSDouglas Gregor // just silently swap in our builtin version. Otherwise, we'll end 16333ec6663bSDouglas Gregor // up adding both (later). 16343ec6663bSDouglas Gregor if (!File && BuiltinFile) { 16353ec6663bSDouglas Gregor File = BuiltinFile; 16363ec6663bSDouglas Gregor BuiltinFile = 0; 16373ec6663bSDouglas Gregor } 16383ec6663bSDouglas Gregor } 1639e7ab3669SDouglas Gregor } 1640e7ab3669SDouglas Gregor } 16415257fc63SDouglas Gregor 16425257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 16435257fc63SDouglas Gregor // Come up with a lazy way to do this. 1644e7ab3669SDouglas Gregor if (File) { 164597da9178SDaniel Jasper if (LeadingToken == MMToken::UmbrellaKeyword) { 1646322f633cSDouglas Gregor const DirectoryEntry *UmbrellaDir = File->getDir(); 164759527666SDouglas Gregor if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { 1648b53e5483SLawrence Crowl Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash) 164959527666SDouglas Gregor << UmbrellaModule->getFullModuleName(); 1650322f633cSDouglas Gregor HadError = true; 16515257fc63SDouglas Gregor } else { 1652322f633cSDouglas Gregor // Record this umbrella header. 1653322f633cSDouglas Gregor Map.setUmbrellaHeader(ActiveModule, File); 1654322f633cSDouglas Gregor } 1655322f633cSDouglas Gregor } else { 1656322f633cSDouglas Gregor // Record this header. 1657b53e5483SLawrence Crowl ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader; 1658b53e5483SLawrence Crowl if (LeadingToken == MMToken::ExcludeKeyword) 1659b53e5483SLawrence Crowl Role = ModuleMap::ExcludedHeader; 1660b53e5483SLawrence Crowl else if (LeadingToken == MMToken::PrivateKeyword) 1661b53e5483SLawrence Crowl Role = ModuleMap::PrivateHeader; 1662b53e5483SLawrence Crowl else 1663b53e5483SLawrence Crowl assert(LeadingToken == MMToken::HeaderKeyword); 1664b53e5483SLawrence Crowl 1665b53e5483SLawrence Crowl Map.addHeader(ActiveModule, File, Role); 16663ec6663bSDouglas Gregor 16673ec6663bSDouglas Gregor // If there is a builtin counterpart to this file, add it now. 16683ec6663bSDouglas Gregor if (BuiltinFile) 1669b53e5483SLawrence Crowl Map.addHeader(ActiveModule, BuiltinFile, Role); 16705257fc63SDouglas Gregor } 1671b53e5483SLawrence Crowl } else if (LeadingToken != MMToken::ExcludeKeyword) { 16724b27a64bSDouglas Gregor // Ignore excluded header files. They're optional anyway. 16734b27a64bSDouglas Gregor 16740761a8a0SDaniel Jasper // If we find a module that has a missing header, we mark this module as 16750761a8a0SDaniel Jasper // unavailable and store the header directive for displaying diagnostics. 16760761a8a0SDaniel Jasper // Other submodules in the same module can still be used. 16770761a8a0SDaniel Jasper Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword; 16780761a8a0SDaniel Jasper ActiveModule->IsAvailable = false; 16790761a8a0SDaniel Jasper ActiveModule->MissingHeaders.push_back(Header); 16805257fc63SDouglas Gregor } 1681718292f2SDouglas Gregor } 1682718292f2SDouglas Gregor 1683524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration. 1684524e33e1SDouglas Gregor /// 1685524e33e1SDouglas Gregor /// umbrella-dir-declaration: 1686524e33e1SDouglas Gregor /// umbrella string-literal 1687524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1688524e33e1SDouglas Gregor // Parse the directory name. 1689524e33e1SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1690524e33e1SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1691524e33e1SDouglas Gregor << "umbrella"; 1692524e33e1SDouglas Gregor HadError = true; 1693524e33e1SDouglas Gregor return; 1694524e33e1SDouglas Gregor } 1695524e33e1SDouglas Gregor 1696524e33e1SDouglas Gregor std::string DirName = Tok.getString(); 1697524e33e1SDouglas Gregor SourceLocation DirNameLoc = consumeToken(); 1698524e33e1SDouglas Gregor 1699524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1700524e33e1SDouglas Gregor if (ActiveModule->Umbrella) { 1701524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1702524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1703524e33e1SDouglas Gregor HadError = true; 1704524e33e1SDouglas Gregor return; 1705524e33e1SDouglas Gregor } 1706524e33e1SDouglas Gregor 1707524e33e1SDouglas Gregor // Look for this file. 1708524e33e1SDouglas Gregor const DirectoryEntry *Dir = 0; 1709524e33e1SDouglas Gregor if (llvm::sys::path::is_absolute(DirName)) 1710524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(DirName); 1711524e33e1SDouglas Gregor else { 17122c1dd271SDylan Noblesmith SmallString<128> PathName; 1713524e33e1SDouglas Gregor PathName = Directory->getName(); 1714524e33e1SDouglas Gregor llvm::sys::path::append(PathName, DirName); 1715524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(PathName); 1716524e33e1SDouglas Gregor } 1717524e33e1SDouglas Gregor 1718524e33e1SDouglas Gregor if (!Dir) { 1719524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1720524e33e1SDouglas Gregor << DirName; 1721524e33e1SDouglas Gregor HadError = true; 1722524e33e1SDouglas Gregor return; 1723524e33e1SDouglas Gregor } 1724524e33e1SDouglas Gregor 1725524e33e1SDouglas Gregor if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1726524e33e1SDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1727524e33e1SDouglas Gregor << OwningModule->getFullModuleName(); 1728524e33e1SDouglas Gregor HadError = true; 1729524e33e1SDouglas Gregor return; 1730524e33e1SDouglas Gregor } 1731524e33e1SDouglas Gregor 1732524e33e1SDouglas Gregor // Record this umbrella directory. 1733524e33e1SDouglas Gregor Map.setUmbrellaDir(ActiveModule, Dir); 1734524e33e1SDouglas Gregor } 1735524e33e1SDouglas Gregor 17362b82c2a5SDouglas Gregor /// \brief Parse a module export declaration. 17372b82c2a5SDouglas Gregor /// 17382b82c2a5SDouglas Gregor /// export-declaration: 17392b82c2a5SDouglas Gregor /// 'export' wildcard-module-id 17402b82c2a5SDouglas Gregor /// 17412b82c2a5SDouglas Gregor /// wildcard-module-id: 17422b82c2a5SDouglas Gregor /// identifier 17432b82c2a5SDouglas Gregor /// '*' 17442b82c2a5SDouglas Gregor /// identifier '.' wildcard-module-id 17452b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() { 17462b82c2a5SDouglas Gregor assert(Tok.is(MMToken::ExportKeyword)); 17472b82c2a5SDouglas Gregor SourceLocation ExportLoc = consumeToken(); 17482b82c2a5SDouglas Gregor 17492b82c2a5SDouglas Gregor // Parse the module-id with an optional wildcard at the end. 17502b82c2a5SDouglas Gregor ModuleId ParsedModuleId; 17512b82c2a5SDouglas Gregor bool Wildcard = false; 17522b82c2a5SDouglas Gregor do { 17532b82c2a5SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 17542b82c2a5SDouglas Gregor ParsedModuleId.push_back(std::make_pair(Tok.getString(), 17552b82c2a5SDouglas Gregor Tok.getLocation())); 17562b82c2a5SDouglas Gregor consumeToken(); 17572b82c2a5SDouglas Gregor 17582b82c2a5SDouglas Gregor if (Tok.is(MMToken::Period)) { 17592b82c2a5SDouglas Gregor consumeToken(); 17602b82c2a5SDouglas Gregor continue; 17612b82c2a5SDouglas Gregor } 17622b82c2a5SDouglas Gregor 17632b82c2a5SDouglas Gregor break; 17642b82c2a5SDouglas Gregor } 17652b82c2a5SDouglas Gregor 17662b82c2a5SDouglas Gregor if(Tok.is(MMToken::Star)) { 17672b82c2a5SDouglas Gregor Wildcard = true; 1768f5eedd05SDouglas Gregor consumeToken(); 17692b82c2a5SDouglas Gregor break; 17702b82c2a5SDouglas Gregor } 17712b82c2a5SDouglas Gregor 1772ba7f2f71SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); 17732b82c2a5SDouglas Gregor HadError = true; 17742b82c2a5SDouglas Gregor return; 17752b82c2a5SDouglas Gregor } while (true); 17762b82c2a5SDouglas Gregor 17772b82c2a5SDouglas Gregor Module::UnresolvedExportDecl Unresolved = { 17782b82c2a5SDouglas Gregor ExportLoc, ParsedModuleId, Wildcard 17792b82c2a5SDouglas Gregor }; 17802b82c2a5SDouglas Gregor ActiveModule->UnresolvedExports.push_back(Unresolved); 17812b82c2a5SDouglas Gregor } 17822b82c2a5SDouglas Gregor 1783ba7f2f71SDaniel Jasper /// \brief Parse a module uses declaration. 1784ba7f2f71SDaniel Jasper /// 1785ba7f2f71SDaniel Jasper /// uses-declaration: 1786ba7f2f71SDaniel Jasper /// 'uses' wildcard-module-id 1787ba7f2f71SDaniel Jasper void ModuleMapParser::parseUseDecl() { 1788ba7f2f71SDaniel Jasper assert(Tok.is(MMToken::UseKeyword)); 1789ba7f2f71SDaniel Jasper consumeToken(); 1790ba7f2f71SDaniel Jasper // Parse the module-id. 1791ba7f2f71SDaniel Jasper ModuleId ParsedModuleId; 17923cd34c76SDaniel Jasper parseModuleId(ParsedModuleId); 1793ba7f2f71SDaniel Jasper 1794ba7f2f71SDaniel Jasper ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId); 1795ba7f2f71SDaniel Jasper } 1796ba7f2f71SDaniel Jasper 17976ddfca91SDouglas Gregor /// \brief Parse a link declaration. 17986ddfca91SDouglas Gregor /// 17996ddfca91SDouglas Gregor /// module-declaration: 18006ddfca91SDouglas Gregor /// 'link' 'framework'[opt] string-literal 18016ddfca91SDouglas Gregor void ModuleMapParser::parseLinkDecl() { 18026ddfca91SDouglas Gregor assert(Tok.is(MMToken::LinkKeyword)); 18036ddfca91SDouglas Gregor SourceLocation LinkLoc = consumeToken(); 18046ddfca91SDouglas Gregor 18056ddfca91SDouglas Gregor // Parse the optional 'framework' keyword. 18066ddfca91SDouglas Gregor bool IsFramework = false; 18076ddfca91SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 18086ddfca91SDouglas Gregor consumeToken(); 18096ddfca91SDouglas Gregor IsFramework = true; 18106ddfca91SDouglas Gregor } 18116ddfca91SDouglas Gregor 18126ddfca91SDouglas Gregor // Parse the library name 18136ddfca91SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 18146ddfca91SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) 18156ddfca91SDouglas Gregor << IsFramework << SourceRange(LinkLoc); 18166ddfca91SDouglas Gregor HadError = true; 18176ddfca91SDouglas Gregor return; 18186ddfca91SDouglas Gregor } 18196ddfca91SDouglas Gregor 18206ddfca91SDouglas Gregor std::string LibraryName = Tok.getString(); 18216ddfca91SDouglas Gregor consumeToken(); 18226ddfca91SDouglas Gregor ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, 18236ddfca91SDouglas Gregor IsFramework)); 18246ddfca91SDouglas Gregor } 18256ddfca91SDouglas Gregor 182635b13eceSDouglas Gregor /// \brief Parse a configuration macro declaration. 182735b13eceSDouglas Gregor /// 182835b13eceSDouglas Gregor /// module-declaration: 182935b13eceSDouglas Gregor /// 'config_macros' attributes[opt] config-macro-list? 183035b13eceSDouglas Gregor /// 183135b13eceSDouglas Gregor /// config-macro-list: 183235b13eceSDouglas Gregor /// identifier (',' identifier)? 183335b13eceSDouglas Gregor void ModuleMapParser::parseConfigMacros() { 183435b13eceSDouglas Gregor assert(Tok.is(MMToken::ConfigMacros)); 183535b13eceSDouglas Gregor SourceLocation ConfigMacrosLoc = consumeToken(); 183635b13eceSDouglas Gregor 183735b13eceSDouglas Gregor // Only top-level modules can have configuration macros. 183835b13eceSDouglas Gregor if (ActiveModule->Parent) { 183935b13eceSDouglas Gregor Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule); 184035b13eceSDouglas Gregor } 184135b13eceSDouglas Gregor 184235b13eceSDouglas Gregor // Parse the optional attributes. 184335b13eceSDouglas Gregor Attributes Attrs; 184435b13eceSDouglas Gregor parseOptionalAttributes(Attrs); 184535b13eceSDouglas Gregor if (Attrs.IsExhaustive && !ActiveModule->Parent) { 184635b13eceSDouglas Gregor ActiveModule->ConfigMacrosExhaustive = true; 184735b13eceSDouglas Gregor } 184835b13eceSDouglas Gregor 184935b13eceSDouglas Gregor // If we don't have an identifier, we're done. 185035b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) 185135b13eceSDouglas Gregor return; 185235b13eceSDouglas Gregor 185335b13eceSDouglas Gregor // Consume the first identifier. 185435b13eceSDouglas Gregor if (!ActiveModule->Parent) { 185535b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 185635b13eceSDouglas Gregor } 185735b13eceSDouglas Gregor consumeToken(); 185835b13eceSDouglas Gregor 185935b13eceSDouglas Gregor do { 186035b13eceSDouglas Gregor // If there's a comma, consume it. 186135b13eceSDouglas Gregor if (!Tok.is(MMToken::Comma)) 186235b13eceSDouglas Gregor break; 186335b13eceSDouglas Gregor consumeToken(); 186435b13eceSDouglas Gregor 186535b13eceSDouglas Gregor // We expect to see a macro name here. 186635b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 186735b13eceSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro); 186835b13eceSDouglas Gregor break; 186935b13eceSDouglas Gregor } 187035b13eceSDouglas Gregor 187135b13eceSDouglas Gregor // Consume the macro name. 187235b13eceSDouglas Gregor if (!ActiveModule->Parent) { 187335b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 187435b13eceSDouglas Gregor } 187535b13eceSDouglas Gregor consumeToken(); 187635b13eceSDouglas Gregor } while (true); 187735b13eceSDouglas Gregor } 187835b13eceSDouglas Gregor 1879fb912657SDouglas Gregor /// \brief Format a module-id into a string. 1880fb912657SDouglas Gregor static std::string formatModuleId(const ModuleId &Id) { 1881fb912657SDouglas Gregor std::string result; 1882fb912657SDouglas Gregor { 1883fb912657SDouglas Gregor llvm::raw_string_ostream OS(result); 1884fb912657SDouglas Gregor 1885fb912657SDouglas Gregor for (unsigned I = 0, N = Id.size(); I != N; ++I) { 1886fb912657SDouglas Gregor if (I) 1887fb912657SDouglas Gregor OS << "."; 1888fb912657SDouglas Gregor OS << Id[I].first; 1889fb912657SDouglas Gregor } 1890fb912657SDouglas Gregor } 1891fb912657SDouglas Gregor 1892fb912657SDouglas Gregor return result; 1893fb912657SDouglas Gregor } 1894fb912657SDouglas Gregor 1895fb912657SDouglas Gregor /// \brief Parse a conflict declaration. 1896fb912657SDouglas Gregor /// 1897fb912657SDouglas Gregor /// module-declaration: 1898fb912657SDouglas Gregor /// 'conflict' module-id ',' string-literal 1899fb912657SDouglas Gregor void ModuleMapParser::parseConflict() { 1900fb912657SDouglas Gregor assert(Tok.is(MMToken::Conflict)); 1901fb912657SDouglas Gregor SourceLocation ConflictLoc = consumeToken(); 1902fb912657SDouglas Gregor Module::UnresolvedConflict Conflict; 1903fb912657SDouglas Gregor 1904fb912657SDouglas Gregor // Parse the module-id. 1905fb912657SDouglas Gregor if (parseModuleId(Conflict.Id)) 1906fb912657SDouglas Gregor return; 1907fb912657SDouglas Gregor 1908fb912657SDouglas Gregor // Parse the ','. 1909fb912657SDouglas Gregor if (!Tok.is(MMToken::Comma)) { 1910fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma) 1911fb912657SDouglas Gregor << SourceRange(ConflictLoc); 1912fb912657SDouglas Gregor return; 1913fb912657SDouglas Gregor } 1914fb912657SDouglas Gregor consumeToken(); 1915fb912657SDouglas Gregor 1916fb912657SDouglas Gregor // Parse the message. 1917fb912657SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1918fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message) 1919fb912657SDouglas Gregor << formatModuleId(Conflict.Id); 1920fb912657SDouglas Gregor return; 1921fb912657SDouglas Gregor } 1922fb912657SDouglas Gregor Conflict.Message = Tok.getString().str(); 1923fb912657SDouglas Gregor consumeToken(); 1924fb912657SDouglas Gregor 1925fb912657SDouglas Gregor // Add this unresolved conflict. 1926fb912657SDouglas Gregor ActiveModule->UnresolvedConflicts.push_back(Conflict); 1927fb912657SDouglas Gregor } 1928fb912657SDouglas Gregor 19296ddfca91SDouglas Gregor /// \brief Parse an inferred module declaration (wildcard modules). 19309194a91dSDouglas Gregor /// 19319194a91dSDouglas Gregor /// module-declaration: 19329194a91dSDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] 19339194a91dSDouglas Gregor /// { inferred-module-member* } 19349194a91dSDouglas Gregor /// 19359194a91dSDouglas Gregor /// inferred-module-member: 19369194a91dSDouglas Gregor /// 'export' '*' 19379194a91dSDouglas Gregor /// 'exclude' identifier 19389194a91dSDouglas Gregor void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { 193973441091SDouglas Gregor assert(Tok.is(MMToken::Star)); 194073441091SDouglas Gregor SourceLocation StarLoc = consumeToken(); 194173441091SDouglas Gregor bool Failed = false; 194273441091SDouglas Gregor 194373441091SDouglas Gregor // Inferred modules must be submodules. 19449194a91dSDouglas Gregor if (!ActiveModule && !Framework) { 194573441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 194673441091SDouglas Gregor Failed = true; 194773441091SDouglas Gregor } 194873441091SDouglas Gregor 19499194a91dSDouglas Gregor if (ActiveModule) { 1950524e33e1SDouglas Gregor // Inferred modules must have umbrella directories. 1951524e33e1SDouglas Gregor if (!Failed && !ActiveModule->getUmbrellaDir()) { 195273441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 195373441091SDouglas Gregor Failed = true; 195473441091SDouglas Gregor } 195573441091SDouglas Gregor 195673441091SDouglas Gregor // Check for redefinition of an inferred module. 1957dd005f69SDouglas Gregor if (!Failed && ActiveModule->InferSubmodules) { 195873441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 1959dd005f69SDouglas Gregor if (ActiveModule->InferredSubmoduleLoc.isValid()) 1960dd005f69SDouglas Gregor Diags.Report(ActiveModule->InferredSubmoduleLoc, 196173441091SDouglas Gregor diag::note_mmap_prev_definition); 196273441091SDouglas Gregor Failed = true; 196373441091SDouglas Gregor } 196473441091SDouglas Gregor 19659194a91dSDouglas Gregor // Check for the 'framework' keyword, which is not permitted here. 19669194a91dSDouglas Gregor if (Framework) { 19679194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); 19689194a91dSDouglas Gregor Framework = false; 19699194a91dSDouglas Gregor } 19709194a91dSDouglas Gregor } else if (Explicit) { 19719194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); 19729194a91dSDouglas Gregor Explicit = false; 19739194a91dSDouglas Gregor } 19749194a91dSDouglas Gregor 197573441091SDouglas Gregor // If there were any problems with this inferred submodule, skip its body. 197673441091SDouglas Gregor if (Failed) { 197773441091SDouglas Gregor if (Tok.is(MMToken::LBrace)) { 197873441091SDouglas Gregor consumeToken(); 197973441091SDouglas Gregor skipUntil(MMToken::RBrace); 198073441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 198173441091SDouglas Gregor consumeToken(); 198273441091SDouglas Gregor } 198373441091SDouglas Gregor HadError = true; 198473441091SDouglas Gregor return; 198573441091SDouglas Gregor } 198673441091SDouglas Gregor 19879194a91dSDouglas Gregor // Parse optional attributes. 19884442605fSBill Wendling Attributes Attrs; 19899194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 19909194a91dSDouglas Gregor 19919194a91dSDouglas Gregor if (ActiveModule) { 199273441091SDouglas Gregor // Note that we have an inferred submodule. 1993dd005f69SDouglas Gregor ActiveModule->InferSubmodules = true; 1994dd005f69SDouglas Gregor ActiveModule->InferredSubmoduleLoc = StarLoc; 1995dd005f69SDouglas Gregor ActiveModule->InferExplicitSubmodules = Explicit; 19969194a91dSDouglas Gregor } else { 19979194a91dSDouglas Gregor // We'll be inferring framework modules for this directory. 19989194a91dSDouglas Gregor Map.InferredDirectories[Directory].InferModules = true; 19999194a91dSDouglas Gregor Map.InferredDirectories[Directory].InferSystemModules = Attrs.IsSystem; 2000131daca0SRichard Smith // FIXME: Handle the 'framework' keyword. 20019194a91dSDouglas Gregor } 200273441091SDouglas Gregor 200373441091SDouglas Gregor // Parse the opening brace. 200473441091SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 200573441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 200673441091SDouglas Gregor HadError = true; 200773441091SDouglas Gregor return; 200873441091SDouglas Gregor } 200973441091SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 201073441091SDouglas Gregor 201173441091SDouglas Gregor // Parse the body of the inferred submodule. 201273441091SDouglas Gregor bool Done = false; 201373441091SDouglas Gregor do { 201473441091SDouglas Gregor switch (Tok.Kind) { 201573441091SDouglas Gregor case MMToken::EndOfFile: 201673441091SDouglas Gregor case MMToken::RBrace: 201773441091SDouglas Gregor Done = true; 201873441091SDouglas Gregor break; 201973441091SDouglas Gregor 20209194a91dSDouglas Gregor case MMToken::ExcludeKeyword: { 20219194a91dSDouglas Gregor if (ActiveModule) { 20229194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2023162405daSDouglas Gregor << (ActiveModule != 0); 20249194a91dSDouglas Gregor consumeToken(); 20259194a91dSDouglas Gregor break; 20269194a91dSDouglas Gregor } 20279194a91dSDouglas Gregor 20289194a91dSDouglas Gregor consumeToken(); 20299194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 20309194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); 20319194a91dSDouglas Gregor break; 20329194a91dSDouglas Gregor } 20339194a91dSDouglas Gregor 20349194a91dSDouglas Gregor Map.InferredDirectories[Directory].ExcludedModules 20359194a91dSDouglas Gregor .push_back(Tok.getString()); 20369194a91dSDouglas Gregor consumeToken(); 20379194a91dSDouglas Gregor break; 20389194a91dSDouglas Gregor } 20399194a91dSDouglas Gregor 20409194a91dSDouglas Gregor case MMToken::ExportKeyword: 20419194a91dSDouglas Gregor if (!ActiveModule) { 20429194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2043162405daSDouglas Gregor << (ActiveModule != 0); 20449194a91dSDouglas Gregor consumeToken(); 20459194a91dSDouglas Gregor break; 20469194a91dSDouglas Gregor } 20479194a91dSDouglas Gregor 204873441091SDouglas Gregor consumeToken(); 204973441091SDouglas Gregor if (Tok.is(MMToken::Star)) 2050dd005f69SDouglas Gregor ActiveModule->InferExportWildcard = true; 205173441091SDouglas Gregor else 205273441091SDouglas Gregor Diags.Report(Tok.getLocation(), 205373441091SDouglas Gregor diag::err_mmap_expected_export_wildcard); 205473441091SDouglas Gregor consumeToken(); 205573441091SDouglas Gregor break; 205673441091SDouglas Gregor 205773441091SDouglas Gregor case MMToken::ExplicitKeyword: 205873441091SDouglas Gregor case MMToken::ModuleKeyword: 205973441091SDouglas Gregor case MMToken::HeaderKeyword: 2060b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 206173441091SDouglas Gregor case MMToken::UmbrellaKeyword: 206273441091SDouglas Gregor default: 20639194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 2064162405daSDouglas Gregor << (ActiveModule != 0); 206573441091SDouglas Gregor consumeToken(); 206673441091SDouglas Gregor break; 206773441091SDouglas Gregor } 206873441091SDouglas Gregor } while (!Done); 206973441091SDouglas Gregor 207073441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 207173441091SDouglas Gregor consumeToken(); 207273441091SDouglas Gregor else { 207373441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 207473441091SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 207573441091SDouglas Gregor HadError = true; 207673441091SDouglas Gregor } 207773441091SDouglas Gregor } 207873441091SDouglas Gregor 20799194a91dSDouglas Gregor /// \brief Parse optional attributes. 20809194a91dSDouglas Gregor /// 20819194a91dSDouglas Gregor /// attributes: 20829194a91dSDouglas Gregor /// attribute attributes 20839194a91dSDouglas Gregor /// attribute 20849194a91dSDouglas Gregor /// 20859194a91dSDouglas Gregor /// attribute: 20869194a91dSDouglas Gregor /// [ identifier ] 20879194a91dSDouglas Gregor /// 20889194a91dSDouglas Gregor /// \param Attrs Will be filled in with the parsed attributes. 20899194a91dSDouglas Gregor /// 20909194a91dSDouglas Gregor /// \returns true if an error occurred, false otherwise. 20914442605fSBill Wendling bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { 20929194a91dSDouglas Gregor bool HadError = false; 20939194a91dSDouglas Gregor 20949194a91dSDouglas Gregor while (Tok.is(MMToken::LSquare)) { 20959194a91dSDouglas Gregor // Consume the '['. 20969194a91dSDouglas Gregor SourceLocation LSquareLoc = consumeToken(); 20979194a91dSDouglas Gregor 20989194a91dSDouglas Gregor // Check whether we have an attribute name here. 20999194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 21009194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 21019194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 21029194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 21039194a91dSDouglas Gregor consumeToken(); 21049194a91dSDouglas Gregor HadError = true; 21059194a91dSDouglas Gregor } 21069194a91dSDouglas Gregor 21079194a91dSDouglas Gregor // Decode the attribute name. 21089194a91dSDouglas Gregor AttributeKind Attribute 21099194a91dSDouglas Gregor = llvm::StringSwitch<AttributeKind>(Tok.getString()) 211035b13eceSDouglas Gregor .Case("exhaustive", AT_exhaustive) 211177944868SRichard Smith .Case("extern_c", AT_extern_c) 21129194a91dSDouglas Gregor .Case("system", AT_system) 21139194a91dSDouglas Gregor .Default(AT_unknown); 21149194a91dSDouglas Gregor switch (Attribute) { 21159194a91dSDouglas Gregor case AT_unknown: 21169194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 21179194a91dSDouglas Gregor << Tok.getString(); 21189194a91dSDouglas Gregor break; 21199194a91dSDouglas Gregor 21209194a91dSDouglas Gregor case AT_system: 21219194a91dSDouglas Gregor Attrs.IsSystem = true; 21229194a91dSDouglas Gregor break; 212335b13eceSDouglas Gregor 212477944868SRichard Smith case AT_extern_c: 212577944868SRichard Smith Attrs.IsExternC = true; 212677944868SRichard Smith break; 212777944868SRichard Smith 212835b13eceSDouglas Gregor case AT_exhaustive: 212935b13eceSDouglas Gregor Attrs.IsExhaustive = true; 213035b13eceSDouglas Gregor break; 21319194a91dSDouglas Gregor } 21329194a91dSDouglas Gregor consumeToken(); 21339194a91dSDouglas Gregor 21349194a91dSDouglas Gregor // Consume the ']'. 21359194a91dSDouglas Gregor if (!Tok.is(MMToken::RSquare)) { 21369194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 21379194a91dSDouglas Gregor Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 21389194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 21399194a91dSDouglas Gregor HadError = true; 21409194a91dSDouglas Gregor } 21419194a91dSDouglas Gregor 21429194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 21439194a91dSDouglas Gregor consumeToken(); 21449194a91dSDouglas Gregor } 21459194a91dSDouglas Gregor 21469194a91dSDouglas Gregor return HadError; 21479194a91dSDouglas Gregor } 21489194a91dSDouglas Gregor 21497033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence 21507033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null. 21517033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() { 21527033127bSDouglas Gregor for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) { 21537033127bSDouglas Gregor // If we have an umbrella directory, use that. 21547033127bSDouglas Gregor if (Mod->hasUmbrellaDir()) 21557033127bSDouglas Gregor return Mod->getUmbrellaDir(); 21567033127bSDouglas Gregor 21577033127bSDouglas Gregor // If we have a framework directory, stop looking. 21587033127bSDouglas Gregor if (Mod->IsFramework) 21597033127bSDouglas Gregor return 0; 21607033127bSDouglas Gregor } 21617033127bSDouglas Gregor 21627033127bSDouglas Gregor return 0; 21637033127bSDouglas Gregor } 21647033127bSDouglas Gregor 2165718292f2SDouglas Gregor /// \brief Parse a module map file. 2166718292f2SDouglas Gregor /// 2167718292f2SDouglas Gregor /// module-map-file: 2168718292f2SDouglas Gregor /// module-declaration* 2169718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() { 2170718292f2SDouglas Gregor do { 2171718292f2SDouglas Gregor switch (Tok.Kind) { 2172718292f2SDouglas Gregor case MMToken::EndOfFile: 2173718292f2SDouglas Gregor return HadError; 2174718292f2SDouglas Gregor 2175e7ab3669SDouglas Gregor case MMToken::ExplicitKeyword: 217697292843SDaniel Jasper case MMToken::ExternKeyword: 2177718292f2SDouglas Gregor case MMToken::ModuleKeyword: 2178755b2055SDouglas Gregor case MMToken::FrameworkKeyword: 2179718292f2SDouglas Gregor parseModuleDecl(); 2180718292f2SDouglas Gregor break; 2181718292f2SDouglas Gregor 21821fb5c3a6SDouglas Gregor case MMToken::Comma: 218335b13eceSDouglas Gregor case MMToken::ConfigMacros: 2184fb912657SDouglas Gregor case MMToken::Conflict: 2185a3feee2aSRichard Smith case MMToken::Exclaim: 218659527666SDouglas Gregor case MMToken::ExcludeKeyword: 21872b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 2188718292f2SDouglas Gregor case MMToken::HeaderKeyword: 2189718292f2SDouglas Gregor case MMToken::Identifier: 2190718292f2SDouglas Gregor case MMToken::LBrace: 21916ddfca91SDouglas Gregor case MMToken::LinkKeyword: 2192a686e1b0SDouglas Gregor case MMToken::LSquare: 21932b82c2a5SDouglas Gregor case MMToken::Period: 2194b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 2195718292f2SDouglas Gregor case MMToken::RBrace: 2196a686e1b0SDouglas Gregor case MMToken::RSquare: 21971fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 21982b82c2a5SDouglas Gregor case MMToken::Star: 2199718292f2SDouglas Gregor case MMToken::StringLiteral: 2200718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 2201ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 2202718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 2203718292f2SDouglas Gregor HadError = true; 2204718292f2SDouglas Gregor consumeToken(); 2205718292f2SDouglas Gregor break; 2206718292f2SDouglas Gregor } 2207718292f2SDouglas Gregor } while (true); 2208718292f2SDouglas Gregor } 2209718292f2SDouglas Gregor 2210963c5535SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem) { 22114ddf2221SDouglas Gregor llvm::DenseMap<const FileEntry *, bool>::iterator Known 22124ddf2221SDouglas Gregor = ParsedModuleMap.find(File); 22134ddf2221SDouglas Gregor if (Known != ParsedModuleMap.end()) 22144ddf2221SDouglas Gregor return Known->second; 22154ddf2221SDouglas Gregor 221689929282SDouglas Gregor assert(Target != 0 && "Missing target information"); 2217cb69b57bSBen Langmuir auto FileCharacter = IsSystem ? SrcMgr::C_System : SrcMgr::C_User; 2218cb69b57bSBen Langmuir FileID ID = SourceMgr.createFileID(File, SourceLocation(), FileCharacter); 22191f76c4e8SManuel Klimek const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID); 2220718292f2SDouglas Gregor if (!Buffer) 22214ddf2221SDouglas Gregor return ParsedModuleMap[File] = true; 2222718292f2SDouglas Gregor 2223*984e1df7SBen Langmuir // Find the directory for the module. For frameworks, that may require going 2224*984e1df7SBen Langmuir // up from the 'Modules' directory. 2225*984e1df7SBen Langmuir const DirectoryEntry *Dir = File->getDir(); 2226*984e1df7SBen Langmuir StringRef DirName(Dir->getName()); 2227*984e1df7SBen Langmuir if (llvm::sys::path::filename(DirName) == "Modules") { 2228*984e1df7SBen Langmuir DirName = llvm::sys::path::parent_path(DirName); 2229*984e1df7SBen Langmuir if (DirName.endswith(".framework")) 2230*984e1df7SBen Langmuir Dir = SourceMgr.getFileManager().getDirectory(DirName); 2231*984e1df7SBen Langmuir assert(Dir && "parent must exist"); 2232*984e1df7SBen Langmuir } 2233*984e1df7SBen Langmuir 2234718292f2SDouglas Gregor // Parse this module map file. 22351f76c4e8SManuel Klimek Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts); 2236*984e1df7SBen Langmuir ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, Dir, 2237963c5535SDouglas Gregor BuiltinIncludeDir, IsSystem); 2238718292f2SDouglas Gregor bool Result = Parser.parseModuleMapFile(); 22394ddf2221SDouglas Gregor ParsedModuleMap[File] = Result; 2240718292f2SDouglas Gregor return Result; 2241718292f2SDouglas Gregor } 2242