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" 19718292f2SDouglas Gregor #include "clang/Basic/FileManager.h" 20718292f2SDouglas Gregor #include "clang/Basic/TargetInfo.h" 21718292f2SDouglas Gregor #include "clang/Basic/TargetOptions.h" 22718292f2SDouglas Gregor #include "llvm/Support/Allocator.h" 23e89dbc1dSDouglas Gregor #include "llvm/Support/FileSystem.h" 24718292f2SDouglas Gregor #include "llvm/Support/Host.h" 255257fc63SDouglas Gregor #include "llvm/Support/PathV2.h" 26718292f2SDouglas Gregor #include "llvm/Support/raw_ostream.h" 27718292f2SDouglas Gregor #include "llvm/ADT/StringRef.h" 28718292f2SDouglas Gregor #include "llvm/ADT/StringSwitch.h" 2907c22b78SDouglas Gregor #include <stdlib.h> 30718292f2SDouglas Gregor using namespace clang; 31718292f2SDouglas Gregor 322b82c2a5SDouglas Gregor Module::ExportDecl 332b82c2a5SDouglas Gregor ModuleMap::resolveExport(Module *Mod, 342b82c2a5SDouglas Gregor const Module::UnresolvedExportDecl &Unresolved, 352b82c2a5SDouglas Gregor bool Complain) { 36f5eedd05SDouglas Gregor // We may have just a wildcard. 37f5eedd05SDouglas Gregor if (Unresolved.Id.empty()) { 38f5eedd05SDouglas Gregor assert(Unresolved.Wildcard && "Invalid unresolved export"); 39f5eedd05SDouglas Gregor return Module::ExportDecl(0, true); 40f5eedd05SDouglas Gregor } 41f5eedd05SDouglas Gregor 422b82c2a5SDouglas Gregor // Find the starting module. 432b82c2a5SDouglas Gregor Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod); 442b82c2a5SDouglas Gregor if (!Context) { 452b82c2a5SDouglas Gregor if (Complain) 462b82c2a5SDouglas Gregor Diags->Report(Unresolved.Id[0].second, 472b82c2a5SDouglas Gregor diag::err_mmap_missing_module_unqualified) 482b82c2a5SDouglas Gregor << Unresolved.Id[0].first << Mod->getFullModuleName(); 492b82c2a5SDouglas Gregor 502b82c2a5SDouglas Gregor return Module::ExportDecl(); 512b82c2a5SDouglas Gregor } 522b82c2a5SDouglas Gregor 532b82c2a5SDouglas Gregor // Dig into the module path. 542b82c2a5SDouglas Gregor for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) { 552b82c2a5SDouglas Gregor Module *Sub = lookupModuleQualified(Unresolved.Id[I].first, 562b82c2a5SDouglas Gregor Context); 572b82c2a5SDouglas Gregor if (!Sub) { 582b82c2a5SDouglas Gregor if (Complain) 592b82c2a5SDouglas Gregor Diags->Report(Unresolved.Id[I].second, 602b82c2a5SDouglas Gregor diag::err_mmap_missing_module_qualified) 612b82c2a5SDouglas Gregor << Unresolved.Id[I].first << Context->getFullModuleName() 622b82c2a5SDouglas Gregor << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second); 632b82c2a5SDouglas Gregor 642b82c2a5SDouglas Gregor return Module::ExportDecl(); 652b82c2a5SDouglas Gregor } 662b82c2a5SDouglas Gregor 672b82c2a5SDouglas Gregor Context = Sub; 682b82c2a5SDouglas Gregor } 692b82c2a5SDouglas Gregor 702b82c2a5SDouglas Gregor return Module::ExportDecl(Context, Unresolved.Wildcard); 712b82c2a5SDouglas Gregor } 722b82c2a5SDouglas Gregor 731fb5c3a6SDouglas Gregor ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC, 7489929282SDouglas Gregor const LangOptions &LangOpts, const TargetInfo *Target) 753ec6663bSDouglas Gregor : LangOpts(LangOpts), Target(Target), BuiltinIncludeDir(0) 761fb5c3a6SDouglas Gregor { 77c95d8192SDylan Noblesmith IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs); 78c95d8192SDylan Noblesmith Diags = IntrusiveRefCntPtr<DiagnosticsEngine>( 79718292f2SDouglas Gregor new DiagnosticsEngine(DiagIDs)); 80718292f2SDouglas Gregor Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true); 81718292f2SDouglas Gregor SourceMgr = new SourceManager(*Diags, FileMgr); 82718292f2SDouglas Gregor } 83718292f2SDouglas Gregor 84718292f2SDouglas Gregor ModuleMap::~ModuleMap() { 855acdf59eSDouglas Gregor for (llvm::StringMap<Module *>::iterator I = Modules.begin(), 865acdf59eSDouglas Gregor IEnd = Modules.end(); 875acdf59eSDouglas Gregor I != IEnd; ++I) { 885acdf59eSDouglas Gregor delete I->getValue(); 895acdf59eSDouglas Gregor } 905acdf59eSDouglas Gregor 91718292f2SDouglas Gregor delete SourceMgr; 92718292f2SDouglas Gregor } 93718292f2SDouglas Gregor 9489929282SDouglas Gregor void ModuleMap::setTarget(const TargetInfo &Target) { 9589929282SDouglas Gregor assert((!this->Target || this->Target == &Target) && 9689929282SDouglas Gregor "Improper target override"); 9789929282SDouglas Gregor this->Target = &Target; 9889929282SDouglas Gregor } 9989929282SDouglas Gregor 100056396aeSDouglas Gregor /// \brief "Sanitize" a filename so that it can be used as an identifier. 101056396aeSDouglas Gregor static StringRef sanitizeFilenameAsIdentifier(StringRef Name, 102056396aeSDouglas Gregor SmallVectorImpl<char> &Buffer) { 103056396aeSDouglas Gregor if (Name.empty()) 104056396aeSDouglas Gregor return Name; 105056396aeSDouglas Gregor 106056396aeSDouglas Gregor // Check whether the filename is already an identifier; this is the common 107056396aeSDouglas Gregor // case. 108056396aeSDouglas Gregor bool isIdentifier = true; 109056396aeSDouglas Gregor for (unsigned I = 0, N = Name.size(); I != N; ++I) { 110056396aeSDouglas Gregor if (isalpha(Name[I]) || Name[I] == '_' || (isdigit(Name[I]) && I > 0)) 111056396aeSDouglas Gregor continue; 112056396aeSDouglas Gregor 113056396aeSDouglas Gregor isIdentifier = false; 114056396aeSDouglas Gregor break; 115056396aeSDouglas Gregor } 116056396aeSDouglas Gregor 117056396aeSDouglas Gregor if (!isIdentifier) { 118056396aeSDouglas Gregor // If we don't already have something with the form of an identifier, 119056396aeSDouglas Gregor // create a buffer with the sanitized name. 120056396aeSDouglas Gregor Buffer.clear(); 121056396aeSDouglas Gregor if (isdigit(Name[0])) 122056396aeSDouglas Gregor Buffer.push_back('_'); 123056396aeSDouglas Gregor Buffer.reserve(Buffer.size() + Name.size()); 124056396aeSDouglas Gregor for (unsigned I = 0, N = Name.size(); I != N; ++I) { 125056396aeSDouglas Gregor if (isalnum(Name[I]) || isspace(Name[I])) 126056396aeSDouglas Gregor Buffer.push_back(Name[I]); 127056396aeSDouglas Gregor else 128056396aeSDouglas Gregor Buffer.push_back('_'); 129056396aeSDouglas Gregor } 130056396aeSDouglas Gregor 131056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 132056396aeSDouglas Gregor } 133056396aeSDouglas Gregor 134056396aeSDouglas Gregor while (llvm::StringSwitch<bool>(Name) 135056396aeSDouglas Gregor #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true) 136056396aeSDouglas Gregor #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true) 137056396aeSDouglas Gregor #include "clang/Basic/TokenKinds.def" 138056396aeSDouglas Gregor .Default(false)) { 139056396aeSDouglas Gregor if (Name.data() != Buffer.data()) 140056396aeSDouglas Gregor Buffer.append(Name.begin(), Name.end()); 141056396aeSDouglas Gregor Buffer.push_back('_'); 142056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 143056396aeSDouglas Gregor } 144056396aeSDouglas Gregor 145056396aeSDouglas Gregor return Name; 146056396aeSDouglas Gregor } 147056396aeSDouglas Gregor 148de3ef502SDouglas Gregor Module *ModuleMap::findModuleForHeader(const FileEntry *File) { 14959527666SDouglas Gregor HeadersMap::iterator Known = Headers.find(File); 1501fb5c3a6SDouglas Gregor if (Known != Headers.end()) { 15159527666SDouglas Gregor // If a header is not available, don't report that it maps to anything. 15259527666SDouglas Gregor if (!Known->second.isAvailable()) 1531fb5c3a6SDouglas Gregor return 0; 1541fb5c3a6SDouglas Gregor 15559527666SDouglas Gregor return Known->second.getModule(); 1561fb5c3a6SDouglas Gregor } 157ab0c8a84SDouglas Gregor 158b65dbfffSDouglas Gregor const DirectoryEntry *Dir = File->getDir(); 159b65dbfffSDouglas Gregor llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs; 160b65dbfffSDouglas Gregor StringRef DirName = Dir->getName(); 161a89c5ac4SDouglas Gregor 162a89c5ac4SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 163a89c5ac4SDouglas Gregor // an umbrella header. 164b65dbfffSDouglas Gregor do { 165a89c5ac4SDouglas Gregor llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir 166a89c5ac4SDouglas Gregor = UmbrellaDirs.find(Dir); 167a89c5ac4SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 168a89c5ac4SDouglas Gregor Module *Result = KnownDir->second; 169930a85ccSDouglas Gregor 170930a85ccSDouglas Gregor // Search up the module stack until we find a module with an umbrella 17173141fa9SDouglas Gregor // directory. 172930a85ccSDouglas Gregor Module *UmbrellaModule = Result; 17373141fa9SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 174930a85ccSDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 175930a85ccSDouglas Gregor 176930a85ccSDouglas Gregor if (UmbrellaModule->InferSubmodules) { 177a89c5ac4SDouglas Gregor // Infer submodules for each of the directories we found between 178a89c5ac4SDouglas Gregor // the directory of the umbrella header and the directory where 179a89c5ac4SDouglas Gregor // the actual header is located. 1809458f82dSDouglas Gregor bool Explicit = UmbrellaModule->InferExplicitSubmodules; 1819458f82dSDouglas Gregor 1827033127bSDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 183a89c5ac4SDouglas Gregor // Find or create the module that corresponds to this directory name. 184056396aeSDouglas Gregor SmallString<32> NameBuf; 185056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 186056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 187056396aeSDouglas Gregor NameBuf); 188a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 1899458f82dSDouglas Gregor Explicit).first; 190a89c5ac4SDouglas Gregor 191a89c5ac4SDouglas Gregor // Associate the module and the directory. 192a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I-1]] = Result; 193a89c5ac4SDouglas Gregor 194a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 195a89c5ac4SDouglas Gregor // wildcard to the set of exports. 196930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 197a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 198a89c5ac4SDouglas Gregor } 199a89c5ac4SDouglas Gregor 200a89c5ac4SDouglas Gregor // Infer a submodule with the same name as this header file. 201056396aeSDouglas Gregor SmallString<32> NameBuf; 202056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 203056396aeSDouglas Gregor llvm::sys::path::stem(File->getName()), NameBuf); 204a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 2059458f82dSDouglas Gregor Explicit).first; 206c597c8c4SArgyrios Kyrtzidis Result->TopHeaders.insert(File); 207a89c5ac4SDouglas Gregor 208a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 209a89c5ac4SDouglas Gregor // wildcard to the set of exports. 210930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 211a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 212a89c5ac4SDouglas Gregor } else { 213a89c5ac4SDouglas Gregor // Record each of the directories we stepped through as being part of 214a89c5ac4SDouglas Gregor // the module we found, since the umbrella header covers them all. 215a89c5ac4SDouglas Gregor for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 216a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I]] = Result; 217a89c5ac4SDouglas Gregor } 218a89c5ac4SDouglas Gregor 21959527666SDouglas Gregor Headers[File] = KnownHeader(Result, /*Excluded=*/false); 2201fb5c3a6SDouglas Gregor 2211fb5c3a6SDouglas Gregor // If a header corresponds to an unavailable module, don't report 2221fb5c3a6SDouglas Gregor // that it maps to anything. 2231fb5c3a6SDouglas Gregor if (!Result->isAvailable()) 2241fb5c3a6SDouglas Gregor return 0; 2251fb5c3a6SDouglas Gregor 226a89c5ac4SDouglas Gregor return Result; 227a89c5ac4SDouglas Gregor } 228a89c5ac4SDouglas Gregor 229a89c5ac4SDouglas Gregor SkippedDirs.push_back(Dir); 230a89c5ac4SDouglas Gregor 231b65dbfffSDouglas Gregor // Retrieve our parent path. 232b65dbfffSDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 233b65dbfffSDouglas Gregor if (DirName.empty()) 234b65dbfffSDouglas Gregor break; 235b65dbfffSDouglas Gregor 236b65dbfffSDouglas Gregor // Resolve the parent path to a directory entry. 237b65dbfffSDouglas Gregor Dir = SourceMgr->getFileManager().getDirectory(DirName); 238a89c5ac4SDouglas Gregor } while (Dir); 239b65dbfffSDouglas Gregor 240ab0c8a84SDouglas Gregor return 0; 241ab0c8a84SDouglas Gregor } 242ab0c8a84SDouglas Gregor 2431fb5c3a6SDouglas Gregor bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) { 24459527666SDouglas Gregor HeadersMap::iterator Known = Headers.find(Header); 2451fb5c3a6SDouglas Gregor if (Known != Headers.end()) 24659527666SDouglas Gregor return !Known->second.isAvailable(); 2471fb5c3a6SDouglas Gregor 2481fb5c3a6SDouglas Gregor const DirectoryEntry *Dir = Header->getDir(); 2491fb5c3a6SDouglas Gregor llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs; 2501fb5c3a6SDouglas Gregor StringRef DirName = Dir->getName(); 2511fb5c3a6SDouglas Gregor 2521fb5c3a6SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 2531fb5c3a6SDouglas Gregor // an umbrella header. 2541fb5c3a6SDouglas Gregor do { 2551fb5c3a6SDouglas Gregor llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir 2561fb5c3a6SDouglas Gregor = UmbrellaDirs.find(Dir); 2571fb5c3a6SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 2581fb5c3a6SDouglas Gregor Module *Found = KnownDir->second; 2591fb5c3a6SDouglas Gregor if (!Found->isAvailable()) 2601fb5c3a6SDouglas Gregor return true; 2611fb5c3a6SDouglas Gregor 2621fb5c3a6SDouglas Gregor // Search up the module stack until we find a module with an umbrella 2631fb5c3a6SDouglas Gregor // directory. 2641fb5c3a6SDouglas Gregor Module *UmbrellaModule = Found; 2651fb5c3a6SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 2661fb5c3a6SDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 2671fb5c3a6SDouglas Gregor 2681fb5c3a6SDouglas Gregor if (UmbrellaModule->InferSubmodules) { 2691fb5c3a6SDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 2701fb5c3a6SDouglas Gregor // Find or create the module that corresponds to this directory name. 271056396aeSDouglas Gregor SmallString<32> NameBuf; 272056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 273056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 274056396aeSDouglas Gregor NameBuf); 2751fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 2761fb5c3a6SDouglas Gregor if (!Found) 2771fb5c3a6SDouglas Gregor return false; 2781fb5c3a6SDouglas Gregor if (!Found->isAvailable()) 2791fb5c3a6SDouglas Gregor return true; 2801fb5c3a6SDouglas Gregor } 2811fb5c3a6SDouglas Gregor 2821fb5c3a6SDouglas Gregor // Infer a submodule with the same name as this header file. 283056396aeSDouglas Gregor SmallString<32> NameBuf; 284056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 285056396aeSDouglas Gregor llvm::sys::path::stem(Header->getName()), 286056396aeSDouglas Gregor NameBuf); 2871fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 2881fb5c3a6SDouglas Gregor if (!Found) 2891fb5c3a6SDouglas Gregor return false; 2901fb5c3a6SDouglas Gregor } 2911fb5c3a6SDouglas Gregor 2921fb5c3a6SDouglas Gregor return !Found->isAvailable(); 2931fb5c3a6SDouglas Gregor } 2941fb5c3a6SDouglas Gregor 2951fb5c3a6SDouglas Gregor SkippedDirs.push_back(Dir); 2961fb5c3a6SDouglas Gregor 2971fb5c3a6SDouglas Gregor // Retrieve our parent path. 2981fb5c3a6SDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 2991fb5c3a6SDouglas Gregor if (DirName.empty()) 3001fb5c3a6SDouglas Gregor break; 3011fb5c3a6SDouglas Gregor 3021fb5c3a6SDouglas Gregor // Resolve the parent path to a directory entry. 3031fb5c3a6SDouglas Gregor Dir = SourceMgr->getFileManager().getDirectory(DirName); 3041fb5c3a6SDouglas Gregor } while (Dir); 3051fb5c3a6SDouglas Gregor 3061fb5c3a6SDouglas Gregor return false; 3071fb5c3a6SDouglas Gregor } 3081fb5c3a6SDouglas Gregor 309de3ef502SDouglas Gregor Module *ModuleMap::findModule(StringRef Name) { 31088bdfb0eSDouglas Gregor llvm::StringMap<Module *>::iterator Known = Modules.find(Name); 31188bdfb0eSDouglas Gregor if (Known != Modules.end()) 31288bdfb0eSDouglas Gregor return Known->getValue(); 31388bdfb0eSDouglas Gregor 31488bdfb0eSDouglas Gregor return 0; 31588bdfb0eSDouglas Gregor } 31688bdfb0eSDouglas Gregor 3172b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) { 3182b82c2a5SDouglas Gregor for(; Context; Context = Context->Parent) { 3192b82c2a5SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Context)) 3202b82c2a5SDouglas Gregor return Sub; 3212b82c2a5SDouglas Gregor } 3222b82c2a5SDouglas Gregor 3232b82c2a5SDouglas Gregor return findModule(Name); 3242b82c2a5SDouglas Gregor } 3252b82c2a5SDouglas Gregor 3262b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) { 3272b82c2a5SDouglas Gregor if (!Context) 3282b82c2a5SDouglas Gregor return findModule(Name); 3292b82c2a5SDouglas Gregor 330eb90e830SDouglas Gregor return Context->findSubmodule(Name); 3312b82c2a5SDouglas Gregor } 3322b82c2a5SDouglas Gregor 333de3ef502SDouglas Gregor std::pair<Module *, bool> 33469021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 33569021974SDouglas Gregor bool IsExplicit) { 33669021974SDouglas Gregor // Try to find an existing module with this name. 337eb90e830SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Parent)) 338eb90e830SDouglas Gregor return std::make_pair(Sub, false); 33969021974SDouglas Gregor 34069021974SDouglas Gregor // Create a new module with this name. 34169021974SDouglas Gregor Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework, 34269021974SDouglas Gregor IsExplicit); 343eb90e830SDouglas Gregor if (!Parent) 34469021974SDouglas Gregor Modules[Name] = Result; 34569021974SDouglas Gregor return std::make_pair(Result, true); 34669021974SDouglas Gregor } 34769021974SDouglas Gregor 348de3ef502SDouglas Gregor Module * 34956c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName, 350e89dbc1dSDouglas Gregor const DirectoryEntry *FrameworkDir, 351a686e1b0SDouglas Gregor bool IsSystem, 352e89dbc1dSDouglas Gregor Module *Parent) { 35356c64013SDouglas Gregor // Check whether we've already found this module. 354e89dbc1dSDouglas Gregor if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 355e89dbc1dSDouglas Gregor return Mod; 356e89dbc1dSDouglas Gregor 357e89dbc1dSDouglas Gregor FileManager &FileMgr = SourceMgr->getFileManager(); 35856c64013SDouglas Gregor 35956c64013SDouglas Gregor // Look for an umbrella header. 3602c1dd271SDylan Noblesmith SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 36156c64013SDouglas Gregor llvm::sys::path::append(UmbrellaName, "Headers"); 36256c64013SDouglas Gregor llvm::sys::path::append(UmbrellaName, ModuleName + ".h"); 363e89dbc1dSDouglas Gregor const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 36456c64013SDouglas Gregor 36556c64013SDouglas Gregor // FIXME: If there's no umbrella header, we could probably scan the 36656c64013SDouglas Gregor // framework to load *everything*. But, it's not clear that this is a good 36756c64013SDouglas Gregor // idea. 36856c64013SDouglas Gregor if (!UmbrellaHeader) 36956c64013SDouglas Gregor return 0; 37056c64013SDouglas Gregor 371e89dbc1dSDouglas Gregor Module *Result = new Module(ModuleName, SourceLocation(), Parent, 372e89dbc1dSDouglas Gregor /*IsFramework=*/true, /*IsExplicit=*/false); 373a686e1b0SDouglas Gregor if (IsSystem) 374a686e1b0SDouglas Gregor Result->IsSystem = IsSystem; 375a686e1b0SDouglas Gregor 376eb90e830SDouglas Gregor if (!Parent) 377e89dbc1dSDouglas Gregor Modules[ModuleName] = Result; 378e89dbc1dSDouglas Gregor 379322f633cSDouglas Gregor // umbrella header "umbrella-header-name" 38073141fa9SDouglas Gregor Result->Umbrella = UmbrellaHeader; 38159527666SDouglas Gregor Headers[UmbrellaHeader] = KnownHeader(Result, /*Excluded=*/false); 3824dc71835SDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Result; 383d8bd7537SDouglas Gregor 384d8bd7537SDouglas Gregor // export * 385d8bd7537SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 386d8bd7537SDouglas Gregor 387a89c5ac4SDouglas Gregor // module * { export * } 388a89c5ac4SDouglas Gregor Result->InferSubmodules = true; 389a89c5ac4SDouglas Gregor Result->InferExportWildcard = true; 390a89c5ac4SDouglas Gregor 391e89dbc1dSDouglas Gregor // Look for subframeworks. 392e89dbc1dSDouglas Gregor llvm::error_code EC; 3932c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirName 394ddaa69cbSDouglas Gregor = StringRef(FrameworkDir->getName()); 395e89dbc1dSDouglas Gregor llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 3962c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirNameNative; 397ddaa69cbSDouglas Gregor llvm::sys::path::native(SubframeworksDirName.str(), 398ddaa69cbSDouglas Gregor SubframeworksDirNameNative); 399ddaa69cbSDouglas Gregor for (llvm::sys::fs::directory_iterator 400ddaa69cbSDouglas Gregor Dir(SubframeworksDirNameNative.str(), EC), DirEnd; 401e89dbc1dSDouglas Gregor Dir != DirEnd && !EC; Dir.increment(EC)) { 402e89dbc1dSDouglas Gregor if (!StringRef(Dir->path()).endswith(".framework")) 403e89dbc1dSDouglas Gregor continue; 404f2161a70SDouglas Gregor 405e89dbc1dSDouglas Gregor if (const DirectoryEntry *SubframeworkDir 406e89dbc1dSDouglas Gregor = FileMgr.getDirectory(Dir->path())) { 40707c22b78SDouglas Gregor // Note: as an egregious but useful hack, we use the real path here and 40807c22b78SDouglas Gregor // check whether it is actually a subdirectory of the parent directory. 40907c22b78SDouglas Gregor // This will not be the case if the 'subframework' is actually a symlink 41007c22b78SDouglas Gregor // out to a top-level framework. 41107c22b78SDouglas Gregor #ifdef LLVM_ON_UNIX 41207c22b78SDouglas Gregor char RealSubframeworkDirName[PATH_MAX]; 41307c22b78SDouglas Gregor if (realpath(Dir->path().c_str(), RealSubframeworkDirName)) { 41407c22b78SDouglas Gregor StringRef SubframeworkDirName = RealSubframeworkDirName; 41507c22b78SDouglas Gregor 41607c22b78SDouglas Gregor bool FoundParent = false; 41707c22b78SDouglas Gregor do { 41807c22b78SDouglas Gregor // Get the parent directory name. 41907c22b78SDouglas Gregor SubframeworkDirName 42007c22b78SDouglas Gregor = llvm::sys::path::parent_path(SubframeworkDirName); 42107c22b78SDouglas Gregor if (SubframeworkDirName.empty()) 42207c22b78SDouglas Gregor break; 42307c22b78SDouglas Gregor 42407c22b78SDouglas Gregor if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { 42507c22b78SDouglas Gregor FoundParent = true; 42607c22b78SDouglas Gregor break; 42707c22b78SDouglas Gregor } 42807c22b78SDouglas Gregor } while (true); 42907c22b78SDouglas Gregor 43007c22b78SDouglas Gregor if (!FoundParent) 43107c22b78SDouglas Gregor continue; 43207c22b78SDouglas Gregor } 43307c22b78SDouglas Gregor #endif 43407c22b78SDouglas Gregor 435e89dbc1dSDouglas Gregor // FIXME: Do we want to warn about subframeworks without umbrella headers? 436056396aeSDouglas Gregor SmallString<32> NameBuf; 437056396aeSDouglas Gregor inferFrameworkModule(sanitizeFilenameAsIdentifier( 438056396aeSDouglas Gregor llvm::sys::path::stem(Dir->path()), NameBuf), 439056396aeSDouglas Gregor SubframeworkDir, IsSystem, Result); 440e89dbc1dSDouglas Gregor } 441e89dbc1dSDouglas Gregor } 442e89dbc1dSDouglas Gregor 44356c64013SDouglas Gregor return Result; 44456c64013SDouglas Gregor } 44556c64013SDouglas Gregor 446a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ 44759527666SDouglas Gregor Headers[UmbrellaHeader] = KnownHeader(Mod, /*Excluded=*/false); 44873141fa9SDouglas Gregor Mod->Umbrella = UmbrellaHeader; 4497033127bSDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; 450a89c5ac4SDouglas Gregor } 451a89c5ac4SDouglas Gregor 452524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) { 453524e33e1SDouglas Gregor Mod->Umbrella = UmbrellaDir; 454524e33e1SDouglas Gregor UmbrellaDirs[UmbrellaDir] = Mod; 455524e33e1SDouglas Gregor } 456524e33e1SDouglas Gregor 45759527666SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header, 45859527666SDouglas Gregor bool Excluded) { 45959527666SDouglas Gregor if (Excluded) 46059527666SDouglas Gregor Mod->ExcludedHeaders.push_back(Header); 46159527666SDouglas Gregor else 462a89c5ac4SDouglas Gregor Mod->Headers.push_back(Header); 46359527666SDouglas Gregor Headers[Header] = KnownHeader(Mod, Excluded); 464a89c5ac4SDouglas Gregor } 465a89c5ac4SDouglas Gregor 466514b636aSDouglas Gregor const FileEntry * 467de3ef502SDouglas Gregor ModuleMap::getContainingModuleMapFile(Module *Module) { 468514b636aSDouglas Gregor if (Module->DefinitionLoc.isInvalid() || !SourceMgr) 469514b636aSDouglas Gregor return 0; 470514b636aSDouglas Gregor 471514b636aSDouglas Gregor return SourceMgr->getFileEntryForID( 472514b636aSDouglas Gregor SourceMgr->getFileID(Module->DefinitionLoc)); 473514b636aSDouglas Gregor } 474514b636aSDouglas Gregor 475718292f2SDouglas Gregor void ModuleMap::dump() { 476718292f2SDouglas Gregor llvm::errs() << "Modules:"; 477718292f2SDouglas Gregor for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 478718292f2SDouglas Gregor MEnd = Modules.end(); 479718292f2SDouglas Gregor M != MEnd; ++M) 480d28d1b8dSDouglas Gregor M->getValue()->print(llvm::errs(), 2); 481718292f2SDouglas Gregor 482718292f2SDouglas Gregor llvm::errs() << "Headers:"; 48359527666SDouglas Gregor for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 484718292f2SDouglas Gregor H != HEnd; ++H) { 485718292f2SDouglas Gregor llvm::errs() << " \"" << H->first->getName() << "\" -> " 48659527666SDouglas Gregor << H->second.getModule()->getFullModuleName() << "\n"; 487718292f2SDouglas Gregor } 488718292f2SDouglas Gregor } 489718292f2SDouglas Gregor 4902b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 4912b82c2a5SDouglas Gregor bool HadError = false; 4922b82c2a5SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { 4932b82c2a5SDouglas Gregor Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 4942b82c2a5SDouglas Gregor Complain); 495f5eedd05SDouglas Gregor if (Export.getPointer() || Export.getInt()) 4962b82c2a5SDouglas Gregor Mod->Exports.push_back(Export); 4972b82c2a5SDouglas Gregor else 4982b82c2a5SDouglas Gregor HadError = true; 4992b82c2a5SDouglas Gregor } 5002b82c2a5SDouglas Gregor Mod->UnresolvedExports.clear(); 5012b82c2a5SDouglas Gregor return HadError; 5022b82c2a5SDouglas Gregor } 5032b82c2a5SDouglas Gregor 5040093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 5050093b3c7SDouglas Gregor if (Loc.isInvalid()) 5060093b3c7SDouglas Gregor return 0; 5070093b3c7SDouglas Gregor 5080093b3c7SDouglas Gregor // Use the expansion location to determine which module we're in. 5090093b3c7SDouglas Gregor FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 5100093b3c7SDouglas Gregor if (!ExpansionLoc.isFileID()) 5110093b3c7SDouglas Gregor return 0; 5120093b3c7SDouglas Gregor 5130093b3c7SDouglas Gregor 5140093b3c7SDouglas Gregor const SourceManager &SrcMgr = Loc.getManager(); 5150093b3c7SDouglas Gregor FileID ExpansionFileID = ExpansionLoc.getFileID(); 516224d8a74SDouglas Gregor 517224d8a74SDouglas Gregor while (const FileEntry *ExpansionFile 518224d8a74SDouglas Gregor = SrcMgr.getFileEntryForID(ExpansionFileID)) { 519224d8a74SDouglas Gregor // Find the module that owns this header (if any). 520224d8a74SDouglas Gregor if (Module *Mod = findModuleForHeader(ExpansionFile)) 521224d8a74SDouglas Gregor return Mod; 522224d8a74SDouglas Gregor 523224d8a74SDouglas Gregor // No module owns this header, so look up the inclusion chain to see if 524224d8a74SDouglas Gregor // any included header has an associated module. 525224d8a74SDouglas Gregor SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); 526224d8a74SDouglas Gregor if (IncludeLoc.isInvalid()) 5270093b3c7SDouglas Gregor return 0; 5280093b3c7SDouglas Gregor 529224d8a74SDouglas Gregor ExpansionFileID = SrcMgr.getFileID(IncludeLoc); 530224d8a74SDouglas Gregor } 531224d8a74SDouglas Gregor 532224d8a74SDouglas Gregor return 0; 5330093b3c7SDouglas Gregor } 5340093b3c7SDouglas Gregor 535718292f2SDouglas Gregor //----------------------------------------------------------------------------// 536718292f2SDouglas Gregor // Module map file parser 537718292f2SDouglas Gregor //----------------------------------------------------------------------------// 538718292f2SDouglas Gregor 539718292f2SDouglas Gregor namespace clang { 540718292f2SDouglas Gregor /// \brief A token in a module map file. 541718292f2SDouglas Gregor struct MMToken { 542718292f2SDouglas Gregor enum TokenKind { 5431fb5c3a6SDouglas Gregor Comma, 544718292f2SDouglas Gregor EndOfFile, 545718292f2SDouglas Gregor HeaderKeyword, 546718292f2SDouglas Gregor Identifier, 54759527666SDouglas Gregor ExcludeKeyword, 548718292f2SDouglas Gregor ExplicitKeyword, 5492b82c2a5SDouglas Gregor ExportKeyword, 550755b2055SDouglas Gregor FrameworkKeyword, 551718292f2SDouglas Gregor ModuleKeyword, 5522b82c2a5SDouglas Gregor Period, 553718292f2SDouglas Gregor UmbrellaKeyword, 5541fb5c3a6SDouglas Gregor RequiresKeyword, 5552b82c2a5SDouglas Gregor Star, 556718292f2SDouglas Gregor StringLiteral, 557718292f2SDouglas Gregor LBrace, 558a686e1b0SDouglas Gregor RBrace, 559a686e1b0SDouglas Gregor LSquare, 560a686e1b0SDouglas Gregor RSquare 561718292f2SDouglas Gregor } Kind; 562718292f2SDouglas Gregor 563718292f2SDouglas Gregor unsigned Location; 564718292f2SDouglas Gregor unsigned StringLength; 565718292f2SDouglas Gregor const char *StringData; 566718292f2SDouglas Gregor 567718292f2SDouglas Gregor void clear() { 568718292f2SDouglas Gregor Kind = EndOfFile; 569718292f2SDouglas Gregor Location = 0; 570718292f2SDouglas Gregor StringLength = 0; 571718292f2SDouglas Gregor StringData = 0; 572718292f2SDouglas Gregor } 573718292f2SDouglas Gregor 574718292f2SDouglas Gregor bool is(TokenKind K) const { return Kind == K; } 575718292f2SDouglas Gregor 576718292f2SDouglas Gregor SourceLocation getLocation() const { 577718292f2SDouglas Gregor return SourceLocation::getFromRawEncoding(Location); 578718292f2SDouglas Gregor } 579718292f2SDouglas Gregor 580718292f2SDouglas Gregor StringRef getString() const { 581718292f2SDouglas Gregor return StringRef(StringData, StringLength); 582718292f2SDouglas Gregor } 583718292f2SDouglas Gregor }; 584718292f2SDouglas Gregor 585718292f2SDouglas Gregor class ModuleMapParser { 586718292f2SDouglas Gregor Lexer &L; 587718292f2SDouglas Gregor SourceManager &SourceMgr; 588*bc10b9fbSDouglas Gregor 589*bc10b9fbSDouglas Gregor /// \brief Default target information, used only for string literal 590*bc10b9fbSDouglas Gregor /// parsing. 591*bc10b9fbSDouglas Gregor const TargetInfo *Target; 592*bc10b9fbSDouglas Gregor 593718292f2SDouglas Gregor DiagnosticsEngine &Diags; 594718292f2SDouglas Gregor ModuleMap ⤅ 595718292f2SDouglas Gregor 5965257fc63SDouglas Gregor /// \brief The directory that this module map resides in. 5975257fc63SDouglas Gregor const DirectoryEntry *Directory; 5985257fc63SDouglas Gregor 5993ec6663bSDouglas Gregor /// \brief The directory containing Clang-supplied headers. 6003ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir; 6013ec6663bSDouglas Gregor 602718292f2SDouglas Gregor /// \brief Whether an error occurred. 603718292f2SDouglas Gregor bool HadError; 604718292f2SDouglas Gregor 605718292f2SDouglas Gregor /// \brief Stores string data for the various string literals referenced 606718292f2SDouglas Gregor /// during parsing. 607718292f2SDouglas Gregor llvm::BumpPtrAllocator StringData; 608718292f2SDouglas Gregor 609718292f2SDouglas Gregor /// \brief The current token. 610718292f2SDouglas Gregor MMToken Tok; 611718292f2SDouglas Gregor 612718292f2SDouglas Gregor /// \brief The active module. 613de3ef502SDouglas Gregor Module *ActiveModule; 614718292f2SDouglas Gregor 615718292f2SDouglas Gregor /// \brief Consume the current token and return its location. 616718292f2SDouglas Gregor SourceLocation consumeToken(); 617718292f2SDouglas Gregor 618718292f2SDouglas Gregor /// \brief Skip tokens until we reach the a token with the given kind 619718292f2SDouglas Gregor /// (or the end of the file). 620718292f2SDouglas Gregor void skipUntil(MMToken::TokenKind K); 621718292f2SDouglas Gregor 622e7ab3669SDouglas Gregor typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2> 623e7ab3669SDouglas Gregor ModuleId; 624e7ab3669SDouglas Gregor bool parseModuleId(ModuleId &Id); 625718292f2SDouglas Gregor void parseModuleDecl(); 6261fb5c3a6SDouglas Gregor void parseRequiresDecl(); 62759527666SDouglas Gregor void parseHeaderDecl(SourceLocation UmbrellaLoc, SourceLocation ExcludeLoc); 628524e33e1SDouglas Gregor void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 6292b82c2a5SDouglas Gregor void parseExportDecl(); 63073441091SDouglas Gregor void parseInferredSubmoduleDecl(bool Explicit); 631718292f2SDouglas Gregor 6327033127bSDouglas Gregor const DirectoryEntry *getOverriddenHeaderSearchDir(); 6337033127bSDouglas Gregor 634718292f2SDouglas Gregor public: 635718292f2SDouglas Gregor explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 636*bc10b9fbSDouglas Gregor const TargetInfo *Target, 637718292f2SDouglas Gregor DiagnosticsEngine &Diags, 6385257fc63SDouglas Gregor ModuleMap &Map, 6393ec6663bSDouglas Gregor const DirectoryEntry *Directory, 6403ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir) 641*bc10b9fbSDouglas Gregor : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 6423ec6663bSDouglas Gregor Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir), 6433ec6663bSDouglas Gregor HadError(false), ActiveModule(0) 644718292f2SDouglas Gregor { 645718292f2SDouglas Gregor Tok.clear(); 646718292f2SDouglas Gregor consumeToken(); 647718292f2SDouglas Gregor } 648718292f2SDouglas Gregor 649718292f2SDouglas Gregor bool parseModuleMapFile(); 650718292f2SDouglas Gregor }; 651718292f2SDouglas Gregor } 652718292f2SDouglas Gregor 653718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() { 654718292f2SDouglas Gregor retry: 655718292f2SDouglas Gregor SourceLocation Result = Tok.getLocation(); 656718292f2SDouglas Gregor Tok.clear(); 657718292f2SDouglas Gregor 658718292f2SDouglas Gregor Token LToken; 659718292f2SDouglas Gregor L.LexFromRawLexer(LToken); 660718292f2SDouglas Gregor Tok.Location = LToken.getLocation().getRawEncoding(); 661718292f2SDouglas Gregor switch (LToken.getKind()) { 662718292f2SDouglas Gregor case tok::raw_identifier: 663718292f2SDouglas Gregor Tok.StringData = LToken.getRawIdentifierData(); 664718292f2SDouglas Gregor Tok.StringLength = LToken.getLength(); 665718292f2SDouglas Gregor Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString()) 666718292f2SDouglas Gregor .Case("header", MMToken::HeaderKeyword) 66759527666SDouglas Gregor .Case("exclude", MMToken::ExcludeKeyword) 668718292f2SDouglas Gregor .Case("explicit", MMToken::ExplicitKeyword) 6692b82c2a5SDouglas Gregor .Case("export", MMToken::ExportKeyword) 670755b2055SDouglas Gregor .Case("framework", MMToken::FrameworkKeyword) 671718292f2SDouglas Gregor .Case("module", MMToken::ModuleKeyword) 6721fb5c3a6SDouglas Gregor .Case("requires", MMToken::RequiresKeyword) 673718292f2SDouglas Gregor .Case("umbrella", MMToken::UmbrellaKeyword) 674718292f2SDouglas Gregor .Default(MMToken::Identifier); 675718292f2SDouglas Gregor break; 676718292f2SDouglas Gregor 6771fb5c3a6SDouglas Gregor case tok::comma: 6781fb5c3a6SDouglas Gregor Tok.Kind = MMToken::Comma; 6791fb5c3a6SDouglas Gregor break; 6801fb5c3a6SDouglas Gregor 681718292f2SDouglas Gregor case tok::eof: 682718292f2SDouglas Gregor Tok.Kind = MMToken::EndOfFile; 683718292f2SDouglas Gregor break; 684718292f2SDouglas Gregor 685718292f2SDouglas Gregor case tok::l_brace: 686718292f2SDouglas Gregor Tok.Kind = MMToken::LBrace; 687718292f2SDouglas Gregor break; 688718292f2SDouglas Gregor 689a686e1b0SDouglas Gregor case tok::l_square: 690a686e1b0SDouglas Gregor Tok.Kind = MMToken::LSquare; 691a686e1b0SDouglas Gregor break; 692a686e1b0SDouglas Gregor 6932b82c2a5SDouglas Gregor case tok::period: 6942b82c2a5SDouglas Gregor Tok.Kind = MMToken::Period; 6952b82c2a5SDouglas Gregor break; 6962b82c2a5SDouglas Gregor 697718292f2SDouglas Gregor case tok::r_brace: 698718292f2SDouglas Gregor Tok.Kind = MMToken::RBrace; 699718292f2SDouglas Gregor break; 700718292f2SDouglas Gregor 701a686e1b0SDouglas Gregor case tok::r_square: 702a686e1b0SDouglas Gregor Tok.Kind = MMToken::RSquare; 703a686e1b0SDouglas Gregor break; 704a686e1b0SDouglas Gregor 7052b82c2a5SDouglas Gregor case tok::star: 7062b82c2a5SDouglas Gregor Tok.Kind = MMToken::Star; 7072b82c2a5SDouglas Gregor break; 7082b82c2a5SDouglas Gregor 709718292f2SDouglas Gregor case tok::string_literal: { 710d67aea28SRichard Smith if (LToken.hasUDSuffix()) { 711d67aea28SRichard Smith Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 712d67aea28SRichard Smith HadError = true; 713d67aea28SRichard Smith goto retry; 714d67aea28SRichard Smith } 715d67aea28SRichard Smith 716718292f2SDouglas Gregor // Parse the string literal. 717718292f2SDouglas Gregor LangOptions LangOpts; 718718292f2SDouglas Gregor StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target); 719718292f2SDouglas Gregor if (StringLiteral.hadError) 720718292f2SDouglas Gregor goto retry; 721718292f2SDouglas Gregor 722718292f2SDouglas Gregor // Copy the string literal into our string data allocator. 723718292f2SDouglas Gregor unsigned Length = StringLiteral.GetStringLength(); 724718292f2SDouglas Gregor char *Saved = StringData.Allocate<char>(Length + 1); 725718292f2SDouglas Gregor memcpy(Saved, StringLiteral.GetString().data(), Length); 726718292f2SDouglas Gregor Saved[Length] = 0; 727718292f2SDouglas Gregor 728718292f2SDouglas Gregor // Form the token. 729718292f2SDouglas Gregor Tok.Kind = MMToken::StringLiteral; 730718292f2SDouglas Gregor Tok.StringData = Saved; 731718292f2SDouglas Gregor Tok.StringLength = Length; 732718292f2SDouglas Gregor break; 733718292f2SDouglas Gregor } 734718292f2SDouglas Gregor 735718292f2SDouglas Gregor case tok::comment: 736718292f2SDouglas Gregor goto retry; 737718292f2SDouglas Gregor 738718292f2SDouglas Gregor default: 739718292f2SDouglas Gregor Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 740718292f2SDouglas Gregor HadError = true; 741718292f2SDouglas Gregor goto retry; 742718292f2SDouglas Gregor } 743718292f2SDouglas Gregor 744718292f2SDouglas Gregor return Result; 745718292f2SDouglas Gregor } 746718292f2SDouglas Gregor 747718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 748718292f2SDouglas Gregor unsigned braceDepth = 0; 749a686e1b0SDouglas Gregor unsigned squareDepth = 0; 750718292f2SDouglas Gregor do { 751718292f2SDouglas Gregor switch (Tok.Kind) { 752718292f2SDouglas Gregor case MMToken::EndOfFile: 753718292f2SDouglas Gregor return; 754718292f2SDouglas Gregor 755718292f2SDouglas Gregor case MMToken::LBrace: 756a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 757718292f2SDouglas Gregor return; 758718292f2SDouglas Gregor 759718292f2SDouglas Gregor ++braceDepth; 760718292f2SDouglas Gregor break; 761718292f2SDouglas Gregor 762a686e1b0SDouglas Gregor case MMToken::LSquare: 763a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 764a686e1b0SDouglas Gregor return; 765a686e1b0SDouglas Gregor 766a686e1b0SDouglas Gregor ++squareDepth; 767a686e1b0SDouglas Gregor break; 768a686e1b0SDouglas Gregor 769718292f2SDouglas Gregor case MMToken::RBrace: 770718292f2SDouglas Gregor if (braceDepth > 0) 771718292f2SDouglas Gregor --braceDepth; 772718292f2SDouglas Gregor else if (Tok.is(K)) 773718292f2SDouglas Gregor return; 774718292f2SDouglas Gregor break; 775718292f2SDouglas Gregor 776a686e1b0SDouglas Gregor case MMToken::RSquare: 777a686e1b0SDouglas Gregor if (squareDepth > 0) 778a686e1b0SDouglas Gregor --squareDepth; 779a686e1b0SDouglas Gregor else if (Tok.is(K)) 780a686e1b0SDouglas Gregor return; 781a686e1b0SDouglas Gregor break; 782a686e1b0SDouglas Gregor 783718292f2SDouglas Gregor default: 784a686e1b0SDouglas Gregor if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 785718292f2SDouglas Gregor return; 786718292f2SDouglas Gregor break; 787718292f2SDouglas Gregor } 788718292f2SDouglas Gregor 789718292f2SDouglas Gregor consumeToken(); 790718292f2SDouglas Gregor } while (true); 791718292f2SDouglas Gregor } 792718292f2SDouglas Gregor 793e7ab3669SDouglas Gregor /// \brief Parse a module-id. 794e7ab3669SDouglas Gregor /// 795e7ab3669SDouglas Gregor /// module-id: 796e7ab3669SDouglas Gregor /// identifier 797e7ab3669SDouglas Gregor /// identifier '.' module-id 798e7ab3669SDouglas Gregor /// 799e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise. 800e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) { 801e7ab3669SDouglas Gregor Id.clear(); 802e7ab3669SDouglas Gregor do { 803e7ab3669SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 804e7ab3669SDouglas Gregor Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 805e7ab3669SDouglas Gregor consumeToken(); 806e7ab3669SDouglas Gregor } else { 807e7ab3669SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 808e7ab3669SDouglas Gregor return true; 809e7ab3669SDouglas Gregor } 810e7ab3669SDouglas Gregor 811e7ab3669SDouglas Gregor if (!Tok.is(MMToken::Period)) 812e7ab3669SDouglas Gregor break; 813e7ab3669SDouglas Gregor 814e7ab3669SDouglas Gregor consumeToken(); 815e7ab3669SDouglas Gregor } while (true); 816e7ab3669SDouglas Gregor 817e7ab3669SDouglas Gregor return false; 818e7ab3669SDouglas Gregor } 819e7ab3669SDouglas Gregor 820a686e1b0SDouglas Gregor namespace { 821a686e1b0SDouglas Gregor /// \brief Enumerates the known attributes. 822a686e1b0SDouglas Gregor enum AttributeKind { 823a686e1b0SDouglas Gregor /// \brief An unknown attribute. 824a686e1b0SDouglas Gregor AT_unknown, 825a686e1b0SDouglas Gregor /// \brief The 'system' attribute. 826a686e1b0SDouglas Gregor AT_system 827a686e1b0SDouglas Gregor }; 828a686e1b0SDouglas Gregor } 829a686e1b0SDouglas Gregor 830718292f2SDouglas Gregor /// \brief Parse a module declaration. 831718292f2SDouglas Gregor /// 832718292f2SDouglas Gregor /// module-declaration: 833a686e1b0SDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 834a686e1b0SDouglas Gregor /// { module-member* } 835a686e1b0SDouglas Gregor /// 836a686e1b0SDouglas Gregor /// attributes: 837a686e1b0SDouglas Gregor /// attribute attributes 838a686e1b0SDouglas Gregor /// attribute 839a686e1b0SDouglas Gregor /// 840a686e1b0SDouglas Gregor /// attribute: 841a686e1b0SDouglas Gregor /// [ identifier ] 842718292f2SDouglas Gregor /// 843718292f2SDouglas Gregor /// module-member: 8441fb5c3a6SDouglas Gregor /// requires-declaration 845718292f2SDouglas Gregor /// header-declaration 846e7ab3669SDouglas Gregor /// submodule-declaration 8472b82c2a5SDouglas Gregor /// export-declaration 84873441091SDouglas Gregor /// 84973441091SDouglas Gregor /// submodule-declaration: 85073441091SDouglas Gregor /// module-declaration 85173441091SDouglas Gregor /// inferred-submodule-declaration 852718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() { 853755b2055SDouglas Gregor assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 854755b2055SDouglas Gregor Tok.is(MMToken::FrameworkKeyword)); 855f2161a70SDouglas Gregor // Parse 'explicit' or 'framework' keyword, if present. 856e7ab3669SDouglas Gregor SourceLocation ExplicitLoc; 857718292f2SDouglas Gregor bool Explicit = false; 858f2161a70SDouglas Gregor bool Framework = false; 859755b2055SDouglas Gregor 860f2161a70SDouglas Gregor // Parse 'explicit' keyword, if present. 861f2161a70SDouglas Gregor if (Tok.is(MMToken::ExplicitKeyword)) { 862e7ab3669SDouglas Gregor ExplicitLoc = consumeToken(); 863f2161a70SDouglas Gregor Explicit = true; 864f2161a70SDouglas Gregor } 865f2161a70SDouglas Gregor 866f2161a70SDouglas Gregor // Parse 'framework' keyword, if present. 867755b2055SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 868755b2055SDouglas Gregor consumeToken(); 869755b2055SDouglas Gregor Framework = true; 870755b2055SDouglas Gregor } 871718292f2SDouglas Gregor 872718292f2SDouglas Gregor // Parse 'module' keyword. 873718292f2SDouglas Gregor if (!Tok.is(MMToken::ModuleKeyword)) { 874d6343c99SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 875718292f2SDouglas Gregor consumeToken(); 876718292f2SDouglas Gregor HadError = true; 877718292f2SDouglas Gregor return; 878718292f2SDouglas Gregor } 879718292f2SDouglas Gregor consumeToken(); // 'module' keyword 880718292f2SDouglas Gregor 88173441091SDouglas Gregor // If we have a wildcard for the module name, this is an inferred submodule. 88273441091SDouglas Gregor // Parse it. 88373441091SDouglas Gregor if (Tok.is(MMToken::Star)) 88473441091SDouglas Gregor return parseInferredSubmoduleDecl(Explicit); 88573441091SDouglas Gregor 886718292f2SDouglas Gregor // Parse the module name. 887e7ab3669SDouglas Gregor ModuleId Id; 888e7ab3669SDouglas Gregor if (parseModuleId(Id)) { 889718292f2SDouglas Gregor HadError = true; 890718292f2SDouglas Gregor return; 891718292f2SDouglas Gregor } 892e7ab3669SDouglas Gregor 893e7ab3669SDouglas Gregor if (ActiveModule) { 894e7ab3669SDouglas Gregor if (Id.size() > 1) { 895e7ab3669SDouglas Gregor Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 896e7ab3669SDouglas Gregor << SourceRange(Id.front().second, Id.back().second); 897e7ab3669SDouglas Gregor 898e7ab3669SDouglas Gregor HadError = true; 899e7ab3669SDouglas Gregor return; 900e7ab3669SDouglas Gregor } 901e7ab3669SDouglas Gregor } else if (Id.size() == 1 && Explicit) { 902e7ab3669SDouglas Gregor // Top-level modules can't be explicit. 903e7ab3669SDouglas Gregor Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 904e7ab3669SDouglas Gregor Explicit = false; 905e7ab3669SDouglas Gregor ExplicitLoc = SourceLocation(); 906e7ab3669SDouglas Gregor HadError = true; 907e7ab3669SDouglas Gregor } 908e7ab3669SDouglas Gregor 909e7ab3669SDouglas Gregor Module *PreviousActiveModule = ActiveModule; 910e7ab3669SDouglas Gregor if (Id.size() > 1) { 911e7ab3669SDouglas Gregor // This module map defines a submodule. Go find the module of which it 912e7ab3669SDouglas Gregor // is a submodule. 913e7ab3669SDouglas Gregor ActiveModule = 0; 914e7ab3669SDouglas Gregor for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 915e7ab3669SDouglas Gregor if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 916e7ab3669SDouglas Gregor ActiveModule = Next; 917e7ab3669SDouglas Gregor continue; 918e7ab3669SDouglas Gregor } 919e7ab3669SDouglas Gregor 920e7ab3669SDouglas Gregor if (ActiveModule) { 921e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 922e7ab3669SDouglas Gregor << Id[I].first << ActiveModule->getTopLevelModule(); 923e7ab3669SDouglas Gregor } else { 924e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 925e7ab3669SDouglas Gregor } 926e7ab3669SDouglas Gregor HadError = true; 927e7ab3669SDouglas Gregor return; 928e7ab3669SDouglas Gregor } 929e7ab3669SDouglas Gregor } 930e7ab3669SDouglas Gregor 931e7ab3669SDouglas Gregor StringRef ModuleName = Id.back().first; 932e7ab3669SDouglas Gregor SourceLocation ModuleNameLoc = Id.back().second; 933718292f2SDouglas Gregor 934a686e1b0SDouglas Gregor // Parse the optional attribute list. 935a686e1b0SDouglas Gregor bool IsSystem = false; 936a686e1b0SDouglas Gregor while (Tok.is(MMToken::LSquare)) { 937a686e1b0SDouglas Gregor // Consume the '['. 938a686e1b0SDouglas Gregor SourceLocation LSquareLoc = consumeToken(); 939a686e1b0SDouglas Gregor 940a686e1b0SDouglas Gregor // Check whether we have an attribute name here. 941a686e1b0SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 942a686e1b0SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 943a686e1b0SDouglas Gregor skipUntil(MMToken::RSquare); 944a686e1b0SDouglas Gregor if (Tok.is(MMToken::RSquare)) 945a686e1b0SDouglas Gregor consumeToken(); 946a686e1b0SDouglas Gregor continue; 947a686e1b0SDouglas Gregor } 948a686e1b0SDouglas Gregor 949a686e1b0SDouglas Gregor // Decode the attribute name. 950a686e1b0SDouglas Gregor AttributeKind Attribute 951a686e1b0SDouglas Gregor = llvm::StringSwitch<AttributeKind>(Tok.getString()) 952a686e1b0SDouglas Gregor .Case("system", AT_system) 953a686e1b0SDouglas Gregor .Default(AT_unknown); 954a686e1b0SDouglas Gregor switch (Attribute) { 955a686e1b0SDouglas Gregor case AT_unknown: 956a686e1b0SDouglas Gregor Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 957a686e1b0SDouglas Gregor << Tok.getString(); 958a686e1b0SDouglas Gregor break; 959a686e1b0SDouglas Gregor 960a686e1b0SDouglas Gregor case AT_system: 961a686e1b0SDouglas Gregor IsSystem = true; 962a686e1b0SDouglas Gregor break; 963a686e1b0SDouglas Gregor } 964a686e1b0SDouglas Gregor consumeToken(); 965a686e1b0SDouglas Gregor 966a686e1b0SDouglas Gregor // Consume the ']'. 967a686e1b0SDouglas Gregor if (!Tok.is(MMToken::RSquare)) { 968a686e1b0SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 969a686e1b0SDouglas Gregor Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 970a686e1b0SDouglas Gregor skipUntil(MMToken::RSquare); 971a686e1b0SDouglas Gregor } 972a686e1b0SDouglas Gregor 973a686e1b0SDouglas Gregor if (Tok.is(MMToken::RSquare)) 974a686e1b0SDouglas Gregor consumeToken(); 975a686e1b0SDouglas Gregor } 976a686e1b0SDouglas Gregor 977718292f2SDouglas Gregor // Parse the opening brace. 978718292f2SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 979718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 980718292f2SDouglas Gregor << ModuleName; 981718292f2SDouglas Gregor HadError = true; 982718292f2SDouglas Gregor return; 983718292f2SDouglas Gregor } 984718292f2SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 985718292f2SDouglas Gregor 986718292f2SDouglas Gregor // Determine whether this (sub)module has already been defined. 987eb90e830SDouglas Gregor if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 988fcc54a3bSDouglas Gregor if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { 989fcc54a3bSDouglas Gregor // Skip the module definition. 990fcc54a3bSDouglas Gregor skipUntil(MMToken::RBrace); 991fcc54a3bSDouglas Gregor if (Tok.is(MMToken::RBrace)) 992fcc54a3bSDouglas Gregor consumeToken(); 993fcc54a3bSDouglas Gregor else { 994fcc54a3bSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 995fcc54a3bSDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 996fcc54a3bSDouglas Gregor HadError = true; 997fcc54a3bSDouglas Gregor } 998fcc54a3bSDouglas Gregor return; 999fcc54a3bSDouglas Gregor } 1000fcc54a3bSDouglas Gregor 1001718292f2SDouglas Gregor Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 1002718292f2SDouglas Gregor << ModuleName; 1003eb90e830SDouglas Gregor Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 1004718292f2SDouglas Gregor 1005718292f2SDouglas Gregor // Skip the module definition. 1006718292f2SDouglas Gregor skipUntil(MMToken::RBrace); 1007718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1008718292f2SDouglas Gregor consumeToken(); 1009718292f2SDouglas Gregor 1010718292f2SDouglas Gregor HadError = true; 1011718292f2SDouglas Gregor return; 1012718292f2SDouglas Gregor } 1013718292f2SDouglas Gregor 1014718292f2SDouglas Gregor // Start defining this module. 1015eb90e830SDouglas Gregor ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, 1016eb90e830SDouglas Gregor Explicit).first; 1017eb90e830SDouglas Gregor ActiveModule->DefinitionLoc = ModuleNameLoc; 1018a686e1b0SDouglas Gregor if (IsSystem) 1019a686e1b0SDouglas Gregor ActiveModule->IsSystem = true; 1020718292f2SDouglas Gregor 1021718292f2SDouglas Gregor bool Done = false; 1022718292f2SDouglas Gregor do { 1023718292f2SDouglas Gregor switch (Tok.Kind) { 1024718292f2SDouglas Gregor case MMToken::EndOfFile: 1025718292f2SDouglas Gregor case MMToken::RBrace: 1026718292f2SDouglas Gregor Done = true; 1027718292f2SDouglas Gregor break; 1028718292f2SDouglas Gregor 1029718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 1030f2161a70SDouglas Gregor case MMToken::FrameworkKeyword: 1031718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1032718292f2SDouglas Gregor parseModuleDecl(); 1033718292f2SDouglas Gregor break; 1034718292f2SDouglas Gregor 10352b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 10362b82c2a5SDouglas Gregor parseExportDecl(); 10372b82c2a5SDouglas Gregor break; 10382b82c2a5SDouglas Gregor 10391fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 10401fb5c3a6SDouglas Gregor parseRequiresDecl(); 10411fb5c3a6SDouglas Gregor break; 10421fb5c3a6SDouglas Gregor 1043524e33e1SDouglas Gregor case MMToken::UmbrellaKeyword: { 1044524e33e1SDouglas Gregor SourceLocation UmbrellaLoc = consumeToken(); 1045524e33e1SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) 104659527666SDouglas Gregor parseHeaderDecl(UmbrellaLoc, SourceLocation()); 1047524e33e1SDouglas Gregor else 1048524e33e1SDouglas Gregor parseUmbrellaDirDecl(UmbrellaLoc); 1049718292f2SDouglas Gregor break; 1050524e33e1SDouglas Gregor } 1051718292f2SDouglas Gregor 105259527666SDouglas Gregor case MMToken::ExcludeKeyword: { 105359527666SDouglas Gregor SourceLocation ExcludeLoc = consumeToken(); 105459527666SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) { 105559527666SDouglas Gregor parseHeaderDecl(SourceLocation(), ExcludeLoc); 105659527666SDouglas Gregor } else { 105759527666SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 105859527666SDouglas Gregor << "exclude"; 105959527666SDouglas Gregor } 106059527666SDouglas Gregor break; 106159527666SDouglas Gregor } 106259527666SDouglas Gregor 1063322f633cSDouglas Gregor case MMToken::HeaderKeyword: 106459527666SDouglas Gregor parseHeaderDecl(SourceLocation(), SourceLocation()); 1065718292f2SDouglas Gregor break; 1066718292f2SDouglas Gregor 1067718292f2SDouglas Gregor default: 1068718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 1069718292f2SDouglas Gregor consumeToken(); 1070718292f2SDouglas Gregor break; 1071718292f2SDouglas Gregor } 1072718292f2SDouglas Gregor } while (!Done); 1073718292f2SDouglas Gregor 1074718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1075718292f2SDouglas Gregor consumeToken(); 1076718292f2SDouglas Gregor else { 1077718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1078718292f2SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1079718292f2SDouglas Gregor HadError = true; 1080718292f2SDouglas Gregor } 1081718292f2SDouglas Gregor 1082e7ab3669SDouglas Gregor // We're done parsing this module. Pop back to the previous module. 1083e7ab3669SDouglas Gregor ActiveModule = PreviousActiveModule; 1084718292f2SDouglas Gregor } 1085718292f2SDouglas Gregor 10861fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration. 10871fb5c3a6SDouglas Gregor /// 10881fb5c3a6SDouglas Gregor /// requires-declaration: 10891fb5c3a6SDouglas Gregor /// 'requires' feature-list 10901fb5c3a6SDouglas Gregor /// 10911fb5c3a6SDouglas Gregor /// feature-list: 10921fb5c3a6SDouglas Gregor /// identifier ',' feature-list 10931fb5c3a6SDouglas Gregor /// identifier 10941fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() { 10951fb5c3a6SDouglas Gregor assert(Tok.is(MMToken::RequiresKeyword)); 10961fb5c3a6SDouglas Gregor 10971fb5c3a6SDouglas Gregor // Parse 'requires' keyword. 10981fb5c3a6SDouglas Gregor consumeToken(); 10991fb5c3a6SDouglas Gregor 11001fb5c3a6SDouglas Gregor // Parse the feature-list. 11011fb5c3a6SDouglas Gregor do { 11021fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 11031fb5c3a6SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 11041fb5c3a6SDouglas Gregor HadError = true; 11051fb5c3a6SDouglas Gregor return; 11061fb5c3a6SDouglas Gregor } 11071fb5c3a6SDouglas Gregor 11081fb5c3a6SDouglas Gregor // Consume the feature name. 11091fb5c3a6SDouglas Gregor std::string Feature = Tok.getString(); 11101fb5c3a6SDouglas Gregor consumeToken(); 11111fb5c3a6SDouglas Gregor 11121fb5c3a6SDouglas Gregor // Add this feature. 111389929282SDouglas Gregor ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target); 11141fb5c3a6SDouglas Gregor 11151fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Comma)) 11161fb5c3a6SDouglas Gregor break; 11171fb5c3a6SDouglas Gregor 11181fb5c3a6SDouglas Gregor // Consume the comma. 11191fb5c3a6SDouglas Gregor consumeToken(); 11201fb5c3a6SDouglas Gregor } while (true); 11211fb5c3a6SDouglas Gregor } 11221fb5c3a6SDouglas Gregor 1123f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the 1124f2161a70SDouglas Gregor /// subframework in which the given module lives. 1125bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod, 1126bf8da9d7SBenjamin Kramer llvm::SmallVectorImpl<char> &Path) { 1127f2161a70SDouglas Gregor // Collect the framework names from the given module to the top-level module. 1128f2161a70SDouglas Gregor llvm::SmallVector<StringRef, 2> Paths; 1129f2161a70SDouglas Gregor for (; Mod; Mod = Mod->Parent) { 1130f2161a70SDouglas Gregor if (Mod->IsFramework) 1131f2161a70SDouglas Gregor Paths.push_back(Mod->Name); 1132f2161a70SDouglas Gregor } 1133f2161a70SDouglas Gregor 1134f2161a70SDouglas Gregor if (Paths.empty()) 1135f2161a70SDouglas Gregor return; 1136f2161a70SDouglas Gregor 1137f2161a70SDouglas Gregor // Add Frameworks/Name.framework for each subframework. 1138f2161a70SDouglas Gregor for (unsigned I = Paths.size() - 1; I != 0; --I) { 1139f2161a70SDouglas Gregor llvm::sys::path::append(Path, "Frameworks"); 1140f2161a70SDouglas Gregor llvm::sys::path::append(Path, Paths[I-1] + ".framework"); 1141f2161a70SDouglas Gregor } 1142f2161a70SDouglas Gregor } 1143f2161a70SDouglas Gregor 11443ec6663bSDouglas Gregor /// \brief Determine whether the given file name is the name of a builtin 11453ec6663bSDouglas Gregor /// header, supplied by Clang to replace, override, or augment existing system 11463ec6663bSDouglas Gregor /// headers. 11473ec6663bSDouglas Gregor static bool isBuiltinHeader(StringRef FileName) { 11483ec6663bSDouglas Gregor return llvm::StringSwitch<bool>(FileName) 11493ec6663bSDouglas Gregor .Case("float.h", true) 11503ec6663bSDouglas Gregor .Case("iso646.h", true) 11513ec6663bSDouglas Gregor .Case("limits.h", true) 11523ec6663bSDouglas Gregor .Case("stdalign.h", true) 11533ec6663bSDouglas Gregor .Case("stdarg.h", true) 11543ec6663bSDouglas Gregor .Case("stdbool.h", true) 11553ec6663bSDouglas Gregor .Case("stddef.h", true) 11563ec6663bSDouglas Gregor .Case("stdint.h", true) 11573ec6663bSDouglas Gregor .Case("tgmath.h", true) 11583ec6663bSDouglas Gregor .Case("unwind.h", true) 11593ec6663bSDouglas Gregor .Default(false); 11603ec6663bSDouglas Gregor } 11613ec6663bSDouglas Gregor 1162718292f2SDouglas Gregor /// \brief Parse a header declaration. 1163718292f2SDouglas Gregor /// 1164718292f2SDouglas Gregor /// header-declaration: 1165322f633cSDouglas Gregor /// 'umbrella'[opt] 'header' string-literal 116659527666SDouglas Gregor /// 'exclude'[opt] 'header' string-literal 116759527666SDouglas Gregor void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc, 116859527666SDouglas Gregor SourceLocation ExcludeLoc) { 1169718292f2SDouglas Gregor assert(Tok.is(MMToken::HeaderKeyword)); 11701871ed3dSBenjamin Kramer consumeToken(); 1171718292f2SDouglas Gregor 1172322f633cSDouglas Gregor bool Umbrella = UmbrellaLoc.isValid(); 117359527666SDouglas Gregor bool Exclude = ExcludeLoc.isValid(); 117459527666SDouglas Gregor assert(!(Umbrella && Exclude) && "Cannot have both 'umbrella' and 'exclude'"); 1175718292f2SDouglas Gregor // Parse the header name. 1176718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1177718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1178718292f2SDouglas Gregor << "header"; 1179718292f2SDouglas Gregor HadError = true; 1180718292f2SDouglas Gregor return; 1181718292f2SDouglas Gregor } 1182e7ab3669SDouglas Gregor std::string FileName = Tok.getString(); 1183718292f2SDouglas Gregor SourceLocation FileNameLoc = consumeToken(); 1184718292f2SDouglas Gregor 1185524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1186524e33e1SDouglas Gregor if (Umbrella && ActiveModule->Umbrella) { 1187524e33e1SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash) 1188524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1189322f633cSDouglas Gregor HadError = true; 1190322f633cSDouglas Gregor return; 1191322f633cSDouglas Gregor } 1192322f633cSDouglas Gregor 11935257fc63SDouglas Gregor // Look for this file. 1194e7ab3669SDouglas Gregor const FileEntry *File = 0; 11953ec6663bSDouglas Gregor const FileEntry *BuiltinFile = 0; 11962c1dd271SDylan Noblesmith SmallString<128> PathName; 1197e7ab3669SDouglas Gregor if (llvm::sys::path::is_absolute(FileName)) { 1198e7ab3669SDouglas Gregor PathName = FileName; 1199e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 12007033127bSDouglas Gregor } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) { 12017033127bSDouglas Gregor PathName = Dir->getName(); 12027033127bSDouglas Gregor llvm::sys::path::append(PathName, FileName); 12037033127bSDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1204e7ab3669SDouglas Gregor } else { 1205e7ab3669SDouglas Gregor // Search for the header file within the search directory. 12067033127bSDouglas Gregor PathName = Directory->getName(); 1207e7ab3669SDouglas Gregor unsigned PathLength = PathName.size(); 1208755b2055SDouglas Gregor 1209f2161a70SDouglas Gregor if (ActiveModule->isPartOfFramework()) { 1210f2161a70SDouglas Gregor appendSubframeworkPaths(ActiveModule, PathName); 1211755b2055SDouglas Gregor 1212e7ab3669SDouglas Gregor // Check whether this file is in the public headers. 1213e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, "Headers"); 12145257fc63SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1215e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1216e7ab3669SDouglas Gregor 1217e7ab3669SDouglas Gregor if (!File) { 1218e7ab3669SDouglas Gregor // Check whether this file is in the private headers. 1219e7ab3669SDouglas Gregor PathName.resize(PathLength); 1220e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, "PrivateHeaders"); 1221e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1222e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1223e7ab3669SDouglas Gregor } 1224e7ab3669SDouglas Gregor } else { 1225e7ab3669SDouglas Gregor // Lookup for normal headers. 1226e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1227e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 12283ec6663bSDouglas Gregor 12293ec6663bSDouglas Gregor // If this is a system module with a top-level header, this header 12303ec6663bSDouglas Gregor // may have a counterpart (or replacement) in the set of headers 12313ec6663bSDouglas Gregor // supplied by Clang. Find that builtin header. 12323ec6663bSDouglas Gregor if (ActiveModule->IsSystem && !Umbrella && BuiltinIncludeDir && 12333ec6663bSDouglas Gregor BuiltinIncludeDir != Directory && isBuiltinHeader(FileName)) { 12342c1dd271SDylan Noblesmith SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); 12353ec6663bSDouglas Gregor llvm::sys::path::append(BuiltinPathName, FileName); 12363ec6663bSDouglas Gregor BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); 12373ec6663bSDouglas Gregor 12383ec6663bSDouglas Gregor // If Clang supplies this header but the underlying system does not, 12393ec6663bSDouglas Gregor // just silently swap in our builtin version. Otherwise, we'll end 12403ec6663bSDouglas Gregor // up adding both (later). 12413ec6663bSDouglas Gregor if (!File && BuiltinFile) { 12423ec6663bSDouglas Gregor File = BuiltinFile; 12433ec6663bSDouglas Gregor BuiltinFile = 0; 12443ec6663bSDouglas Gregor } 12453ec6663bSDouglas Gregor } 1246e7ab3669SDouglas Gregor } 1247e7ab3669SDouglas Gregor } 12485257fc63SDouglas Gregor 12495257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 12505257fc63SDouglas Gregor // Come up with a lazy way to do this. 1251e7ab3669SDouglas Gregor if (File) { 125259527666SDouglas Gregor if (ModuleMap::KnownHeader OwningModule = Map.Headers[File]) { 12535257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_conflict) 125459527666SDouglas Gregor << FileName << OwningModule.getModule()->getFullModuleName(); 12555257fc63SDouglas Gregor HadError = true; 1256322f633cSDouglas Gregor } else if (Umbrella) { 1257322f633cSDouglas Gregor const DirectoryEntry *UmbrellaDir = File->getDir(); 125859527666SDouglas Gregor if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { 1259322f633cSDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 126059527666SDouglas Gregor << UmbrellaModule->getFullModuleName(); 1261322f633cSDouglas Gregor HadError = true; 12625257fc63SDouglas Gregor } else { 1263322f633cSDouglas Gregor // Record this umbrella header. 1264322f633cSDouglas Gregor Map.setUmbrellaHeader(ActiveModule, File); 1265322f633cSDouglas Gregor } 1266322f633cSDouglas Gregor } else { 1267322f633cSDouglas Gregor // Record this header. 126859527666SDouglas Gregor Map.addHeader(ActiveModule, File, Exclude); 12693ec6663bSDouglas Gregor 12703ec6663bSDouglas Gregor // If there is a builtin counterpart to this file, add it now. 12713ec6663bSDouglas Gregor if (BuiltinFile) 127259527666SDouglas Gregor Map.addHeader(ActiveModule, BuiltinFile, Exclude); 12735257fc63SDouglas Gregor } 12745257fc63SDouglas Gregor } else { 12755257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_not_found) 1276524e33e1SDouglas Gregor << Umbrella << FileName; 12775257fc63SDouglas Gregor HadError = true; 12785257fc63SDouglas Gregor } 1279718292f2SDouglas Gregor } 1280718292f2SDouglas Gregor 1281524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration. 1282524e33e1SDouglas Gregor /// 1283524e33e1SDouglas Gregor /// umbrella-dir-declaration: 1284524e33e1SDouglas Gregor /// umbrella string-literal 1285524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1286524e33e1SDouglas Gregor // Parse the directory name. 1287524e33e1SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1288524e33e1SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1289524e33e1SDouglas Gregor << "umbrella"; 1290524e33e1SDouglas Gregor HadError = true; 1291524e33e1SDouglas Gregor return; 1292524e33e1SDouglas Gregor } 1293524e33e1SDouglas Gregor 1294524e33e1SDouglas Gregor std::string DirName = Tok.getString(); 1295524e33e1SDouglas Gregor SourceLocation DirNameLoc = consumeToken(); 1296524e33e1SDouglas Gregor 1297524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1298524e33e1SDouglas Gregor if (ActiveModule->Umbrella) { 1299524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1300524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1301524e33e1SDouglas Gregor HadError = true; 1302524e33e1SDouglas Gregor return; 1303524e33e1SDouglas Gregor } 1304524e33e1SDouglas Gregor 1305524e33e1SDouglas Gregor // Look for this file. 1306524e33e1SDouglas Gregor const DirectoryEntry *Dir = 0; 1307524e33e1SDouglas Gregor if (llvm::sys::path::is_absolute(DirName)) 1308524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(DirName); 1309524e33e1SDouglas Gregor else { 13102c1dd271SDylan Noblesmith SmallString<128> PathName; 1311524e33e1SDouglas Gregor PathName = Directory->getName(); 1312524e33e1SDouglas Gregor llvm::sys::path::append(PathName, DirName); 1313524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(PathName); 1314524e33e1SDouglas Gregor } 1315524e33e1SDouglas Gregor 1316524e33e1SDouglas Gregor if (!Dir) { 1317524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1318524e33e1SDouglas Gregor << DirName; 1319524e33e1SDouglas Gregor HadError = true; 1320524e33e1SDouglas Gregor return; 1321524e33e1SDouglas Gregor } 1322524e33e1SDouglas Gregor 1323524e33e1SDouglas Gregor if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1324524e33e1SDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1325524e33e1SDouglas Gregor << OwningModule->getFullModuleName(); 1326524e33e1SDouglas Gregor HadError = true; 1327524e33e1SDouglas Gregor return; 1328524e33e1SDouglas Gregor } 1329524e33e1SDouglas Gregor 1330524e33e1SDouglas Gregor // Record this umbrella directory. 1331524e33e1SDouglas Gregor Map.setUmbrellaDir(ActiveModule, Dir); 1332524e33e1SDouglas Gregor } 1333524e33e1SDouglas Gregor 13342b82c2a5SDouglas Gregor /// \brief Parse a module export declaration. 13352b82c2a5SDouglas Gregor /// 13362b82c2a5SDouglas Gregor /// export-declaration: 13372b82c2a5SDouglas Gregor /// 'export' wildcard-module-id 13382b82c2a5SDouglas Gregor /// 13392b82c2a5SDouglas Gregor /// wildcard-module-id: 13402b82c2a5SDouglas Gregor /// identifier 13412b82c2a5SDouglas Gregor /// '*' 13422b82c2a5SDouglas Gregor /// identifier '.' wildcard-module-id 13432b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() { 13442b82c2a5SDouglas Gregor assert(Tok.is(MMToken::ExportKeyword)); 13452b82c2a5SDouglas Gregor SourceLocation ExportLoc = consumeToken(); 13462b82c2a5SDouglas Gregor 13472b82c2a5SDouglas Gregor // Parse the module-id with an optional wildcard at the end. 13482b82c2a5SDouglas Gregor ModuleId ParsedModuleId; 13492b82c2a5SDouglas Gregor bool Wildcard = false; 13502b82c2a5SDouglas Gregor do { 13512b82c2a5SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 13522b82c2a5SDouglas Gregor ParsedModuleId.push_back(std::make_pair(Tok.getString(), 13532b82c2a5SDouglas Gregor Tok.getLocation())); 13542b82c2a5SDouglas Gregor consumeToken(); 13552b82c2a5SDouglas Gregor 13562b82c2a5SDouglas Gregor if (Tok.is(MMToken::Period)) { 13572b82c2a5SDouglas Gregor consumeToken(); 13582b82c2a5SDouglas Gregor continue; 13592b82c2a5SDouglas Gregor } 13602b82c2a5SDouglas Gregor 13612b82c2a5SDouglas Gregor break; 13622b82c2a5SDouglas Gregor } 13632b82c2a5SDouglas Gregor 13642b82c2a5SDouglas Gregor if(Tok.is(MMToken::Star)) { 13652b82c2a5SDouglas Gregor Wildcard = true; 1366f5eedd05SDouglas Gregor consumeToken(); 13672b82c2a5SDouglas Gregor break; 13682b82c2a5SDouglas Gregor } 13692b82c2a5SDouglas Gregor 13702b82c2a5SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id); 13712b82c2a5SDouglas Gregor HadError = true; 13722b82c2a5SDouglas Gregor return; 13732b82c2a5SDouglas Gregor } while (true); 13742b82c2a5SDouglas Gregor 13752b82c2a5SDouglas Gregor Module::UnresolvedExportDecl Unresolved = { 13762b82c2a5SDouglas Gregor ExportLoc, ParsedModuleId, Wildcard 13772b82c2a5SDouglas Gregor }; 13782b82c2a5SDouglas Gregor ActiveModule->UnresolvedExports.push_back(Unresolved); 13792b82c2a5SDouglas Gregor } 13802b82c2a5SDouglas Gregor 138173441091SDouglas Gregor void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) { 138273441091SDouglas Gregor assert(Tok.is(MMToken::Star)); 138373441091SDouglas Gregor SourceLocation StarLoc = consumeToken(); 138473441091SDouglas Gregor bool Failed = false; 138573441091SDouglas Gregor 138673441091SDouglas Gregor // Inferred modules must be submodules. 138773441091SDouglas Gregor if (!ActiveModule) { 138873441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 138973441091SDouglas Gregor Failed = true; 139073441091SDouglas Gregor } 139173441091SDouglas Gregor 1392524e33e1SDouglas Gregor // Inferred modules must have umbrella directories. 1393524e33e1SDouglas Gregor if (!Failed && !ActiveModule->getUmbrellaDir()) { 139473441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 139573441091SDouglas Gregor Failed = true; 139673441091SDouglas Gregor } 139773441091SDouglas Gregor 139873441091SDouglas Gregor // Check for redefinition of an inferred module. 1399dd005f69SDouglas Gregor if (!Failed && ActiveModule->InferSubmodules) { 140073441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 1401dd005f69SDouglas Gregor if (ActiveModule->InferredSubmoduleLoc.isValid()) 1402dd005f69SDouglas Gregor Diags.Report(ActiveModule->InferredSubmoduleLoc, 140373441091SDouglas Gregor diag::note_mmap_prev_definition); 140473441091SDouglas Gregor Failed = true; 140573441091SDouglas Gregor } 140673441091SDouglas Gregor 140773441091SDouglas Gregor // If there were any problems with this inferred submodule, skip its body. 140873441091SDouglas Gregor if (Failed) { 140973441091SDouglas Gregor if (Tok.is(MMToken::LBrace)) { 141073441091SDouglas Gregor consumeToken(); 141173441091SDouglas Gregor skipUntil(MMToken::RBrace); 141273441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 141373441091SDouglas Gregor consumeToken(); 141473441091SDouglas Gregor } 141573441091SDouglas Gregor HadError = true; 141673441091SDouglas Gregor return; 141773441091SDouglas Gregor } 141873441091SDouglas Gregor 141973441091SDouglas Gregor // Note that we have an inferred submodule. 1420dd005f69SDouglas Gregor ActiveModule->InferSubmodules = true; 1421dd005f69SDouglas Gregor ActiveModule->InferredSubmoduleLoc = StarLoc; 1422dd005f69SDouglas Gregor ActiveModule->InferExplicitSubmodules = Explicit; 142373441091SDouglas Gregor 142473441091SDouglas Gregor // Parse the opening brace. 142573441091SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 142673441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 142773441091SDouglas Gregor HadError = true; 142873441091SDouglas Gregor return; 142973441091SDouglas Gregor } 143073441091SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 143173441091SDouglas Gregor 143273441091SDouglas Gregor // Parse the body of the inferred submodule. 143373441091SDouglas Gregor bool Done = false; 143473441091SDouglas Gregor do { 143573441091SDouglas Gregor switch (Tok.Kind) { 143673441091SDouglas Gregor case MMToken::EndOfFile: 143773441091SDouglas Gregor case MMToken::RBrace: 143873441091SDouglas Gregor Done = true; 143973441091SDouglas Gregor break; 144073441091SDouglas Gregor 144173441091SDouglas Gregor case MMToken::ExportKeyword: { 144273441091SDouglas Gregor consumeToken(); 144373441091SDouglas Gregor if (Tok.is(MMToken::Star)) 1444dd005f69SDouglas Gregor ActiveModule->InferExportWildcard = true; 144573441091SDouglas Gregor else 144673441091SDouglas Gregor Diags.Report(Tok.getLocation(), 144773441091SDouglas Gregor diag::err_mmap_expected_export_wildcard); 144873441091SDouglas Gregor consumeToken(); 144973441091SDouglas Gregor break; 145073441091SDouglas Gregor } 145173441091SDouglas Gregor 145273441091SDouglas Gregor case MMToken::ExplicitKeyword: 145373441091SDouglas Gregor case MMToken::ModuleKeyword: 145473441091SDouglas Gregor case MMToken::HeaderKeyword: 145573441091SDouglas Gregor case MMToken::UmbrellaKeyword: 145673441091SDouglas Gregor default: 145773441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member); 145873441091SDouglas Gregor consumeToken(); 145973441091SDouglas Gregor break; 146073441091SDouglas Gregor } 146173441091SDouglas Gregor } while (!Done); 146273441091SDouglas Gregor 146373441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 146473441091SDouglas Gregor consumeToken(); 146573441091SDouglas Gregor else { 146673441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 146773441091SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 146873441091SDouglas Gregor HadError = true; 146973441091SDouglas Gregor } 147073441091SDouglas Gregor } 147173441091SDouglas Gregor 14727033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence 14737033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null. 14747033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() { 14757033127bSDouglas Gregor for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) { 14767033127bSDouglas Gregor // If we have an umbrella directory, use that. 14777033127bSDouglas Gregor if (Mod->hasUmbrellaDir()) 14787033127bSDouglas Gregor return Mod->getUmbrellaDir(); 14797033127bSDouglas Gregor 14807033127bSDouglas Gregor // If we have a framework directory, stop looking. 14817033127bSDouglas Gregor if (Mod->IsFramework) 14827033127bSDouglas Gregor return 0; 14837033127bSDouglas Gregor } 14847033127bSDouglas Gregor 14857033127bSDouglas Gregor return 0; 14867033127bSDouglas Gregor } 14877033127bSDouglas Gregor 1488718292f2SDouglas Gregor /// \brief Parse a module map file. 1489718292f2SDouglas Gregor /// 1490718292f2SDouglas Gregor /// module-map-file: 1491718292f2SDouglas Gregor /// module-declaration* 1492718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() { 1493718292f2SDouglas Gregor do { 1494718292f2SDouglas Gregor switch (Tok.Kind) { 1495718292f2SDouglas Gregor case MMToken::EndOfFile: 1496718292f2SDouglas Gregor return HadError; 1497718292f2SDouglas Gregor 1498e7ab3669SDouglas Gregor case MMToken::ExplicitKeyword: 1499718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1500755b2055SDouglas Gregor case MMToken::FrameworkKeyword: 1501718292f2SDouglas Gregor parseModuleDecl(); 1502718292f2SDouglas Gregor break; 1503718292f2SDouglas Gregor 15041fb5c3a6SDouglas Gregor case MMToken::Comma: 150559527666SDouglas Gregor case MMToken::ExcludeKeyword: 15062b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 1507718292f2SDouglas Gregor case MMToken::HeaderKeyword: 1508718292f2SDouglas Gregor case MMToken::Identifier: 1509718292f2SDouglas Gregor case MMToken::LBrace: 1510a686e1b0SDouglas Gregor case MMToken::LSquare: 15112b82c2a5SDouglas Gregor case MMToken::Period: 1512718292f2SDouglas Gregor case MMToken::RBrace: 1513a686e1b0SDouglas Gregor case MMToken::RSquare: 15141fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 15152b82c2a5SDouglas Gregor case MMToken::Star: 1516718292f2SDouglas Gregor case MMToken::StringLiteral: 1517718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 1518718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1519718292f2SDouglas Gregor HadError = true; 1520718292f2SDouglas Gregor consumeToken(); 1521718292f2SDouglas Gregor break; 1522718292f2SDouglas Gregor } 1523718292f2SDouglas Gregor } while (true); 1524718292f2SDouglas Gregor } 1525718292f2SDouglas Gregor 1526718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) { 152789929282SDouglas Gregor assert(Target != 0 && "Missing target information"); 1528718292f2SDouglas Gregor FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User); 1529718292f2SDouglas Gregor const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID); 1530718292f2SDouglas Gregor if (!Buffer) 1531718292f2SDouglas Gregor return true; 1532718292f2SDouglas Gregor 1533718292f2SDouglas Gregor // Parse this module map file. 15341fb5c3a6SDouglas Gregor Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts); 15351fb5c3a6SDouglas Gregor Diags->getClient()->BeginSourceFile(MMapLangOpts); 1536*bc10b9fbSDouglas Gregor ModuleMapParser Parser(L, *SourceMgr, Target, *Diags, *this, File->getDir(), 15373ec6663bSDouglas Gregor BuiltinIncludeDir); 1538718292f2SDouglas Gregor bool Result = Parser.parseModuleMapFile(); 1539718292f2SDouglas Gregor Diags->getClient()->EndSourceFile(); 1540718292f2SDouglas Gregor 1541718292f2SDouglas Gregor return Result; 1542718292f2SDouglas Gregor } 1543