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" 29718292f2SDouglas Gregor using namespace clang; 30718292f2SDouglas Gregor 312b82c2a5SDouglas Gregor Module::ExportDecl 322b82c2a5SDouglas Gregor ModuleMap::resolveExport(Module *Mod, 332b82c2a5SDouglas Gregor const Module::UnresolvedExportDecl &Unresolved, 342b82c2a5SDouglas Gregor bool Complain) { 35f5eedd05SDouglas Gregor // We may have just a wildcard. 36f5eedd05SDouglas Gregor if (Unresolved.Id.empty()) { 37f5eedd05SDouglas Gregor assert(Unresolved.Wildcard && "Invalid unresolved export"); 38f5eedd05SDouglas Gregor return Module::ExportDecl(0, true); 39f5eedd05SDouglas Gregor } 40f5eedd05SDouglas Gregor 412b82c2a5SDouglas Gregor // Find the starting module. 422b82c2a5SDouglas Gregor Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod); 432b82c2a5SDouglas Gregor if (!Context) { 442b82c2a5SDouglas Gregor if (Complain) 452b82c2a5SDouglas Gregor Diags->Report(Unresolved.Id[0].second, 462b82c2a5SDouglas Gregor diag::err_mmap_missing_module_unqualified) 472b82c2a5SDouglas Gregor << Unresolved.Id[0].first << Mod->getFullModuleName(); 482b82c2a5SDouglas Gregor 492b82c2a5SDouglas Gregor return Module::ExportDecl(); 502b82c2a5SDouglas Gregor } 512b82c2a5SDouglas Gregor 522b82c2a5SDouglas Gregor // Dig into the module path. 532b82c2a5SDouglas Gregor for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) { 542b82c2a5SDouglas Gregor Module *Sub = lookupModuleQualified(Unresolved.Id[I].first, 552b82c2a5SDouglas Gregor Context); 562b82c2a5SDouglas Gregor if (!Sub) { 572b82c2a5SDouglas Gregor if (Complain) 582b82c2a5SDouglas Gregor Diags->Report(Unresolved.Id[I].second, 592b82c2a5SDouglas Gregor diag::err_mmap_missing_module_qualified) 602b82c2a5SDouglas Gregor << Unresolved.Id[I].first << Context->getFullModuleName() 612b82c2a5SDouglas Gregor << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second); 622b82c2a5SDouglas Gregor 632b82c2a5SDouglas Gregor return Module::ExportDecl(); 642b82c2a5SDouglas Gregor } 652b82c2a5SDouglas Gregor 662b82c2a5SDouglas Gregor Context = Sub; 672b82c2a5SDouglas Gregor } 682b82c2a5SDouglas Gregor 692b82c2a5SDouglas Gregor return Module::ExportDecl(Context, Unresolved.Wildcard); 702b82c2a5SDouglas Gregor } 712b82c2a5SDouglas Gregor 721fb5c3a6SDouglas Gregor ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC, 7389929282SDouglas Gregor const LangOptions &LangOpts, const TargetInfo *Target) 743ec6663bSDouglas Gregor : LangOpts(LangOpts), Target(Target), BuiltinIncludeDir(0) 751fb5c3a6SDouglas Gregor { 76c95d8192SDylan Noblesmith IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs); 77c95d8192SDylan Noblesmith Diags = IntrusiveRefCntPtr<DiagnosticsEngine>( 78718292f2SDouglas Gregor new DiagnosticsEngine(DiagIDs)); 79718292f2SDouglas Gregor Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true); 80718292f2SDouglas Gregor SourceMgr = new SourceManager(*Diags, FileMgr); 81718292f2SDouglas Gregor } 82718292f2SDouglas Gregor 83718292f2SDouglas Gregor ModuleMap::~ModuleMap() { 845acdf59eSDouglas Gregor for (llvm::StringMap<Module *>::iterator I = Modules.begin(), 855acdf59eSDouglas Gregor IEnd = Modules.end(); 865acdf59eSDouglas Gregor I != IEnd; ++I) { 875acdf59eSDouglas Gregor delete I->getValue(); 885acdf59eSDouglas Gregor } 895acdf59eSDouglas Gregor 90718292f2SDouglas Gregor delete SourceMgr; 91718292f2SDouglas Gregor } 92718292f2SDouglas Gregor 9389929282SDouglas Gregor void ModuleMap::setTarget(const TargetInfo &Target) { 9489929282SDouglas Gregor assert((!this->Target || this->Target == &Target) && 9589929282SDouglas Gregor "Improper target override"); 9689929282SDouglas Gregor this->Target = &Target; 9789929282SDouglas Gregor } 9889929282SDouglas Gregor 99de3ef502SDouglas Gregor Module *ModuleMap::findModuleForHeader(const FileEntry *File) { 100ab0c8a84SDouglas Gregor llvm::DenseMap<const FileEntry *, Module *>::iterator Known 101ab0c8a84SDouglas Gregor = Headers.find(File); 1021fb5c3a6SDouglas Gregor if (Known != Headers.end()) { 1031fb5c3a6SDouglas Gregor // If a header corresponds to an unavailable module, don't report 1041fb5c3a6SDouglas Gregor // that it maps to anything. 1051fb5c3a6SDouglas Gregor if (!Known->second->isAvailable()) 1061fb5c3a6SDouglas Gregor return 0; 1071fb5c3a6SDouglas Gregor 108ab0c8a84SDouglas Gregor return Known->second; 1091fb5c3a6SDouglas Gregor } 110ab0c8a84SDouglas Gregor 111b65dbfffSDouglas Gregor const DirectoryEntry *Dir = File->getDir(); 112b65dbfffSDouglas Gregor llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs; 113b65dbfffSDouglas Gregor StringRef DirName = Dir->getName(); 114a89c5ac4SDouglas Gregor 115a89c5ac4SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 116a89c5ac4SDouglas Gregor // an umbrella header. 117b65dbfffSDouglas Gregor do { 118a89c5ac4SDouglas Gregor llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir 119a89c5ac4SDouglas Gregor = UmbrellaDirs.find(Dir); 120a89c5ac4SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 121a89c5ac4SDouglas Gregor Module *Result = KnownDir->second; 122930a85ccSDouglas Gregor 123930a85ccSDouglas Gregor // Search up the module stack until we find a module with an umbrella 12473141fa9SDouglas Gregor // directory. 125930a85ccSDouglas Gregor Module *UmbrellaModule = Result; 12673141fa9SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 127930a85ccSDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 128930a85ccSDouglas Gregor 129930a85ccSDouglas Gregor if (UmbrellaModule->InferSubmodules) { 130a89c5ac4SDouglas Gregor // Infer submodules for each of the directories we found between 131a89c5ac4SDouglas Gregor // the directory of the umbrella header and the directory where 132a89c5ac4SDouglas Gregor // the actual header is located. 1339458f82dSDouglas Gregor bool Explicit = UmbrellaModule->InferExplicitSubmodules; 1349458f82dSDouglas Gregor 1357033127bSDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 136a89c5ac4SDouglas Gregor // Find or create the module that corresponds to this directory name. 137a89c5ac4SDouglas Gregor StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName()); 138a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 1399458f82dSDouglas Gregor Explicit).first; 140a89c5ac4SDouglas Gregor 141a89c5ac4SDouglas Gregor // Associate the module and the directory. 142a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I-1]] = Result; 143a89c5ac4SDouglas Gregor 144a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 145a89c5ac4SDouglas Gregor // wildcard to the set of exports. 146930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 147a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 148a89c5ac4SDouglas Gregor } 149a89c5ac4SDouglas Gregor 150a89c5ac4SDouglas Gregor // Infer a submodule with the same name as this header file. 151a89c5ac4SDouglas Gregor StringRef Name = llvm::sys::path::stem(File->getName()); 152a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 1539458f82dSDouglas Gregor Explicit).first; 154a89c5ac4SDouglas Gregor 155a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 156a89c5ac4SDouglas Gregor // wildcard to the set of exports. 157930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 158a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 159a89c5ac4SDouglas Gregor } else { 160a89c5ac4SDouglas Gregor // Record each of the directories we stepped through as being part of 161a89c5ac4SDouglas Gregor // the module we found, since the umbrella header covers them all. 162a89c5ac4SDouglas Gregor for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 163a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I]] = Result; 164a89c5ac4SDouglas Gregor } 165a89c5ac4SDouglas Gregor 166a89c5ac4SDouglas Gregor Headers[File] = Result; 1671fb5c3a6SDouglas Gregor 1681fb5c3a6SDouglas Gregor // If a header corresponds to an unavailable module, don't report 1691fb5c3a6SDouglas Gregor // that it maps to anything. 1701fb5c3a6SDouglas Gregor if (!Result->isAvailable()) 1711fb5c3a6SDouglas Gregor return 0; 1721fb5c3a6SDouglas Gregor 173a89c5ac4SDouglas Gregor return Result; 174a89c5ac4SDouglas Gregor } 175a89c5ac4SDouglas Gregor 176a89c5ac4SDouglas Gregor SkippedDirs.push_back(Dir); 177a89c5ac4SDouglas Gregor 178b65dbfffSDouglas Gregor // Retrieve our parent path. 179b65dbfffSDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 180b65dbfffSDouglas Gregor if (DirName.empty()) 181b65dbfffSDouglas Gregor break; 182b65dbfffSDouglas Gregor 183b65dbfffSDouglas Gregor // Resolve the parent path to a directory entry. 184b65dbfffSDouglas Gregor Dir = SourceMgr->getFileManager().getDirectory(DirName); 185a89c5ac4SDouglas Gregor } while (Dir); 186b65dbfffSDouglas Gregor 187ab0c8a84SDouglas Gregor return 0; 188ab0c8a84SDouglas Gregor } 189ab0c8a84SDouglas Gregor 1901fb5c3a6SDouglas Gregor bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) { 1911fb5c3a6SDouglas Gregor llvm::DenseMap<const FileEntry *, Module *>::iterator Known 1921fb5c3a6SDouglas Gregor = Headers.find(Header); 1931fb5c3a6SDouglas Gregor if (Known != Headers.end()) 1941fb5c3a6SDouglas Gregor return !Known->second->isAvailable(); 1951fb5c3a6SDouglas Gregor 1961fb5c3a6SDouglas Gregor const DirectoryEntry *Dir = Header->getDir(); 1971fb5c3a6SDouglas Gregor llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs; 1981fb5c3a6SDouglas Gregor StringRef DirName = Dir->getName(); 1991fb5c3a6SDouglas Gregor 2001fb5c3a6SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 2011fb5c3a6SDouglas Gregor // an umbrella header. 2021fb5c3a6SDouglas Gregor do { 2031fb5c3a6SDouglas Gregor llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir 2041fb5c3a6SDouglas Gregor = UmbrellaDirs.find(Dir); 2051fb5c3a6SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 2061fb5c3a6SDouglas Gregor Module *Found = KnownDir->second; 2071fb5c3a6SDouglas Gregor if (!Found->isAvailable()) 2081fb5c3a6SDouglas Gregor return true; 2091fb5c3a6SDouglas Gregor 2101fb5c3a6SDouglas Gregor // Search up the module stack until we find a module with an umbrella 2111fb5c3a6SDouglas Gregor // directory. 2121fb5c3a6SDouglas Gregor Module *UmbrellaModule = Found; 2131fb5c3a6SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 2141fb5c3a6SDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 2151fb5c3a6SDouglas Gregor 2161fb5c3a6SDouglas Gregor if (UmbrellaModule->InferSubmodules) { 2171fb5c3a6SDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 2181fb5c3a6SDouglas Gregor // Find or create the module that corresponds to this directory name. 2191fb5c3a6SDouglas Gregor StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName()); 2201fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 2211fb5c3a6SDouglas Gregor if (!Found) 2221fb5c3a6SDouglas Gregor return false; 2231fb5c3a6SDouglas Gregor if (!Found->isAvailable()) 2241fb5c3a6SDouglas Gregor return true; 2251fb5c3a6SDouglas Gregor } 2261fb5c3a6SDouglas Gregor 2271fb5c3a6SDouglas Gregor // Infer a submodule with the same name as this header file. 2281fb5c3a6SDouglas Gregor StringRef Name = llvm::sys::path::stem(Header->getName()); 2291fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 2301fb5c3a6SDouglas Gregor if (!Found) 2311fb5c3a6SDouglas Gregor return false; 2321fb5c3a6SDouglas Gregor } 2331fb5c3a6SDouglas Gregor 2341fb5c3a6SDouglas Gregor return !Found->isAvailable(); 2351fb5c3a6SDouglas Gregor } 2361fb5c3a6SDouglas Gregor 2371fb5c3a6SDouglas Gregor SkippedDirs.push_back(Dir); 2381fb5c3a6SDouglas Gregor 2391fb5c3a6SDouglas Gregor // Retrieve our parent path. 2401fb5c3a6SDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 2411fb5c3a6SDouglas Gregor if (DirName.empty()) 2421fb5c3a6SDouglas Gregor break; 2431fb5c3a6SDouglas Gregor 2441fb5c3a6SDouglas Gregor // Resolve the parent path to a directory entry. 2451fb5c3a6SDouglas Gregor Dir = SourceMgr->getFileManager().getDirectory(DirName); 2461fb5c3a6SDouglas Gregor } while (Dir); 2471fb5c3a6SDouglas Gregor 2481fb5c3a6SDouglas Gregor return false; 2491fb5c3a6SDouglas Gregor } 2501fb5c3a6SDouglas Gregor 251de3ef502SDouglas Gregor Module *ModuleMap::findModule(StringRef Name) { 25288bdfb0eSDouglas Gregor llvm::StringMap<Module *>::iterator Known = Modules.find(Name); 25388bdfb0eSDouglas Gregor if (Known != Modules.end()) 25488bdfb0eSDouglas Gregor return Known->getValue(); 25588bdfb0eSDouglas Gregor 25688bdfb0eSDouglas Gregor return 0; 25788bdfb0eSDouglas Gregor } 25888bdfb0eSDouglas Gregor 2592b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) { 2602b82c2a5SDouglas Gregor for(; Context; Context = Context->Parent) { 2612b82c2a5SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Context)) 2622b82c2a5SDouglas Gregor return Sub; 2632b82c2a5SDouglas Gregor } 2642b82c2a5SDouglas Gregor 2652b82c2a5SDouglas Gregor return findModule(Name); 2662b82c2a5SDouglas Gregor } 2672b82c2a5SDouglas Gregor 2682b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) { 2692b82c2a5SDouglas Gregor if (!Context) 2702b82c2a5SDouglas Gregor return findModule(Name); 2712b82c2a5SDouglas Gregor 272eb90e830SDouglas Gregor return Context->findSubmodule(Name); 2732b82c2a5SDouglas Gregor } 2742b82c2a5SDouglas Gregor 275de3ef502SDouglas Gregor std::pair<Module *, bool> 27669021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 27769021974SDouglas Gregor bool IsExplicit) { 27869021974SDouglas Gregor // Try to find an existing module with this name. 279eb90e830SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Parent)) 280eb90e830SDouglas Gregor return std::make_pair(Sub, false); 28169021974SDouglas Gregor 28269021974SDouglas Gregor // Create a new module with this name. 28369021974SDouglas Gregor Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework, 28469021974SDouglas Gregor IsExplicit); 285eb90e830SDouglas Gregor if (!Parent) 28669021974SDouglas Gregor Modules[Name] = Result; 28769021974SDouglas Gregor return std::make_pair(Result, true); 28869021974SDouglas Gregor } 28969021974SDouglas Gregor 290de3ef502SDouglas Gregor Module * 29156c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName, 292e89dbc1dSDouglas Gregor const DirectoryEntry *FrameworkDir, 293a686e1b0SDouglas Gregor bool IsSystem, 294e89dbc1dSDouglas Gregor Module *Parent) { 29556c64013SDouglas Gregor // Check whether we've already found this module. 296e89dbc1dSDouglas Gregor if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 297e89dbc1dSDouglas Gregor return Mod; 298e89dbc1dSDouglas Gregor 299e89dbc1dSDouglas Gregor FileManager &FileMgr = SourceMgr->getFileManager(); 30056c64013SDouglas Gregor 30156c64013SDouglas Gregor // Look for an umbrella header. 3022c1dd271SDylan Noblesmith SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 30356c64013SDouglas Gregor llvm::sys::path::append(UmbrellaName, "Headers"); 30456c64013SDouglas Gregor llvm::sys::path::append(UmbrellaName, ModuleName + ".h"); 305e89dbc1dSDouglas Gregor const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 30656c64013SDouglas Gregor 30756c64013SDouglas Gregor // FIXME: If there's no umbrella header, we could probably scan the 30856c64013SDouglas Gregor // framework to load *everything*. But, it's not clear that this is a good 30956c64013SDouglas Gregor // idea. 31056c64013SDouglas Gregor if (!UmbrellaHeader) 31156c64013SDouglas Gregor return 0; 31256c64013SDouglas Gregor 313e89dbc1dSDouglas Gregor Module *Result = new Module(ModuleName, SourceLocation(), Parent, 314e89dbc1dSDouglas Gregor /*IsFramework=*/true, /*IsExplicit=*/false); 315a686e1b0SDouglas Gregor if (IsSystem) 316a686e1b0SDouglas Gregor Result->IsSystem = IsSystem; 317a686e1b0SDouglas Gregor 318eb90e830SDouglas Gregor if (!Parent) 319e89dbc1dSDouglas Gregor Modules[ModuleName] = Result; 320e89dbc1dSDouglas Gregor 321322f633cSDouglas Gregor // umbrella header "umbrella-header-name" 32273141fa9SDouglas Gregor Result->Umbrella = UmbrellaHeader; 32356c64013SDouglas Gregor Headers[UmbrellaHeader] = Result; 3244dc71835SDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Result; 325d8bd7537SDouglas Gregor 326d8bd7537SDouglas Gregor // export * 327d8bd7537SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 328d8bd7537SDouglas Gregor 329a89c5ac4SDouglas Gregor // module * { export * } 330a89c5ac4SDouglas Gregor Result->InferSubmodules = true; 331a89c5ac4SDouglas Gregor Result->InferExportWildcard = true; 332a89c5ac4SDouglas Gregor 333e89dbc1dSDouglas Gregor // Look for subframeworks. 334e89dbc1dSDouglas Gregor llvm::error_code EC; 3352c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirName 336ddaa69cbSDouglas Gregor = StringRef(FrameworkDir->getName()); 337e89dbc1dSDouglas Gregor llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 3382c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirNameNative; 339ddaa69cbSDouglas Gregor llvm::sys::path::native(SubframeworksDirName.str(), 340ddaa69cbSDouglas Gregor SubframeworksDirNameNative); 341ddaa69cbSDouglas Gregor for (llvm::sys::fs::directory_iterator 342ddaa69cbSDouglas Gregor Dir(SubframeworksDirNameNative.str(), EC), DirEnd; 343e89dbc1dSDouglas Gregor Dir != DirEnd && !EC; Dir.increment(EC)) { 344e89dbc1dSDouglas Gregor if (!StringRef(Dir->path()).endswith(".framework")) 345e89dbc1dSDouglas Gregor continue; 346f2161a70SDouglas Gregor 347e89dbc1dSDouglas Gregor if (const DirectoryEntry *SubframeworkDir 348e89dbc1dSDouglas Gregor = FileMgr.getDirectory(Dir->path())) { 349e89dbc1dSDouglas Gregor // FIXME: Do we want to warn about subframeworks without umbrella headers? 350e89dbc1dSDouglas Gregor inferFrameworkModule(llvm::sys::path::stem(Dir->path()), SubframeworkDir, 351a686e1b0SDouglas Gregor IsSystem, Result); 352e89dbc1dSDouglas Gregor } 353e89dbc1dSDouglas Gregor } 354e89dbc1dSDouglas Gregor 35556c64013SDouglas Gregor return Result; 35656c64013SDouglas Gregor } 35756c64013SDouglas Gregor 358a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ 359a89c5ac4SDouglas Gregor Headers[UmbrellaHeader] = Mod; 36073141fa9SDouglas Gregor Mod->Umbrella = UmbrellaHeader; 3617033127bSDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; 362a89c5ac4SDouglas Gregor } 363a89c5ac4SDouglas Gregor 364524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) { 365524e33e1SDouglas Gregor Mod->Umbrella = UmbrellaDir; 366524e33e1SDouglas Gregor UmbrellaDirs[UmbrellaDir] = Mod; 367524e33e1SDouglas Gregor } 368524e33e1SDouglas Gregor 369a89c5ac4SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header) { 370a89c5ac4SDouglas Gregor Mod->Headers.push_back(Header); 371a89c5ac4SDouglas Gregor Headers[Header] = Mod; 372a89c5ac4SDouglas Gregor } 373a89c5ac4SDouglas Gregor 374514b636aSDouglas Gregor const FileEntry * 375de3ef502SDouglas Gregor ModuleMap::getContainingModuleMapFile(Module *Module) { 376514b636aSDouglas Gregor if (Module->DefinitionLoc.isInvalid() || !SourceMgr) 377514b636aSDouglas Gregor return 0; 378514b636aSDouglas Gregor 379514b636aSDouglas Gregor return SourceMgr->getFileEntryForID( 380514b636aSDouglas Gregor SourceMgr->getFileID(Module->DefinitionLoc)); 381514b636aSDouglas Gregor } 382514b636aSDouglas Gregor 383718292f2SDouglas Gregor void ModuleMap::dump() { 384718292f2SDouglas Gregor llvm::errs() << "Modules:"; 385718292f2SDouglas Gregor for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 386718292f2SDouglas Gregor MEnd = Modules.end(); 387718292f2SDouglas Gregor M != MEnd; ++M) 388d28d1b8dSDouglas Gregor M->getValue()->print(llvm::errs(), 2); 389718292f2SDouglas Gregor 390718292f2SDouglas Gregor llvm::errs() << "Headers:"; 391718292f2SDouglas Gregor for (llvm::DenseMap<const FileEntry *, Module *>::iterator 392718292f2SDouglas Gregor H = Headers.begin(), 393718292f2SDouglas Gregor HEnd = Headers.end(); 394718292f2SDouglas Gregor H != HEnd; ++H) { 395718292f2SDouglas Gregor llvm::errs() << " \"" << H->first->getName() << "\" -> " 396718292f2SDouglas Gregor << H->second->getFullModuleName() << "\n"; 397718292f2SDouglas Gregor } 398718292f2SDouglas Gregor } 399718292f2SDouglas Gregor 4002b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 4012b82c2a5SDouglas Gregor bool HadError = false; 4022b82c2a5SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { 4032b82c2a5SDouglas Gregor Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 4042b82c2a5SDouglas Gregor Complain); 405f5eedd05SDouglas Gregor if (Export.getPointer() || Export.getInt()) 4062b82c2a5SDouglas Gregor Mod->Exports.push_back(Export); 4072b82c2a5SDouglas Gregor else 4082b82c2a5SDouglas Gregor HadError = true; 4092b82c2a5SDouglas Gregor } 4102b82c2a5SDouglas Gregor Mod->UnresolvedExports.clear(); 4112b82c2a5SDouglas Gregor return HadError; 4122b82c2a5SDouglas Gregor } 4132b82c2a5SDouglas Gregor 4140093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 4150093b3c7SDouglas Gregor if (Loc.isInvalid()) 4160093b3c7SDouglas Gregor return 0; 4170093b3c7SDouglas Gregor 4180093b3c7SDouglas Gregor // Use the expansion location to determine which module we're in. 4190093b3c7SDouglas Gregor FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 4200093b3c7SDouglas Gregor if (!ExpansionLoc.isFileID()) 4210093b3c7SDouglas Gregor return 0; 4220093b3c7SDouglas Gregor 4230093b3c7SDouglas Gregor 4240093b3c7SDouglas Gregor const SourceManager &SrcMgr = Loc.getManager(); 4250093b3c7SDouglas Gregor FileID ExpansionFileID = ExpansionLoc.getFileID(); 426224d8a74SDouglas Gregor 427224d8a74SDouglas Gregor while (const FileEntry *ExpansionFile 428224d8a74SDouglas Gregor = SrcMgr.getFileEntryForID(ExpansionFileID)) { 429224d8a74SDouglas Gregor // Find the module that owns this header (if any). 430224d8a74SDouglas Gregor if (Module *Mod = findModuleForHeader(ExpansionFile)) 431224d8a74SDouglas Gregor return Mod; 432224d8a74SDouglas Gregor 433224d8a74SDouglas Gregor // No module owns this header, so look up the inclusion chain to see if 434224d8a74SDouglas Gregor // any included header has an associated module. 435224d8a74SDouglas Gregor SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); 436224d8a74SDouglas Gregor if (IncludeLoc.isInvalid()) 4370093b3c7SDouglas Gregor return 0; 4380093b3c7SDouglas Gregor 439224d8a74SDouglas Gregor ExpansionFileID = SrcMgr.getFileID(IncludeLoc); 440224d8a74SDouglas Gregor } 441224d8a74SDouglas Gregor 442224d8a74SDouglas Gregor return 0; 4430093b3c7SDouglas Gregor } 4440093b3c7SDouglas Gregor 445718292f2SDouglas Gregor //----------------------------------------------------------------------------// 446718292f2SDouglas Gregor // Module map file parser 447718292f2SDouglas Gregor //----------------------------------------------------------------------------// 448718292f2SDouglas Gregor 449718292f2SDouglas Gregor namespace clang { 450718292f2SDouglas Gregor /// \brief A token in a module map file. 451718292f2SDouglas Gregor struct MMToken { 452718292f2SDouglas Gregor enum TokenKind { 4531fb5c3a6SDouglas Gregor Comma, 454718292f2SDouglas Gregor EndOfFile, 455718292f2SDouglas Gregor HeaderKeyword, 456718292f2SDouglas Gregor Identifier, 457718292f2SDouglas Gregor ExplicitKeyword, 4582b82c2a5SDouglas Gregor ExportKeyword, 459755b2055SDouglas Gregor FrameworkKeyword, 460718292f2SDouglas Gregor ModuleKeyword, 4612b82c2a5SDouglas Gregor Period, 462718292f2SDouglas Gregor UmbrellaKeyword, 4631fb5c3a6SDouglas Gregor RequiresKeyword, 4642b82c2a5SDouglas Gregor Star, 465718292f2SDouglas Gregor StringLiteral, 466718292f2SDouglas Gregor LBrace, 467a686e1b0SDouglas Gregor RBrace, 468a686e1b0SDouglas Gregor LSquare, 469a686e1b0SDouglas Gregor RSquare 470718292f2SDouglas Gregor } Kind; 471718292f2SDouglas Gregor 472718292f2SDouglas Gregor unsigned Location; 473718292f2SDouglas Gregor unsigned StringLength; 474718292f2SDouglas Gregor const char *StringData; 475718292f2SDouglas Gregor 476718292f2SDouglas Gregor void clear() { 477718292f2SDouglas Gregor Kind = EndOfFile; 478718292f2SDouglas Gregor Location = 0; 479718292f2SDouglas Gregor StringLength = 0; 480718292f2SDouglas Gregor StringData = 0; 481718292f2SDouglas Gregor } 482718292f2SDouglas Gregor 483718292f2SDouglas Gregor bool is(TokenKind K) const { return Kind == K; } 484718292f2SDouglas Gregor 485718292f2SDouglas Gregor SourceLocation getLocation() const { 486718292f2SDouglas Gregor return SourceLocation::getFromRawEncoding(Location); 487718292f2SDouglas Gregor } 488718292f2SDouglas Gregor 489718292f2SDouglas Gregor StringRef getString() const { 490718292f2SDouglas Gregor return StringRef(StringData, StringLength); 491718292f2SDouglas Gregor } 492718292f2SDouglas Gregor }; 493718292f2SDouglas Gregor 494718292f2SDouglas Gregor class ModuleMapParser { 495718292f2SDouglas Gregor Lexer &L; 496718292f2SDouglas Gregor SourceManager &SourceMgr; 497718292f2SDouglas Gregor DiagnosticsEngine &Diags; 498718292f2SDouglas Gregor ModuleMap ⤅ 499718292f2SDouglas Gregor 5005257fc63SDouglas Gregor /// \brief The directory that this module map resides in. 5015257fc63SDouglas Gregor const DirectoryEntry *Directory; 5025257fc63SDouglas Gregor 5033ec6663bSDouglas Gregor /// \brief The directory containing Clang-supplied headers. 5043ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir; 5053ec6663bSDouglas Gregor 506718292f2SDouglas Gregor /// \brief Whether an error occurred. 507718292f2SDouglas Gregor bool HadError; 508718292f2SDouglas Gregor 509718292f2SDouglas Gregor /// \brief Default target information, used only for string literal 510718292f2SDouglas Gregor /// parsing. 511718292f2SDouglas Gregor TargetInfo *Target; 512718292f2SDouglas Gregor 513718292f2SDouglas Gregor /// \brief Stores string data for the various string literals referenced 514718292f2SDouglas Gregor /// during parsing. 515718292f2SDouglas Gregor llvm::BumpPtrAllocator StringData; 516718292f2SDouglas Gregor 517718292f2SDouglas Gregor /// \brief The current token. 518718292f2SDouglas Gregor MMToken Tok; 519718292f2SDouglas Gregor 520718292f2SDouglas Gregor /// \brief The active module. 521de3ef502SDouglas Gregor Module *ActiveModule; 522718292f2SDouglas Gregor 523718292f2SDouglas Gregor /// \brief Consume the current token and return its location. 524718292f2SDouglas Gregor SourceLocation consumeToken(); 525718292f2SDouglas Gregor 526718292f2SDouglas Gregor /// \brief Skip tokens until we reach the a token with the given kind 527718292f2SDouglas Gregor /// (or the end of the file). 528718292f2SDouglas Gregor void skipUntil(MMToken::TokenKind K); 529718292f2SDouglas Gregor 530e7ab3669SDouglas Gregor typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2> 531e7ab3669SDouglas Gregor ModuleId; 532e7ab3669SDouglas Gregor bool parseModuleId(ModuleId &Id); 533718292f2SDouglas Gregor void parseModuleDecl(); 5341fb5c3a6SDouglas Gregor void parseRequiresDecl(); 535322f633cSDouglas Gregor void parseHeaderDecl(SourceLocation UmbrellaLoc); 536524e33e1SDouglas Gregor void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 5372b82c2a5SDouglas Gregor void parseExportDecl(); 53873441091SDouglas Gregor void parseInferredSubmoduleDecl(bool Explicit); 539718292f2SDouglas Gregor 5407033127bSDouglas Gregor const DirectoryEntry *getOverriddenHeaderSearchDir(); 5417033127bSDouglas Gregor 542718292f2SDouglas Gregor public: 543718292f2SDouglas Gregor explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 544718292f2SDouglas Gregor DiagnosticsEngine &Diags, 5455257fc63SDouglas Gregor ModuleMap &Map, 5463ec6663bSDouglas Gregor const DirectoryEntry *Directory, 5473ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir) 5485257fc63SDouglas Gregor : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map), 5493ec6663bSDouglas Gregor Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir), 5503ec6663bSDouglas Gregor HadError(false), ActiveModule(0) 551718292f2SDouglas Gregor { 552718292f2SDouglas Gregor TargetOptions TargetOpts; 553718292f2SDouglas Gregor TargetOpts.Triple = llvm::sys::getDefaultTargetTriple(); 554718292f2SDouglas Gregor Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts); 555718292f2SDouglas Gregor 556718292f2SDouglas Gregor Tok.clear(); 557718292f2SDouglas Gregor consumeToken(); 558718292f2SDouglas Gregor } 559718292f2SDouglas Gregor 560718292f2SDouglas Gregor bool parseModuleMapFile(); 561718292f2SDouglas Gregor }; 562718292f2SDouglas Gregor } 563718292f2SDouglas Gregor 564718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() { 565718292f2SDouglas Gregor retry: 566718292f2SDouglas Gregor SourceLocation Result = Tok.getLocation(); 567718292f2SDouglas Gregor Tok.clear(); 568718292f2SDouglas Gregor 569718292f2SDouglas Gregor Token LToken; 570718292f2SDouglas Gregor L.LexFromRawLexer(LToken); 571718292f2SDouglas Gregor Tok.Location = LToken.getLocation().getRawEncoding(); 572718292f2SDouglas Gregor switch (LToken.getKind()) { 573718292f2SDouglas Gregor case tok::raw_identifier: 574718292f2SDouglas Gregor Tok.StringData = LToken.getRawIdentifierData(); 575718292f2SDouglas Gregor Tok.StringLength = LToken.getLength(); 576718292f2SDouglas Gregor Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString()) 577718292f2SDouglas Gregor .Case("header", MMToken::HeaderKeyword) 578718292f2SDouglas Gregor .Case("explicit", MMToken::ExplicitKeyword) 5792b82c2a5SDouglas Gregor .Case("export", MMToken::ExportKeyword) 580755b2055SDouglas Gregor .Case("framework", MMToken::FrameworkKeyword) 581718292f2SDouglas Gregor .Case("module", MMToken::ModuleKeyword) 5821fb5c3a6SDouglas Gregor .Case("requires", MMToken::RequiresKeyword) 583718292f2SDouglas Gregor .Case("umbrella", MMToken::UmbrellaKeyword) 584718292f2SDouglas Gregor .Default(MMToken::Identifier); 585718292f2SDouglas Gregor break; 586718292f2SDouglas Gregor 5871fb5c3a6SDouglas Gregor case tok::comma: 5881fb5c3a6SDouglas Gregor Tok.Kind = MMToken::Comma; 5891fb5c3a6SDouglas Gregor break; 5901fb5c3a6SDouglas Gregor 591718292f2SDouglas Gregor case tok::eof: 592718292f2SDouglas Gregor Tok.Kind = MMToken::EndOfFile; 593718292f2SDouglas Gregor break; 594718292f2SDouglas Gregor 595718292f2SDouglas Gregor case tok::l_brace: 596718292f2SDouglas Gregor Tok.Kind = MMToken::LBrace; 597718292f2SDouglas Gregor break; 598718292f2SDouglas Gregor 599a686e1b0SDouglas Gregor case tok::l_square: 600a686e1b0SDouglas Gregor Tok.Kind = MMToken::LSquare; 601a686e1b0SDouglas Gregor break; 602a686e1b0SDouglas Gregor 6032b82c2a5SDouglas Gregor case tok::period: 6042b82c2a5SDouglas Gregor Tok.Kind = MMToken::Period; 6052b82c2a5SDouglas Gregor break; 6062b82c2a5SDouglas Gregor 607718292f2SDouglas Gregor case tok::r_brace: 608718292f2SDouglas Gregor Tok.Kind = MMToken::RBrace; 609718292f2SDouglas Gregor break; 610718292f2SDouglas Gregor 611a686e1b0SDouglas Gregor case tok::r_square: 612a686e1b0SDouglas Gregor Tok.Kind = MMToken::RSquare; 613a686e1b0SDouglas Gregor break; 614a686e1b0SDouglas Gregor 6152b82c2a5SDouglas Gregor case tok::star: 6162b82c2a5SDouglas Gregor Tok.Kind = MMToken::Star; 6172b82c2a5SDouglas Gregor break; 6182b82c2a5SDouglas Gregor 619718292f2SDouglas Gregor case tok::string_literal: { 620*d67aea28SRichard Smith if (LToken.hasUDSuffix()) { 621*d67aea28SRichard Smith Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 622*d67aea28SRichard Smith HadError = true; 623*d67aea28SRichard Smith goto retry; 624*d67aea28SRichard Smith } 625*d67aea28SRichard Smith 626718292f2SDouglas Gregor // Parse the string literal. 627718292f2SDouglas Gregor LangOptions LangOpts; 628718292f2SDouglas Gregor StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target); 629718292f2SDouglas Gregor if (StringLiteral.hadError) 630718292f2SDouglas Gregor goto retry; 631718292f2SDouglas Gregor 632718292f2SDouglas Gregor // Copy the string literal into our string data allocator. 633718292f2SDouglas Gregor unsigned Length = StringLiteral.GetStringLength(); 634718292f2SDouglas Gregor char *Saved = StringData.Allocate<char>(Length + 1); 635718292f2SDouglas Gregor memcpy(Saved, StringLiteral.GetString().data(), Length); 636718292f2SDouglas Gregor Saved[Length] = 0; 637718292f2SDouglas Gregor 638718292f2SDouglas Gregor // Form the token. 639718292f2SDouglas Gregor Tok.Kind = MMToken::StringLiteral; 640718292f2SDouglas Gregor Tok.StringData = Saved; 641718292f2SDouglas Gregor Tok.StringLength = Length; 642718292f2SDouglas Gregor break; 643718292f2SDouglas Gregor } 644718292f2SDouglas Gregor 645718292f2SDouglas Gregor case tok::comment: 646718292f2SDouglas Gregor goto retry; 647718292f2SDouglas Gregor 648718292f2SDouglas Gregor default: 649718292f2SDouglas Gregor Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 650718292f2SDouglas Gregor HadError = true; 651718292f2SDouglas Gregor goto retry; 652718292f2SDouglas Gregor } 653718292f2SDouglas Gregor 654718292f2SDouglas Gregor return Result; 655718292f2SDouglas Gregor } 656718292f2SDouglas Gregor 657718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 658718292f2SDouglas Gregor unsigned braceDepth = 0; 659a686e1b0SDouglas Gregor unsigned squareDepth = 0; 660718292f2SDouglas Gregor do { 661718292f2SDouglas Gregor switch (Tok.Kind) { 662718292f2SDouglas Gregor case MMToken::EndOfFile: 663718292f2SDouglas Gregor return; 664718292f2SDouglas Gregor 665718292f2SDouglas Gregor case MMToken::LBrace: 666a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 667718292f2SDouglas Gregor return; 668718292f2SDouglas Gregor 669718292f2SDouglas Gregor ++braceDepth; 670718292f2SDouglas Gregor break; 671718292f2SDouglas Gregor 672a686e1b0SDouglas Gregor case MMToken::LSquare: 673a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 674a686e1b0SDouglas Gregor return; 675a686e1b0SDouglas Gregor 676a686e1b0SDouglas Gregor ++squareDepth; 677a686e1b0SDouglas Gregor break; 678a686e1b0SDouglas Gregor 679718292f2SDouglas Gregor case MMToken::RBrace: 680718292f2SDouglas Gregor if (braceDepth > 0) 681718292f2SDouglas Gregor --braceDepth; 682718292f2SDouglas Gregor else if (Tok.is(K)) 683718292f2SDouglas Gregor return; 684718292f2SDouglas Gregor break; 685718292f2SDouglas Gregor 686a686e1b0SDouglas Gregor case MMToken::RSquare: 687a686e1b0SDouglas Gregor if (squareDepth > 0) 688a686e1b0SDouglas Gregor --squareDepth; 689a686e1b0SDouglas Gregor else if (Tok.is(K)) 690a686e1b0SDouglas Gregor return; 691a686e1b0SDouglas Gregor break; 692a686e1b0SDouglas Gregor 693718292f2SDouglas Gregor default: 694a686e1b0SDouglas Gregor if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 695718292f2SDouglas Gregor return; 696718292f2SDouglas Gregor break; 697718292f2SDouglas Gregor } 698718292f2SDouglas Gregor 699718292f2SDouglas Gregor consumeToken(); 700718292f2SDouglas Gregor } while (true); 701718292f2SDouglas Gregor } 702718292f2SDouglas Gregor 703e7ab3669SDouglas Gregor /// \brief Parse a module-id. 704e7ab3669SDouglas Gregor /// 705e7ab3669SDouglas Gregor /// module-id: 706e7ab3669SDouglas Gregor /// identifier 707e7ab3669SDouglas Gregor /// identifier '.' module-id 708e7ab3669SDouglas Gregor /// 709e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise. 710e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) { 711e7ab3669SDouglas Gregor Id.clear(); 712e7ab3669SDouglas Gregor do { 713e7ab3669SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 714e7ab3669SDouglas Gregor Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 715e7ab3669SDouglas Gregor consumeToken(); 716e7ab3669SDouglas Gregor } else { 717e7ab3669SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 718e7ab3669SDouglas Gregor return true; 719e7ab3669SDouglas Gregor } 720e7ab3669SDouglas Gregor 721e7ab3669SDouglas Gregor if (!Tok.is(MMToken::Period)) 722e7ab3669SDouglas Gregor break; 723e7ab3669SDouglas Gregor 724e7ab3669SDouglas Gregor consumeToken(); 725e7ab3669SDouglas Gregor } while (true); 726e7ab3669SDouglas Gregor 727e7ab3669SDouglas Gregor return false; 728e7ab3669SDouglas Gregor } 729e7ab3669SDouglas Gregor 730a686e1b0SDouglas Gregor namespace { 731a686e1b0SDouglas Gregor /// \brief Enumerates the known attributes. 732a686e1b0SDouglas Gregor enum AttributeKind { 733a686e1b0SDouglas Gregor /// \brief An unknown attribute. 734a686e1b0SDouglas Gregor AT_unknown, 735a686e1b0SDouglas Gregor /// \brief The 'system' attribute. 736a686e1b0SDouglas Gregor AT_system 737a686e1b0SDouglas Gregor }; 738a686e1b0SDouglas Gregor } 739a686e1b0SDouglas Gregor 740718292f2SDouglas Gregor /// \brief Parse a module declaration. 741718292f2SDouglas Gregor /// 742718292f2SDouglas Gregor /// module-declaration: 743a686e1b0SDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 744a686e1b0SDouglas Gregor /// { module-member* } 745a686e1b0SDouglas Gregor /// 746a686e1b0SDouglas Gregor /// attributes: 747a686e1b0SDouglas Gregor /// attribute attributes 748a686e1b0SDouglas Gregor /// attribute 749a686e1b0SDouglas Gregor /// 750a686e1b0SDouglas Gregor /// attribute: 751a686e1b0SDouglas Gregor /// [ identifier ] 752718292f2SDouglas Gregor /// 753718292f2SDouglas Gregor /// module-member: 7541fb5c3a6SDouglas Gregor /// requires-declaration 755718292f2SDouglas Gregor /// header-declaration 756e7ab3669SDouglas Gregor /// submodule-declaration 7572b82c2a5SDouglas Gregor /// export-declaration 75873441091SDouglas Gregor /// 75973441091SDouglas Gregor /// submodule-declaration: 76073441091SDouglas Gregor /// module-declaration 76173441091SDouglas Gregor /// inferred-submodule-declaration 762718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() { 763755b2055SDouglas Gregor assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 764755b2055SDouglas Gregor Tok.is(MMToken::FrameworkKeyword)); 765f2161a70SDouglas Gregor // Parse 'explicit' or 'framework' keyword, if present. 766e7ab3669SDouglas Gregor SourceLocation ExplicitLoc; 767718292f2SDouglas Gregor bool Explicit = false; 768f2161a70SDouglas Gregor bool Framework = false; 769755b2055SDouglas Gregor 770f2161a70SDouglas Gregor // Parse 'explicit' keyword, if present. 771f2161a70SDouglas Gregor if (Tok.is(MMToken::ExplicitKeyword)) { 772e7ab3669SDouglas Gregor ExplicitLoc = consumeToken(); 773f2161a70SDouglas Gregor Explicit = true; 774f2161a70SDouglas Gregor } 775f2161a70SDouglas Gregor 776f2161a70SDouglas Gregor // Parse 'framework' keyword, if present. 777755b2055SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 778755b2055SDouglas Gregor consumeToken(); 779755b2055SDouglas Gregor Framework = true; 780755b2055SDouglas Gregor } 781718292f2SDouglas Gregor 782718292f2SDouglas Gregor // Parse 'module' keyword. 783718292f2SDouglas Gregor if (!Tok.is(MMToken::ModuleKeyword)) { 784d6343c99SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 785718292f2SDouglas Gregor consumeToken(); 786718292f2SDouglas Gregor HadError = true; 787718292f2SDouglas Gregor return; 788718292f2SDouglas Gregor } 789718292f2SDouglas Gregor consumeToken(); // 'module' keyword 790718292f2SDouglas Gregor 79173441091SDouglas Gregor // If we have a wildcard for the module name, this is an inferred submodule. 79273441091SDouglas Gregor // Parse it. 79373441091SDouglas Gregor if (Tok.is(MMToken::Star)) 79473441091SDouglas Gregor return parseInferredSubmoduleDecl(Explicit); 79573441091SDouglas Gregor 796718292f2SDouglas Gregor // Parse the module name. 797e7ab3669SDouglas Gregor ModuleId Id; 798e7ab3669SDouglas Gregor if (parseModuleId(Id)) { 799718292f2SDouglas Gregor HadError = true; 800718292f2SDouglas Gregor return; 801718292f2SDouglas Gregor } 802e7ab3669SDouglas Gregor 803e7ab3669SDouglas Gregor if (ActiveModule) { 804e7ab3669SDouglas Gregor if (Id.size() > 1) { 805e7ab3669SDouglas Gregor Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 806e7ab3669SDouglas Gregor << SourceRange(Id.front().second, Id.back().second); 807e7ab3669SDouglas Gregor 808e7ab3669SDouglas Gregor HadError = true; 809e7ab3669SDouglas Gregor return; 810e7ab3669SDouglas Gregor } 811e7ab3669SDouglas Gregor } else if (Id.size() == 1 && Explicit) { 812e7ab3669SDouglas Gregor // Top-level modules can't be explicit. 813e7ab3669SDouglas Gregor Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 814e7ab3669SDouglas Gregor Explicit = false; 815e7ab3669SDouglas Gregor ExplicitLoc = SourceLocation(); 816e7ab3669SDouglas Gregor HadError = true; 817e7ab3669SDouglas Gregor } 818e7ab3669SDouglas Gregor 819e7ab3669SDouglas Gregor Module *PreviousActiveModule = ActiveModule; 820e7ab3669SDouglas Gregor if (Id.size() > 1) { 821e7ab3669SDouglas Gregor // This module map defines a submodule. Go find the module of which it 822e7ab3669SDouglas Gregor // is a submodule. 823e7ab3669SDouglas Gregor ActiveModule = 0; 824e7ab3669SDouglas Gregor for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 825e7ab3669SDouglas Gregor if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 826e7ab3669SDouglas Gregor ActiveModule = Next; 827e7ab3669SDouglas Gregor continue; 828e7ab3669SDouglas Gregor } 829e7ab3669SDouglas Gregor 830e7ab3669SDouglas Gregor if (ActiveModule) { 831e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 832e7ab3669SDouglas Gregor << Id[I].first << ActiveModule->getTopLevelModule(); 833e7ab3669SDouglas Gregor } else { 834e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 835e7ab3669SDouglas Gregor } 836e7ab3669SDouglas Gregor HadError = true; 837e7ab3669SDouglas Gregor return; 838e7ab3669SDouglas Gregor } 839e7ab3669SDouglas Gregor } 840e7ab3669SDouglas Gregor 841e7ab3669SDouglas Gregor StringRef ModuleName = Id.back().first; 842e7ab3669SDouglas Gregor SourceLocation ModuleNameLoc = Id.back().second; 843718292f2SDouglas Gregor 844a686e1b0SDouglas Gregor // Parse the optional attribute list. 845a686e1b0SDouglas Gregor bool IsSystem = false; 846a686e1b0SDouglas Gregor while (Tok.is(MMToken::LSquare)) { 847a686e1b0SDouglas Gregor // Consume the '['. 848a686e1b0SDouglas Gregor SourceLocation LSquareLoc = consumeToken(); 849a686e1b0SDouglas Gregor 850a686e1b0SDouglas Gregor // Check whether we have an attribute name here. 851a686e1b0SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 852a686e1b0SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 853a686e1b0SDouglas Gregor skipUntil(MMToken::RSquare); 854a686e1b0SDouglas Gregor if (Tok.is(MMToken::RSquare)) 855a686e1b0SDouglas Gregor consumeToken(); 856a686e1b0SDouglas Gregor continue; 857a686e1b0SDouglas Gregor } 858a686e1b0SDouglas Gregor 859a686e1b0SDouglas Gregor // Decode the attribute name. 860a686e1b0SDouglas Gregor AttributeKind Attribute 861a686e1b0SDouglas Gregor = llvm::StringSwitch<AttributeKind>(Tok.getString()) 862a686e1b0SDouglas Gregor .Case("system", AT_system) 863a686e1b0SDouglas Gregor .Default(AT_unknown); 864a686e1b0SDouglas Gregor switch (Attribute) { 865a686e1b0SDouglas Gregor case AT_unknown: 866a686e1b0SDouglas Gregor Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 867a686e1b0SDouglas Gregor << Tok.getString(); 868a686e1b0SDouglas Gregor break; 869a686e1b0SDouglas Gregor 870a686e1b0SDouglas Gregor case AT_system: 871a686e1b0SDouglas Gregor IsSystem = true; 872a686e1b0SDouglas Gregor break; 873a686e1b0SDouglas Gregor } 874a686e1b0SDouglas Gregor consumeToken(); 875a686e1b0SDouglas Gregor 876a686e1b0SDouglas Gregor // Consume the ']'. 877a686e1b0SDouglas Gregor if (!Tok.is(MMToken::RSquare)) { 878a686e1b0SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 879a686e1b0SDouglas Gregor Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 880a686e1b0SDouglas Gregor skipUntil(MMToken::RSquare); 881a686e1b0SDouglas Gregor } 882a686e1b0SDouglas Gregor 883a686e1b0SDouglas Gregor if (Tok.is(MMToken::RSquare)) 884a686e1b0SDouglas Gregor consumeToken(); 885a686e1b0SDouglas Gregor } 886a686e1b0SDouglas Gregor 887718292f2SDouglas Gregor // Parse the opening brace. 888718292f2SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 889718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 890718292f2SDouglas Gregor << ModuleName; 891718292f2SDouglas Gregor HadError = true; 892718292f2SDouglas Gregor return; 893718292f2SDouglas Gregor } 894718292f2SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 895718292f2SDouglas Gregor 896718292f2SDouglas Gregor // Determine whether this (sub)module has already been defined. 897eb90e830SDouglas Gregor if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 898fcc54a3bSDouglas Gregor if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { 899fcc54a3bSDouglas Gregor // Skip the module definition. 900fcc54a3bSDouglas Gregor skipUntil(MMToken::RBrace); 901fcc54a3bSDouglas Gregor if (Tok.is(MMToken::RBrace)) 902fcc54a3bSDouglas Gregor consumeToken(); 903fcc54a3bSDouglas Gregor else { 904fcc54a3bSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 905fcc54a3bSDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 906fcc54a3bSDouglas Gregor HadError = true; 907fcc54a3bSDouglas Gregor } 908fcc54a3bSDouglas Gregor return; 909fcc54a3bSDouglas Gregor } 910fcc54a3bSDouglas Gregor 911718292f2SDouglas Gregor Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 912718292f2SDouglas Gregor << ModuleName; 913eb90e830SDouglas Gregor Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 914718292f2SDouglas Gregor 915718292f2SDouglas Gregor // Skip the module definition. 916718292f2SDouglas Gregor skipUntil(MMToken::RBrace); 917718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 918718292f2SDouglas Gregor consumeToken(); 919718292f2SDouglas Gregor 920718292f2SDouglas Gregor HadError = true; 921718292f2SDouglas Gregor return; 922718292f2SDouglas Gregor } 923718292f2SDouglas Gregor 924718292f2SDouglas Gregor // Start defining this module. 925eb90e830SDouglas Gregor ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, 926eb90e830SDouglas Gregor Explicit).first; 927eb90e830SDouglas Gregor ActiveModule->DefinitionLoc = ModuleNameLoc; 928a686e1b0SDouglas Gregor if (IsSystem) 929a686e1b0SDouglas Gregor ActiveModule->IsSystem = true; 930718292f2SDouglas Gregor 931718292f2SDouglas Gregor bool Done = false; 932718292f2SDouglas Gregor do { 933718292f2SDouglas Gregor switch (Tok.Kind) { 934718292f2SDouglas Gregor case MMToken::EndOfFile: 935718292f2SDouglas Gregor case MMToken::RBrace: 936718292f2SDouglas Gregor Done = true; 937718292f2SDouglas Gregor break; 938718292f2SDouglas Gregor 939718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 940f2161a70SDouglas Gregor case MMToken::FrameworkKeyword: 941718292f2SDouglas Gregor case MMToken::ModuleKeyword: 942718292f2SDouglas Gregor parseModuleDecl(); 943718292f2SDouglas Gregor break; 944718292f2SDouglas Gregor 9452b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 9462b82c2a5SDouglas Gregor parseExportDecl(); 9472b82c2a5SDouglas Gregor break; 9482b82c2a5SDouglas Gregor 9491fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 9501fb5c3a6SDouglas Gregor parseRequiresDecl(); 9511fb5c3a6SDouglas Gregor break; 9521fb5c3a6SDouglas Gregor 953524e33e1SDouglas Gregor case MMToken::UmbrellaKeyword: { 954524e33e1SDouglas Gregor SourceLocation UmbrellaLoc = consumeToken(); 955524e33e1SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) 956524e33e1SDouglas Gregor parseHeaderDecl(UmbrellaLoc); 957524e33e1SDouglas Gregor else 958524e33e1SDouglas Gregor parseUmbrellaDirDecl(UmbrellaLoc); 959718292f2SDouglas Gregor break; 960524e33e1SDouglas Gregor } 961718292f2SDouglas Gregor 962322f633cSDouglas Gregor case MMToken::HeaderKeyword: 963322f633cSDouglas Gregor parseHeaderDecl(SourceLocation()); 964718292f2SDouglas Gregor break; 965718292f2SDouglas Gregor 966718292f2SDouglas Gregor default: 967718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 968718292f2SDouglas Gregor consumeToken(); 969718292f2SDouglas Gregor break; 970718292f2SDouglas Gregor } 971718292f2SDouglas Gregor } while (!Done); 972718292f2SDouglas Gregor 973718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 974718292f2SDouglas Gregor consumeToken(); 975718292f2SDouglas Gregor else { 976718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 977718292f2SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 978718292f2SDouglas Gregor HadError = true; 979718292f2SDouglas Gregor } 980718292f2SDouglas Gregor 981e7ab3669SDouglas Gregor // We're done parsing this module. Pop back to the previous module. 982e7ab3669SDouglas Gregor ActiveModule = PreviousActiveModule; 983718292f2SDouglas Gregor } 984718292f2SDouglas Gregor 9851fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration. 9861fb5c3a6SDouglas Gregor /// 9871fb5c3a6SDouglas Gregor /// requires-declaration: 9881fb5c3a6SDouglas Gregor /// 'requires' feature-list 9891fb5c3a6SDouglas Gregor /// 9901fb5c3a6SDouglas Gregor /// feature-list: 9911fb5c3a6SDouglas Gregor /// identifier ',' feature-list 9921fb5c3a6SDouglas Gregor /// identifier 9931fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() { 9941fb5c3a6SDouglas Gregor assert(Tok.is(MMToken::RequiresKeyword)); 9951fb5c3a6SDouglas Gregor 9961fb5c3a6SDouglas Gregor // Parse 'requires' keyword. 9971fb5c3a6SDouglas Gregor consumeToken(); 9981fb5c3a6SDouglas Gregor 9991fb5c3a6SDouglas Gregor // Parse the feature-list. 10001fb5c3a6SDouglas Gregor do { 10011fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 10021fb5c3a6SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 10031fb5c3a6SDouglas Gregor HadError = true; 10041fb5c3a6SDouglas Gregor return; 10051fb5c3a6SDouglas Gregor } 10061fb5c3a6SDouglas Gregor 10071fb5c3a6SDouglas Gregor // Consume the feature name. 10081fb5c3a6SDouglas Gregor std::string Feature = Tok.getString(); 10091fb5c3a6SDouglas Gregor consumeToken(); 10101fb5c3a6SDouglas Gregor 10111fb5c3a6SDouglas Gregor // Add this feature. 101289929282SDouglas Gregor ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target); 10131fb5c3a6SDouglas Gregor 10141fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Comma)) 10151fb5c3a6SDouglas Gregor break; 10161fb5c3a6SDouglas Gregor 10171fb5c3a6SDouglas Gregor // Consume the comma. 10181fb5c3a6SDouglas Gregor consumeToken(); 10191fb5c3a6SDouglas Gregor } while (true); 10201fb5c3a6SDouglas Gregor } 10211fb5c3a6SDouglas Gregor 1022f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the 1023f2161a70SDouglas Gregor /// subframework in which the given module lives. 1024bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod, 1025bf8da9d7SBenjamin Kramer llvm::SmallVectorImpl<char> &Path) { 1026f2161a70SDouglas Gregor // Collect the framework names from the given module to the top-level module. 1027f2161a70SDouglas Gregor llvm::SmallVector<StringRef, 2> Paths; 1028f2161a70SDouglas Gregor for (; Mod; Mod = Mod->Parent) { 1029f2161a70SDouglas Gregor if (Mod->IsFramework) 1030f2161a70SDouglas Gregor Paths.push_back(Mod->Name); 1031f2161a70SDouglas Gregor } 1032f2161a70SDouglas Gregor 1033f2161a70SDouglas Gregor if (Paths.empty()) 1034f2161a70SDouglas Gregor return; 1035f2161a70SDouglas Gregor 1036f2161a70SDouglas Gregor // Add Frameworks/Name.framework for each subframework. 1037f2161a70SDouglas Gregor for (unsigned I = Paths.size() - 1; I != 0; --I) { 1038f2161a70SDouglas Gregor llvm::sys::path::append(Path, "Frameworks"); 1039f2161a70SDouglas Gregor llvm::sys::path::append(Path, Paths[I-1] + ".framework"); 1040f2161a70SDouglas Gregor } 1041f2161a70SDouglas Gregor } 1042f2161a70SDouglas Gregor 10433ec6663bSDouglas Gregor /// \brief Determine whether the given file name is the name of a builtin 10443ec6663bSDouglas Gregor /// header, supplied by Clang to replace, override, or augment existing system 10453ec6663bSDouglas Gregor /// headers. 10463ec6663bSDouglas Gregor static bool isBuiltinHeader(StringRef FileName) { 10473ec6663bSDouglas Gregor return llvm::StringSwitch<bool>(FileName) 10483ec6663bSDouglas Gregor .Case("float.h", true) 10493ec6663bSDouglas Gregor .Case("iso646.h", true) 10503ec6663bSDouglas Gregor .Case("limits.h", true) 10513ec6663bSDouglas Gregor .Case("stdalign.h", true) 10523ec6663bSDouglas Gregor .Case("stdarg.h", true) 10533ec6663bSDouglas Gregor .Case("stdbool.h", true) 10543ec6663bSDouglas Gregor .Case("stddef.h", true) 10553ec6663bSDouglas Gregor .Case("stdint.h", true) 10563ec6663bSDouglas Gregor .Case("tgmath.h", true) 10573ec6663bSDouglas Gregor .Case("unwind.h", true) 10583ec6663bSDouglas Gregor .Default(false); 10593ec6663bSDouglas Gregor } 10603ec6663bSDouglas Gregor 1061718292f2SDouglas Gregor /// \brief Parse a header declaration. 1062718292f2SDouglas Gregor /// 1063718292f2SDouglas Gregor /// header-declaration: 1064322f633cSDouglas Gregor /// 'umbrella'[opt] 'header' string-literal 1065322f633cSDouglas Gregor void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc) { 1066718292f2SDouglas Gregor assert(Tok.is(MMToken::HeaderKeyword)); 10671871ed3dSBenjamin Kramer consumeToken(); 1068718292f2SDouglas Gregor 1069322f633cSDouglas Gregor bool Umbrella = UmbrellaLoc.isValid(); 1070322f633cSDouglas Gregor 1071718292f2SDouglas Gregor // Parse the header name. 1072718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1073718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1074718292f2SDouglas Gregor << "header"; 1075718292f2SDouglas Gregor HadError = true; 1076718292f2SDouglas Gregor return; 1077718292f2SDouglas Gregor } 1078e7ab3669SDouglas Gregor std::string FileName = Tok.getString(); 1079718292f2SDouglas Gregor SourceLocation FileNameLoc = consumeToken(); 1080718292f2SDouglas Gregor 1081524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1082524e33e1SDouglas Gregor if (Umbrella && ActiveModule->Umbrella) { 1083524e33e1SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash) 1084524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1085322f633cSDouglas Gregor HadError = true; 1086322f633cSDouglas Gregor return; 1087322f633cSDouglas Gregor } 1088322f633cSDouglas Gregor 10895257fc63SDouglas Gregor // Look for this file. 1090e7ab3669SDouglas Gregor const FileEntry *File = 0; 10913ec6663bSDouglas Gregor const FileEntry *BuiltinFile = 0; 10922c1dd271SDylan Noblesmith SmallString<128> PathName; 1093e7ab3669SDouglas Gregor if (llvm::sys::path::is_absolute(FileName)) { 1094e7ab3669SDouglas Gregor PathName = FileName; 1095e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 10967033127bSDouglas Gregor } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) { 10977033127bSDouglas Gregor PathName = Dir->getName(); 10987033127bSDouglas Gregor llvm::sys::path::append(PathName, FileName); 10997033127bSDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1100e7ab3669SDouglas Gregor } else { 1101e7ab3669SDouglas Gregor // Search for the header file within the search directory. 11027033127bSDouglas Gregor PathName = Directory->getName(); 1103e7ab3669SDouglas Gregor unsigned PathLength = PathName.size(); 1104755b2055SDouglas Gregor 1105f2161a70SDouglas Gregor if (ActiveModule->isPartOfFramework()) { 1106f2161a70SDouglas Gregor appendSubframeworkPaths(ActiveModule, PathName); 1107755b2055SDouglas Gregor 1108e7ab3669SDouglas Gregor // Check whether this file is in the public headers. 1109e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, "Headers"); 11105257fc63SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1111e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1112e7ab3669SDouglas Gregor 1113e7ab3669SDouglas Gregor if (!File) { 1114e7ab3669SDouglas Gregor // Check whether this file is in the private headers. 1115e7ab3669SDouglas Gregor PathName.resize(PathLength); 1116e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, "PrivateHeaders"); 1117e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1118e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1119e7ab3669SDouglas Gregor } 1120e7ab3669SDouglas Gregor } else { 1121e7ab3669SDouglas Gregor // Lookup for normal headers. 1122e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1123e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 11243ec6663bSDouglas Gregor 11253ec6663bSDouglas Gregor // If this is a system module with a top-level header, this header 11263ec6663bSDouglas Gregor // may have a counterpart (or replacement) in the set of headers 11273ec6663bSDouglas Gregor // supplied by Clang. Find that builtin header. 11283ec6663bSDouglas Gregor if (ActiveModule->IsSystem && !Umbrella && BuiltinIncludeDir && 11293ec6663bSDouglas Gregor BuiltinIncludeDir != Directory && isBuiltinHeader(FileName)) { 11302c1dd271SDylan Noblesmith SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); 11313ec6663bSDouglas Gregor llvm::sys::path::append(BuiltinPathName, FileName); 11323ec6663bSDouglas Gregor BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); 11333ec6663bSDouglas Gregor 11343ec6663bSDouglas Gregor // If Clang supplies this header but the underlying system does not, 11353ec6663bSDouglas Gregor // just silently swap in our builtin version. Otherwise, we'll end 11363ec6663bSDouglas Gregor // up adding both (later). 11373ec6663bSDouglas Gregor if (!File && BuiltinFile) { 11383ec6663bSDouglas Gregor File = BuiltinFile; 11393ec6663bSDouglas Gregor BuiltinFile = 0; 11403ec6663bSDouglas Gregor } 11413ec6663bSDouglas Gregor } 1142e7ab3669SDouglas Gregor } 1143e7ab3669SDouglas Gregor } 11445257fc63SDouglas Gregor 11455257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 11465257fc63SDouglas Gregor // Come up with a lazy way to do this. 1147e7ab3669SDouglas Gregor if (File) { 11485257fc63SDouglas Gregor if (const Module *OwningModule = Map.Headers[File]) { 11495257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_conflict) 11505257fc63SDouglas Gregor << FileName << OwningModule->getFullModuleName(); 11515257fc63SDouglas Gregor HadError = true; 1152322f633cSDouglas Gregor } else if (Umbrella) { 1153322f633cSDouglas Gregor const DirectoryEntry *UmbrellaDir = File->getDir(); 1154322f633cSDouglas Gregor if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) { 1155322f633cSDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1156322f633cSDouglas Gregor << OwningModule->getFullModuleName(); 1157322f633cSDouglas Gregor HadError = true; 11585257fc63SDouglas Gregor } else { 1159322f633cSDouglas Gregor // Record this umbrella header. 1160322f633cSDouglas Gregor Map.setUmbrellaHeader(ActiveModule, File); 1161322f633cSDouglas Gregor } 1162322f633cSDouglas Gregor } else { 1163322f633cSDouglas Gregor // Record this header. 1164a89c5ac4SDouglas Gregor Map.addHeader(ActiveModule, File); 11653ec6663bSDouglas Gregor 11663ec6663bSDouglas Gregor // If there is a builtin counterpart to this file, add it now. 11673ec6663bSDouglas Gregor if (BuiltinFile) 11683ec6663bSDouglas Gregor Map.addHeader(ActiveModule, BuiltinFile); 11695257fc63SDouglas Gregor } 11705257fc63SDouglas Gregor } else { 11715257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_not_found) 1172524e33e1SDouglas Gregor << Umbrella << FileName; 11735257fc63SDouglas Gregor HadError = true; 11745257fc63SDouglas Gregor } 1175718292f2SDouglas Gregor } 1176718292f2SDouglas Gregor 1177524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration. 1178524e33e1SDouglas Gregor /// 1179524e33e1SDouglas Gregor /// umbrella-dir-declaration: 1180524e33e1SDouglas Gregor /// umbrella string-literal 1181524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1182524e33e1SDouglas Gregor // Parse the directory name. 1183524e33e1SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1184524e33e1SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1185524e33e1SDouglas Gregor << "umbrella"; 1186524e33e1SDouglas Gregor HadError = true; 1187524e33e1SDouglas Gregor return; 1188524e33e1SDouglas Gregor } 1189524e33e1SDouglas Gregor 1190524e33e1SDouglas Gregor std::string DirName = Tok.getString(); 1191524e33e1SDouglas Gregor SourceLocation DirNameLoc = consumeToken(); 1192524e33e1SDouglas Gregor 1193524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1194524e33e1SDouglas Gregor if (ActiveModule->Umbrella) { 1195524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1196524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1197524e33e1SDouglas Gregor HadError = true; 1198524e33e1SDouglas Gregor return; 1199524e33e1SDouglas Gregor } 1200524e33e1SDouglas Gregor 1201524e33e1SDouglas Gregor // Look for this file. 1202524e33e1SDouglas Gregor const DirectoryEntry *Dir = 0; 1203524e33e1SDouglas Gregor if (llvm::sys::path::is_absolute(DirName)) 1204524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(DirName); 1205524e33e1SDouglas Gregor else { 12062c1dd271SDylan Noblesmith SmallString<128> PathName; 1207524e33e1SDouglas Gregor PathName = Directory->getName(); 1208524e33e1SDouglas Gregor llvm::sys::path::append(PathName, DirName); 1209524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(PathName); 1210524e33e1SDouglas Gregor } 1211524e33e1SDouglas Gregor 1212524e33e1SDouglas Gregor if (!Dir) { 1213524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1214524e33e1SDouglas Gregor << DirName; 1215524e33e1SDouglas Gregor HadError = true; 1216524e33e1SDouglas Gregor return; 1217524e33e1SDouglas Gregor } 1218524e33e1SDouglas Gregor 1219524e33e1SDouglas Gregor if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1220524e33e1SDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1221524e33e1SDouglas Gregor << OwningModule->getFullModuleName(); 1222524e33e1SDouglas Gregor HadError = true; 1223524e33e1SDouglas Gregor return; 1224524e33e1SDouglas Gregor } 1225524e33e1SDouglas Gregor 1226524e33e1SDouglas Gregor // Record this umbrella directory. 1227524e33e1SDouglas Gregor Map.setUmbrellaDir(ActiveModule, Dir); 1228524e33e1SDouglas Gregor } 1229524e33e1SDouglas Gregor 12302b82c2a5SDouglas Gregor /// \brief Parse a module export declaration. 12312b82c2a5SDouglas Gregor /// 12322b82c2a5SDouglas Gregor /// export-declaration: 12332b82c2a5SDouglas Gregor /// 'export' wildcard-module-id 12342b82c2a5SDouglas Gregor /// 12352b82c2a5SDouglas Gregor /// wildcard-module-id: 12362b82c2a5SDouglas Gregor /// identifier 12372b82c2a5SDouglas Gregor /// '*' 12382b82c2a5SDouglas Gregor /// identifier '.' wildcard-module-id 12392b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() { 12402b82c2a5SDouglas Gregor assert(Tok.is(MMToken::ExportKeyword)); 12412b82c2a5SDouglas Gregor SourceLocation ExportLoc = consumeToken(); 12422b82c2a5SDouglas Gregor 12432b82c2a5SDouglas Gregor // Parse the module-id with an optional wildcard at the end. 12442b82c2a5SDouglas Gregor ModuleId ParsedModuleId; 12452b82c2a5SDouglas Gregor bool Wildcard = false; 12462b82c2a5SDouglas Gregor do { 12472b82c2a5SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 12482b82c2a5SDouglas Gregor ParsedModuleId.push_back(std::make_pair(Tok.getString(), 12492b82c2a5SDouglas Gregor Tok.getLocation())); 12502b82c2a5SDouglas Gregor consumeToken(); 12512b82c2a5SDouglas Gregor 12522b82c2a5SDouglas Gregor if (Tok.is(MMToken::Period)) { 12532b82c2a5SDouglas Gregor consumeToken(); 12542b82c2a5SDouglas Gregor continue; 12552b82c2a5SDouglas Gregor } 12562b82c2a5SDouglas Gregor 12572b82c2a5SDouglas Gregor break; 12582b82c2a5SDouglas Gregor } 12592b82c2a5SDouglas Gregor 12602b82c2a5SDouglas Gregor if(Tok.is(MMToken::Star)) { 12612b82c2a5SDouglas Gregor Wildcard = true; 1262f5eedd05SDouglas Gregor consumeToken(); 12632b82c2a5SDouglas Gregor break; 12642b82c2a5SDouglas Gregor } 12652b82c2a5SDouglas Gregor 12662b82c2a5SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id); 12672b82c2a5SDouglas Gregor HadError = true; 12682b82c2a5SDouglas Gregor return; 12692b82c2a5SDouglas Gregor } while (true); 12702b82c2a5SDouglas Gregor 12712b82c2a5SDouglas Gregor Module::UnresolvedExportDecl Unresolved = { 12722b82c2a5SDouglas Gregor ExportLoc, ParsedModuleId, Wildcard 12732b82c2a5SDouglas Gregor }; 12742b82c2a5SDouglas Gregor ActiveModule->UnresolvedExports.push_back(Unresolved); 12752b82c2a5SDouglas Gregor } 12762b82c2a5SDouglas Gregor 127773441091SDouglas Gregor void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) { 127873441091SDouglas Gregor assert(Tok.is(MMToken::Star)); 127973441091SDouglas Gregor SourceLocation StarLoc = consumeToken(); 128073441091SDouglas Gregor bool Failed = false; 128173441091SDouglas Gregor 128273441091SDouglas Gregor // Inferred modules must be submodules. 128373441091SDouglas Gregor if (!ActiveModule) { 128473441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 128573441091SDouglas Gregor Failed = true; 128673441091SDouglas Gregor } 128773441091SDouglas Gregor 1288524e33e1SDouglas Gregor // Inferred modules must have umbrella directories. 1289524e33e1SDouglas Gregor if (!Failed && !ActiveModule->getUmbrellaDir()) { 129073441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 129173441091SDouglas Gregor Failed = true; 129273441091SDouglas Gregor } 129373441091SDouglas Gregor 129473441091SDouglas Gregor // Check for redefinition of an inferred module. 1295dd005f69SDouglas Gregor if (!Failed && ActiveModule->InferSubmodules) { 129673441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 1297dd005f69SDouglas Gregor if (ActiveModule->InferredSubmoduleLoc.isValid()) 1298dd005f69SDouglas Gregor Diags.Report(ActiveModule->InferredSubmoduleLoc, 129973441091SDouglas Gregor diag::note_mmap_prev_definition); 130073441091SDouglas Gregor Failed = true; 130173441091SDouglas Gregor } 130273441091SDouglas Gregor 130373441091SDouglas Gregor // If there were any problems with this inferred submodule, skip its body. 130473441091SDouglas Gregor if (Failed) { 130573441091SDouglas Gregor if (Tok.is(MMToken::LBrace)) { 130673441091SDouglas Gregor consumeToken(); 130773441091SDouglas Gregor skipUntil(MMToken::RBrace); 130873441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 130973441091SDouglas Gregor consumeToken(); 131073441091SDouglas Gregor } 131173441091SDouglas Gregor HadError = true; 131273441091SDouglas Gregor return; 131373441091SDouglas Gregor } 131473441091SDouglas Gregor 131573441091SDouglas Gregor // Note that we have an inferred submodule. 1316dd005f69SDouglas Gregor ActiveModule->InferSubmodules = true; 1317dd005f69SDouglas Gregor ActiveModule->InferredSubmoduleLoc = StarLoc; 1318dd005f69SDouglas Gregor ActiveModule->InferExplicitSubmodules = Explicit; 131973441091SDouglas Gregor 132073441091SDouglas Gregor // Parse the opening brace. 132173441091SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 132273441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 132373441091SDouglas Gregor HadError = true; 132473441091SDouglas Gregor return; 132573441091SDouglas Gregor } 132673441091SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 132773441091SDouglas Gregor 132873441091SDouglas Gregor // Parse the body of the inferred submodule. 132973441091SDouglas Gregor bool Done = false; 133073441091SDouglas Gregor do { 133173441091SDouglas Gregor switch (Tok.Kind) { 133273441091SDouglas Gregor case MMToken::EndOfFile: 133373441091SDouglas Gregor case MMToken::RBrace: 133473441091SDouglas Gregor Done = true; 133573441091SDouglas Gregor break; 133673441091SDouglas Gregor 133773441091SDouglas Gregor case MMToken::ExportKeyword: { 133873441091SDouglas Gregor consumeToken(); 133973441091SDouglas Gregor if (Tok.is(MMToken::Star)) 1340dd005f69SDouglas Gregor ActiveModule->InferExportWildcard = true; 134173441091SDouglas Gregor else 134273441091SDouglas Gregor Diags.Report(Tok.getLocation(), 134373441091SDouglas Gregor diag::err_mmap_expected_export_wildcard); 134473441091SDouglas Gregor consumeToken(); 134573441091SDouglas Gregor break; 134673441091SDouglas Gregor } 134773441091SDouglas Gregor 134873441091SDouglas Gregor case MMToken::ExplicitKeyword: 134973441091SDouglas Gregor case MMToken::ModuleKeyword: 135073441091SDouglas Gregor case MMToken::HeaderKeyword: 135173441091SDouglas Gregor case MMToken::UmbrellaKeyword: 135273441091SDouglas Gregor default: 135373441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member); 135473441091SDouglas Gregor consumeToken(); 135573441091SDouglas Gregor break; 135673441091SDouglas Gregor } 135773441091SDouglas Gregor } while (!Done); 135873441091SDouglas Gregor 135973441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 136073441091SDouglas Gregor consumeToken(); 136173441091SDouglas Gregor else { 136273441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 136373441091SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 136473441091SDouglas Gregor HadError = true; 136573441091SDouglas Gregor } 136673441091SDouglas Gregor } 136773441091SDouglas Gregor 13687033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence 13697033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null. 13707033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() { 13717033127bSDouglas Gregor for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) { 13727033127bSDouglas Gregor // If we have an umbrella directory, use that. 13737033127bSDouglas Gregor if (Mod->hasUmbrellaDir()) 13747033127bSDouglas Gregor return Mod->getUmbrellaDir(); 13757033127bSDouglas Gregor 13767033127bSDouglas Gregor // If we have a framework directory, stop looking. 13777033127bSDouglas Gregor if (Mod->IsFramework) 13787033127bSDouglas Gregor return 0; 13797033127bSDouglas Gregor } 13807033127bSDouglas Gregor 13817033127bSDouglas Gregor return 0; 13827033127bSDouglas Gregor } 13837033127bSDouglas Gregor 1384718292f2SDouglas Gregor /// \brief Parse a module map file. 1385718292f2SDouglas Gregor /// 1386718292f2SDouglas Gregor /// module-map-file: 1387718292f2SDouglas Gregor /// module-declaration* 1388718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() { 1389718292f2SDouglas Gregor do { 1390718292f2SDouglas Gregor switch (Tok.Kind) { 1391718292f2SDouglas Gregor case MMToken::EndOfFile: 1392718292f2SDouglas Gregor return HadError; 1393718292f2SDouglas Gregor 1394e7ab3669SDouglas Gregor case MMToken::ExplicitKeyword: 1395718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1396755b2055SDouglas Gregor case MMToken::FrameworkKeyword: 1397718292f2SDouglas Gregor parseModuleDecl(); 1398718292f2SDouglas Gregor break; 1399718292f2SDouglas Gregor 14001fb5c3a6SDouglas Gregor case MMToken::Comma: 14012b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 1402718292f2SDouglas Gregor case MMToken::HeaderKeyword: 1403718292f2SDouglas Gregor case MMToken::Identifier: 1404718292f2SDouglas Gregor case MMToken::LBrace: 1405a686e1b0SDouglas Gregor case MMToken::LSquare: 14062b82c2a5SDouglas Gregor case MMToken::Period: 1407718292f2SDouglas Gregor case MMToken::RBrace: 1408a686e1b0SDouglas Gregor case MMToken::RSquare: 14091fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 14102b82c2a5SDouglas Gregor case MMToken::Star: 1411718292f2SDouglas Gregor case MMToken::StringLiteral: 1412718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 1413718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1414718292f2SDouglas Gregor HadError = true; 1415718292f2SDouglas Gregor consumeToken(); 1416718292f2SDouglas Gregor break; 1417718292f2SDouglas Gregor } 1418718292f2SDouglas Gregor } while (true); 1419718292f2SDouglas Gregor } 1420718292f2SDouglas Gregor 1421718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) { 142289929282SDouglas Gregor assert(Target != 0 && "Missing target information"); 1423718292f2SDouglas Gregor FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User); 1424718292f2SDouglas Gregor const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID); 1425718292f2SDouglas Gregor if (!Buffer) 1426718292f2SDouglas Gregor return true; 1427718292f2SDouglas Gregor 1428718292f2SDouglas Gregor // Parse this module map file. 14291fb5c3a6SDouglas Gregor Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts); 14301fb5c3a6SDouglas Gregor Diags->getClient()->BeginSourceFile(MMapLangOpts); 14313ec6663bSDouglas Gregor ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir(), 14323ec6663bSDouglas Gregor BuiltinIncludeDir); 1433718292f2SDouglas Gregor bool Result = Parser.parseModuleMapFile(); 1434718292f2SDouglas Gregor Diags->getClient()->EndSourceFile(); 1435718292f2SDouglas Gregor 1436718292f2SDouglas Gregor return Result; 1437718292f2SDouglas Gregor } 1438