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" 15718292f2SDouglas Gregor #include "clang/Lex/Lexer.h" 16718292f2SDouglas Gregor #include "clang/Lex/LiteralSupport.h" 17718292f2SDouglas Gregor #include "clang/Lex/LexDiagnostic.h" 18718292f2SDouglas Gregor #include "clang/Basic/Diagnostic.h" 19*811db4eaSDouglas Gregor #include "clang/Basic/DiagnosticOptions.h" 20718292f2SDouglas Gregor #include "clang/Basic/FileManager.h" 21718292f2SDouglas Gregor #include "clang/Basic/TargetInfo.h" 22718292f2SDouglas Gregor #include "clang/Basic/TargetOptions.h" 23718292f2SDouglas Gregor #include "llvm/Support/Allocator.h" 24e89dbc1dSDouglas Gregor #include "llvm/Support/FileSystem.h" 25718292f2SDouglas Gregor #include "llvm/Support/Host.h" 265257fc63SDouglas Gregor #include "llvm/Support/PathV2.h" 27718292f2SDouglas Gregor #include "llvm/Support/raw_ostream.h" 28718292f2SDouglas Gregor #include "llvm/ADT/StringRef.h" 29718292f2SDouglas Gregor #include "llvm/ADT/StringSwitch.h" 3007c22b78SDouglas Gregor #include <stdlib.h> 31718292f2SDouglas Gregor using namespace clang; 32718292f2SDouglas Gregor 332b82c2a5SDouglas Gregor Module::ExportDecl 342b82c2a5SDouglas Gregor ModuleMap::resolveExport(Module *Mod, 352b82c2a5SDouglas Gregor const Module::UnresolvedExportDecl &Unresolved, 362b82c2a5SDouglas Gregor bool Complain) { 37f5eedd05SDouglas Gregor // We may have just a wildcard. 38f5eedd05SDouglas Gregor if (Unresolved.Id.empty()) { 39f5eedd05SDouglas Gregor assert(Unresolved.Wildcard && "Invalid unresolved export"); 40f5eedd05SDouglas Gregor return Module::ExportDecl(0, true); 41f5eedd05SDouglas Gregor } 42f5eedd05SDouglas Gregor 432b82c2a5SDouglas Gregor // Find the starting module. 442b82c2a5SDouglas Gregor Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod); 452b82c2a5SDouglas Gregor if (!Context) { 462b82c2a5SDouglas Gregor if (Complain) 472b82c2a5SDouglas Gregor Diags->Report(Unresolved.Id[0].second, 482b82c2a5SDouglas Gregor diag::err_mmap_missing_module_unqualified) 492b82c2a5SDouglas Gregor << Unresolved.Id[0].first << Mod->getFullModuleName(); 502b82c2a5SDouglas Gregor 512b82c2a5SDouglas Gregor return Module::ExportDecl(); 522b82c2a5SDouglas Gregor } 532b82c2a5SDouglas Gregor 542b82c2a5SDouglas Gregor // Dig into the module path. 552b82c2a5SDouglas Gregor for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) { 562b82c2a5SDouglas Gregor Module *Sub = lookupModuleQualified(Unresolved.Id[I].first, 572b82c2a5SDouglas Gregor Context); 582b82c2a5SDouglas Gregor if (!Sub) { 592b82c2a5SDouglas Gregor if (Complain) 602b82c2a5SDouglas Gregor Diags->Report(Unresolved.Id[I].second, 612b82c2a5SDouglas Gregor diag::err_mmap_missing_module_qualified) 622b82c2a5SDouglas Gregor << Unresolved.Id[I].first << Context->getFullModuleName() 632b82c2a5SDouglas Gregor << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second); 642b82c2a5SDouglas Gregor 652b82c2a5SDouglas Gregor return Module::ExportDecl(); 662b82c2a5SDouglas Gregor } 672b82c2a5SDouglas Gregor 682b82c2a5SDouglas Gregor Context = Sub; 692b82c2a5SDouglas Gregor } 702b82c2a5SDouglas Gregor 712b82c2a5SDouglas Gregor return Module::ExportDecl(Context, Unresolved.Wildcard); 722b82c2a5SDouglas Gregor } 732b82c2a5SDouglas Gregor 741fb5c3a6SDouglas Gregor ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC, 7589929282SDouglas Gregor const LangOptions &LangOpts, const TargetInfo *Target) 763ec6663bSDouglas Gregor : LangOpts(LangOpts), Target(Target), BuiltinIncludeDir(0) 771fb5c3a6SDouglas Gregor { 78c95d8192SDylan Noblesmith IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs); 79c95d8192SDylan Noblesmith Diags = IntrusiveRefCntPtr<DiagnosticsEngine>( 80*811db4eaSDouglas Gregor new DiagnosticsEngine(DiagIDs, new DiagnosticOptions)); 81718292f2SDouglas Gregor Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true); 82718292f2SDouglas Gregor SourceMgr = new SourceManager(*Diags, FileMgr); 83718292f2SDouglas Gregor } 84718292f2SDouglas Gregor 85718292f2SDouglas Gregor ModuleMap::~ModuleMap() { 865acdf59eSDouglas Gregor for (llvm::StringMap<Module *>::iterator I = Modules.begin(), 875acdf59eSDouglas Gregor IEnd = Modules.end(); 885acdf59eSDouglas Gregor I != IEnd; ++I) { 895acdf59eSDouglas Gregor delete I->getValue(); 905acdf59eSDouglas Gregor } 915acdf59eSDouglas Gregor 92718292f2SDouglas Gregor delete SourceMgr; 93718292f2SDouglas Gregor } 94718292f2SDouglas Gregor 9589929282SDouglas Gregor void ModuleMap::setTarget(const TargetInfo &Target) { 9689929282SDouglas Gregor assert((!this->Target || this->Target == &Target) && 9789929282SDouglas Gregor "Improper target override"); 9889929282SDouglas Gregor this->Target = &Target; 9989929282SDouglas Gregor } 10089929282SDouglas Gregor 101056396aeSDouglas Gregor /// \brief "Sanitize" a filename so that it can be used as an identifier. 102056396aeSDouglas Gregor static StringRef sanitizeFilenameAsIdentifier(StringRef Name, 103056396aeSDouglas Gregor SmallVectorImpl<char> &Buffer) { 104056396aeSDouglas Gregor if (Name.empty()) 105056396aeSDouglas Gregor return Name; 106056396aeSDouglas Gregor 107056396aeSDouglas Gregor // Check whether the filename is already an identifier; this is the common 108056396aeSDouglas Gregor // case. 109056396aeSDouglas Gregor bool isIdentifier = true; 110056396aeSDouglas Gregor for (unsigned I = 0, N = Name.size(); I != N; ++I) { 111056396aeSDouglas Gregor if (isalpha(Name[I]) || Name[I] == '_' || (isdigit(Name[I]) && I > 0)) 112056396aeSDouglas Gregor continue; 113056396aeSDouglas Gregor 114056396aeSDouglas Gregor isIdentifier = false; 115056396aeSDouglas Gregor break; 116056396aeSDouglas Gregor } 117056396aeSDouglas Gregor 118056396aeSDouglas Gregor if (!isIdentifier) { 119056396aeSDouglas Gregor // If we don't already have something with the form of an identifier, 120056396aeSDouglas Gregor // create a buffer with the sanitized name. 121056396aeSDouglas Gregor Buffer.clear(); 122056396aeSDouglas Gregor if (isdigit(Name[0])) 123056396aeSDouglas Gregor Buffer.push_back('_'); 124056396aeSDouglas Gregor Buffer.reserve(Buffer.size() + Name.size()); 125056396aeSDouglas Gregor for (unsigned I = 0, N = Name.size(); I != N; ++I) { 126056396aeSDouglas Gregor if (isalnum(Name[I]) || isspace(Name[I])) 127056396aeSDouglas Gregor Buffer.push_back(Name[I]); 128056396aeSDouglas Gregor else 129056396aeSDouglas Gregor Buffer.push_back('_'); 130056396aeSDouglas Gregor } 131056396aeSDouglas Gregor 132056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 133056396aeSDouglas Gregor } 134056396aeSDouglas Gregor 135056396aeSDouglas Gregor while (llvm::StringSwitch<bool>(Name) 136056396aeSDouglas Gregor #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true) 137056396aeSDouglas Gregor #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true) 138056396aeSDouglas Gregor #include "clang/Basic/TokenKinds.def" 139056396aeSDouglas Gregor .Default(false)) { 140056396aeSDouglas Gregor if (Name.data() != Buffer.data()) 141056396aeSDouglas Gregor Buffer.append(Name.begin(), Name.end()); 142056396aeSDouglas Gregor Buffer.push_back('_'); 143056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 144056396aeSDouglas Gregor } 145056396aeSDouglas Gregor 146056396aeSDouglas Gregor return Name; 147056396aeSDouglas Gregor } 148056396aeSDouglas Gregor 149de3ef502SDouglas Gregor Module *ModuleMap::findModuleForHeader(const FileEntry *File) { 15059527666SDouglas Gregor HeadersMap::iterator Known = Headers.find(File); 1511fb5c3a6SDouglas Gregor if (Known != Headers.end()) { 15259527666SDouglas Gregor // If a header is not available, don't report that it maps to anything. 15359527666SDouglas Gregor if (!Known->second.isAvailable()) 1541fb5c3a6SDouglas Gregor return 0; 1551fb5c3a6SDouglas Gregor 15659527666SDouglas Gregor return Known->second.getModule(); 1571fb5c3a6SDouglas Gregor } 158ab0c8a84SDouglas Gregor 159b65dbfffSDouglas Gregor const DirectoryEntry *Dir = File->getDir(); 160b65dbfffSDouglas Gregor llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs; 161b65dbfffSDouglas Gregor StringRef DirName = Dir->getName(); 162a89c5ac4SDouglas Gregor 163a89c5ac4SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 164a89c5ac4SDouglas Gregor // an umbrella header. 165b65dbfffSDouglas Gregor do { 166a89c5ac4SDouglas Gregor llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir 167a89c5ac4SDouglas Gregor = UmbrellaDirs.find(Dir); 168a89c5ac4SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 169a89c5ac4SDouglas Gregor Module *Result = KnownDir->second; 170930a85ccSDouglas Gregor 171930a85ccSDouglas Gregor // Search up the module stack until we find a module with an umbrella 17273141fa9SDouglas Gregor // directory. 173930a85ccSDouglas Gregor Module *UmbrellaModule = Result; 17473141fa9SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 175930a85ccSDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 176930a85ccSDouglas Gregor 177930a85ccSDouglas Gregor if (UmbrellaModule->InferSubmodules) { 178a89c5ac4SDouglas Gregor // Infer submodules for each of the directories we found between 179a89c5ac4SDouglas Gregor // the directory of the umbrella header and the directory where 180a89c5ac4SDouglas Gregor // the actual header is located. 1819458f82dSDouglas Gregor bool Explicit = UmbrellaModule->InferExplicitSubmodules; 1829458f82dSDouglas Gregor 1837033127bSDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 184a89c5ac4SDouglas Gregor // Find or create the module that corresponds to this directory name. 185056396aeSDouglas Gregor SmallString<32> NameBuf; 186056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 187056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 188056396aeSDouglas Gregor NameBuf); 189a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 1909458f82dSDouglas Gregor Explicit).first; 191a89c5ac4SDouglas Gregor 192a89c5ac4SDouglas Gregor // Associate the module and the directory. 193a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I-1]] = Result; 194a89c5ac4SDouglas Gregor 195a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 196a89c5ac4SDouglas Gregor // wildcard to the set of exports. 197930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 198a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 199a89c5ac4SDouglas Gregor } 200a89c5ac4SDouglas Gregor 201a89c5ac4SDouglas Gregor // Infer a submodule with the same name as this header file. 202056396aeSDouglas Gregor SmallString<32> NameBuf; 203056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 204056396aeSDouglas Gregor llvm::sys::path::stem(File->getName()), NameBuf); 205a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 2069458f82dSDouglas Gregor Explicit).first; 207c597c8c4SArgyrios Kyrtzidis Result->TopHeaders.insert(File); 208a89c5ac4SDouglas Gregor 209a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 210a89c5ac4SDouglas Gregor // wildcard to the set of exports. 211930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 212a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 213a89c5ac4SDouglas Gregor } else { 214a89c5ac4SDouglas Gregor // Record each of the directories we stepped through as being part of 215a89c5ac4SDouglas Gregor // the module we found, since the umbrella header covers them all. 216a89c5ac4SDouglas Gregor for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 217a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I]] = Result; 218a89c5ac4SDouglas Gregor } 219a89c5ac4SDouglas Gregor 22059527666SDouglas Gregor Headers[File] = KnownHeader(Result, /*Excluded=*/false); 2211fb5c3a6SDouglas Gregor 2221fb5c3a6SDouglas Gregor // If a header corresponds to an unavailable module, don't report 2231fb5c3a6SDouglas Gregor // that it maps to anything. 2241fb5c3a6SDouglas Gregor if (!Result->isAvailable()) 2251fb5c3a6SDouglas Gregor return 0; 2261fb5c3a6SDouglas Gregor 227a89c5ac4SDouglas Gregor return Result; 228a89c5ac4SDouglas Gregor } 229a89c5ac4SDouglas Gregor 230a89c5ac4SDouglas Gregor SkippedDirs.push_back(Dir); 231a89c5ac4SDouglas Gregor 232b65dbfffSDouglas Gregor // Retrieve our parent path. 233b65dbfffSDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 234b65dbfffSDouglas Gregor if (DirName.empty()) 235b65dbfffSDouglas Gregor break; 236b65dbfffSDouglas Gregor 237b65dbfffSDouglas Gregor // Resolve the parent path to a directory entry. 238b65dbfffSDouglas Gregor Dir = SourceMgr->getFileManager().getDirectory(DirName); 239a89c5ac4SDouglas Gregor } while (Dir); 240b65dbfffSDouglas Gregor 241ab0c8a84SDouglas Gregor return 0; 242ab0c8a84SDouglas Gregor } 243ab0c8a84SDouglas Gregor 2441fb5c3a6SDouglas Gregor bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) { 24559527666SDouglas Gregor HeadersMap::iterator Known = Headers.find(Header); 2461fb5c3a6SDouglas Gregor if (Known != Headers.end()) 24759527666SDouglas Gregor return !Known->second.isAvailable(); 2481fb5c3a6SDouglas Gregor 2491fb5c3a6SDouglas Gregor const DirectoryEntry *Dir = Header->getDir(); 2501fb5c3a6SDouglas Gregor llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs; 2511fb5c3a6SDouglas Gregor StringRef DirName = Dir->getName(); 2521fb5c3a6SDouglas Gregor 2531fb5c3a6SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 2541fb5c3a6SDouglas Gregor // an umbrella header. 2551fb5c3a6SDouglas Gregor do { 2561fb5c3a6SDouglas Gregor llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir 2571fb5c3a6SDouglas Gregor = UmbrellaDirs.find(Dir); 2581fb5c3a6SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 2591fb5c3a6SDouglas Gregor Module *Found = KnownDir->second; 2601fb5c3a6SDouglas Gregor if (!Found->isAvailable()) 2611fb5c3a6SDouglas Gregor return true; 2621fb5c3a6SDouglas Gregor 2631fb5c3a6SDouglas Gregor // Search up the module stack until we find a module with an umbrella 2641fb5c3a6SDouglas Gregor // directory. 2651fb5c3a6SDouglas Gregor Module *UmbrellaModule = Found; 2661fb5c3a6SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 2671fb5c3a6SDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 2681fb5c3a6SDouglas Gregor 2691fb5c3a6SDouglas Gregor if (UmbrellaModule->InferSubmodules) { 2701fb5c3a6SDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 2711fb5c3a6SDouglas Gregor // Find or create the module that corresponds to this directory name. 272056396aeSDouglas Gregor SmallString<32> NameBuf; 273056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 274056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 275056396aeSDouglas Gregor NameBuf); 2761fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 2771fb5c3a6SDouglas Gregor if (!Found) 2781fb5c3a6SDouglas Gregor return false; 2791fb5c3a6SDouglas Gregor if (!Found->isAvailable()) 2801fb5c3a6SDouglas Gregor return true; 2811fb5c3a6SDouglas Gregor } 2821fb5c3a6SDouglas Gregor 2831fb5c3a6SDouglas Gregor // Infer a submodule with the same name as this header file. 284056396aeSDouglas Gregor SmallString<32> NameBuf; 285056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 286056396aeSDouglas Gregor llvm::sys::path::stem(Header->getName()), 287056396aeSDouglas Gregor NameBuf); 2881fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 2891fb5c3a6SDouglas Gregor if (!Found) 2901fb5c3a6SDouglas Gregor return false; 2911fb5c3a6SDouglas Gregor } 2921fb5c3a6SDouglas Gregor 2931fb5c3a6SDouglas Gregor return !Found->isAvailable(); 2941fb5c3a6SDouglas Gregor } 2951fb5c3a6SDouglas Gregor 2961fb5c3a6SDouglas Gregor SkippedDirs.push_back(Dir); 2971fb5c3a6SDouglas Gregor 2981fb5c3a6SDouglas Gregor // Retrieve our parent path. 2991fb5c3a6SDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 3001fb5c3a6SDouglas Gregor if (DirName.empty()) 3011fb5c3a6SDouglas Gregor break; 3021fb5c3a6SDouglas Gregor 3031fb5c3a6SDouglas Gregor // Resolve the parent path to a directory entry. 3041fb5c3a6SDouglas Gregor Dir = SourceMgr->getFileManager().getDirectory(DirName); 3051fb5c3a6SDouglas Gregor } while (Dir); 3061fb5c3a6SDouglas Gregor 3071fb5c3a6SDouglas Gregor return false; 3081fb5c3a6SDouglas Gregor } 3091fb5c3a6SDouglas Gregor 310de3ef502SDouglas Gregor Module *ModuleMap::findModule(StringRef Name) { 31188bdfb0eSDouglas Gregor llvm::StringMap<Module *>::iterator Known = Modules.find(Name); 31288bdfb0eSDouglas Gregor if (Known != Modules.end()) 31388bdfb0eSDouglas Gregor return Known->getValue(); 31488bdfb0eSDouglas Gregor 31588bdfb0eSDouglas Gregor return 0; 31688bdfb0eSDouglas Gregor } 31788bdfb0eSDouglas Gregor 3182b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) { 3192b82c2a5SDouglas Gregor for(; Context; Context = Context->Parent) { 3202b82c2a5SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Context)) 3212b82c2a5SDouglas Gregor return Sub; 3222b82c2a5SDouglas Gregor } 3232b82c2a5SDouglas Gregor 3242b82c2a5SDouglas Gregor return findModule(Name); 3252b82c2a5SDouglas Gregor } 3262b82c2a5SDouglas Gregor 3272b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) { 3282b82c2a5SDouglas Gregor if (!Context) 3292b82c2a5SDouglas Gregor return findModule(Name); 3302b82c2a5SDouglas Gregor 331eb90e830SDouglas Gregor return Context->findSubmodule(Name); 3322b82c2a5SDouglas Gregor } 3332b82c2a5SDouglas Gregor 334de3ef502SDouglas Gregor std::pair<Module *, bool> 33569021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 33669021974SDouglas Gregor bool IsExplicit) { 33769021974SDouglas Gregor // Try to find an existing module with this name. 338eb90e830SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Parent)) 339eb90e830SDouglas Gregor return std::make_pair(Sub, false); 34069021974SDouglas Gregor 34169021974SDouglas Gregor // Create a new module with this name. 34269021974SDouglas Gregor Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework, 34369021974SDouglas Gregor IsExplicit); 344eb90e830SDouglas Gregor if (!Parent) 34569021974SDouglas Gregor Modules[Name] = Result; 34669021974SDouglas Gregor return std::make_pair(Result, true); 34769021974SDouglas Gregor } 34869021974SDouglas Gregor 349de3ef502SDouglas Gregor Module * 35056c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName, 351e89dbc1dSDouglas Gregor const DirectoryEntry *FrameworkDir, 352a686e1b0SDouglas Gregor bool IsSystem, 353e89dbc1dSDouglas Gregor Module *Parent) { 35456c64013SDouglas Gregor // Check whether we've already found this module. 355e89dbc1dSDouglas Gregor if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 356e89dbc1dSDouglas Gregor return Mod; 357e89dbc1dSDouglas Gregor 358e89dbc1dSDouglas Gregor FileManager &FileMgr = SourceMgr->getFileManager(); 35956c64013SDouglas Gregor 36056c64013SDouglas Gregor // Look for an umbrella header. 3612c1dd271SDylan Noblesmith SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 36256c64013SDouglas Gregor llvm::sys::path::append(UmbrellaName, "Headers"); 36356c64013SDouglas Gregor llvm::sys::path::append(UmbrellaName, ModuleName + ".h"); 364e89dbc1dSDouglas Gregor const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 36556c64013SDouglas Gregor 36656c64013SDouglas Gregor // FIXME: If there's no umbrella header, we could probably scan the 36756c64013SDouglas Gregor // framework to load *everything*. But, it's not clear that this is a good 36856c64013SDouglas Gregor // idea. 36956c64013SDouglas Gregor if (!UmbrellaHeader) 37056c64013SDouglas Gregor return 0; 37156c64013SDouglas Gregor 372e89dbc1dSDouglas Gregor Module *Result = new Module(ModuleName, SourceLocation(), Parent, 373e89dbc1dSDouglas Gregor /*IsFramework=*/true, /*IsExplicit=*/false); 374a686e1b0SDouglas Gregor if (IsSystem) 375a686e1b0SDouglas Gregor Result->IsSystem = IsSystem; 376a686e1b0SDouglas Gregor 377eb90e830SDouglas Gregor if (!Parent) 378e89dbc1dSDouglas Gregor Modules[ModuleName] = Result; 379e89dbc1dSDouglas Gregor 380322f633cSDouglas Gregor // umbrella header "umbrella-header-name" 38173141fa9SDouglas Gregor Result->Umbrella = UmbrellaHeader; 38259527666SDouglas Gregor Headers[UmbrellaHeader] = KnownHeader(Result, /*Excluded=*/false); 3834dc71835SDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Result; 384d8bd7537SDouglas Gregor 385d8bd7537SDouglas Gregor // export * 386d8bd7537SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 387d8bd7537SDouglas Gregor 388a89c5ac4SDouglas Gregor // module * { export * } 389a89c5ac4SDouglas Gregor Result->InferSubmodules = true; 390a89c5ac4SDouglas Gregor Result->InferExportWildcard = true; 391a89c5ac4SDouglas Gregor 392e89dbc1dSDouglas Gregor // Look for subframeworks. 393e89dbc1dSDouglas Gregor llvm::error_code EC; 3942c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirName 395ddaa69cbSDouglas Gregor = StringRef(FrameworkDir->getName()); 396e89dbc1dSDouglas Gregor llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 3972c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirNameNative; 398ddaa69cbSDouglas Gregor llvm::sys::path::native(SubframeworksDirName.str(), 399ddaa69cbSDouglas Gregor SubframeworksDirNameNative); 400ddaa69cbSDouglas Gregor for (llvm::sys::fs::directory_iterator 401ddaa69cbSDouglas Gregor Dir(SubframeworksDirNameNative.str(), EC), DirEnd; 402e89dbc1dSDouglas Gregor Dir != DirEnd && !EC; Dir.increment(EC)) { 403e89dbc1dSDouglas Gregor if (!StringRef(Dir->path()).endswith(".framework")) 404e89dbc1dSDouglas Gregor continue; 405f2161a70SDouglas Gregor 406e89dbc1dSDouglas Gregor if (const DirectoryEntry *SubframeworkDir 407e89dbc1dSDouglas Gregor = FileMgr.getDirectory(Dir->path())) { 40807c22b78SDouglas Gregor // Note: as an egregious but useful hack, we use the real path here and 40907c22b78SDouglas Gregor // check whether it is actually a subdirectory of the parent directory. 41007c22b78SDouglas Gregor // This will not be the case if the 'subframework' is actually a symlink 41107c22b78SDouglas Gregor // out to a top-level framework. 41207c22b78SDouglas Gregor #ifdef LLVM_ON_UNIX 41307c22b78SDouglas Gregor char RealSubframeworkDirName[PATH_MAX]; 41407c22b78SDouglas Gregor if (realpath(Dir->path().c_str(), RealSubframeworkDirName)) { 41507c22b78SDouglas Gregor StringRef SubframeworkDirName = RealSubframeworkDirName; 41607c22b78SDouglas Gregor 41707c22b78SDouglas Gregor bool FoundParent = false; 41807c22b78SDouglas Gregor do { 41907c22b78SDouglas Gregor // Get the parent directory name. 42007c22b78SDouglas Gregor SubframeworkDirName 42107c22b78SDouglas Gregor = llvm::sys::path::parent_path(SubframeworkDirName); 42207c22b78SDouglas Gregor if (SubframeworkDirName.empty()) 42307c22b78SDouglas Gregor break; 42407c22b78SDouglas Gregor 42507c22b78SDouglas Gregor if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { 42607c22b78SDouglas Gregor FoundParent = true; 42707c22b78SDouglas Gregor break; 42807c22b78SDouglas Gregor } 42907c22b78SDouglas Gregor } while (true); 43007c22b78SDouglas Gregor 43107c22b78SDouglas Gregor if (!FoundParent) 43207c22b78SDouglas Gregor continue; 43307c22b78SDouglas Gregor } 43407c22b78SDouglas Gregor #endif 43507c22b78SDouglas Gregor 436e89dbc1dSDouglas Gregor // FIXME: Do we want to warn about subframeworks without umbrella headers? 437056396aeSDouglas Gregor SmallString<32> NameBuf; 438056396aeSDouglas Gregor inferFrameworkModule(sanitizeFilenameAsIdentifier( 439056396aeSDouglas Gregor llvm::sys::path::stem(Dir->path()), NameBuf), 440056396aeSDouglas Gregor SubframeworkDir, IsSystem, Result); 441e89dbc1dSDouglas Gregor } 442e89dbc1dSDouglas Gregor } 443e89dbc1dSDouglas Gregor 44456c64013SDouglas Gregor return Result; 44556c64013SDouglas Gregor } 44656c64013SDouglas Gregor 447a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ 44859527666SDouglas Gregor Headers[UmbrellaHeader] = KnownHeader(Mod, /*Excluded=*/false); 44973141fa9SDouglas Gregor Mod->Umbrella = UmbrellaHeader; 4507033127bSDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; 451a89c5ac4SDouglas Gregor } 452a89c5ac4SDouglas Gregor 453524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) { 454524e33e1SDouglas Gregor Mod->Umbrella = UmbrellaDir; 455524e33e1SDouglas Gregor UmbrellaDirs[UmbrellaDir] = Mod; 456524e33e1SDouglas Gregor } 457524e33e1SDouglas Gregor 45859527666SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header, 45959527666SDouglas Gregor bool Excluded) { 46059527666SDouglas Gregor if (Excluded) 46159527666SDouglas Gregor Mod->ExcludedHeaders.push_back(Header); 46259527666SDouglas Gregor else 463a89c5ac4SDouglas Gregor Mod->Headers.push_back(Header); 46459527666SDouglas Gregor Headers[Header] = KnownHeader(Mod, Excluded); 465a89c5ac4SDouglas Gregor } 466a89c5ac4SDouglas Gregor 467514b636aSDouglas Gregor const FileEntry * 468de3ef502SDouglas Gregor ModuleMap::getContainingModuleMapFile(Module *Module) { 469514b636aSDouglas Gregor if (Module->DefinitionLoc.isInvalid() || !SourceMgr) 470514b636aSDouglas Gregor return 0; 471514b636aSDouglas Gregor 472514b636aSDouglas Gregor return SourceMgr->getFileEntryForID( 473514b636aSDouglas Gregor SourceMgr->getFileID(Module->DefinitionLoc)); 474514b636aSDouglas Gregor } 475514b636aSDouglas Gregor 476718292f2SDouglas Gregor void ModuleMap::dump() { 477718292f2SDouglas Gregor llvm::errs() << "Modules:"; 478718292f2SDouglas Gregor for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 479718292f2SDouglas Gregor MEnd = Modules.end(); 480718292f2SDouglas Gregor M != MEnd; ++M) 481d28d1b8dSDouglas Gregor M->getValue()->print(llvm::errs(), 2); 482718292f2SDouglas Gregor 483718292f2SDouglas Gregor llvm::errs() << "Headers:"; 48459527666SDouglas Gregor for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 485718292f2SDouglas Gregor H != HEnd; ++H) { 486718292f2SDouglas Gregor llvm::errs() << " \"" << H->first->getName() << "\" -> " 48759527666SDouglas Gregor << H->second.getModule()->getFullModuleName() << "\n"; 488718292f2SDouglas Gregor } 489718292f2SDouglas Gregor } 490718292f2SDouglas Gregor 4912b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 4922b82c2a5SDouglas Gregor bool HadError = false; 4932b82c2a5SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { 4942b82c2a5SDouglas Gregor Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 4952b82c2a5SDouglas Gregor Complain); 496f5eedd05SDouglas Gregor if (Export.getPointer() || Export.getInt()) 4972b82c2a5SDouglas Gregor Mod->Exports.push_back(Export); 4982b82c2a5SDouglas Gregor else 4992b82c2a5SDouglas Gregor HadError = true; 5002b82c2a5SDouglas Gregor } 5012b82c2a5SDouglas Gregor Mod->UnresolvedExports.clear(); 5022b82c2a5SDouglas Gregor return HadError; 5032b82c2a5SDouglas Gregor } 5042b82c2a5SDouglas Gregor 5050093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 5060093b3c7SDouglas Gregor if (Loc.isInvalid()) 5070093b3c7SDouglas Gregor return 0; 5080093b3c7SDouglas Gregor 5090093b3c7SDouglas Gregor // Use the expansion location to determine which module we're in. 5100093b3c7SDouglas Gregor FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 5110093b3c7SDouglas Gregor if (!ExpansionLoc.isFileID()) 5120093b3c7SDouglas Gregor return 0; 5130093b3c7SDouglas Gregor 5140093b3c7SDouglas Gregor 5150093b3c7SDouglas Gregor const SourceManager &SrcMgr = Loc.getManager(); 5160093b3c7SDouglas Gregor FileID ExpansionFileID = ExpansionLoc.getFileID(); 517224d8a74SDouglas Gregor 518224d8a74SDouglas Gregor while (const FileEntry *ExpansionFile 519224d8a74SDouglas Gregor = SrcMgr.getFileEntryForID(ExpansionFileID)) { 520224d8a74SDouglas Gregor // Find the module that owns this header (if any). 521224d8a74SDouglas Gregor if (Module *Mod = findModuleForHeader(ExpansionFile)) 522224d8a74SDouglas Gregor return Mod; 523224d8a74SDouglas Gregor 524224d8a74SDouglas Gregor // No module owns this header, so look up the inclusion chain to see if 525224d8a74SDouglas Gregor // any included header has an associated module. 526224d8a74SDouglas Gregor SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); 527224d8a74SDouglas Gregor if (IncludeLoc.isInvalid()) 5280093b3c7SDouglas Gregor return 0; 5290093b3c7SDouglas Gregor 530224d8a74SDouglas Gregor ExpansionFileID = SrcMgr.getFileID(IncludeLoc); 531224d8a74SDouglas Gregor } 532224d8a74SDouglas Gregor 533224d8a74SDouglas Gregor return 0; 5340093b3c7SDouglas Gregor } 5350093b3c7SDouglas Gregor 536718292f2SDouglas Gregor //----------------------------------------------------------------------------// 537718292f2SDouglas Gregor // Module map file parser 538718292f2SDouglas Gregor //----------------------------------------------------------------------------// 539718292f2SDouglas Gregor 540718292f2SDouglas Gregor namespace clang { 541718292f2SDouglas Gregor /// \brief A token in a module map file. 542718292f2SDouglas Gregor struct MMToken { 543718292f2SDouglas Gregor enum TokenKind { 5441fb5c3a6SDouglas Gregor Comma, 545718292f2SDouglas Gregor EndOfFile, 546718292f2SDouglas Gregor HeaderKeyword, 547718292f2SDouglas Gregor Identifier, 54859527666SDouglas Gregor ExcludeKeyword, 549718292f2SDouglas Gregor ExplicitKeyword, 5502b82c2a5SDouglas Gregor ExportKeyword, 551755b2055SDouglas Gregor FrameworkKeyword, 552718292f2SDouglas Gregor ModuleKeyword, 5532b82c2a5SDouglas Gregor Period, 554718292f2SDouglas Gregor UmbrellaKeyword, 5551fb5c3a6SDouglas Gregor RequiresKeyword, 5562b82c2a5SDouglas Gregor Star, 557718292f2SDouglas Gregor StringLiteral, 558718292f2SDouglas Gregor LBrace, 559a686e1b0SDouglas Gregor RBrace, 560a686e1b0SDouglas Gregor LSquare, 561a686e1b0SDouglas Gregor RSquare 562718292f2SDouglas Gregor } Kind; 563718292f2SDouglas Gregor 564718292f2SDouglas Gregor unsigned Location; 565718292f2SDouglas Gregor unsigned StringLength; 566718292f2SDouglas Gregor const char *StringData; 567718292f2SDouglas Gregor 568718292f2SDouglas Gregor void clear() { 569718292f2SDouglas Gregor Kind = EndOfFile; 570718292f2SDouglas Gregor Location = 0; 571718292f2SDouglas Gregor StringLength = 0; 572718292f2SDouglas Gregor StringData = 0; 573718292f2SDouglas Gregor } 574718292f2SDouglas Gregor 575718292f2SDouglas Gregor bool is(TokenKind K) const { return Kind == K; } 576718292f2SDouglas Gregor 577718292f2SDouglas Gregor SourceLocation getLocation() const { 578718292f2SDouglas Gregor return SourceLocation::getFromRawEncoding(Location); 579718292f2SDouglas Gregor } 580718292f2SDouglas Gregor 581718292f2SDouglas Gregor StringRef getString() const { 582718292f2SDouglas Gregor return StringRef(StringData, StringLength); 583718292f2SDouglas Gregor } 584718292f2SDouglas Gregor }; 585718292f2SDouglas Gregor 586718292f2SDouglas Gregor class ModuleMapParser { 587718292f2SDouglas Gregor Lexer &L; 588718292f2SDouglas Gregor SourceManager &SourceMgr; 589bc10b9fbSDouglas Gregor 590bc10b9fbSDouglas Gregor /// \brief Default target information, used only for string literal 591bc10b9fbSDouglas Gregor /// parsing. 592bc10b9fbSDouglas Gregor const TargetInfo *Target; 593bc10b9fbSDouglas Gregor 594718292f2SDouglas Gregor DiagnosticsEngine &Diags; 595718292f2SDouglas Gregor ModuleMap ⤅ 596718292f2SDouglas Gregor 5975257fc63SDouglas Gregor /// \brief The directory that this module map resides in. 5985257fc63SDouglas Gregor const DirectoryEntry *Directory; 5995257fc63SDouglas Gregor 6003ec6663bSDouglas Gregor /// \brief The directory containing Clang-supplied headers. 6013ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir; 6023ec6663bSDouglas Gregor 603718292f2SDouglas Gregor /// \brief Whether an error occurred. 604718292f2SDouglas Gregor bool HadError; 605718292f2SDouglas Gregor 606718292f2SDouglas Gregor /// \brief Stores string data for the various string literals referenced 607718292f2SDouglas Gregor /// during parsing. 608718292f2SDouglas Gregor llvm::BumpPtrAllocator StringData; 609718292f2SDouglas Gregor 610718292f2SDouglas Gregor /// \brief The current token. 611718292f2SDouglas Gregor MMToken Tok; 612718292f2SDouglas Gregor 613718292f2SDouglas Gregor /// \brief The active module. 614de3ef502SDouglas Gregor Module *ActiveModule; 615718292f2SDouglas Gregor 616718292f2SDouglas Gregor /// \brief Consume the current token and return its location. 617718292f2SDouglas Gregor SourceLocation consumeToken(); 618718292f2SDouglas Gregor 619718292f2SDouglas Gregor /// \brief Skip tokens until we reach the a token with the given kind 620718292f2SDouglas Gregor /// (or the end of the file). 621718292f2SDouglas Gregor void skipUntil(MMToken::TokenKind K); 622718292f2SDouglas Gregor 623e7ab3669SDouglas Gregor typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2> 624e7ab3669SDouglas Gregor ModuleId; 625e7ab3669SDouglas Gregor bool parseModuleId(ModuleId &Id); 626718292f2SDouglas Gregor void parseModuleDecl(); 6271fb5c3a6SDouglas Gregor void parseRequiresDecl(); 62859527666SDouglas Gregor void parseHeaderDecl(SourceLocation UmbrellaLoc, SourceLocation ExcludeLoc); 629524e33e1SDouglas Gregor void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 6302b82c2a5SDouglas Gregor void parseExportDecl(); 63173441091SDouglas Gregor void parseInferredSubmoduleDecl(bool Explicit); 632718292f2SDouglas Gregor 6337033127bSDouglas Gregor const DirectoryEntry *getOverriddenHeaderSearchDir(); 6347033127bSDouglas Gregor 635718292f2SDouglas Gregor public: 636718292f2SDouglas Gregor explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 637bc10b9fbSDouglas Gregor const TargetInfo *Target, 638718292f2SDouglas Gregor DiagnosticsEngine &Diags, 6395257fc63SDouglas Gregor ModuleMap &Map, 6403ec6663bSDouglas Gregor const DirectoryEntry *Directory, 6413ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir) 642bc10b9fbSDouglas Gregor : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 6433ec6663bSDouglas Gregor Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir), 6443ec6663bSDouglas Gregor HadError(false), ActiveModule(0) 645718292f2SDouglas Gregor { 646718292f2SDouglas Gregor Tok.clear(); 647718292f2SDouglas Gregor consumeToken(); 648718292f2SDouglas Gregor } 649718292f2SDouglas Gregor 650718292f2SDouglas Gregor bool parseModuleMapFile(); 651718292f2SDouglas Gregor }; 652718292f2SDouglas Gregor } 653718292f2SDouglas Gregor 654718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() { 655718292f2SDouglas Gregor retry: 656718292f2SDouglas Gregor SourceLocation Result = Tok.getLocation(); 657718292f2SDouglas Gregor Tok.clear(); 658718292f2SDouglas Gregor 659718292f2SDouglas Gregor Token LToken; 660718292f2SDouglas Gregor L.LexFromRawLexer(LToken); 661718292f2SDouglas Gregor Tok.Location = LToken.getLocation().getRawEncoding(); 662718292f2SDouglas Gregor switch (LToken.getKind()) { 663718292f2SDouglas Gregor case tok::raw_identifier: 664718292f2SDouglas Gregor Tok.StringData = LToken.getRawIdentifierData(); 665718292f2SDouglas Gregor Tok.StringLength = LToken.getLength(); 666718292f2SDouglas Gregor Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString()) 667718292f2SDouglas Gregor .Case("header", MMToken::HeaderKeyword) 66859527666SDouglas Gregor .Case("exclude", MMToken::ExcludeKeyword) 669718292f2SDouglas Gregor .Case("explicit", MMToken::ExplicitKeyword) 6702b82c2a5SDouglas Gregor .Case("export", MMToken::ExportKeyword) 671755b2055SDouglas Gregor .Case("framework", MMToken::FrameworkKeyword) 672718292f2SDouglas Gregor .Case("module", MMToken::ModuleKeyword) 6731fb5c3a6SDouglas Gregor .Case("requires", MMToken::RequiresKeyword) 674718292f2SDouglas Gregor .Case("umbrella", MMToken::UmbrellaKeyword) 675718292f2SDouglas Gregor .Default(MMToken::Identifier); 676718292f2SDouglas Gregor break; 677718292f2SDouglas Gregor 6781fb5c3a6SDouglas Gregor case tok::comma: 6791fb5c3a6SDouglas Gregor Tok.Kind = MMToken::Comma; 6801fb5c3a6SDouglas Gregor break; 6811fb5c3a6SDouglas Gregor 682718292f2SDouglas Gregor case tok::eof: 683718292f2SDouglas Gregor Tok.Kind = MMToken::EndOfFile; 684718292f2SDouglas Gregor break; 685718292f2SDouglas Gregor 686718292f2SDouglas Gregor case tok::l_brace: 687718292f2SDouglas Gregor Tok.Kind = MMToken::LBrace; 688718292f2SDouglas Gregor break; 689718292f2SDouglas Gregor 690a686e1b0SDouglas Gregor case tok::l_square: 691a686e1b0SDouglas Gregor Tok.Kind = MMToken::LSquare; 692a686e1b0SDouglas Gregor break; 693a686e1b0SDouglas Gregor 6942b82c2a5SDouglas Gregor case tok::period: 6952b82c2a5SDouglas Gregor Tok.Kind = MMToken::Period; 6962b82c2a5SDouglas Gregor break; 6972b82c2a5SDouglas Gregor 698718292f2SDouglas Gregor case tok::r_brace: 699718292f2SDouglas Gregor Tok.Kind = MMToken::RBrace; 700718292f2SDouglas Gregor break; 701718292f2SDouglas Gregor 702a686e1b0SDouglas Gregor case tok::r_square: 703a686e1b0SDouglas Gregor Tok.Kind = MMToken::RSquare; 704a686e1b0SDouglas Gregor break; 705a686e1b0SDouglas Gregor 7062b82c2a5SDouglas Gregor case tok::star: 7072b82c2a5SDouglas Gregor Tok.Kind = MMToken::Star; 7082b82c2a5SDouglas Gregor break; 7092b82c2a5SDouglas Gregor 710718292f2SDouglas Gregor case tok::string_literal: { 711d67aea28SRichard Smith if (LToken.hasUDSuffix()) { 712d67aea28SRichard Smith Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 713d67aea28SRichard Smith HadError = true; 714d67aea28SRichard Smith goto retry; 715d67aea28SRichard Smith } 716d67aea28SRichard Smith 717718292f2SDouglas Gregor // Parse the string literal. 718718292f2SDouglas Gregor LangOptions LangOpts; 719718292f2SDouglas Gregor StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target); 720718292f2SDouglas Gregor if (StringLiteral.hadError) 721718292f2SDouglas Gregor goto retry; 722718292f2SDouglas Gregor 723718292f2SDouglas Gregor // Copy the string literal into our string data allocator. 724718292f2SDouglas Gregor unsigned Length = StringLiteral.GetStringLength(); 725718292f2SDouglas Gregor char *Saved = StringData.Allocate<char>(Length + 1); 726718292f2SDouglas Gregor memcpy(Saved, StringLiteral.GetString().data(), Length); 727718292f2SDouglas Gregor Saved[Length] = 0; 728718292f2SDouglas Gregor 729718292f2SDouglas Gregor // Form the token. 730718292f2SDouglas Gregor Tok.Kind = MMToken::StringLiteral; 731718292f2SDouglas Gregor Tok.StringData = Saved; 732718292f2SDouglas Gregor Tok.StringLength = Length; 733718292f2SDouglas Gregor break; 734718292f2SDouglas Gregor } 735718292f2SDouglas Gregor 736718292f2SDouglas Gregor case tok::comment: 737718292f2SDouglas Gregor goto retry; 738718292f2SDouglas Gregor 739718292f2SDouglas Gregor default: 740718292f2SDouglas Gregor Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 741718292f2SDouglas Gregor HadError = true; 742718292f2SDouglas Gregor goto retry; 743718292f2SDouglas Gregor } 744718292f2SDouglas Gregor 745718292f2SDouglas Gregor return Result; 746718292f2SDouglas Gregor } 747718292f2SDouglas Gregor 748718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 749718292f2SDouglas Gregor unsigned braceDepth = 0; 750a686e1b0SDouglas Gregor unsigned squareDepth = 0; 751718292f2SDouglas Gregor do { 752718292f2SDouglas Gregor switch (Tok.Kind) { 753718292f2SDouglas Gregor case MMToken::EndOfFile: 754718292f2SDouglas Gregor return; 755718292f2SDouglas Gregor 756718292f2SDouglas Gregor case MMToken::LBrace: 757a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 758718292f2SDouglas Gregor return; 759718292f2SDouglas Gregor 760718292f2SDouglas Gregor ++braceDepth; 761718292f2SDouglas Gregor break; 762718292f2SDouglas Gregor 763a686e1b0SDouglas Gregor case MMToken::LSquare: 764a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 765a686e1b0SDouglas Gregor return; 766a686e1b0SDouglas Gregor 767a686e1b0SDouglas Gregor ++squareDepth; 768a686e1b0SDouglas Gregor break; 769a686e1b0SDouglas Gregor 770718292f2SDouglas Gregor case MMToken::RBrace: 771718292f2SDouglas Gregor if (braceDepth > 0) 772718292f2SDouglas Gregor --braceDepth; 773718292f2SDouglas Gregor else if (Tok.is(K)) 774718292f2SDouglas Gregor return; 775718292f2SDouglas Gregor break; 776718292f2SDouglas Gregor 777a686e1b0SDouglas Gregor case MMToken::RSquare: 778a686e1b0SDouglas Gregor if (squareDepth > 0) 779a686e1b0SDouglas Gregor --squareDepth; 780a686e1b0SDouglas Gregor else if (Tok.is(K)) 781a686e1b0SDouglas Gregor return; 782a686e1b0SDouglas Gregor break; 783a686e1b0SDouglas Gregor 784718292f2SDouglas Gregor default: 785a686e1b0SDouglas Gregor if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 786718292f2SDouglas Gregor return; 787718292f2SDouglas Gregor break; 788718292f2SDouglas Gregor } 789718292f2SDouglas Gregor 790718292f2SDouglas Gregor consumeToken(); 791718292f2SDouglas Gregor } while (true); 792718292f2SDouglas Gregor } 793718292f2SDouglas Gregor 794e7ab3669SDouglas Gregor /// \brief Parse a module-id. 795e7ab3669SDouglas Gregor /// 796e7ab3669SDouglas Gregor /// module-id: 797e7ab3669SDouglas Gregor /// identifier 798e7ab3669SDouglas Gregor /// identifier '.' module-id 799e7ab3669SDouglas Gregor /// 800e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise. 801e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) { 802e7ab3669SDouglas Gregor Id.clear(); 803e7ab3669SDouglas Gregor do { 804e7ab3669SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 805e7ab3669SDouglas Gregor Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 806e7ab3669SDouglas Gregor consumeToken(); 807e7ab3669SDouglas Gregor } else { 808e7ab3669SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 809e7ab3669SDouglas Gregor return true; 810e7ab3669SDouglas Gregor } 811e7ab3669SDouglas Gregor 812e7ab3669SDouglas Gregor if (!Tok.is(MMToken::Period)) 813e7ab3669SDouglas Gregor break; 814e7ab3669SDouglas Gregor 815e7ab3669SDouglas Gregor consumeToken(); 816e7ab3669SDouglas Gregor } while (true); 817e7ab3669SDouglas Gregor 818e7ab3669SDouglas Gregor return false; 819e7ab3669SDouglas Gregor } 820e7ab3669SDouglas Gregor 821a686e1b0SDouglas Gregor namespace { 822a686e1b0SDouglas Gregor /// \brief Enumerates the known attributes. 823a686e1b0SDouglas Gregor enum AttributeKind { 824a686e1b0SDouglas Gregor /// \brief An unknown attribute. 825a686e1b0SDouglas Gregor AT_unknown, 826a686e1b0SDouglas Gregor /// \brief The 'system' attribute. 827a686e1b0SDouglas Gregor AT_system 828a686e1b0SDouglas Gregor }; 829a686e1b0SDouglas Gregor } 830a686e1b0SDouglas Gregor 831718292f2SDouglas Gregor /// \brief Parse a module declaration. 832718292f2SDouglas Gregor /// 833718292f2SDouglas Gregor /// module-declaration: 834a686e1b0SDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 835a686e1b0SDouglas Gregor /// { module-member* } 836a686e1b0SDouglas Gregor /// 837a686e1b0SDouglas Gregor /// attributes: 838a686e1b0SDouglas Gregor /// attribute attributes 839a686e1b0SDouglas Gregor /// attribute 840a686e1b0SDouglas Gregor /// 841a686e1b0SDouglas Gregor /// attribute: 842a686e1b0SDouglas Gregor /// [ identifier ] 843718292f2SDouglas Gregor /// 844718292f2SDouglas Gregor /// module-member: 8451fb5c3a6SDouglas Gregor /// requires-declaration 846718292f2SDouglas Gregor /// header-declaration 847e7ab3669SDouglas Gregor /// submodule-declaration 8482b82c2a5SDouglas Gregor /// export-declaration 84973441091SDouglas Gregor /// 85073441091SDouglas Gregor /// submodule-declaration: 85173441091SDouglas Gregor /// module-declaration 85273441091SDouglas Gregor /// inferred-submodule-declaration 853718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() { 854755b2055SDouglas Gregor assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 855755b2055SDouglas Gregor Tok.is(MMToken::FrameworkKeyword)); 856f2161a70SDouglas Gregor // Parse 'explicit' or 'framework' keyword, if present. 857e7ab3669SDouglas Gregor SourceLocation ExplicitLoc; 858718292f2SDouglas Gregor bool Explicit = false; 859f2161a70SDouglas Gregor bool Framework = false; 860755b2055SDouglas Gregor 861f2161a70SDouglas Gregor // Parse 'explicit' keyword, if present. 862f2161a70SDouglas Gregor if (Tok.is(MMToken::ExplicitKeyword)) { 863e7ab3669SDouglas Gregor ExplicitLoc = consumeToken(); 864f2161a70SDouglas Gregor Explicit = true; 865f2161a70SDouglas Gregor } 866f2161a70SDouglas Gregor 867f2161a70SDouglas Gregor // Parse 'framework' keyword, if present. 868755b2055SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 869755b2055SDouglas Gregor consumeToken(); 870755b2055SDouglas Gregor Framework = true; 871755b2055SDouglas Gregor } 872718292f2SDouglas Gregor 873718292f2SDouglas Gregor // Parse 'module' keyword. 874718292f2SDouglas Gregor if (!Tok.is(MMToken::ModuleKeyword)) { 875d6343c99SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 876718292f2SDouglas Gregor consumeToken(); 877718292f2SDouglas Gregor HadError = true; 878718292f2SDouglas Gregor return; 879718292f2SDouglas Gregor } 880718292f2SDouglas Gregor consumeToken(); // 'module' keyword 881718292f2SDouglas Gregor 88273441091SDouglas Gregor // If we have a wildcard for the module name, this is an inferred submodule. 88373441091SDouglas Gregor // Parse it. 88473441091SDouglas Gregor if (Tok.is(MMToken::Star)) 88573441091SDouglas Gregor return parseInferredSubmoduleDecl(Explicit); 88673441091SDouglas Gregor 887718292f2SDouglas Gregor // Parse the module name. 888e7ab3669SDouglas Gregor ModuleId Id; 889e7ab3669SDouglas Gregor if (parseModuleId(Id)) { 890718292f2SDouglas Gregor HadError = true; 891718292f2SDouglas Gregor return; 892718292f2SDouglas Gregor } 893e7ab3669SDouglas Gregor 894e7ab3669SDouglas Gregor if (ActiveModule) { 895e7ab3669SDouglas Gregor if (Id.size() > 1) { 896e7ab3669SDouglas Gregor Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 897e7ab3669SDouglas Gregor << SourceRange(Id.front().second, Id.back().second); 898e7ab3669SDouglas Gregor 899e7ab3669SDouglas Gregor HadError = true; 900e7ab3669SDouglas Gregor return; 901e7ab3669SDouglas Gregor } 902e7ab3669SDouglas Gregor } else if (Id.size() == 1 && Explicit) { 903e7ab3669SDouglas Gregor // Top-level modules can't be explicit. 904e7ab3669SDouglas Gregor Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 905e7ab3669SDouglas Gregor Explicit = false; 906e7ab3669SDouglas Gregor ExplicitLoc = SourceLocation(); 907e7ab3669SDouglas Gregor HadError = true; 908e7ab3669SDouglas Gregor } 909e7ab3669SDouglas Gregor 910e7ab3669SDouglas Gregor Module *PreviousActiveModule = ActiveModule; 911e7ab3669SDouglas Gregor if (Id.size() > 1) { 912e7ab3669SDouglas Gregor // This module map defines a submodule. Go find the module of which it 913e7ab3669SDouglas Gregor // is a submodule. 914e7ab3669SDouglas Gregor ActiveModule = 0; 915e7ab3669SDouglas Gregor for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 916e7ab3669SDouglas Gregor if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 917e7ab3669SDouglas Gregor ActiveModule = Next; 918e7ab3669SDouglas Gregor continue; 919e7ab3669SDouglas Gregor } 920e7ab3669SDouglas Gregor 921e7ab3669SDouglas Gregor if (ActiveModule) { 922e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 923e7ab3669SDouglas Gregor << Id[I].first << ActiveModule->getTopLevelModule(); 924e7ab3669SDouglas Gregor } else { 925e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 926e7ab3669SDouglas Gregor } 927e7ab3669SDouglas Gregor HadError = true; 928e7ab3669SDouglas Gregor return; 929e7ab3669SDouglas Gregor } 930e7ab3669SDouglas Gregor } 931e7ab3669SDouglas Gregor 932e7ab3669SDouglas Gregor StringRef ModuleName = Id.back().first; 933e7ab3669SDouglas Gregor SourceLocation ModuleNameLoc = Id.back().second; 934718292f2SDouglas Gregor 935a686e1b0SDouglas Gregor // Parse the optional attribute list. 936a686e1b0SDouglas Gregor bool IsSystem = false; 937a686e1b0SDouglas Gregor while (Tok.is(MMToken::LSquare)) { 938a686e1b0SDouglas Gregor // Consume the '['. 939a686e1b0SDouglas Gregor SourceLocation LSquareLoc = consumeToken(); 940a686e1b0SDouglas Gregor 941a686e1b0SDouglas Gregor // Check whether we have an attribute name here. 942a686e1b0SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 943a686e1b0SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 944a686e1b0SDouglas Gregor skipUntil(MMToken::RSquare); 945a686e1b0SDouglas Gregor if (Tok.is(MMToken::RSquare)) 946a686e1b0SDouglas Gregor consumeToken(); 947a686e1b0SDouglas Gregor continue; 948a686e1b0SDouglas Gregor } 949a686e1b0SDouglas Gregor 950a686e1b0SDouglas Gregor // Decode the attribute name. 951a686e1b0SDouglas Gregor AttributeKind Attribute 952a686e1b0SDouglas Gregor = llvm::StringSwitch<AttributeKind>(Tok.getString()) 953a686e1b0SDouglas Gregor .Case("system", AT_system) 954a686e1b0SDouglas Gregor .Default(AT_unknown); 955a686e1b0SDouglas Gregor switch (Attribute) { 956a686e1b0SDouglas Gregor case AT_unknown: 957a686e1b0SDouglas Gregor Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 958a686e1b0SDouglas Gregor << Tok.getString(); 959a686e1b0SDouglas Gregor break; 960a686e1b0SDouglas Gregor 961a686e1b0SDouglas Gregor case AT_system: 962a686e1b0SDouglas Gregor IsSystem = true; 963a686e1b0SDouglas Gregor break; 964a686e1b0SDouglas Gregor } 965a686e1b0SDouglas Gregor consumeToken(); 966a686e1b0SDouglas Gregor 967a686e1b0SDouglas Gregor // Consume the ']'. 968a686e1b0SDouglas Gregor if (!Tok.is(MMToken::RSquare)) { 969a686e1b0SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 970a686e1b0SDouglas Gregor Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 971a686e1b0SDouglas Gregor skipUntil(MMToken::RSquare); 972a686e1b0SDouglas Gregor } 973a686e1b0SDouglas Gregor 974a686e1b0SDouglas Gregor if (Tok.is(MMToken::RSquare)) 975a686e1b0SDouglas Gregor consumeToken(); 976a686e1b0SDouglas Gregor } 977a686e1b0SDouglas Gregor 978718292f2SDouglas Gregor // Parse the opening brace. 979718292f2SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 980718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 981718292f2SDouglas Gregor << ModuleName; 982718292f2SDouglas Gregor HadError = true; 983718292f2SDouglas Gregor return; 984718292f2SDouglas Gregor } 985718292f2SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 986718292f2SDouglas Gregor 987718292f2SDouglas Gregor // Determine whether this (sub)module has already been defined. 988eb90e830SDouglas Gregor if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 989fcc54a3bSDouglas Gregor if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { 990fcc54a3bSDouglas Gregor // Skip the module definition. 991fcc54a3bSDouglas Gregor skipUntil(MMToken::RBrace); 992fcc54a3bSDouglas Gregor if (Tok.is(MMToken::RBrace)) 993fcc54a3bSDouglas Gregor consumeToken(); 994fcc54a3bSDouglas Gregor else { 995fcc54a3bSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 996fcc54a3bSDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 997fcc54a3bSDouglas Gregor HadError = true; 998fcc54a3bSDouglas Gregor } 999fcc54a3bSDouglas Gregor return; 1000fcc54a3bSDouglas Gregor } 1001fcc54a3bSDouglas Gregor 1002718292f2SDouglas Gregor Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 1003718292f2SDouglas Gregor << ModuleName; 1004eb90e830SDouglas Gregor Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 1005718292f2SDouglas Gregor 1006718292f2SDouglas Gregor // Skip the module definition. 1007718292f2SDouglas Gregor skipUntil(MMToken::RBrace); 1008718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1009718292f2SDouglas Gregor consumeToken(); 1010718292f2SDouglas Gregor 1011718292f2SDouglas Gregor HadError = true; 1012718292f2SDouglas Gregor return; 1013718292f2SDouglas Gregor } 1014718292f2SDouglas Gregor 1015718292f2SDouglas Gregor // Start defining this module. 1016eb90e830SDouglas Gregor ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, 1017eb90e830SDouglas Gregor Explicit).first; 1018eb90e830SDouglas Gregor ActiveModule->DefinitionLoc = ModuleNameLoc; 1019a686e1b0SDouglas Gregor if (IsSystem) 1020a686e1b0SDouglas Gregor ActiveModule->IsSystem = true; 1021718292f2SDouglas Gregor 1022718292f2SDouglas Gregor bool Done = false; 1023718292f2SDouglas Gregor do { 1024718292f2SDouglas Gregor switch (Tok.Kind) { 1025718292f2SDouglas Gregor case MMToken::EndOfFile: 1026718292f2SDouglas Gregor case MMToken::RBrace: 1027718292f2SDouglas Gregor Done = true; 1028718292f2SDouglas Gregor break; 1029718292f2SDouglas Gregor 1030718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 1031f2161a70SDouglas Gregor case MMToken::FrameworkKeyword: 1032718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1033718292f2SDouglas Gregor parseModuleDecl(); 1034718292f2SDouglas Gregor break; 1035718292f2SDouglas Gregor 10362b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 10372b82c2a5SDouglas Gregor parseExportDecl(); 10382b82c2a5SDouglas Gregor break; 10392b82c2a5SDouglas Gregor 10401fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 10411fb5c3a6SDouglas Gregor parseRequiresDecl(); 10421fb5c3a6SDouglas Gregor break; 10431fb5c3a6SDouglas Gregor 1044524e33e1SDouglas Gregor case MMToken::UmbrellaKeyword: { 1045524e33e1SDouglas Gregor SourceLocation UmbrellaLoc = consumeToken(); 1046524e33e1SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) 104759527666SDouglas Gregor parseHeaderDecl(UmbrellaLoc, SourceLocation()); 1048524e33e1SDouglas Gregor else 1049524e33e1SDouglas Gregor parseUmbrellaDirDecl(UmbrellaLoc); 1050718292f2SDouglas Gregor break; 1051524e33e1SDouglas Gregor } 1052718292f2SDouglas Gregor 105359527666SDouglas Gregor case MMToken::ExcludeKeyword: { 105459527666SDouglas Gregor SourceLocation ExcludeLoc = consumeToken(); 105559527666SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) { 105659527666SDouglas Gregor parseHeaderDecl(SourceLocation(), ExcludeLoc); 105759527666SDouglas Gregor } else { 105859527666SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 105959527666SDouglas Gregor << "exclude"; 106059527666SDouglas Gregor } 106159527666SDouglas Gregor break; 106259527666SDouglas Gregor } 106359527666SDouglas Gregor 1064322f633cSDouglas Gregor case MMToken::HeaderKeyword: 106559527666SDouglas Gregor parseHeaderDecl(SourceLocation(), SourceLocation()); 1066718292f2SDouglas Gregor break; 1067718292f2SDouglas Gregor 1068718292f2SDouglas Gregor default: 1069718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 1070718292f2SDouglas Gregor consumeToken(); 1071718292f2SDouglas Gregor break; 1072718292f2SDouglas Gregor } 1073718292f2SDouglas Gregor } while (!Done); 1074718292f2SDouglas Gregor 1075718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1076718292f2SDouglas Gregor consumeToken(); 1077718292f2SDouglas Gregor else { 1078718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1079718292f2SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1080718292f2SDouglas Gregor HadError = true; 1081718292f2SDouglas Gregor } 1082718292f2SDouglas Gregor 1083e7ab3669SDouglas Gregor // We're done parsing this module. Pop back to the previous module. 1084e7ab3669SDouglas Gregor ActiveModule = PreviousActiveModule; 1085718292f2SDouglas Gregor } 1086718292f2SDouglas Gregor 10871fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration. 10881fb5c3a6SDouglas Gregor /// 10891fb5c3a6SDouglas Gregor /// requires-declaration: 10901fb5c3a6SDouglas Gregor /// 'requires' feature-list 10911fb5c3a6SDouglas Gregor /// 10921fb5c3a6SDouglas Gregor /// feature-list: 10931fb5c3a6SDouglas Gregor /// identifier ',' feature-list 10941fb5c3a6SDouglas Gregor /// identifier 10951fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() { 10961fb5c3a6SDouglas Gregor assert(Tok.is(MMToken::RequiresKeyword)); 10971fb5c3a6SDouglas Gregor 10981fb5c3a6SDouglas Gregor // Parse 'requires' keyword. 10991fb5c3a6SDouglas Gregor consumeToken(); 11001fb5c3a6SDouglas Gregor 11011fb5c3a6SDouglas Gregor // Parse the feature-list. 11021fb5c3a6SDouglas Gregor do { 11031fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 11041fb5c3a6SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 11051fb5c3a6SDouglas Gregor HadError = true; 11061fb5c3a6SDouglas Gregor return; 11071fb5c3a6SDouglas Gregor } 11081fb5c3a6SDouglas Gregor 11091fb5c3a6SDouglas Gregor // Consume the feature name. 11101fb5c3a6SDouglas Gregor std::string Feature = Tok.getString(); 11111fb5c3a6SDouglas Gregor consumeToken(); 11121fb5c3a6SDouglas Gregor 11131fb5c3a6SDouglas Gregor // Add this feature. 111489929282SDouglas Gregor ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target); 11151fb5c3a6SDouglas Gregor 11161fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Comma)) 11171fb5c3a6SDouglas Gregor break; 11181fb5c3a6SDouglas Gregor 11191fb5c3a6SDouglas Gregor // Consume the comma. 11201fb5c3a6SDouglas Gregor consumeToken(); 11211fb5c3a6SDouglas Gregor } while (true); 11221fb5c3a6SDouglas Gregor } 11231fb5c3a6SDouglas Gregor 1124f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the 1125f2161a70SDouglas Gregor /// subframework in which the given module lives. 1126bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod, 1127bf8da9d7SBenjamin Kramer llvm::SmallVectorImpl<char> &Path) { 1128f2161a70SDouglas Gregor // Collect the framework names from the given module to the top-level module. 1129f2161a70SDouglas Gregor llvm::SmallVector<StringRef, 2> Paths; 1130f2161a70SDouglas Gregor for (; Mod; Mod = Mod->Parent) { 1131f2161a70SDouglas Gregor if (Mod->IsFramework) 1132f2161a70SDouglas Gregor Paths.push_back(Mod->Name); 1133f2161a70SDouglas Gregor } 1134f2161a70SDouglas Gregor 1135f2161a70SDouglas Gregor if (Paths.empty()) 1136f2161a70SDouglas Gregor return; 1137f2161a70SDouglas Gregor 1138f2161a70SDouglas Gregor // Add Frameworks/Name.framework for each subframework. 1139f2161a70SDouglas Gregor for (unsigned I = Paths.size() - 1; I != 0; --I) { 1140f2161a70SDouglas Gregor llvm::sys::path::append(Path, "Frameworks"); 1141f2161a70SDouglas Gregor llvm::sys::path::append(Path, Paths[I-1] + ".framework"); 1142f2161a70SDouglas Gregor } 1143f2161a70SDouglas Gregor } 1144f2161a70SDouglas Gregor 11453ec6663bSDouglas Gregor /// \brief Determine whether the given file name is the name of a builtin 11463ec6663bSDouglas Gregor /// header, supplied by Clang to replace, override, or augment existing system 11473ec6663bSDouglas Gregor /// headers. 11483ec6663bSDouglas Gregor static bool isBuiltinHeader(StringRef FileName) { 11493ec6663bSDouglas Gregor return llvm::StringSwitch<bool>(FileName) 11503ec6663bSDouglas Gregor .Case("float.h", true) 11513ec6663bSDouglas Gregor .Case("iso646.h", true) 11523ec6663bSDouglas Gregor .Case("limits.h", true) 11533ec6663bSDouglas Gregor .Case("stdalign.h", true) 11543ec6663bSDouglas Gregor .Case("stdarg.h", true) 11553ec6663bSDouglas Gregor .Case("stdbool.h", true) 11563ec6663bSDouglas Gregor .Case("stddef.h", true) 11573ec6663bSDouglas Gregor .Case("stdint.h", true) 11583ec6663bSDouglas Gregor .Case("tgmath.h", true) 11593ec6663bSDouglas Gregor .Case("unwind.h", true) 11603ec6663bSDouglas Gregor .Default(false); 11613ec6663bSDouglas Gregor } 11623ec6663bSDouglas Gregor 1163718292f2SDouglas Gregor /// \brief Parse a header declaration. 1164718292f2SDouglas Gregor /// 1165718292f2SDouglas Gregor /// header-declaration: 1166322f633cSDouglas Gregor /// 'umbrella'[opt] 'header' string-literal 116759527666SDouglas Gregor /// 'exclude'[opt] 'header' string-literal 116859527666SDouglas Gregor void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc, 116959527666SDouglas Gregor SourceLocation ExcludeLoc) { 1170718292f2SDouglas Gregor assert(Tok.is(MMToken::HeaderKeyword)); 11711871ed3dSBenjamin Kramer consumeToken(); 1172718292f2SDouglas Gregor 1173322f633cSDouglas Gregor bool Umbrella = UmbrellaLoc.isValid(); 117459527666SDouglas Gregor bool Exclude = ExcludeLoc.isValid(); 117559527666SDouglas Gregor assert(!(Umbrella && Exclude) && "Cannot have both 'umbrella' and 'exclude'"); 1176718292f2SDouglas Gregor // Parse the header name. 1177718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1178718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1179718292f2SDouglas Gregor << "header"; 1180718292f2SDouglas Gregor HadError = true; 1181718292f2SDouglas Gregor return; 1182718292f2SDouglas Gregor } 1183e7ab3669SDouglas Gregor std::string FileName = Tok.getString(); 1184718292f2SDouglas Gregor SourceLocation FileNameLoc = consumeToken(); 1185718292f2SDouglas Gregor 1186524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1187524e33e1SDouglas Gregor if (Umbrella && ActiveModule->Umbrella) { 1188524e33e1SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash) 1189524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1190322f633cSDouglas Gregor HadError = true; 1191322f633cSDouglas Gregor return; 1192322f633cSDouglas Gregor } 1193322f633cSDouglas Gregor 11945257fc63SDouglas Gregor // Look for this file. 1195e7ab3669SDouglas Gregor const FileEntry *File = 0; 11963ec6663bSDouglas Gregor const FileEntry *BuiltinFile = 0; 11972c1dd271SDylan Noblesmith SmallString<128> PathName; 1198e7ab3669SDouglas Gregor if (llvm::sys::path::is_absolute(FileName)) { 1199e7ab3669SDouglas Gregor PathName = FileName; 1200e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 12017033127bSDouglas Gregor } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) { 12027033127bSDouglas Gregor PathName = Dir->getName(); 12037033127bSDouglas Gregor llvm::sys::path::append(PathName, FileName); 12047033127bSDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1205e7ab3669SDouglas Gregor } else { 1206e7ab3669SDouglas Gregor // Search for the header file within the search directory. 12077033127bSDouglas Gregor PathName = Directory->getName(); 1208e7ab3669SDouglas Gregor unsigned PathLength = PathName.size(); 1209755b2055SDouglas Gregor 1210f2161a70SDouglas Gregor if (ActiveModule->isPartOfFramework()) { 1211f2161a70SDouglas Gregor appendSubframeworkPaths(ActiveModule, PathName); 1212755b2055SDouglas Gregor 1213e7ab3669SDouglas Gregor // Check whether this file is in the public headers. 1214e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, "Headers"); 12155257fc63SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1216e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1217e7ab3669SDouglas Gregor 1218e7ab3669SDouglas Gregor if (!File) { 1219e7ab3669SDouglas Gregor // Check whether this file is in the private headers. 1220e7ab3669SDouglas Gregor PathName.resize(PathLength); 1221e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, "PrivateHeaders"); 1222e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1223e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1224e7ab3669SDouglas Gregor } 1225e7ab3669SDouglas Gregor } else { 1226e7ab3669SDouglas Gregor // Lookup for normal headers. 1227e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1228e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 12293ec6663bSDouglas Gregor 12303ec6663bSDouglas Gregor // If this is a system module with a top-level header, this header 12313ec6663bSDouglas Gregor // may have a counterpart (or replacement) in the set of headers 12323ec6663bSDouglas Gregor // supplied by Clang. Find that builtin header. 12333ec6663bSDouglas Gregor if (ActiveModule->IsSystem && !Umbrella && BuiltinIncludeDir && 12343ec6663bSDouglas Gregor BuiltinIncludeDir != Directory && isBuiltinHeader(FileName)) { 12352c1dd271SDylan Noblesmith SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); 12363ec6663bSDouglas Gregor llvm::sys::path::append(BuiltinPathName, FileName); 12373ec6663bSDouglas Gregor BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); 12383ec6663bSDouglas Gregor 12393ec6663bSDouglas Gregor // If Clang supplies this header but the underlying system does not, 12403ec6663bSDouglas Gregor // just silently swap in our builtin version. Otherwise, we'll end 12413ec6663bSDouglas Gregor // up adding both (later). 12423ec6663bSDouglas Gregor if (!File && BuiltinFile) { 12433ec6663bSDouglas Gregor File = BuiltinFile; 12443ec6663bSDouglas Gregor BuiltinFile = 0; 12453ec6663bSDouglas Gregor } 12463ec6663bSDouglas Gregor } 1247e7ab3669SDouglas Gregor } 1248e7ab3669SDouglas Gregor } 12495257fc63SDouglas Gregor 12505257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 12515257fc63SDouglas Gregor // Come up with a lazy way to do this. 1252e7ab3669SDouglas Gregor if (File) { 125359527666SDouglas Gregor if (ModuleMap::KnownHeader OwningModule = Map.Headers[File]) { 12545257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_conflict) 125559527666SDouglas Gregor << FileName << OwningModule.getModule()->getFullModuleName(); 12565257fc63SDouglas Gregor HadError = true; 1257322f633cSDouglas Gregor } else if (Umbrella) { 1258322f633cSDouglas Gregor const DirectoryEntry *UmbrellaDir = File->getDir(); 125959527666SDouglas Gregor if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { 1260322f633cSDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 126159527666SDouglas Gregor << UmbrellaModule->getFullModuleName(); 1262322f633cSDouglas Gregor HadError = true; 12635257fc63SDouglas Gregor } else { 1264322f633cSDouglas Gregor // Record this umbrella header. 1265322f633cSDouglas Gregor Map.setUmbrellaHeader(ActiveModule, File); 1266322f633cSDouglas Gregor } 1267322f633cSDouglas Gregor } else { 1268322f633cSDouglas Gregor // Record this header. 126959527666SDouglas Gregor Map.addHeader(ActiveModule, File, Exclude); 12703ec6663bSDouglas Gregor 12713ec6663bSDouglas Gregor // If there is a builtin counterpart to this file, add it now. 12723ec6663bSDouglas Gregor if (BuiltinFile) 127359527666SDouglas Gregor Map.addHeader(ActiveModule, BuiltinFile, Exclude); 12745257fc63SDouglas Gregor } 12755257fc63SDouglas Gregor } else { 12765257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_not_found) 1277524e33e1SDouglas Gregor << Umbrella << FileName; 12785257fc63SDouglas Gregor HadError = true; 12795257fc63SDouglas Gregor } 1280718292f2SDouglas Gregor } 1281718292f2SDouglas Gregor 1282524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration. 1283524e33e1SDouglas Gregor /// 1284524e33e1SDouglas Gregor /// umbrella-dir-declaration: 1285524e33e1SDouglas Gregor /// umbrella string-literal 1286524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1287524e33e1SDouglas Gregor // Parse the directory name. 1288524e33e1SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1289524e33e1SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1290524e33e1SDouglas Gregor << "umbrella"; 1291524e33e1SDouglas Gregor HadError = true; 1292524e33e1SDouglas Gregor return; 1293524e33e1SDouglas Gregor } 1294524e33e1SDouglas Gregor 1295524e33e1SDouglas Gregor std::string DirName = Tok.getString(); 1296524e33e1SDouglas Gregor SourceLocation DirNameLoc = consumeToken(); 1297524e33e1SDouglas Gregor 1298524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1299524e33e1SDouglas Gregor if (ActiveModule->Umbrella) { 1300524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1301524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1302524e33e1SDouglas Gregor HadError = true; 1303524e33e1SDouglas Gregor return; 1304524e33e1SDouglas Gregor } 1305524e33e1SDouglas Gregor 1306524e33e1SDouglas Gregor // Look for this file. 1307524e33e1SDouglas Gregor const DirectoryEntry *Dir = 0; 1308524e33e1SDouglas Gregor if (llvm::sys::path::is_absolute(DirName)) 1309524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(DirName); 1310524e33e1SDouglas Gregor else { 13112c1dd271SDylan Noblesmith SmallString<128> PathName; 1312524e33e1SDouglas Gregor PathName = Directory->getName(); 1313524e33e1SDouglas Gregor llvm::sys::path::append(PathName, DirName); 1314524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(PathName); 1315524e33e1SDouglas Gregor } 1316524e33e1SDouglas Gregor 1317524e33e1SDouglas Gregor if (!Dir) { 1318524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1319524e33e1SDouglas Gregor << DirName; 1320524e33e1SDouglas Gregor HadError = true; 1321524e33e1SDouglas Gregor return; 1322524e33e1SDouglas Gregor } 1323524e33e1SDouglas Gregor 1324524e33e1SDouglas Gregor if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1325524e33e1SDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1326524e33e1SDouglas Gregor << OwningModule->getFullModuleName(); 1327524e33e1SDouglas Gregor HadError = true; 1328524e33e1SDouglas Gregor return; 1329524e33e1SDouglas Gregor } 1330524e33e1SDouglas Gregor 1331524e33e1SDouglas Gregor // Record this umbrella directory. 1332524e33e1SDouglas Gregor Map.setUmbrellaDir(ActiveModule, Dir); 1333524e33e1SDouglas Gregor } 1334524e33e1SDouglas Gregor 13352b82c2a5SDouglas Gregor /// \brief Parse a module export declaration. 13362b82c2a5SDouglas Gregor /// 13372b82c2a5SDouglas Gregor /// export-declaration: 13382b82c2a5SDouglas Gregor /// 'export' wildcard-module-id 13392b82c2a5SDouglas Gregor /// 13402b82c2a5SDouglas Gregor /// wildcard-module-id: 13412b82c2a5SDouglas Gregor /// identifier 13422b82c2a5SDouglas Gregor /// '*' 13432b82c2a5SDouglas Gregor /// identifier '.' wildcard-module-id 13442b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() { 13452b82c2a5SDouglas Gregor assert(Tok.is(MMToken::ExportKeyword)); 13462b82c2a5SDouglas Gregor SourceLocation ExportLoc = consumeToken(); 13472b82c2a5SDouglas Gregor 13482b82c2a5SDouglas Gregor // Parse the module-id with an optional wildcard at the end. 13492b82c2a5SDouglas Gregor ModuleId ParsedModuleId; 13502b82c2a5SDouglas Gregor bool Wildcard = false; 13512b82c2a5SDouglas Gregor do { 13522b82c2a5SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 13532b82c2a5SDouglas Gregor ParsedModuleId.push_back(std::make_pair(Tok.getString(), 13542b82c2a5SDouglas Gregor Tok.getLocation())); 13552b82c2a5SDouglas Gregor consumeToken(); 13562b82c2a5SDouglas Gregor 13572b82c2a5SDouglas Gregor if (Tok.is(MMToken::Period)) { 13582b82c2a5SDouglas Gregor consumeToken(); 13592b82c2a5SDouglas Gregor continue; 13602b82c2a5SDouglas Gregor } 13612b82c2a5SDouglas Gregor 13622b82c2a5SDouglas Gregor break; 13632b82c2a5SDouglas Gregor } 13642b82c2a5SDouglas Gregor 13652b82c2a5SDouglas Gregor if(Tok.is(MMToken::Star)) { 13662b82c2a5SDouglas Gregor Wildcard = true; 1367f5eedd05SDouglas Gregor consumeToken(); 13682b82c2a5SDouglas Gregor break; 13692b82c2a5SDouglas Gregor } 13702b82c2a5SDouglas Gregor 13712b82c2a5SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id); 13722b82c2a5SDouglas Gregor HadError = true; 13732b82c2a5SDouglas Gregor return; 13742b82c2a5SDouglas Gregor } while (true); 13752b82c2a5SDouglas Gregor 13762b82c2a5SDouglas Gregor Module::UnresolvedExportDecl Unresolved = { 13772b82c2a5SDouglas Gregor ExportLoc, ParsedModuleId, Wildcard 13782b82c2a5SDouglas Gregor }; 13792b82c2a5SDouglas Gregor ActiveModule->UnresolvedExports.push_back(Unresolved); 13802b82c2a5SDouglas Gregor } 13812b82c2a5SDouglas Gregor 138273441091SDouglas Gregor void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) { 138373441091SDouglas Gregor assert(Tok.is(MMToken::Star)); 138473441091SDouglas Gregor SourceLocation StarLoc = consumeToken(); 138573441091SDouglas Gregor bool Failed = false; 138673441091SDouglas Gregor 138773441091SDouglas Gregor // Inferred modules must be submodules. 138873441091SDouglas Gregor if (!ActiveModule) { 138973441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 139073441091SDouglas Gregor Failed = true; 139173441091SDouglas Gregor } 139273441091SDouglas Gregor 1393524e33e1SDouglas Gregor // Inferred modules must have umbrella directories. 1394524e33e1SDouglas Gregor if (!Failed && !ActiveModule->getUmbrellaDir()) { 139573441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 139673441091SDouglas Gregor Failed = true; 139773441091SDouglas Gregor } 139873441091SDouglas Gregor 139973441091SDouglas Gregor // Check for redefinition of an inferred module. 1400dd005f69SDouglas Gregor if (!Failed && ActiveModule->InferSubmodules) { 140173441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 1402dd005f69SDouglas Gregor if (ActiveModule->InferredSubmoduleLoc.isValid()) 1403dd005f69SDouglas Gregor Diags.Report(ActiveModule->InferredSubmoduleLoc, 140473441091SDouglas Gregor diag::note_mmap_prev_definition); 140573441091SDouglas Gregor Failed = true; 140673441091SDouglas Gregor } 140773441091SDouglas Gregor 140873441091SDouglas Gregor // If there were any problems with this inferred submodule, skip its body. 140973441091SDouglas Gregor if (Failed) { 141073441091SDouglas Gregor if (Tok.is(MMToken::LBrace)) { 141173441091SDouglas Gregor consumeToken(); 141273441091SDouglas Gregor skipUntil(MMToken::RBrace); 141373441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 141473441091SDouglas Gregor consumeToken(); 141573441091SDouglas Gregor } 141673441091SDouglas Gregor HadError = true; 141773441091SDouglas Gregor return; 141873441091SDouglas Gregor } 141973441091SDouglas Gregor 142073441091SDouglas Gregor // Note that we have an inferred submodule. 1421dd005f69SDouglas Gregor ActiveModule->InferSubmodules = true; 1422dd005f69SDouglas Gregor ActiveModule->InferredSubmoduleLoc = StarLoc; 1423dd005f69SDouglas Gregor ActiveModule->InferExplicitSubmodules = Explicit; 142473441091SDouglas Gregor 142573441091SDouglas Gregor // Parse the opening brace. 142673441091SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 142773441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 142873441091SDouglas Gregor HadError = true; 142973441091SDouglas Gregor return; 143073441091SDouglas Gregor } 143173441091SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 143273441091SDouglas Gregor 143373441091SDouglas Gregor // Parse the body of the inferred submodule. 143473441091SDouglas Gregor bool Done = false; 143573441091SDouglas Gregor do { 143673441091SDouglas Gregor switch (Tok.Kind) { 143773441091SDouglas Gregor case MMToken::EndOfFile: 143873441091SDouglas Gregor case MMToken::RBrace: 143973441091SDouglas Gregor Done = true; 144073441091SDouglas Gregor break; 144173441091SDouglas Gregor 144273441091SDouglas Gregor case MMToken::ExportKeyword: { 144373441091SDouglas Gregor consumeToken(); 144473441091SDouglas Gregor if (Tok.is(MMToken::Star)) 1445dd005f69SDouglas Gregor ActiveModule->InferExportWildcard = true; 144673441091SDouglas Gregor else 144773441091SDouglas Gregor Diags.Report(Tok.getLocation(), 144873441091SDouglas Gregor diag::err_mmap_expected_export_wildcard); 144973441091SDouglas Gregor consumeToken(); 145073441091SDouglas Gregor break; 145173441091SDouglas Gregor } 145273441091SDouglas Gregor 145373441091SDouglas Gregor case MMToken::ExplicitKeyword: 145473441091SDouglas Gregor case MMToken::ModuleKeyword: 145573441091SDouglas Gregor case MMToken::HeaderKeyword: 145673441091SDouglas Gregor case MMToken::UmbrellaKeyword: 145773441091SDouglas Gregor default: 145873441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member); 145973441091SDouglas Gregor consumeToken(); 146073441091SDouglas Gregor break; 146173441091SDouglas Gregor } 146273441091SDouglas Gregor } while (!Done); 146373441091SDouglas Gregor 146473441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 146573441091SDouglas Gregor consumeToken(); 146673441091SDouglas Gregor else { 146773441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 146873441091SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 146973441091SDouglas Gregor HadError = true; 147073441091SDouglas Gregor } 147173441091SDouglas Gregor } 147273441091SDouglas Gregor 14737033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence 14747033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null. 14757033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() { 14767033127bSDouglas Gregor for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) { 14777033127bSDouglas Gregor // If we have an umbrella directory, use that. 14787033127bSDouglas Gregor if (Mod->hasUmbrellaDir()) 14797033127bSDouglas Gregor return Mod->getUmbrellaDir(); 14807033127bSDouglas Gregor 14817033127bSDouglas Gregor // If we have a framework directory, stop looking. 14827033127bSDouglas Gregor if (Mod->IsFramework) 14837033127bSDouglas Gregor return 0; 14847033127bSDouglas Gregor } 14857033127bSDouglas Gregor 14867033127bSDouglas Gregor return 0; 14877033127bSDouglas Gregor } 14887033127bSDouglas Gregor 1489718292f2SDouglas Gregor /// \brief Parse a module map file. 1490718292f2SDouglas Gregor /// 1491718292f2SDouglas Gregor /// module-map-file: 1492718292f2SDouglas Gregor /// module-declaration* 1493718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() { 1494718292f2SDouglas Gregor do { 1495718292f2SDouglas Gregor switch (Tok.Kind) { 1496718292f2SDouglas Gregor case MMToken::EndOfFile: 1497718292f2SDouglas Gregor return HadError; 1498718292f2SDouglas Gregor 1499e7ab3669SDouglas Gregor case MMToken::ExplicitKeyword: 1500718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1501755b2055SDouglas Gregor case MMToken::FrameworkKeyword: 1502718292f2SDouglas Gregor parseModuleDecl(); 1503718292f2SDouglas Gregor break; 1504718292f2SDouglas Gregor 15051fb5c3a6SDouglas Gregor case MMToken::Comma: 150659527666SDouglas Gregor case MMToken::ExcludeKeyword: 15072b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 1508718292f2SDouglas Gregor case MMToken::HeaderKeyword: 1509718292f2SDouglas Gregor case MMToken::Identifier: 1510718292f2SDouglas Gregor case MMToken::LBrace: 1511a686e1b0SDouglas Gregor case MMToken::LSquare: 15122b82c2a5SDouglas Gregor case MMToken::Period: 1513718292f2SDouglas Gregor case MMToken::RBrace: 1514a686e1b0SDouglas Gregor case MMToken::RSquare: 15151fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 15162b82c2a5SDouglas Gregor case MMToken::Star: 1517718292f2SDouglas Gregor case MMToken::StringLiteral: 1518718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 1519718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1520718292f2SDouglas Gregor HadError = true; 1521718292f2SDouglas Gregor consumeToken(); 1522718292f2SDouglas Gregor break; 1523718292f2SDouglas Gregor } 1524718292f2SDouglas Gregor } while (true); 1525718292f2SDouglas Gregor } 1526718292f2SDouglas Gregor 1527718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) { 152889929282SDouglas Gregor assert(Target != 0 && "Missing target information"); 1529718292f2SDouglas Gregor FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User); 1530718292f2SDouglas Gregor const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID); 1531718292f2SDouglas Gregor if (!Buffer) 1532718292f2SDouglas Gregor return true; 1533718292f2SDouglas Gregor 1534718292f2SDouglas Gregor // Parse this module map file. 15351fb5c3a6SDouglas Gregor Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts); 15361fb5c3a6SDouglas Gregor Diags->getClient()->BeginSourceFile(MMapLangOpts); 1537bc10b9fbSDouglas Gregor ModuleMapParser Parser(L, *SourceMgr, Target, *Diags, *this, File->getDir(), 15383ec6663bSDouglas Gregor BuiltinIncludeDir); 1539718292f2SDouglas Gregor bool Result = Parser.parseModuleMapFile(); 1540718292f2SDouglas Gregor Diags->getClient()->EndSourceFile(); 1541718292f2SDouglas Gregor 1542718292f2SDouglas Gregor return Result; 1543718292f2SDouglas Gregor } 1544