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) 62fb912657SDouglas Gregor 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) 73fb912657SDouglas Gregor 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 866b930967SDouglas Gregor ModuleMap::ModuleMap(FileManager &FileMgr, DiagnosticConsumer &DC, 87b146baabSArgyrios Kyrtzidis const LangOptions &LangOpts, const TargetInfo *Target, 88b146baabSArgyrios Kyrtzidis HeaderSearch &HeaderInfo) 89b146baabSArgyrios Kyrtzidis : LangOpts(LangOpts), Target(Target), HeaderInfo(HeaderInfo), 906f722b4eSArgyrios Kyrtzidis BuiltinIncludeDir(0), CompilingModule(0) 911fb5c3a6SDouglas Gregor { 92c95d8192SDylan Noblesmith IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs); 93c95d8192SDylan Noblesmith Diags = IntrusiveRefCntPtr<DiagnosticsEngine>( 94811db4eaSDouglas Gregor new DiagnosticsEngine(DiagIDs, new DiagnosticOptions)); 956b930967SDouglas Gregor Diags->setClient(new ForwardingDiagnosticConsumer(DC), 966b930967SDouglas Gregor /*ShouldOwnClient=*/true); 97718292f2SDouglas Gregor SourceMgr = new SourceManager(*Diags, FileMgr); 98718292f2SDouglas Gregor } 99718292f2SDouglas Gregor 100718292f2SDouglas Gregor ModuleMap::~ModuleMap() { 1015acdf59eSDouglas Gregor for (llvm::StringMap<Module *>::iterator I = Modules.begin(), 1025acdf59eSDouglas Gregor IEnd = Modules.end(); 1035acdf59eSDouglas Gregor I != IEnd; ++I) { 1045acdf59eSDouglas Gregor delete I->getValue(); 1055acdf59eSDouglas Gregor } 1065acdf59eSDouglas Gregor 107718292f2SDouglas Gregor delete SourceMgr; 108718292f2SDouglas Gregor } 109718292f2SDouglas Gregor 11089929282SDouglas Gregor void ModuleMap::setTarget(const TargetInfo &Target) { 11189929282SDouglas Gregor assert((!this->Target || this->Target == &Target) && 11289929282SDouglas Gregor "Improper target override"); 11389929282SDouglas Gregor this->Target = &Target; 11489929282SDouglas Gregor } 11589929282SDouglas Gregor 116056396aeSDouglas Gregor /// \brief "Sanitize" a filename so that it can be used as an identifier. 117056396aeSDouglas Gregor static StringRef sanitizeFilenameAsIdentifier(StringRef Name, 118056396aeSDouglas Gregor SmallVectorImpl<char> &Buffer) { 119056396aeSDouglas Gregor if (Name.empty()) 120056396aeSDouglas Gregor return Name; 121056396aeSDouglas Gregor 122a7d03840SJordan Rose if (!isValidIdentifier(Name)) { 123056396aeSDouglas Gregor // If we don't already have something with the form of an identifier, 124056396aeSDouglas Gregor // create a buffer with the sanitized name. 125056396aeSDouglas Gregor Buffer.clear(); 126a7d03840SJordan Rose if (isDigit(Name[0])) 127056396aeSDouglas Gregor Buffer.push_back('_'); 128056396aeSDouglas Gregor Buffer.reserve(Buffer.size() + Name.size()); 129056396aeSDouglas Gregor for (unsigned I = 0, N = Name.size(); I != N; ++I) { 130a7d03840SJordan Rose if (isIdentifierBody(Name[I])) 131056396aeSDouglas Gregor Buffer.push_back(Name[I]); 132056396aeSDouglas Gregor else 133056396aeSDouglas Gregor Buffer.push_back('_'); 134056396aeSDouglas Gregor } 135056396aeSDouglas Gregor 136056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 137056396aeSDouglas Gregor } 138056396aeSDouglas Gregor 139056396aeSDouglas Gregor while (llvm::StringSwitch<bool>(Name) 140056396aeSDouglas Gregor #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true) 141056396aeSDouglas Gregor #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true) 142056396aeSDouglas Gregor #include "clang/Basic/TokenKinds.def" 143056396aeSDouglas Gregor .Default(false)) { 144056396aeSDouglas Gregor if (Name.data() != Buffer.data()) 145056396aeSDouglas Gregor Buffer.append(Name.begin(), Name.end()); 146056396aeSDouglas Gregor Buffer.push_back('_'); 147056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 148056396aeSDouglas Gregor } 149056396aeSDouglas Gregor 150056396aeSDouglas Gregor return Name; 151056396aeSDouglas Gregor } 152056396aeSDouglas Gregor 15334d52749SDouglas Gregor /// \brief Determine whether the given file name is the name of a builtin 15434d52749SDouglas Gregor /// header, supplied by Clang to replace, override, or augment existing system 15534d52749SDouglas Gregor /// headers. 15634d52749SDouglas Gregor static bool isBuiltinHeader(StringRef FileName) { 15734d52749SDouglas Gregor return llvm::StringSwitch<bool>(FileName) 15834d52749SDouglas Gregor .Case("float.h", true) 15934d52749SDouglas Gregor .Case("iso646.h", true) 16034d52749SDouglas Gregor .Case("limits.h", true) 16134d52749SDouglas Gregor .Case("stdalign.h", true) 16234d52749SDouglas Gregor .Case("stdarg.h", true) 16334d52749SDouglas Gregor .Case("stdbool.h", true) 16434d52749SDouglas Gregor .Case("stddef.h", true) 16534d52749SDouglas Gregor .Case("stdint.h", true) 16634d52749SDouglas Gregor .Case("tgmath.h", true) 16734d52749SDouglas Gregor .Case("unwind.h", true) 16834d52749SDouglas Gregor .Default(false); 16934d52749SDouglas Gregor } 17034d52749SDouglas Gregor 171b53e5483SLawrence Crowl ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File) { 17259527666SDouglas Gregor HeadersMap::iterator Known = Headers.find(File); 1731fb5c3a6SDouglas Gregor if (Known != Headers.end()) { 17459527666SDouglas Gregor // If a header is not available, don't report that it maps to anything. 17559527666SDouglas Gregor if (!Known->second.isAvailable()) 176b53e5483SLawrence Crowl return KnownHeader(); 1771fb5c3a6SDouglas Gregor 178b53e5483SLawrence Crowl return Known->second; 1791fb5c3a6SDouglas Gregor } 180ab0c8a84SDouglas Gregor 18134d52749SDouglas Gregor // If we've found a builtin header within Clang's builtin include directory, 18234d52749SDouglas Gregor // load all of the module maps to see if it will get associated with a 18334d52749SDouglas Gregor // specific module (e.g., in /usr/include). 18434d52749SDouglas Gregor if (File->getDir() == BuiltinIncludeDir && 18534d52749SDouglas Gregor isBuiltinHeader(llvm::sys::path::filename(File->getName()))) { 18664a1fa5cSDouglas Gregor HeaderInfo.loadTopLevelSystemModules(); 18734d52749SDouglas Gregor 18834d52749SDouglas Gregor // Check again. 18934d52749SDouglas Gregor Known = Headers.find(File); 19034d52749SDouglas Gregor if (Known != Headers.end()) { 19134d52749SDouglas Gregor // If a header is not available, don't report that it maps to anything. 19234d52749SDouglas Gregor if (!Known->second.isAvailable()) 193b53e5483SLawrence Crowl return KnownHeader(); 19434d52749SDouglas Gregor 195b53e5483SLawrence Crowl return Known->second; 19634d52749SDouglas Gregor } 19734d52749SDouglas Gregor } 19834d52749SDouglas Gregor 199b65dbfffSDouglas Gregor const DirectoryEntry *Dir = File->getDir(); 200f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 201e00c8b20SDouglas Gregor 20274260502SDouglas Gregor // Note: as an egregious but useful hack we use the real path here, because 20374260502SDouglas Gregor // frameworks moving from top-level frameworks to embedded frameworks tend 20474260502SDouglas Gregor // to be symlinked from the top-level location to the embedded location, 20574260502SDouglas Gregor // and we need to resolve lookups as if we had found the embedded location. 206e00c8b20SDouglas Gregor StringRef DirName = SourceMgr->getFileManager().getCanonicalName(Dir); 207a89c5ac4SDouglas Gregor 208a89c5ac4SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 209a89c5ac4SDouglas Gregor // an umbrella header. 210b65dbfffSDouglas Gregor do { 211a89c5ac4SDouglas Gregor llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir 212a89c5ac4SDouglas Gregor = UmbrellaDirs.find(Dir); 213a89c5ac4SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 214a89c5ac4SDouglas Gregor Module *Result = KnownDir->second; 215930a85ccSDouglas Gregor 216930a85ccSDouglas Gregor // Search up the module stack until we find a module with an umbrella 21773141fa9SDouglas Gregor // directory. 218930a85ccSDouglas Gregor Module *UmbrellaModule = Result; 21973141fa9SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 220930a85ccSDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 221930a85ccSDouglas Gregor 222930a85ccSDouglas Gregor if (UmbrellaModule->InferSubmodules) { 223a89c5ac4SDouglas Gregor // Infer submodules for each of the directories we found between 224a89c5ac4SDouglas Gregor // the directory of the umbrella header and the directory where 225a89c5ac4SDouglas Gregor // the actual header is located. 2269458f82dSDouglas Gregor bool Explicit = UmbrellaModule->InferExplicitSubmodules; 2279458f82dSDouglas Gregor 2287033127bSDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 229a89c5ac4SDouglas Gregor // Find or create the module that corresponds to this directory name. 230056396aeSDouglas Gregor SmallString<32> NameBuf; 231056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 232056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 233056396aeSDouglas Gregor NameBuf); 234a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 2359458f82dSDouglas Gregor Explicit).first; 236a89c5ac4SDouglas Gregor 237a89c5ac4SDouglas Gregor // Associate the module and the directory. 238a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I-1]] = Result; 239a89c5ac4SDouglas Gregor 240a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 241a89c5ac4SDouglas Gregor // wildcard to the set of exports. 242930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 243a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 244a89c5ac4SDouglas Gregor } 245a89c5ac4SDouglas Gregor 246a89c5ac4SDouglas Gregor // Infer a submodule with the same name as this header file. 247056396aeSDouglas Gregor SmallString<32> NameBuf; 248056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 249056396aeSDouglas Gregor llvm::sys::path::stem(File->getName()), NameBuf); 250a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 2519458f82dSDouglas Gregor Explicit).first; 2523c5305c1SArgyrios Kyrtzidis Result->addTopHeader(File); 253a89c5ac4SDouglas Gregor 254a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 255a89c5ac4SDouglas Gregor // wildcard to the set of exports. 256930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 257a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 258a89c5ac4SDouglas Gregor } else { 259a89c5ac4SDouglas Gregor // Record each of the directories we stepped through as being part of 260a89c5ac4SDouglas Gregor // the module we found, since the umbrella header covers them all. 261a89c5ac4SDouglas Gregor for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 262a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I]] = Result; 263a89c5ac4SDouglas Gregor } 264a89c5ac4SDouglas Gregor 265b53e5483SLawrence Crowl Headers[File] = KnownHeader(Result, NormalHeader); 2661fb5c3a6SDouglas Gregor 2671fb5c3a6SDouglas Gregor // If a header corresponds to an unavailable module, don't report 2681fb5c3a6SDouglas Gregor // that it maps to anything. 2691fb5c3a6SDouglas Gregor if (!Result->isAvailable()) 270b53e5483SLawrence Crowl return KnownHeader(); 2711fb5c3a6SDouglas Gregor 272b53e5483SLawrence Crowl return Headers[File]; 273a89c5ac4SDouglas Gregor } 274a89c5ac4SDouglas Gregor 275a89c5ac4SDouglas Gregor SkippedDirs.push_back(Dir); 276a89c5ac4SDouglas Gregor 277b65dbfffSDouglas Gregor // Retrieve our parent path. 278b65dbfffSDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 279b65dbfffSDouglas Gregor if (DirName.empty()) 280b65dbfffSDouglas Gregor break; 281b65dbfffSDouglas Gregor 282b65dbfffSDouglas Gregor // Resolve the parent path to a directory entry. 283b65dbfffSDouglas Gregor Dir = SourceMgr->getFileManager().getDirectory(DirName); 284a89c5ac4SDouglas Gregor } while (Dir); 285b65dbfffSDouglas Gregor 286b53e5483SLawrence Crowl return KnownHeader(); 287ab0c8a84SDouglas Gregor } 288ab0c8a84SDouglas Gregor 289e4412640SArgyrios Kyrtzidis bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const { 290e4412640SArgyrios Kyrtzidis HeadersMap::const_iterator Known = Headers.find(Header); 2911fb5c3a6SDouglas Gregor if (Known != Headers.end()) 29259527666SDouglas Gregor return !Known->second.isAvailable(); 2931fb5c3a6SDouglas Gregor 2941fb5c3a6SDouglas Gregor const DirectoryEntry *Dir = Header->getDir(); 295f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 2961fb5c3a6SDouglas Gregor StringRef DirName = Dir->getName(); 2971fb5c3a6SDouglas Gregor 2981fb5c3a6SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 2991fb5c3a6SDouglas Gregor // an umbrella header. 3001fb5c3a6SDouglas Gregor do { 301e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir 3021fb5c3a6SDouglas Gregor = UmbrellaDirs.find(Dir); 3031fb5c3a6SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 3041fb5c3a6SDouglas Gregor Module *Found = KnownDir->second; 3051fb5c3a6SDouglas Gregor if (!Found->isAvailable()) 3061fb5c3a6SDouglas Gregor return true; 3071fb5c3a6SDouglas Gregor 3081fb5c3a6SDouglas Gregor // Search up the module stack until we find a module with an umbrella 3091fb5c3a6SDouglas Gregor // directory. 3101fb5c3a6SDouglas Gregor Module *UmbrellaModule = Found; 3111fb5c3a6SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 3121fb5c3a6SDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 3131fb5c3a6SDouglas Gregor 3141fb5c3a6SDouglas Gregor if (UmbrellaModule->InferSubmodules) { 3151fb5c3a6SDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 3161fb5c3a6SDouglas Gregor // Find or create the module that corresponds to this directory name. 317056396aeSDouglas Gregor SmallString<32> NameBuf; 318056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 319056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 320056396aeSDouglas Gregor NameBuf); 3211fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 3221fb5c3a6SDouglas Gregor if (!Found) 3231fb5c3a6SDouglas Gregor return false; 3241fb5c3a6SDouglas Gregor if (!Found->isAvailable()) 3251fb5c3a6SDouglas Gregor return true; 3261fb5c3a6SDouglas Gregor } 3271fb5c3a6SDouglas Gregor 3281fb5c3a6SDouglas Gregor // Infer a submodule with the same name as this header file. 329056396aeSDouglas Gregor SmallString<32> NameBuf; 330056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 331056396aeSDouglas Gregor llvm::sys::path::stem(Header->getName()), 332056396aeSDouglas Gregor NameBuf); 3331fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 3341fb5c3a6SDouglas Gregor if (!Found) 3351fb5c3a6SDouglas Gregor return false; 3361fb5c3a6SDouglas Gregor } 3371fb5c3a6SDouglas Gregor 3381fb5c3a6SDouglas Gregor return !Found->isAvailable(); 3391fb5c3a6SDouglas Gregor } 3401fb5c3a6SDouglas Gregor 3411fb5c3a6SDouglas Gregor SkippedDirs.push_back(Dir); 3421fb5c3a6SDouglas Gregor 3431fb5c3a6SDouglas Gregor // Retrieve our parent path. 3441fb5c3a6SDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 3451fb5c3a6SDouglas Gregor if (DirName.empty()) 3461fb5c3a6SDouglas Gregor break; 3471fb5c3a6SDouglas Gregor 3481fb5c3a6SDouglas Gregor // Resolve the parent path to a directory entry. 3491fb5c3a6SDouglas Gregor Dir = SourceMgr->getFileManager().getDirectory(DirName); 3501fb5c3a6SDouglas Gregor } while (Dir); 3511fb5c3a6SDouglas Gregor 3521fb5c3a6SDouglas Gregor return false; 3531fb5c3a6SDouglas Gregor } 3541fb5c3a6SDouglas Gregor 355e4412640SArgyrios Kyrtzidis Module *ModuleMap::findModule(StringRef Name) const { 356e4412640SArgyrios Kyrtzidis llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name); 35788bdfb0eSDouglas Gregor if (Known != Modules.end()) 35888bdfb0eSDouglas Gregor return Known->getValue(); 35988bdfb0eSDouglas Gregor 36088bdfb0eSDouglas Gregor return 0; 36188bdfb0eSDouglas Gregor } 36288bdfb0eSDouglas Gregor 363e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleUnqualified(StringRef Name, 364e4412640SArgyrios Kyrtzidis Module *Context) const { 3652b82c2a5SDouglas Gregor for(; Context; Context = Context->Parent) { 3662b82c2a5SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Context)) 3672b82c2a5SDouglas Gregor return Sub; 3682b82c2a5SDouglas Gregor } 3692b82c2a5SDouglas Gregor 3702b82c2a5SDouglas Gregor return findModule(Name); 3712b82c2a5SDouglas Gregor } 3722b82c2a5SDouglas Gregor 373e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{ 3742b82c2a5SDouglas Gregor if (!Context) 3752b82c2a5SDouglas Gregor return findModule(Name); 3762b82c2a5SDouglas Gregor 377eb90e830SDouglas Gregor return Context->findSubmodule(Name); 3782b82c2a5SDouglas Gregor } 3792b82c2a5SDouglas Gregor 380de3ef502SDouglas Gregor std::pair<Module *, bool> 38169021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 38269021974SDouglas Gregor bool IsExplicit) { 38369021974SDouglas Gregor // Try to find an existing module with this name. 384eb90e830SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Parent)) 385eb90e830SDouglas Gregor return std::make_pair(Sub, false); 38669021974SDouglas Gregor 38769021974SDouglas Gregor // Create a new module with this name. 38869021974SDouglas Gregor Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework, 38969021974SDouglas Gregor IsExplicit); 390*ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == Name) { 391*ba7f2f71SDaniel Jasper SourceModule = Result; 392*ba7f2f71SDaniel Jasper SourceModuleName = Name; 393*ba7f2f71SDaniel Jasper } 3946f722b4eSArgyrios Kyrtzidis if (!Parent) { 39569021974SDouglas Gregor Modules[Name] = Result; 3966f722b4eSArgyrios Kyrtzidis if (!LangOpts.CurrentModule.empty() && !CompilingModule && 3976f722b4eSArgyrios Kyrtzidis Name == LangOpts.CurrentModule) { 3986f722b4eSArgyrios Kyrtzidis CompilingModule = Result; 3996f722b4eSArgyrios Kyrtzidis } 4006f722b4eSArgyrios Kyrtzidis } 40169021974SDouglas Gregor return std::make_pair(Result, true); 40269021974SDouglas Gregor } 40369021974SDouglas Gregor 4049194a91dSDouglas Gregor bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir, 405e4412640SArgyrios Kyrtzidis StringRef Name, bool &IsSystem) const { 4069194a91dSDouglas Gregor // Check whether we have already looked into the parent directory 4079194a91dSDouglas Gregor // for a module map. 408e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 4099194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 4109194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) 4119194a91dSDouglas Gregor return false; 4129194a91dSDouglas Gregor 4139194a91dSDouglas Gregor if (!inferred->second.InferModules) 4149194a91dSDouglas Gregor return false; 4159194a91dSDouglas Gregor 4169194a91dSDouglas Gregor // We're allowed to infer for this directory, but make sure it's okay 4179194a91dSDouglas Gregor // to infer this particular module. 4189194a91dSDouglas Gregor bool canInfer = std::find(inferred->second.ExcludedModules.begin(), 4199194a91dSDouglas Gregor inferred->second.ExcludedModules.end(), 4209194a91dSDouglas Gregor Name) == inferred->second.ExcludedModules.end(); 4219194a91dSDouglas Gregor 4229194a91dSDouglas Gregor if (canInfer && inferred->second.InferSystemModules) 4239194a91dSDouglas Gregor IsSystem = true; 4249194a91dSDouglas Gregor 4259194a91dSDouglas Gregor return canInfer; 4269194a91dSDouglas Gregor } 4279194a91dSDouglas Gregor 42811dfe6feSDouglas Gregor /// \brief For a framework module, infer the framework against which we 42911dfe6feSDouglas Gregor /// should link. 43011dfe6feSDouglas Gregor static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir, 43111dfe6feSDouglas Gregor FileManager &FileMgr) { 43211dfe6feSDouglas Gregor assert(Mod->IsFramework && "Can only infer linking for framework modules"); 43311dfe6feSDouglas Gregor assert(!Mod->isSubFramework() && 43411dfe6feSDouglas Gregor "Can only infer linking for top-level frameworks"); 43511dfe6feSDouglas Gregor 43611dfe6feSDouglas Gregor SmallString<128> LibName; 43711dfe6feSDouglas Gregor LibName += FrameworkDir->getName(); 43811dfe6feSDouglas Gregor llvm::sys::path::append(LibName, Mod->Name); 43911dfe6feSDouglas Gregor if (FileMgr.getFile(LibName)) { 44011dfe6feSDouglas Gregor Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name, 44111dfe6feSDouglas Gregor /*IsFramework=*/true)); 44211dfe6feSDouglas Gregor } 44311dfe6feSDouglas Gregor } 44411dfe6feSDouglas Gregor 445de3ef502SDouglas Gregor Module * 44656c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName, 447e89dbc1dSDouglas Gregor const DirectoryEntry *FrameworkDir, 448a686e1b0SDouglas Gregor bool IsSystem, 449e89dbc1dSDouglas Gregor Module *Parent) { 45056c64013SDouglas Gregor // Check whether we've already found this module. 451e89dbc1dSDouglas Gregor if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 452e89dbc1dSDouglas Gregor return Mod; 453e89dbc1dSDouglas Gregor 454e89dbc1dSDouglas Gregor FileManager &FileMgr = SourceMgr->getFileManager(); 45556c64013SDouglas Gregor 4569194a91dSDouglas Gregor // If the framework has a parent path from which we're allowed to infer 4579194a91dSDouglas Gregor // a framework module, do so. 4589194a91dSDouglas Gregor if (!Parent) { 4594ddf2221SDouglas Gregor // Determine whether we're allowed to infer a module map. 460e00c8b20SDouglas Gregor 4614ddf2221SDouglas Gregor // Note: as an egregious but useful hack we use the real path here, because 4624ddf2221SDouglas Gregor // we might be looking at an embedded framework that symlinks out to a 4634ddf2221SDouglas Gregor // top-level framework, and we need to infer as if we were naming the 4644ddf2221SDouglas Gregor // top-level framework. 465e00c8b20SDouglas Gregor StringRef FrameworkDirName 466e00c8b20SDouglas Gregor = SourceMgr->getFileManager().getCanonicalName(FrameworkDir); 4674ddf2221SDouglas Gregor 4689194a91dSDouglas Gregor bool canInfer = false; 4694ddf2221SDouglas Gregor if (llvm::sys::path::has_parent_path(FrameworkDirName)) { 4709194a91dSDouglas Gregor // Figure out the parent path. 4714ddf2221SDouglas Gregor StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); 4729194a91dSDouglas Gregor if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) { 4739194a91dSDouglas Gregor // Check whether we have already looked into the parent directory 4749194a91dSDouglas Gregor // for a module map. 475e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 4769194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 4779194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) { 4789194a91dSDouglas Gregor // We haven't looked here before. Load a module map, if there is 4799194a91dSDouglas Gregor // one. 4809194a91dSDouglas Gregor SmallString<128> ModMapPath = Parent; 4819194a91dSDouglas Gregor llvm::sys::path::append(ModMapPath, "module.map"); 4829194a91dSDouglas Gregor if (const FileEntry *ModMapFile = FileMgr.getFile(ModMapPath)) { 483963c5535SDouglas Gregor parseModuleMapFile(ModMapFile, IsSystem); 4849194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 4859194a91dSDouglas Gregor } 4869194a91dSDouglas Gregor 4879194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) 4889194a91dSDouglas Gregor inferred = InferredDirectories.insert( 4899194a91dSDouglas Gregor std::make_pair(ParentDir, InferredDirectory())).first; 4909194a91dSDouglas Gregor } 4919194a91dSDouglas Gregor 4929194a91dSDouglas Gregor if (inferred->second.InferModules) { 4939194a91dSDouglas Gregor // We're allowed to infer for this directory, but make sure it's okay 4949194a91dSDouglas Gregor // to infer this particular module. 4954ddf2221SDouglas Gregor StringRef Name = llvm::sys::path::stem(FrameworkDirName); 4969194a91dSDouglas Gregor canInfer = std::find(inferred->second.ExcludedModules.begin(), 4979194a91dSDouglas Gregor inferred->second.ExcludedModules.end(), 4989194a91dSDouglas Gregor Name) == inferred->second.ExcludedModules.end(); 4999194a91dSDouglas Gregor 5009194a91dSDouglas Gregor if (inferred->second.InferSystemModules) 5019194a91dSDouglas Gregor IsSystem = true; 5029194a91dSDouglas Gregor } 5039194a91dSDouglas Gregor } 5049194a91dSDouglas Gregor } 5059194a91dSDouglas Gregor 5069194a91dSDouglas Gregor // If we're not allowed to infer a framework module, don't. 5079194a91dSDouglas Gregor if (!canInfer) 5089194a91dSDouglas Gregor return 0; 5099194a91dSDouglas Gregor } 5109194a91dSDouglas Gregor 5119194a91dSDouglas Gregor 51256c64013SDouglas Gregor // Look for an umbrella header. 5132c1dd271SDylan Noblesmith SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 51417381a06SBenjamin Kramer llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h"); 515e89dbc1dSDouglas Gregor const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 51656c64013SDouglas Gregor 51756c64013SDouglas Gregor // FIXME: If there's no umbrella header, we could probably scan the 51856c64013SDouglas Gregor // framework to load *everything*. But, it's not clear that this is a good 51956c64013SDouglas Gregor // idea. 52056c64013SDouglas Gregor if (!UmbrellaHeader) 52156c64013SDouglas Gregor return 0; 52256c64013SDouglas Gregor 523e89dbc1dSDouglas Gregor Module *Result = new Module(ModuleName, SourceLocation(), Parent, 524e89dbc1dSDouglas Gregor /*IsFramework=*/true, /*IsExplicit=*/false); 525*ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == ModuleName) { 526*ba7f2f71SDaniel Jasper SourceModule = Result; 527*ba7f2f71SDaniel Jasper SourceModuleName = ModuleName; 528*ba7f2f71SDaniel Jasper } 529a686e1b0SDouglas Gregor if (IsSystem) 530a686e1b0SDouglas Gregor Result->IsSystem = IsSystem; 531a686e1b0SDouglas Gregor 532eb90e830SDouglas Gregor if (!Parent) 533e89dbc1dSDouglas Gregor Modules[ModuleName] = Result; 534e89dbc1dSDouglas Gregor 535322f633cSDouglas Gregor // umbrella header "umbrella-header-name" 53673141fa9SDouglas Gregor Result->Umbrella = UmbrellaHeader; 537b53e5483SLawrence Crowl Headers[UmbrellaHeader] = KnownHeader(Result, NormalHeader); 5384dc71835SDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Result; 539d8bd7537SDouglas Gregor 540d8bd7537SDouglas Gregor // export * 541d8bd7537SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 542d8bd7537SDouglas Gregor 543a89c5ac4SDouglas Gregor // module * { export * } 544a89c5ac4SDouglas Gregor Result->InferSubmodules = true; 545a89c5ac4SDouglas Gregor Result->InferExportWildcard = true; 546a89c5ac4SDouglas Gregor 547e89dbc1dSDouglas Gregor // Look for subframeworks. 548e89dbc1dSDouglas Gregor llvm::error_code EC; 5492c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirName 550ddaa69cbSDouglas Gregor = StringRef(FrameworkDir->getName()); 551e89dbc1dSDouglas Gregor llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 5522d4d8cb3SBenjamin Kramer llvm::sys::path::native(SubframeworksDirName); 553ddaa69cbSDouglas Gregor for (llvm::sys::fs::directory_iterator 5542d4d8cb3SBenjamin Kramer Dir(SubframeworksDirName.str(), EC), DirEnd; 555e89dbc1dSDouglas Gregor Dir != DirEnd && !EC; Dir.increment(EC)) { 556e89dbc1dSDouglas Gregor if (!StringRef(Dir->path()).endswith(".framework")) 557e89dbc1dSDouglas Gregor continue; 558f2161a70SDouglas Gregor 559e89dbc1dSDouglas Gregor if (const DirectoryEntry *SubframeworkDir 560e89dbc1dSDouglas Gregor = FileMgr.getDirectory(Dir->path())) { 56107c22b78SDouglas Gregor // Note: as an egregious but useful hack, we use the real path here and 56207c22b78SDouglas Gregor // check whether it is actually a subdirectory of the parent directory. 56307c22b78SDouglas Gregor // This will not be the case if the 'subframework' is actually a symlink 56407c22b78SDouglas Gregor // out to a top-level framework. 565e00c8b20SDouglas Gregor StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir); 56607c22b78SDouglas Gregor bool FoundParent = false; 56707c22b78SDouglas Gregor do { 56807c22b78SDouglas Gregor // Get the parent directory name. 56907c22b78SDouglas Gregor SubframeworkDirName 57007c22b78SDouglas Gregor = llvm::sys::path::parent_path(SubframeworkDirName); 57107c22b78SDouglas Gregor if (SubframeworkDirName.empty()) 57207c22b78SDouglas Gregor break; 57307c22b78SDouglas Gregor 57407c22b78SDouglas Gregor if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { 57507c22b78SDouglas Gregor FoundParent = true; 57607c22b78SDouglas Gregor break; 57707c22b78SDouglas Gregor } 57807c22b78SDouglas Gregor } while (true); 57907c22b78SDouglas Gregor 58007c22b78SDouglas Gregor if (!FoundParent) 58107c22b78SDouglas Gregor continue; 58207c22b78SDouglas Gregor 583e89dbc1dSDouglas Gregor // FIXME: Do we want to warn about subframeworks without umbrella headers? 584056396aeSDouglas Gregor SmallString<32> NameBuf; 585056396aeSDouglas Gregor inferFrameworkModule(sanitizeFilenameAsIdentifier( 586056396aeSDouglas Gregor llvm::sys::path::stem(Dir->path()), NameBuf), 587056396aeSDouglas Gregor SubframeworkDir, IsSystem, Result); 588e89dbc1dSDouglas Gregor } 589e89dbc1dSDouglas Gregor } 590e89dbc1dSDouglas Gregor 59111dfe6feSDouglas Gregor // If the module is a top-level framework, automatically link against the 59211dfe6feSDouglas Gregor // framework. 59311dfe6feSDouglas Gregor if (!Result->isSubFramework()) { 59411dfe6feSDouglas Gregor inferFrameworkLink(Result, FrameworkDir, FileMgr); 59511dfe6feSDouglas Gregor } 59611dfe6feSDouglas Gregor 59756c64013SDouglas Gregor return Result; 59856c64013SDouglas Gregor } 59956c64013SDouglas Gregor 600a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ 601b53e5483SLawrence Crowl Headers[UmbrellaHeader] = KnownHeader(Mod, NormalHeader); 60273141fa9SDouglas Gregor Mod->Umbrella = UmbrellaHeader; 6037033127bSDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; 604a89c5ac4SDouglas Gregor } 605a89c5ac4SDouglas Gregor 606524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) { 607524e33e1SDouglas Gregor Mod->Umbrella = UmbrellaDir; 608524e33e1SDouglas Gregor UmbrellaDirs[UmbrellaDir] = Mod; 609524e33e1SDouglas Gregor } 610524e33e1SDouglas Gregor 61159527666SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header, 612b53e5483SLawrence Crowl ModuleHeaderRole Role) { 613b53e5483SLawrence Crowl if (Role == ExcludedHeader) { 61459527666SDouglas Gregor Mod->ExcludedHeaders.push_back(Header); 615b146baabSArgyrios Kyrtzidis } else { 616b53e5483SLawrence Crowl if (Role == PrivateHeader) 617b53e5483SLawrence Crowl Mod->PrivateHeaders.push_back(Header); 618b53e5483SLawrence Crowl else 619b53e5483SLawrence Crowl Mod->NormalHeaders.push_back(Header); 6206f722b4eSArgyrios Kyrtzidis bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule; 621b53e5483SLawrence Crowl HeaderInfo.MarkFileModuleHeader(Header, Role, isCompilingModuleHeader); 622b146baabSArgyrios Kyrtzidis } 623b53e5483SLawrence Crowl Headers[Header] = KnownHeader(Mod, Role); 624a89c5ac4SDouglas Gregor } 625a89c5ac4SDouglas Gregor 626514b636aSDouglas Gregor const FileEntry * 627e4412640SArgyrios Kyrtzidis ModuleMap::getContainingModuleMapFile(Module *Module) const { 628514b636aSDouglas Gregor if (Module->DefinitionLoc.isInvalid() || !SourceMgr) 629514b636aSDouglas Gregor return 0; 630514b636aSDouglas Gregor 631514b636aSDouglas Gregor return SourceMgr->getFileEntryForID( 632514b636aSDouglas Gregor SourceMgr->getFileID(Module->DefinitionLoc)); 633514b636aSDouglas Gregor } 634514b636aSDouglas Gregor 635718292f2SDouglas Gregor void ModuleMap::dump() { 636718292f2SDouglas Gregor llvm::errs() << "Modules:"; 637718292f2SDouglas Gregor for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 638718292f2SDouglas Gregor MEnd = Modules.end(); 639718292f2SDouglas Gregor M != MEnd; ++M) 640d28d1b8dSDouglas Gregor M->getValue()->print(llvm::errs(), 2); 641718292f2SDouglas Gregor 642718292f2SDouglas Gregor llvm::errs() << "Headers:"; 64359527666SDouglas Gregor for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 644718292f2SDouglas Gregor H != HEnd; ++H) { 645718292f2SDouglas Gregor llvm::errs() << " \"" << H->first->getName() << "\" -> " 64659527666SDouglas Gregor << H->second.getModule()->getFullModuleName() << "\n"; 647718292f2SDouglas Gregor } 648718292f2SDouglas Gregor } 649718292f2SDouglas Gregor 6502b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 6512b82c2a5SDouglas Gregor bool HadError = false; 6522b82c2a5SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { 6532b82c2a5SDouglas Gregor Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 6542b82c2a5SDouglas Gregor Complain); 655f5eedd05SDouglas Gregor if (Export.getPointer() || Export.getInt()) 6562b82c2a5SDouglas Gregor Mod->Exports.push_back(Export); 6572b82c2a5SDouglas Gregor else 6582b82c2a5SDouglas Gregor HadError = true; 6592b82c2a5SDouglas Gregor } 6602b82c2a5SDouglas Gregor Mod->UnresolvedExports.clear(); 6612b82c2a5SDouglas Gregor return HadError; 6622b82c2a5SDouglas Gregor } 6632b82c2a5SDouglas Gregor 664*ba7f2f71SDaniel Jasper bool ModuleMap::resolveUses(Module *Mod, bool Complain) { 665*ba7f2f71SDaniel Jasper bool HadError = false; 666*ba7f2f71SDaniel Jasper for (unsigned I = 0, N = Mod->UnresolvedDirectUses.size(); I != N; ++I) { 667*ba7f2f71SDaniel Jasper Module *DirectUse = 668*ba7f2f71SDaniel Jasper resolveModuleId(Mod->UnresolvedDirectUses[I], Mod, Complain); 669*ba7f2f71SDaniel Jasper if (DirectUse) 670*ba7f2f71SDaniel Jasper Mod->DirectUses.push_back(DirectUse); 671*ba7f2f71SDaniel Jasper else 672*ba7f2f71SDaniel Jasper HadError = true; 673*ba7f2f71SDaniel Jasper } 674*ba7f2f71SDaniel Jasper Mod->UnresolvedDirectUses.clear(); 675*ba7f2f71SDaniel Jasper return HadError; 676*ba7f2f71SDaniel Jasper } 677*ba7f2f71SDaniel Jasper 678fb912657SDouglas Gregor bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) { 679fb912657SDouglas Gregor bool HadError = false; 680fb912657SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) { 681fb912657SDouglas Gregor Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id, 682fb912657SDouglas Gregor Mod, Complain); 683fb912657SDouglas Gregor if (!OtherMod) { 684fb912657SDouglas Gregor HadError = true; 685fb912657SDouglas Gregor continue; 686fb912657SDouglas Gregor } 687fb912657SDouglas Gregor 688fb912657SDouglas Gregor Module::Conflict Conflict; 689fb912657SDouglas Gregor Conflict.Other = OtherMod; 690fb912657SDouglas Gregor Conflict.Message = Mod->UnresolvedConflicts[I].Message; 691fb912657SDouglas Gregor Mod->Conflicts.push_back(Conflict); 692fb912657SDouglas Gregor } 693fb912657SDouglas Gregor Mod->UnresolvedConflicts.clear(); 694fb912657SDouglas Gregor return HadError; 695fb912657SDouglas Gregor } 696fb912657SDouglas Gregor 6970093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 6980093b3c7SDouglas Gregor if (Loc.isInvalid()) 6990093b3c7SDouglas Gregor return 0; 7000093b3c7SDouglas Gregor 7010093b3c7SDouglas Gregor // Use the expansion location to determine which module we're in. 7020093b3c7SDouglas Gregor FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 7030093b3c7SDouglas Gregor if (!ExpansionLoc.isFileID()) 7040093b3c7SDouglas Gregor return 0; 7050093b3c7SDouglas Gregor 7060093b3c7SDouglas Gregor 7070093b3c7SDouglas Gregor const SourceManager &SrcMgr = Loc.getManager(); 7080093b3c7SDouglas Gregor FileID ExpansionFileID = ExpansionLoc.getFileID(); 709224d8a74SDouglas Gregor 710224d8a74SDouglas Gregor while (const FileEntry *ExpansionFile 711224d8a74SDouglas Gregor = SrcMgr.getFileEntryForID(ExpansionFileID)) { 712224d8a74SDouglas Gregor // Find the module that owns this header (if any). 713b53e5483SLawrence Crowl if (Module *Mod = findModuleForHeader(ExpansionFile).getModule()) 714224d8a74SDouglas Gregor return Mod; 715224d8a74SDouglas Gregor 716224d8a74SDouglas Gregor // No module owns this header, so look up the inclusion chain to see if 717224d8a74SDouglas Gregor // any included header has an associated module. 718224d8a74SDouglas Gregor SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); 719224d8a74SDouglas Gregor if (IncludeLoc.isInvalid()) 7200093b3c7SDouglas Gregor return 0; 7210093b3c7SDouglas Gregor 722224d8a74SDouglas Gregor ExpansionFileID = SrcMgr.getFileID(IncludeLoc); 723224d8a74SDouglas Gregor } 724224d8a74SDouglas Gregor 725224d8a74SDouglas Gregor return 0; 7260093b3c7SDouglas Gregor } 7270093b3c7SDouglas Gregor 728718292f2SDouglas Gregor //----------------------------------------------------------------------------// 729718292f2SDouglas Gregor // Module map file parser 730718292f2SDouglas Gregor //----------------------------------------------------------------------------// 731718292f2SDouglas Gregor 732718292f2SDouglas Gregor namespace clang { 733718292f2SDouglas Gregor /// \brief A token in a module map file. 734718292f2SDouglas Gregor struct MMToken { 735718292f2SDouglas Gregor enum TokenKind { 7361fb5c3a6SDouglas Gregor Comma, 73735b13eceSDouglas Gregor ConfigMacros, 738fb912657SDouglas Gregor Conflict, 739718292f2SDouglas Gregor EndOfFile, 740718292f2SDouglas Gregor HeaderKeyword, 741718292f2SDouglas Gregor Identifier, 74259527666SDouglas Gregor ExcludeKeyword, 743718292f2SDouglas Gregor ExplicitKeyword, 7442b82c2a5SDouglas Gregor ExportKeyword, 74597292843SDaniel Jasper ExternKeyword, 746755b2055SDouglas Gregor FrameworkKeyword, 7476ddfca91SDouglas Gregor LinkKeyword, 748718292f2SDouglas Gregor ModuleKeyword, 7492b82c2a5SDouglas Gregor Period, 750b53e5483SLawrence Crowl PrivateKeyword, 751718292f2SDouglas Gregor UmbrellaKeyword, 752*ba7f2f71SDaniel Jasper UseKeyword, 7531fb5c3a6SDouglas Gregor RequiresKeyword, 7542b82c2a5SDouglas Gregor Star, 755718292f2SDouglas Gregor StringLiteral, 756718292f2SDouglas Gregor LBrace, 757a686e1b0SDouglas Gregor RBrace, 758a686e1b0SDouglas Gregor LSquare, 759a686e1b0SDouglas Gregor RSquare 760718292f2SDouglas Gregor } Kind; 761718292f2SDouglas Gregor 762718292f2SDouglas Gregor unsigned Location; 763718292f2SDouglas Gregor unsigned StringLength; 764718292f2SDouglas Gregor const char *StringData; 765718292f2SDouglas Gregor 766718292f2SDouglas Gregor void clear() { 767718292f2SDouglas Gregor Kind = EndOfFile; 768718292f2SDouglas Gregor Location = 0; 769718292f2SDouglas Gregor StringLength = 0; 770718292f2SDouglas Gregor StringData = 0; 771718292f2SDouglas Gregor } 772718292f2SDouglas Gregor 773718292f2SDouglas Gregor bool is(TokenKind K) const { return Kind == K; } 774718292f2SDouglas Gregor 775718292f2SDouglas Gregor SourceLocation getLocation() const { 776718292f2SDouglas Gregor return SourceLocation::getFromRawEncoding(Location); 777718292f2SDouglas Gregor } 778718292f2SDouglas Gregor 779718292f2SDouglas Gregor StringRef getString() const { 780718292f2SDouglas Gregor return StringRef(StringData, StringLength); 781718292f2SDouglas Gregor } 782718292f2SDouglas Gregor }; 783718292f2SDouglas Gregor 7849194a91dSDouglas Gregor /// \brief The set of attributes that can be attached to a module. 7854442605fSBill Wendling struct Attributes { 78635b13eceSDouglas Gregor Attributes() : IsSystem(), IsExhaustive() { } 7879194a91dSDouglas Gregor 7889194a91dSDouglas Gregor /// \brief Whether this is a system module. 7899194a91dSDouglas Gregor unsigned IsSystem : 1; 79035b13eceSDouglas Gregor 79135b13eceSDouglas Gregor /// \brief Whether this is an exhaustive set of configuration macros. 79235b13eceSDouglas Gregor unsigned IsExhaustive : 1; 7939194a91dSDouglas Gregor }; 7949194a91dSDouglas Gregor 7959194a91dSDouglas Gregor 796718292f2SDouglas Gregor class ModuleMapParser { 797718292f2SDouglas Gregor Lexer &L; 798718292f2SDouglas Gregor SourceManager &SourceMgr; 799bc10b9fbSDouglas Gregor 800bc10b9fbSDouglas Gregor /// \brief Default target information, used only for string literal 801bc10b9fbSDouglas Gregor /// parsing. 802bc10b9fbSDouglas Gregor const TargetInfo *Target; 803bc10b9fbSDouglas Gregor 804718292f2SDouglas Gregor DiagnosticsEngine &Diags; 805718292f2SDouglas Gregor ModuleMap ⤅ 806718292f2SDouglas Gregor 8075257fc63SDouglas Gregor /// \brief The directory that this module map resides in. 8085257fc63SDouglas Gregor const DirectoryEntry *Directory; 8095257fc63SDouglas Gregor 8103ec6663bSDouglas Gregor /// \brief The directory containing Clang-supplied headers. 8113ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir; 8123ec6663bSDouglas Gregor 813963c5535SDouglas Gregor /// \brief Whether this module map is in a system header directory. 814963c5535SDouglas Gregor bool IsSystem; 815963c5535SDouglas Gregor 816718292f2SDouglas Gregor /// \brief Whether an error occurred. 817718292f2SDouglas Gregor bool HadError; 818718292f2SDouglas Gregor 819718292f2SDouglas Gregor /// \brief Stores string data for the various string literals referenced 820718292f2SDouglas Gregor /// during parsing. 821718292f2SDouglas Gregor llvm::BumpPtrAllocator StringData; 822718292f2SDouglas Gregor 823718292f2SDouglas Gregor /// \brief The current token. 824718292f2SDouglas Gregor MMToken Tok; 825718292f2SDouglas Gregor 826718292f2SDouglas Gregor /// \brief The active module. 827de3ef502SDouglas Gregor Module *ActiveModule; 828718292f2SDouglas Gregor 829718292f2SDouglas Gregor /// \brief Consume the current token and return its location. 830718292f2SDouglas Gregor SourceLocation consumeToken(); 831718292f2SDouglas Gregor 832718292f2SDouglas Gregor /// \brief Skip tokens until we reach the a token with the given kind 833718292f2SDouglas Gregor /// (or the end of the file). 834718292f2SDouglas Gregor void skipUntil(MMToken::TokenKind K); 835718292f2SDouglas Gregor 836f857950dSDmitri Gribenko typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; 837e7ab3669SDouglas Gregor bool parseModuleId(ModuleId &Id); 838718292f2SDouglas Gregor void parseModuleDecl(); 83997292843SDaniel Jasper void parseExternModuleDecl(); 8401fb5c3a6SDouglas Gregor void parseRequiresDecl(); 841b53e5483SLawrence Crowl void parseHeaderDecl(clang::MMToken::TokenKind, 842b53e5483SLawrence Crowl SourceLocation LeadingLoc); 843524e33e1SDouglas Gregor void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 8442b82c2a5SDouglas Gregor void parseExportDecl(); 845*ba7f2f71SDaniel Jasper void parseUseDecl(); 8466ddfca91SDouglas Gregor void parseLinkDecl(); 84735b13eceSDouglas Gregor void parseConfigMacros(); 848fb912657SDouglas Gregor void parseConflict(); 8499194a91dSDouglas Gregor void parseInferredModuleDecl(bool Framework, bool Explicit); 8504442605fSBill Wendling bool parseOptionalAttributes(Attributes &Attrs); 851718292f2SDouglas Gregor 8527033127bSDouglas Gregor const DirectoryEntry *getOverriddenHeaderSearchDir(); 8537033127bSDouglas Gregor 854718292f2SDouglas Gregor public: 855718292f2SDouglas Gregor explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 856bc10b9fbSDouglas Gregor const TargetInfo *Target, 857718292f2SDouglas Gregor DiagnosticsEngine &Diags, 8585257fc63SDouglas Gregor ModuleMap &Map, 8593ec6663bSDouglas Gregor const DirectoryEntry *Directory, 860963c5535SDouglas Gregor const DirectoryEntry *BuiltinIncludeDir, 861963c5535SDouglas Gregor bool IsSystem) 862bc10b9fbSDouglas Gregor : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 8633ec6663bSDouglas Gregor Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir), 864963c5535SDouglas Gregor IsSystem(IsSystem), HadError(false), ActiveModule(0) 865718292f2SDouglas Gregor { 866718292f2SDouglas Gregor Tok.clear(); 867718292f2SDouglas Gregor consumeToken(); 868718292f2SDouglas Gregor } 869718292f2SDouglas Gregor 870718292f2SDouglas Gregor bool parseModuleMapFile(); 871718292f2SDouglas Gregor }; 872718292f2SDouglas Gregor } 873718292f2SDouglas Gregor 874718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() { 875718292f2SDouglas Gregor retry: 876718292f2SDouglas Gregor SourceLocation Result = Tok.getLocation(); 877718292f2SDouglas Gregor Tok.clear(); 878718292f2SDouglas Gregor 879718292f2SDouglas Gregor Token LToken; 880718292f2SDouglas Gregor L.LexFromRawLexer(LToken); 881718292f2SDouglas Gregor Tok.Location = LToken.getLocation().getRawEncoding(); 882718292f2SDouglas Gregor switch (LToken.getKind()) { 883718292f2SDouglas Gregor case tok::raw_identifier: 884718292f2SDouglas Gregor Tok.StringData = LToken.getRawIdentifierData(); 885718292f2SDouglas Gregor Tok.StringLength = LToken.getLength(); 886718292f2SDouglas Gregor Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString()) 88735b13eceSDouglas Gregor .Case("config_macros", MMToken::ConfigMacros) 888fb912657SDouglas Gregor .Case("conflict", MMToken::Conflict) 88959527666SDouglas Gregor .Case("exclude", MMToken::ExcludeKeyword) 890718292f2SDouglas Gregor .Case("explicit", MMToken::ExplicitKeyword) 8912b82c2a5SDouglas Gregor .Case("export", MMToken::ExportKeyword) 89297292843SDaniel Jasper .Case("extern", MMToken::ExternKeyword) 893755b2055SDouglas Gregor .Case("framework", MMToken::FrameworkKeyword) 89435b13eceSDouglas Gregor .Case("header", MMToken::HeaderKeyword) 8956ddfca91SDouglas Gregor .Case("link", MMToken::LinkKeyword) 896718292f2SDouglas Gregor .Case("module", MMToken::ModuleKeyword) 897b53e5483SLawrence Crowl .Case("private", MMToken::PrivateKeyword) 8981fb5c3a6SDouglas Gregor .Case("requires", MMToken::RequiresKeyword) 899718292f2SDouglas Gregor .Case("umbrella", MMToken::UmbrellaKeyword) 900*ba7f2f71SDaniel Jasper .Case("use", MMToken::UseKeyword) 901718292f2SDouglas Gregor .Default(MMToken::Identifier); 902718292f2SDouglas Gregor break; 903718292f2SDouglas Gregor 9041fb5c3a6SDouglas Gregor case tok::comma: 9051fb5c3a6SDouglas Gregor Tok.Kind = MMToken::Comma; 9061fb5c3a6SDouglas Gregor break; 9071fb5c3a6SDouglas Gregor 908718292f2SDouglas Gregor case tok::eof: 909718292f2SDouglas Gregor Tok.Kind = MMToken::EndOfFile; 910718292f2SDouglas Gregor break; 911718292f2SDouglas Gregor 912718292f2SDouglas Gregor case tok::l_brace: 913718292f2SDouglas Gregor Tok.Kind = MMToken::LBrace; 914718292f2SDouglas Gregor break; 915718292f2SDouglas Gregor 916a686e1b0SDouglas Gregor case tok::l_square: 917a686e1b0SDouglas Gregor Tok.Kind = MMToken::LSquare; 918a686e1b0SDouglas Gregor break; 919a686e1b0SDouglas Gregor 9202b82c2a5SDouglas Gregor case tok::period: 9212b82c2a5SDouglas Gregor Tok.Kind = MMToken::Period; 9222b82c2a5SDouglas Gregor break; 9232b82c2a5SDouglas Gregor 924718292f2SDouglas Gregor case tok::r_brace: 925718292f2SDouglas Gregor Tok.Kind = MMToken::RBrace; 926718292f2SDouglas Gregor break; 927718292f2SDouglas Gregor 928a686e1b0SDouglas Gregor case tok::r_square: 929a686e1b0SDouglas Gregor Tok.Kind = MMToken::RSquare; 930a686e1b0SDouglas Gregor break; 931a686e1b0SDouglas Gregor 9322b82c2a5SDouglas Gregor case tok::star: 9332b82c2a5SDouglas Gregor Tok.Kind = MMToken::Star; 9342b82c2a5SDouglas Gregor break; 9352b82c2a5SDouglas Gregor 936718292f2SDouglas Gregor case tok::string_literal: { 937d67aea28SRichard Smith if (LToken.hasUDSuffix()) { 938d67aea28SRichard Smith Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 939d67aea28SRichard Smith HadError = true; 940d67aea28SRichard Smith goto retry; 941d67aea28SRichard Smith } 942d67aea28SRichard Smith 943718292f2SDouglas Gregor // Parse the string literal. 944718292f2SDouglas Gregor LangOptions LangOpts; 945718292f2SDouglas Gregor StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target); 946718292f2SDouglas Gregor if (StringLiteral.hadError) 947718292f2SDouglas Gregor goto retry; 948718292f2SDouglas Gregor 949718292f2SDouglas Gregor // Copy the string literal into our string data allocator. 950718292f2SDouglas Gregor unsigned Length = StringLiteral.GetStringLength(); 951718292f2SDouglas Gregor char *Saved = StringData.Allocate<char>(Length + 1); 952718292f2SDouglas Gregor memcpy(Saved, StringLiteral.GetString().data(), Length); 953718292f2SDouglas Gregor Saved[Length] = 0; 954718292f2SDouglas Gregor 955718292f2SDouglas Gregor // Form the token. 956718292f2SDouglas Gregor Tok.Kind = MMToken::StringLiteral; 957718292f2SDouglas Gregor Tok.StringData = Saved; 958718292f2SDouglas Gregor Tok.StringLength = Length; 959718292f2SDouglas Gregor break; 960718292f2SDouglas Gregor } 961718292f2SDouglas Gregor 962718292f2SDouglas Gregor case tok::comment: 963718292f2SDouglas Gregor goto retry; 964718292f2SDouglas Gregor 965718292f2SDouglas Gregor default: 966718292f2SDouglas Gregor Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 967718292f2SDouglas Gregor HadError = true; 968718292f2SDouglas Gregor goto retry; 969718292f2SDouglas Gregor } 970718292f2SDouglas Gregor 971718292f2SDouglas Gregor return Result; 972718292f2SDouglas Gregor } 973718292f2SDouglas Gregor 974718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 975718292f2SDouglas Gregor unsigned braceDepth = 0; 976a686e1b0SDouglas Gregor unsigned squareDepth = 0; 977718292f2SDouglas Gregor do { 978718292f2SDouglas Gregor switch (Tok.Kind) { 979718292f2SDouglas Gregor case MMToken::EndOfFile: 980718292f2SDouglas Gregor return; 981718292f2SDouglas Gregor 982718292f2SDouglas Gregor case MMToken::LBrace: 983a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 984718292f2SDouglas Gregor return; 985718292f2SDouglas Gregor 986718292f2SDouglas Gregor ++braceDepth; 987718292f2SDouglas Gregor break; 988718292f2SDouglas Gregor 989a686e1b0SDouglas Gregor case MMToken::LSquare: 990a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 991a686e1b0SDouglas Gregor return; 992a686e1b0SDouglas Gregor 993a686e1b0SDouglas Gregor ++squareDepth; 994a686e1b0SDouglas Gregor break; 995a686e1b0SDouglas Gregor 996718292f2SDouglas Gregor case MMToken::RBrace: 997718292f2SDouglas Gregor if (braceDepth > 0) 998718292f2SDouglas Gregor --braceDepth; 999718292f2SDouglas Gregor else if (Tok.is(K)) 1000718292f2SDouglas Gregor return; 1001718292f2SDouglas Gregor break; 1002718292f2SDouglas Gregor 1003a686e1b0SDouglas Gregor case MMToken::RSquare: 1004a686e1b0SDouglas Gregor if (squareDepth > 0) 1005a686e1b0SDouglas Gregor --squareDepth; 1006a686e1b0SDouglas Gregor else if (Tok.is(K)) 1007a686e1b0SDouglas Gregor return; 1008a686e1b0SDouglas Gregor break; 1009a686e1b0SDouglas Gregor 1010718292f2SDouglas Gregor default: 1011a686e1b0SDouglas Gregor if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 1012718292f2SDouglas Gregor return; 1013718292f2SDouglas Gregor break; 1014718292f2SDouglas Gregor } 1015718292f2SDouglas Gregor 1016718292f2SDouglas Gregor consumeToken(); 1017718292f2SDouglas Gregor } while (true); 1018718292f2SDouglas Gregor } 1019718292f2SDouglas Gregor 1020e7ab3669SDouglas Gregor /// \brief Parse a module-id. 1021e7ab3669SDouglas Gregor /// 1022e7ab3669SDouglas Gregor /// module-id: 1023e7ab3669SDouglas Gregor /// identifier 1024e7ab3669SDouglas Gregor /// identifier '.' module-id 1025e7ab3669SDouglas Gregor /// 1026e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise. 1027e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) { 1028e7ab3669SDouglas Gregor Id.clear(); 1029e7ab3669SDouglas Gregor do { 1030e7ab3669SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 1031e7ab3669SDouglas Gregor Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 1032e7ab3669SDouglas Gregor consumeToken(); 1033e7ab3669SDouglas Gregor } else { 1034e7ab3669SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 1035e7ab3669SDouglas Gregor return true; 1036e7ab3669SDouglas Gregor } 1037e7ab3669SDouglas Gregor 1038e7ab3669SDouglas Gregor if (!Tok.is(MMToken::Period)) 1039e7ab3669SDouglas Gregor break; 1040e7ab3669SDouglas Gregor 1041e7ab3669SDouglas Gregor consumeToken(); 1042e7ab3669SDouglas Gregor } while (true); 1043e7ab3669SDouglas Gregor 1044e7ab3669SDouglas Gregor return false; 1045e7ab3669SDouglas Gregor } 1046e7ab3669SDouglas Gregor 1047a686e1b0SDouglas Gregor namespace { 1048a686e1b0SDouglas Gregor /// \brief Enumerates the known attributes. 1049a686e1b0SDouglas Gregor enum AttributeKind { 1050a686e1b0SDouglas Gregor /// \brief An unknown attribute. 1051a686e1b0SDouglas Gregor AT_unknown, 1052a686e1b0SDouglas Gregor /// \brief The 'system' attribute. 105335b13eceSDouglas Gregor AT_system, 105435b13eceSDouglas Gregor /// \brief The 'exhaustive' attribute. 105535b13eceSDouglas Gregor AT_exhaustive 1056a686e1b0SDouglas Gregor }; 1057a686e1b0SDouglas Gregor } 1058a686e1b0SDouglas Gregor 1059718292f2SDouglas Gregor /// \brief Parse a module declaration. 1060718292f2SDouglas Gregor /// 1061718292f2SDouglas Gregor /// module-declaration: 106297292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 1063a686e1b0SDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 1064a686e1b0SDouglas Gregor /// { module-member* } 1065a686e1b0SDouglas Gregor /// 1066718292f2SDouglas Gregor /// module-member: 10671fb5c3a6SDouglas Gregor /// requires-declaration 1068718292f2SDouglas Gregor /// header-declaration 1069e7ab3669SDouglas Gregor /// submodule-declaration 10702b82c2a5SDouglas Gregor /// export-declaration 10716ddfca91SDouglas Gregor /// link-declaration 107273441091SDouglas Gregor /// 107373441091SDouglas Gregor /// submodule-declaration: 107473441091SDouglas Gregor /// module-declaration 107573441091SDouglas Gregor /// inferred-submodule-declaration 1076718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() { 1077755b2055SDouglas Gregor assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 107897292843SDaniel Jasper Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword)); 107997292843SDaniel Jasper if (Tok.is(MMToken::ExternKeyword)) { 108097292843SDaniel Jasper parseExternModuleDecl(); 108197292843SDaniel Jasper return; 108297292843SDaniel Jasper } 108397292843SDaniel Jasper 1084f2161a70SDouglas Gregor // Parse 'explicit' or 'framework' keyword, if present. 1085e7ab3669SDouglas Gregor SourceLocation ExplicitLoc; 1086718292f2SDouglas Gregor bool Explicit = false; 1087f2161a70SDouglas Gregor bool Framework = false; 1088755b2055SDouglas Gregor 1089f2161a70SDouglas Gregor // Parse 'explicit' keyword, if present. 1090f2161a70SDouglas Gregor if (Tok.is(MMToken::ExplicitKeyword)) { 1091e7ab3669SDouglas Gregor ExplicitLoc = consumeToken(); 1092f2161a70SDouglas Gregor Explicit = true; 1093f2161a70SDouglas Gregor } 1094f2161a70SDouglas Gregor 1095f2161a70SDouglas Gregor // Parse 'framework' keyword, if present. 1096755b2055SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 1097755b2055SDouglas Gregor consumeToken(); 1098755b2055SDouglas Gregor Framework = true; 1099755b2055SDouglas Gregor } 1100718292f2SDouglas Gregor 1101718292f2SDouglas Gregor // Parse 'module' keyword. 1102718292f2SDouglas Gregor if (!Tok.is(MMToken::ModuleKeyword)) { 1103d6343c99SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1104718292f2SDouglas Gregor consumeToken(); 1105718292f2SDouglas Gregor HadError = true; 1106718292f2SDouglas Gregor return; 1107718292f2SDouglas Gregor } 1108718292f2SDouglas Gregor consumeToken(); // 'module' keyword 1109718292f2SDouglas Gregor 111073441091SDouglas Gregor // If we have a wildcard for the module name, this is an inferred submodule. 111173441091SDouglas Gregor // Parse it. 111273441091SDouglas Gregor if (Tok.is(MMToken::Star)) 11139194a91dSDouglas Gregor return parseInferredModuleDecl(Framework, Explicit); 111473441091SDouglas Gregor 1115718292f2SDouglas Gregor // Parse the module name. 1116e7ab3669SDouglas Gregor ModuleId Id; 1117e7ab3669SDouglas Gregor if (parseModuleId(Id)) { 1118718292f2SDouglas Gregor HadError = true; 1119718292f2SDouglas Gregor return; 1120718292f2SDouglas Gregor } 1121e7ab3669SDouglas Gregor 1122e7ab3669SDouglas Gregor if (ActiveModule) { 1123e7ab3669SDouglas Gregor if (Id.size() > 1) { 1124e7ab3669SDouglas Gregor Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 1125e7ab3669SDouglas Gregor << SourceRange(Id.front().second, Id.back().second); 1126e7ab3669SDouglas Gregor 1127e7ab3669SDouglas Gregor HadError = true; 1128e7ab3669SDouglas Gregor return; 1129e7ab3669SDouglas Gregor } 1130e7ab3669SDouglas Gregor } else if (Id.size() == 1 && Explicit) { 1131e7ab3669SDouglas Gregor // Top-level modules can't be explicit. 1132e7ab3669SDouglas Gregor Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 1133e7ab3669SDouglas Gregor Explicit = false; 1134e7ab3669SDouglas Gregor ExplicitLoc = SourceLocation(); 1135e7ab3669SDouglas Gregor HadError = true; 1136e7ab3669SDouglas Gregor } 1137e7ab3669SDouglas Gregor 1138e7ab3669SDouglas Gregor Module *PreviousActiveModule = ActiveModule; 1139e7ab3669SDouglas Gregor if (Id.size() > 1) { 1140e7ab3669SDouglas Gregor // This module map defines a submodule. Go find the module of which it 1141e7ab3669SDouglas Gregor // is a submodule. 1142e7ab3669SDouglas Gregor ActiveModule = 0; 1143e7ab3669SDouglas Gregor for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 1144e7ab3669SDouglas Gregor if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 1145e7ab3669SDouglas Gregor ActiveModule = Next; 1146e7ab3669SDouglas Gregor continue; 1147e7ab3669SDouglas Gregor } 1148e7ab3669SDouglas Gregor 1149e7ab3669SDouglas Gregor if (ActiveModule) { 1150e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 1151e7ab3669SDouglas Gregor << Id[I].first << ActiveModule->getTopLevelModule(); 1152e7ab3669SDouglas Gregor } else { 1153e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 1154e7ab3669SDouglas Gregor } 1155e7ab3669SDouglas Gregor HadError = true; 1156e7ab3669SDouglas Gregor return; 1157e7ab3669SDouglas Gregor } 1158e7ab3669SDouglas Gregor } 1159e7ab3669SDouglas Gregor 1160e7ab3669SDouglas Gregor StringRef ModuleName = Id.back().first; 1161e7ab3669SDouglas Gregor SourceLocation ModuleNameLoc = Id.back().second; 1162718292f2SDouglas Gregor 1163a686e1b0SDouglas Gregor // Parse the optional attribute list. 11644442605fSBill Wendling Attributes Attrs; 11659194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 1166a686e1b0SDouglas Gregor 1167718292f2SDouglas Gregor // Parse the opening brace. 1168718292f2SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 1169718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 1170718292f2SDouglas Gregor << ModuleName; 1171718292f2SDouglas Gregor HadError = true; 1172718292f2SDouglas Gregor return; 1173718292f2SDouglas Gregor } 1174718292f2SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 1175718292f2SDouglas Gregor 1176718292f2SDouglas Gregor // Determine whether this (sub)module has already been defined. 1177eb90e830SDouglas Gregor if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 1178fcc54a3bSDouglas Gregor if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { 1179fcc54a3bSDouglas Gregor // Skip the module definition. 1180fcc54a3bSDouglas Gregor skipUntil(MMToken::RBrace); 1181fcc54a3bSDouglas Gregor if (Tok.is(MMToken::RBrace)) 1182fcc54a3bSDouglas Gregor consumeToken(); 1183fcc54a3bSDouglas Gregor else { 1184fcc54a3bSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1185fcc54a3bSDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1186fcc54a3bSDouglas Gregor HadError = true; 1187fcc54a3bSDouglas Gregor } 1188fcc54a3bSDouglas Gregor return; 1189fcc54a3bSDouglas Gregor } 1190fcc54a3bSDouglas Gregor 1191718292f2SDouglas Gregor Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 1192718292f2SDouglas Gregor << ModuleName; 1193eb90e830SDouglas Gregor Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 1194718292f2SDouglas Gregor 1195718292f2SDouglas Gregor // Skip the module definition. 1196718292f2SDouglas Gregor skipUntil(MMToken::RBrace); 1197718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1198718292f2SDouglas Gregor consumeToken(); 1199718292f2SDouglas Gregor 1200718292f2SDouglas Gregor HadError = true; 1201718292f2SDouglas Gregor return; 1202718292f2SDouglas Gregor } 1203718292f2SDouglas Gregor 1204718292f2SDouglas Gregor // Start defining this module. 1205eb90e830SDouglas Gregor ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, 1206eb90e830SDouglas Gregor Explicit).first; 1207eb90e830SDouglas Gregor ActiveModule->DefinitionLoc = ModuleNameLoc; 1208963c5535SDouglas Gregor if (Attrs.IsSystem || IsSystem) 1209a686e1b0SDouglas Gregor ActiveModule->IsSystem = true; 1210718292f2SDouglas Gregor 1211718292f2SDouglas Gregor bool Done = false; 1212718292f2SDouglas Gregor do { 1213718292f2SDouglas Gregor switch (Tok.Kind) { 1214718292f2SDouglas Gregor case MMToken::EndOfFile: 1215718292f2SDouglas Gregor case MMToken::RBrace: 1216718292f2SDouglas Gregor Done = true; 1217718292f2SDouglas Gregor break; 1218718292f2SDouglas Gregor 121935b13eceSDouglas Gregor case MMToken::ConfigMacros: 122035b13eceSDouglas Gregor parseConfigMacros(); 122135b13eceSDouglas Gregor break; 122235b13eceSDouglas Gregor 1223fb912657SDouglas Gregor case MMToken::Conflict: 1224fb912657SDouglas Gregor parseConflict(); 1225fb912657SDouglas Gregor break; 1226fb912657SDouglas Gregor 1227718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 122897292843SDaniel Jasper case MMToken::ExternKeyword: 1229f2161a70SDouglas Gregor case MMToken::FrameworkKeyword: 1230718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1231718292f2SDouglas Gregor parseModuleDecl(); 1232718292f2SDouglas Gregor break; 1233718292f2SDouglas Gregor 12342b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 12352b82c2a5SDouglas Gregor parseExportDecl(); 12362b82c2a5SDouglas Gregor break; 12372b82c2a5SDouglas Gregor 1238*ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 1239*ba7f2f71SDaniel Jasper parseUseDecl(); 1240*ba7f2f71SDaniel Jasper break; 1241*ba7f2f71SDaniel Jasper 12421fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 12431fb5c3a6SDouglas Gregor parseRequiresDecl(); 12441fb5c3a6SDouglas Gregor break; 12451fb5c3a6SDouglas Gregor 1246524e33e1SDouglas Gregor case MMToken::UmbrellaKeyword: { 1247524e33e1SDouglas Gregor SourceLocation UmbrellaLoc = consumeToken(); 1248524e33e1SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) 1249b53e5483SLawrence Crowl parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc); 1250524e33e1SDouglas Gregor else 1251524e33e1SDouglas Gregor parseUmbrellaDirDecl(UmbrellaLoc); 1252718292f2SDouglas Gregor break; 1253524e33e1SDouglas Gregor } 1254718292f2SDouglas Gregor 125559527666SDouglas Gregor case MMToken::ExcludeKeyword: { 125659527666SDouglas Gregor SourceLocation ExcludeLoc = consumeToken(); 125759527666SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) { 1258b53e5483SLawrence Crowl parseHeaderDecl(MMToken::ExcludeKeyword, ExcludeLoc); 125959527666SDouglas Gregor } else { 126059527666SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 126159527666SDouglas Gregor << "exclude"; 126259527666SDouglas Gregor } 126359527666SDouglas Gregor break; 126459527666SDouglas Gregor } 126559527666SDouglas Gregor 1266b53e5483SLawrence Crowl case MMToken::PrivateKeyword: { 1267b53e5483SLawrence Crowl SourceLocation PrivateLoc = consumeToken(); 1268b53e5483SLawrence Crowl if (Tok.is(MMToken::HeaderKeyword)) { 1269b53e5483SLawrence Crowl parseHeaderDecl(MMToken::PrivateKeyword, PrivateLoc); 1270b53e5483SLawrence Crowl } else { 1271b53e5483SLawrence Crowl Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1272b53e5483SLawrence Crowl << "private"; 1273b53e5483SLawrence Crowl } 1274b53e5483SLawrence Crowl break; 1275b53e5483SLawrence Crowl } 1276b53e5483SLawrence Crowl 1277322f633cSDouglas Gregor case MMToken::HeaderKeyword: 1278b53e5483SLawrence Crowl parseHeaderDecl(MMToken::HeaderKeyword, SourceLocation()); 1279718292f2SDouglas Gregor break; 1280718292f2SDouglas Gregor 12816ddfca91SDouglas Gregor case MMToken::LinkKeyword: 12826ddfca91SDouglas Gregor parseLinkDecl(); 12836ddfca91SDouglas Gregor break; 12846ddfca91SDouglas Gregor 1285718292f2SDouglas Gregor default: 1286718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 1287718292f2SDouglas Gregor consumeToken(); 1288718292f2SDouglas Gregor break; 1289718292f2SDouglas Gregor } 1290718292f2SDouglas Gregor } while (!Done); 1291718292f2SDouglas Gregor 1292718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1293718292f2SDouglas Gregor consumeToken(); 1294718292f2SDouglas Gregor else { 1295718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1296718292f2SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1297718292f2SDouglas Gregor HadError = true; 1298718292f2SDouglas Gregor } 1299718292f2SDouglas Gregor 130011dfe6feSDouglas Gregor // If the active module is a top-level framework, and there are no link 130111dfe6feSDouglas Gregor // libraries, automatically link against the framework. 130211dfe6feSDouglas Gregor if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() && 130311dfe6feSDouglas Gregor ActiveModule->LinkLibraries.empty()) { 130411dfe6feSDouglas Gregor inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager()); 130511dfe6feSDouglas Gregor } 130611dfe6feSDouglas Gregor 1307e7ab3669SDouglas Gregor // We're done parsing this module. Pop back to the previous module. 1308e7ab3669SDouglas Gregor ActiveModule = PreviousActiveModule; 1309718292f2SDouglas Gregor } 1310718292f2SDouglas Gregor 131197292843SDaniel Jasper /// \brief Parse an extern module declaration. 131297292843SDaniel Jasper /// 131397292843SDaniel Jasper /// extern module-declaration: 131497292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 131597292843SDaniel Jasper void ModuleMapParser::parseExternModuleDecl() { 131697292843SDaniel Jasper assert(Tok.is(MMToken::ExternKeyword)); 131797292843SDaniel Jasper consumeToken(); // 'extern' keyword 131897292843SDaniel Jasper 131997292843SDaniel Jasper // Parse 'module' keyword. 132097292843SDaniel Jasper if (!Tok.is(MMToken::ModuleKeyword)) { 132197292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 132297292843SDaniel Jasper consumeToken(); 132397292843SDaniel Jasper HadError = true; 132497292843SDaniel Jasper return; 132597292843SDaniel Jasper } 132697292843SDaniel Jasper consumeToken(); // 'module' keyword 132797292843SDaniel Jasper 132897292843SDaniel Jasper // Parse the module name. 132997292843SDaniel Jasper ModuleId Id; 133097292843SDaniel Jasper if (parseModuleId(Id)) { 133197292843SDaniel Jasper HadError = true; 133297292843SDaniel Jasper return; 133397292843SDaniel Jasper } 133497292843SDaniel Jasper 133597292843SDaniel Jasper // Parse the referenced module map file name. 133697292843SDaniel Jasper if (!Tok.is(MMToken::StringLiteral)) { 133797292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file); 133897292843SDaniel Jasper HadError = true; 133997292843SDaniel Jasper return; 134097292843SDaniel Jasper } 134197292843SDaniel Jasper std::string FileName = Tok.getString(); 134297292843SDaniel Jasper consumeToken(); // filename 134397292843SDaniel Jasper 134497292843SDaniel Jasper StringRef FileNameRef = FileName; 134597292843SDaniel Jasper SmallString<128> ModuleMapFileName; 134697292843SDaniel Jasper if (llvm::sys::path::is_relative(FileNameRef)) { 134797292843SDaniel Jasper ModuleMapFileName += Directory->getName(); 134897292843SDaniel Jasper llvm::sys::path::append(ModuleMapFileName, FileName); 134997292843SDaniel Jasper FileNameRef = ModuleMapFileName.str(); 135097292843SDaniel Jasper } 135197292843SDaniel Jasper if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef)) 135297292843SDaniel Jasper Map.parseModuleMapFile(File, /*IsSystem=*/false); 135397292843SDaniel Jasper } 135497292843SDaniel Jasper 13551fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration. 13561fb5c3a6SDouglas Gregor /// 13571fb5c3a6SDouglas Gregor /// requires-declaration: 13581fb5c3a6SDouglas Gregor /// 'requires' feature-list 13591fb5c3a6SDouglas Gregor /// 13601fb5c3a6SDouglas Gregor /// feature-list: 13611fb5c3a6SDouglas Gregor /// identifier ',' feature-list 13621fb5c3a6SDouglas Gregor /// identifier 13631fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() { 13641fb5c3a6SDouglas Gregor assert(Tok.is(MMToken::RequiresKeyword)); 13651fb5c3a6SDouglas Gregor 13661fb5c3a6SDouglas Gregor // Parse 'requires' keyword. 13671fb5c3a6SDouglas Gregor consumeToken(); 13681fb5c3a6SDouglas Gregor 13691fb5c3a6SDouglas Gregor // Parse the feature-list. 13701fb5c3a6SDouglas Gregor do { 13711fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 13721fb5c3a6SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 13731fb5c3a6SDouglas Gregor HadError = true; 13741fb5c3a6SDouglas Gregor return; 13751fb5c3a6SDouglas Gregor } 13761fb5c3a6SDouglas Gregor 13771fb5c3a6SDouglas Gregor // Consume the feature name. 13781fb5c3a6SDouglas Gregor std::string Feature = Tok.getString(); 13791fb5c3a6SDouglas Gregor consumeToken(); 13801fb5c3a6SDouglas Gregor 13811fb5c3a6SDouglas Gregor // Add this feature. 138289929282SDouglas Gregor ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target); 13831fb5c3a6SDouglas Gregor 13841fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Comma)) 13851fb5c3a6SDouglas Gregor break; 13861fb5c3a6SDouglas Gregor 13871fb5c3a6SDouglas Gregor // Consume the comma. 13881fb5c3a6SDouglas Gregor consumeToken(); 13891fb5c3a6SDouglas Gregor } while (true); 13901fb5c3a6SDouglas Gregor } 13911fb5c3a6SDouglas Gregor 1392f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the 1393f2161a70SDouglas Gregor /// subframework in which the given module lives. 1394bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod, 1395f857950dSDmitri Gribenko SmallVectorImpl<char> &Path) { 1396f2161a70SDouglas Gregor // Collect the framework names from the given module to the top-level module. 1397f857950dSDmitri Gribenko SmallVector<StringRef, 2> Paths; 1398f2161a70SDouglas Gregor for (; Mod; Mod = Mod->Parent) { 1399f2161a70SDouglas Gregor if (Mod->IsFramework) 1400f2161a70SDouglas Gregor Paths.push_back(Mod->Name); 1401f2161a70SDouglas Gregor } 1402f2161a70SDouglas Gregor 1403f2161a70SDouglas Gregor if (Paths.empty()) 1404f2161a70SDouglas Gregor return; 1405f2161a70SDouglas Gregor 1406f2161a70SDouglas Gregor // Add Frameworks/Name.framework for each subframework. 140717381a06SBenjamin Kramer for (unsigned I = Paths.size() - 1; I != 0; --I) 140817381a06SBenjamin Kramer llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework"); 1409f2161a70SDouglas Gregor } 1410f2161a70SDouglas Gregor 1411718292f2SDouglas Gregor /// \brief Parse a header declaration. 1412718292f2SDouglas Gregor /// 1413718292f2SDouglas Gregor /// header-declaration: 1414322f633cSDouglas Gregor /// 'umbrella'[opt] 'header' string-literal 141559527666SDouglas Gregor /// 'exclude'[opt] 'header' string-literal 1416b53e5483SLawrence Crowl void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken, 1417b53e5483SLawrence Crowl SourceLocation LeadingLoc) { 1418718292f2SDouglas Gregor assert(Tok.is(MMToken::HeaderKeyword)); 14191871ed3dSBenjamin Kramer consumeToken(); 1420718292f2SDouglas Gregor 1421718292f2SDouglas Gregor // Parse the header name. 1422718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1423718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1424718292f2SDouglas Gregor << "header"; 1425718292f2SDouglas Gregor HadError = true; 1426718292f2SDouglas Gregor return; 1427718292f2SDouglas Gregor } 1428e7ab3669SDouglas Gregor std::string FileName = Tok.getString(); 1429718292f2SDouglas Gregor SourceLocation FileNameLoc = consumeToken(); 1430718292f2SDouglas Gregor 1431524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1432b53e5483SLawrence Crowl if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) { 1433524e33e1SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash) 1434524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1435322f633cSDouglas Gregor HadError = true; 1436322f633cSDouglas Gregor return; 1437322f633cSDouglas Gregor } 1438322f633cSDouglas Gregor 14395257fc63SDouglas Gregor // Look for this file. 1440e7ab3669SDouglas Gregor const FileEntry *File = 0; 14413ec6663bSDouglas Gregor const FileEntry *BuiltinFile = 0; 14422c1dd271SDylan Noblesmith SmallString<128> PathName; 1443e7ab3669SDouglas Gregor if (llvm::sys::path::is_absolute(FileName)) { 1444e7ab3669SDouglas Gregor PathName = FileName; 1445e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 14467033127bSDouglas Gregor } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) { 14477033127bSDouglas Gregor PathName = Dir->getName(); 14487033127bSDouglas Gregor llvm::sys::path::append(PathName, FileName); 14497033127bSDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1450e7ab3669SDouglas Gregor } else { 1451e7ab3669SDouglas Gregor // Search for the header file within the search directory. 14527033127bSDouglas Gregor PathName = Directory->getName(); 1453e7ab3669SDouglas Gregor unsigned PathLength = PathName.size(); 1454755b2055SDouglas Gregor 1455f2161a70SDouglas Gregor if (ActiveModule->isPartOfFramework()) { 1456f2161a70SDouglas Gregor appendSubframeworkPaths(ActiveModule, PathName); 1457755b2055SDouglas Gregor 1458e7ab3669SDouglas Gregor // Check whether this file is in the public headers. 145917381a06SBenjamin Kramer llvm::sys::path::append(PathName, "Headers", FileName); 1460e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1461e7ab3669SDouglas Gregor 1462e7ab3669SDouglas Gregor if (!File) { 1463e7ab3669SDouglas Gregor // Check whether this file is in the private headers. 1464e7ab3669SDouglas Gregor PathName.resize(PathLength); 146517381a06SBenjamin Kramer llvm::sys::path::append(PathName, "PrivateHeaders", FileName); 1466e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1467e7ab3669SDouglas Gregor } 1468e7ab3669SDouglas Gregor } else { 1469e7ab3669SDouglas Gregor // Lookup for normal headers. 1470e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1471e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 14723ec6663bSDouglas Gregor 14733ec6663bSDouglas Gregor // If this is a system module with a top-level header, this header 14743ec6663bSDouglas Gregor // may have a counterpart (or replacement) in the set of headers 14753ec6663bSDouglas Gregor // supplied by Clang. Find that builtin header. 1476b53e5483SLawrence Crowl if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword && 1477b53e5483SLawrence Crowl BuiltinIncludeDir && BuiltinIncludeDir != Directory && 1478b53e5483SLawrence Crowl isBuiltinHeader(FileName)) { 14792c1dd271SDylan Noblesmith SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); 14803ec6663bSDouglas Gregor llvm::sys::path::append(BuiltinPathName, FileName); 14813ec6663bSDouglas Gregor BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); 14823ec6663bSDouglas Gregor 14833ec6663bSDouglas Gregor // If Clang supplies this header but the underlying system does not, 14843ec6663bSDouglas Gregor // just silently swap in our builtin version. Otherwise, we'll end 14853ec6663bSDouglas Gregor // up adding both (later). 14863ec6663bSDouglas Gregor if (!File && BuiltinFile) { 14873ec6663bSDouglas Gregor File = BuiltinFile; 14883ec6663bSDouglas Gregor BuiltinFile = 0; 14893ec6663bSDouglas Gregor } 14903ec6663bSDouglas Gregor } 1491e7ab3669SDouglas Gregor } 1492e7ab3669SDouglas Gregor } 14935257fc63SDouglas Gregor 14945257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 14955257fc63SDouglas Gregor // Come up with a lazy way to do this. 1496e7ab3669SDouglas Gregor if (File) { 149759527666SDouglas Gregor if (ModuleMap::KnownHeader OwningModule = Map.Headers[File]) { 14985257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_conflict) 149959527666SDouglas Gregor << FileName << OwningModule.getModule()->getFullModuleName(); 15005257fc63SDouglas Gregor HadError = true; 1501b53e5483SLawrence Crowl } else if (LeadingToken == MMToken::UmbrellaKeyword) { 1502322f633cSDouglas Gregor const DirectoryEntry *UmbrellaDir = File->getDir(); 150359527666SDouglas Gregor if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { 1504b53e5483SLawrence Crowl Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash) 150559527666SDouglas Gregor << UmbrellaModule->getFullModuleName(); 1506322f633cSDouglas Gregor HadError = true; 15075257fc63SDouglas Gregor } else { 1508322f633cSDouglas Gregor // Record this umbrella header. 1509322f633cSDouglas Gregor Map.setUmbrellaHeader(ActiveModule, File); 1510322f633cSDouglas Gregor } 1511322f633cSDouglas Gregor } else { 1512322f633cSDouglas Gregor // Record this header. 1513b53e5483SLawrence Crowl ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader; 1514b53e5483SLawrence Crowl if (LeadingToken == MMToken::ExcludeKeyword) 1515b53e5483SLawrence Crowl Role = ModuleMap::ExcludedHeader; 1516b53e5483SLawrence Crowl else if (LeadingToken == MMToken::PrivateKeyword) 1517b53e5483SLawrence Crowl Role = ModuleMap::PrivateHeader; 1518b53e5483SLawrence Crowl else 1519b53e5483SLawrence Crowl assert(LeadingToken == MMToken::HeaderKeyword); 1520b53e5483SLawrence Crowl 1521b53e5483SLawrence Crowl Map.addHeader(ActiveModule, File, Role); 15223ec6663bSDouglas Gregor 15233ec6663bSDouglas Gregor // If there is a builtin counterpart to this file, add it now. 15243ec6663bSDouglas Gregor if (BuiltinFile) 1525b53e5483SLawrence Crowl Map.addHeader(ActiveModule, BuiltinFile, Role); 15265257fc63SDouglas Gregor } 1527b53e5483SLawrence Crowl } else if (LeadingToken != MMToken::ExcludeKeyword) { 15284b27a64bSDouglas Gregor // Ignore excluded header files. They're optional anyway. 15294b27a64bSDouglas Gregor 15305257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_not_found) 1531b53e5483SLawrence Crowl << (LeadingToken == MMToken::UmbrellaKeyword) << FileName; 15325257fc63SDouglas Gregor HadError = true; 15335257fc63SDouglas Gregor } 1534718292f2SDouglas Gregor } 1535718292f2SDouglas Gregor 1536524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration. 1537524e33e1SDouglas Gregor /// 1538524e33e1SDouglas Gregor /// umbrella-dir-declaration: 1539524e33e1SDouglas Gregor /// umbrella string-literal 1540524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1541524e33e1SDouglas Gregor // Parse the directory name. 1542524e33e1SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1543524e33e1SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1544524e33e1SDouglas Gregor << "umbrella"; 1545524e33e1SDouglas Gregor HadError = true; 1546524e33e1SDouglas Gregor return; 1547524e33e1SDouglas Gregor } 1548524e33e1SDouglas Gregor 1549524e33e1SDouglas Gregor std::string DirName = Tok.getString(); 1550524e33e1SDouglas Gregor SourceLocation DirNameLoc = consumeToken(); 1551524e33e1SDouglas Gregor 1552524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1553524e33e1SDouglas Gregor if (ActiveModule->Umbrella) { 1554524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1555524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1556524e33e1SDouglas Gregor HadError = true; 1557524e33e1SDouglas Gregor return; 1558524e33e1SDouglas Gregor } 1559524e33e1SDouglas Gregor 1560524e33e1SDouglas Gregor // Look for this file. 1561524e33e1SDouglas Gregor const DirectoryEntry *Dir = 0; 1562524e33e1SDouglas Gregor if (llvm::sys::path::is_absolute(DirName)) 1563524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(DirName); 1564524e33e1SDouglas Gregor else { 15652c1dd271SDylan Noblesmith SmallString<128> PathName; 1566524e33e1SDouglas Gregor PathName = Directory->getName(); 1567524e33e1SDouglas Gregor llvm::sys::path::append(PathName, DirName); 1568524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(PathName); 1569524e33e1SDouglas Gregor } 1570524e33e1SDouglas Gregor 1571524e33e1SDouglas Gregor if (!Dir) { 1572524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1573524e33e1SDouglas Gregor << DirName; 1574524e33e1SDouglas Gregor HadError = true; 1575524e33e1SDouglas Gregor return; 1576524e33e1SDouglas Gregor } 1577524e33e1SDouglas Gregor 1578524e33e1SDouglas Gregor if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1579524e33e1SDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1580524e33e1SDouglas Gregor << OwningModule->getFullModuleName(); 1581524e33e1SDouglas Gregor HadError = true; 1582524e33e1SDouglas Gregor return; 1583524e33e1SDouglas Gregor } 1584524e33e1SDouglas Gregor 1585524e33e1SDouglas Gregor // Record this umbrella directory. 1586524e33e1SDouglas Gregor Map.setUmbrellaDir(ActiveModule, Dir); 1587524e33e1SDouglas Gregor } 1588524e33e1SDouglas Gregor 15892b82c2a5SDouglas Gregor /// \brief Parse a module export declaration. 15902b82c2a5SDouglas Gregor /// 15912b82c2a5SDouglas Gregor /// export-declaration: 15922b82c2a5SDouglas Gregor /// 'export' wildcard-module-id 15932b82c2a5SDouglas Gregor /// 15942b82c2a5SDouglas Gregor /// wildcard-module-id: 15952b82c2a5SDouglas Gregor /// identifier 15962b82c2a5SDouglas Gregor /// '*' 15972b82c2a5SDouglas Gregor /// identifier '.' wildcard-module-id 15982b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() { 15992b82c2a5SDouglas Gregor assert(Tok.is(MMToken::ExportKeyword)); 16002b82c2a5SDouglas Gregor SourceLocation ExportLoc = consumeToken(); 16012b82c2a5SDouglas Gregor 16022b82c2a5SDouglas Gregor // Parse the module-id with an optional wildcard at the end. 16032b82c2a5SDouglas Gregor ModuleId ParsedModuleId; 16042b82c2a5SDouglas Gregor bool Wildcard = false; 16052b82c2a5SDouglas Gregor do { 16062b82c2a5SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 16072b82c2a5SDouglas Gregor ParsedModuleId.push_back(std::make_pair(Tok.getString(), 16082b82c2a5SDouglas Gregor Tok.getLocation())); 16092b82c2a5SDouglas Gregor consumeToken(); 16102b82c2a5SDouglas Gregor 16112b82c2a5SDouglas Gregor if (Tok.is(MMToken::Period)) { 16122b82c2a5SDouglas Gregor consumeToken(); 16132b82c2a5SDouglas Gregor continue; 16142b82c2a5SDouglas Gregor } 16152b82c2a5SDouglas Gregor 16162b82c2a5SDouglas Gregor break; 16172b82c2a5SDouglas Gregor } 16182b82c2a5SDouglas Gregor 16192b82c2a5SDouglas Gregor if(Tok.is(MMToken::Star)) { 16202b82c2a5SDouglas Gregor Wildcard = true; 1621f5eedd05SDouglas Gregor consumeToken(); 16222b82c2a5SDouglas Gregor break; 16232b82c2a5SDouglas Gregor } 16242b82c2a5SDouglas Gregor 1625*ba7f2f71SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); 16262b82c2a5SDouglas Gregor HadError = true; 16272b82c2a5SDouglas Gregor return; 16282b82c2a5SDouglas Gregor } while (true); 16292b82c2a5SDouglas Gregor 16302b82c2a5SDouglas Gregor Module::UnresolvedExportDecl Unresolved = { 16312b82c2a5SDouglas Gregor ExportLoc, ParsedModuleId, Wildcard 16322b82c2a5SDouglas Gregor }; 16332b82c2a5SDouglas Gregor ActiveModule->UnresolvedExports.push_back(Unresolved); 16342b82c2a5SDouglas Gregor } 16352b82c2a5SDouglas Gregor 1636*ba7f2f71SDaniel Jasper /// \brief Parse a module uses declaration. 1637*ba7f2f71SDaniel Jasper /// 1638*ba7f2f71SDaniel Jasper /// uses-declaration: 1639*ba7f2f71SDaniel Jasper /// 'uses' wildcard-module-id 1640*ba7f2f71SDaniel Jasper void ModuleMapParser::parseUseDecl() { 1641*ba7f2f71SDaniel Jasper assert(Tok.is(MMToken::UseKeyword)); 1642*ba7f2f71SDaniel Jasper consumeToken(); 1643*ba7f2f71SDaniel Jasper // Parse the module-id. 1644*ba7f2f71SDaniel Jasper ModuleId ParsedModuleId; 1645*ba7f2f71SDaniel Jasper 1646*ba7f2f71SDaniel Jasper do { 1647*ba7f2f71SDaniel Jasper if (Tok.is(MMToken::Identifier)) { 1648*ba7f2f71SDaniel Jasper ParsedModuleId.push_back( 1649*ba7f2f71SDaniel Jasper std::make_pair(Tok.getString(), Tok.getLocation())); 1650*ba7f2f71SDaniel Jasper consumeToken(); 1651*ba7f2f71SDaniel Jasper 1652*ba7f2f71SDaniel Jasper if (Tok.is(MMToken::Period)) { 1653*ba7f2f71SDaniel Jasper consumeToken(); 1654*ba7f2f71SDaniel Jasper continue; 1655*ba7f2f71SDaniel Jasper } 1656*ba7f2f71SDaniel Jasper 1657*ba7f2f71SDaniel Jasper break; 1658*ba7f2f71SDaniel Jasper } 1659*ba7f2f71SDaniel Jasper 1660*ba7f2f71SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); 1661*ba7f2f71SDaniel Jasper HadError = true; 1662*ba7f2f71SDaniel Jasper return; 1663*ba7f2f71SDaniel Jasper } while (true); 1664*ba7f2f71SDaniel Jasper 1665*ba7f2f71SDaniel Jasper ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId); 1666*ba7f2f71SDaniel Jasper } 1667*ba7f2f71SDaniel Jasper 16686ddfca91SDouglas Gregor /// \brief Parse a link declaration. 16696ddfca91SDouglas Gregor /// 16706ddfca91SDouglas Gregor /// module-declaration: 16716ddfca91SDouglas Gregor /// 'link' 'framework'[opt] string-literal 16726ddfca91SDouglas Gregor void ModuleMapParser::parseLinkDecl() { 16736ddfca91SDouglas Gregor assert(Tok.is(MMToken::LinkKeyword)); 16746ddfca91SDouglas Gregor SourceLocation LinkLoc = consumeToken(); 16756ddfca91SDouglas Gregor 16766ddfca91SDouglas Gregor // Parse the optional 'framework' keyword. 16776ddfca91SDouglas Gregor bool IsFramework = false; 16786ddfca91SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 16796ddfca91SDouglas Gregor consumeToken(); 16806ddfca91SDouglas Gregor IsFramework = true; 16816ddfca91SDouglas Gregor } 16826ddfca91SDouglas Gregor 16836ddfca91SDouglas Gregor // Parse the library name 16846ddfca91SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 16856ddfca91SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) 16866ddfca91SDouglas Gregor << IsFramework << SourceRange(LinkLoc); 16876ddfca91SDouglas Gregor HadError = true; 16886ddfca91SDouglas Gregor return; 16896ddfca91SDouglas Gregor } 16906ddfca91SDouglas Gregor 16916ddfca91SDouglas Gregor std::string LibraryName = Tok.getString(); 16926ddfca91SDouglas Gregor consumeToken(); 16936ddfca91SDouglas Gregor ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, 16946ddfca91SDouglas Gregor IsFramework)); 16956ddfca91SDouglas Gregor } 16966ddfca91SDouglas Gregor 169735b13eceSDouglas Gregor /// \brief Parse a configuration macro declaration. 169835b13eceSDouglas Gregor /// 169935b13eceSDouglas Gregor /// module-declaration: 170035b13eceSDouglas Gregor /// 'config_macros' attributes[opt] config-macro-list? 170135b13eceSDouglas Gregor /// 170235b13eceSDouglas Gregor /// config-macro-list: 170335b13eceSDouglas Gregor /// identifier (',' identifier)? 170435b13eceSDouglas Gregor void ModuleMapParser::parseConfigMacros() { 170535b13eceSDouglas Gregor assert(Tok.is(MMToken::ConfigMacros)); 170635b13eceSDouglas Gregor SourceLocation ConfigMacrosLoc = consumeToken(); 170735b13eceSDouglas Gregor 170835b13eceSDouglas Gregor // Only top-level modules can have configuration macros. 170935b13eceSDouglas Gregor if (ActiveModule->Parent) { 171035b13eceSDouglas Gregor Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule); 171135b13eceSDouglas Gregor } 171235b13eceSDouglas Gregor 171335b13eceSDouglas Gregor // Parse the optional attributes. 171435b13eceSDouglas Gregor Attributes Attrs; 171535b13eceSDouglas Gregor parseOptionalAttributes(Attrs); 171635b13eceSDouglas Gregor if (Attrs.IsExhaustive && !ActiveModule->Parent) { 171735b13eceSDouglas Gregor ActiveModule->ConfigMacrosExhaustive = true; 171835b13eceSDouglas Gregor } 171935b13eceSDouglas Gregor 172035b13eceSDouglas Gregor // If we don't have an identifier, we're done. 172135b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) 172235b13eceSDouglas Gregor return; 172335b13eceSDouglas Gregor 172435b13eceSDouglas Gregor // Consume the first identifier. 172535b13eceSDouglas Gregor if (!ActiveModule->Parent) { 172635b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 172735b13eceSDouglas Gregor } 172835b13eceSDouglas Gregor consumeToken(); 172935b13eceSDouglas Gregor 173035b13eceSDouglas Gregor do { 173135b13eceSDouglas Gregor // If there's a comma, consume it. 173235b13eceSDouglas Gregor if (!Tok.is(MMToken::Comma)) 173335b13eceSDouglas Gregor break; 173435b13eceSDouglas Gregor consumeToken(); 173535b13eceSDouglas Gregor 173635b13eceSDouglas Gregor // We expect to see a macro name here. 173735b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 173835b13eceSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro); 173935b13eceSDouglas Gregor break; 174035b13eceSDouglas Gregor } 174135b13eceSDouglas Gregor 174235b13eceSDouglas Gregor // Consume the macro name. 174335b13eceSDouglas Gregor if (!ActiveModule->Parent) { 174435b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 174535b13eceSDouglas Gregor } 174635b13eceSDouglas Gregor consumeToken(); 174735b13eceSDouglas Gregor } while (true); 174835b13eceSDouglas Gregor } 174935b13eceSDouglas Gregor 1750fb912657SDouglas Gregor /// \brief Format a module-id into a string. 1751fb912657SDouglas Gregor static std::string formatModuleId(const ModuleId &Id) { 1752fb912657SDouglas Gregor std::string result; 1753fb912657SDouglas Gregor { 1754fb912657SDouglas Gregor llvm::raw_string_ostream OS(result); 1755fb912657SDouglas Gregor 1756fb912657SDouglas Gregor for (unsigned I = 0, N = Id.size(); I != N; ++I) { 1757fb912657SDouglas Gregor if (I) 1758fb912657SDouglas Gregor OS << "."; 1759fb912657SDouglas Gregor OS << Id[I].first; 1760fb912657SDouglas Gregor } 1761fb912657SDouglas Gregor } 1762fb912657SDouglas Gregor 1763fb912657SDouglas Gregor return result; 1764fb912657SDouglas Gregor } 1765fb912657SDouglas Gregor 1766fb912657SDouglas Gregor /// \brief Parse a conflict declaration. 1767fb912657SDouglas Gregor /// 1768fb912657SDouglas Gregor /// module-declaration: 1769fb912657SDouglas Gregor /// 'conflict' module-id ',' string-literal 1770fb912657SDouglas Gregor void ModuleMapParser::parseConflict() { 1771fb912657SDouglas Gregor assert(Tok.is(MMToken::Conflict)); 1772fb912657SDouglas Gregor SourceLocation ConflictLoc = consumeToken(); 1773fb912657SDouglas Gregor Module::UnresolvedConflict Conflict; 1774fb912657SDouglas Gregor 1775fb912657SDouglas Gregor // Parse the module-id. 1776fb912657SDouglas Gregor if (parseModuleId(Conflict.Id)) 1777fb912657SDouglas Gregor return; 1778fb912657SDouglas Gregor 1779fb912657SDouglas Gregor // Parse the ','. 1780fb912657SDouglas Gregor if (!Tok.is(MMToken::Comma)) { 1781fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma) 1782fb912657SDouglas Gregor << SourceRange(ConflictLoc); 1783fb912657SDouglas Gregor return; 1784fb912657SDouglas Gregor } 1785fb912657SDouglas Gregor consumeToken(); 1786fb912657SDouglas Gregor 1787fb912657SDouglas Gregor // Parse the message. 1788fb912657SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1789fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message) 1790fb912657SDouglas Gregor << formatModuleId(Conflict.Id); 1791fb912657SDouglas Gregor return; 1792fb912657SDouglas Gregor } 1793fb912657SDouglas Gregor Conflict.Message = Tok.getString().str(); 1794fb912657SDouglas Gregor consumeToken(); 1795fb912657SDouglas Gregor 1796fb912657SDouglas Gregor // Add this unresolved conflict. 1797fb912657SDouglas Gregor ActiveModule->UnresolvedConflicts.push_back(Conflict); 1798fb912657SDouglas Gregor } 1799fb912657SDouglas Gregor 18006ddfca91SDouglas Gregor /// \brief Parse an inferred module declaration (wildcard modules). 18019194a91dSDouglas Gregor /// 18029194a91dSDouglas Gregor /// module-declaration: 18039194a91dSDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] 18049194a91dSDouglas Gregor /// { inferred-module-member* } 18059194a91dSDouglas Gregor /// 18069194a91dSDouglas Gregor /// inferred-module-member: 18079194a91dSDouglas Gregor /// 'export' '*' 18089194a91dSDouglas Gregor /// 'exclude' identifier 18099194a91dSDouglas Gregor void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { 181073441091SDouglas Gregor assert(Tok.is(MMToken::Star)); 181173441091SDouglas Gregor SourceLocation StarLoc = consumeToken(); 181273441091SDouglas Gregor bool Failed = false; 181373441091SDouglas Gregor 181473441091SDouglas Gregor // Inferred modules must be submodules. 18159194a91dSDouglas Gregor if (!ActiveModule && !Framework) { 181673441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 181773441091SDouglas Gregor Failed = true; 181873441091SDouglas Gregor } 181973441091SDouglas Gregor 18209194a91dSDouglas Gregor if (ActiveModule) { 1821524e33e1SDouglas Gregor // Inferred modules must have umbrella directories. 1822524e33e1SDouglas Gregor if (!Failed && !ActiveModule->getUmbrellaDir()) { 182373441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 182473441091SDouglas Gregor Failed = true; 182573441091SDouglas Gregor } 182673441091SDouglas Gregor 182773441091SDouglas Gregor // Check for redefinition of an inferred module. 1828dd005f69SDouglas Gregor if (!Failed && ActiveModule->InferSubmodules) { 182973441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 1830dd005f69SDouglas Gregor if (ActiveModule->InferredSubmoduleLoc.isValid()) 1831dd005f69SDouglas Gregor Diags.Report(ActiveModule->InferredSubmoduleLoc, 183273441091SDouglas Gregor diag::note_mmap_prev_definition); 183373441091SDouglas Gregor Failed = true; 183473441091SDouglas Gregor } 183573441091SDouglas Gregor 18369194a91dSDouglas Gregor // Check for the 'framework' keyword, which is not permitted here. 18379194a91dSDouglas Gregor if (Framework) { 18389194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); 18399194a91dSDouglas Gregor Framework = false; 18409194a91dSDouglas Gregor } 18419194a91dSDouglas Gregor } else if (Explicit) { 18429194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); 18439194a91dSDouglas Gregor Explicit = false; 18449194a91dSDouglas Gregor } 18459194a91dSDouglas Gregor 184673441091SDouglas Gregor // If there were any problems with this inferred submodule, skip its body. 184773441091SDouglas Gregor if (Failed) { 184873441091SDouglas Gregor if (Tok.is(MMToken::LBrace)) { 184973441091SDouglas Gregor consumeToken(); 185073441091SDouglas Gregor skipUntil(MMToken::RBrace); 185173441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 185273441091SDouglas Gregor consumeToken(); 185373441091SDouglas Gregor } 185473441091SDouglas Gregor HadError = true; 185573441091SDouglas Gregor return; 185673441091SDouglas Gregor } 185773441091SDouglas Gregor 18589194a91dSDouglas Gregor // Parse optional attributes. 18594442605fSBill Wendling Attributes Attrs; 18609194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 18619194a91dSDouglas Gregor 18629194a91dSDouglas Gregor if (ActiveModule) { 186373441091SDouglas Gregor // Note that we have an inferred submodule. 1864dd005f69SDouglas Gregor ActiveModule->InferSubmodules = true; 1865dd005f69SDouglas Gregor ActiveModule->InferredSubmoduleLoc = StarLoc; 1866dd005f69SDouglas Gregor ActiveModule->InferExplicitSubmodules = Explicit; 18679194a91dSDouglas Gregor } else { 18689194a91dSDouglas Gregor // We'll be inferring framework modules for this directory. 18699194a91dSDouglas Gregor Map.InferredDirectories[Directory].InferModules = true; 18709194a91dSDouglas Gregor Map.InferredDirectories[Directory].InferSystemModules = Attrs.IsSystem; 18719194a91dSDouglas Gregor } 187273441091SDouglas Gregor 187373441091SDouglas Gregor // Parse the opening brace. 187473441091SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 187573441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 187673441091SDouglas Gregor HadError = true; 187773441091SDouglas Gregor return; 187873441091SDouglas Gregor } 187973441091SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 188073441091SDouglas Gregor 188173441091SDouglas Gregor // Parse the body of the inferred submodule. 188273441091SDouglas Gregor bool Done = false; 188373441091SDouglas Gregor do { 188473441091SDouglas Gregor switch (Tok.Kind) { 188573441091SDouglas Gregor case MMToken::EndOfFile: 188673441091SDouglas Gregor case MMToken::RBrace: 188773441091SDouglas Gregor Done = true; 188873441091SDouglas Gregor break; 188973441091SDouglas Gregor 18909194a91dSDouglas Gregor case MMToken::ExcludeKeyword: { 18919194a91dSDouglas Gregor if (ActiveModule) { 18929194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 1893162405daSDouglas Gregor << (ActiveModule != 0); 18949194a91dSDouglas Gregor consumeToken(); 18959194a91dSDouglas Gregor break; 18969194a91dSDouglas Gregor } 18979194a91dSDouglas Gregor 18989194a91dSDouglas Gregor consumeToken(); 18999194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 19009194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); 19019194a91dSDouglas Gregor break; 19029194a91dSDouglas Gregor } 19039194a91dSDouglas Gregor 19049194a91dSDouglas Gregor Map.InferredDirectories[Directory].ExcludedModules 19059194a91dSDouglas Gregor .push_back(Tok.getString()); 19069194a91dSDouglas Gregor consumeToken(); 19079194a91dSDouglas Gregor break; 19089194a91dSDouglas Gregor } 19099194a91dSDouglas Gregor 19109194a91dSDouglas Gregor case MMToken::ExportKeyword: 19119194a91dSDouglas Gregor if (!ActiveModule) { 19129194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 1913162405daSDouglas Gregor << (ActiveModule != 0); 19149194a91dSDouglas Gregor consumeToken(); 19159194a91dSDouglas Gregor break; 19169194a91dSDouglas Gregor } 19179194a91dSDouglas Gregor 191873441091SDouglas Gregor consumeToken(); 191973441091SDouglas Gregor if (Tok.is(MMToken::Star)) 1920dd005f69SDouglas Gregor ActiveModule->InferExportWildcard = true; 192173441091SDouglas Gregor else 192273441091SDouglas Gregor Diags.Report(Tok.getLocation(), 192373441091SDouglas Gregor diag::err_mmap_expected_export_wildcard); 192473441091SDouglas Gregor consumeToken(); 192573441091SDouglas Gregor break; 192673441091SDouglas Gregor 192773441091SDouglas Gregor case MMToken::ExplicitKeyword: 192873441091SDouglas Gregor case MMToken::ModuleKeyword: 192973441091SDouglas Gregor case MMToken::HeaderKeyword: 1930b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 193173441091SDouglas Gregor case MMToken::UmbrellaKeyword: 193273441091SDouglas Gregor default: 19339194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 1934162405daSDouglas Gregor << (ActiveModule != 0); 193573441091SDouglas Gregor consumeToken(); 193673441091SDouglas Gregor break; 193773441091SDouglas Gregor } 193873441091SDouglas Gregor } while (!Done); 193973441091SDouglas Gregor 194073441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 194173441091SDouglas Gregor consumeToken(); 194273441091SDouglas Gregor else { 194373441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 194473441091SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 194573441091SDouglas Gregor HadError = true; 194673441091SDouglas Gregor } 194773441091SDouglas Gregor } 194873441091SDouglas Gregor 19499194a91dSDouglas Gregor /// \brief Parse optional attributes. 19509194a91dSDouglas Gregor /// 19519194a91dSDouglas Gregor /// attributes: 19529194a91dSDouglas Gregor /// attribute attributes 19539194a91dSDouglas Gregor /// attribute 19549194a91dSDouglas Gregor /// 19559194a91dSDouglas Gregor /// attribute: 19569194a91dSDouglas Gregor /// [ identifier ] 19579194a91dSDouglas Gregor /// 19589194a91dSDouglas Gregor /// \param Attrs Will be filled in with the parsed attributes. 19599194a91dSDouglas Gregor /// 19609194a91dSDouglas Gregor /// \returns true if an error occurred, false otherwise. 19614442605fSBill Wendling bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { 19629194a91dSDouglas Gregor bool HadError = false; 19639194a91dSDouglas Gregor 19649194a91dSDouglas Gregor while (Tok.is(MMToken::LSquare)) { 19659194a91dSDouglas Gregor // Consume the '['. 19669194a91dSDouglas Gregor SourceLocation LSquareLoc = consumeToken(); 19679194a91dSDouglas Gregor 19689194a91dSDouglas Gregor // Check whether we have an attribute name here. 19699194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 19709194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 19719194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 19729194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 19739194a91dSDouglas Gregor consumeToken(); 19749194a91dSDouglas Gregor HadError = true; 19759194a91dSDouglas Gregor } 19769194a91dSDouglas Gregor 19779194a91dSDouglas Gregor // Decode the attribute name. 19789194a91dSDouglas Gregor AttributeKind Attribute 19799194a91dSDouglas Gregor = llvm::StringSwitch<AttributeKind>(Tok.getString()) 198035b13eceSDouglas Gregor .Case("exhaustive", AT_exhaustive) 19819194a91dSDouglas Gregor .Case("system", AT_system) 19829194a91dSDouglas Gregor .Default(AT_unknown); 19839194a91dSDouglas Gregor switch (Attribute) { 19849194a91dSDouglas Gregor case AT_unknown: 19859194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 19869194a91dSDouglas Gregor << Tok.getString(); 19879194a91dSDouglas Gregor break; 19889194a91dSDouglas Gregor 19899194a91dSDouglas Gregor case AT_system: 19909194a91dSDouglas Gregor Attrs.IsSystem = true; 19919194a91dSDouglas Gregor break; 199235b13eceSDouglas Gregor 199335b13eceSDouglas Gregor case AT_exhaustive: 199435b13eceSDouglas Gregor Attrs.IsExhaustive = true; 199535b13eceSDouglas Gregor break; 19969194a91dSDouglas Gregor } 19979194a91dSDouglas Gregor consumeToken(); 19989194a91dSDouglas Gregor 19999194a91dSDouglas Gregor // Consume the ']'. 20009194a91dSDouglas Gregor if (!Tok.is(MMToken::RSquare)) { 20019194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 20029194a91dSDouglas Gregor Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 20039194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 20049194a91dSDouglas Gregor HadError = true; 20059194a91dSDouglas Gregor } 20069194a91dSDouglas Gregor 20079194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 20089194a91dSDouglas Gregor consumeToken(); 20099194a91dSDouglas Gregor } 20109194a91dSDouglas Gregor 20119194a91dSDouglas Gregor return HadError; 20129194a91dSDouglas Gregor } 20139194a91dSDouglas Gregor 20147033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence 20157033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null. 20167033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() { 20177033127bSDouglas Gregor for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) { 20187033127bSDouglas Gregor // If we have an umbrella directory, use that. 20197033127bSDouglas Gregor if (Mod->hasUmbrellaDir()) 20207033127bSDouglas Gregor return Mod->getUmbrellaDir(); 20217033127bSDouglas Gregor 20227033127bSDouglas Gregor // If we have a framework directory, stop looking. 20237033127bSDouglas Gregor if (Mod->IsFramework) 20247033127bSDouglas Gregor return 0; 20257033127bSDouglas Gregor } 20267033127bSDouglas Gregor 20277033127bSDouglas Gregor return 0; 20287033127bSDouglas Gregor } 20297033127bSDouglas Gregor 2030718292f2SDouglas Gregor /// \brief Parse a module map file. 2031718292f2SDouglas Gregor /// 2032718292f2SDouglas Gregor /// module-map-file: 2033718292f2SDouglas Gregor /// module-declaration* 2034718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() { 2035718292f2SDouglas Gregor do { 2036718292f2SDouglas Gregor switch (Tok.Kind) { 2037718292f2SDouglas Gregor case MMToken::EndOfFile: 2038718292f2SDouglas Gregor return HadError; 2039718292f2SDouglas Gregor 2040e7ab3669SDouglas Gregor case MMToken::ExplicitKeyword: 204197292843SDaniel Jasper case MMToken::ExternKeyword: 2042718292f2SDouglas Gregor case MMToken::ModuleKeyword: 2043755b2055SDouglas Gregor case MMToken::FrameworkKeyword: 2044718292f2SDouglas Gregor parseModuleDecl(); 2045718292f2SDouglas Gregor break; 2046718292f2SDouglas Gregor 20471fb5c3a6SDouglas Gregor case MMToken::Comma: 204835b13eceSDouglas Gregor case MMToken::ConfigMacros: 2049fb912657SDouglas Gregor case MMToken::Conflict: 205059527666SDouglas Gregor case MMToken::ExcludeKeyword: 20512b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 2052718292f2SDouglas Gregor case MMToken::HeaderKeyword: 2053718292f2SDouglas Gregor case MMToken::Identifier: 2054718292f2SDouglas Gregor case MMToken::LBrace: 20556ddfca91SDouglas Gregor case MMToken::LinkKeyword: 2056a686e1b0SDouglas Gregor case MMToken::LSquare: 20572b82c2a5SDouglas Gregor case MMToken::Period: 2058b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 2059718292f2SDouglas Gregor case MMToken::RBrace: 2060a686e1b0SDouglas Gregor case MMToken::RSquare: 20611fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 20622b82c2a5SDouglas Gregor case MMToken::Star: 2063718292f2SDouglas Gregor case MMToken::StringLiteral: 2064718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 2065*ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 2066718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 2067718292f2SDouglas Gregor HadError = true; 2068718292f2SDouglas Gregor consumeToken(); 2069718292f2SDouglas Gregor break; 2070718292f2SDouglas Gregor } 2071718292f2SDouglas Gregor } while (true); 2072718292f2SDouglas Gregor } 2073718292f2SDouglas Gregor 2074963c5535SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem) { 20754ddf2221SDouglas Gregor llvm::DenseMap<const FileEntry *, bool>::iterator Known 20764ddf2221SDouglas Gregor = ParsedModuleMap.find(File); 20774ddf2221SDouglas Gregor if (Known != ParsedModuleMap.end()) 20784ddf2221SDouglas Gregor return Known->second; 20794ddf2221SDouglas Gregor 208089929282SDouglas Gregor assert(Target != 0 && "Missing target information"); 2081718292f2SDouglas Gregor FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User); 2082718292f2SDouglas Gregor const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID); 2083718292f2SDouglas Gregor if (!Buffer) 20844ddf2221SDouglas Gregor return ParsedModuleMap[File] = true; 2085718292f2SDouglas Gregor 2086718292f2SDouglas Gregor // Parse this module map file. 20871fb5c3a6SDouglas Gregor Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts); 20881fb5c3a6SDouglas Gregor Diags->getClient()->BeginSourceFile(MMapLangOpts); 2089bc10b9fbSDouglas Gregor ModuleMapParser Parser(L, *SourceMgr, Target, *Diags, *this, File->getDir(), 2090963c5535SDouglas Gregor BuiltinIncludeDir, IsSystem); 2091718292f2SDouglas Gregor bool Result = Parser.parseModuleMapFile(); 2092718292f2SDouglas Gregor Diags->getClient()->EndSourceFile(); 20934ddf2221SDouglas Gregor ParsedModuleMap[File] = Result; 2094718292f2SDouglas Gregor return Result; 2095718292f2SDouglas Gregor } 2096