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" 15a7d03840SJordan Rose #include "clang/Basic/CharInfo.h" 16718292f2SDouglas Gregor #include "clang/Basic/Diagnostic.h" 17811db4eaSDouglas Gregor #include "clang/Basic/DiagnosticOptions.h" 18718292f2SDouglas Gregor #include "clang/Basic/FileManager.h" 19718292f2SDouglas Gregor #include "clang/Basic/TargetInfo.h" 20718292f2SDouglas Gregor #include "clang/Basic/TargetOptions.h" 21b146baabSArgyrios Kyrtzidis #include "clang/Lex/HeaderSearch.h" 223a02247dSChandler Carruth #include "clang/Lex/LexDiagnostic.h" 233a02247dSChandler Carruth #include "clang/Lex/Lexer.h" 243a02247dSChandler Carruth #include "clang/Lex/LiteralSupport.h" 253a02247dSChandler Carruth #include "llvm/ADT/StringRef.h" 263a02247dSChandler Carruth #include "llvm/ADT/StringSwitch.h" 27718292f2SDouglas Gregor #include "llvm/Support/Allocator.h" 28e89dbc1dSDouglas Gregor #include "llvm/Support/FileSystem.h" 29718292f2SDouglas Gregor #include "llvm/Support/Host.h" 30552c169eSRafael Espindola #include "llvm/Support/Path.h" 31718292f2SDouglas Gregor #include "llvm/Support/raw_ostream.h" 3207c22b78SDouglas Gregor #include <stdlib.h> 3301c7cfa2SDouglas Gregor #if defined(LLVM_ON_UNIX) 34eadae014SDmitri Gribenko #include <limits.h> 3501c7cfa2SDouglas 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, 41e4412640SArgyrios Kyrtzidis bool Complain) const { 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 48fb912657SDouglas Gregor // Resolve the module-id. 49fb912657SDouglas Gregor Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain); 50fb912657SDouglas Gregor if (!Context) 51fb912657SDouglas Gregor return Module::ExportDecl(); 52fb912657SDouglas Gregor 53fb912657SDouglas Gregor return Module::ExportDecl(Context, Unresolved.Wildcard); 54fb912657SDouglas Gregor } 55fb912657SDouglas Gregor 56fb912657SDouglas Gregor Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod, 57fb912657SDouglas Gregor bool Complain) const { 582b82c2a5SDouglas Gregor // Find the starting module. 59fb912657SDouglas Gregor Module *Context = lookupModuleUnqualified(Id[0].first, Mod); 602b82c2a5SDouglas Gregor if (!Context) { 612b82c2a5SDouglas Gregor if (Complain) 62fb912657SDouglas Gregor Diags->Report(Id[0].second, diag::err_mmap_missing_module_unqualified) 63fb912657SDouglas Gregor << Id[0].first << Mod->getFullModuleName(); 642b82c2a5SDouglas Gregor 65fb912657SDouglas Gregor return 0; 662b82c2a5SDouglas Gregor } 672b82c2a5SDouglas Gregor 682b82c2a5SDouglas Gregor // Dig into the module path. 69fb912657SDouglas Gregor for (unsigned I = 1, N = Id.size(); I != N; ++I) { 70fb912657SDouglas Gregor Module *Sub = lookupModuleQualified(Id[I].first, Context); 712b82c2a5SDouglas Gregor if (!Sub) { 722b82c2a5SDouglas Gregor if (Complain) 73fb912657SDouglas Gregor Diags->Report(Id[I].second, diag::err_mmap_missing_module_qualified) 74fb912657SDouglas Gregor << Id[I].first << Context->getFullModuleName() 75fb912657SDouglas Gregor << SourceRange(Id[0].second, Id[I-1].second); 762b82c2a5SDouglas Gregor 77fb912657SDouglas Gregor return 0; 782b82c2a5SDouglas Gregor } 792b82c2a5SDouglas Gregor 802b82c2a5SDouglas Gregor Context = Sub; 812b82c2a5SDouglas Gregor } 822b82c2a5SDouglas Gregor 83fb912657SDouglas Gregor return Context; 842b82c2a5SDouglas Gregor } 852b82c2a5SDouglas Gregor 861f76c4e8SManuel Klimek ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticConsumer &DC, 87b146baabSArgyrios Kyrtzidis const LangOptions &LangOpts, const TargetInfo *Target, 88b146baabSArgyrios Kyrtzidis HeaderSearch &HeaderInfo) 891f76c4e8SManuel Klimek : SourceMgr(SourceMgr), LangOpts(LangOpts), Target(Target), 901f76c4e8SManuel Klimek HeaderInfo(HeaderInfo), BuiltinIncludeDir(0), CompilingModule(0), 911f76c4e8SManuel Klimek SourceModule(0) { 92c95d8192SDylan Noblesmith IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs); 93c95d8192SDylan Noblesmith Diags = IntrusiveRefCntPtr<DiagnosticsEngine>( 94811db4eaSDouglas Gregor new DiagnosticsEngine(DiagIDs, new DiagnosticOptions)); 956b930967SDouglas Gregor Diags->setClient(new ForwardingDiagnosticConsumer(DC), 966b930967SDouglas Gregor /*ShouldOwnClient=*/true); 971f76c4e8SManuel Klimek Diags->setSourceManager(&SourceMgr); 98718292f2SDouglas Gregor } 99718292f2SDouglas Gregor 100718292f2SDouglas Gregor ModuleMap::~ModuleMap() { 1015acdf59eSDouglas Gregor for (llvm::StringMap<Module *>::iterator I = Modules.begin(), 1025acdf59eSDouglas Gregor IEnd = Modules.end(); 1035acdf59eSDouglas Gregor I != IEnd; ++I) { 1045acdf59eSDouglas Gregor delete I->getValue(); 1055acdf59eSDouglas Gregor } 106718292f2SDouglas Gregor } 107718292f2SDouglas Gregor 10889929282SDouglas Gregor void ModuleMap::setTarget(const TargetInfo &Target) { 10989929282SDouglas Gregor assert((!this->Target || this->Target == &Target) && 11089929282SDouglas Gregor "Improper target override"); 11189929282SDouglas Gregor this->Target = &Target; 11289929282SDouglas Gregor } 11389929282SDouglas Gregor 114056396aeSDouglas Gregor /// \brief "Sanitize" a filename so that it can be used as an identifier. 115056396aeSDouglas Gregor static StringRef sanitizeFilenameAsIdentifier(StringRef Name, 116056396aeSDouglas Gregor SmallVectorImpl<char> &Buffer) { 117056396aeSDouglas Gregor if (Name.empty()) 118056396aeSDouglas Gregor return Name; 119056396aeSDouglas Gregor 120a7d03840SJordan Rose if (!isValidIdentifier(Name)) { 121056396aeSDouglas Gregor // If we don't already have something with the form of an identifier, 122056396aeSDouglas Gregor // create a buffer with the sanitized name. 123056396aeSDouglas Gregor Buffer.clear(); 124a7d03840SJordan Rose if (isDigit(Name[0])) 125056396aeSDouglas Gregor Buffer.push_back('_'); 126056396aeSDouglas Gregor Buffer.reserve(Buffer.size() + Name.size()); 127056396aeSDouglas Gregor for (unsigned I = 0, N = Name.size(); I != N; ++I) { 128a7d03840SJordan Rose if (isIdentifierBody(Name[I])) 129056396aeSDouglas Gregor Buffer.push_back(Name[I]); 130056396aeSDouglas Gregor else 131056396aeSDouglas Gregor Buffer.push_back('_'); 132056396aeSDouglas Gregor } 133056396aeSDouglas Gregor 134056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 135056396aeSDouglas Gregor } 136056396aeSDouglas Gregor 137056396aeSDouglas Gregor while (llvm::StringSwitch<bool>(Name) 138056396aeSDouglas Gregor #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true) 139056396aeSDouglas Gregor #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true) 140056396aeSDouglas Gregor #include "clang/Basic/TokenKinds.def" 141056396aeSDouglas Gregor .Default(false)) { 142056396aeSDouglas Gregor if (Name.data() != Buffer.data()) 143056396aeSDouglas Gregor Buffer.append(Name.begin(), Name.end()); 144056396aeSDouglas Gregor Buffer.push_back('_'); 145056396aeSDouglas Gregor Name = StringRef(Buffer.data(), Buffer.size()); 146056396aeSDouglas Gregor } 147056396aeSDouglas Gregor 148056396aeSDouglas Gregor return Name; 149056396aeSDouglas Gregor } 150056396aeSDouglas Gregor 15134d52749SDouglas Gregor /// \brief Determine whether the given file name is the name of a builtin 15234d52749SDouglas Gregor /// header, supplied by Clang to replace, override, or augment existing system 15334d52749SDouglas Gregor /// headers. 15434d52749SDouglas Gregor static bool isBuiltinHeader(StringRef FileName) { 15534d52749SDouglas Gregor return llvm::StringSwitch<bool>(FileName) 15634d52749SDouglas Gregor .Case("float.h", true) 15734d52749SDouglas Gregor .Case("iso646.h", true) 15834d52749SDouglas Gregor .Case("limits.h", true) 15934d52749SDouglas Gregor .Case("stdalign.h", true) 16034d52749SDouglas Gregor .Case("stdarg.h", true) 16134d52749SDouglas Gregor .Case("stdbool.h", true) 16234d52749SDouglas Gregor .Case("stddef.h", true) 16334d52749SDouglas Gregor .Case("stdint.h", true) 16434d52749SDouglas Gregor .Case("tgmath.h", true) 16534d52749SDouglas Gregor .Case("unwind.h", true) 16634d52749SDouglas Gregor .Default(false); 16734d52749SDouglas Gregor } 16834d52749SDouglas Gregor 16997da9178SDaniel Jasper ModuleMap::KnownHeader 17097da9178SDaniel Jasper ModuleMap::findModuleForHeader(const FileEntry *File, 17197da9178SDaniel Jasper Module *RequestingModule) { 17259527666SDouglas Gregor HeadersMap::iterator Known = Headers.find(File); 1731fb5c3a6SDouglas Gregor if (Known != Headers.end()) { 17497da9178SDaniel Jasper ModuleMap::KnownHeader Result = KnownHeader(); 1751fb5c3a6SDouglas Gregor 17697da9178SDaniel Jasper // Iterate over all modules that 'File' is part of to find the best fit. 17797da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::iterator I = Known->second.begin(), 17897da9178SDaniel Jasper E = Known->second.end(); 17997da9178SDaniel Jasper I != E; ++I) { 18097da9178SDaniel Jasper // Cannot use a module if the header is excluded or unavailable in it. 18197da9178SDaniel Jasper if (I->getRole() == ModuleMap::ExcludedHeader || 18297da9178SDaniel Jasper !I->getModule()->isAvailable()) 18397da9178SDaniel Jasper continue; 18497da9178SDaniel Jasper 18597da9178SDaniel Jasper // If 'File' is part of 'RequestingModule', 'RequestingModule' is the 18697da9178SDaniel Jasper // module we are looking for. 18797da9178SDaniel Jasper if (I->getModule() == RequestingModule) 18897da9178SDaniel Jasper return *I; 18997da9178SDaniel Jasper 19097da9178SDaniel Jasper // If uses need to be specified explicitly, we are only allowed to return 19197da9178SDaniel Jasper // modules that are explicitly used by the requesting module. 19297da9178SDaniel Jasper if (RequestingModule && LangOpts.ModulesDeclUse && 19397da9178SDaniel Jasper std::find(RequestingModule->DirectUses.begin(), 19497da9178SDaniel Jasper RequestingModule->DirectUses.end(), 19597da9178SDaniel Jasper I->getModule()) == RequestingModule->DirectUses.end()) 19697da9178SDaniel Jasper continue; 19797da9178SDaniel Jasper Result = *I; 19897da9178SDaniel Jasper // If 'File' is a public header of this module, this is as good as we 19997da9178SDaniel Jasper // are going to get. 20097da9178SDaniel Jasper if (I->getRole() == ModuleMap::NormalHeader) 20197da9178SDaniel Jasper break; 20297da9178SDaniel Jasper } 20397da9178SDaniel Jasper return Result; 2041fb5c3a6SDouglas Gregor } 205ab0c8a84SDouglas Gregor 20634d52749SDouglas Gregor // If we've found a builtin header within Clang's builtin include directory, 20734d52749SDouglas Gregor // load all of the module maps to see if it will get associated with a 20834d52749SDouglas Gregor // specific module (e.g., in /usr/include). 20934d52749SDouglas Gregor if (File->getDir() == BuiltinIncludeDir && 21034d52749SDouglas Gregor isBuiltinHeader(llvm::sys::path::filename(File->getName()))) { 21164a1fa5cSDouglas Gregor HeaderInfo.loadTopLevelSystemModules(); 21234d52749SDouglas Gregor 21334d52749SDouglas Gregor // Check again. 21497da9178SDaniel Jasper if (Headers.find(File) != Headers.end()) 21597da9178SDaniel Jasper return findModuleForHeader(File, RequestingModule); 21634d52749SDouglas Gregor } 21734d52749SDouglas Gregor 218b65dbfffSDouglas Gregor const DirectoryEntry *Dir = File->getDir(); 219f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 220e00c8b20SDouglas Gregor 22174260502SDouglas Gregor // Note: as an egregious but useful hack we use the real path here, because 22274260502SDouglas Gregor // frameworks moving from top-level frameworks to embedded frameworks tend 22374260502SDouglas Gregor // to be symlinked from the top-level location to the embedded location, 22474260502SDouglas Gregor // and we need to resolve lookups as if we had found the embedded location. 2251f76c4e8SManuel Klimek StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir); 226a89c5ac4SDouglas Gregor 227a89c5ac4SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 228a89c5ac4SDouglas Gregor // an umbrella header. 229b65dbfffSDouglas Gregor do { 230a89c5ac4SDouglas Gregor llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir 231a89c5ac4SDouglas Gregor = UmbrellaDirs.find(Dir); 232a89c5ac4SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 233a89c5ac4SDouglas Gregor Module *Result = KnownDir->second; 234930a85ccSDouglas Gregor 235930a85ccSDouglas Gregor // Search up the module stack until we find a module with an umbrella 23673141fa9SDouglas Gregor // directory. 237930a85ccSDouglas Gregor Module *UmbrellaModule = Result; 23873141fa9SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 239930a85ccSDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 240930a85ccSDouglas Gregor 241930a85ccSDouglas Gregor if (UmbrellaModule->InferSubmodules) { 242a89c5ac4SDouglas Gregor // Infer submodules for each of the directories we found between 243a89c5ac4SDouglas Gregor // the directory of the umbrella header and the directory where 244a89c5ac4SDouglas Gregor // the actual header is located. 2459458f82dSDouglas Gregor bool Explicit = UmbrellaModule->InferExplicitSubmodules; 2469458f82dSDouglas Gregor 2477033127bSDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 248a89c5ac4SDouglas Gregor // Find or create the module that corresponds to this directory name. 249056396aeSDouglas Gregor SmallString<32> NameBuf; 250056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 251056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 252056396aeSDouglas Gregor NameBuf); 253a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 2549458f82dSDouglas Gregor Explicit).first; 255a89c5ac4SDouglas Gregor 256a89c5ac4SDouglas Gregor // Associate the module and the directory. 257a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I-1]] = Result; 258a89c5ac4SDouglas Gregor 259a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 260a89c5ac4SDouglas Gregor // wildcard to the set of exports. 261930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 262a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 263a89c5ac4SDouglas Gregor } 264a89c5ac4SDouglas Gregor 265a89c5ac4SDouglas Gregor // Infer a submodule with the same name as this header file. 266056396aeSDouglas Gregor SmallString<32> NameBuf; 267056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 268056396aeSDouglas Gregor llvm::sys::path::stem(File->getName()), NameBuf); 269a89c5ac4SDouglas Gregor Result = findOrCreateModule(Name, Result, /*IsFramework=*/false, 2709458f82dSDouglas Gregor Explicit).first; 2713c5305c1SArgyrios Kyrtzidis Result->addTopHeader(File); 272a89c5ac4SDouglas Gregor 273a89c5ac4SDouglas Gregor // If inferred submodules export everything they import, add a 274a89c5ac4SDouglas Gregor // wildcard to the set of exports. 275930a85ccSDouglas Gregor if (UmbrellaModule->InferExportWildcard && Result->Exports.empty()) 276a89c5ac4SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 277a89c5ac4SDouglas Gregor } else { 278a89c5ac4SDouglas Gregor // Record each of the directories we stepped through as being part of 279a89c5ac4SDouglas Gregor // the module we found, since the umbrella header covers them all. 280a89c5ac4SDouglas Gregor for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I) 281a89c5ac4SDouglas Gregor UmbrellaDirs[SkippedDirs[I]] = Result; 282a89c5ac4SDouglas Gregor } 283a89c5ac4SDouglas Gregor 28497da9178SDaniel Jasper Headers[File].push_back(KnownHeader(Result, NormalHeader)); 2851fb5c3a6SDouglas Gregor 2861fb5c3a6SDouglas Gregor // If a header corresponds to an unavailable module, don't report 2871fb5c3a6SDouglas Gregor // that it maps to anything. 2881fb5c3a6SDouglas Gregor if (!Result->isAvailable()) 289b53e5483SLawrence Crowl return KnownHeader(); 2901fb5c3a6SDouglas Gregor 29197da9178SDaniel Jasper return Headers[File].back(); 292a89c5ac4SDouglas Gregor } 293a89c5ac4SDouglas Gregor 294a89c5ac4SDouglas Gregor SkippedDirs.push_back(Dir); 295a89c5ac4SDouglas Gregor 296b65dbfffSDouglas Gregor // Retrieve our parent path. 297b65dbfffSDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 298b65dbfffSDouglas Gregor if (DirName.empty()) 299b65dbfffSDouglas Gregor break; 300b65dbfffSDouglas Gregor 301b65dbfffSDouglas Gregor // Resolve the parent path to a directory entry. 3021f76c4e8SManuel Klimek Dir = SourceMgr.getFileManager().getDirectory(DirName); 303a89c5ac4SDouglas Gregor } while (Dir); 304b65dbfffSDouglas Gregor 305b53e5483SLawrence Crowl return KnownHeader(); 306ab0c8a84SDouglas Gregor } 307ab0c8a84SDouglas Gregor 308e4412640SArgyrios Kyrtzidis bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const { 309e4412640SArgyrios Kyrtzidis HeadersMap::const_iterator Known = Headers.find(Header); 31097da9178SDaniel Jasper if (Known != Headers.end()) { 31197da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::const_iterator 31297da9178SDaniel Jasper I = Known->second.begin(), 31397da9178SDaniel Jasper E = Known->second.end(); 31497da9178SDaniel Jasper I != E; ++I) { 31597da9178SDaniel Jasper if (I->isAvailable()) 31697da9178SDaniel Jasper return false; 31797da9178SDaniel Jasper } 31897da9178SDaniel Jasper return true; 31997da9178SDaniel Jasper } 3201fb5c3a6SDouglas Gregor 3211fb5c3a6SDouglas Gregor const DirectoryEntry *Dir = Header->getDir(); 322f857950dSDmitri Gribenko SmallVector<const DirectoryEntry *, 2> SkippedDirs; 3231fb5c3a6SDouglas Gregor StringRef DirName = Dir->getName(); 3241fb5c3a6SDouglas Gregor 3251fb5c3a6SDouglas Gregor // Keep walking up the directory hierarchy, looking for a directory with 3261fb5c3a6SDouglas Gregor // an umbrella header. 3271fb5c3a6SDouglas Gregor do { 328e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir 3291fb5c3a6SDouglas Gregor = UmbrellaDirs.find(Dir); 3301fb5c3a6SDouglas Gregor if (KnownDir != UmbrellaDirs.end()) { 3311fb5c3a6SDouglas Gregor Module *Found = KnownDir->second; 3321fb5c3a6SDouglas Gregor if (!Found->isAvailable()) 3331fb5c3a6SDouglas Gregor return true; 3341fb5c3a6SDouglas Gregor 3351fb5c3a6SDouglas Gregor // Search up the module stack until we find a module with an umbrella 3361fb5c3a6SDouglas Gregor // directory. 3371fb5c3a6SDouglas Gregor Module *UmbrellaModule = Found; 3381fb5c3a6SDouglas Gregor while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent) 3391fb5c3a6SDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 3401fb5c3a6SDouglas Gregor 3411fb5c3a6SDouglas Gregor if (UmbrellaModule->InferSubmodules) { 3421fb5c3a6SDouglas Gregor for (unsigned I = SkippedDirs.size(); I != 0; --I) { 3431fb5c3a6SDouglas Gregor // Find or create the module that corresponds to this directory name. 344056396aeSDouglas Gregor SmallString<32> NameBuf; 345056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 346056396aeSDouglas Gregor llvm::sys::path::stem(SkippedDirs[I-1]->getName()), 347056396aeSDouglas Gregor NameBuf); 3481fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 3491fb5c3a6SDouglas Gregor if (!Found) 3501fb5c3a6SDouglas Gregor return false; 3511fb5c3a6SDouglas Gregor if (!Found->isAvailable()) 3521fb5c3a6SDouglas Gregor return true; 3531fb5c3a6SDouglas Gregor } 3541fb5c3a6SDouglas Gregor 3551fb5c3a6SDouglas Gregor // Infer a submodule with the same name as this header file. 356056396aeSDouglas Gregor SmallString<32> NameBuf; 357056396aeSDouglas Gregor StringRef Name = sanitizeFilenameAsIdentifier( 358056396aeSDouglas Gregor llvm::sys::path::stem(Header->getName()), 359056396aeSDouglas Gregor NameBuf); 3601fb5c3a6SDouglas Gregor Found = lookupModuleQualified(Name, Found); 3611fb5c3a6SDouglas Gregor if (!Found) 3621fb5c3a6SDouglas Gregor return false; 3631fb5c3a6SDouglas Gregor } 3641fb5c3a6SDouglas Gregor 3651fb5c3a6SDouglas Gregor return !Found->isAvailable(); 3661fb5c3a6SDouglas Gregor } 3671fb5c3a6SDouglas Gregor 3681fb5c3a6SDouglas Gregor SkippedDirs.push_back(Dir); 3691fb5c3a6SDouglas Gregor 3701fb5c3a6SDouglas Gregor // Retrieve our parent path. 3711fb5c3a6SDouglas Gregor DirName = llvm::sys::path::parent_path(DirName); 3721fb5c3a6SDouglas Gregor if (DirName.empty()) 3731fb5c3a6SDouglas Gregor break; 3741fb5c3a6SDouglas Gregor 3751fb5c3a6SDouglas Gregor // Resolve the parent path to a directory entry. 3761f76c4e8SManuel Klimek Dir = SourceMgr.getFileManager().getDirectory(DirName); 3771fb5c3a6SDouglas Gregor } while (Dir); 3781fb5c3a6SDouglas Gregor 3791fb5c3a6SDouglas Gregor return false; 3801fb5c3a6SDouglas Gregor } 3811fb5c3a6SDouglas Gregor 382e4412640SArgyrios Kyrtzidis Module *ModuleMap::findModule(StringRef Name) const { 383e4412640SArgyrios Kyrtzidis llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name); 38488bdfb0eSDouglas Gregor if (Known != Modules.end()) 38588bdfb0eSDouglas Gregor return Known->getValue(); 38688bdfb0eSDouglas Gregor 38788bdfb0eSDouglas Gregor return 0; 38888bdfb0eSDouglas Gregor } 38988bdfb0eSDouglas Gregor 390e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleUnqualified(StringRef Name, 391e4412640SArgyrios Kyrtzidis Module *Context) const { 3922b82c2a5SDouglas Gregor for(; Context; Context = Context->Parent) { 3932b82c2a5SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Context)) 3942b82c2a5SDouglas Gregor return Sub; 3952b82c2a5SDouglas Gregor } 3962b82c2a5SDouglas Gregor 3972b82c2a5SDouglas Gregor return findModule(Name); 3982b82c2a5SDouglas Gregor } 3992b82c2a5SDouglas Gregor 400e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{ 4012b82c2a5SDouglas Gregor if (!Context) 4022b82c2a5SDouglas Gregor return findModule(Name); 4032b82c2a5SDouglas Gregor 404eb90e830SDouglas Gregor return Context->findSubmodule(Name); 4052b82c2a5SDouglas Gregor } 4062b82c2a5SDouglas Gregor 407de3ef502SDouglas Gregor std::pair<Module *, bool> 40869021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework, 40969021974SDouglas Gregor bool IsExplicit) { 41069021974SDouglas Gregor // Try to find an existing module with this name. 411eb90e830SDouglas Gregor if (Module *Sub = lookupModuleQualified(Name, Parent)) 412eb90e830SDouglas Gregor return std::make_pair(Sub, false); 41369021974SDouglas Gregor 41469021974SDouglas Gregor // Create a new module with this name. 41569021974SDouglas Gregor Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework, 41669021974SDouglas Gregor IsExplicit); 417ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == Name) { 418ba7f2f71SDaniel Jasper SourceModule = Result; 419ba7f2f71SDaniel Jasper SourceModuleName = Name; 420ba7f2f71SDaniel Jasper } 4216f722b4eSArgyrios Kyrtzidis if (!Parent) { 42269021974SDouglas Gregor Modules[Name] = Result; 4236f722b4eSArgyrios Kyrtzidis if (!LangOpts.CurrentModule.empty() && !CompilingModule && 4246f722b4eSArgyrios Kyrtzidis Name == LangOpts.CurrentModule) { 4256f722b4eSArgyrios Kyrtzidis CompilingModule = Result; 4266f722b4eSArgyrios Kyrtzidis } 4276f722b4eSArgyrios Kyrtzidis } 42869021974SDouglas Gregor return std::make_pair(Result, true); 42969021974SDouglas Gregor } 43069021974SDouglas Gregor 4319194a91dSDouglas Gregor bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir, 432e4412640SArgyrios Kyrtzidis StringRef Name, bool &IsSystem) const { 4339194a91dSDouglas Gregor // Check whether we have already looked into the parent directory 4349194a91dSDouglas Gregor // for a module map. 435e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 4369194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 4379194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) 4389194a91dSDouglas Gregor return false; 4399194a91dSDouglas Gregor 4409194a91dSDouglas Gregor if (!inferred->second.InferModules) 4419194a91dSDouglas Gregor return false; 4429194a91dSDouglas Gregor 4439194a91dSDouglas Gregor // We're allowed to infer for this directory, but make sure it's okay 4449194a91dSDouglas Gregor // to infer this particular module. 4459194a91dSDouglas Gregor bool canInfer = std::find(inferred->second.ExcludedModules.begin(), 4469194a91dSDouglas Gregor inferred->second.ExcludedModules.end(), 4479194a91dSDouglas Gregor Name) == inferred->second.ExcludedModules.end(); 4489194a91dSDouglas Gregor 4499194a91dSDouglas Gregor if (canInfer && inferred->second.InferSystemModules) 4509194a91dSDouglas Gregor IsSystem = true; 4519194a91dSDouglas Gregor 4529194a91dSDouglas Gregor return canInfer; 4539194a91dSDouglas Gregor } 4549194a91dSDouglas Gregor 45511dfe6feSDouglas Gregor /// \brief For a framework module, infer the framework against which we 45611dfe6feSDouglas Gregor /// should link. 45711dfe6feSDouglas Gregor static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir, 45811dfe6feSDouglas Gregor FileManager &FileMgr) { 45911dfe6feSDouglas Gregor assert(Mod->IsFramework && "Can only infer linking for framework modules"); 46011dfe6feSDouglas Gregor assert(!Mod->isSubFramework() && 46111dfe6feSDouglas Gregor "Can only infer linking for top-level frameworks"); 46211dfe6feSDouglas Gregor 46311dfe6feSDouglas Gregor SmallString<128> LibName; 46411dfe6feSDouglas Gregor LibName += FrameworkDir->getName(); 46511dfe6feSDouglas Gregor llvm::sys::path::append(LibName, Mod->Name); 46611dfe6feSDouglas Gregor if (FileMgr.getFile(LibName)) { 46711dfe6feSDouglas Gregor Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name, 46811dfe6feSDouglas Gregor /*IsFramework=*/true)); 46911dfe6feSDouglas Gregor } 47011dfe6feSDouglas Gregor } 47111dfe6feSDouglas Gregor 472de3ef502SDouglas Gregor Module * 47356c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName, 474e89dbc1dSDouglas Gregor const DirectoryEntry *FrameworkDir, 475a686e1b0SDouglas Gregor bool IsSystem, 476e89dbc1dSDouglas Gregor Module *Parent) { 47756c64013SDouglas Gregor // Check whether we've already found this module. 478e89dbc1dSDouglas Gregor if (Module *Mod = lookupModuleQualified(ModuleName, Parent)) 479e89dbc1dSDouglas Gregor return Mod; 480e89dbc1dSDouglas Gregor 4811f76c4e8SManuel Klimek FileManager &FileMgr = SourceMgr.getFileManager(); 48256c64013SDouglas Gregor 4839194a91dSDouglas Gregor // If the framework has a parent path from which we're allowed to infer 4849194a91dSDouglas Gregor // a framework module, do so. 4859194a91dSDouglas Gregor if (!Parent) { 4864ddf2221SDouglas Gregor // Determine whether we're allowed to infer a module map. 487e00c8b20SDouglas Gregor 4884ddf2221SDouglas Gregor // Note: as an egregious but useful hack we use the real path here, because 4894ddf2221SDouglas Gregor // we might be looking at an embedded framework that symlinks out to a 4904ddf2221SDouglas Gregor // top-level framework, and we need to infer as if we were naming the 4914ddf2221SDouglas Gregor // top-level framework. 492e00c8b20SDouglas Gregor StringRef FrameworkDirName 4931f76c4e8SManuel Klimek = SourceMgr.getFileManager().getCanonicalName(FrameworkDir); 4944ddf2221SDouglas Gregor 4959194a91dSDouglas Gregor bool canInfer = false; 4964ddf2221SDouglas Gregor if (llvm::sys::path::has_parent_path(FrameworkDirName)) { 4979194a91dSDouglas Gregor // Figure out the parent path. 4984ddf2221SDouglas Gregor StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName); 4999194a91dSDouglas Gregor if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) { 5009194a91dSDouglas Gregor // Check whether we have already looked into the parent directory 5019194a91dSDouglas Gregor // for a module map. 502e4412640SArgyrios Kyrtzidis llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator 5039194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 5049194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) { 5059194a91dSDouglas Gregor // We haven't looked here before. Load a module map, if there is 5069194a91dSDouglas Gregor // one. 5079194a91dSDouglas Gregor SmallString<128> ModMapPath = Parent; 5089194a91dSDouglas Gregor llvm::sys::path::append(ModMapPath, "module.map"); 5099194a91dSDouglas Gregor if (const FileEntry *ModMapFile = FileMgr.getFile(ModMapPath)) { 510963c5535SDouglas Gregor parseModuleMapFile(ModMapFile, IsSystem); 5119194a91dSDouglas Gregor inferred = InferredDirectories.find(ParentDir); 5129194a91dSDouglas Gregor } 5139194a91dSDouglas Gregor 5149194a91dSDouglas Gregor if (inferred == InferredDirectories.end()) 5159194a91dSDouglas Gregor inferred = InferredDirectories.insert( 5169194a91dSDouglas Gregor std::make_pair(ParentDir, InferredDirectory())).first; 5179194a91dSDouglas Gregor } 5189194a91dSDouglas Gregor 5199194a91dSDouglas Gregor if (inferred->second.InferModules) { 5209194a91dSDouglas Gregor // We're allowed to infer for this directory, but make sure it's okay 5219194a91dSDouglas Gregor // to infer this particular module. 5224ddf2221SDouglas Gregor StringRef Name = llvm::sys::path::stem(FrameworkDirName); 5239194a91dSDouglas Gregor canInfer = std::find(inferred->second.ExcludedModules.begin(), 5249194a91dSDouglas Gregor inferred->second.ExcludedModules.end(), 5259194a91dSDouglas Gregor Name) == inferred->second.ExcludedModules.end(); 5269194a91dSDouglas Gregor 5279194a91dSDouglas Gregor if (inferred->second.InferSystemModules) 5289194a91dSDouglas Gregor IsSystem = true; 5299194a91dSDouglas Gregor } 5309194a91dSDouglas Gregor } 5319194a91dSDouglas Gregor } 5329194a91dSDouglas Gregor 5339194a91dSDouglas Gregor // If we're not allowed to infer a framework module, don't. 5349194a91dSDouglas Gregor if (!canInfer) 5359194a91dSDouglas Gregor return 0; 5369194a91dSDouglas Gregor } 5379194a91dSDouglas Gregor 5389194a91dSDouglas Gregor 53956c64013SDouglas Gregor // Look for an umbrella header. 5402c1dd271SDylan Noblesmith SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName()); 54117381a06SBenjamin Kramer llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h"); 542e89dbc1dSDouglas Gregor const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName); 54356c64013SDouglas Gregor 54456c64013SDouglas Gregor // FIXME: If there's no umbrella header, we could probably scan the 54556c64013SDouglas Gregor // framework to load *everything*. But, it's not clear that this is a good 54656c64013SDouglas Gregor // idea. 54756c64013SDouglas Gregor if (!UmbrellaHeader) 54856c64013SDouglas Gregor return 0; 54956c64013SDouglas Gregor 550e89dbc1dSDouglas Gregor Module *Result = new Module(ModuleName, SourceLocation(), Parent, 551e89dbc1dSDouglas Gregor /*IsFramework=*/true, /*IsExplicit=*/false); 552ba7f2f71SDaniel Jasper if (LangOpts.CurrentModule == ModuleName) { 553ba7f2f71SDaniel Jasper SourceModule = Result; 554ba7f2f71SDaniel Jasper SourceModuleName = ModuleName; 555ba7f2f71SDaniel Jasper } 556a686e1b0SDouglas Gregor if (IsSystem) 557a686e1b0SDouglas Gregor Result->IsSystem = IsSystem; 558a686e1b0SDouglas Gregor 559eb90e830SDouglas Gregor if (!Parent) 560e89dbc1dSDouglas Gregor Modules[ModuleName] = Result; 561e89dbc1dSDouglas Gregor 562322f633cSDouglas Gregor // umbrella header "umbrella-header-name" 56373141fa9SDouglas Gregor Result->Umbrella = UmbrellaHeader; 56497da9178SDaniel Jasper Headers[UmbrellaHeader].push_back(KnownHeader(Result, NormalHeader)); 5654dc71835SDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Result; 566d8bd7537SDouglas Gregor 567d8bd7537SDouglas Gregor // export * 568d8bd7537SDouglas Gregor Result->Exports.push_back(Module::ExportDecl(0, true)); 569d8bd7537SDouglas Gregor 570a89c5ac4SDouglas Gregor // module * { export * } 571a89c5ac4SDouglas Gregor Result->InferSubmodules = true; 572a89c5ac4SDouglas Gregor Result->InferExportWildcard = true; 573a89c5ac4SDouglas Gregor 574e89dbc1dSDouglas Gregor // Look for subframeworks. 575e89dbc1dSDouglas Gregor llvm::error_code EC; 5762c1dd271SDylan Noblesmith SmallString<128> SubframeworksDirName 577ddaa69cbSDouglas Gregor = StringRef(FrameworkDir->getName()); 578e89dbc1dSDouglas Gregor llvm::sys::path::append(SubframeworksDirName, "Frameworks"); 5792d4d8cb3SBenjamin Kramer llvm::sys::path::native(SubframeworksDirName); 580ddaa69cbSDouglas Gregor for (llvm::sys::fs::directory_iterator 5812d4d8cb3SBenjamin Kramer Dir(SubframeworksDirName.str(), EC), DirEnd; 582e89dbc1dSDouglas Gregor Dir != DirEnd && !EC; Dir.increment(EC)) { 583e89dbc1dSDouglas Gregor if (!StringRef(Dir->path()).endswith(".framework")) 584e89dbc1dSDouglas Gregor continue; 585f2161a70SDouglas Gregor 586e89dbc1dSDouglas Gregor if (const DirectoryEntry *SubframeworkDir 587e89dbc1dSDouglas Gregor = FileMgr.getDirectory(Dir->path())) { 58807c22b78SDouglas Gregor // Note: as an egregious but useful hack, we use the real path here and 58907c22b78SDouglas Gregor // check whether it is actually a subdirectory of the parent directory. 59007c22b78SDouglas Gregor // This will not be the case if the 'subframework' is actually a symlink 59107c22b78SDouglas Gregor // out to a top-level framework. 592e00c8b20SDouglas Gregor StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir); 59307c22b78SDouglas Gregor bool FoundParent = false; 59407c22b78SDouglas Gregor do { 59507c22b78SDouglas Gregor // Get the parent directory name. 59607c22b78SDouglas Gregor SubframeworkDirName 59707c22b78SDouglas Gregor = llvm::sys::path::parent_path(SubframeworkDirName); 59807c22b78SDouglas Gregor if (SubframeworkDirName.empty()) 59907c22b78SDouglas Gregor break; 60007c22b78SDouglas Gregor 60107c22b78SDouglas Gregor if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) { 60207c22b78SDouglas Gregor FoundParent = true; 60307c22b78SDouglas Gregor break; 60407c22b78SDouglas Gregor } 60507c22b78SDouglas Gregor } while (true); 60607c22b78SDouglas Gregor 60707c22b78SDouglas Gregor if (!FoundParent) 60807c22b78SDouglas Gregor continue; 60907c22b78SDouglas Gregor 610e89dbc1dSDouglas Gregor // FIXME: Do we want to warn about subframeworks without umbrella headers? 611056396aeSDouglas Gregor SmallString<32> NameBuf; 612056396aeSDouglas Gregor inferFrameworkModule(sanitizeFilenameAsIdentifier( 613056396aeSDouglas Gregor llvm::sys::path::stem(Dir->path()), NameBuf), 614056396aeSDouglas Gregor SubframeworkDir, IsSystem, Result); 615e89dbc1dSDouglas Gregor } 616e89dbc1dSDouglas Gregor } 617e89dbc1dSDouglas Gregor 61811dfe6feSDouglas Gregor // If the module is a top-level framework, automatically link against the 61911dfe6feSDouglas Gregor // framework. 62011dfe6feSDouglas Gregor if (!Result->isSubFramework()) { 62111dfe6feSDouglas Gregor inferFrameworkLink(Result, FrameworkDir, FileMgr); 62211dfe6feSDouglas Gregor } 62311dfe6feSDouglas Gregor 62456c64013SDouglas Gregor return Result; 62556c64013SDouglas Gregor } 62656c64013SDouglas Gregor 627a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ 62897da9178SDaniel Jasper Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader)); 62973141fa9SDouglas Gregor Mod->Umbrella = UmbrellaHeader; 6307033127bSDouglas Gregor UmbrellaDirs[UmbrellaHeader->getDir()] = Mod; 631a89c5ac4SDouglas Gregor } 632a89c5ac4SDouglas Gregor 633524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) { 634524e33e1SDouglas Gregor Mod->Umbrella = UmbrellaDir; 635524e33e1SDouglas Gregor UmbrellaDirs[UmbrellaDir] = Mod; 636524e33e1SDouglas Gregor } 637524e33e1SDouglas Gregor 63859527666SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header, 639b53e5483SLawrence Crowl ModuleHeaderRole Role) { 640b53e5483SLawrence Crowl if (Role == ExcludedHeader) { 64159527666SDouglas Gregor Mod->ExcludedHeaders.push_back(Header); 642b146baabSArgyrios Kyrtzidis } else { 643b53e5483SLawrence Crowl if (Role == PrivateHeader) 644b53e5483SLawrence Crowl Mod->PrivateHeaders.push_back(Header); 645b53e5483SLawrence Crowl else 646b53e5483SLawrence Crowl Mod->NormalHeaders.push_back(Header); 6476f722b4eSArgyrios Kyrtzidis bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule; 648b53e5483SLawrence Crowl HeaderInfo.MarkFileModuleHeader(Header, Role, isCompilingModuleHeader); 649b146baabSArgyrios Kyrtzidis } 65097da9178SDaniel Jasper Headers[Header].push_back(KnownHeader(Mod, Role)); 651a89c5ac4SDouglas Gregor } 652a89c5ac4SDouglas Gregor 653514b636aSDouglas Gregor const FileEntry * 654e4412640SArgyrios Kyrtzidis ModuleMap::getContainingModuleMapFile(Module *Module) const { 6551f76c4e8SManuel Klimek if (Module->DefinitionLoc.isInvalid()) 656514b636aSDouglas Gregor return 0; 657514b636aSDouglas Gregor 6581f76c4e8SManuel Klimek return SourceMgr.getFileEntryForID( 6591f76c4e8SManuel Klimek SourceMgr.getFileID(Module->DefinitionLoc)); 660514b636aSDouglas Gregor } 661514b636aSDouglas Gregor 662718292f2SDouglas Gregor void ModuleMap::dump() { 663718292f2SDouglas Gregor llvm::errs() << "Modules:"; 664718292f2SDouglas Gregor for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 665718292f2SDouglas Gregor MEnd = Modules.end(); 666718292f2SDouglas Gregor M != MEnd; ++M) 667d28d1b8dSDouglas Gregor M->getValue()->print(llvm::errs(), 2); 668718292f2SDouglas Gregor 669718292f2SDouglas Gregor llvm::errs() << "Headers:"; 67059527666SDouglas Gregor for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end(); 671718292f2SDouglas Gregor H != HEnd; ++H) { 67297da9178SDaniel Jasper llvm::errs() << " \"" << H->first->getName() << "\" -> "; 67397da9178SDaniel Jasper for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(), 67497da9178SDaniel Jasper E = H->second.end(); 67597da9178SDaniel Jasper I != E; ++I) { 67697da9178SDaniel Jasper if (I != H->second.begin()) 67797da9178SDaniel Jasper llvm::errs() << ","; 67897da9178SDaniel Jasper llvm::errs() << I->getModule()->getFullModuleName(); 67997da9178SDaniel Jasper } 68097da9178SDaniel Jasper llvm::errs() << "\n"; 681718292f2SDouglas Gregor } 682718292f2SDouglas Gregor } 683718292f2SDouglas Gregor 6842b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 6852b82c2a5SDouglas Gregor bool HadError = false; 6862b82c2a5SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { 6872b82c2a5SDouglas Gregor Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 6882b82c2a5SDouglas Gregor Complain); 689f5eedd05SDouglas Gregor if (Export.getPointer() || Export.getInt()) 6902b82c2a5SDouglas Gregor Mod->Exports.push_back(Export); 6912b82c2a5SDouglas Gregor else 6922b82c2a5SDouglas Gregor HadError = true; 6932b82c2a5SDouglas Gregor } 6942b82c2a5SDouglas Gregor Mod->UnresolvedExports.clear(); 6952b82c2a5SDouglas Gregor return HadError; 6962b82c2a5SDouglas Gregor } 6972b82c2a5SDouglas Gregor 698ba7f2f71SDaniel Jasper bool ModuleMap::resolveUses(Module *Mod, bool Complain) { 699ba7f2f71SDaniel Jasper bool HadError = false; 700ba7f2f71SDaniel Jasper for (unsigned I = 0, N = Mod->UnresolvedDirectUses.size(); I != N; ++I) { 701ba7f2f71SDaniel Jasper Module *DirectUse = 702ba7f2f71SDaniel Jasper resolveModuleId(Mod->UnresolvedDirectUses[I], Mod, Complain); 703ba7f2f71SDaniel Jasper if (DirectUse) 704ba7f2f71SDaniel Jasper Mod->DirectUses.push_back(DirectUse); 705ba7f2f71SDaniel Jasper else 706ba7f2f71SDaniel Jasper HadError = true; 707ba7f2f71SDaniel Jasper } 708ba7f2f71SDaniel Jasper Mod->UnresolvedDirectUses.clear(); 709ba7f2f71SDaniel Jasper return HadError; 710ba7f2f71SDaniel Jasper } 711ba7f2f71SDaniel Jasper 712fb912657SDouglas Gregor bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) { 713fb912657SDouglas Gregor bool HadError = false; 714fb912657SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) { 715fb912657SDouglas Gregor Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id, 716fb912657SDouglas Gregor Mod, Complain); 717fb912657SDouglas Gregor if (!OtherMod) { 718fb912657SDouglas Gregor HadError = true; 719fb912657SDouglas Gregor continue; 720fb912657SDouglas Gregor } 721fb912657SDouglas Gregor 722fb912657SDouglas Gregor Module::Conflict Conflict; 723fb912657SDouglas Gregor Conflict.Other = OtherMod; 724fb912657SDouglas Gregor Conflict.Message = Mod->UnresolvedConflicts[I].Message; 725fb912657SDouglas Gregor Mod->Conflicts.push_back(Conflict); 726fb912657SDouglas Gregor } 727fb912657SDouglas Gregor Mod->UnresolvedConflicts.clear(); 728fb912657SDouglas Gregor return HadError; 729fb912657SDouglas Gregor } 730fb912657SDouglas Gregor 7310093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 7320093b3c7SDouglas Gregor if (Loc.isInvalid()) 7330093b3c7SDouglas Gregor return 0; 7340093b3c7SDouglas Gregor 7350093b3c7SDouglas Gregor // Use the expansion location to determine which module we're in. 7360093b3c7SDouglas Gregor FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 7370093b3c7SDouglas Gregor if (!ExpansionLoc.isFileID()) 7380093b3c7SDouglas Gregor return 0; 7390093b3c7SDouglas Gregor 7400093b3c7SDouglas Gregor 7410093b3c7SDouglas Gregor const SourceManager &SrcMgr = Loc.getManager(); 7420093b3c7SDouglas Gregor FileID ExpansionFileID = ExpansionLoc.getFileID(); 743224d8a74SDouglas Gregor 744224d8a74SDouglas Gregor while (const FileEntry *ExpansionFile 745224d8a74SDouglas Gregor = SrcMgr.getFileEntryForID(ExpansionFileID)) { 746224d8a74SDouglas Gregor // Find the module that owns this header (if any). 747b53e5483SLawrence Crowl if (Module *Mod = findModuleForHeader(ExpansionFile).getModule()) 748224d8a74SDouglas Gregor return Mod; 749224d8a74SDouglas Gregor 750224d8a74SDouglas Gregor // No module owns this header, so look up the inclusion chain to see if 751224d8a74SDouglas Gregor // any included header has an associated module. 752224d8a74SDouglas Gregor SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID); 753224d8a74SDouglas Gregor if (IncludeLoc.isInvalid()) 7540093b3c7SDouglas Gregor return 0; 7550093b3c7SDouglas Gregor 756224d8a74SDouglas Gregor ExpansionFileID = SrcMgr.getFileID(IncludeLoc); 757224d8a74SDouglas Gregor } 758224d8a74SDouglas Gregor 759224d8a74SDouglas Gregor return 0; 7600093b3c7SDouglas Gregor } 7610093b3c7SDouglas Gregor 762718292f2SDouglas Gregor //----------------------------------------------------------------------------// 763718292f2SDouglas Gregor // Module map file parser 764718292f2SDouglas Gregor //----------------------------------------------------------------------------// 765718292f2SDouglas Gregor 766718292f2SDouglas Gregor namespace clang { 767718292f2SDouglas Gregor /// \brief A token in a module map file. 768718292f2SDouglas Gregor struct MMToken { 769718292f2SDouglas Gregor enum TokenKind { 7701fb5c3a6SDouglas Gregor Comma, 77135b13eceSDouglas Gregor ConfigMacros, 772fb912657SDouglas Gregor Conflict, 773718292f2SDouglas Gregor EndOfFile, 774718292f2SDouglas Gregor HeaderKeyword, 775718292f2SDouglas Gregor Identifier, 776*a3feee2aSRichard Smith Exclaim, 77759527666SDouglas Gregor ExcludeKeyword, 778718292f2SDouglas Gregor ExplicitKeyword, 7792b82c2a5SDouglas Gregor ExportKeyword, 78097292843SDaniel Jasper ExternKeyword, 781755b2055SDouglas Gregor FrameworkKeyword, 7826ddfca91SDouglas Gregor LinkKeyword, 783718292f2SDouglas Gregor ModuleKeyword, 7842b82c2a5SDouglas Gregor Period, 785b53e5483SLawrence Crowl PrivateKeyword, 786718292f2SDouglas Gregor UmbrellaKeyword, 787ba7f2f71SDaniel Jasper UseKeyword, 7881fb5c3a6SDouglas Gregor RequiresKeyword, 7892b82c2a5SDouglas Gregor Star, 790718292f2SDouglas Gregor StringLiteral, 791718292f2SDouglas Gregor LBrace, 792a686e1b0SDouglas Gregor RBrace, 793a686e1b0SDouglas Gregor LSquare, 794a686e1b0SDouglas Gregor RSquare 795718292f2SDouglas Gregor } Kind; 796718292f2SDouglas Gregor 797718292f2SDouglas Gregor unsigned Location; 798718292f2SDouglas Gregor unsigned StringLength; 799718292f2SDouglas Gregor const char *StringData; 800718292f2SDouglas Gregor 801718292f2SDouglas Gregor void clear() { 802718292f2SDouglas Gregor Kind = EndOfFile; 803718292f2SDouglas Gregor Location = 0; 804718292f2SDouglas Gregor StringLength = 0; 805718292f2SDouglas Gregor StringData = 0; 806718292f2SDouglas Gregor } 807718292f2SDouglas Gregor 808718292f2SDouglas Gregor bool is(TokenKind K) const { return Kind == K; } 809718292f2SDouglas Gregor 810718292f2SDouglas Gregor SourceLocation getLocation() const { 811718292f2SDouglas Gregor return SourceLocation::getFromRawEncoding(Location); 812718292f2SDouglas Gregor } 813718292f2SDouglas Gregor 814718292f2SDouglas Gregor StringRef getString() const { 815718292f2SDouglas Gregor return StringRef(StringData, StringLength); 816718292f2SDouglas Gregor } 817718292f2SDouglas Gregor }; 818718292f2SDouglas Gregor 8199194a91dSDouglas Gregor /// \brief The set of attributes that can be attached to a module. 8204442605fSBill Wendling struct Attributes { 82135b13eceSDouglas Gregor Attributes() : IsSystem(), IsExhaustive() { } 8229194a91dSDouglas Gregor 8239194a91dSDouglas Gregor /// \brief Whether this is a system module. 8249194a91dSDouglas Gregor unsigned IsSystem : 1; 82535b13eceSDouglas Gregor 82635b13eceSDouglas Gregor /// \brief Whether this is an exhaustive set of configuration macros. 82735b13eceSDouglas Gregor unsigned IsExhaustive : 1; 8289194a91dSDouglas Gregor }; 8299194a91dSDouglas Gregor 8309194a91dSDouglas Gregor 831718292f2SDouglas Gregor class ModuleMapParser { 832718292f2SDouglas Gregor Lexer &L; 833718292f2SDouglas Gregor SourceManager &SourceMgr; 834bc10b9fbSDouglas Gregor 835bc10b9fbSDouglas Gregor /// \brief Default target information, used only for string literal 836bc10b9fbSDouglas Gregor /// parsing. 837bc10b9fbSDouglas Gregor const TargetInfo *Target; 838bc10b9fbSDouglas Gregor 839718292f2SDouglas Gregor DiagnosticsEngine &Diags; 840718292f2SDouglas Gregor ModuleMap ⤅ 841718292f2SDouglas Gregor 8425257fc63SDouglas Gregor /// \brief The directory that this module map resides in. 8435257fc63SDouglas Gregor const DirectoryEntry *Directory; 8445257fc63SDouglas Gregor 8453ec6663bSDouglas Gregor /// \brief The directory containing Clang-supplied headers. 8463ec6663bSDouglas Gregor const DirectoryEntry *BuiltinIncludeDir; 8473ec6663bSDouglas Gregor 848963c5535SDouglas Gregor /// \brief Whether this module map is in a system header directory. 849963c5535SDouglas Gregor bool IsSystem; 850963c5535SDouglas Gregor 851718292f2SDouglas Gregor /// \brief Whether an error occurred. 852718292f2SDouglas Gregor bool HadError; 853718292f2SDouglas Gregor 854718292f2SDouglas Gregor /// \brief Stores string data for the various string literals referenced 855718292f2SDouglas Gregor /// during parsing. 856718292f2SDouglas Gregor llvm::BumpPtrAllocator StringData; 857718292f2SDouglas Gregor 858718292f2SDouglas Gregor /// \brief The current token. 859718292f2SDouglas Gregor MMToken Tok; 860718292f2SDouglas Gregor 861718292f2SDouglas Gregor /// \brief The active module. 862de3ef502SDouglas Gregor Module *ActiveModule; 863718292f2SDouglas Gregor 864718292f2SDouglas Gregor /// \brief Consume the current token and return its location. 865718292f2SDouglas Gregor SourceLocation consumeToken(); 866718292f2SDouglas Gregor 867718292f2SDouglas Gregor /// \brief Skip tokens until we reach the a token with the given kind 868718292f2SDouglas Gregor /// (or the end of the file). 869718292f2SDouglas Gregor void skipUntil(MMToken::TokenKind K); 870718292f2SDouglas Gregor 871f857950dSDmitri Gribenko typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; 872e7ab3669SDouglas Gregor bool parseModuleId(ModuleId &Id); 873718292f2SDouglas Gregor void parseModuleDecl(); 87497292843SDaniel Jasper void parseExternModuleDecl(); 8751fb5c3a6SDouglas Gregor void parseRequiresDecl(); 876b53e5483SLawrence Crowl void parseHeaderDecl(clang::MMToken::TokenKind, 877b53e5483SLawrence Crowl SourceLocation LeadingLoc); 878524e33e1SDouglas Gregor void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc); 8792b82c2a5SDouglas Gregor void parseExportDecl(); 880ba7f2f71SDaniel Jasper void parseUseDecl(); 8816ddfca91SDouglas Gregor void parseLinkDecl(); 88235b13eceSDouglas Gregor void parseConfigMacros(); 883fb912657SDouglas Gregor void parseConflict(); 8849194a91dSDouglas Gregor void parseInferredModuleDecl(bool Framework, bool Explicit); 8854442605fSBill Wendling bool parseOptionalAttributes(Attributes &Attrs); 886718292f2SDouglas Gregor 8877033127bSDouglas Gregor const DirectoryEntry *getOverriddenHeaderSearchDir(); 8887033127bSDouglas Gregor 889718292f2SDouglas Gregor public: 890718292f2SDouglas Gregor explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 891bc10b9fbSDouglas Gregor const TargetInfo *Target, 892718292f2SDouglas Gregor DiagnosticsEngine &Diags, 8935257fc63SDouglas Gregor ModuleMap &Map, 8943ec6663bSDouglas Gregor const DirectoryEntry *Directory, 895963c5535SDouglas Gregor const DirectoryEntry *BuiltinIncludeDir, 896963c5535SDouglas Gregor bool IsSystem) 897bc10b9fbSDouglas Gregor : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 8983ec6663bSDouglas Gregor Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir), 899963c5535SDouglas Gregor IsSystem(IsSystem), HadError(false), ActiveModule(0) 900718292f2SDouglas Gregor { 901718292f2SDouglas Gregor Tok.clear(); 902718292f2SDouglas Gregor consumeToken(); 903718292f2SDouglas Gregor } 904718292f2SDouglas Gregor 905718292f2SDouglas Gregor bool parseModuleMapFile(); 906718292f2SDouglas Gregor }; 907718292f2SDouglas Gregor } 908718292f2SDouglas Gregor 909718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() { 910718292f2SDouglas Gregor retry: 911718292f2SDouglas Gregor SourceLocation Result = Tok.getLocation(); 912718292f2SDouglas Gregor Tok.clear(); 913718292f2SDouglas Gregor 914718292f2SDouglas Gregor Token LToken; 915718292f2SDouglas Gregor L.LexFromRawLexer(LToken); 916718292f2SDouglas Gregor Tok.Location = LToken.getLocation().getRawEncoding(); 917718292f2SDouglas Gregor switch (LToken.getKind()) { 918718292f2SDouglas Gregor case tok::raw_identifier: 919718292f2SDouglas Gregor Tok.StringData = LToken.getRawIdentifierData(); 920718292f2SDouglas Gregor Tok.StringLength = LToken.getLength(); 921718292f2SDouglas Gregor Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString()) 92235b13eceSDouglas Gregor .Case("config_macros", MMToken::ConfigMacros) 923fb912657SDouglas Gregor .Case("conflict", MMToken::Conflict) 92459527666SDouglas Gregor .Case("exclude", MMToken::ExcludeKeyword) 925718292f2SDouglas Gregor .Case("explicit", MMToken::ExplicitKeyword) 9262b82c2a5SDouglas Gregor .Case("export", MMToken::ExportKeyword) 92797292843SDaniel Jasper .Case("extern", MMToken::ExternKeyword) 928755b2055SDouglas Gregor .Case("framework", MMToken::FrameworkKeyword) 92935b13eceSDouglas Gregor .Case("header", MMToken::HeaderKeyword) 9306ddfca91SDouglas Gregor .Case("link", MMToken::LinkKeyword) 931718292f2SDouglas Gregor .Case("module", MMToken::ModuleKeyword) 932b53e5483SLawrence Crowl .Case("private", MMToken::PrivateKeyword) 9331fb5c3a6SDouglas Gregor .Case("requires", MMToken::RequiresKeyword) 934718292f2SDouglas Gregor .Case("umbrella", MMToken::UmbrellaKeyword) 935ba7f2f71SDaniel Jasper .Case("use", MMToken::UseKeyword) 936718292f2SDouglas Gregor .Default(MMToken::Identifier); 937718292f2SDouglas Gregor break; 938718292f2SDouglas Gregor 9391fb5c3a6SDouglas Gregor case tok::comma: 9401fb5c3a6SDouglas Gregor Tok.Kind = MMToken::Comma; 9411fb5c3a6SDouglas Gregor break; 9421fb5c3a6SDouglas Gregor 943718292f2SDouglas Gregor case tok::eof: 944718292f2SDouglas Gregor Tok.Kind = MMToken::EndOfFile; 945718292f2SDouglas Gregor break; 946718292f2SDouglas Gregor 947718292f2SDouglas Gregor case tok::l_brace: 948718292f2SDouglas Gregor Tok.Kind = MMToken::LBrace; 949718292f2SDouglas Gregor break; 950718292f2SDouglas Gregor 951a686e1b0SDouglas Gregor case tok::l_square: 952a686e1b0SDouglas Gregor Tok.Kind = MMToken::LSquare; 953a686e1b0SDouglas Gregor break; 954a686e1b0SDouglas Gregor 9552b82c2a5SDouglas Gregor case tok::period: 9562b82c2a5SDouglas Gregor Tok.Kind = MMToken::Period; 9572b82c2a5SDouglas Gregor break; 9582b82c2a5SDouglas Gregor 959718292f2SDouglas Gregor case tok::r_brace: 960718292f2SDouglas Gregor Tok.Kind = MMToken::RBrace; 961718292f2SDouglas Gregor break; 962718292f2SDouglas Gregor 963a686e1b0SDouglas Gregor case tok::r_square: 964a686e1b0SDouglas Gregor Tok.Kind = MMToken::RSquare; 965a686e1b0SDouglas Gregor break; 966a686e1b0SDouglas Gregor 9672b82c2a5SDouglas Gregor case tok::star: 9682b82c2a5SDouglas Gregor Tok.Kind = MMToken::Star; 9692b82c2a5SDouglas Gregor break; 9702b82c2a5SDouglas Gregor 971*a3feee2aSRichard Smith case tok::exclaim: 972*a3feee2aSRichard Smith Tok.Kind = MMToken::Exclaim; 973*a3feee2aSRichard Smith break; 974*a3feee2aSRichard Smith 975718292f2SDouglas Gregor case tok::string_literal: { 976d67aea28SRichard Smith if (LToken.hasUDSuffix()) { 977d67aea28SRichard Smith Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl); 978d67aea28SRichard Smith HadError = true; 979d67aea28SRichard Smith goto retry; 980d67aea28SRichard Smith } 981d67aea28SRichard Smith 982718292f2SDouglas Gregor // Parse the string literal. 983718292f2SDouglas Gregor LangOptions LangOpts; 984718292f2SDouglas Gregor StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target); 985718292f2SDouglas Gregor if (StringLiteral.hadError) 986718292f2SDouglas Gregor goto retry; 987718292f2SDouglas Gregor 988718292f2SDouglas Gregor // Copy the string literal into our string data allocator. 989718292f2SDouglas Gregor unsigned Length = StringLiteral.GetStringLength(); 990718292f2SDouglas Gregor char *Saved = StringData.Allocate<char>(Length + 1); 991718292f2SDouglas Gregor memcpy(Saved, StringLiteral.GetString().data(), Length); 992718292f2SDouglas Gregor Saved[Length] = 0; 993718292f2SDouglas Gregor 994718292f2SDouglas Gregor // Form the token. 995718292f2SDouglas Gregor Tok.Kind = MMToken::StringLiteral; 996718292f2SDouglas Gregor Tok.StringData = Saved; 997718292f2SDouglas Gregor Tok.StringLength = Length; 998718292f2SDouglas Gregor break; 999718292f2SDouglas Gregor } 1000718292f2SDouglas Gregor 1001718292f2SDouglas Gregor case tok::comment: 1002718292f2SDouglas Gregor goto retry; 1003718292f2SDouglas Gregor 1004718292f2SDouglas Gregor default: 1005718292f2SDouglas Gregor Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 1006718292f2SDouglas Gregor HadError = true; 1007718292f2SDouglas Gregor goto retry; 1008718292f2SDouglas Gregor } 1009718292f2SDouglas Gregor 1010718292f2SDouglas Gregor return Result; 1011718292f2SDouglas Gregor } 1012718292f2SDouglas Gregor 1013718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 1014718292f2SDouglas Gregor unsigned braceDepth = 0; 1015a686e1b0SDouglas Gregor unsigned squareDepth = 0; 1016718292f2SDouglas Gregor do { 1017718292f2SDouglas Gregor switch (Tok.Kind) { 1018718292f2SDouglas Gregor case MMToken::EndOfFile: 1019718292f2SDouglas Gregor return; 1020718292f2SDouglas Gregor 1021718292f2SDouglas Gregor case MMToken::LBrace: 1022a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1023718292f2SDouglas Gregor return; 1024718292f2SDouglas Gregor 1025718292f2SDouglas Gregor ++braceDepth; 1026718292f2SDouglas Gregor break; 1027718292f2SDouglas Gregor 1028a686e1b0SDouglas Gregor case MMToken::LSquare: 1029a686e1b0SDouglas Gregor if (Tok.is(K) && braceDepth == 0 && squareDepth == 0) 1030a686e1b0SDouglas Gregor return; 1031a686e1b0SDouglas Gregor 1032a686e1b0SDouglas Gregor ++squareDepth; 1033a686e1b0SDouglas Gregor break; 1034a686e1b0SDouglas Gregor 1035718292f2SDouglas Gregor case MMToken::RBrace: 1036718292f2SDouglas Gregor if (braceDepth > 0) 1037718292f2SDouglas Gregor --braceDepth; 1038718292f2SDouglas Gregor else if (Tok.is(K)) 1039718292f2SDouglas Gregor return; 1040718292f2SDouglas Gregor break; 1041718292f2SDouglas Gregor 1042a686e1b0SDouglas Gregor case MMToken::RSquare: 1043a686e1b0SDouglas Gregor if (squareDepth > 0) 1044a686e1b0SDouglas Gregor --squareDepth; 1045a686e1b0SDouglas Gregor else if (Tok.is(K)) 1046a686e1b0SDouglas Gregor return; 1047a686e1b0SDouglas Gregor break; 1048a686e1b0SDouglas Gregor 1049718292f2SDouglas Gregor default: 1050a686e1b0SDouglas Gregor if (braceDepth == 0 && squareDepth == 0 && Tok.is(K)) 1051718292f2SDouglas Gregor return; 1052718292f2SDouglas Gregor break; 1053718292f2SDouglas Gregor } 1054718292f2SDouglas Gregor 1055718292f2SDouglas Gregor consumeToken(); 1056718292f2SDouglas Gregor } while (true); 1057718292f2SDouglas Gregor } 1058718292f2SDouglas Gregor 1059e7ab3669SDouglas Gregor /// \brief Parse a module-id. 1060e7ab3669SDouglas Gregor /// 1061e7ab3669SDouglas Gregor /// module-id: 1062e7ab3669SDouglas Gregor /// identifier 1063e7ab3669SDouglas Gregor /// identifier '.' module-id 1064e7ab3669SDouglas Gregor /// 1065e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise. 1066e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) { 1067e7ab3669SDouglas Gregor Id.clear(); 1068e7ab3669SDouglas Gregor do { 1069e7ab3669SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 1070e7ab3669SDouglas Gregor Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation())); 1071e7ab3669SDouglas Gregor consumeToken(); 1072e7ab3669SDouglas Gregor } else { 1073e7ab3669SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 1074e7ab3669SDouglas Gregor return true; 1075e7ab3669SDouglas Gregor } 1076e7ab3669SDouglas Gregor 1077e7ab3669SDouglas Gregor if (!Tok.is(MMToken::Period)) 1078e7ab3669SDouglas Gregor break; 1079e7ab3669SDouglas Gregor 1080e7ab3669SDouglas Gregor consumeToken(); 1081e7ab3669SDouglas Gregor } while (true); 1082e7ab3669SDouglas Gregor 1083e7ab3669SDouglas Gregor return false; 1084e7ab3669SDouglas Gregor } 1085e7ab3669SDouglas Gregor 1086a686e1b0SDouglas Gregor namespace { 1087a686e1b0SDouglas Gregor /// \brief Enumerates the known attributes. 1088a686e1b0SDouglas Gregor enum AttributeKind { 1089a686e1b0SDouglas Gregor /// \brief An unknown attribute. 1090a686e1b0SDouglas Gregor AT_unknown, 1091a686e1b0SDouglas Gregor /// \brief The 'system' attribute. 109235b13eceSDouglas Gregor AT_system, 109335b13eceSDouglas Gregor /// \brief The 'exhaustive' attribute. 109435b13eceSDouglas Gregor AT_exhaustive 1095a686e1b0SDouglas Gregor }; 1096a686e1b0SDouglas Gregor } 1097a686e1b0SDouglas Gregor 1098718292f2SDouglas Gregor /// \brief Parse a module declaration. 1099718292f2SDouglas Gregor /// 1100718292f2SDouglas Gregor /// module-declaration: 110197292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 1102a686e1b0SDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 1103a686e1b0SDouglas Gregor /// { module-member* } 1104a686e1b0SDouglas Gregor /// 1105718292f2SDouglas Gregor /// module-member: 11061fb5c3a6SDouglas Gregor /// requires-declaration 1107718292f2SDouglas Gregor /// header-declaration 1108e7ab3669SDouglas Gregor /// submodule-declaration 11092b82c2a5SDouglas Gregor /// export-declaration 11106ddfca91SDouglas Gregor /// link-declaration 111173441091SDouglas Gregor /// 111273441091SDouglas Gregor /// submodule-declaration: 111373441091SDouglas Gregor /// module-declaration 111473441091SDouglas Gregor /// inferred-submodule-declaration 1115718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() { 1116755b2055SDouglas Gregor assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 111797292843SDaniel Jasper Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword)); 111897292843SDaniel Jasper if (Tok.is(MMToken::ExternKeyword)) { 111997292843SDaniel Jasper parseExternModuleDecl(); 112097292843SDaniel Jasper return; 112197292843SDaniel Jasper } 112297292843SDaniel Jasper 1123f2161a70SDouglas Gregor // Parse 'explicit' or 'framework' keyword, if present. 1124e7ab3669SDouglas Gregor SourceLocation ExplicitLoc; 1125718292f2SDouglas Gregor bool Explicit = false; 1126f2161a70SDouglas Gregor bool Framework = false; 1127755b2055SDouglas Gregor 1128f2161a70SDouglas Gregor // Parse 'explicit' keyword, if present. 1129f2161a70SDouglas Gregor if (Tok.is(MMToken::ExplicitKeyword)) { 1130e7ab3669SDouglas Gregor ExplicitLoc = consumeToken(); 1131f2161a70SDouglas Gregor Explicit = true; 1132f2161a70SDouglas Gregor } 1133f2161a70SDouglas Gregor 1134f2161a70SDouglas Gregor // Parse 'framework' keyword, if present. 1135755b2055SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 1136755b2055SDouglas Gregor consumeToken(); 1137755b2055SDouglas Gregor Framework = true; 1138755b2055SDouglas Gregor } 1139718292f2SDouglas Gregor 1140718292f2SDouglas Gregor // Parse 'module' keyword. 1141718292f2SDouglas Gregor if (!Tok.is(MMToken::ModuleKeyword)) { 1142d6343c99SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1143718292f2SDouglas Gregor consumeToken(); 1144718292f2SDouglas Gregor HadError = true; 1145718292f2SDouglas Gregor return; 1146718292f2SDouglas Gregor } 1147718292f2SDouglas Gregor consumeToken(); // 'module' keyword 1148718292f2SDouglas Gregor 114973441091SDouglas Gregor // If we have a wildcard for the module name, this is an inferred submodule. 115073441091SDouglas Gregor // Parse it. 115173441091SDouglas Gregor if (Tok.is(MMToken::Star)) 11529194a91dSDouglas Gregor return parseInferredModuleDecl(Framework, Explicit); 115373441091SDouglas Gregor 1154718292f2SDouglas Gregor // Parse the module name. 1155e7ab3669SDouglas Gregor ModuleId Id; 1156e7ab3669SDouglas Gregor if (parseModuleId(Id)) { 1157718292f2SDouglas Gregor HadError = true; 1158718292f2SDouglas Gregor return; 1159718292f2SDouglas Gregor } 1160e7ab3669SDouglas Gregor 1161e7ab3669SDouglas Gregor if (ActiveModule) { 1162e7ab3669SDouglas Gregor if (Id.size() > 1) { 1163e7ab3669SDouglas Gregor Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id) 1164e7ab3669SDouglas Gregor << SourceRange(Id.front().second, Id.back().second); 1165e7ab3669SDouglas Gregor 1166e7ab3669SDouglas Gregor HadError = true; 1167e7ab3669SDouglas Gregor return; 1168e7ab3669SDouglas Gregor } 1169e7ab3669SDouglas Gregor } else if (Id.size() == 1 && Explicit) { 1170e7ab3669SDouglas Gregor // Top-level modules can't be explicit. 1171e7ab3669SDouglas Gregor Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level); 1172e7ab3669SDouglas Gregor Explicit = false; 1173e7ab3669SDouglas Gregor ExplicitLoc = SourceLocation(); 1174e7ab3669SDouglas Gregor HadError = true; 1175e7ab3669SDouglas Gregor } 1176e7ab3669SDouglas Gregor 1177e7ab3669SDouglas Gregor Module *PreviousActiveModule = ActiveModule; 1178e7ab3669SDouglas Gregor if (Id.size() > 1) { 1179e7ab3669SDouglas Gregor // This module map defines a submodule. Go find the module of which it 1180e7ab3669SDouglas Gregor // is a submodule. 1181e7ab3669SDouglas Gregor ActiveModule = 0; 1182e7ab3669SDouglas Gregor for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) { 1183e7ab3669SDouglas Gregor if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) { 1184e7ab3669SDouglas Gregor ActiveModule = Next; 1185e7ab3669SDouglas Gregor continue; 1186e7ab3669SDouglas Gregor } 1187e7ab3669SDouglas Gregor 1188e7ab3669SDouglas Gregor if (ActiveModule) { 1189e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified) 1190e7ab3669SDouglas Gregor << Id[I].first << ActiveModule->getTopLevelModule(); 1191e7ab3669SDouglas Gregor } else { 1192e7ab3669SDouglas Gregor Diags.Report(Id[I].second, diag::err_mmap_expected_module_name); 1193e7ab3669SDouglas Gregor } 1194e7ab3669SDouglas Gregor HadError = true; 1195e7ab3669SDouglas Gregor return; 1196e7ab3669SDouglas Gregor } 1197e7ab3669SDouglas Gregor } 1198e7ab3669SDouglas Gregor 1199e7ab3669SDouglas Gregor StringRef ModuleName = Id.back().first; 1200e7ab3669SDouglas Gregor SourceLocation ModuleNameLoc = Id.back().second; 1201718292f2SDouglas Gregor 1202a686e1b0SDouglas Gregor // Parse the optional attribute list. 12034442605fSBill Wendling Attributes Attrs; 12049194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 1205a686e1b0SDouglas Gregor 1206718292f2SDouglas Gregor // Parse the opening brace. 1207718292f2SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 1208718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 1209718292f2SDouglas Gregor << ModuleName; 1210718292f2SDouglas Gregor HadError = true; 1211718292f2SDouglas Gregor return; 1212718292f2SDouglas Gregor } 1213718292f2SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 1214718292f2SDouglas Gregor 1215718292f2SDouglas Gregor // Determine whether this (sub)module has already been defined. 1216eb90e830SDouglas Gregor if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) { 1217fcc54a3bSDouglas Gregor if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) { 1218fcc54a3bSDouglas Gregor // Skip the module definition. 1219fcc54a3bSDouglas Gregor skipUntil(MMToken::RBrace); 1220fcc54a3bSDouglas Gregor if (Tok.is(MMToken::RBrace)) 1221fcc54a3bSDouglas Gregor consumeToken(); 1222fcc54a3bSDouglas Gregor else { 1223fcc54a3bSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1224fcc54a3bSDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1225fcc54a3bSDouglas Gregor HadError = true; 1226fcc54a3bSDouglas Gregor } 1227fcc54a3bSDouglas Gregor return; 1228fcc54a3bSDouglas Gregor } 1229fcc54a3bSDouglas Gregor 1230718292f2SDouglas Gregor Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 1231718292f2SDouglas Gregor << ModuleName; 1232eb90e830SDouglas Gregor Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition); 1233718292f2SDouglas Gregor 1234718292f2SDouglas Gregor // Skip the module definition. 1235718292f2SDouglas Gregor skipUntil(MMToken::RBrace); 1236718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1237718292f2SDouglas Gregor consumeToken(); 1238718292f2SDouglas Gregor 1239718292f2SDouglas Gregor HadError = true; 1240718292f2SDouglas Gregor return; 1241718292f2SDouglas Gregor } 1242718292f2SDouglas Gregor 1243718292f2SDouglas Gregor // Start defining this module. 1244eb90e830SDouglas Gregor ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework, 1245eb90e830SDouglas Gregor Explicit).first; 1246eb90e830SDouglas Gregor ActiveModule->DefinitionLoc = ModuleNameLoc; 1247963c5535SDouglas Gregor if (Attrs.IsSystem || IsSystem) 1248a686e1b0SDouglas Gregor ActiveModule->IsSystem = true; 1249718292f2SDouglas Gregor 1250718292f2SDouglas Gregor bool Done = false; 1251718292f2SDouglas Gregor do { 1252718292f2SDouglas Gregor switch (Tok.Kind) { 1253718292f2SDouglas Gregor case MMToken::EndOfFile: 1254718292f2SDouglas Gregor case MMToken::RBrace: 1255718292f2SDouglas Gregor Done = true; 1256718292f2SDouglas Gregor break; 1257718292f2SDouglas Gregor 125835b13eceSDouglas Gregor case MMToken::ConfigMacros: 125935b13eceSDouglas Gregor parseConfigMacros(); 126035b13eceSDouglas Gregor break; 126135b13eceSDouglas Gregor 1262fb912657SDouglas Gregor case MMToken::Conflict: 1263fb912657SDouglas Gregor parseConflict(); 1264fb912657SDouglas Gregor break; 1265fb912657SDouglas Gregor 1266718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 126797292843SDaniel Jasper case MMToken::ExternKeyword: 1268f2161a70SDouglas Gregor case MMToken::FrameworkKeyword: 1269718292f2SDouglas Gregor case MMToken::ModuleKeyword: 1270718292f2SDouglas Gregor parseModuleDecl(); 1271718292f2SDouglas Gregor break; 1272718292f2SDouglas Gregor 12732b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 12742b82c2a5SDouglas Gregor parseExportDecl(); 12752b82c2a5SDouglas Gregor break; 12762b82c2a5SDouglas Gregor 1277ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 1278ba7f2f71SDaniel Jasper parseUseDecl(); 1279ba7f2f71SDaniel Jasper break; 1280ba7f2f71SDaniel Jasper 12811fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 12821fb5c3a6SDouglas Gregor parseRequiresDecl(); 12831fb5c3a6SDouglas Gregor break; 12841fb5c3a6SDouglas Gregor 1285524e33e1SDouglas Gregor case MMToken::UmbrellaKeyword: { 1286524e33e1SDouglas Gregor SourceLocation UmbrellaLoc = consumeToken(); 1287524e33e1SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) 1288b53e5483SLawrence Crowl parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc); 1289524e33e1SDouglas Gregor else 1290524e33e1SDouglas Gregor parseUmbrellaDirDecl(UmbrellaLoc); 1291718292f2SDouglas Gregor break; 1292524e33e1SDouglas Gregor } 1293718292f2SDouglas Gregor 129459527666SDouglas Gregor case MMToken::ExcludeKeyword: { 129559527666SDouglas Gregor SourceLocation ExcludeLoc = consumeToken(); 129659527666SDouglas Gregor if (Tok.is(MMToken::HeaderKeyword)) { 1297b53e5483SLawrence Crowl parseHeaderDecl(MMToken::ExcludeKeyword, ExcludeLoc); 129859527666SDouglas Gregor } else { 129959527666SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 130059527666SDouglas Gregor << "exclude"; 130159527666SDouglas Gregor } 130259527666SDouglas Gregor break; 130359527666SDouglas Gregor } 130459527666SDouglas Gregor 1305b53e5483SLawrence Crowl case MMToken::PrivateKeyword: { 1306b53e5483SLawrence Crowl SourceLocation PrivateLoc = consumeToken(); 1307b53e5483SLawrence Crowl if (Tok.is(MMToken::HeaderKeyword)) { 1308b53e5483SLawrence Crowl parseHeaderDecl(MMToken::PrivateKeyword, PrivateLoc); 1309b53e5483SLawrence Crowl } else { 1310b53e5483SLawrence Crowl Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1311b53e5483SLawrence Crowl << "private"; 1312b53e5483SLawrence Crowl } 1313b53e5483SLawrence Crowl break; 1314b53e5483SLawrence Crowl } 1315b53e5483SLawrence Crowl 1316322f633cSDouglas Gregor case MMToken::HeaderKeyword: 1317b53e5483SLawrence Crowl parseHeaderDecl(MMToken::HeaderKeyword, SourceLocation()); 1318718292f2SDouglas Gregor break; 1319718292f2SDouglas Gregor 13206ddfca91SDouglas Gregor case MMToken::LinkKeyword: 13216ddfca91SDouglas Gregor parseLinkDecl(); 13226ddfca91SDouglas Gregor break; 13236ddfca91SDouglas Gregor 1324718292f2SDouglas Gregor default: 1325718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 1326718292f2SDouglas Gregor consumeToken(); 1327718292f2SDouglas Gregor break; 1328718292f2SDouglas Gregor } 1329718292f2SDouglas Gregor } while (!Done); 1330718292f2SDouglas Gregor 1331718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 1332718292f2SDouglas Gregor consumeToken(); 1333718292f2SDouglas Gregor else { 1334718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 1335718292f2SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 1336718292f2SDouglas Gregor HadError = true; 1337718292f2SDouglas Gregor } 1338718292f2SDouglas Gregor 133911dfe6feSDouglas Gregor // If the active module is a top-level framework, and there are no link 134011dfe6feSDouglas Gregor // libraries, automatically link against the framework. 134111dfe6feSDouglas Gregor if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() && 134211dfe6feSDouglas Gregor ActiveModule->LinkLibraries.empty()) { 134311dfe6feSDouglas Gregor inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager()); 134411dfe6feSDouglas Gregor } 134511dfe6feSDouglas Gregor 1346e7ab3669SDouglas Gregor // We're done parsing this module. Pop back to the previous module. 1347e7ab3669SDouglas Gregor ActiveModule = PreviousActiveModule; 1348718292f2SDouglas Gregor } 1349718292f2SDouglas Gregor 135097292843SDaniel Jasper /// \brief Parse an extern module declaration. 135197292843SDaniel Jasper /// 135297292843SDaniel Jasper /// extern module-declaration: 135397292843SDaniel Jasper /// 'extern' 'module' module-id string-literal 135497292843SDaniel Jasper void ModuleMapParser::parseExternModuleDecl() { 135597292843SDaniel Jasper assert(Tok.is(MMToken::ExternKeyword)); 135697292843SDaniel Jasper consumeToken(); // 'extern' keyword 135797292843SDaniel Jasper 135897292843SDaniel Jasper // Parse 'module' keyword. 135997292843SDaniel Jasper if (!Tok.is(MMToken::ModuleKeyword)) { 136097292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 136197292843SDaniel Jasper consumeToken(); 136297292843SDaniel Jasper HadError = true; 136397292843SDaniel Jasper return; 136497292843SDaniel Jasper } 136597292843SDaniel Jasper consumeToken(); // 'module' keyword 136697292843SDaniel Jasper 136797292843SDaniel Jasper // Parse the module name. 136897292843SDaniel Jasper ModuleId Id; 136997292843SDaniel Jasper if (parseModuleId(Id)) { 137097292843SDaniel Jasper HadError = true; 137197292843SDaniel Jasper return; 137297292843SDaniel Jasper } 137397292843SDaniel Jasper 137497292843SDaniel Jasper // Parse the referenced module map file name. 137597292843SDaniel Jasper if (!Tok.is(MMToken::StringLiteral)) { 137697292843SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file); 137797292843SDaniel Jasper HadError = true; 137897292843SDaniel Jasper return; 137997292843SDaniel Jasper } 138097292843SDaniel Jasper std::string FileName = Tok.getString(); 138197292843SDaniel Jasper consumeToken(); // filename 138297292843SDaniel Jasper 138397292843SDaniel Jasper StringRef FileNameRef = FileName; 138497292843SDaniel Jasper SmallString<128> ModuleMapFileName; 138597292843SDaniel Jasper if (llvm::sys::path::is_relative(FileNameRef)) { 138697292843SDaniel Jasper ModuleMapFileName += Directory->getName(); 138797292843SDaniel Jasper llvm::sys::path::append(ModuleMapFileName, FileName); 138897292843SDaniel Jasper FileNameRef = ModuleMapFileName.str(); 138997292843SDaniel Jasper } 139097292843SDaniel Jasper if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef)) 139197292843SDaniel Jasper Map.parseModuleMapFile(File, /*IsSystem=*/false); 139297292843SDaniel Jasper } 139397292843SDaniel Jasper 13941fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration. 13951fb5c3a6SDouglas Gregor /// 13961fb5c3a6SDouglas Gregor /// requires-declaration: 13971fb5c3a6SDouglas Gregor /// 'requires' feature-list 13981fb5c3a6SDouglas Gregor /// 13991fb5c3a6SDouglas Gregor /// feature-list: 1400*a3feee2aSRichard Smith /// feature ',' feature-list 1401*a3feee2aSRichard Smith /// feature 1402*a3feee2aSRichard Smith /// 1403*a3feee2aSRichard Smith /// feature: 1404*a3feee2aSRichard Smith /// '!'[opt] identifier 14051fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() { 14061fb5c3a6SDouglas Gregor assert(Tok.is(MMToken::RequiresKeyword)); 14071fb5c3a6SDouglas Gregor 14081fb5c3a6SDouglas Gregor // Parse 'requires' keyword. 14091fb5c3a6SDouglas Gregor consumeToken(); 14101fb5c3a6SDouglas Gregor 14111fb5c3a6SDouglas Gregor // Parse the feature-list. 14121fb5c3a6SDouglas Gregor do { 1413*a3feee2aSRichard Smith bool RequiredState = true; 1414*a3feee2aSRichard Smith if (Tok.is(MMToken::Exclaim)) { 1415*a3feee2aSRichard Smith RequiredState = false; 1416*a3feee2aSRichard Smith consumeToken(); 1417*a3feee2aSRichard Smith } 1418*a3feee2aSRichard Smith 14191fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 14201fb5c3a6SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature); 14211fb5c3a6SDouglas Gregor HadError = true; 14221fb5c3a6SDouglas Gregor return; 14231fb5c3a6SDouglas Gregor } 14241fb5c3a6SDouglas Gregor 14251fb5c3a6SDouglas Gregor // Consume the feature name. 14261fb5c3a6SDouglas Gregor std::string Feature = Tok.getString(); 14271fb5c3a6SDouglas Gregor consumeToken(); 14281fb5c3a6SDouglas Gregor 14291fb5c3a6SDouglas Gregor // Add this feature. 1430*a3feee2aSRichard Smith ActiveModule->addRequirement(Feature, RequiredState, 1431*a3feee2aSRichard Smith Map.LangOpts, *Map.Target); 14321fb5c3a6SDouglas Gregor 14331fb5c3a6SDouglas Gregor if (!Tok.is(MMToken::Comma)) 14341fb5c3a6SDouglas Gregor break; 14351fb5c3a6SDouglas Gregor 14361fb5c3a6SDouglas Gregor // Consume the comma. 14371fb5c3a6SDouglas Gregor consumeToken(); 14381fb5c3a6SDouglas Gregor } while (true); 14391fb5c3a6SDouglas Gregor } 14401fb5c3a6SDouglas Gregor 1441f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the 1442f2161a70SDouglas Gregor /// subframework in which the given module lives. 1443bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod, 1444f857950dSDmitri Gribenko SmallVectorImpl<char> &Path) { 1445f2161a70SDouglas Gregor // Collect the framework names from the given module to the top-level module. 1446f857950dSDmitri Gribenko SmallVector<StringRef, 2> Paths; 1447f2161a70SDouglas Gregor for (; Mod; Mod = Mod->Parent) { 1448f2161a70SDouglas Gregor if (Mod->IsFramework) 1449f2161a70SDouglas Gregor Paths.push_back(Mod->Name); 1450f2161a70SDouglas Gregor } 1451f2161a70SDouglas Gregor 1452f2161a70SDouglas Gregor if (Paths.empty()) 1453f2161a70SDouglas Gregor return; 1454f2161a70SDouglas Gregor 1455f2161a70SDouglas Gregor // Add Frameworks/Name.framework for each subframework. 145617381a06SBenjamin Kramer for (unsigned I = Paths.size() - 1; I != 0; --I) 145717381a06SBenjamin Kramer llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework"); 1458f2161a70SDouglas Gregor } 1459f2161a70SDouglas Gregor 1460718292f2SDouglas Gregor /// \brief Parse a header declaration. 1461718292f2SDouglas Gregor /// 1462718292f2SDouglas Gregor /// header-declaration: 1463322f633cSDouglas Gregor /// 'umbrella'[opt] 'header' string-literal 146459527666SDouglas Gregor /// 'exclude'[opt] 'header' string-literal 1465b53e5483SLawrence Crowl void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken, 1466b53e5483SLawrence Crowl SourceLocation LeadingLoc) { 1467718292f2SDouglas Gregor assert(Tok.is(MMToken::HeaderKeyword)); 14681871ed3dSBenjamin Kramer consumeToken(); 1469718292f2SDouglas Gregor 1470718292f2SDouglas Gregor // Parse the header name. 1471718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1472718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1473718292f2SDouglas Gregor << "header"; 1474718292f2SDouglas Gregor HadError = true; 1475718292f2SDouglas Gregor return; 1476718292f2SDouglas Gregor } 1477e7ab3669SDouglas Gregor std::string FileName = Tok.getString(); 1478718292f2SDouglas Gregor SourceLocation FileNameLoc = consumeToken(); 1479718292f2SDouglas Gregor 1480524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1481b53e5483SLawrence Crowl if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) { 1482524e33e1SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash) 1483524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1484322f633cSDouglas Gregor HadError = true; 1485322f633cSDouglas Gregor return; 1486322f633cSDouglas Gregor } 1487322f633cSDouglas Gregor 14885257fc63SDouglas Gregor // Look for this file. 1489e7ab3669SDouglas Gregor const FileEntry *File = 0; 14903ec6663bSDouglas Gregor const FileEntry *BuiltinFile = 0; 14912c1dd271SDylan Noblesmith SmallString<128> PathName; 1492e7ab3669SDouglas Gregor if (llvm::sys::path::is_absolute(FileName)) { 1493e7ab3669SDouglas Gregor PathName = FileName; 1494e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 14957033127bSDouglas Gregor } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) { 14967033127bSDouglas Gregor PathName = Dir->getName(); 14977033127bSDouglas Gregor llvm::sys::path::append(PathName, FileName); 14987033127bSDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1499e7ab3669SDouglas Gregor } else { 1500e7ab3669SDouglas Gregor // Search for the header file within the search directory. 15017033127bSDouglas Gregor PathName = Directory->getName(); 1502e7ab3669SDouglas Gregor unsigned PathLength = PathName.size(); 1503755b2055SDouglas Gregor 1504f2161a70SDouglas Gregor if (ActiveModule->isPartOfFramework()) { 1505f2161a70SDouglas Gregor appendSubframeworkPaths(ActiveModule, PathName); 1506755b2055SDouglas Gregor 1507e7ab3669SDouglas Gregor // Check whether this file is in the public headers. 150817381a06SBenjamin Kramer llvm::sys::path::append(PathName, "Headers", FileName); 1509e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1510e7ab3669SDouglas Gregor 1511e7ab3669SDouglas Gregor if (!File) { 1512e7ab3669SDouglas Gregor // Check whether this file is in the private headers. 1513e7ab3669SDouglas Gregor PathName.resize(PathLength); 151417381a06SBenjamin Kramer llvm::sys::path::append(PathName, "PrivateHeaders", FileName); 1515e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 1516e7ab3669SDouglas Gregor } 1517e7ab3669SDouglas Gregor } else { 1518e7ab3669SDouglas Gregor // Lookup for normal headers. 1519e7ab3669SDouglas Gregor llvm::sys::path::append(PathName, FileName); 1520e7ab3669SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 15213ec6663bSDouglas Gregor 15223ec6663bSDouglas Gregor // If this is a system module with a top-level header, this header 15233ec6663bSDouglas Gregor // may have a counterpart (or replacement) in the set of headers 15243ec6663bSDouglas Gregor // supplied by Clang. Find that builtin header. 1525b53e5483SLawrence Crowl if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword && 1526b53e5483SLawrence Crowl BuiltinIncludeDir && BuiltinIncludeDir != Directory && 1527b53e5483SLawrence Crowl isBuiltinHeader(FileName)) { 15282c1dd271SDylan Noblesmith SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName()); 15293ec6663bSDouglas Gregor llvm::sys::path::append(BuiltinPathName, FileName); 15303ec6663bSDouglas Gregor BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName); 15313ec6663bSDouglas Gregor 15323ec6663bSDouglas Gregor // If Clang supplies this header but the underlying system does not, 15333ec6663bSDouglas Gregor // just silently swap in our builtin version. Otherwise, we'll end 15343ec6663bSDouglas Gregor // up adding both (later). 15353ec6663bSDouglas Gregor if (!File && BuiltinFile) { 15363ec6663bSDouglas Gregor File = BuiltinFile; 15373ec6663bSDouglas Gregor BuiltinFile = 0; 15383ec6663bSDouglas Gregor } 15393ec6663bSDouglas Gregor } 1540e7ab3669SDouglas Gregor } 1541e7ab3669SDouglas Gregor } 15425257fc63SDouglas Gregor 15435257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 15445257fc63SDouglas Gregor // Come up with a lazy way to do this. 1545e7ab3669SDouglas Gregor if (File) { 154697da9178SDaniel Jasper if (LeadingToken == MMToken::UmbrellaKeyword) { 1547322f633cSDouglas Gregor const DirectoryEntry *UmbrellaDir = File->getDir(); 154859527666SDouglas Gregor if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) { 1549b53e5483SLawrence Crowl Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash) 155059527666SDouglas Gregor << UmbrellaModule->getFullModuleName(); 1551322f633cSDouglas Gregor HadError = true; 15525257fc63SDouglas Gregor } else { 1553322f633cSDouglas Gregor // Record this umbrella header. 1554322f633cSDouglas Gregor Map.setUmbrellaHeader(ActiveModule, File); 1555322f633cSDouglas Gregor } 1556322f633cSDouglas Gregor } else { 1557322f633cSDouglas Gregor // Record this header. 1558b53e5483SLawrence Crowl ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader; 1559b53e5483SLawrence Crowl if (LeadingToken == MMToken::ExcludeKeyword) 1560b53e5483SLawrence Crowl Role = ModuleMap::ExcludedHeader; 1561b53e5483SLawrence Crowl else if (LeadingToken == MMToken::PrivateKeyword) 1562b53e5483SLawrence Crowl Role = ModuleMap::PrivateHeader; 1563b53e5483SLawrence Crowl else 1564b53e5483SLawrence Crowl assert(LeadingToken == MMToken::HeaderKeyword); 1565b53e5483SLawrence Crowl 1566b53e5483SLawrence Crowl Map.addHeader(ActiveModule, File, Role); 15673ec6663bSDouglas Gregor 15683ec6663bSDouglas Gregor // If there is a builtin counterpart to this file, add it now. 15693ec6663bSDouglas Gregor if (BuiltinFile) 1570b53e5483SLawrence Crowl Map.addHeader(ActiveModule, BuiltinFile, Role); 15715257fc63SDouglas Gregor } 1572b53e5483SLawrence Crowl } else if (LeadingToken != MMToken::ExcludeKeyword) { 15734b27a64bSDouglas Gregor // Ignore excluded header files. They're optional anyway. 15744b27a64bSDouglas Gregor 15755257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_not_found) 1576b53e5483SLawrence Crowl << (LeadingToken == MMToken::UmbrellaKeyword) << FileName; 15775257fc63SDouglas Gregor HadError = true; 15785257fc63SDouglas Gregor } 1579718292f2SDouglas Gregor } 1580718292f2SDouglas Gregor 1581524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration. 1582524e33e1SDouglas Gregor /// 1583524e33e1SDouglas Gregor /// umbrella-dir-declaration: 1584524e33e1SDouglas Gregor /// umbrella string-literal 1585524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) { 1586524e33e1SDouglas Gregor // Parse the directory name. 1587524e33e1SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1588524e33e1SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 1589524e33e1SDouglas Gregor << "umbrella"; 1590524e33e1SDouglas Gregor HadError = true; 1591524e33e1SDouglas Gregor return; 1592524e33e1SDouglas Gregor } 1593524e33e1SDouglas Gregor 1594524e33e1SDouglas Gregor std::string DirName = Tok.getString(); 1595524e33e1SDouglas Gregor SourceLocation DirNameLoc = consumeToken(); 1596524e33e1SDouglas Gregor 1597524e33e1SDouglas Gregor // Check whether we already have an umbrella. 1598524e33e1SDouglas Gregor if (ActiveModule->Umbrella) { 1599524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash) 1600524e33e1SDouglas Gregor << ActiveModule->getFullModuleName(); 1601524e33e1SDouglas Gregor HadError = true; 1602524e33e1SDouglas Gregor return; 1603524e33e1SDouglas Gregor } 1604524e33e1SDouglas Gregor 1605524e33e1SDouglas Gregor // Look for this file. 1606524e33e1SDouglas Gregor const DirectoryEntry *Dir = 0; 1607524e33e1SDouglas Gregor if (llvm::sys::path::is_absolute(DirName)) 1608524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(DirName); 1609524e33e1SDouglas Gregor else { 16102c1dd271SDylan Noblesmith SmallString<128> PathName; 1611524e33e1SDouglas Gregor PathName = Directory->getName(); 1612524e33e1SDouglas Gregor llvm::sys::path::append(PathName, DirName); 1613524e33e1SDouglas Gregor Dir = SourceMgr.getFileManager().getDirectory(PathName); 1614524e33e1SDouglas Gregor } 1615524e33e1SDouglas Gregor 1616524e33e1SDouglas Gregor if (!Dir) { 1617524e33e1SDouglas Gregor Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found) 1618524e33e1SDouglas Gregor << DirName; 1619524e33e1SDouglas Gregor HadError = true; 1620524e33e1SDouglas Gregor return; 1621524e33e1SDouglas Gregor } 1622524e33e1SDouglas Gregor 1623524e33e1SDouglas Gregor if (Module *OwningModule = Map.UmbrellaDirs[Dir]) { 1624524e33e1SDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 1625524e33e1SDouglas Gregor << OwningModule->getFullModuleName(); 1626524e33e1SDouglas Gregor HadError = true; 1627524e33e1SDouglas Gregor return; 1628524e33e1SDouglas Gregor } 1629524e33e1SDouglas Gregor 1630524e33e1SDouglas Gregor // Record this umbrella directory. 1631524e33e1SDouglas Gregor Map.setUmbrellaDir(ActiveModule, Dir); 1632524e33e1SDouglas Gregor } 1633524e33e1SDouglas Gregor 16342b82c2a5SDouglas Gregor /// \brief Parse a module export declaration. 16352b82c2a5SDouglas Gregor /// 16362b82c2a5SDouglas Gregor /// export-declaration: 16372b82c2a5SDouglas Gregor /// 'export' wildcard-module-id 16382b82c2a5SDouglas Gregor /// 16392b82c2a5SDouglas Gregor /// wildcard-module-id: 16402b82c2a5SDouglas Gregor /// identifier 16412b82c2a5SDouglas Gregor /// '*' 16422b82c2a5SDouglas Gregor /// identifier '.' wildcard-module-id 16432b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() { 16442b82c2a5SDouglas Gregor assert(Tok.is(MMToken::ExportKeyword)); 16452b82c2a5SDouglas Gregor SourceLocation ExportLoc = consumeToken(); 16462b82c2a5SDouglas Gregor 16472b82c2a5SDouglas Gregor // Parse the module-id with an optional wildcard at the end. 16482b82c2a5SDouglas Gregor ModuleId ParsedModuleId; 16492b82c2a5SDouglas Gregor bool Wildcard = false; 16502b82c2a5SDouglas Gregor do { 16512b82c2a5SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 16522b82c2a5SDouglas Gregor ParsedModuleId.push_back(std::make_pair(Tok.getString(), 16532b82c2a5SDouglas Gregor Tok.getLocation())); 16542b82c2a5SDouglas Gregor consumeToken(); 16552b82c2a5SDouglas Gregor 16562b82c2a5SDouglas Gregor if (Tok.is(MMToken::Period)) { 16572b82c2a5SDouglas Gregor consumeToken(); 16582b82c2a5SDouglas Gregor continue; 16592b82c2a5SDouglas Gregor } 16602b82c2a5SDouglas Gregor 16612b82c2a5SDouglas Gregor break; 16622b82c2a5SDouglas Gregor } 16632b82c2a5SDouglas Gregor 16642b82c2a5SDouglas Gregor if(Tok.is(MMToken::Star)) { 16652b82c2a5SDouglas Gregor Wildcard = true; 1666f5eedd05SDouglas Gregor consumeToken(); 16672b82c2a5SDouglas Gregor break; 16682b82c2a5SDouglas Gregor } 16692b82c2a5SDouglas Gregor 1670ba7f2f71SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); 16712b82c2a5SDouglas Gregor HadError = true; 16722b82c2a5SDouglas Gregor return; 16732b82c2a5SDouglas Gregor } while (true); 16742b82c2a5SDouglas Gregor 16752b82c2a5SDouglas Gregor Module::UnresolvedExportDecl Unresolved = { 16762b82c2a5SDouglas Gregor ExportLoc, ParsedModuleId, Wildcard 16772b82c2a5SDouglas Gregor }; 16782b82c2a5SDouglas Gregor ActiveModule->UnresolvedExports.push_back(Unresolved); 16792b82c2a5SDouglas Gregor } 16802b82c2a5SDouglas Gregor 1681ba7f2f71SDaniel Jasper /// \brief Parse a module uses declaration. 1682ba7f2f71SDaniel Jasper /// 1683ba7f2f71SDaniel Jasper /// uses-declaration: 1684ba7f2f71SDaniel Jasper /// 'uses' wildcard-module-id 1685ba7f2f71SDaniel Jasper void ModuleMapParser::parseUseDecl() { 1686ba7f2f71SDaniel Jasper assert(Tok.is(MMToken::UseKeyword)); 1687ba7f2f71SDaniel Jasper consumeToken(); 1688ba7f2f71SDaniel Jasper // Parse the module-id. 1689ba7f2f71SDaniel Jasper ModuleId ParsedModuleId; 1690ba7f2f71SDaniel Jasper 1691ba7f2f71SDaniel Jasper do { 1692ba7f2f71SDaniel Jasper if (Tok.is(MMToken::Identifier)) { 1693ba7f2f71SDaniel Jasper ParsedModuleId.push_back( 1694ba7f2f71SDaniel Jasper std::make_pair(Tok.getString(), Tok.getLocation())); 1695ba7f2f71SDaniel Jasper consumeToken(); 1696ba7f2f71SDaniel Jasper 1697ba7f2f71SDaniel Jasper if (Tok.is(MMToken::Period)) { 1698ba7f2f71SDaniel Jasper consumeToken(); 1699ba7f2f71SDaniel Jasper continue; 1700ba7f2f71SDaniel Jasper } 1701ba7f2f71SDaniel Jasper 1702ba7f2f71SDaniel Jasper break; 1703ba7f2f71SDaniel Jasper } 1704ba7f2f71SDaniel Jasper 1705ba7f2f71SDaniel Jasper Diags.Report(Tok.getLocation(), diag::err_mmap_module_id); 1706ba7f2f71SDaniel Jasper HadError = true; 1707ba7f2f71SDaniel Jasper return; 1708ba7f2f71SDaniel Jasper } while (true); 1709ba7f2f71SDaniel Jasper 1710ba7f2f71SDaniel Jasper ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId); 1711ba7f2f71SDaniel Jasper } 1712ba7f2f71SDaniel Jasper 17136ddfca91SDouglas Gregor /// \brief Parse a link declaration. 17146ddfca91SDouglas Gregor /// 17156ddfca91SDouglas Gregor /// module-declaration: 17166ddfca91SDouglas Gregor /// 'link' 'framework'[opt] string-literal 17176ddfca91SDouglas Gregor void ModuleMapParser::parseLinkDecl() { 17186ddfca91SDouglas Gregor assert(Tok.is(MMToken::LinkKeyword)); 17196ddfca91SDouglas Gregor SourceLocation LinkLoc = consumeToken(); 17206ddfca91SDouglas Gregor 17216ddfca91SDouglas Gregor // Parse the optional 'framework' keyword. 17226ddfca91SDouglas Gregor bool IsFramework = false; 17236ddfca91SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 17246ddfca91SDouglas Gregor consumeToken(); 17256ddfca91SDouglas Gregor IsFramework = true; 17266ddfca91SDouglas Gregor } 17276ddfca91SDouglas Gregor 17286ddfca91SDouglas Gregor // Parse the library name 17296ddfca91SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 17306ddfca91SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name) 17316ddfca91SDouglas Gregor << IsFramework << SourceRange(LinkLoc); 17326ddfca91SDouglas Gregor HadError = true; 17336ddfca91SDouglas Gregor return; 17346ddfca91SDouglas Gregor } 17356ddfca91SDouglas Gregor 17366ddfca91SDouglas Gregor std::string LibraryName = Tok.getString(); 17376ddfca91SDouglas Gregor consumeToken(); 17386ddfca91SDouglas Gregor ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName, 17396ddfca91SDouglas Gregor IsFramework)); 17406ddfca91SDouglas Gregor } 17416ddfca91SDouglas Gregor 174235b13eceSDouglas Gregor /// \brief Parse a configuration macro declaration. 174335b13eceSDouglas Gregor /// 174435b13eceSDouglas Gregor /// module-declaration: 174535b13eceSDouglas Gregor /// 'config_macros' attributes[opt] config-macro-list? 174635b13eceSDouglas Gregor /// 174735b13eceSDouglas Gregor /// config-macro-list: 174835b13eceSDouglas Gregor /// identifier (',' identifier)? 174935b13eceSDouglas Gregor void ModuleMapParser::parseConfigMacros() { 175035b13eceSDouglas Gregor assert(Tok.is(MMToken::ConfigMacros)); 175135b13eceSDouglas Gregor SourceLocation ConfigMacrosLoc = consumeToken(); 175235b13eceSDouglas Gregor 175335b13eceSDouglas Gregor // Only top-level modules can have configuration macros. 175435b13eceSDouglas Gregor if (ActiveModule->Parent) { 175535b13eceSDouglas Gregor Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule); 175635b13eceSDouglas Gregor } 175735b13eceSDouglas Gregor 175835b13eceSDouglas Gregor // Parse the optional attributes. 175935b13eceSDouglas Gregor Attributes Attrs; 176035b13eceSDouglas Gregor parseOptionalAttributes(Attrs); 176135b13eceSDouglas Gregor if (Attrs.IsExhaustive && !ActiveModule->Parent) { 176235b13eceSDouglas Gregor ActiveModule->ConfigMacrosExhaustive = true; 176335b13eceSDouglas Gregor } 176435b13eceSDouglas Gregor 176535b13eceSDouglas Gregor // If we don't have an identifier, we're done. 176635b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) 176735b13eceSDouglas Gregor return; 176835b13eceSDouglas Gregor 176935b13eceSDouglas Gregor // Consume the first identifier. 177035b13eceSDouglas Gregor if (!ActiveModule->Parent) { 177135b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 177235b13eceSDouglas Gregor } 177335b13eceSDouglas Gregor consumeToken(); 177435b13eceSDouglas Gregor 177535b13eceSDouglas Gregor do { 177635b13eceSDouglas Gregor // If there's a comma, consume it. 177735b13eceSDouglas Gregor if (!Tok.is(MMToken::Comma)) 177835b13eceSDouglas Gregor break; 177935b13eceSDouglas Gregor consumeToken(); 178035b13eceSDouglas Gregor 178135b13eceSDouglas Gregor // We expect to see a macro name here. 178235b13eceSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 178335b13eceSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro); 178435b13eceSDouglas Gregor break; 178535b13eceSDouglas Gregor } 178635b13eceSDouglas Gregor 178735b13eceSDouglas Gregor // Consume the macro name. 178835b13eceSDouglas Gregor if (!ActiveModule->Parent) { 178935b13eceSDouglas Gregor ActiveModule->ConfigMacros.push_back(Tok.getString().str()); 179035b13eceSDouglas Gregor } 179135b13eceSDouglas Gregor consumeToken(); 179235b13eceSDouglas Gregor } while (true); 179335b13eceSDouglas Gregor } 179435b13eceSDouglas Gregor 1795fb912657SDouglas Gregor /// \brief Format a module-id into a string. 1796fb912657SDouglas Gregor static std::string formatModuleId(const ModuleId &Id) { 1797fb912657SDouglas Gregor std::string result; 1798fb912657SDouglas Gregor { 1799fb912657SDouglas Gregor llvm::raw_string_ostream OS(result); 1800fb912657SDouglas Gregor 1801fb912657SDouglas Gregor for (unsigned I = 0, N = Id.size(); I != N; ++I) { 1802fb912657SDouglas Gregor if (I) 1803fb912657SDouglas Gregor OS << "."; 1804fb912657SDouglas Gregor OS << Id[I].first; 1805fb912657SDouglas Gregor } 1806fb912657SDouglas Gregor } 1807fb912657SDouglas Gregor 1808fb912657SDouglas Gregor return result; 1809fb912657SDouglas Gregor } 1810fb912657SDouglas Gregor 1811fb912657SDouglas Gregor /// \brief Parse a conflict declaration. 1812fb912657SDouglas Gregor /// 1813fb912657SDouglas Gregor /// module-declaration: 1814fb912657SDouglas Gregor /// 'conflict' module-id ',' string-literal 1815fb912657SDouglas Gregor void ModuleMapParser::parseConflict() { 1816fb912657SDouglas Gregor assert(Tok.is(MMToken::Conflict)); 1817fb912657SDouglas Gregor SourceLocation ConflictLoc = consumeToken(); 1818fb912657SDouglas Gregor Module::UnresolvedConflict Conflict; 1819fb912657SDouglas Gregor 1820fb912657SDouglas Gregor // Parse the module-id. 1821fb912657SDouglas Gregor if (parseModuleId(Conflict.Id)) 1822fb912657SDouglas Gregor return; 1823fb912657SDouglas Gregor 1824fb912657SDouglas Gregor // Parse the ','. 1825fb912657SDouglas Gregor if (!Tok.is(MMToken::Comma)) { 1826fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma) 1827fb912657SDouglas Gregor << SourceRange(ConflictLoc); 1828fb912657SDouglas Gregor return; 1829fb912657SDouglas Gregor } 1830fb912657SDouglas Gregor consumeToken(); 1831fb912657SDouglas Gregor 1832fb912657SDouglas Gregor // Parse the message. 1833fb912657SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 1834fb912657SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message) 1835fb912657SDouglas Gregor << formatModuleId(Conflict.Id); 1836fb912657SDouglas Gregor return; 1837fb912657SDouglas Gregor } 1838fb912657SDouglas Gregor Conflict.Message = Tok.getString().str(); 1839fb912657SDouglas Gregor consumeToken(); 1840fb912657SDouglas Gregor 1841fb912657SDouglas Gregor // Add this unresolved conflict. 1842fb912657SDouglas Gregor ActiveModule->UnresolvedConflicts.push_back(Conflict); 1843fb912657SDouglas Gregor } 1844fb912657SDouglas Gregor 18456ddfca91SDouglas Gregor /// \brief Parse an inferred module declaration (wildcard modules). 18469194a91dSDouglas Gregor /// 18479194a91dSDouglas Gregor /// module-declaration: 18489194a91dSDouglas Gregor /// 'explicit'[opt] 'framework'[opt] 'module' * attributes[opt] 18499194a91dSDouglas Gregor /// { inferred-module-member* } 18509194a91dSDouglas Gregor /// 18519194a91dSDouglas Gregor /// inferred-module-member: 18529194a91dSDouglas Gregor /// 'export' '*' 18539194a91dSDouglas Gregor /// 'exclude' identifier 18549194a91dSDouglas Gregor void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) { 185573441091SDouglas Gregor assert(Tok.is(MMToken::Star)); 185673441091SDouglas Gregor SourceLocation StarLoc = consumeToken(); 185773441091SDouglas Gregor bool Failed = false; 185873441091SDouglas Gregor 185973441091SDouglas Gregor // Inferred modules must be submodules. 18609194a91dSDouglas Gregor if (!ActiveModule && !Framework) { 186173441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 186273441091SDouglas Gregor Failed = true; 186373441091SDouglas Gregor } 186473441091SDouglas Gregor 18659194a91dSDouglas Gregor if (ActiveModule) { 1866524e33e1SDouglas Gregor // Inferred modules must have umbrella directories. 1867524e33e1SDouglas Gregor if (!Failed && !ActiveModule->getUmbrellaDir()) { 186873441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 186973441091SDouglas Gregor Failed = true; 187073441091SDouglas Gregor } 187173441091SDouglas Gregor 187273441091SDouglas Gregor // Check for redefinition of an inferred module. 1873dd005f69SDouglas Gregor if (!Failed && ActiveModule->InferSubmodules) { 187473441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 1875dd005f69SDouglas Gregor if (ActiveModule->InferredSubmoduleLoc.isValid()) 1876dd005f69SDouglas Gregor Diags.Report(ActiveModule->InferredSubmoduleLoc, 187773441091SDouglas Gregor diag::note_mmap_prev_definition); 187873441091SDouglas Gregor Failed = true; 187973441091SDouglas Gregor } 188073441091SDouglas Gregor 18819194a91dSDouglas Gregor // Check for the 'framework' keyword, which is not permitted here. 18829194a91dSDouglas Gregor if (Framework) { 18839194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule); 18849194a91dSDouglas Gregor Framework = false; 18859194a91dSDouglas Gregor } 18869194a91dSDouglas Gregor } else if (Explicit) { 18879194a91dSDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework); 18889194a91dSDouglas Gregor Explicit = false; 18899194a91dSDouglas Gregor } 18909194a91dSDouglas Gregor 189173441091SDouglas Gregor // If there were any problems with this inferred submodule, skip its body. 189273441091SDouglas Gregor if (Failed) { 189373441091SDouglas Gregor if (Tok.is(MMToken::LBrace)) { 189473441091SDouglas Gregor consumeToken(); 189573441091SDouglas Gregor skipUntil(MMToken::RBrace); 189673441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 189773441091SDouglas Gregor consumeToken(); 189873441091SDouglas Gregor } 189973441091SDouglas Gregor HadError = true; 190073441091SDouglas Gregor return; 190173441091SDouglas Gregor } 190273441091SDouglas Gregor 19039194a91dSDouglas Gregor // Parse optional attributes. 19044442605fSBill Wendling Attributes Attrs; 19059194a91dSDouglas Gregor parseOptionalAttributes(Attrs); 19069194a91dSDouglas Gregor 19079194a91dSDouglas Gregor if (ActiveModule) { 190873441091SDouglas Gregor // Note that we have an inferred submodule. 1909dd005f69SDouglas Gregor ActiveModule->InferSubmodules = true; 1910dd005f69SDouglas Gregor ActiveModule->InferredSubmoduleLoc = StarLoc; 1911dd005f69SDouglas Gregor ActiveModule->InferExplicitSubmodules = Explicit; 19129194a91dSDouglas Gregor } else { 19139194a91dSDouglas Gregor // We'll be inferring framework modules for this directory. 19149194a91dSDouglas Gregor Map.InferredDirectories[Directory].InferModules = true; 19159194a91dSDouglas Gregor Map.InferredDirectories[Directory].InferSystemModules = Attrs.IsSystem; 19169194a91dSDouglas Gregor } 191773441091SDouglas Gregor 191873441091SDouglas Gregor // Parse the opening brace. 191973441091SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 192073441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 192173441091SDouglas Gregor HadError = true; 192273441091SDouglas Gregor return; 192373441091SDouglas Gregor } 192473441091SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 192573441091SDouglas Gregor 192673441091SDouglas Gregor // Parse the body of the inferred submodule. 192773441091SDouglas Gregor bool Done = false; 192873441091SDouglas Gregor do { 192973441091SDouglas Gregor switch (Tok.Kind) { 193073441091SDouglas Gregor case MMToken::EndOfFile: 193173441091SDouglas Gregor case MMToken::RBrace: 193273441091SDouglas Gregor Done = true; 193373441091SDouglas Gregor break; 193473441091SDouglas Gregor 19359194a91dSDouglas Gregor case MMToken::ExcludeKeyword: { 19369194a91dSDouglas Gregor if (ActiveModule) { 19379194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 1938162405daSDouglas Gregor << (ActiveModule != 0); 19399194a91dSDouglas Gregor consumeToken(); 19409194a91dSDouglas Gregor break; 19419194a91dSDouglas Gregor } 19429194a91dSDouglas Gregor 19439194a91dSDouglas Gregor consumeToken(); 19449194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 19459194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name); 19469194a91dSDouglas Gregor break; 19479194a91dSDouglas Gregor } 19489194a91dSDouglas Gregor 19499194a91dSDouglas Gregor Map.InferredDirectories[Directory].ExcludedModules 19509194a91dSDouglas Gregor .push_back(Tok.getString()); 19519194a91dSDouglas Gregor consumeToken(); 19529194a91dSDouglas Gregor break; 19539194a91dSDouglas Gregor } 19549194a91dSDouglas Gregor 19559194a91dSDouglas Gregor case MMToken::ExportKeyword: 19569194a91dSDouglas Gregor if (!ActiveModule) { 19579194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 1958162405daSDouglas Gregor << (ActiveModule != 0); 19599194a91dSDouglas Gregor consumeToken(); 19609194a91dSDouglas Gregor break; 19619194a91dSDouglas Gregor } 19629194a91dSDouglas Gregor 196373441091SDouglas Gregor consumeToken(); 196473441091SDouglas Gregor if (Tok.is(MMToken::Star)) 1965dd005f69SDouglas Gregor ActiveModule->InferExportWildcard = true; 196673441091SDouglas Gregor else 196773441091SDouglas Gregor Diags.Report(Tok.getLocation(), 196873441091SDouglas Gregor diag::err_mmap_expected_export_wildcard); 196973441091SDouglas Gregor consumeToken(); 197073441091SDouglas Gregor break; 197173441091SDouglas Gregor 197273441091SDouglas Gregor case MMToken::ExplicitKeyword: 197373441091SDouglas Gregor case MMToken::ModuleKeyword: 197473441091SDouglas Gregor case MMToken::HeaderKeyword: 1975b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 197673441091SDouglas Gregor case MMToken::UmbrellaKeyword: 197773441091SDouglas Gregor default: 19789194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member) 1979162405daSDouglas Gregor << (ActiveModule != 0); 198073441091SDouglas Gregor consumeToken(); 198173441091SDouglas Gregor break; 198273441091SDouglas Gregor } 198373441091SDouglas Gregor } while (!Done); 198473441091SDouglas Gregor 198573441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 198673441091SDouglas Gregor consumeToken(); 198773441091SDouglas Gregor else { 198873441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 198973441091SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 199073441091SDouglas Gregor HadError = true; 199173441091SDouglas Gregor } 199273441091SDouglas Gregor } 199373441091SDouglas Gregor 19949194a91dSDouglas Gregor /// \brief Parse optional attributes. 19959194a91dSDouglas Gregor /// 19969194a91dSDouglas Gregor /// attributes: 19979194a91dSDouglas Gregor /// attribute attributes 19989194a91dSDouglas Gregor /// attribute 19999194a91dSDouglas Gregor /// 20009194a91dSDouglas Gregor /// attribute: 20019194a91dSDouglas Gregor /// [ identifier ] 20029194a91dSDouglas Gregor /// 20039194a91dSDouglas Gregor /// \param Attrs Will be filled in with the parsed attributes. 20049194a91dSDouglas Gregor /// 20059194a91dSDouglas Gregor /// \returns true if an error occurred, false otherwise. 20064442605fSBill Wendling bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) { 20079194a91dSDouglas Gregor bool HadError = false; 20089194a91dSDouglas Gregor 20099194a91dSDouglas Gregor while (Tok.is(MMToken::LSquare)) { 20109194a91dSDouglas Gregor // Consume the '['. 20119194a91dSDouglas Gregor SourceLocation LSquareLoc = consumeToken(); 20129194a91dSDouglas Gregor 20139194a91dSDouglas Gregor // Check whether we have an attribute name here. 20149194a91dSDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 20159194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute); 20169194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 20179194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 20189194a91dSDouglas Gregor consumeToken(); 20199194a91dSDouglas Gregor HadError = true; 20209194a91dSDouglas Gregor } 20219194a91dSDouglas Gregor 20229194a91dSDouglas Gregor // Decode the attribute name. 20239194a91dSDouglas Gregor AttributeKind Attribute 20249194a91dSDouglas Gregor = llvm::StringSwitch<AttributeKind>(Tok.getString()) 202535b13eceSDouglas Gregor .Case("exhaustive", AT_exhaustive) 20269194a91dSDouglas Gregor .Case("system", AT_system) 20279194a91dSDouglas Gregor .Default(AT_unknown); 20289194a91dSDouglas Gregor switch (Attribute) { 20299194a91dSDouglas Gregor case AT_unknown: 20309194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute) 20319194a91dSDouglas Gregor << Tok.getString(); 20329194a91dSDouglas Gregor break; 20339194a91dSDouglas Gregor 20349194a91dSDouglas Gregor case AT_system: 20359194a91dSDouglas Gregor Attrs.IsSystem = true; 20369194a91dSDouglas Gregor break; 203735b13eceSDouglas Gregor 203835b13eceSDouglas Gregor case AT_exhaustive: 203935b13eceSDouglas Gregor Attrs.IsExhaustive = true; 204035b13eceSDouglas Gregor break; 20419194a91dSDouglas Gregor } 20429194a91dSDouglas Gregor consumeToken(); 20439194a91dSDouglas Gregor 20449194a91dSDouglas Gregor // Consume the ']'. 20459194a91dSDouglas Gregor if (!Tok.is(MMToken::RSquare)) { 20469194a91dSDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare); 20479194a91dSDouglas Gregor Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match); 20489194a91dSDouglas Gregor skipUntil(MMToken::RSquare); 20499194a91dSDouglas Gregor HadError = true; 20509194a91dSDouglas Gregor } 20519194a91dSDouglas Gregor 20529194a91dSDouglas Gregor if (Tok.is(MMToken::RSquare)) 20539194a91dSDouglas Gregor consumeToken(); 20549194a91dSDouglas Gregor } 20559194a91dSDouglas Gregor 20569194a91dSDouglas Gregor return HadError; 20579194a91dSDouglas Gregor } 20589194a91dSDouglas Gregor 20597033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence 20607033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null. 20617033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() { 20627033127bSDouglas Gregor for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) { 20637033127bSDouglas Gregor // If we have an umbrella directory, use that. 20647033127bSDouglas Gregor if (Mod->hasUmbrellaDir()) 20657033127bSDouglas Gregor return Mod->getUmbrellaDir(); 20667033127bSDouglas Gregor 20677033127bSDouglas Gregor // If we have a framework directory, stop looking. 20687033127bSDouglas Gregor if (Mod->IsFramework) 20697033127bSDouglas Gregor return 0; 20707033127bSDouglas Gregor } 20717033127bSDouglas Gregor 20727033127bSDouglas Gregor return 0; 20737033127bSDouglas Gregor } 20747033127bSDouglas Gregor 2075718292f2SDouglas Gregor /// \brief Parse a module map file. 2076718292f2SDouglas Gregor /// 2077718292f2SDouglas Gregor /// module-map-file: 2078718292f2SDouglas Gregor /// module-declaration* 2079718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() { 2080718292f2SDouglas Gregor do { 2081718292f2SDouglas Gregor switch (Tok.Kind) { 2082718292f2SDouglas Gregor case MMToken::EndOfFile: 2083718292f2SDouglas Gregor return HadError; 2084718292f2SDouglas Gregor 2085e7ab3669SDouglas Gregor case MMToken::ExplicitKeyword: 208697292843SDaniel Jasper case MMToken::ExternKeyword: 2087718292f2SDouglas Gregor case MMToken::ModuleKeyword: 2088755b2055SDouglas Gregor case MMToken::FrameworkKeyword: 2089718292f2SDouglas Gregor parseModuleDecl(); 2090718292f2SDouglas Gregor break; 2091718292f2SDouglas Gregor 20921fb5c3a6SDouglas Gregor case MMToken::Comma: 209335b13eceSDouglas Gregor case MMToken::ConfigMacros: 2094fb912657SDouglas Gregor case MMToken::Conflict: 2095*a3feee2aSRichard Smith case MMToken::Exclaim: 209659527666SDouglas Gregor case MMToken::ExcludeKeyword: 20972b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 2098718292f2SDouglas Gregor case MMToken::HeaderKeyword: 2099718292f2SDouglas Gregor case MMToken::Identifier: 2100718292f2SDouglas Gregor case MMToken::LBrace: 21016ddfca91SDouglas Gregor case MMToken::LinkKeyword: 2102a686e1b0SDouglas Gregor case MMToken::LSquare: 21032b82c2a5SDouglas Gregor case MMToken::Period: 2104b53e5483SLawrence Crowl case MMToken::PrivateKeyword: 2105718292f2SDouglas Gregor case MMToken::RBrace: 2106a686e1b0SDouglas Gregor case MMToken::RSquare: 21071fb5c3a6SDouglas Gregor case MMToken::RequiresKeyword: 21082b82c2a5SDouglas Gregor case MMToken::Star: 2109718292f2SDouglas Gregor case MMToken::StringLiteral: 2110718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 2111ba7f2f71SDaniel Jasper case MMToken::UseKeyword: 2112718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 2113718292f2SDouglas Gregor HadError = true; 2114718292f2SDouglas Gregor consumeToken(); 2115718292f2SDouglas Gregor break; 2116718292f2SDouglas Gregor } 2117718292f2SDouglas Gregor } while (true); 2118718292f2SDouglas Gregor } 2119718292f2SDouglas Gregor 2120963c5535SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem) { 21214ddf2221SDouglas Gregor llvm::DenseMap<const FileEntry *, bool>::iterator Known 21224ddf2221SDouglas Gregor = ParsedModuleMap.find(File); 21234ddf2221SDouglas Gregor if (Known != ParsedModuleMap.end()) 21244ddf2221SDouglas Gregor return Known->second; 21254ddf2221SDouglas Gregor 212689929282SDouglas Gregor assert(Target != 0 && "Missing target information"); 21271f76c4e8SManuel Klimek FileID ID = SourceMgr.createFileID(File, SourceLocation(), SrcMgr::C_User); 21281f76c4e8SManuel Klimek const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID); 2129718292f2SDouglas Gregor if (!Buffer) 21304ddf2221SDouglas Gregor return ParsedModuleMap[File] = true; 2131718292f2SDouglas Gregor 2132718292f2SDouglas Gregor // Parse this module map file. 21331f76c4e8SManuel Klimek Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts); 21341fb5c3a6SDouglas Gregor Diags->getClient()->BeginSourceFile(MMapLangOpts); 21351f76c4e8SManuel Klimek ModuleMapParser Parser(L, SourceMgr, Target, *Diags, *this, File->getDir(), 2136963c5535SDouglas Gregor BuiltinIncludeDir, IsSystem); 2137718292f2SDouglas Gregor bool Result = Parser.parseModuleMapFile(); 2138718292f2SDouglas Gregor Diags->getClient()->EndSourceFile(); 21394ddf2221SDouglas Gregor ParsedModuleMap[File] = Result; 2140718292f2SDouglas Gregor return Result; 2141718292f2SDouglas Gregor } 2142