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 { 76718292f2SDouglas Gregor llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs); 77718292f2SDouglas Gregor Diags = llvm::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: { 620718292f2SDouglas Gregor // Parse the string literal. 621718292f2SDouglas Gregor LangOptions LangOpts; 622718292f2SDouglas Gregor StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target); 623718292f2SDouglas Gregor if (StringLiteral.hadError) 624718292f2SDouglas Gregor goto retry; 625718292f2SDouglas Gregor 626718292f2SDouglas Gregor // Copy the string literal into our string data allocator. 627718292f2SDouglas Gregor unsigned Length = StringLiteral.GetStringLength(); 628718292f2SDouglas Gregor char *Saved = StringData.Allocate<char>(Length + 1); 629718292f2SDouglas Gregor memcpy(Saved, StringLiteral.GetString().data(), Length); 630718292f2SDouglas Gregor Saved[Length] = 0; 631718292f2SDouglas Gregor 632718292f2SDouglas Gregor // Form the token. 633718292f2SDouglas Gregor Tok.Kind = MMToken::StringLiteral; 634718292f2SDouglas Gregor Tok.StringData = Saved; 635718292f2SDouglas Gregor Tok.StringLength = Length; 636718292f2SDouglas Gregor break; 637718292f2SDouglas Gregor } 638718292f2SDouglas Gregor 639718292f2SDouglas Gregor case tok::comment: 640718292f2SDouglas Gregor goto retry; 641718292f2SDouglas Gregor 642718292f2SDouglas Gregor default: 643718292f2SDouglas Gregor Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 644718292f2SDouglas Gregor HadError = true; 645718292f2SDouglas Gregor goto retry; 646718292f2SDouglas Gregor } 647718292f2SDouglas Gregor 648718292f2SDouglas Gregor return Result; 649718292f2SDouglas Gregor } 650718292f2SDouglas Gregor 651718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 652718292f2SDouglas Gregor unsigned braceDepth = 0; 653a686e1b0SDouglas Gregor unsigned squareDepth = 0; 654718292f2SDouglas Gregor do { 655718292f2SDouglas Gregor switch (Tok.Kind) { 656718292f2SDouglas Gregor case MMToken::EndOfFile: 657718292f2SDouglas Gregor return; 658718292f2SDouglas Gregor 659718292f2SDouglas Gregor case MMToken::LBrace: 660a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 661718292f2SDouglas Gregor return; 662718292f2SDouglas Gregor 663718292f2SDouglas Gregor ++braceDepth; 664718292f2SDouglas Gregor break; 665718292f2SDouglas Gregor 666a686e1b0SDouglas Gregor case MMToken::LSquare: 667a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 668a686e1b0SDouglas Gregor return; 669a686e1b0SDouglas Gregor 670a686e1b0SDouglas Gregor ++squareDepth; 671a686e1b0SDouglas Gregor break; 672a686e1b0SDouglas Gregor 673718292f2SDouglas Gregor case MMToken::RBrace: 674718292f2SDouglas Gregor if (braceDepth > 0) 675718292f2SDouglas Gregor --braceDepth; 676718292f2SDouglas Gregor else if (Tok.is(K)) 677718292f2SDouglas Gregor return; 678718292f2SDouglas Gregor break; 679718292f2SDouglas Gregor 680a686e1b0SDouglas Gregor case MMToken::RSquare: 681a686e1b0SDouglas Gregor if (squareDepth > 0) 682a686e1b0SDouglas Gregor --squareDepth; 683a686e1b0SDouglas Gregor else if (Tok.is(K)) 684a686e1b0SDouglas Gregor return; 685a686e1b0SDouglas Gregor break; 686a686e1b0SDouglas Gregor 687718292f2SDouglas Gregor default: 688a686e1b0SDouglas Gregor if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 689718292f2SDouglas Gregor return; 690718292f2SDouglas Gregor break; 691718292f2SDouglas Gregor } 692718292f2SDouglas Gregor 693718292f2SDouglas Gregor consumeToken(); 694718292f2SDouglas Gregor } while (true); 695718292f2SDouglas Gregor } 696718292f2SDouglas Gregor 697e7ab3669SDouglas Gregor /// \brief Parse a module-id. 698e7ab3669SDouglas Gregor /// 699e7ab3669SDouglas Gregor /// module-id: 700e7ab3669SDouglas Gregor /// identifier 701e7ab3669SDouglas Gregor /// identifier '.' module-id 702e7ab3669SDouglas Gregor /// 703e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise. 704e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) { 705e7ab3669SDouglas Gregor Id.clear(); 706e7ab3669SDouglas Gregor do { 707e7ab3669SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 708e7ab3669SDouglas Gregor Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 709e7ab3669SDouglas Gregor consumeToken(); 710e7ab3669SDouglas Gregor } else { 711e7ab3669SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 712e7ab3669SDouglas Gregor return true; 713e7ab3669SDouglas Gregor } 714e7ab3669SDouglas Gregor 715e7ab3669SDouglas Gregor if (!Tok.is(MMToken::Period)) 716e7ab3669SDouglas Gregor break; 717e7ab3669SDouglas Gregor 718e7ab3669SDouglas Gregor consumeToken(); 719e7ab3669SDouglas Gregor } while (true); 720e7ab3669SDouglas Gregor 721e7ab3669SDouglas Gregor return false; 722e7ab3669SDouglas Gregor } 723e7ab3669SDouglas Gregor 724a686e1b0SDouglas Gregor namespace { 725a686e1b0SDouglas Gregor /// \brief Enumerates the known attributes. 726a686e1b0SDouglas Gregor enum AttributeKind { 727a686e1b0SDouglas Gregor /// \brief An unknown attribute. 728a686e1b0SDouglas Gregor AT_unknown, 729a686e1b0SDouglas Gregor /// \brief The 'system' attribute. 730a686e1b0SDouglas Gregor AT_system 731a686e1b0SDouglas Gregor }; 732a686e1b0SDouglas Gregor } 733a686e1b0SDouglas Gregor 734718292f2SDouglas Gregor /// \brief Parse a module declaration. 735718292f2SDouglas Gregor /// 736718292f2SDouglas Gregor /// module-declaration: 737a686e1b0SDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 738a686e1b0SDouglas Gregor /// { module-member* } 739a686e1b0SDouglas Gregor /// 740a686e1b0SDouglas Gregor /// attributes: 741a686e1b0SDouglas Gregor /// attribute attributes 742a686e1b0SDouglas Gregor /// attribute 743a686e1b0SDouglas Gregor /// 744a686e1b0SDouglas Gregor /// attribute: 745a686e1b0SDouglas Gregor /// [ identifier ] 746718292f2SDouglas Gregor /// 747718292f2SDouglas Gregor /// module-member: 7481fb5c3a6SDouglas Gregor /// requires-declaration 749718292f2SDouglas Gregor /// header-declaration 750e7ab3669SDouglas Gregor /// submodule-declaration 7512b82c2a5SDouglas Gregor /// export-declaration 75273441091SDouglas Gregor /// 75373441091SDouglas Gregor /// submodule-declaration: 75473441091SDouglas Gregor /// module-declaration 75573441091SDouglas Gregor /// inferred-submodule-declaration 756718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() { 757755b2055SDouglas Gregor assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 758755b2055SDouglas Gregor Tok.is(MMToken::FrameworkKeyword)); 759f2161a70SDouglas Gregor // Parse 'explicit' or 'framework' keyword, if present. 760e7ab3669SDouglas Gregor SourceLocation ExplicitLoc; 761718292f2SDouglas Gregor bool Explicit = false; 762f2161a70SDouglas Gregor bool Framework = false; 763755b2055SDouglas Gregor 764f2161a70SDouglas Gregor // Parse 'explicit' keyword, if present. 765f2161a70SDouglas Gregor if (Tok.is(MMToken::ExplicitKeyword)) { 766e7ab3669SDouglas Gregor ExplicitLoc = consumeToken(); 767f2161a70SDouglas Gregor Explicit = true; 768f2161a70SDouglas Gregor } 769f2161a70SDouglas Gregor 770f2161a70SDouglas Gregor // Parse 'framework' keyword, if present. 771755b2055SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 772755b2055SDouglas Gregor consumeToken(); 773755b2055SDouglas Gregor Framework = true; 774755b2055SDouglas Gregor } 775718292f2SDouglas Gregor 776718292f2SDouglas Gregor // Parse 'module' keyword. 777718292f2SDouglas Gregor if (!Tok.is(MMToken::ModuleKeyword)) { 778d6343c99SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 779718292f2SDouglas Gregor consumeToken(); 780718292f2SDouglas Gregor HadError = true; 781718292f2SDouglas Gregor return; 782718292f2SDouglas Gregor } 783718292f2SDouglas Gregor consumeToken(); // 'module' keyword 784718292f2SDouglas Gregor 78573441091SDouglas Gregor // If we have a wildcard for the module name, this is an inferred submodule. 78673441091SDouglas Gregor // Parse it. 78773441091SDouglas Gregor if (Tok.is(MMToken::Star)) 78873441091SDouglas Gregor return parseInferredSubmoduleDecl(Explicit); 78973441091SDouglas Gregor 790718292f2SDouglas Gregor // Parse the module name. 791e7ab3669SDouglas Gregor ModuleId Id; 792e7ab3669SDouglas Gregor if (parseModuleId(Id)) { 793718292f2SDouglas Gregor HadError = true; 794718292f2SDouglas Gregor return; 795718292f2SDouglas Gregor } 796e7ab3669SDouglas Gregor 797e7ab3669SDouglas Gregor if (ActiveModule) { 798e7ab3669SDouglas Gregor if (Id.size() > 1) { 799e7ab3669SDouglas Gregor Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 800e7ab3669SDouglas Gregor << SourceRange(Id.front().second, Id.back().second); 801e7ab3669SDouglas Gregor 802e7ab3669SDouglas Gregor HadError = true; 803e7ab3669SDouglas Gregor return; 804e7ab3669SDouglas Gregor } 805e7ab3669SDouglas Gregor } else if (Id.size() == 1 && Explicit) { 806e7ab3669SDouglas Gregor // Top-level modules can't be explicit. 807e7ab3669SDouglas Gregor Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 808e7ab3669SDouglas Gregor Explicit = false; 809e7ab3669SDouglas Gregor ExplicitLoc = SourceLocation(); 810e7ab3669SDouglas Gregor HadError = true; 811e7ab3669SDouglas Gregor } 812e7ab3669SDouglas Gregor 813e7ab3669SDouglas Gregor Module *PreviousActiveModule = ActiveModule; 814e7ab3669SDouglas Gregor if (Id.size() > 1) { 815e7ab3669SDouglas Gregor // This module map defines a submodule. Go find the module of which it 816e7ab3669SDouglas Gregor // is a submodule. 817e7ab3669SDouglas Gregor ActiveModule = 0; 818e7ab3669SDouglas Gregor for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 819e7ab3669SDouglas Gregor if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 820e7ab3669SDouglas Gregor ActiveModule = Next; 821e7ab3669SDouglas Gregor continue; 822e7ab3669SDouglas Gregor } 823e7ab3669SDouglas Gregor 824e7ab3669SDouglas Gregor if (ActiveModule) { 825e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 826e7ab3669SDouglas Gregor << Id[I].first << ActiveModule->getTopLevelModule(); 827e7ab3669SDouglas Gregor } else { 828e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 829e7ab3669SDouglas Gregor } 830e7ab3669SDouglas Gregor HadError = true; 831e7ab3669SDouglas Gregor return; 832e7ab3669SDouglas Gregor } 833e7ab3669SDouglas Gregor } 834e7ab3669SDouglas Gregor 835e7ab3669SDouglas Gregor StringRef ModuleName = Id.back().first; 836e7ab3669SDouglas Gregor SourceLocation ModuleNameLoc = Id.back().second; 837718292f2SDouglas Gregor 838a686e1b0SDouglas Gregor // Parse the optional attribute list. 839a686e1b0SDouglas Gregor bool IsSystem = false; 840a686e1b0SDouglas Gregor while (Tok.is(MMToken::LSquare)) { 841a686e1b0SDouglas Gregor // Consume the '['. 842a686e1b0SDouglas Gregor SourceLocation LSquareLoc = consumeToken(); 843a686e1b0SDouglas Gregor 844a686e1b0SDouglas Gregor // Check whether we have an attribute name here. 845a686e1b0SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 846a686e1b0SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 847a686e1b0SDouglas Gregor skipUntil(MMToken::RSquare); 848a686e1b0SDouglas Gregor if (Tok.is(MMToken::RSquare)) 849a686e1b0SDouglas Gregor consumeToken(); 850a686e1b0SDouglas Gregor continue; 851a686e1b0SDouglas Gregor } 852a686e1b0SDouglas Gregor 853a686e1b0SDouglas Gregor // Decode the attribute name. 854a686e1b0SDouglas Gregor AttributeKind Attribute 855a686e1b0SDouglas Gregor = llvm::StringSwitch<AttributeKind>(Tok.getString()) 856a686e1b0SDouglas Gregor .Case("system", AT_system) 857a686e1b0SDouglas Gregor .Default(AT_unknown); 858a686e1b0SDouglas Gregor switch (Attribute) { 859a686e1b0SDouglas Gregor case AT_unknown: 860a686e1b0SDouglas Gregor Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 861a686e1b0SDouglas Gregor << Tok.getString(); 862a686e1b0SDouglas Gregor break; 863a686e1b0SDouglas Gregor 864a686e1b0SDouglas Gregor case AT_system: 865a686e1b0SDouglas Gregor IsSystem = true; 866a686e1b0SDouglas Gregor break; 867a686e1b0SDouglas Gregor } 868a686e1b0SDouglas Gregor consumeToken(); 869a686e1b0SDouglas Gregor 870a686e1b0SDouglas Gregor // Consume the ']'. 871a686e1b0SDouglas Gregor if (!Tok.is(MMToken::RSquare)) { 872a686e1b0SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 873a686e1b0SDouglas Gregor Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 874a686e1b0SDouglas Gregor skipUntil(MMToken::RSquare); 875a686e1b0SDouglas Gregor } 876a686e1b0SDouglas Gregor 877a686e1b0SDouglas Gregor if (Tok.is(MMToken::RSquare)) 878a686e1b0SDouglas Gregor consumeToken(); 879a686e1b0SDouglas Gregor } 880a686e1b0SDouglas Gregor 881718292f2SDouglas Gregor // Parse the opening brace. 882718292f2SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 883718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 884718292f2SDouglas Gregor << ModuleName; 885718292f2SDouglas Gregor HadError = true; 886718292f2SDouglas Gregor return; 887718292f2SDouglas Gregor } 888718292f2SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 889718292f2SDouglas Gregor 890718292f2SDouglas Gregor // Determine whether this (sub)module has already been defined. 891eb90e830SDouglas Gregor if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 892fcc54a3bSDouglas Gregor if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { 893fcc54a3bSDouglas Gregor // Skip the module definition. 894fcc54a3bSDouglas Gregor skipUntil(MMToken::RBrace); 895fcc54a3bSDouglas Gregor if (Tok.is(MMToken::RBrace)) 896fcc54a3bSDouglas Gregor consumeToken(); 897fcc54a3bSDouglas Gregor else { 898fcc54a3bSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 899fcc54a3bSDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 900fcc54a3bSDouglas Gregor HadError = true; 901fcc54a3bSDouglas Gregor } 902fcc54a3bSDouglas Gregor return; 903fcc54a3bSDouglas Gregor } 904fcc54a3bSDouglas Gregor 905718292f2SDouglas Gregor Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 906718292f2SDouglas Gregor << ModuleName; 907eb90e830SDouglas Gregor Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 908718292f2SDouglas Gregor 909718292f2SDouglas Gregor // Skip the module definition. 910718292f2SDouglas Gregor skipUntil(MMToken::RBrace); 911718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 912718292f2SDouglas Gregor consumeToken(); 913718292f2SDouglas Gregor 914718292f2SDouglas Gregor HadError = true; 915718292f2SDouglas Gregor return; 916718292f2SDouglas Gregor } 917718292f2SDouglas Gregor 918718292f2SDouglas Gregor // Start defining this module. 919eb90e830SDouglas Gregor ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, 920eb90e830SDouglas Gregor Explicit).first; 921eb90e830SDouglas Gregor ActiveModule->DefinitionLoc = ModuleNameLoc; 922a686e1b0SDouglas Gregor if (IsSystem) 923a686e1b0SDouglas Gregor ActiveModule->IsSystem = true; 924718292f2SDouglas Gregor 925718292f2SDouglas Gregor bool Done = false; 926718292f2SDouglas Gregor do { 927718292f2SDouglas Gregor switch (Tok.Kind) { 928718292f2SDouglas Gregor case MMToken::EndOfFile: 929718292f2SDouglas Gregor case MMToken::RBrace: 930718292f2SDouglas Gregor Done = true; 931718292f2SDouglas Gregor break; 932718292f2SDouglas Gregor 933718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 934f2161a70SDouglas Gregor case MMToken::FrameworkKeyword: 935718292f2SDouglas Gregor case MMToken::ModuleKeyword: 936718292f2SDouglas Gregor parseModuleDecl(); 937718292f2SDouglas Gregor break; 938718292f2SDouglas Gregor 9392b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 9402b82c2a5SDouglas Gregor parseExportDecl(); 9412b82c2a5SDouglas Gregor break; 9422b82c2a5SDouglas Gregor 9431fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 9441fb5c3a6SDouglas Gregor parseRequiresDecl(); 9451fb5c3a6SDouglas Gregor break; 9461fb5c3a6SDouglas Gregor 947524e33e1SDouglas Gregor case MMToken::UmbrellaKeyword: { 948524e33e1SDouglas Gregor SourceLocation UmbrellaLoc = consumeToken(); 949524e33e1SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) 950524e33e1SDouglas Gregor parseHeaderDecl(UmbrellaLoc); 951524e33e1SDouglas Gregor else 952524e33e1SDouglas Gregor parseUmbrellaDirDecl(UmbrellaLoc); 953718292f2SDouglas Gregor break; 954524e33e1SDouglas Gregor } 955718292f2SDouglas Gregor 956322f633cSDouglas Gregor case MMToken::HeaderKeyword: 957322f633cSDouglas Gregor parseHeaderDecl(SourceLocation()); 958718292f2SDouglas Gregor break; 959718292f2SDouglas Gregor 960718292f2SDouglas Gregor default: 961718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 962718292f2SDouglas Gregor consumeToken(); 963718292f2SDouglas Gregor break; 964718292f2SDouglas Gregor } 965718292f2SDouglas Gregor } while (!Done); 966718292f2SDouglas Gregor 967718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 968718292f2SDouglas Gregor consumeToken(); 969718292f2SDouglas Gregor else { 970718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 971718292f2SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 972718292f2SDouglas Gregor HadError = true; 973718292f2SDouglas Gregor } 974718292f2SDouglas Gregor 975e7ab3669SDouglas Gregor // We're done parsing this module. Pop back to the previous module. 976e7ab3669SDouglas Gregor ActiveModule = PreviousActiveModule; 977718292f2SDouglas Gregor } 978718292f2SDouglas Gregor 9791fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration. 9801fb5c3a6SDouglas Gregor /// 9811fb5c3a6SDouglas Gregor /// requires-declaration: 9821fb5c3a6SDouglas Gregor /// 'requires' feature-list 9831fb5c3a6SDouglas Gregor /// 9841fb5c3a6SDouglas Gregor /// feature-list: 9851fb5c3a6SDouglas Gregor /// identifier ',' feature-list 9861fb5c3a6SDouglas Gregor /// identifier 9871fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() { 9881fb5c3a6SDouglas Gregor assert(Tok.is(MMToken::RequiresKeyword)); 9891fb5c3a6SDouglas Gregor 9901fb5c3a6SDouglas Gregor // Parse 'requires' keyword. 9911fb5c3a6SDouglas Gregor consumeToken(); 9921fb5c3a6SDouglas Gregor 9931fb5c3a6SDouglas Gregor // Parse the feature-list. 9941fb5c3a6SDouglas Gregor do { 9951fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 9961fb5c3a6SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 9971fb5c3a6SDouglas Gregor HadError = true; 9981fb5c3a6SDouglas Gregor return; 9991fb5c3a6SDouglas Gregor } 10001fb5c3a6SDouglas Gregor 10011fb5c3a6SDouglas Gregor // Consume the feature name. 10021fb5c3a6SDouglas Gregor std::string Feature = Tok.getString(); 10031fb5c3a6SDouglas Gregor consumeToken(); 10041fb5c3a6SDouglas Gregor 10051fb5c3a6SDouglas Gregor // Add this feature. 100689929282SDouglas Gregor ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target); 10071fb5c3a6SDouglas Gregor 10081fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Comma)) 10091fb5c3a6SDouglas Gregor break; 10101fb5c3a6SDouglas Gregor 10111fb5c3a6SDouglas Gregor // Consume the comma. 10121fb5c3a6SDouglas Gregor consumeToken(); 10131fb5c3a6SDouglas Gregor } while (true); 10141fb5c3a6SDouglas Gregor } 10151fb5c3a6SDouglas Gregor 1016f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the 1017f2161a70SDouglas Gregor /// subframework in which the given module lives. 1018*bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod, 1019*bf8da9d7SBenjamin Kramer llvm::SmallVectorImpl<char> &Path) { 1020f2161a70SDouglas Gregor // Collect the framework names from the given module to the top-level module. 1021f2161a70SDouglas Gregor llvm::SmallVector<StringRef, 2> Paths; 1022f2161a70SDouglas Gregor for (; Mod; Mod = Mod->Parent) { 1023f2161a70SDouglas Gregor if (Mod->IsFramework) 1024f2161a70SDouglas Gregor Paths.push_back(Mod->Name); 1025f2161a70SDouglas Gregor } 1026f2161a70SDouglas Gregor 1027f2161a70SDouglas Gregor if (Paths.empty()) 1028f2161a70SDouglas Gregor return; 1029f2161a70SDouglas Gregor 1030f2161a70SDouglas Gregor // Add Frameworks/Name.framework for each subframework. 1031f2161a70SDouglas Gregor for (unsigned I = Paths.size() - 1; I != 0; --I) { 1032f2161a70SDouglas Gregor llvm::sys::path::append(Path, "Frameworks"); 1033f2161a70SDouglas Gregor llvm::sys::path::append(Path, Paths[I-1] + ".framework"); 1034f2161a70SDouglas Gregor } 1035f2161a70SDouglas Gregor } 1036f2161a70SDouglas Gregor 10373ec6663bSDouglas Gregor /// \brief Determine whether the given file name is the name of a builtin 10383ec6663bSDouglas Gregor /// header, supplied by Clang to replace, override, or augment existing system 10393ec6663bSDouglas Gregor /// headers. 10403ec6663bSDouglas Gregor static bool isBuiltinHeader(StringRef FileName) { 10413ec6663bSDouglas Gregor return llvm::StringSwitch<bool>(FileName) 10423ec6663bSDouglas Gregor .Case("float.h", true) 10433ec6663bSDouglas Gregor .Case("iso646.h", true) 10443ec6663bSDouglas Gregor .Case("limits.h", true) 10453ec6663bSDouglas Gregor .Case("stdalign.h", true) 10463ec6663bSDouglas Gregor .Case("stdarg.h", true) 10473ec6663bSDouglas Gregor .Case("stdbool.h", true) 10483ec6663bSDouglas Gregor .Case("stddef.h", true) 10493ec6663bSDouglas Gregor .Case("stdint.h", true) 10503ec6663bSDouglas Gregor .Case("tgmath.h", true) 10513ec6663bSDouglas Gregor .Case("unwind.h", true) 10523ec6663bSDouglas Gregor .Default(false); 10533ec6663bSDouglas Gregor } 10543ec6663bSDouglas Gregor 1055718292f2SDouglas Gregor /// \brief Parse a header declaration. 1056718292f2SDouglas Gregor /// 1057718292f2SDouglas Gregor /// header-declaration: 1058322f633cSDouglas Gregor /// 'umbrella'[opt] 'header' string-literal 1059322f633cSDouglas Gregor void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc) { 1060718292f2SDouglas Gregor assert(Tok.is(MMToken::HeaderKeyword)); 10611871ed3dSBenjamin Kramer consumeToken(); 1062718292f2SDouglas Gregor 1063322f633cSDouglas Gregor bool Umbrella = UmbrellaLoc.isValid(); 1064322f633cSDouglas Gregor 1065718292f2SDouglas Gregor // Parse the header name. 1066718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1067718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1068718292f2SDouglas Gregor << "header"; 1069718292f2SDouglas Gregor HadError = true; 1070718292f2SDouglas Gregor return; 1071718292f2SDouglas Gregor } 1072e7ab3669SDouglas Gregor std::string FileName = Tok.getString(); 1073718292f2SDouglas Gregor SourceLocation FileNameLoc = consumeToken(); 1074718292f2SDouglas Gregor 1075524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1076524e33e1SDouglas Gregor if (Umbrella && ActiveModule->Umbrella) { 1077524e33e1SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash) 1078524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1079322f633cSDouglas Gregor HadError = true; 1080322f633cSDouglas Gregor return; 1081322f633cSDouglas Gregor } 1082322f633cSDouglas Gregor 10835257fc63SDouglas Gregor // Look for this file. 1084e7ab3669SDouglas Gregor const FileEntry *File = 0; 10853ec6663bSDouglas Gregor const FileEntry *BuiltinFile = 0; 10862c1dd271SDylan Noblesmith SmallString<128> PathName; 1087e7ab3669SDouglas Gregor if (llvm::sys::path::is_absolute(FileName)) { 1088e7ab3669SDouglas Gregor PathName = FileName; 1089e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 10907033127bSDouglas Gregor } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) { 10917033127bSDouglas Gregor PathName = Dir->getName(); 10927033127bSDouglas Gregor llvm::sys::path::append(PathName, FileName); 10937033127bSDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1094e7ab3669SDouglas Gregor } else { 1095e7ab3669SDouglas Gregor // Search for the header file within the search directory. 10967033127bSDouglas Gregor PathName = Directory->getName(); 1097e7ab3669SDouglas Gregor unsigned PathLength = PathName.size(); 1098755b2055SDouglas Gregor 1099f2161a70SDouglas Gregor if (ActiveModule->isPartOfFramework()) { 1100f2161a70SDouglas Gregor appendSubframeworkPaths(ActiveModule, PathName); 1101755b2055SDouglas Gregor 1102e7ab3669SDouglas Gregor // Check whether this file is in the public headers. 1103e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, "Headers"); 11045257fc63SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1105e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1106e7ab3669SDouglas Gregor 1107e7ab3669SDouglas Gregor if (!File) { 1108e7ab3669SDouglas Gregor // Check whether this file is in the private headers. 1109e7ab3669SDouglas Gregor PathName.resize(PathLength); 1110e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, "PrivateHeaders"); 1111e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1112e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1113e7ab3669SDouglas Gregor } 1114e7ab3669SDouglas Gregor } else { 1115e7ab3669SDouglas Gregor // Lookup for normal headers. 1116e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1117e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 11183ec6663bSDouglas Gregor 11193ec6663bSDouglas Gregor // If this is a system module with a top-level header, this header 11203ec6663bSDouglas Gregor // may have a counterpart (or replacement) in the set of headers 11213ec6663bSDouglas Gregor // supplied by Clang. Find that builtin header. 11223ec6663bSDouglas Gregor if (ActiveModule->IsSystem && !Umbrella && BuiltinIncludeDir && 11233ec6663bSDouglas Gregor BuiltinIncludeDir != Directory && isBuiltinHeader(FileName)) { 11242c1dd271SDylan Noblesmith SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); 11253ec6663bSDouglas Gregor llvm::sys::path::append(BuiltinPathName, FileName); 11263ec6663bSDouglas Gregor BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); 11273ec6663bSDouglas Gregor 11283ec6663bSDouglas Gregor // If Clang supplies this header but the underlying system does not, 11293ec6663bSDouglas Gregor // just silently swap in our builtin version. Otherwise, we'll end 11303ec6663bSDouglas Gregor // up adding both (later). 11313ec6663bSDouglas Gregor if (!File && BuiltinFile) { 11323ec6663bSDouglas Gregor File = BuiltinFile; 11333ec6663bSDouglas Gregor BuiltinFile = 0; 11343ec6663bSDouglas Gregor } 11353ec6663bSDouglas Gregor } 1136e7ab3669SDouglas Gregor } 1137e7ab3669SDouglas Gregor } 11385257fc63SDouglas Gregor 11395257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 11405257fc63SDouglas Gregor // Come up with a lazy way to do this. 1141e7ab3669SDouglas Gregor if (File) { 11425257fc63SDouglas Gregor if (const Module *OwningModule = Map.Headers[File]) { 11435257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_conflict) 11445257fc63SDouglas Gregor << FileName << OwningModule->getFullModuleName(); 11455257fc63SDouglas Gregor HadError = true; 1146322f633cSDouglas Gregor } else if (Umbrella) { 1147322f633cSDouglas Gregor const DirectoryEntry *UmbrellaDir = File->getDir(); 1148322f633cSDouglas Gregor if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) { 1149322f633cSDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1150322f633cSDouglas Gregor << OwningModule->getFullModuleName(); 1151322f633cSDouglas Gregor HadError = true; 11525257fc63SDouglas Gregor } else { 1153322f633cSDouglas Gregor // Record this umbrella header. 1154322f633cSDouglas Gregor Map.setUmbrellaHeader(ActiveModule, File); 1155322f633cSDouglas Gregor } 1156322f633cSDouglas Gregor } else { 1157322f633cSDouglas Gregor // Record this header. 1158a89c5ac4SDouglas Gregor Map.addHeader(ActiveModule, File); 11593ec6663bSDouglas Gregor 11603ec6663bSDouglas Gregor // If there is a builtin counterpart to this file, add it now. 11613ec6663bSDouglas Gregor if (BuiltinFile) 11623ec6663bSDouglas Gregor Map.addHeader(ActiveModule, BuiltinFile); 11635257fc63SDouglas Gregor } 11645257fc63SDouglas Gregor } else { 11655257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_not_found) 1166524e33e1SDouglas Gregor << Umbrella << FileName; 11675257fc63SDouglas Gregor HadError = true; 11685257fc63SDouglas Gregor } 1169718292f2SDouglas Gregor } 1170718292f2SDouglas Gregor 1171524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration. 1172524e33e1SDouglas Gregor /// 1173524e33e1SDouglas Gregor /// umbrella-dir-declaration: 1174524e33e1SDouglas Gregor /// umbrella string-literal 1175524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1176524e33e1SDouglas Gregor // Parse the directory name. 1177524e33e1SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1178524e33e1SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1179524e33e1SDouglas Gregor << "umbrella"; 1180524e33e1SDouglas Gregor HadError = true; 1181524e33e1SDouglas Gregor return; 1182524e33e1SDouglas Gregor } 1183524e33e1SDouglas Gregor 1184524e33e1SDouglas Gregor std::string DirName = Tok.getString(); 1185524e33e1SDouglas Gregor SourceLocation DirNameLoc = consumeToken(); 1186524e33e1SDouglas Gregor 1187524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1188524e33e1SDouglas Gregor if (ActiveModule->Umbrella) { 1189524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1190524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1191524e33e1SDouglas Gregor HadError = true; 1192524e33e1SDouglas Gregor return; 1193524e33e1SDouglas Gregor } 1194524e33e1SDouglas Gregor 1195524e33e1SDouglas Gregor // Look for this file. 1196524e33e1SDouglas Gregor const DirectoryEntry *Dir = 0; 1197524e33e1SDouglas Gregor if (llvm::sys::path::is_absolute(DirName)) 1198524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(DirName); 1199524e33e1SDouglas Gregor else { 12002c1dd271SDylan Noblesmith SmallString<128> PathName; 1201524e33e1SDouglas Gregor PathName = Directory->getName(); 1202524e33e1SDouglas Gregor llvm::sys::path::append(PathName, DirName); 1203524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(PathName); 1204524e33e1SDouglas Gregor } 1205524e33e1SDouglas Gregor 1206524e33e1SDouglas Gregor if (!Dir) { 1207524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1208524e33e1SDouglas Gregor << DirName; 1209524e33e1SDouglas Gregor HadError = true; 1210524e33e1SDouglas Gregor return; 1211524e33e1SDouglas Gregor } 1212524e33e1SDouglas Gregor 1213524e33e1SDouglas Gregor if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1214524e33e1SDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1215524e33e1SDouglas Gregor << OwningModule->getFullModuleName(); 1216524e33e1SDouglas Gregor HadError = true; 1217524e33e1SDouglas Gregor return; 1218524e33e1SDouglas Gregor } 1219524e33e1SDouglas Gregor 1220524e33e1SDouglas Gregor // Record this umbrella directory. 1221524e33e1SDouglas Gregor Map.setUmbrellaDir(ActiveModule, Dir); 1222524e33e1SDouglas Gregor } 1223524e33e1SDouglas Gregor 12242b82c2a5SDouglas Gregor /// \brief Parse a module export declaration. 12252b82c2a5SDouglas Gregor /// 12262b82c2a5SDouglas Gregor /// export-declaration: 12272b82c2a5SDouglas Gregor /// 'export' wildcard-module-id 12282b82c2a5SDouglas Gregor /// 12292b82c2a5SDouglas Gregor /// wildcard-module-id: 12302b82c2a5SDouglas Gregor /// identifier 12312b82c2a5SDouglas Gregor /// '*' 12322b82c2a5SDouglas Gregor /// identifier '.' wildcard-module-id 12332b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() { 12342b82c2a5SDouglas Gregor assert(Tok.is(MMToken::ExportKeyword)); 12352b82c2a5SDouglas Gregor SourceLocation ExportLoc = consumeToken(); 12362b82c2a5SDouglas Gregor 12372b82c2a5SDouglas Gregor // Parse the module-id with an optional wildcard at the end. 12382b82c2a5SDouglas Gregor ModuleId ParsedModuleId; 12392b82c2a5SDouglas Gregor bool Wildcard = false; 12402b82c2a5SDouglas Gregor do { 12412b82c2a5SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 12422b82c2a5SDouglas Gregor ParsedModuleId.push_back(std::make_pair(Tok.getString(), 12432b82c2a5SDouglas Gregor Tok.getLocation())); 12442b82c2a5SDouglas Gregor consumeToken(); 12452b82c2a5SDouglas Gregor 12462b82c2a5SDouglas Gregor if (Tok.is(MMToken::Period)) { 12472b82c2a5SDouglas Gregor consumeToken(); 12482b82c2a5SDouglas Gregor continue; 12492b82c2a5SDouglas Gregor } 12502b82c2a5SDouglas Gregor 12512b82c2a5SDouglas Gregor break; 12522b82c2a5SDouglas Gregor } 12532b82c2a5SDouglas Gregor 12542b82c2a5SDouglas Gregor if(Tok.is(MMToken::Star)) { 12552b82c2a5SDouglas Gregor Wildcard = true; 1256f5eedd05SDouglas Gregor consumeToken(); 12572b82c2a5SDouglas Gregor break; 12582b82c2a5SDouglas Gregor } 12592b82c2a5SDouglas Gregor 12602b82c2a5SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id); 12612b82c2a5SDouglas Gregor HadError = true; 12622b82c2a5SDouglas Gregor return; 12632b82c2a5SDouglas Gregor } while (true); 12642b82c2a5SDouglas Gregor 12652b82c2a5SDouglas Gregor Module::UnresolvedExportDecl Unresolved = { 12662b82c2a5SDouglas Gregor ExportLoc, ParsedModuleId, Wildcard 12672b82c2a5SDouglas Gregor }; 12682b82c2a5SDouglas Gregor ActiveModule->UnresolvedExports.push_back(Unresolved); 12692b82c2a5SDouglas Gregor } 12702b82c2a5SDouglas Gregor 127173441091SDouglas Gregor void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) { 127273441091SDouglas Gregor assert(Tok.is(MMToken::Star)); 127373441091SDouglas Gregor SourceLocation StarLoc = consumeToken(); 127473441091SDouglas Gregor bool Failed = false; 127573441091SDouglas Gregor 127673441091SDouglas Gregor // Inferred modules must be submodules. 127773441091SDouglas Gregor if (!ActiveModule) { 127873441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 127973441091SDouglas Gregor Failed = true; 128073441091SDouglas Gregor } 128173441091SDouglas Gregor 1282524e33e1SDouglas Gregor // Inferred modules must have umbrella directories. 1283524e33e1SDouglas Gregor if (!Failed && !ActiveModule->getUmbrellaDir()) { 128473441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 128573441091SDouglas Gregor Failed = true; 128673441091SDouglas Gregor } 128773441091SDouglas Gregor 128873441091SDouglas Gregor // Check for redefinition of an inferred module. 1289dd005f69SDouglas Gregor if (!Failed && ActiveModule->InferSubmodules) { 129073441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 1291dd005f69SDouglas Gregor if (ActiveModule->InferredSubmoduleLoc.isValid()) 1292dd005f69SDouglas Gregor Diags.Report(ActiveModule->InferredSubmoduleLoc, 129373441091SDouglas Gregor diag::note_mmap_prev_definition); 129473441091SDouglas Gregor Failed = true; 129573441091SDouglas Gregor } 129673441091SDouglas Gregor 129773441091SDouglas Gregor // If there were any problems with this inferred submodule, skip its body. 129873441091SDouglas Gregor if (Failed) { 129973441091SDouglas Gregor if (Tok.is(MMToken::LBrace)) { 130073441091SDouglas Gregor consumeToken(); 130173441091SDouglas Gregor skipUntil(MMToken::RBrace); 130273441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 130373441091SDouglas Gregor consumeToken(); 130473441091SDouglas Gregor } 130573441091SDouglas Gregor HadError = true; 130673441091SDouglas Gregor return; 130773441091SDouglas Gregor } 130873441091SDouglas Gregor 130973441091SDouglas Gregor // Note that we have an inferred submodule. 1310dd005f69SDouglas Gregor ActiveModule->InferSubmodules = true; 1311dd005f69SDouglas Gregor ActiveModule->InferredSubmoduleLoc = StarLoc; 1312dd005f69SDouglas Gregor ActiveModule->InferExplicitSubmodules = Explicit; 131373441091SDouglas Gregor 131473441091SDouglas Gregor // Parse the opening brace. 131573441091SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 131673441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 131773441091SDouglas Gregor HadError = true; 131873441091SDouglas Gregor return; 131973441091SDouglas Gregor } 132073441091SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 132173441091SDouglas Gregor 132273441091SDouglas Gregor // Parse the body of the inferred submodule. 132373441091SDouglas Gregor bool Done = false; 132473441091SDouglas Gregor do { 132573441091SDouglas Gregor switch (Tok.Kind) { 132673441091SDouglas Gregor case MMToken::EndOfFile: 132773441091SDouglas Gregor case MMToken::RBrace: 132873441091SDouglas Gregor Done = true; 132973441091SDouglas Gregor break; 133073441091SDouglas Gregor 133173441091SDouglas Gregor case MMToken::ExportKeyword: { 133273441091SDouglas Gregor consumeToken(); 133373441091SDouglas Gregor if (Tok.is(MMToken::Star)) 1334dd005f69SDouglas Gregor ActiveModule->InferExportWildcard = true; 133573441091SDouglas Gregor else 133673441091SDouglas Gregor Diags.Report(Tok.getLocation(), 133773441091SDouglas Gregor diag::err_mmap_expected_export_wildcard); 133873441091SDouglas Gregor consumeToken(); 133973441091SDouglas Gregor break; 134073441091SDouglas Gregor } 134173441091SDouglas Gregor 134273441091SDouglas Gregor case MMToken::ExplicitKeyword: 134373441091SDouglas Gregor case MMToken::ModuleKeyword: 134473441091SDouglas Gregor case MMToken::HeaderKeyword: 134573441091SDouglas Gregor case MMToken::UmbrellaKeyword: 134673441091SDouglas Gregor default: 134773441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member); 134873441091SDouglas Gregor consumeToken(); 134973441091SDouglas Gregor break; 135073441091SDouglas Gregor } 135173441091SDouglas Gregor } while (!Done); 135273441091SDouglas Gregor 135373441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 135473441091SDouglas Gregor consumeToken(); 135573441091SDouglas Gregor else { 135673441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 135773441091SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 135873441091SDouglas Gregor HadError = true; 135973441091SDouglas Gregor } 136073441091SDouglas Gregor } 136173441091SDouglas Gregor 13627033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence 13637033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null. 13647033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() { 13657033127bSDouglas Gregor for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) { 13667033127bSDouglas Gregor // If we have an umbrella directory, use that. 13677033127bSDouglas Gregor if (Mod->hasUmbrellaDir()) 13687033127bSDouglas Gregor return Mod->getUmbrellaDir(); 13697033127bSDouglas Gregor 13707033127bSDouglas Gregor // If we have a framework directory, stop looking. 13717033127bSDouglas Gregor if (Mod->IsFramework) 13727033127bSDouglas Gregor return 0; 13737033127bSDouglas Gregor } 13747033127bSDouglas Gregor 13757033127bSDouglas Gregor return 0; 13767033127bSDouglas Gregor } 13777033127bSDouglas Gregor 1378718292f2SDouglas Gregor /// \brief Parse a module map file. 1379718292f2SDouglas Gregor /// 1380718292f2SDouglas Gregor /// module-map-file: 1381718292f2SDouglas Gregor /// module-declaration* 1382718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() { 1383718292f2SDouglas Gregor do { 1384718292f2SDouglas Gregor switch (Tok.Kind) { 1385718292f2SDouglas Gregor case MMToken::EndOfFile: 1386718292f2SDouglas Gregor return HadError; 1387718292f2SDouglas Gregor 1388e7ab3669SDouglas Gregor case MMToken::ExplicitKeyword: 1389718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1390755b2055SDouglas Gregor case MMToken::FrameworkKeyword: 1391718292f2SDouglas Gregor parseModuleDecl(); 1392718292f2SDouglas Gregor break; 1393718292f2SDouglas Gregor 13941fb5c3a6SDouglas Gregor case MMToken::Comma: 13952b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 1396718292f2SDouglas Gregor case MMToken::HeaderKeyword: 1397718292f2SDouglas Gregor case MMToken::Identifier: 1398718292f2SDouglas Gregor case MMToken::LBrace: 1399a686e1b0SDouglas Gregor case MMToken::LSquare: 14002b82c2a5SDouglas Gregor case MMToken::Period: 1401718292f2SDouglas Gregor case MMToken::RBrace: 1402a686e1b0SDouglas Gregor case MMToken::RSquare: 14031fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 14042b82c2a5SDouglas Gregor case MMToken::Star: 1405718292f2SDouglas Gregor case MMToken::StringLiteral: 1406718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 1407718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1408718292f2SDouglas Gregor HadError = true; 1409718292f2SDouglas Gregor consumeToken(); 1410718292f2SDouglas Gregor break; 1411718292f2SDouglas Gregor } 1412718292f2SDouglas Gregor } while (true); 1413718292f2SDouglas Gregor } 1414718292f2SDouglas Gregor 1415718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) { 141689929282SDouglas Gregor assert(Target != 0 && "Missing target information"); 1417718292f2SDouglas Gregor FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User); 1418718292f2SDouglas Gregor const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID); 1419718292f2SDouglas Gregor if (!Buffer) 1420718292f2SDouglas Gregor return true; 1421718292f2SDouglas Gregor 1422718292f2SDouglas Gregor // Parse this module map file. 14231fb5c3a6SDouglas Gregor Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts); 14241fb5c3a6SDouglas Gregor Diags->getClient()->BeginSourceFile(MMapLangOpts); 14253ec6663bSDouglas Gregor ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir(), 14263ec6663bSDouglas Gregor BuiltinIncludeDir); 1427718292f2SDouglas Gregor bool Result = Parser.parseModuleMapFile(); 1428718292f2SDouglas Gregor Diags->getClient()->EndSourceFile(); 1429718292f2SDouglas Gregor 1430718292f2SDouglas Gregor return Result; 1431718292f2SDouglas Gregor } 1432