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/Basic/Diagnostic.h" 16811db4eaSDouglas Gregor #include "clang/Basic/DiagnosticOptions.h" 17718292f2SDouglas Gregor #include "clang/Basic/FileManager.h" 18718292f2SDouglas Gregor #include "clang/Basic/TargetInfo.h" 19718292f2SDouglas Gregor #include "clang/Basic/TargetOptions.h" 203a02247dSChandler Carruth #include "clang/Lex/LexDiagnostic.h" 213a02247dSChandler Carruth #include "clang/Lex/Lexer.h" 223a02247dSChandler Carruth #include "clang/Lex/LiteralSupport.h" 233a02247dSChandler Carruth #include "llvm/ADT/StringRef.h" 243a02247dSChandler Carruth #include "llvm/ADT/StringSwitch.h" 25718292f2SDouglas Gregor #include "llvm/Support/Allocator.h" 26e89dbc1dSDouglas Gregor #include "llvm/Support/FileSystem.h" 27718292f2SDouglas Gregor #include "llvm/Support/Host.h" 285257fc63SDouglas Gregor #include "llvm/Support/PathV2.h" 29718292f2SDouglas Gregor #include "llvm/Support/raw_ostream.h" 3007c22b78SDouglas Gregor #include <stdlib.h> 31*01c7cfa2SDouglas Gregor #if defined(LLVM_ON_UNIX) 32*01c7cfa2SDouglas Gregor #if defined(__linux__) 33*01c7cfa2SDouglas Gregor #include <linux/limits.h> 34*01c7cfa2SDouglas Gregor #endif 35*01c7cfa2SDouglas Gregor #endif 36718292f2SDouglas Gregor using namespace clang; 37718292f2SDouglas Gregor 382b82c2a5SDouglas Gregor Module::ExportDecl 392b82c2a5SDouglas Gregor ModuleMap::resolveExport(Module *Mod, 402b82c2a5SDouglas Gregor const Module::UnresolvedExportDecl &Unresolved, 412b82c2a5SDouglas Gregor bool Complain) { 42f5eedd05SDouglas Gregor // We may have just a wildcard. 43f5eedd05SDouglas Gregor if (Unresolved.Id.empty()) { 44f5eedd05SDouglas Gregor assert(Unresolved.Wildcard && "Invalid unresolved export"); 45f5eedd05SDouglas Gregor return Module::ExportDecl(0, true); 46f5eedd05SDouglas Gregor } 47f5eedd05SDouglas Gregor 482b82c2a5SDouglas Gregor // Find the starting module. 492b82c2a5SDouglas Gregor Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod); 502b82c2a5SDouglas Gregor if (!Context) { 512b82c2a5SDouglas Gregor if (Complain) 522b82c2a5SDouglas Gregor Diags->Report(Unresolved.Id[0].second, 532b82c2a5SDouglas Gregor diag::err_mmap_missing_module_unqualified) 542b82c2a5SDouglas Gregor << Unresolved.Id[0].first << Mod->getFullModuleName(); 552b82c2a5SDouglas Gregor 562b82c2a5SDouglas Gregor return Module::ExportDecl(); 572b82c2a5SDouglas Gregor } 582b82c2a5SDouglas Gregor 592b82c2a5SDouglas Gregor // Dig into the module path. 602b82c2a5SDouglas Gregor for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) { 612b82c2a5SDouglas Gregor Module *Sub = lookupModuleQualified(Unresolved.Id[I].first, 622b82c2a5SDouglas Gregor Context); 632b82c2a5SDouglas Gregor if (!Sub) { 642b82c2a5SDouglas Gregor if (Complain) 652b82c2a5SDouglas Gregor Diags->Report(Unresolved.Id[I].second, 662b82c2a5SDouglas Gregor diag::err_mmap_missing_module_qualified) 672b82c2a5SDouglas Gregor << Unresolved.Id[I].first << Context->getFullModuleName() 682b82c2a5SDouglas Gregor << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second); 692b82c2a5SDouglas Gregor 702b82c2a5SDouglas Gregor return Module::ExportDecl(); 712b82c2a5SDouglas Gregor } 722b82c2a5SDouglas Gregor 732b82c2a5SDouglas Gregor Context = Sub; 742b82c2a5SDouglas Gregor } 752b82c2a5SDouglas Gregor 762b82c2a5SDouglas Gregor return Module::ExportDecl(Context, Unresolved.Wildcard); 772b82c2a5SDouglas Gregor } 782b82c2a5SDouglas Gregor 791fb5c3a6SDouglas Gregor ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC, 8089929282SDouglas Gregor const LangOptions &LangOpts, const TargetInfo *Target) 813ec6663bSDouglas Gregor : LangOpts(LangOpts), Target(Target), BuiltinIncludeDir(0) 821fb5c3a6SDouglas Gregor { 83c95d8192SDylan Noblesmith IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs); 84c95d8192SDylan Noblesmith Diags = IntrusiveRefCntPtr<DiagnosticsEngine>( 85811db4eaSDouglas Gregor new DiagnosticsEngine(DiagIDs, new DiagnosticOptions)); 86718292f2SDouglas Gregor Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true); 87718292f2SDouglas Gregor SourceMgr = new SourceManager(*Diags, FileMgr); 88718292f2SDouglas Gregor } 89718292f2SDouglas Gregor 90718292f2SDouglas Gregor ModuleMap::~ModuleMap() { 915acdf59eSDouglas Gregor for (llvm::StringMap<Module *>::iterator I = Modules.begin(), 925acdf59eSDouglas Gregor IEnd = Modules.end(); 935acdf59eSDouglas Gregor I != IEnd; ++I) { 945acdf59eSDouglas Gregor delete I->getValue(); 955acdf59eSDouglas Gregor } 965acdf59eSDouglas Gregor 97718292f2SDouglas Gregor delete SourceMgr; 98718292f2SDouglas Gregor } 99718292f2SDouglas Gregor 10089929282SDouglas Gregor void ModuleMap::setTarget(const TargetInfo &Target) { 10189929282SDouglas Gregor assert((!this->Target || this->Target == &Target) && 10289929282SDouglas Gregor "Improper target override"); 10389929282SDouglas Gregor this->Target = &Target; 10489929282SDouglas Gregor } 10589929282SDouglas Gregor 106056396aeSDouglas Gregor /// \brief "Sanitize" a filename so that it can be used as an identifier. 107056396aeSDouglas Gregor static StringRef sanitizeFilenameAsIdentifier(StringRef Name, 108056396aeSDouglas Gregor SmallVectorImpl<char> &Buffer) { 109056396aeSDouglas Gregor if (Name.empty()) 110056396aeSDouglas Gregor return Name; 111056396aeSDouglas Gregor 112056396aeSDouglas Gregor // Check whether the filename is already an identifier; this is the common 113056396aeSDouglas Gregor // case. 114056396aeSDouglas Gregor bool isIdentifier = true; 115056396aeSDouglas Gregor for (unsigned I = 0, N = Name.size(); I != N; ++I) { 116056396aeSDouglas Gregor if (isalpha(Name[I]) || Name[I] == '_' || (isdigit(Name[I]) && I > 0)) 117056396aeSDouglas Gregor continue; 118056396aeSDouglas Gregor 119056396aeSDouglas Gregor isIdentifier = false; 120056396aeSDouglas Gregor break; 121056396aeSDouglas Gregor } 122056396aeSDouglas Gregor 123056396aeSDouglas Gregor if (!isIdentifier) { 124056396aeSDouglas Gregor // If we don't already have something with the form of an identifier, 125056396aeSDouglas Gregor // create a buffer with the sanitized name. 126056396aeSDouglas Gregor Buffer.clear(); 127056396aeSDouglas Gregor if (isdigit(Name[0])) 128056396aeSDouglas Gregor Buffer.push_back('_'); 129056396aeSDouglas Gregor Buffer.reserve(Buffer.size() + Name.size()); 130056396aeSDouglas Gregor for (unsigned I = 0, N = Name.size(); I != N; ++I) { 131056396aeSDouglas Gregor if (isalnum(Name[I]) || isspace(Name[I])) 132056396aeSDouglas Gregor Buffer.push_back(Name[I]); 133056396aeSDouglas Gregor else 134056396aeSDouglas Gregor Buffer.push_back('_'); 135056396aeSDouglas Gregor } 136056396aeSDouglas Gregor 137056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 138056396aeSDouglas Gregor } 139056396aeSDouglas Gregor 140056396aeSDouglas Gregor while (llvm::StringSwitch<bool>(Name) 141056396aeSDouglas Gregor #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true) 142056396aeSDouglas Gregor #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true) 143056396aeSDouglas Gregor #include "clang/Basic/TokenKinds.def" 144056396aeSDouglas Gregor .Default(false)) { 145056396aeSDouglas Gregor if (Name.data() != Buffer.data()) 146056396aeSDouglas Gregor Buffer.append(Name.begin(), Name.end()); 147056396aeSDouglas Gregor Buffer.push_back('_'); 148056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 149056396aeSDouglas Gregor } 150056396aeSDouglas Gregor 151056396aeSDouglas Gregor return Name; 152056396aeSDouglas Gregor } 153056396aeSDouglas Gregor 154de3ef502SDouglas Gregor Module *ModuleMap::findModuleForHeader(const FileEntry *File) { 15559527666SDouglas Gregor HeadersMap::iterator Known = Headers.find(File); 1561fb5c3a6SDouglas Gregor if (Known != Headers.end()) { 15759527666SDouglas Gregor // If a header is not available, don't report that it maps to anything. 15859527666SDouglas Gregor if (!Known->second.isAvailable()) 1591fb5c3a6SDouglas Gregor return 0; 1601fb5c3a6SDouglas Gregor 16159527666SDouglas Gregor return Known->second.getModule(); 1621fb5c3a6SDouglas Gregor } 163ab0c8a84SDouglas Gregor 164b65dbfffSDouglas Gregor const DirectoryEntry *Dir = File->getDir(); 165f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 16674260502SDouglas Gregor #ifdef LLVM_ON_UNIX 16774260502SDouglas Gregor // Note: as an egregious but useful hack we use the real path here, because 16874260502SDouglas Gregor // frameworks moving from top-level frameworks to embedded frameworks tend 16974260502SDouglas Gregor // to be symlinked from the top-level location to the embedded location, 17074260502SDouglas Gregor // and we need to resolve lookups as if we had found the embedded location. 17174260502SDouglas Gregor char RealDirName[PATH_MAX]; 17274260502SDouglas Gregor StringRef DirName; 17374260502SDouglas Gregor if (realpath(Dir->getName(), RealDirName)) 17474260502SDouglas Gregor DirName = RealDirName; 17574260502SDouglas Gregor else 17674260502SDouglas Gregor DirName = Dir->getName(); 17774260502SDouglas Gregor #else 178b65dbfffSDouglas Gregor StringRef DirName = Dir->getName(); 17974260502SDouglas Gregor #endif 180a89c5ac4SDouglas Gregor 181a89c5ac4SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 182a89c5ac4SDouglas Gregor // an umbrella header. 183b65dbfffSDouglas Gregor do { 184a89c5ac4SDouglas Gregor llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir 185a89c5ac4SDouglas Gregor = UmbrellaDirs.find(Dir); 186a89c5ac4SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 187a89c5ac4SDouglas Gregor Module *Result = KnownDir->second; 188930a85ccSDouglas Gregor 189930a85ccSDouglas Gregor // Search up the module stack until we find a module with an umbrella 19073141fa9SDouglas Gregor // directory. 191930a85ccSDouglas Gregor Module *UmbrellaModule = Result; 19273141fa9SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 193930a85ccSDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 194930a85ccSDouglas Gregor 195930a85ccSDouglas Gregor if (UmbrellaModule->InferSubmodules) { 196a89c5ac4SDouglas Gregor // Infer submodules for each of the directories we found between 197a89c5ac4SDouglas Gregor // the directory of the umbrella header and the directory where 198a89c5ac4SDouglas Gregor // the actual header is located. 1999458f82dSDouglas Gregor bool Explicit = UmbrellaModule->InferExplicitSubmodules; 2009458f82dSDouglas Gregor 2017033127bSDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 202a89c5ac4SDouglas Gregor // Find or create the module that corresponds to this directory name. 203056396aeSDouglas Gregor SmallString<32> NameBuf; 204056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 205056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 206056396aeSDouglas Gregor NameBuf); 207a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 2089458f82dSDouglas Gregor Explicit).first; 209a89c5ac4SDouglas Gregor 210a89c5ac4SDouglas Gregor // Associate the module and the directory. 211a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I-1]] = Result; 212a89c5ac4SDouglas Gregor 213a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 214a89c5ac4SDouglas Gregor // wildcard to the set of exports. 215930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 216a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 217a89c5ac4SDouglas Gregor } 218a89c5ac4SDouglas Gregor 219a89c5ac4SDouglas Gregor // Infer a submodule with the same name as this header file. 220056396aeSDouglas Gregor SmallString<32> NameBuf; 221056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 222056396aeSDouglas Gregor llvm::sys::path::stem(File->getName()), NameBuf); 223a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 2249458f82dSDouglas Gregor Explicit).first; 225c597c8c4SArgyrios Kyrtzidis Result->TopHeaders.insert(File); 226a89c5ac4SDouglas Gregor 227a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 228a89c5ac4SDouglas Gregor // wildcard to the set of exports. 229930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 230a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 231a89c5ac4SDouglas Gregor } else { 232a89c5ac4SDouglas Gregor // Record each of the directories we stepped through as being part of 233a89c5ac4SDouglas Gregor // the module we found, since the umbrella header covers them all. 234a89c5ac4SDouglas Gregor for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 235a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I]] = Result; 236a89c5ac4SDouglas Gregor } 237a89c5ac4SDouglas Gregor 23859527666SDouglas Gregor Headers[File] = KnownHeader(Result, /*Excluded=*/false); 2391fb5c3a6SDouglas Gregor 2401fb5c3a6SDouglas Gregor // If a header corresponds to an unavailable module, don't report 2411fb5c3a6SDouglas Gregor // that it maps to anything. 2421fb5c3a6SDouglas Gregor if (!Result->isAvailable()) 2431fb5c3a6SDouglas Gregor return 0; 2441fb5c3a6SDouglas Gregor 245a89c5ac4SDouglas Gregor return Result; 246a89c5ac4SDouglas Gregor } 247a89c5ac4SDouglas Gregor 248a89c5ac4SDouglas Gregor SkippedDirs.push_back(Dir); 249a89c5ac4SDouglas Gregor 250b65dbfffSDouglas Gregor // Retrieve our parent path. 251b65dbfffSDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 252b65dbfffSDouglas Gregor if (DirName.empty()) 253b65dbfffSDouglas Gregor break; 254b65dbfffSDouglas Gregor 255b65dbfffSDouglas Gregor // Resolve the parent path to a directory entry. 256b65dbfffSDouglas Gregor Dir = SourceMgr->getFileManager().getDirectory(DirName); 257a89c5ac4SDouglas Gregor } while (Dir); 258b65dbfffSDouglas Gregor 259ab0c8a84SDouglas Gregor return 0; 260ab0c8a84SDouglas Gregor } 261ab0c8a84SDouglas Gregor 2621fb5c3a6SDouglas Gregor bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) { 26359527666SDouglas Gregor HeadersMap::iterator Known = Headers.find(Header); 2641fb5c3a6SDouglas Gregor if (Known != Headers.end()) 26559527666SDouglas Gregor return !Known->second.isAvailable(); 2661fb5c3a6SDouglas Gregor 2671fb5c3a6SDouglas Gregor const DirectoryEntry *Dir = Header->getDir(); 268f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 2691fb5c3a6SDouglas Gregor StringRef DirName = Dir->getName(); 2701fb5c3a6SDouglas Gregor 2711fb5c3a6SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 2721fb5c3a6SDouglas Gregor // an umbrella header. 2731fb5c3a6SDouglas Gregor do { 2741fb5c3a6SDouglas Gregor llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir 2751fb5c3a6SDouglas Gregor = UmbrellaDirs.find(Dir); 2761fb5c3a6SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 2771fb5c3a6SDouglas Gregor Module *Found = KnownDir->second; 2781fb5c3a6SDouglas Gregor if (!Found->isAvailable()) 2791fb5c3a6SDouglas Gregor return true; 2801fb5c3a6SDouglas Gregor 2811fb5c3a6SDouglas Gregor // Search up the module stack until we find a module with an umbrella 2821fb5c3a6SDouglas Gregor // directory. 2831fb5c3a6SDouglas Gregor Module *UmbrellaModule = Found; 2841fb5c3a6SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 2851fb5c3a6SDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 2861fb5c3a6SDouglas Gregor 2871fb5c3a6SDouglas Gregor if (UmbrellaModule->InferSubmodules) { 2881fb5c3a6SDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 2891fb5c3a6SDouglas Gregor // Find or create the module that corresponds to this directory name. 290056396aeSDouglas Gregor SmallString<32> NameBuf; 291056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 292056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 293056396aeSDouglas Gregor NameBuf); 2941fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 2951fb5c3a6SDouglas Gregor if (!Found) 2961fb5c3a6SDouglas Gregor return false; 2971fb5c3a6SDouglas Gregor if (!Found->isAvailable()) 2981fb5c3a6SDouglas Gregor return true; 2991fb5c3a6SDouglas Gregor } 3001fb5c3a6SDouglas Gregor 3011fb5c3a6SDouglas Gregor // Infer a submodule with the same name as this header file. 302056396aeSDouglas Gregor SmallString<32> NameBuf; 303056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 304056396aeSDouglas Gregor llvm::sys::path::stem(Header->getName()), 305056396aeSDouglas Gregor NameBuf); 3061fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 3071fb5c3a6SDouglas Gregor if (!Found) 3081fb5c3a6SDouglas Gregor return false; 3091fb5c3a6SDouglas Gregor } 3101fb5c3a6SDouglas Gregor 3111fb5c3a6SDouglas Gregor return !Found->isAvailable(); 3121fb5c3a6SDouglas Gregor } 3131fb5c3a6SDouglas Gregor 3141fb5c3a6SDouglas Gregor SkippedDirs.push_back(Dir); 3151fb5c3a6SDouglas Gregor 3161fb5c3a6SDouglas Gregor // Retrieve our parent path. 3171fb5c3a6SDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 3181fb5c3a6SDouglas Gregor if (DirName.empty()) 3191fb5c3a6SDouglas Gregor break; 3201fb5c3a6SDouglas Gregor 3211fb5c3a6SDouglas Gregor // Resolve the parent path to a directory entry. 3221fb5c3a6SDouglas Gregor Dir = SourceMgr->getFileManager().getDirectory(DirName); 3231fb5c3a6SDouglas Gregor } while (Dir); 3241fb5c3a6SDouglas Gregor 3251fb5c3a6SDouglas Gregor return false; 3261fb5c3a6SDouglas Gregor } 3271fb5c3a6SDouglas Gregor 328de3ef502SDouglas Gregor Module *ModuleMap::findModule(StringRef Name) { 32988bdfb0eSDouglas Gregor llvm::StringMap<Module *>::iterator Known = Modules.find(Name); 33088bdfb0eSDouglas Gregor if (Known != Modules.end()) 33188bdfb0eSDouglas Gregor return Known->getValue(); 33288bdfb0eSDouglas Gregor 33388bdfb0eSDouglas Gregor return 0; 33488bdfb0eSDouglas Gregor } 33588bdfb0eSDouglas Gregor 3362b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) { 3372b82c2a5SDouglas Gregor for(; Context; Context = Context->Parent) { 3382b82c2a5SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Context)) 3392b82c2a5SDouglas Gregor return Sub; 3402b82c2a5SDouglas Gregor } 3412b82c2a5SDouglas Gregor 3422b82c2a5SDouglas Gregor return findModule(Name); 3432b82c2a5SDouglas Gregor } 3442b82c2a5SDouglas Gregor 3452b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) { 3462b82c2a5SDouglas Gregor if (!Context) 3472b82c2a5SDouglas Gregor return findModule(Name); 3482b82c2a5SDouglas Gregor 349eb90e830SDouglas Gregor return Context->findSubmodule(Name); 3502b82c2a5SDouglas Gregor } 3512b82c2a5SDouglas Gregor 352de3ef502SDouglas Gregor std::pair<Module *, bool> 35369021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 35469021974SDouglas Gregor bool IsExplicit) { 35569021974SDouglas Gregor // Try to find an existing module with this name. 356eb90e830SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Parent)) 357eb90e830SDouglas Gregor return std::make_pair(Sub, false); 35869021974SDouglas Gregor 35969021974SDouglas Gregor // Create a new module with this name. 36069021974SDouglas Gregor Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework, 36169021974SDouglas Gregor IsExplicit); 362eb90e830SDouglas Gregor if (!Parent) 36369021974SDouglas Gregor Modules[Name] = Result; 36469021974SDouglas Gregor return std::make_pair(Result, true); 36569021974SDouglas Gregor } 36669021974SDouglas Gregor 3679194a91dSDouglas Gregor bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir, 3689194a91dSDouglas Gregor StringRef Name, bool &IsSystem) { 3699194a91dSDouglas Gregor // Check whether we have already looked into the parent directory 3709194a91dSDouglas Gregor // for a module map. 3719194a91dSDouglas Gregor llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::iterator 3729194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 3739194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) 3749194a91dSDouglas Gregor return false; 3759194a91dSDouglas Gregor 3769194a91dSDouglas Gregor if (!inferred->second.InferModules) 3779194a91dSDouglas Gregor return false; 3789194a91dSDouglas Gregor 3799194a91dSDouglas Gregor // We're allowed to infer for this directory, but make sure it's okay 3809194a91dSDouglas Gregor // to infer this particular module. 3819194a91dSDouglas Gregor bool canInfer = std::find(inferred->second.ExcludedModules.begin(), 3829194a91dSDouglas Gregor inferred->second.ExcludedModules.end(), 3839194a91dSDouglas Gregor Name) == inferred->second.ExcludedModules.end(); 3849194a91dSDouglas Gregor 3859194a91dSDouglas Gregor if (canInfer && inferred->second.InferSystemModules) 3869194a91dSDouglas Gregor IsSystem = true; 3879194a91dSDouglas Gregor 3889194a91dSDouglas Gregor return canInfer; 3899194a91dSDouglas Gregor } 3909194a91dSDouglas Gregor 39111dfe6feSDouglas Gregor /// \brief For a framework module, infer the framework against which we 39211dfe6feSDouglas Gregor /// should link. 39311dfe6feSDouglas Gregor static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir, 39411dfe6feSDouglas Gregor FileManager &FileMgr) { 39511dfe6feSDouglas Gregor assert(Mod->IsFramework && "Can only infer linking for framework modules"); 39611dfe6feSDouglas Gregor assert(!Mod->isSubFramework() && 39711dfe6feSDouglas Gregor "Can only infer linking for top-level frameworks"); 39811dfe6feSDouglas Gregor 39911dfe6feSDouglas Gregor SmallString<128> LibName; 40011dfe6feSDouglas Gregor LibName += FrameworkDir->getName(); 40111dfe6feSDouglas Gregor llvm::sys::path::append(LibName, Mod->Name); 40211dfe6feSDouglas Gregor if (FileMgr.getFile(LibName)) { 40311dfe6feSDouglas Gregor Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name, 40411dfe6feSDouglas Gregor /*IsFramework=*/true)); 40511dfe6feSDouglas Gregor } 40611dfe6feSDouglas Gregor } 40711dfe6feSDouglas Gregor 408de3ef502SDouglas Gregor Module * 40956c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName, 410e89dbc1dSDouglas Gregor const DirectoryEntry *FrameworkDir, 411a686e1b0SDouglas Gregor bool IsSystem, 412e89dbc1dSDouglas Gregor Module *Parent) { 41356c64013SDouglas Gregor // Check whether we've already found this module. 414e89dbc1dSDouglas Gregor if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 415e89dbc1dSDouglas Gregor return Mod; 416e89dbc1dSDouglas Gregor 417e89dbc1dSDouglas Gregor FileManager &FileMgr = SourceMgr->getFileManager(); 41856c64013SDouglas Gregor 4199194a91dSDouglas Gregor // If the framework has a parent path from which we're allowed to infer 4209194a91dSDouglas Gregor // a framework module, do so. 4219194a91dSDouglas Gregor if (!Parent) { 4224ddf2221SDouglas Gregor // Determine whether we're allowed to infer a module map. 4234ddf2221SDouglas Gregor StringRef FrameworkDirName = FrameworkDir->getName(); 4244ddf2221SDouglas Gregor #ifdef LLVM_ON_UNIX 4254ddf2221SDouglas Gregor // Note: as an egregious but useful hack we use the real path here, because 4264ddf2221SDouglas Gregor // we might be looking at an embedded framework that symlinks out to a 4274ddf2221SDouglas Gregor // top-level framework, and we need to infer as if we were naming the 4284ddf2221SDouglas Gregor // top-level framework. 4294ddf2221SDouglas Gregor char RealFrameworkDirName[PATH_MAX]; 4304ddf2221SDouglas Gregor if (realpath(FrameworkDir->getName(), RealFrameworkDirName)) 4314ddf2221SDouglas Gregor FrameworkDirName = RealFrameworkDirName; 4324ddf2221SDouglas Gregor #endif 4334ddf2221SDouglas Gregor 4349194a91dSDouglas Gregor bool canInfer = false; 4354ddf2221SDouglas Gregor if (llvm::sys::path::has_parent_path(FrameworkDirName)) { 4369194a91dSDouglas Gregor // Figure out the parent path. 4374ddf2221SDouglas Gregor StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); 4389194a91dSDouglas Gregor if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) { 4399194a91dSDouglas Gregor // Check whether we have already looked into the parent directory 4409194a91dSDouglas Gregor // for a module map. 4419194a91dSDouglas Gregor llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::iterator 4429194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 4439194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) { 4449194a91dSDouglas Gregor // We haven't looked here before. Load a module map, if there is 4459194a91dSDouglas Gregor // one. 4469194a91dSDouglas Gregor SmallString<128> ModMapPath = Parent; 4479194a91dSDouglas Gregor llvm::sys::path::append(ModMapPath, "module.map"); 4489194a91dSDouglas Gregor if (const FileEntry *ModMapFile = FileMgr.getFile(ModMapPath)) { 4499194a91dSDouglas Gregor parseModuleMapFile(ModMapFile); 4509194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 4519194a91dSDouglas Gregor } 4529194a91dSDouglas Gregor 4539194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) 4549194a91dSDouglas Gregor inferred = InferredDirectories.insert( 4559194a91dSDouglas Gregor std::make_pair(ParentDir, InferredDirectory())).first; 4569194a91dSDouglas Gregor } 4579194a91dSDouglas Gregor 4589194a91dSDouglas Gregor if (inferred->second.InferModules) { 4599194a91dSDouglas Gregor // We're allowed to infer for this directory, but make sure it's okay 4609194a91dSDouglas Gregor // to infer this particular module. 4614ddf2221SDouglas Gregor StringRef Name = llvm::sys::path::stem(FrameworkDirName); 4629194a91dSDouglas Gregor canInfer = std::find(inferred->second.ExcludedModules.begin(), 4639194a91dSDouglas Gregor inferred->second.ExcludedModules.end(), 4649194a91dSDouglas Gregor Name) == inferred->second.ExcludedModules.end(); 4659194a91dSDouglas Gregor 4669194a91dSDouglas Gregor if (inferred->second.InferSystemModules) 4679194a91dSDouglas Gregor IsSystem = true; 4689194a91dSDouglas Gregor } 4699194a91dSDouglas Gregor } 4709194a91dSDouglas Gregor } 4719194a91dSDouglas Gregor 4729194a91dSDouglas Gregor // If we're not allowed to infer a framework module, don't. 4739194a91dSDouglas Gregor if (!canInfer) 4749194a91dSDouglas Gregor return 0; 4759194a91dSDouglas Gregor } 4769194a91dSDouglas Gregor 4779194a91dSDouglas Gregor 47856c64013SDouglas Gregor // Look for an umbrella header. 4792c1dd271SDylan Noblesmith SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 48056c64013SDouglas Gregor llvm::sys::path::append(UmbrellaName, "Headers"); 48156c64013SDouglas Gregor llvm::sys::path::append(UmbrellaName, ModuleName + ".h"); 482e89dbc1dSDouglas Gregor const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 48356c64013SDouglas Gregor 48456c64013SDouglas Gregor // FIXME: If there's no umbrella header, we could probably scan the 48556c64013SDouglas Gregor // framework to load *everything*. But, it's not clear that this is a good 48656c64013SDouglas Gregor // idea. 48756c64013SDouglas Gregor if (!UmbrellaHeader) 48856c64013SDouglas Gregor return 0; 48956c64013SDouglas Gregor 490e89dbc1dSDouglas Gregor Module *Result = new Module(ModuleName, SourceLocation(), Parent, 491e89dbc1dSDouglas Gregor /*IsFramework=*/true, /*IsExplicit=*/false); 492a686e1b0SDouglas Gregor if (IsSystem) 493a686e1b0SDouglas Gregor Result->IsSystem = IsSystem; 494a686e1b0SDouglas Gregor 495eb90e830SDouglas Gregor if (!Parent) 496e89dbc1dSDouglas Gregor Modules[ModuleName] = Result; 497e89dbc1dSDouglas Gregor 498322f633cSDouglas Gregor // umbrella header "umbrella-header-name" 49973141fa9SDouglas Gregor Result->Umbrella = UmbrellaHeader; 50059527666SDouglas Gregor Headers[UmbrellaHeader] = KnownHeader(Result, /*Excluded=*/false); 5014dc71835SDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Result; 502d8bd7537SDouglas Gregor 503d8bd7537SDouglas Gregor // export * 504d8bd7537SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 505d8bd7537SDouglas Gregor 506a89c5ac4SDouglas Gregor // module * { export * } 507a89c5ac4SDouglas Gregor Result->InferSubmodules = true; 508a89c5ac4SDouglas Gregor Result->InferExportWildcard = true; 509a89c5ac4SDouglas Gregor 510e89dbc1dSDouglas Gregor // Look for subframeworks. 511e89dbc1dSDouglas Gregor llvm::error_code EC; 5122c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirName 513ddaa69cbSDouglas Gregor = StringRef(FrameworkDir->getName()); 514e89dbc1dSDouglas Gregor llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 5152c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirNameNative; 516ddaa69cbSDouglas Gregor llvm::sys::path::native(SubframeworksDirName.str(), 517ddaa69cbSDouglas Gregor SubframeworksDirNameNative); 518ddaa69cbSDouglas Gregor for (llvm::sys::fs::directory_iterator 519ddaa69cbSDouglas Gregor Dir(SubframeworksDirNameNative.str(), EC), DirEnd; 520e89dbc1dSDouglas Gregor Dir != DirEnd && !EC; Dir.increment(EC)) { 521e89dbc1dSDouglas Gregor if (!StringRef(Dir->path()).endswith(".framework")) 522e89dbc1dSDouglas Gregor continue; 523f2161a70SDouglas Gregor 524e89dbc1dSDouglas Gregor if (const DirectoryEntry *SubframeworkDir 525e89dbc1dSDouglas Gregor = FileMgr.getDirectory(Dir->path())) { 52607c22b78SDouglas Gregor // Note: as an egregious but useful hack, we use the real path here and 52707c22b78SDouglas Gregor // check whether it is actually a subdirectory of the parent directory. 52807c22b78SDouglas Gregor // This will not be the case if the 'subframework' is actually a symlink 52907c22b78SDouglas Gregor // out to a top-level framework. 53007c22b78SDouglas Gregor #ifdef LLVM_ON_UNIX 53107c22b78SDouglas Gregor char RealSubframeworkDirName[PATH_MAX]; 53207c22b78SDouglas Gregor if (realpath(Dir->path().c_str(), RealSubframeworkDirName)) { 53307c22b78SDouglas Gregor StringRef SubframeworkDirName = RealSubframeworkDirName; 53407c22b78SDouglas Gregor 53507c22b78SDouglas Gregor bool FoundParent = false; 53607c22b78SDouglas Gregor do { 53707c22b78SDouglas Gregor // Get the parent directory name. 53807c22b78SDouglas Gregor SubframeworkDirName 53907c22b78SDouglas Gregor = llvm::sys::path::parent_path(SubframeworkDirName); 54007c22b78SDouglas Gregor if (SubframeworkDirName.empty()) 54107c22b78SDouglas Gregor break; 54207c22b78SDouglas Gregor 54307c22b78SDouglas Gregor if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { 54407c22b78SDouglas Gregor FoundParent = true; 54507c22b78SDouglas Gregor break; 54607c22b78SDouglas Gregor } 54707c22b78SDouglas Gregor } while (true); 54807c22b78SDouglas Gregor 54907c22b78SDouglas Gregor if (!FoundParent) 55007c22b78SDouglas Gregor continue; 55107c22b78SDouglas Gregor } 55207c22b78SDouglas Gregor #endif 55307c22b78SDouglas Gregor 554e89dbc1dSDouglas Gregor // FIXME: Do we want to warn about subframeworks without umbrella headers? 555056396aeSDouglas Gregor SmallString<32> NameBuf; 556056396aeSDouglas Gregor inferFrameworkModule(sanitizeFilenameAsIdentifier( 557056396aeSDouglas Gregor llvm::sys::path::stem(Dir->path()), NameBuf), 558056396aeSDouglas Gregor SubframeworkDir, IsSystem, Result); 559e89dbc1dSDouglas Gregor } 560e89dbc1dSDouglas Gregor } 561e89dbc1dSDouglas Gregor 56211dfe6feSDouglas Gregor // If the module is a top-level framework, automatically link against the 56311dfe6feSDouglas Gregor // framework. 56411dfe6feSDouglas Gregor if (!Result->isSubFramework()) { 56511dfe6feSDouglas Gregor inferFrameworkLink(Result, FrameworkDir, FileMgr); 56611dfe6feSDouglas Gregor } 56711dfe6feSDouglas Gregor 56856c64013SDouglas Gregor return Result; 56956c64013SDouglas Gregor } 57056c64013SDouglas Gregor 571a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ 57259527666SDouglas Gregor Headers[UmbrellaHeader] = KnownHeader(Mod, /*Excluded=*/false); 57373141fa9SDouglas Gregor Mod->Umbrella = UmbrellaHeader; 5747033127bSDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; 575a89c5ac4SDouglas Gregor } 576a89c5ac4SDouglas Gregor 577524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) { 578524e33e1SDouglas Gregor Mod->Umbrella = UmbrellaDir; 579524e33e1SDouglas Gregor UmbrellaDirs[UmbrellaDir] = Mod; 580524e33e1SDouglas Gregor } 581524e33e1SDouglas Gregor 58259527666SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header, 58359527666SDouglas Gregor bool Excluded) { 58459527666SDouglas Gregor if (Excluded) 58559527666SDouglas Gregor Mod->ExcludedHeaders.push_back(Header); 58659527666SDouglas Gregor else 587a89c5ac4SDouglas Gregor Mod->Headers.push_back(Header); 58859527666SDouglas Gregor Headers[Header] = KnownHeader(Mod, Excluded); 589a89c5ac4SDouglas Gregor } 590a89c5ac4SDouglas Gregor 591514b636aSDouglas Gregor const FileEntry * 592de3ef502SDouglas Gregor ModuleMap::getContainingModuleMapFile(Module *Module) { 593514b636aSDouglas Gregor if (Module->DefinitionLoc.isInvalid() || !SourceMgr) 594514b636aSDouglas Gregor return 0; 595514b636aSDouglas Gregor 596514b636aSDouglas Gregor return SourceMgr->getFileEntryForID( 597514b636aSDouglas Gregor SourceMgr->getFileID(Module->DefinitionLoc)); 598514b636aSDouglas Gregor } 599514b636aSDouglas Gregor 600718292f2SDouglas Gregor void ModuleMap::dump() { 601718292f2SDouglas Gregor llvm::errs() << "Modules:"; 602718292f2SDouglas Gregor for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 603718292f2SDouglas Gregor MEnd = Modules.end(); 604718292f2SDouglas Gregor M != MEnd; ++M) 605d28d1b8dSDouglas Gregor M->getValue()->print(llvm::errs(), 2); 606718292f2SDouglas Gregor 607718292f2SDouglas Gregor llvm::errs() << "Headers:"; 60859527666SDouglas Gregor for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 609718292f2SDouglas Gregor H != HEnd; ++H) { 610718292f2SDouglas Gregor llvm::errs() << " \"" << H->first->getName() << "\" -> " 61159527666SDouglas Gregor << H->second.getModule()->getFullModuleName() << "\n"; 612718292f2SDouglas Gregor } 613718292f2SDouglas Gregor } 614718292f2SDouglas Gregor 6152b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 6162b82c2a5SDouglas Gregor bool HadError = false; 6172b82c2a5SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { 6182b82c2a5SDouglas Gregor Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 6192b82c2a5SDouglas Gregor Complain); 620f5eedd05SDouglas Gregor if (Export.getPointer() || Export.getInt()) 6212b82c2a5SDouglas Gregor Mod->Exports.push_back(Export); 6222b82c2a5SDouglas Gregor else 6232b82c2a5SDouglas Gregor HadError = true; 6242b82c2a5SDouglas Gregor } 6252b82c2a5SDouglas Gregor Mod->UnresolvedExports.clear(); 6262b82c2a5SDouglas Gregor return HadError; 6272b82c2a5SDouglas Gregor } 6282b82c2a5SDouglas Gregor 6290093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 6300093b3c7SDouglas Gregor if (Loc.isInvalid()) 6310093b3c7SDouglas Gregor return 0; 6320093b3c7SDouglas Gregor 6330093b3c7SDouglas Gregor // Use the expansion location to determine which module we're in. 6340093b3c7SDouglas Gregor FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 6350093b3c7SDouglas Gregor if (!ExpansionLoc.isFileID()) 6360093b3c7SDouglas Gregor return 0; 6370093b3c7SDouglas Gregor 6380093b3c7SDouglas Gregor 6390093b3c7SDouglas Gregor const SourceManager &SrcMgr = Loc.getManager(); 6400093b3c7SDouglas Gregor FileID ExpansionFileID = ExpansionLoc.getFileID(); 641224d8a74SDouglas Gregor 642224d8a74SDouglas Gregor while (const FileEntry *ExpansionFile 643224d8a74SDouglas Gregor = SrcMgr.getFileEntryForID(ExpansionFileID)) { 644224d8a74SDouglas Gregor // Find the module that owns this header (if any). 645224d8a74SDouglas Gregor if (Module *Mod = findModuleForHeader(ExpansionFile)) 646224d8a74SDouglas Gregor return Mod; 647224d8a74SDouglas Gregor 648224d8a74SDouglas Gregor // No module owns this header, so look up the inclusion chain to see if 649224d8a74SDouglas Gregor // any included header has an associated module. 650224d8a74SDouglas Gregor SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); 651224d8a74SDouglas Gregor if (IncludeLoc.isInvalid()) 6520093b3c7SDouglas Gregor return 0; 6530093b3c7SDouglas Gregor 654224d8a74SDouglas Gregor ExpansionFileID = SrcMgr.getFileID(IncludeLoc); 655224d8a74SDouglas Gregor } 656224d8a74SDouglas Gregor 657224d8a74SDouglas Gregor return 0; 6580093b3c7SDouglas Gregor } 6590093b3c7SDouglas Gregor 660718292f2SDouglas Gregor //----------------------------------------------------------------------------// 661718292f2SDouglas Gregor // Module map file parser 662718292f2SDouglas Gregor //----------------------------------------------------------------------------// 663718292f2SDouglas Gregor 664718292f2SDouglas Gregor namespace clang { 665718292f2SDouglas Gregor /// \brief A token in a module map file. 666718292f2SDouglas Gregor struct MMToken { 667718292f2SDouglas Gregor enum TokenKind { 6681fb5c3a6SDouglas Gregor Comma, 669718292f2SDouglas Gregor EndOfFile, 670718292f2SDouglas Gregor HeaderKeyword, 671718292f2SDouglas Gregor Identifier, 67259527666SDouglas Gregor ExcludeKeyword, 673718292f2SDouglas Gregor ExplicitKeyword, 6742b82c2a5SDouglas Gregor ExportKeyword, 675755b2055SDouglas Gregor FrameworkKeyword, 6766ddfca91SDouglas Gregor LinkKeyword, 677718292f2SDouglas Gregor ModuleKeyword, 6782b82c2a5SDouglas Gregor Period, 679718292f2SDouglas Gregor UmbrellaKeyword, 6801fb5c3a6SDouglas Gregor RequiresKeyword, 6812b82c2a5SDouglas Gregor Star, 682718292f2SDouglas Gregor StringLiteral, 683718292f2SDouglas Gregor LBrace, 684a686e1b0SDouglas Gregor RBrace, 685a686e1b0SDouglas Gregor LSquare, 686a686e1b0SDouglas Gregor RSquare 687718292f2SDouglas Gregor } Kind; 688718292f2SDouglas Gregor 689718292f2SDouglas Gregor unsigned Location; 690718292f2SDouglas Gregor unsigned StringLength; 691718292f2SDouglas Gregor const char *StringData; 692718292f2SDouglas Gregor 693718292f2SDouglas Gregor void clear() { 694718292f2SDouglas Gregor Kind = EndOfFile; 695718292f2SDouglas Gregor Location = 0; 696718292f2SDouglas Gregor StringLength = 0; 697718292f2SDouglas Gregor StringData = 0; 698718292f2SDouglas Gregor } 699718292f2SDouglas Gregor 700718292f2SDouglas Gregor bool is(TokenKind K) const { return Kind == K; } 701718292f2SDouglas Gregor 702718292f2SDouglas Gregor SourceLocation getLocation() const { 703718292f2SDouglas Gregor return SourceLocation::getFromRawEncoding(Location); 704718292f2SDouglas Gregor } 705718292f2SDouglas Gregor 706718292f2SDouglas Gregor StringRef getString() const { 707718292f2SDouglas Gregor return StringRef(StringData, StringLength); 708718292f2SDouglas Gregor } 709718292f2SDouglas Gregor }; 710718292f2SDouglas Gregor 7119194a91dSDouglas Gregor /// \brief The set of attributes that can be attached to a module. 7124442605fSBill Wendling struct Attributes { 7134442605fSBill Wendling Attributes() : IsSystem() { } 7149194a91dSDouglas Gregor 7159194a91dSDouglas Gregor /// \brief Whether this is a system module. 7169194a91dSDouglas Gregor unsigned IsSystem : 1; 7179194a91dSDouglas Gregor }; 7189194a91dSDouglas Gregor 7199194a91dSDouglas Gregor 720718292f2SDouglas Gregor class ModuleMapParser { 721718292f2SDouglas Gregor Lexer &L; 722718292f2SDouglas Gregor SourceManager &SourceMgr; 723bc10b9fbSDouglas Gregor 724bc10b9fbSDouglas Gregor /// \brief Default target information, used only for string literal 725bc10b9fbSDouglas Gregor /// parsing. 726bc10b9fbSDouglas Gregor const TargetInfo *Target; 727bc10b9fbSDouglas Gregor 728718292f2SDouglas Gregor DiagnosticsEngine &Diags; 729718292f2SDouglas Gregor ModuleMap ⤅ 730718292f2SDouglas Gregor 7315257fc63SDouglas Gregor /// \brief The directory that this module map resides in. 7325257fc63SDouglas Gregor const DirectoryEntry *Directory; 7335257fc63SDouglas Gregor 7343ec6663bSDouglas Gregor /// \brief The directory containing Clang-supplied headers. 7353ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir; 7363ec6663bSDouglas Gregor 737718292f2SDouglas Gregor /// \brief Whether an error occurred. 738718292f2SDouglas Gregor bool HadError; 739718292f2SDouglas Gregor 740718292f2SDouglas Gregor /// \brief Stores string data for the various string literals referenced 741718292f2SDouglas Gregor /// during parsing. 742718292f2SDouglas Gregor llvm::BumpPtrAllocator StringData; 743718292f2SDouglas Gregor 744718292f2SDouglas Gregor /// \brief The current token. 745718292f2SDouglas Gregor MMToken Tok; 746718292f2SDouglas Gregor 747718292f2SDouglas Gregor /// \brief The active module. 748de3ef502SDouglas Gregor Module *ActiveModule; 749718292f2SDouglas Gregor 750718292f2SDouglas Gregor /// \brief Consume the current token and return its location. 751718292f2SDouglas Gregor SourceLocation consumeToken(); 752718292f2SDouglas Gregor 753718292f2SDouglas Gregor /// \brief Skip tokens until we reach the a token with the given kind 754718292f2SDouglas Gregor /// (or the end of the file). 755718292f2SDouglas Gregor void skipUntil(MMToken::TokenKind K); 756718292f2SDouglas Gregor 757f857950dSDmitri Gribenko typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; 758e7ab3669SDouglas Gregor bool parseModuleId(ModuleId &Id); 759718292f2SDouglas Gregor void parseModuleDecl(); 7601fb5c3a6SDouglas Gregor void parseRequiresDecl(); 76159527666SDouglas Gregor void parseHeaderDecl(SourceLocation UmbrellaLoc, SourceLocation ExcludeLoc); 762524e33e1SDouglas Gregor void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 7632b82c2a5SDouglas Gregor void parseExportDecl(); 7646ddfca91SDouglas Gregor void parseLinkDecl(); 7659194a91dSDouglas Gregor void parseInferredModuleDecl(bool Framework, bool Explicit); 7664442605fSBill Wendling bool parseOptionalAttributes(Attributes &Attrs); 767718292f2SDouglas Gregor 7687033127bSDouglas Gregor const DirectoryEntry *getOverriddenHeaderSearchDir(); 7697033127bSDouglas Gregor 770718292f2SDouglas Gregor public: 771718292f2SDouglas Gregor explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 772bc10b9fbSDouglas Gregor const TargetInfo *Target, 773718292f2SDouglas Gregor DiagnosticsEngine &Diags, 7745257fc63SDouglas Gregor ModuleMap &Map, 7753ec6663bSDouglas Gregor const DirectoryEntry *Directory, 7763ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir) 777bc10b9fbSDouglas Gregor : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 7783ec6663bSDouglas Gregor Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir), 7793ec6663bSDouglas Gregor HadError(false), ActiveModule(0) 780718292f2SDouglas Gregor { 781718292f2SDouglas Gregor Tok.clear(); 782718292f2SDouglas Gregor consumeToken(); 783718292f2SDouglas Gregor } 784718292f2SDouglas Gregor 785718292f2SDouglas Gregor bool parseModuleMapFile(); 786718292f2SDouglas Gregor }; 787718292f2SDouglas Gregor } 788718292f2SDouglas Gregor 789718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() { 790718292f2SDouglas Gregor retry: 791718292f2SDouglas Gregor SourceLocation Result = Tok.getLocation(); 792718292f2SDouglas Gregor Tok.clear(); 793718292f2SDouglas Gregor 794718292f2SDouglas Gregor Token LToken; 795718292f2SDouglas Gregor L.LexFromRawLexer(LToken); 796718292f2SDouglas Gregor Tok.Location = LToken.getLocation().getRawEncoding(); 797718292f2SDouglas Gregor switch (LToken.getKind()) { 798718292f2SDouglas Gregor case tok::raw_identifier: 799718292f2SDouglas Gregor Tok.StringData = LToken.getRawIdentifierData(); 800718292f2SDouglas Gregor Tok.StringLength = LToken.getLength(); 801718292f2SDouglas Gregor Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString()) 802718292f2SDouglas Gregor .Case("header", MMToken::HeaderKeyword) 80359527666SDouglas Gregor .Case("exclude", MMToken::ExcludeKeyword) 804718292f2SDouglas Gregor .Case("explicit", MMToken::ExplicitKeyword) 8052b82c2a5SDouglas Gregor .Case("export", MMToken::ExportKeyword) 806755b2055SDouglas Gregor .Case("framework", MMToken::FrameworkKeyword) 8076ddfca91SDouglas Gregor .Case("link", MMToken::LinkKeyword) 808718292f2SDouglas Gregor .Case("module", MMToken::ModuleKeyword) 8091fb5c3a6SDouglas Gregor .Case("requires", MMToken::RequiresKeyword) 810718292f2SDouglas Gregor .Case("umbrella", MMToken::UmbrellaKeyword) 811718292f2SDouglas Gregor .Default(MMToken::Identifier); 812718292f2SDouglas Gregor break; 813718292f2SDouglas Gregor 8141fb5c3a6SDouglas Gregor case tok::comma: 8151fb5c3a6SDouglas Gregor Tok.Kind = MMToken::Comma; 8161fb5c3a6SDouglas Gregor break; 8171fb5c3a6SDouglas Gregor 818718292f2SDouglas Gregor case tok::eof: 819718292f2SDouglas Gregor Tok.Kind = MMToken::EndOfFile; 820718292f2SDouglas Gregor break; 821718292f2SDouglas Gregor 822718292f2SDouglas Gregor case tok::l_brace: 823718292f2SDouglas Gregor Tok.Kind = MMToken::LBrace; 824718292f2SDouglas Gregor break; 825718292f2SDouglas Gregor 826a686e1b0SDouglas Gregor case tok::l_square: 827a686e1b0SDouglas Gregor Tok.Kind = MMToken::LSquare; 828a686e1b0SDouglas Gregor break; 829a686e1b0SDouglas Gregor 8302b82c2a5SDouglas Gregor case tok::period: 8312b82c2a5SDouglas Gregor Tok.Kind = MMToken::Period; 8322b82c2a5SDouglas Gregor break; 8332b82c2a5SDouglas Gregor 834718292f2SDouglas Gregor case tok::r_brace: 835718292f2SDouglas Gregor Tok.Kind = MMToken::RBrace; 836718292f2SDouglas Gregor break; 837718292f2SDouglas Gregor 838a686e1b0SDouglas Gregor case tok::r_square: 839a686e1b0SDouglas Gregor Tok.Kind = MMToken::RSquare; 840a686e1b0SDouglas Gregor break; 841a686e1b0SDouglas Gregor 8422b82c2a5SDouglas Gregor case tok::star: 8432b82c2a5SDouglas Gregor Tok.Kind = MMToken::Star; 8442b82c2a5SDouglas Gregor break; 8452b82c2a5SDouglas Gregor 846718292f2SDouglas Gregor case tok::string_literal: { 847d67aea28SRichard Smith if (LToken.hasUDSuffix()) { 848d67aea28SRichard Smith Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 849d67aea28SRichard Smith HadError = true; 850d67aea28SRichard Smith goto retry; 851d67aea28SRichard Smith } 852d67aea28SRichard Smith 853718292f2SDouglas Gregor // Parse the string literal. 854718292f2SDouglas Gregor LangOptions LangOpts; 855718292f2SDouglas Gregor StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target); 856718292f2SDouglas Gregor if (StringLiteral.hadError) 857718292f2SDouglas Gregor goto retry; 858718292f2SDouglas Gregor 859718292f2SDouglas Gregor // Copy the string literal into our string data allocator. 860718292f2SDouglas Gregor unsigned Length = StringLiteral.GetStringLength(); 861718292f2SDouglas Gregor char *Saved = StringData.Allocate<char>(Length + 1); 862718292f2SDouglas Gregor memcpy(Saved, StringLiteral.GetString().data(), Length); 863718292f2SDouglas Gregor Saved[Length] = 0; 864718292f2SDouglas Gregor 865718292f2SDouglas Gregor // Form the token. 866718292f2SDouglas Gregor Tok.Kind = MMToken::StringLiteral; 867718292f2SDouglas Gregor Tok.StringData = Saved; 868718292f2SDouglas Gregor Tok.StringLength = Length; 869718292f2SDouglas Gregor break; 870718292f2SDouglas Gregor } 871718292f2SDouglas Gregor 872718292f2SDouglas Gregor case tok::comment: 873718292f2SDouglas Gregor goto retry; 874718292f2SDouglas Gregor 875718292f2SDouglas Gregor default: 876718292f2SDouglas Gregor Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 877718292f2SDouglas Gregor HadError = true; 878718292f2SDouglas Gregor goto retry; 879718292f2SDouglas Gregor } 880718292f2SDouglas Gregor 881718292f2SDouglas Gregor return Result; 882718292f2SDouglas Gregor } 883718292f2SDouglas Gregor 884718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 885718292f2SDouglas Gregor unsigned braceDepth = 0; 886a686e1b0SDouglas Gregor unsigned squareDepth = 0; 887718292f2SDouglas Gregor do { 888718292f2SDouglas Gregor switch (Tok.Kind) { 889718292f2SDouglas Gregor case MMToken::EndOfFile: 890718292f2SDouglas Gregor return; 891718292f2SDouglas Gregor 892718292f2SDouglas Gregor case MMToken::LBrace: 893a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 894718292f2SDouglas Gregor return; 895718292f2SDouglas Gregor 896718292f2SDouglas Gregor ++braceDepth; 897718292f2SDouglas Gregor break; 898718292f2SDouglas Gregor 899a686e1b0SDouglas Gregor case MMToken::LSquare: 900a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 901a686e1b0SDouglas Gregor return; 902a686e1b0SDouglas Gregor 903a686e1b0SDouglas Gregor ++squareDepth; 904a686e1b0SDouglas Gregor break; 905a686e1b0SDouglas Gregor 906718292f2SDouglas Gregor case MMToken::RBrace: 907718292f2SDouglas Gregor if (braceDepth > 0) 908718292f2SDouglas Gregor --braceDepth; 909718292f2SDouglas Gregor else if (Tok.is(K)) 910718292f2SDouglas Gregor return; 911718292f2SDouglas Gregor break; 912718292f2SDouglas Gregor 913a686e1b0SDouglas Gregor case MMToken::RSquare: 914a686e1b0SDouglas Gregor if (squareDepth > 0) 915a686e1b0SDouglas Gregor --squareDepth; 916a686e1b0SDouglas Gregor else if (Tok.is(K)) 917a686e1b0SDouglas Gregor return; 918a686e1b0SDouglas Gregor break; 919a686e1b0SDouglas Gregor 920718292f2SDouglas Gregor default: 921a686e1b0SDouglas Gregor if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 922718292f2SDouglas Gregor return; 923718292f2SDouglas Gregor break; 924718292f2SDouglas Gregor } 925718292f2SDouglas Gregor 926718292f2SDouglas Gregor consumeToken(); 927718292f2SDouglas Gregor } while (true); 928718292f2SDouglas Gregor } 929718292f2SDouglas Gregor 930e7ab3669SDouglas Gregor /// \brief Parse a module-id. 931e7ab3669SDouglas Gregor /// 932e7ab3669SDouglas Gregor /// module-id: 933e7ab3669SDouglas Gregor /// identifier 934e7ab3669SDouglas Gregor /// identifier '.' module-id 935e7ab3669SDouglas Gregor /// 936e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise. 937e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) { 938e7ab3669SDouglas Gregor Id.clear(); 939e7ab3669SDouglas Gregor do { 940e7ab3669SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 941e7ab3669SDouglas Gregor Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 942e7ab3669SDouglas Gregor consumeToken(); 943e7ab3669SDouglas Gregor } else { 944e7ab3669SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 945e7ab3669SDouglas Gregor return true; 946e7ab3669SDouglas Gregor } 947e7ab3669SDouglas Gregor 948e7ab3669SDouglas Gregor if (!Tok.is(MMToken::Period)) 949e7ab3669SDouglas Gregor break; 950e7ab3669SDouglas Gregor 951e7ab3669SDouglas Gregor consumeToken(); 952e7ab3669SDouglas Gregor } while (true); 953e7ab3669SDouglas Gregor 954e7ab3669SDouglas Gregor return false; 955e7ab3669SDouglas Gregor } 956e7ab3669SDouglas Gregor 957a686e1b0SDouglas Gregor namespace { 958a686e1b0SDouglas Gregor /// \brief Enumerates the known attributes. 959a686e1b0SDouglas Gregor enum AttributeKind { 960a686e1b0SDouglas Gregor /// \brief An unknown attribute. 961a686e1b0SDouglas Gregor AT_unknown, 962a686e1b0SDouglas Gregor /// \brief The 'system' attribute. 963a686e1b0SDouglas Gregor AT_system 964a686e1b0SDouglas Gregor }; 965a686e1b0SDouglas Gregor } 966a686e1b0SDouglas Gregor 967718292f2SDouglas Gregor /// \brief Parse a module declaration. 968718292f2SDouglas Gregor /// 969718292f2SDouglas Gregor /// module-declaration: 970a686e1b0SDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 971a686e1b0SDouglas Gregor /// { module-member* } 972a686e1b0SDouglas Gregor /// 973718292f2SDouglas Gregor /// module-member: 9741fb5c3a6SDouglas Gregor /// requires-declaration 975718292f2SDouglas Gregor /// header-declaration 976e7ab3669SDouglas Gregor /// submodule-declaration 9772b82c2a5SDouglas Gregor /// export-declaration 9786ddfca91SDouglas Gregor /// link-declaration 97973441091SDouglas Gregor /// 98073441091SDouglas Gregor /// submodule-declaration: 98173441091SDouglas Gregor /// module-declaration 98273441091SDouglas Gregor /// inferred-submodule-declaration 983718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() { 984755b2055SDouglas Gregor assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 985755b2055SDouglas Gregor Tok.is(MMToken::FrameworkKeyword)); 986f2161a70SDouglas Gregor // Parse 'explicit' or 'framework' keyword, if present. 987e7ab3669SDouglas Gregor SourceLocation ExplicitLoc; 988718292f2SDouglas Gregor bool Explicit = false; 989f2161a70SDouglas Gregor bool Framework = false; 990755b2055SDouglas Gregor 991f2161a70SDouglas Gregor // Parse 'explicit' keyword, if present. 992f2161a70SDouglas Gregor if (Tok.is(MMToken::ExplicitKeyword)) { 993e7ab3669SDouglas Gregor ExplicitLoc = consumeToken(); 994f2161a70SDouglas Gregor Explicit = true; 995f2161a70SDouglas Gregor } 996f2161a70SDouglas Gregor 997f2161a70SDouglas Gregor // Parse 'framework' keyword, if present. 998755b2055SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 999755b2055SDouglas Gregor consumeToken(); 1000755b2055SDouglas Gregor Framework = true; 1001755b2055SDouglas Gregor } 1002718292f2SDouglas Gregor 1003718292f2SDouglas Gregor // Parse 'module' keyword. 1004718292f2SDouglas Gregor if (!Tok.is(MMToken::ModuleKeyword)) { 1005d6343c99SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1006718292f2SDouglas Gregor consumeToken(); 1007718292f2SDouglas Gregor HadError = true; 1008718292f2SDouglas Gregor return; 1009718292f2SDouglas Gregor } 1010718292f2SDouglas Gregor consumeToken(); // 'module' keyword 1011718292f2SDouglas Gregor 101273441091SDouglas Gregor // If we have a wildcard for the module name, this is an inferred submodule. 101373441091SDouglas Gregor // Parse it. 101473441091SDouglas Gregor if (Tok.is(MMToken::Star)) 10159194a91dSDouglas Gregor return parseInferredModuleDecl(Framework, Explicit); 101673441091SDouglas Gregor 1017718292f2SDouglas Gregor // Parse the module name. 1018e7ab3669SDouglas Gregor ModuleId Id; 1019e7ab3669SDouglas Gregor if (parseModuleId(Id)) { 1020718292f2SDouglas Gregor HadError = true; 1021718292f2SDouglas Gregor return; 1022718292f2SDouglas Gregor } 1023e7ab3669SDouglas Gregor 1024e7ab3669SDouglas Gregor if (ActiveModule) { 1025e7ab3669SDouglas Gregor if (Id.size() > 1) { 1026e7ab3669SDouglas Gregor Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 1027e7ab3669SDouglas Gregor << SourceRange(Id.front().second, Id.back().second); 1028e7ab3669SDouglas Gregor 1029e7ab3669SDouglas Gregor HadError = true; 1030e7ab3669SDouglas Gregor return; 1031e7ab3669SDouglas Gregor } 1032e7ab3669SDouglas Gregor } else if (Id.size() == 1 && Explicit) { 1033e7ab3669SDouglas Gregor // Top-level modules can't be explicit. 1034e7ab3669SDouglas Gregor Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 1035e7ab3669SDouglas Gregor Explicit = false; 1036e7ab3669SDouglas Gregor ExplicitLoc = SourceLocation(); 1037e7ab3669SDouglas Gregor HadError = true; 1038e7ab3669SDouglas Gregor } 1039e7ab3669SDouglas Gregor 1040e7ab3669SDouglas Gregor Module *PreviousActiveModule = ActiveModule; 1041e7ab3669SDouglas Gregor if (Id.size() > 1) { 1042e7ab3669SDouglas Gregor // This module map defines a submodule. Go find the module of which it 1043e7ab3669SDouglas Gregor // is a submodule. 1044e7ab3669SDouglas Gregor ActiveModule = 0; 1045e7ab3669SDouglas Gregor for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 1046e7ab3669SDouglas Gregor if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 1047e7ab3669SDouglas Gregor ActiveModule = Next; 1048e7ab3669SDouglas Gregor continue; 1049e7ab3669SDouglas Gregor } 1050e7ab3669SDouglas Gregor 1051e7ab3669SDouglas Gregor if (ActiveModule) { 1052e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 1053e7ab3669SDouglas Gregor << Id[I].first << ActiveModule->getTopLevelModule(); 1054e7ab3669SDouglas Gregor } else { 1055e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 1056e7ab3669SDouglas Gregor } 1057e7ab3669SDouglas Gregor HadError = true; 1058e7ab3669SDouglas Gregor return; 1059e7ab3669SDouglas Gregor } 1060e7ab3669SDouglas Gregor } 1061e7ab3669SDouglas Gregor 1062e7ab3669SDouglas Gregor StringRef ModuleName = Id.back().first; 1063e7ab3669SDouglas Gregor SourceLocation ModuleNameLoc = Id.back().second; 1064718292f2SDouglas Gregor 1065a686e1b0SDouglas Gregor // Parse the optional attribute list. 10664442605fSBill Wendling Attributes Attrs; 10679194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 1068a686e1b0SDouglas Gregor 1069718292f2SDouglas Gregor // Parse the opening brace. 1070718292f2SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 1071718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 1072718292f2SDouglas Gregor << ModuleName; 1073718292f2SDouglas Gregor HadError = true; 1074718292f2SDouglas Gregor return; 1075718292f2SDouglas Gregor } 1076718292f2SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 1077718292f2SDouglas Gregor 1078718292f2SDouglas Gregor // Determine whether this (sub)module has already been defined. 1079eb90e830SDouglas Gregor if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 1080fcc54a3bSDouglas Gregor if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { 1081fcc54a3bSDouglas Gregor // Skip the module definition. 1082fcc54a3bSDouglas Gregor skipUntil(MMToken::RBrace); 1083fcc54a3bSDouglas Gregor if (Tok.is(MMToken::RBrace)) 1084fcc54a3bSDouglas Gregor consumeToken(); 1085fcc54a3bSDouglas Gregor else { 1086fcc54a3bSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1087fcc54a3bSDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1088fcc54a3bSDouglas Gregor HadError = true; 1089fcc54a3bSDouglas Gregor } 1090fcc54a3bSDouglas Gregor return; 1091fcc54a3bSDouglas Gregor } 1092fcc54a3bSDouglas Gregor 1093718292f2SDouglas Gregor Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 1094718292f2SDouglas Gregor << ModuleName; 1095eb90e830SDouglas Gregor Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 1096718292f2SDouglas Gregor 1097718292f2SDouglas Gregor // Skip the module definition. 1098718292f2SDouglas Gregor skipUntil(MMToken::RBrace); 1099718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1100718292f2SDouglas Gregor consumeToken(); 1101718292f2SDouglas Gregor 1102718292f2SDouglas Gregor HadError = true; 1103718292f2SDouglas Gregor return; 1104718292f2SDouglas Gregor } 1105718292f2SDouglas Gregor 1106718292f2SDouglas Gregor // Start defining this module. 1107eb90e830SDouglas Gregor ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, 1108eb90e830SDouglas Gregor Explicit).first; 1109eb90e830SDouglas Gregor ActiveModule->DefinitionLoc = ModuleNameLoc; 11109194a91dSDouglas Gregor if (Attrs.IsSystem) 1111a686e1b0SDouglas Gregor ActiveModule->IsSystem = true; 1112718292f2SDouglas Gregor 1113718292f2SDouglas Gregor bool Done = false; 1114718292f2SDouglas Gregor do { 1115718292f2SDouglas Gregor switch (Tok.Kind) { 1116718292f2SDouglas Gregor case MMToken::EndOfFile: 1117718292f2SDouglas Gregor case MMToken::RBrace: 1118718292f2SDouglas Gregor Done = true; 1119718292f2SDouglas Gregor break; 1120718292f2SDouglas Gregor 1121718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 1122f2161a70SDouglas Gregor case MMToken::FrameworkKeyword: 1123718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1124718292f2SDouglas Gregor parseModuleDecl(); 1125718292f2SDouglas Gregor break; 1126718292f2SDouglas Gregor 11272b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 11282b82c2a5SDouglas Gregor parseExportDecl(); 11292b82c2a5SDouglas Gregor break; 11302b82c2a5SDouglas Gregor 11311fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 11321fb5c3a6SDouglas Gregor parseRequiresDecl(); 11331fb5c3a6SDouglas Gregor break; 11341fb5c3a6SDouglas Gregor 1135524e33e1SDouglas Gregor case MMToken::UmbrellaKeyword: { 1136524e33e1SDouglas Gregor SourceLocation UmbrellaLoc = consumeToken(); 1137524e33e1SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) 113859527666SDouglas Gregor parseHeaderDecl(UmbrellaLoc, SourceLocation()); 1139524e33e1SDouglas Gregor else 1140524e33e1SDouglas Gregor parseUmbrellaDirDecl(UmbrellaLoc); 1141718292f2SDouglas Gregor break; 1142524e33e1SDouglas Gregor } 1143718292f2SDouglas Gregor 114459527666SDouglas Gregor case MMToken::ExcludeKeyword: { 114559527666SDouglas Gregor SourceLocation ExcludeLoc = consumeToken(); 114659527666SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) { 114759527666SDouglas Gregor parseHeaderDecl(SourceLocation(), ExcludeLoc); 114859527666SDouglas Gregor } else { 114959527666SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 115059527666SDouglas Gregor << "exclude"; 115159527666SDouglas Gregor } 115259527666SDouglas Gregor break; 115359527666SDouglas Gregor } 115459527666SDouglas Gregor 1155322f633cSDouglas Gregor case MMToken::HeaderKeyword: 115659527666SDouglas Gregor parseHeaderDecl(SourceLocation(), SourceLocation()); 1157718292f2SDouglas Gregor break; 1158718292f2SDouglas Gregor 11596ddfca91SDouglas Gregor case MMToken::LinkKeyword: 11606ddfca91SDouglas Gregor parseLinkDecl(); 11616ddfca91SDouglas Gregor break; 11626ddfca91SDouglas Gregor 1163718292f2SDouglas Gregor default: 1164718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 1165718292f2SDouglas Gregor consumeToken(); 1166718292f2SDouglas Gregor break; 1167718292f2SDouglas Gregor } 1168718292f2SDouglas Gregor } while (!Done); 1169718292f2SDouglas Gregor 1170718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1171718292f2SDouglas Gregor consumeToken(); 1172718292f2SDouglas Gregor else { 1173718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1174718292f2SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1175718292f2SDouglas Gregor HadError = true; 1176718292f2SDouglas Gregor } 1177718292f2SDouglas Gregor 117811dfe6feSDouglas Gregor // If the active module is a top-level framework, and there are no link 117911dfe6feSDouglas Gregor // libraries, automatically link against the framework. 118011dfe6feSDouglas Gregor if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() && 118111dfe6feSDouglas Gregor ActiveModule->LinkLibraries.empty()) { 118211dfe6feSDouglas Gregor inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager()); 118311dfe6feSDouglas Gregor } 118411dfe6feSDouglas Gregor 1185e7ab3669SDouglas Gregor // We're done parsing this module. Pop back to the previous module. 1186e7ab3669SDouglas Gregor ActiveModule = PreviousActiveModule; 1187718292f2SDouglas Gregor } 1188718292f2SDouglas Gregor 11891fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration. 11901fb5c3a6SDouglas Gregor /// 11911fb5c3a6SDouglas Gregor /// requires-declaration: 11921fb5c3a6SDouglas Gregor /// 'requires' feature-list 11931fb5c3a6SDouglas Gregor /// 11941fb5c3a6SDouglas Gregor /// feature-list: 11951fb5c3a6SDouglas Gregor /// identifier ',' feature-list 11961fb5c3a6SDouglas Gregor /// identifier 11971fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() { 11981fb5c3a6SDouglas Gregor assert(Tok.is(MMToken::RequiresKeyword)); 11991fb5c3a6SDouglas Gregor 12001fb5c3a6SDouglas Gregor // Parse 'requires' keyword. 12011fb5c3a6SDouglas Gregor consumeToken(); 12021fb5c3a6SDouglas Gregor 12031fb5c3a6SDouglas Gregor // Parse the feature-list. 12041fb5c3a6SDouglas Gregor do { 12051fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 12061fb5c3a6SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 12071fb5c3a6SDouglas Gregor HadError = true; 12081fb5c3a6SDouglas Gregor return; 12091fb5c3a6SDouglas Gregor } 12101fb5c3a6SDouglas Gregor 12111fb5c3a6SDouglas Gregor // Consume the feature name. 12121fb5c3a6SDouglas Gregor std::string Feature = Tok.getString(); 12131fb5c3a6SDouglas Gregor consumeToken(); 12141fb5c3a6SDouglas Gregor 12151fb5c3a6SDouglas Gregor // Add this feature. 121689929282SDouglas Gregor ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target); 12171fb5c3a6SDouglas Gregor 12181fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Comma)) 12191fb5c3a6SDouglas Gregor break; 12201fb5c3a6SDouglas Gregor 12211fb5c3a6SDouglas Gregor // Consume the comma. 12221fb5c3a6SDouglas Gregor consumeToken(); 12231fb5c3a6SDouglas Gregor } while (true); 12241fb5c3a6SDouglas Gregor } 12251fb5c3a6SDouglas Gregor 1226f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the 1227f2161a70SDouglas Gregor /// subframework in which the given module lives. 1228bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod, 1229f857950dSDmitri Gribenko SmallVectorImpl<char> &Path) { 1230f2161a70SDouglas Gregor // Collect the framework names from the given module to the top-level module. 1231f857950dSDmitri Gribenko SmallVector<StringRef, 2> Paths; 1232f2161a70SDouglas Gregor for (; Mod; Mod = Mod->Parent) { 1233f2161a70SDouglas Gregor if (Mod->IsFramework) 1234f2161a70SDouglas Gregor Paths.push_back(Mod->Name); 1235f2161a70SDouglas Gregor } 1236f2161a70SDouglas Gregor 1237f2161a70SDouglas Gregor if (Paths.empty()) 1238f2161a70SDouglas Gregor return; 1239f2161a70SDouglas Gregor 1240f2161a70SDouglas Gregor // Add Frameworks/Name.framework for each subframework. 1241f2161a70SDouglas Gregor for (unsigned I = Paths.size() - 1; I != 0; --I) { 1242f2161a70SDouglas Gregor llvm::sys::path::append(Path, "Frameworks"); 1243f2161a70SDouglas Gregor llvm::sys::path::append(Path, Paths[I-1] + ".framework"); 1244f2161a70SDouglas Gregor } 1245f2161a70SDouglas Gregor } 1246f2161a70SDouglas Gregor 12473ec6663bSDouglas Gregor /// \brief Determine whether the given file name is the name of a builtin 12483ec6663bSDouglas Gregor /// header, supplied by Clang to replace, override, or augment existing system 12493ec6663bSDouglas Gregor /// headers. 12503ec6663bSDouglas Gregor static bool isBuiltinHeader(StringRef FileName) { 12513ec6663bSDouglas Gregor return llvm::StringSwitch<bool>(FileName) 12523ec6663bSDouglas Gregor .Case("float.h", true) 12533ec6663bSDouglas Gregor .Case("iso646.h", true) 12543ec6663bSDouglas Gregor .Case("limits.h", true) 12553ec6663bSDouglas Gregor .Case("stdalign.h", true) 12563ec6663bSDouglas Gregor .Case("stdarg.h", true) 12573ec6663bSDouglas Gregor .Case("stdbool.h", true) 12583ec6663bSDouglas Gregor .Case("stddef.h", true) 12593ec6663bSDouglas Gregor .Case("stdint.h", true) 12603ec6663bSDouglas Gregor .Case("tgmath.h", true) 12613ec6663bSDouglas Gregor .Case("unwind.h", true) 12623ec6663bSDouglas Gregor .Default(false); 12633ec6663bSDouglas Gregor } 12643ec6663bSDouglas Gregor 1265718292f2SDouglas Gregor /// \brief Parse a header declaration. 1266718292f2SDouglas Gregor /// 1267718292f2SDouglas Gregor /// header-declaration: 1268322f633cSDouglas Gregor /// 'umbrella'[opt] 'header' string-literal 126959527666SDouglas Gregor /// 'exclude'[opt] 'header' string-literal 127059527666SDouglas Gregor void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc, 127159527666SDouglas Gregor SourceLocation ExcludeLoc) { 1272718292f2SDouglas Gregor assert(Tok.is(MMToken::HeaderKeyword)); 12731871ed3dSBenjamin Kramer consumeToken(); 1274718292f2SDouglas Gregor 1275322f633cSDouglas Gregor bool Umbrella = UmbrellaLoc.isValid(); 127659527666SDouglas Gregor bool Exclude = ExcludeLoc.isValid(); 127759527666SDouglas Gregor assert(!(Umbrella && Exclude) && "Cannot have both 'umbrella' and 'exclude'"); 1278718292f2SDouglas Gregor // Parse the header name. 1279718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1280718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1281718292f2SDouglas Gregor << "header"; 1282718292f2SDouglas Gregor HadError = true; 1283718292f2SDouglas Gregor return; 1284718292f2SDouglas Gregor } 1285e7ab3669SDouglas Gregor std::string FileName = Tok.getString(); 1286718292f2SDouglas Gregor SourceLocation FileNameLoc = consumeToken(); 1287718292f2SDouglas Gregor 1288524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1289524e33e1SDouglas Gregor if (Umbrella && ActiveModule->Umbrella) { 1290524e33e1SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash) 1291524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1292322f633cSDouglas Gregor HadError = true; 1293322f633cSDouglas Gregor return; 1294322f633cSDouglas Gregor } 1295322f633cSDouglas Gregor 12965257fc63SDouglas Gregor // Look for this file. 1297e7ab3669SDouglas Gregor const FileEntry *File = 0; 12983ec6663bSDouglas Gregor const FileEntry *BuiltinFile = 0; 12992c1dd271SDylan Noblesmith SmallString<128> PathName; 1300e7ab3669SDouglas Gregor if (llvm::sys::path::is_absolute(FileName)) { 1301e7ab3669SDouglas Gregor PathName = FileName; 1302e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 13037033127bSDouglas Gregor } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) { 13047033127bSDouglas Gregor PathName = Dir->getName(); 13057033127bSDouglas Gregor llvm::sys::path::append(PathName, FileName); 13067033127bSDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1307e7ab3669SDouglas Gregor } else { 1308e7ab3669SDouglas Gregor // Search for the header file within the search directory. 13097033127bSDouglas Gregor PathName = Directory->getName(); 1310e7ab3669SDouglas Gregor unsigned PathLength = PathName.size(); 1311755b2055SDouglas Gregor 1312f2161a70SDouglas Gregor if (ActiveModule->isPartOfFramework()) { 1313f2161a70SDouglas Gregor appendSubframeworkPaths(ActiveModule, PathName); 1314755b2055SDouglas Gregor 1315e7ab3669SDouglas Gregor // Check whether this file is in the public headers. 1316e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, "Headers"); 13175257fc63SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1318e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1319e7ab3669SDouglas Gregor 1320e7ab3669SDouglas Gregor if (!File) { 1321e7ab3669SDouglas Gregor // Check whether this file is in the private headers. 1322e7ab3669SDouglas Gregor PathName.resize(PathLength); 1323e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, "PrivateHeaders"); 1324e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1325e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1326e7ab3669SDouglas Gregor } 1327e7ab3669SDouglas Gregor } else { 1328e7ab3669SDouglas Gregor // Lookup for normal headers. 1329e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1330e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 13313ec6663bSDouglas Gregor 13323ec6663bSDouglas Gregor // If this is a system module with a top-level header, this header 13333ec6663bSDouglas Gregor // may have a counterpart (or replacement) in the set of headers 13343ec6663bSDouglas Gregor // supplied by Clang. Find that builtin header. 13353ec6663bSDouglas Gregor if (ActiveModule->IsSystem && !Umbrella && BuiltinIncludeDir && 13363ec6663bSDouglas Gregor BuiltinIncludeDir != Directory && isBuiltinHeader(FileName)) { 13372c1dd271SDylan Noblesmith SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); 13383ec6663bSDouglas Gregor llvm::sys::path::append(BuiltinPathName, FileName); 13393ec6663bSDouglas Gregor BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); 13403ec6663bSDouglas Gregor 13413ec6663bSDouglas Gregor // If Clang supplies this header but the underlying system does not, 13423ec6663bSDouglas Gregor // just silently swap in our builtin version. Otherwise, we'll end 13433ec6663bSDouglas Gregor // up adding both (later). 13443ec6663bSDouglas Gregor if (!File && BuiltinFile) { 13453ec6663bSDouglas Gregor File = BuiltinFile; 13463ec6663bSDouglas Gregor BuiltinFile = 0; 13473ec6663bSDouglas Gregor } 13483ec6663bSDouglas Gregor } 1349e7ab3669SDouglas Gregor } 1350e7ab3669SDouglas Gregor } 13515257fc63SDouglas Gregor 13525257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 13535257fc63SDouglas Gregor // Come up with a lazy way to do this. 1354e7ab3669SDouglas Gregor if (File) { 135559527666SDouglas Gregor if (ModuleMap::KnownHeader OwningModule = Map.Headers[File]) { 13565257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_conflict) 135759527666SDouglas Gregor << FileName << OwningModule.getModule()->getFullModuleName(); 13585257fc63SDouglas Gregor HadError = true; 1359322f633cSDouglas Gregor } else if (Umbrella) { 1360322f633cSDouglas Gregor const DirectoryEntry *UmbrellaDir = File->getDir(); 136159527666SDouglas Gregor if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { 1362322f633cSDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 136359527666SDouglas Gregor << UmbrellaModule->getFullModuleName(); 1364322f633cSDouglas Gregor HadError = true; 13655257fc63SDouglas Gregor } else { 1366322f633cSDouglas Gregor // Record this umbrella header. 1367322f633cSDouglas Gregor Map.setUmbrellaHeader(ActiveModule, File); 1368322f633cSDouglas Gregor } 1369322f633cSDouglas Gregor } else { 1370322f633cSDouglas Gregor // Record this header. 137159527666SDouglas Gregor Map.addHeader(ActiveModule, File, Exclude); 13723ec6663bSDouglas Gregor 13733ec6663bSDouglas Gregor // If there is a builtin counterpart to this file, add it now. 13743ec6663bSDouglas Gregor if (BuiltinFile) 137559527666SDouglas Gregor Map.addHeader(ActiveModule, BuiltinFile, Exclude); 13765257fc63SDouglas Gregor } 13774b27a64bSDouglas Gregor } else if (!Exclude) { 13784b27a64bSDouglas Gregor // Ignore excluded header files. They're optional anyway. 13794b27a64bSDouglas Gregor 13805257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_not_found) 1381524e33e1SDouglas Gregor << Umbrella << FileName; 13825257fc63SDouglas Gregor HadError = true; 13835257fc63SDouglas Gregor } 1384718292f2SDouglas Gregor } 1385718292f2SDouglas Gregor 1386524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration. 1387524e33e1SDouglas Gregor /// 1388524e33e1SDouglas Gregor /// umbrella-dir-declaration: 1389524e33e1SDouglas Gregor /// umbrella string-literal 1390524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1391524e33e1SDouglas Gregor // Parse the directory name. 1392524e33e1SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1393524e33e1SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1394524e33e1SDouglas Gregor << "umbrella"; 1395524e33e1SDouglas Gregor HadError = true; 1396524e33e1SDouglas Gregor return; 1397524e33e1SDouglas Gregor } 1398524e33e1SDouglas Gregor 1399524e33e1SDouglas Gregor std::string DirName = Tok.getString(); 1400524e33e1SDouglas Gregor SourceLocation DirNameLoc = consumeToken(); 1401524e33e1SDouglas Gregor 1402524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1403524e33e1SDouglas Gregor if (ActiveModule->Umbrella) { 1404524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1405524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1406524e33e1SDouglas Gregor HadError = true; 1407524e33e1SDouglas Gregor return; 1408524e33e1SDouglas Gregor } 1409524e33e1SDouglas Gregor 1410524e33e1SDouglas Gregor // Look for this file. 1411524e33e1SDouglas Gregor const DirectoryEntry *Dir = 0; 1412524e33e1SDouglas Gregor if (llvm::sys::path::is_absolute(DirName)) 1413524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(DirName); 1414524e33e1SDouglas Gregor else { 14152c1dd271SDylan Noblesmith SmallString<128> PathName; 1416524e33e1SDouglas Gregor PathName = Directory->getName(); 1417524e33e1SDouglas Gregor llvm::sys::path::append(PathName, DirName); 1418524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(PathName); 1419524e33e1SDouglas Gregor } 1420524e33e1SDouglas Gregor 1421524e33e1SDouglas Gregor if (!Dir) { 1422524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1423524e33e1SDouglas Gregor << DirName; 1424524e33e1SDouglas Gregor HadError = true; 1425524e33e1SDouglas Gregor return; 1426524e33e1SDouglas Gregor } 1427524e33e1SDouglas Gregor 1428524e33e1SDouglas Gregor if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1429524e33e1SDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1430524e33e1SDouglas Gregor << OwningModule->getFullModuleName(); 1431524e33e1SDouglas Gregor HadError = true; 1432524e33e1SDouglas Gregor return; 1433524e33e1SDouglas Gregor } 1434524e33e1SDouglas Gregor 1435524e33e1SDouglas Gregor // Record this umbrella directory. 1436524e33e1SDouglas Gregor Map.setUmbrellaDir(ActiveModule, Dir); 1437524e33e1SDouglas Gregor } 1438524e33e1SDouglas Gregor 14392b82c2a5SDouglas Gregor /// \brief Parse a module export declaration. 14402b82c2a5SDouglas Gregor /// 14412b82c2a5SDouglas Gregor /// export-declaration: 14422b82c2a5SDouglas Gregor /// 'export' wildcard-module-id 14432b82c2a5SDouglas Gregor /// 14442b82c2a5SDouglas Gregor /// wildcard-module-id: 14452b82c2a5SDouglas Gregor /// identifier 14462b82c2a5SDouglas Gregor /// '*' 14472b82c2a5SDouglas Gregor /// identifier '.' wildcard-module-id 14482b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() { 14492b82c2a5SDouglas Gregor assert(Tok.is(MMToken::ExportKeyword)); 14502b82c2a5SDouglas Gregor SourceLocation ExportLoc = consumeToken(); 14512b82c2a5SDouglas Gregor 14522b82c2a5SDouglas Gregor // Parse the module-id with an optional wildcard at the end. 14532b82c2a5SDouglas Gregor ModuleId ParsedModuleId; 14542b82c2a5SDouglas Gregor bool Wildcard = false; 14552b82c2a5SDouglas Gregor do { 14562b82c2a5SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 14572b82c2a5SDouglas Gregor ParsedModuleId.push_back(std::make_pair(Tok.getString(), 14582b82c2a5SDouglas Gregor Tok.getLocation())); 14592b82c2a5SDouglas Gregor consumeToken(); 14602b82c2a5SDouglas Gregor 14612b82c2a5SDouglas Gregor if (Tok.is(MMToken::Period)) { 14622b82c2a5SDouglas Gregor consumeToken(); 14632b82c2a5SDouglas Gregor continue; 14642b82c2a5SDouglas Gregor } 14652b82c2a5SDouglas Gregor 14662b82c2a5SDouglas Gregor break; 14672b82c2a5SDouglas Gregor } 14682b82c2a5SDouglas Gregor 14692b82c2a5SDouglas Gregor if(Tok.is(MMToken::Star)) { 14702b82c2a5SDouglas Gregor Wildcard = true; 1471f5eedd05SDouglas Gregor consumeToken(); 14722b82c2a5SDouglas Gregor break; 14732b82c2a5SDouglas Gregor } 14742b82c2a5SDouglas Gregor 14752b82c2a5SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id); 14762b82c2a5SDouglas Gregor HadError = true; 14772b82c2a5SDouglas Gregor return; 14782b82c2a5SDouglas Gregor } while (true); 14792b82c2a5SDouglas Gregor 14802b82c2a5SDouglas Gregor Module::UnresolvedExportDecl Unresolved = { 14812b82c2a5SDouglas Gregor ExportLoc, ParsedModuleId, Wildcard 14822b82c2a5SDouglas Gregor }; 14832b82c2a5SDouglas Gregor ActiveModule->UnresolvedExports.push_back(Unresolved); 14842b82c2a5SDouglas Gregor } 14852b82c2a5SDouglas Gregor 14866ddfca91SDouglas Gregor /// \brief Parse a link declaration. 14876ddfca91SDouglas Gregor /// 14886ddfca91SDouglas Gregor /// module-declaration: 14896ddfca91SDouglas Gregor /// 'link' 'framework'[opt] string-literal 14906ddfca91SDouglas Gregor void ModuleMapParser::parseLinkDecl() { 14916ddfca91SDouglas Gregor assert(Tok.is(MMToken::LinkKeyword)); 14926ddfca91SDouglas Gregor SourceLocation LinkLoc = consumeToken(); 14936ddfca91SDouglas Gregor 14946ddfca91SDouglas Gregor // Parse the optional 'framework' keyword. 14956ddfca91SDouglas Gregor bool IsFramework = false; 14966ddfca91SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 14976ddfca91SDouglas Gregor consumeToken(); 14986ddfca91SDouglas Gregor IsFramework = true; 14996ddfca91SDouglas Gregor } 15006ddfca91SDouglas Gregor 15016ddfca91SDouglas Gregor // Parse the library name 15026ddfca91SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 15036ddfca91SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) 15046ddfca91SDouglas Gregor << IsFramework << SourceRange(LinkLoc); 15056ddfca91SDouglas Gregor HadError = true; 15066ddfca91SDouglas Gregor return; 15076ddfca91SDouglas Gregor } 15086ddfca91SDouglas Gregor 15096ddfca91SDouglas Gregor std::string LibraryName = Tok.getString(); 15106ddfca91SDouglas Gregor consumeToken(); 15116ddfca91SDouglas Gregor ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, 15126ddfca91SDouglas Gregor IsFramework)); 15136ddfca91SDouglas Gregor } 15146ddfca91SDouglas Gregor 15156ddfca91SDouglas Gregor /// \brief Parse an inferred module declaration (wildcard modules). 15169194a91dSDouglas Gregor /// 15179194a91dSDouglas Gregor /// module-declaration: 15189194a91dSDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] 15199194a91dSDouglas Gregor /// { inferred-module-member* } 15209194a91dSDouglas Gregor /// 15219194a91dSDouglas Gregor /// inferred-module-member: 15229194a91dSDouglas Gregor /// 'export' '*' 15239194a91dSDouglas Gregor /// 'exclude' identifier 15249194a91dSDouglas Gregor void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { 152573441091SDouglas Gregor assert(Tok.is(MMToken::Star)); 152673441091SDouglas Gregor SourceLocation StarLoc = consumeToken(); 152773441091SDouglas Gregor bool Failed = false; 152873441091SDouglas Gregor 152973441091SDouglas Gregor // Inferred modules must be submodules. 15309194a91dSDouglas Gregor if (!ActiveModule && !Framework) { 153173441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 153273441091SDouglas Gregor Failed = true; 153373441091SDouglas Gregor } 153473441091SDouglas Gregor 15359194a91dSDouglas Gregor if (ActiveModule) { 1536524e33e1SDouglas Gregor // Inferred modules must have umbrella directories. 1537524e33e1SDouglas Gregor if (!Failed && !ActiveModule->getUmbrellaDir()) { 153873441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 153973441091SDouglas Gregor Failed = true; 154073441091SDouglas Gregor } 154173441091SDouglas Gregor 154273441091SDouglas Gregor // Check for redefinition of an inferred module. 1543dd005f69SDouglas Gregor if (!Failed && ActiveModule->InferSubmodules) { 154473441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 1545dd005f69SDouglas Gregor if (ActiveModule->InferredSubmoduleLoc.isValid()) 1546dd005f69SDouglas Gregor Diags.Report(ActiveModule->InferredSubmoduleLoc, 154773441091SDouglas Gregor diag::note_mmap_prev_definition); 154873441091SDouglas Gregor Failed = true; 154973441091SDouglas Gregor } 155073441091SDouglas Gregor 15519194a91dSDouglas Gregor // Check for the 'framework' keyword, which is not permitted here. 15529194a91dSDouglas Gregor if (Framework) { 15539194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); 15549194a91dSDouglas Gregor Framework = false; 15559194a91dSDouglas Gregor } 15569194a91dSDouglas Gregor } else if (Explicit) { 15579194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); 15589194a91dSDouglas Gregor Explicit = false; 15599194a91dSDouglas Gregor } 15609194a91dSDouglas Gregor 156173441091SDouglas Gregor // If there were any problems with this inferred submodule, skip its body. 156273441091SDouglas Gregor if (Failed) { 156373441091SDouglas Gregor if (Tok.is(MMToken::LBrace)) { 156473441091SDouglas Gregor consumeToken(); 156573441091SDouglas Gregor skipUntil(MMToken::RBrace); 156673441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 156773441091SDouglas Gregor consumeToken(); 156873441091SDouglas Gregor } 156973441091SDouglas Gregor HadError = true; 157073441091SDouglas Gregor return; 157173441091SDouglas Gregor } 157273441091SDouglas Gregor 15739194a91dSDouglas Gregor // Parse optional attributes. 15744442605fSBill Wendling Attributes Attrs; 15759194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 15769194a91dSDouglas Gregor 15779194a91dSDouglas Gregor if (ActiveModule) { 157873441091SDouglas Gregor // Note that we have an inferred submodule. 1579dd005f69SDouglas Gregor ActiveModule->InferSubmodules = true; 1580dd005f69SDouglas Gregor ActiveModule->InferredSubmoduleLoc = StarLoc; 1581dd005f69SDouglas Gregor ActiveModule->InferExplicitSubmodules = Explicit; 15829194a91dSDouglas Gregor } else { 15839194a91dSDouglas Gregor // We'll be inferring framework modules for this directory. 15849194a91dSDouglas Gregor Map.InferredDirectories[Directory].InferModules = true; 15859194a91dSDouglas Gregor Map.InferredDirectories[Directory].InferSystemModules = Attrs.IsSystem; 15869194a91dSDouglas Gregor } 158773441091SDouglas Gregor 158873441091SDouglas Gregor // Parse the opening brace. 158973441091SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 159073441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 159173441091SDouglas Gregor HadError = true; 159273441091SDouglas Gregor return; 159373441091SDouglas Gregor } 159473441091SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 159573441091SDouglas Gregor 159673441091SDouglas Gregor // Parse the body of the inferred submodule. 159773441091SDouglas Gregor bool Done = false; 159873441091SDouglas Gregor do { 159973441091SDouglas Gregor switch (Tok.Kind) { 160073441091SDouglas Gregor case MMToken::EndOfFile: 160173441091SDouglas Gregor case MMToken::RBrace: 160273441091SDouglas Gregor Done = true; 160373441091SDouglas Gregor break; 160473441091SDouglas Gregor 16059194a91dSDouglas Gregor case MMToken::ExcludeKeyword: { 16069194a91dSDouglas Gregor if (ActiveModule) { 16079194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 1608162405daSDouglas Gregor << (ActiveModule != 0); 16099194a91dSDouglas Gregor consumeToken(); 16109194a91dSDouglas Gregor break; 16119194a91dSDouglas Gregor } 16129194a91dSDouglas Gregor 16139194a91dSDouglas Gregor consumeToken(); 16149194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 16159194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); 16169194a91dSDouglas Gregor break; 16179194a91dSDouglas Gregor } 16189194a91dSDouglas Gregor 16199194a91dSDouglas Gregor Map.InferredDirectories[Directory].ExcludedModules 16209194a91dSDouglas Gregor .push_back(Tok.getString()); 16219194a91dSDouglas Gregor consumeToken(); 16229194a91dSDouglas Gregor break; 16239194a91dSDouglas Gregor } 16249194a91dSDouglas Gregor 16259194a91dSDouglas Gregor case MMToken::ExportKeyword: 16269194a91dSDouglas Gregor if (!ActiveModule) { 16279194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 1628162405daSDouglas Gregor << (ActiveModule != 0); 16299194a91dSDouglas Gregor consumeToken(); 16309194a91dSDouglas Gregor break; 16319194a91dSDouglas Gregor } 16329194a91dSDouglas Gregor 163373441091SDouglas Gregor consumeToken(); 163473441091SDouglas Gregor if (Tok.is(MMToken::Star)) 1635dd005f69SDouglas Gregor ActiveModule->InferExportWildcard = true; 163673441091SDouglas Gregor else 163773441091SDouglas Gregor Diags.Report(Tok.getLocation(), 163873441091SDouglas Gregor diag::err_mmap_expected_export_wildcard); 163973441091SDouglas Gregor consumeToken(); 164073441091SDouglas Gregor break; 164173441091SDouglas Gregor 164273441091SDouglas Gregor case MMToken::ExplicitKeyword: 164373441091SDouglas Gregor case MMToken::ModuleKeyword: 164473441091SDouglas Gregor case MMToken::HeaderKeyword: 164573441091SDouglas Gregor case MMToken::UmbrellaKeyword: 164673441091SDouglas Gregor default: 16479194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 1648162405daSDouglas Gregor << (ActiveModule != 0); 164973441091SDouglas Gregor consumeToken(); 165073441091SDouglas Gregor break; 165173441091SDouglas Gregor } 165273441091SDouglas Gregor } while (!Done); 165373441091SDouglas Gregor 165473441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 165573441091SDouglas Gregor consumeToken(); 165673441091SDouglas Gregor else { 165773441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 165873441091SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 165973441091SDouglas Gregor HadError = true; 166073441091SDouglas Gregor } 166173441091SDouglas Gregor } 166273441091SDouglas Gregor 16639194a91dSDouglas Gregor /// \brief Parse optional attributes. 16649194a91dSDouglas Gregor /// 16659194a91dSDouglas Gregor /// attributes: 16669194a91dSDouglas Gregor /// attribute attributes 16679194a91dSDouglas Gregor /// attribute 16689194a91dSDouglas Gregor /// 16699194a91dSDouglas Gregor /// attribute: 16709194a91dSDouglas Gregor /// [ identifier ] 16719194a91dSDouglas Gregor /// 16729194a91dSDouglas Gregor /// \param Attrs Will be filled in with the parsed attributes. 16739194a91dSDouglas Gregor /// 16749194a91dSDouglas Gregor /// \returns true if an error occurred, false otherwise. 16754442605fSBill Wendling bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { 16769194a91dSDouglas Gregor bool HadError = false; 16779194a91dSDouglas Gregor 16789194a91dSDouglas Gregor while (Tok.is(MMToken::LSquare)) { 16799194a91dSDouglas Gregor // Consume the '['. 16809194a91dSDouglas Gregor SourceLocation LSquareLoc = consumeToken(); 16819194a91dSDouglas Gregor 16829194a91dSDouglas Gregor // Check whether we have an attribute name here. 16839194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 16849194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 16859194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 16869194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 16879194a91dSDouglas Gregor consumeToken(); 16889194a91dSDouglas Gregor HadError = true; 16899194a91dSDouglas Gregor } 16909194a91dSDouglas Gregor 16919194a91dSDouglas Gregor // Decode the attribute name. 16929194a91dSDouglas Gregor AttributeKind Attribute 16939194a91dSDouglas Gregor = llvm::StringSwitch<AttributeKind>(Tok.getString()) 16949194a91dSDouglas Gregor .Case("system", AT_system) 16959194a91dSDouglas Gregor .Default(AT_unknown); 16969194a91dSDouglas Gregor switch (Attribute) { 16979194a91dSDouglas Gregor case AT_unknown: 16989194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 16999194a91dSDouglas Gregor << Tok.getString(); 17009194a91dSDouglas Gregor break; 17019194a91dSDouglas Gregor 17029194a91dSDouglas Gregor case AT_system: 17039194a91dSDouglas Gregor Attrs.IsSystem = true; 17049194a91dSDouglas Gregor break; 17059194a91dSDouglas Gregor } 17069194a91dSDouglas Gregor consumeToken(); 17079194a91dSDouglas Gregor 17089194a91dSDouglas Gregor // Consume the ']'. 17099194a91dSDouglas Gregor if (!Tok.is(MMToken::RSquare)) { 17109194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 17119194a91dSDouglas Gregor Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 17129194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 17139194a91dSDouglas Gregor HadError = true; 17149194a91dSDouglas Gregor } 17159194a91dSDouglas Gregor 17169194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 17179194a91dSDouglas Gregor consumeToken(); 17189194a91dSDouglas Gregor } 17199194a91dSDouglas Gregor 17209194a91dSDouglas Gregor return HadError; 17219194a91dSDouglas Gregor } 17229194a91dSDouglas Gregor 17237033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence 17247033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null. 17257033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() { 17267033127bSDouglas Gregor for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) { 17277033127bSDouglas Gregor // If we have an umbrella directory, use that. 17287033127bSDouglas Gregor if (Mod->hasUmbrellaDir()) 17297033127bSDouglas Gregor return Mod->getUmbrellaDir(); 17307033127bSDouglas Gregor 17317033127bSDouglas Gregor // If we have a framework directory, stop looking. 17327033127bSDouglas Gregor if (Mod->IsFramework) 17337033127bSDouglas Gregor return 0; 17347033127bSDouglas Gregor } 17357033127bSDouglas Gregor 17367033127bSDouglas Gregor return 0; 17377033127bSDouglas Gregor } 17387033127bSDouglas Gregor 1739718292f2SDouglas Gregor /// \brief Parse a module map file. 1740718292f2SDouglas Gregor /// 1741718292f2SDouglas Gregor /// module-map-file: 1742718292f2SDouglas Gregor /// module-declaration* 1743718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() { 1744718292f2SDouglas Gregor do { 1745718292f2SDouglas Gregor switch (Tok.Kind) { 1746718292f2SDouglas Gregor case MMToken::EndOfFile: 1747718292f2SDouglas Gregor return HadError; 1748718292f2SDouglas Gregor 1749e7ab3669SDouglas Gregor case MMToken::ExplicitKeyword: 1750718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1751755b2055SDouglas Gregor case MMToken::FrameworkKeyword: 1752718292f2SDouglas Gregor parseModuleDecl(); 1753718292f2SDouglas Gregor break; 1754718292f2SDouglas Gregor 17551fb5c3a6SDouglas Gregor case MMToken::Comma: 175659527666SDouglas Gregor case MMToken::ExcludeKeyword: 17572b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 1758718292f2SDouglas Gregor case MMToken::HeaderKeyword: 1759718292f2SDouglas Gregor case MMToken::Identifier: 1760718292f2SDouglas Gregor case MMToken::LBrace: 17616ddfca91SDouglas Gregor case MMToken::LinkKeyword: 1762a686e1b0SDouglas Gregor case MMToken::LSquare: 17632b82c2a5SDouglas Gregor case MMToken::Period: 1764718292f2SDouglas Gregor case MMToken::RBrace: 1765a686e1b0SDouglas Gregor case MMToken::RSquare: 17661fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 17672b82c2a5SDouglas Gregor case MMToken::Star: 1768718292f2SDouglas Gregor case MMToken::StringLiteral: 1769718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 1770718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1771718292f2SDouglas Gregor HadError = true; 1772718292f2SDouglas Gregor consumeToken(); 1773718292f2SDouglas Gregor break; 1774718292f2SDouglas Gregor } 1775718292f2SDouglas Gregor } while (true); 1776718292f2SDouglas Gregor } 1777718292f2SDouglas Gregor 1778718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) { 17794ddf2221SDouglas Gregor llvm::DenseMap<const FileEntry *, bool>::iterator Known 17804ddf2221SDouglas Gregor = ParsedModuleMap.find(File); 17814ddf2221SDouglas Gregor if (Known != ParsedModuleMap.end()) 17824ddf2221SDouglas Gregor return Known->second; 17834ddf2221SDouglas Gregor 178489929282SDouglas Gregor assert(Target != 0 && "Missing target information"); 1785718292f2SDouglas Gregor FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User); 1786718292f2SDouglas Gregor const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID); 1787718292f2SDouglas Gregor if (!Buffer) 17884ddf2221SDouglas Gregor return ParsedModuleMap[File] = true; 1789718292f2SDouglas Gregor 1790718292f2SDouglas Gregor // Parse this module map file. 17911fb5c3a6SDouglas Gregor Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts); 17921fb5c3a6SDouglas Gregor Diags->getClient()->BeginSourceFile(MMapLangOpts); 1793bc10b9fbSDouglas Gregor ModuleMapParser Parser(L, *SourceMgr, Target, *Diags, *this, File->getDir(), 17943ec6663bSDouglas Gregor BuiltinIncludeDir); 1795718292f2SDouglas Gregor bool Result = Parser.parseModuleMapFile(); 1796718292f2SDouglas Gregor Diags->getClient()->EndSourceFile(); 17974ddf2221SDouglas Gregor ParsedModuleMap[File] = Result; 1798718292f2SDouglas Gregor return Result; 1799718292f2SDouglas Gregor } 1800