123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188 |
- //===--- ModuleMap.cpp - Describe the layout of modules ---------*- C++ -*-===//
- //
- // The LLVM Compiler Infrastructure
- //
- // This file is distributed under the University of Illinois Open Source
- // License. See LICENSE.TXT for details.
- //
- //===----------------------------------------------------------------------===//
- //
- // This file defines the ModuleMap implementation, which describes the layout
- // of a module as it relates to headers.
- //
- //===----------------------------------------------------------------------===//
- #include "clang/Lex/ModuleMap.h"
- #include "clang/Lex/Lexer.h"
- #include "clang/Lex/LiteralSupport.h"
- #include "clang/Lex/LexDiagnostic.h"
- #include "clang/Basic/Diagnostic.h"
- #include "clang/Basic/FileManager.h"
- #include "clang/Basic/TargetInfo.h"
- #include "clang/Basic/TargetOptions.h"
- #include "llvm/Support/Allocator.h"
- #include "llvm/Support/FileSystem.h"
- #include "llvm/Support/Host.h"
- #include "llvm/Support/PathV2.h"
- #include "llvm/Support/raw_ostream.h"
- #include "llvm/ADT/StringRef.h"
- #include "llvm/ADT/StringSwitch.h"
- using namespace clang;
- Module::ExportDecl
- ModuleMap::resolveExport(Module *Mod,
- const Module::UnresolvedExportDecl &Unresolved,
- bool Complain) {
- // We may have just a wildcard.
- if (Unresolved.Id.empty()) {
- assert(Unresolved.Wildcard && "Invalid unresolved export");
- return Module::ExportDecl(0, true);
- }
-
- // Find the starting module.
- Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod);
- if (!Context) {
- if (Complain)
- Diags->Report(Unresolved.Id[0].second,
- diag::err_mmap_missing_module_unqualified)
- << Unresolved.Id[0].first << Mod->getFullModuleName();
-
- return Module::ExportDecl();
- }
- // Dig into the module path.
- for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) {
- Module *Sub = lookupModuleQualified(Unresolved.Id[I].first,
- Context);
- if (!Sub) {
- if (Complain)
- Diags->Report(Unresolved.Id[I].second,
- diag::err_mmap_missing_module_qualified)
- << Unresolved.Id[I].first << Context->getFullModuleName()
- << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second);
-
- return Module::ExportDecl();
- }
-
- Context = Sub;
- }
-
- return Module::ExportDecl(Context, Unresolved.Wildcard);
- }
- ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC) {
- llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs);
- Diags = llvm::IntrusiveRefCntPtr<DiagnosticsEngine>(
- new DiagnosticsEngine(DiagIDs));
- Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true);
- SourceMgr = new SourceManager(*Diags, FileMgr);
- }
- ModuleMap::~ModuleMap() {
- for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
- IEnd = Modules.end();
- I != IEnd; ++I) {
- delete I->getValue();
- }
-
- delete SourceMgr;
- }
- Module *ModuleMap::findModuleForHeader(const FileEntry *File) {
- llvm::DenseMap<const FileEntry *, Module *>::iterator Known
- = Headers.find(File);
- if (Known != Headers.end())
- return Known->second;
-
- const DirectoryEntry *Dir = File->getDir();
- llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
- StringRef DirName = Dir->getName();
- // Keep walking up the directory hierarchy, looking for a directory with
- // an umbrella header.
- do {
- llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
- = UmbrellaDirs.find(Dir);
- if (KnownDir != UmbrellaDirs.end()) {
- Module *Result = KnownDir->second;
-
- // Search up the module stack until we find a module with an umbrella
- // header.
- Module *UmbrellaModule = Result;
- while (!UmbrellaModule->UmbrellaHeader && UmbrellaModule->Parent)
- UmbrellaModule = UmbrellaModule->Parent;
-
- if (UmbrellaModule->InferSubmodules) {
- // Infer submodules for each of the directories we found between
- // the directory of the umbrella header and the directory where
- // the actual header is located.
-
- // For a framework module, the umbrella directory is the framework
- // directory, so strip off the "Headers" or "PrivateHeaders".
- bool Explicit = UmbrellaModule->InferExplicitSubmodules;
- unsigned LastSkippedDir = SkippedDirs.size();
- if (LastSkippedDir && UmbrellaModule->IsFramework) {
- if (llvm::sys::path::filename(SkippedDirs.back()->getName())
- == "PrivateHeaders") {
- // For private headers, add an explicit "Private" module.
- // FIXME: This feels somewhat hackish. Do we want to introduce
- // some kind of "umbrella directory" here?
- Result = findOrCreateModule("Private", Result,
- /*IsFramework=*/false,
- /*IsExplicit=*/true).first;
- Explicit = true;
- }
-
- --LastSkippedDir;
- }
-
- for (unsigned I = LastSkippedDir; I != 0; --I) {
- // Find or create the module that corresponds to this directory name.
- StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName());
- Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
- Explicit).first;
-
- // Associate the module and the directory.
- UmbrellaDirs[SkippedDirs[I-1]] = Result;
- // If inferred submodules export everything they import, add a
- // wildcard to the set of exports.
- if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
- Result->Exports.push_back(Module::ExportDecl(0, true));
- }
-
- // Infer a submodule with the same name as this header file.
- StringRef Name = llvm::sys::path::stem(File->getName());
- Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
- Explicit).first;
-
- // If inferred submodules export everything they import, add a
- // wildcard to the set of exports.
- if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
- Result->Exports.push_back(Module::ExportDecl(0, true));
- } else {
- // Record each of the directories we stepped through as being part of
- // the module we found, since the umbrella header covers them all.
- for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
- UmbrellaDirs[SkippedDirs[I]] = Result;
- }
-
- Headers[File] = Result;
- return Result;
- }
-
- SkippedDirs.push_back(Dir);
-
- // Retrieve our parent path.
- DirName = llvm::sys::path::parent_path(DirName);
- if (DirName.empty())
- break;
-
- // Resolve the parent path to a directory entry.
- Dir = SourceMgr->getFileManager().getDirectory(DirName);
- } while (Dir);
-
- return 0;
- }
- Module *ModuleMap::findModule(StringRef Name) {
- llvm::StringMap<Module *>::iterator Known = Modules.find(Name);
- if (Known != Modules.end())
- return Known->getValue();
-
- return 0;
- }
- Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) {
- for(; Context; Context = Context->Parent) {
- if (Module *Sub = lookupModuleQualified(Name, Context))
- return Sub;
- }
-
- return findModule(Name);
- }
- Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) {
- if (!Context)
- return findModule(Name);
-
- llvm::StringMap<Module *>::iterator Sub = Context->SubModules.find(Name);
- if (Sub != Context->SubModules.end())
- return Sub->getValue();
- return 0;
- }
- std::pair<Module *, bool>
- ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
- bool IsExplicit) {
- // Try to find an existing module with this name.
- if (Module *Found = Parent? Parent->SubModules[Name] : Modules[Name])
- return std::make_pair(Found, false);
-
- // Create a new module with this name.
- Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
- IsExplicit);
- if (Parent)
- Parent->SubModules[Name] = Result;
- else
- Modules[Name] = Result;
- return std::make_pair(Result, true);
- }
- Module *
- ModuleMap::inferFrameworkModule(StringRef ModuleName,
- const DirectoryEntry *FrameworkDir,
- Module *Parent) {
- // Check whether we've already found this module.
- if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
- return Mod;
-
- FileManager &FileMgr = SourceMgr->getFileManager();
-
- // Look for an umbrella header.
- llvm::SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
- llvm::sys::path::append(UmbrellaName, "Headers");
- llvm::sys::path::append(UmbrellaName, ModuleName + ".h");
- const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
-
- // FIXME: If there's no umbrella header, we could probably scan the
- // framework to load *everything*. But, it's not clear that this is a good
- // idea.
- if (!UmbrellaHeader)
- return 0;
-
- Module *Result = new Module(ModuleName, SourceLocation(), Parent,
- /*IsFramework=*/true, /*IsExplicit=*/false);
-
- if (Parent)
- Parent->SubModules[ModuleName] = Result;
- else
- Modules[ModuleName] = Result;
- // umbrella "umbrella-header-name"
- Result->UmbrellaHeader = UmbrellaHeader;
- Headers[UmbrellaHeader] = Result;
- UmbrellaDirs[FrameworkDir] = Result;
-
- // export *
- Result->Exports.push_back(Module::ExportDecl(0, true));
-
- // module * { export * }
- Result->InferSubmodules = true;
- Result->InferExportWildcard = true;
-
- // Look for subframeworks.
- llvm::error_code EC;
- llvm::SmallString<128> SubframeworksDirName = StringRef(FrameworkDir->getName());
- llvm::sys::path::append(SubframeworksDirName, "Frameworks");
- for (llvm::sys::fs::directory_iterator Dir(SubframeworksDirName.str(), EC),
- DirEnd;
- Dir != DirEnd && !EC; Dir.increment(EC)) {
- if (!StringRef(Dir->path()).endswith(".framework"))
- continue;
-
- if (const DirectoryEntry *SubframeworkDir
- = FileMgr.getDirectory(Dir->path())) {
- // FIXME: Do we want to warn about subframeworks without umbrella headers?
- inferFrameworkModule(llvm::sys::path::stem(Dir->path()), SubframeworkDir,
- Result);
- }
- }
-
- // Look for private headers.
- Module *ModulePrivate = 0;
- llvm::SmallString<128> PrivateHeadersDirName(FrameworkDir->getName());
- llvm::sys::path::append(PrivateHeadersDirName, "PrivateHeaders");
- for (llvm::sys::fs::directory_iterator Dir(PrivateHeadersDirName.str(), EC),
- DirEnd;
- Dir != DirEnd && !EC; Dir.increment(EC)) {
- // Check whether this entry has an extension typically associated with
- // headers.
- if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
- .Cases(".h", ".H", ".hh", ".hpp", true)
- .Default(false))
- continue;
- if (const FileEntry *PrivateHeader = FileMgr.getFile(Dir->path())) {
- // Create the "private" submodule, if we haven't done so already.
- if (!ModulePrivate) {
- ModulePrivate = findOrCreateModule("Private", Result,
- /*IsFramework=*/false,
- /*IsExplicit=*/true).first;
- }
-
- Module *Sub = findOrCreateModule(llvm::sys::path::stem(Dir->path()),
- ModulePrivate, /*IsFramework=*/false,
- /*IsExplicit=*/true).first;
- // header "the private header"
- Sub->Headers.push_back(PrivateHeader);
-
- // export *
- Sub->Exports.push_back(Module::ExportDecl(0, true));
-
- Headers[PrivateHeader] = Sub;
- }
- }
-
- return Result;
- }
- void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
- Headers[UmbrellaHeader] = Mod;
- Mod->UmbrellaHeader = UmbrellaHeader;
-
- const DirectoryEntry *UmbrellaDir = UmbrellaHeader->getDir();
- if (Mod->IsFramework)
- UmbrellaDir = SourceMgr->getFileManager().getDirectory(
- llvm::sys::path::parent_path(UmbrellaDir->getName()));
-
- UmbrellaDirs[UmbrellaDir] = Mod;
- }
- void ModuleMap::addHeader(Module *Mod, const FileEntry *Header) {
- Mod->Headers.push_back(Header);
- Headers[Header] = Mod;
- }
- const FileEntry *
- ModuleMap::getContainingModuleMapFile(Module *Module) {
- if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
- return 0;
- return SourceMgr->getFileEntryForID(
- SourceMgr->getFileID(Module->DefinitionLoc));
- }
- void ModuleMap::dump() {
- llvm::errs() << "Modules:";
- for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
- MEnd = Modules.end();
- M != MEnd; ++M)
- M->getValue()->print(llvm::errs(), 2);
-
- llvm::errs() << "Headers:";
- for (llvm::DenseMap<const FileEntry *, Module *>::iterator
- H = Headers.begin(),
- HEnd = Headers.end();
- H != HEnd; ++H) {
- llvm::errs() << " \"" << H->first->getName() << "\" -> "
- << H->second->getFullModuleName() << "\n";
- }
- }
- bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
- bool HadError = false;
- for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
- Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
- Complain);
- if (Export.getPointer() || Export.getInt())
- Mod->Exports.push_back(Export);
- else
- HadError = true;
- }
- Mod->UnresolvedExports.clear();
- return HadError;
- }
- Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
- if (Loc.isInvalid())
- return 0;
-
- // Use the expansion location to determine which module we're in.
- FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
- if (!ExpansionLoc.isFileID())
- return 0;
-
-
- const SourceManager &SrcMgr = Loc.getManager();
- FileID ExpansionFileID = ExpansionLoc.getFileID();
- const FileEntry *ExpansionFile = SrcMgr.getFileEntryForID(ExpansionFileID);
- if (!ExpansionFile)
- return 0;
-
- // Find the module that owns this header.
- return findModuleForHeader(ExpansionFile);
- }
- //----------------------------------------------------------------------------//
- // Module map file parser
- //----------------------------------------------------------------------------//
- namespace clang {
- /// \brief A token in a module map file.
- struct MMToken {
- enum TokenKind {
- EndOfFile,
- HeaderKeyword,
- Identifier,
- ExplicitKeyword,
- ExportKeyword,
- FrameworkKeyword,
- ModuleKeyword,
- Period,
- UmbrellaKeyword,
- Star,
- StringLiteral,
- LBrace,
- RBrace
- } Kind;
-
- unsigned Location;
- unsigned StringLength;
- const char *StringData;
-
- void clear() {
- Kind = EndOfFile;
- Location = 0;
- StringLength = 0;
- StringData = 0;
- }
-
- bool is(TokenKind K) const { return Kind == K; }
-
- SourceLocation getLocation() const {
- return SourceLocation::getFromRawEncoding(Location);
- }
-
- StringRef getString() const {
- return StringRef(StringData, StringLength);
- }
- };
-
- class ModuleMapParser {
- Lexer &L;
- SourceManager &SourceMgr;
- DiagnosticsEngine &Diags;
- ModuleMap ⤅
-
- /// \brief The directory that this module map resides in.
- const DirectoryEntry *Directory;
-
- /// \brief Whether an error occurred.
- bool HadError;
-
- /// \brief Default target information, used only for string literal
- /// parsing.
- TargetInfo *Target;
-
- /// \brief Stores string data for the various string literals referenced
- /// during parsing.
- llvm::BumpPtrAllocator StringData;
-
- /// \brief The current token.
- MMToken Tok;
-
- /// \brief The active module.
- Module *ActiveModule;
-
- /// \brief Consume the current token and return its location.
- SourceLocation consumeToken();
-
- /// \brief Skip tokens until we reach the a token with the given kind
- /// (or the end of the file).
- void skipUntil(MMToken::TokenKind K);
- typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2>
- ModuleId;
- bool parseModuleId(ModuleId &Id);
- void parseModuleDecl();
- void parseUmbrellaDecl();
- void parseHeaderDecl();
- void parseExportDecl();
- void parseInferredSubmoduleDecl(bool Explicit);
-
- public:
- explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
- DiagnosticsEngine &Diags,
- ModuleMap &Map,
- const DirectoryEntry *Directory)
- : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map),
- Directory(Directory), HadError(false), ActiveModule(0)
- {
- TargetOptions TargetOpts;
- TargetOpts.Triple = llvm::sys::getDefaultTargetTriple();
- Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
-
- Tok.clear();
- consumeToken();
- }
-
- bool parseModuleMapFile();
- };
- }
- SourceLocation ModuleMapParser::consumeToken() {
- retry:
- SourceLocation Result = Tok.getLocation();
- Tok.clear();
-
- Token LToken;
- L.LexFromRawLexer(LToken);
- Tok.Location = LToken.getLocation().getRawEncoding();
- switch (LToken.getKind()) {
- case tok::raw_identifier:
- Tok.StringData = LToken.getRawIdentifierData();
- Tok.StringLength = LToken.getLength();
- Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
- .Case("header", MMToken::HeaderKeyword)
- .Case("explicit", MMToken::ExplicitKeyword)
- .Case("export", MMToken::ExportKeyword)
- .Case("framework", MMToken::FrameworkKeyword)
- .Case("module", MMToken::ModuleKeyword)
- .Case("umbrella", MMToken::UmbrellaKeyword)
- .Default(MMToken::Identifier);
- break;
-
- case tok::eof:
- Tok.Kind = MMToken::EndOfFile;
- break;
-
- case tok::l_brace:
- Tok.Kind = MMToken::LBrace;
- break;
- case tok::period:
- Tok.Kind = MMToken::Period;
- break;
-
- case tok::r_brace:
- Tok.Kind = MMToken::RBrace;
- break;
-
- case tok::star:
- Tok.Kind = MMToken::Star;
- break;
-
- case tok::string_literal: {
- // Parse the string literal.
- LangOptions LangOpts;
- StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target);
- if (StringLiteral.hadError)
- goto retry;
-
- // Copy the string literal into our string data allocator.
- unsigned Length = StringLiteral.GetStringLength();
- char *Saved = StringData.Allocate<char>(Length + 1);
- memcpy(Saved, StringLiteral.GetString().data(), Length);
- Saved[Length] = 0;
-
- // Form the token.
- Tok.Kind = MMToken::StringLiteral;
- Tok.StringData = Saved;
- Tok.StringLength = Length;
- break;
- }
-
- case tok::comment:
- goto retry;
-
- default:
- Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
- HadError = true;
- goto retry;
- }
-
- return Result;
- }
- void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
- unsigned braceDepth = 0;
- do {
- switch (Tok.Kind) {
- case MMToken::EndOfFile:
- return;
- case MMToken::LBrace:
- if (Tok.is(K) && braceDepth == 0)
- return;
-
- ++braceDepth;
- break;
-
- case MMToken::RBrace:
- if (braceDepth > 0)
- --braceDepth;
- else if (Tok.is(K))
- return;
- break;
-
- default:
- if (braceDepth == 0 && Tok.is(K))
- return;
- break;
- }
-
- consumeToken();
- } while (true);
- }
- /// \brief Parse a module-id.
- ///
- /// module-id:
- /// identifier
- /// identifier '.' module-id
- ///
- /// \returns true if an error occurred, false otherwise.
- bool ModuleMapParser::parseModuleId(ModuleId &Id) {
- Id.clear();
- do {
- if (Tok.is(MMToken::Identifier)) {
- Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
- consumeToken();
- } else {
- Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
- return true;
- }
-
- if (!Tok.is(MMToken::Period))
- break;
-
- consumeToken();
- } while (true);
-
- return false;
- }
- /// \brief Parse a module declaration.
- ///
- /// module-declaration:
- /// 'explicit'[opt] 'framework'[opt] 'module' module-id { module-member* }
- ///
- /// module-member:
- /// umbrella-declaration
- /// header-declaration
- /// submodule-declaration
- /// export-declaration
- ///
- /// submodule-declaration:
- /// module-declaration
- /// inferred-submodule-declaration
- void ModuleMapParser::parseModuleDecl() {
- assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
- Tok.is(MMToken::FrameworkKeyword));
- // Parse 'explicit' or 'framework' keyword, if present.
- SourceLocation ExplicitLoc;
- bool Explicit = false;
- bool Framework = false;
- // Parse 'explicit' keyword, if present.
- if (Tok.is(MMToken::ExplicitKeyword)) {
- ExplicitLoc = consumeToken();
- Explicit = true;
- }
- // Parse 'framework' keyword, if present.
- if (Tok.is(MMToken::FrameworkKeyword)) {
- consumeToken();
- Framework = true;
- }
-
- // Parse 'module' keyword.
- if (!Tok.is(MMToken::ModuleKeyword)) {
- Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
- consumeToken();
- HadError = true;
- return;
- }
- consumeToken(); // 'module' keyword
- // If we have a wildcard for the module name, this is an inferred submodule.
- // Parse it.
- if (Tok.is(MMToken::Star))
- return parseInferredSubmoduleDecl(Explicit);
-
- // Parse the module name.
- ModuleId Id;
- if (parseModuleId(Id)) {
- HadError = true;
- return;
- }
-
- if (ActiveModule) {
- if (Id.size() > 1) {
- Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
- << SourceRange(Id.front().second, Id.back().second);
-
- HadError = true;
- return;
- }
- } else if (Id.size() == 1 && Explicit) {
- // Top-level modules can't be explicit.
- Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
- Explicit = false;
- ExplicitLoc = SourceLocation();
- HadError = true;
- }
-
- Module *PreviousActiveModule = ActiveModule;
- if (Id.size() > 1) {
- // This module map defines a submodule. Go find the module of which it
- // is a submodule.
- ActiveModule = 0;
- for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
- if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
- ActiveModule = Next;
- continue;
- }
-
- if (ActiveModule) {
- Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
- << Id[I].first << ActiveModule->getTopLevelModule();
- } else {
- Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
- }
- HadError = true;
- return;
- }
- }
-
- StringRef ModuleName = Id.back().first;
- SourceLocation ModuleNameLoc = Id.back().second;
-
- // Parse the opening brace.
- if (!Tok.is(MMToken::LBrace)) {
- Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
- << ModuleName;
- HadError = true;
- return;
- }
- SourceLocation LBraceLoc = consumeToken();
-
- // Determine whether this (sub)module has already been defined.
- llvm::StringMap<Module *> &ModuleSpace
- = ActiveModule? ActiveModule->SubModules : Map.Modules;
- llvm::StringMap<Module *>::iterator ExistingModule
- = ModuleSpace.find(ModuleName);
- if (ExistingModule != ModuleSpace.end()) {
- Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
- << ModuleName;
- Diags.Report(ExistingModule->getValue()->DefinitionLoc,
- diag::note_mmap_prev_definition);
-
- // Skip the module definition.
- skipUntil(MMToken::RBrace);
- if (Tok.is(MMToken::RBrace))
- consumeToken();
-
- HadError = true;
- return;
- }
- // Start defining this module.
- ActiveModule = new Module(ModuleName, ModuleNameLoc, ActiveModule, Framework,
- Explicit);
- ModuleSpace[ModuleName] = ActiveModule;
-
- bool Done = false;
- do {
- switch (Tok.Kind) {
- case MMToken::EndOfFile:
- case MMToken::RBrace:
- Done = true;
- break;
-
- case MMToken::ExplicitKeyword:
- case MMToken::FrameworkKeyword:
- case MMToken::ModuleKeyword:
- parseModuleDecl();
- break;
-
- case MMToken::ExportKeyword:
- parseExportDecl();
- break;
-
- case MMToken::HeaderKeyword:
- parseHeaderDecl();
- break;
-
- case MMToken::UmbrellaKeyword:
- parseUmbrellaDecl();
- break;
- default:
- Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
- consumeToken();
- break;
- }
- } while (!Done);
- if (Tok.is(MMToken::RBrace))
- consumeToken();
- else {
- Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
- Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
- HadError = true;
- }
- // We're done parsing this module. Pop back to the previous module.
- ActiveModule = PreviousActiveModule;
- }
- /// \brief Append to \p Paths the set of paths needed to get to the
- /// subframework in which the given module lives.
- void appendSubframeworkPaths(Module *Mod, llvm::SmallVectorImpl<char> &Path) {
- // Collect the framework names from the given module to the top-level module.
- llvm::SmallVector<StringRef, 2> Paths;
- for (; Mod; Mod = Mod->Parent) {
- if (Mod->IsFramework)
- Paths.push_back(Mod->Name);
- }
-
- if (Paths.empty())
- return;
-
- // Add Frameworks/Name.framework for each subframework.
- for (unsigned I = Paths.size() - 1; I != 0; --I) {
- llvm::sys::path::append(Path, "Frameworks");
- llvm::sys::path::append(Path, Paths[I-1] + ".framework");
- }
- }
- /// \brief Parse an umbrella header declaration.
- ///
- /// umbrella-declaration:
- /// 'umbrella' string-literal
- void ModuleMapParser::parseUmbrellaDecl() {
- assert(Tok.is(MMToken::UmbrellaKeyword));
- SourceLocation UmbrellaLoc = consumeToken();
-
- // Parse the header name.
- if (!Tok.is(MMToken::StringLiteral)) {
- Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
- << "umbrella";
- HadError = true;
- return;
- }
- std::string FileName = Tok.getString();
- SourceLocation FileNameLoc = consumeToken();
- // Check whether we already have an umbrella header.
- if (ActiveModule->UmbrellaHeader) {
- Diags.Report(FileNameLoc, diag::err_mmap_umbrella_header_conflict)
- << ActiveModule->getFullModuleName()
- << ActiveModule->UmbrellaHeader->getName();
- HadError = true;
- return;
- }
-
- // Look for this file.
- llvm::SmallString<128> PathName;
- const FileEntry *File = 0;
-
- if (llvm::sys::path::is_absolute(FileName)) {
- PathName = FileName;
- File = SourceMgr.getFileManager().getFile(PathName);
- } else {
- // Search for the header file within the search directory.
- PathName += Directory->getName();
- unsigned PathLength = PathName.size();
-
- if (ActiveModule->isPartOfFramework()) {
- appendSubframeworkPaths(ActiveModule, PathName);
-
- // Check whether this file is in the public headers.
- llvm::sys::path::append(PathName, "Headers");
- llvm::sys::path::append(PathName, FileName);
- File = SourceMgr.getFileManager().getFile(PathName);
- if (!File) {
- // Check whether this file is in the private headers.
- PathName.resize(PathLength);
- llvm::sys::path::append(PathName, "PrivateHeaders");
- llvm::sys::path::append(PathName, FileName);
- File = SourceMgr.getFileManager().getFile(PathName);
- }
- } else {
- // Lookup for normal headers.
- llvm::sys::path::append(PathName, FileName);
- File = SourceMgr.getFileManager().getFile(PathName);
- }
- }
-
- // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
- // Come up with a lazy way to do this.
- if (File) {
- const DirectoryEntry *UmbrellaDir = File->getDir();
- if (ActiveModule->IsFramework) {
- // For framework modules, use the framework directory as the umbrella
- // directory.
- UmbrellaDir = SourceMgr.getFileManager().getDirectory(
- llvm::sys::path::parent_path(UmbrellaDir->getName()));
- }
-
- if (const Module *OwningModule = Map.Headers[File]) {
- Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
- << FileName << OwningModule->getFullModuleName();
- HadError = true;
- } else if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) {
- Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
- << OwningModule->getFullModuleName();
- HadError = true;
- } else {
- // Record this umbrella header.
- Map.setUmbrellaHeader(ActiveModule, File);
- }
- } else {
- Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
- << true << FileName;
- HadError = true;
- }
- }
- /// \brief Parse a header declaration.
- ///
- /// header-declaration:
- /// 'header' string-literal
- void ModuleMapParser::parseHeaderDecl() {
- assert(Tok.is(MMToken::HeaderKeyword));
- consumeToken();
- // Parse the header name.
- if (!Tok.is(MMToken::StringLiteral)) {
- Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
- << "header";
- HadError = true;
- return;
- }
- std::string FileName = Tok.getString();
- SourceLocation FileNameLoc = consumeToken();
-
- // Look for this file.
- const FileEntry *File = 0;
- llvm::SmallString<128> PathName;
- if (llvm::sys::path::is_absolute(FileName)) {
- PathName = FileName;
- File = SourceMgr.getFileManager().getFile(PathName);
- } else {
- // Search for the header file within the search directory.
- PathName += Directory->getName();
- unsigned PathLength = PathName.size();
-
- if (ActiveModule->isPartOfFramework()) {
- appendSubframeworkPaths(ActiveModule, PathName);
-
- // Check whether this file is in the public headers.
- llvm::sys::path::append(PathName, "Headers");
- llvm::sys::path::append(PathName, FileName);
- File = SourceMgr.getFileManager().getFile(PathName);
-
- if (!File) {
- // Check whether this file is in the private headers.
- PathName.resize(PathLength);
- llvm::sys::path::append(PathName, "PrivateHeaders");
- llvm::sys::path::append(PathName, FileName);
- File = SourceMgr.getFileManager().getFile(PathName);
- }
- } else {
- // Lookup for normal headers.
- llvm::sys::path::append(PathName, FileName);
- File = SourceMgr.getFileManager().getFile(PathName);
- }
- }
-
- // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
- // Come up with a lazy way to do this.
- if (File) {
- if (const Module *OwningModule = Map.Headers[File]) {
- Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
- << FileName << OwningModule->getFullModuleName();
- HadError = true;
- } else {
- // Record this file.
- Map.addHeader(ActiveModule, File);
- }
- } else {
- Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
- << false << FileName;
- HadError = true;
- }
- }
- /// \brief Parse a module export declaration.
- ///
- /// export-declaration:
- /// 'export' wildcard-module-id
- ///
- /// wildcard-module-id:
- /// identifier
- /// '*'
- /// identifier '.' wildcard-module-id
- void ModuleMapParser::parseExportDecl() {
- assert(Tok.is(MMToken::ExportKeyword));
- SourceLocation ExportLoc = consumeToken();
-
- // Parse the module-id with an optional wildcard at the end.
- ModuleId ParsedModuleId;
- bool Wildcard = false;
- do {
- if (Tok.is(MMToken::Identifier)) {
- ParsedModuleId.push_back(std::make_pair(Tok.getString(),
- Tok.getLocation()));
- consumeToken();
-
- if (Tok.is(MMToken::Period)) {
- consumeToken();
- continue;
- }
-
- break;
- }
-
- if(Tok.is(MMToken::Star)) {
- Wildcard = true;
- consumeToken();
- break;
- }
-
- Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id);
- HadError = true;
- return;
- } while (true);
-
- Module::UnresolvedExportDecl Unresolved = {
- ExportLoc, ParsedModuleId, Wildcard
- };
- ActiveModule->UnresolvedExports.push_back(Unresolved);
- }
- void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) {
- assert(Tok.is(MMToken::Star));
- SourceLocation StarLoc = consumeToken();
- bool Failed = false;
-
- // Inferred modules must be submodules.
- if (!ActiveModule) {
- Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
- Failed = true;
- }
-
- // Inferred modules must have umbrella headers.
- if (!Failed && !ActiveModule->UmbrellaHeader) {
- Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
- Failed = true;
- }
-
- // Check for redefinition of an inferred module.
- if (!Failed && ActiveModule->InferSubmodules) {
- Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
- if (ActiveModule->InferredSubmoduleLoc.isValid())
- Diags.Report(ActiveModule->InferredSubmoduleLoc,
- diag::note_mmap_prev_definition);
- Failed = true;
- }
-
- // If there were any problems with this inferred submodule, skip its body.
- if (Failed) {
- if (Tok.is(MMToken::LBrace)) {
- consumeToken();
- skipUntil(MMToken::RBrace);
- if (Tok.is(MMToken::RBrace))
- consumeToken();
- }
- HadError = true;
- return;
- }
-
- // Note that we have an inferred submodule.
- ActiveModule->InferSubmodules = true;
- ActiveModule->InferredSubmoduleLoc = StarLoc;
- ActiveModule->InferExplicitSubmodules = Explicit;
-
- // Parse the opening brace.
- if (!Tok.is(MMToken::LBrace)) {
- Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
- HadError = true;
- return;
- }
- SourceLocation LBraceLoc = consumeToken();
- // Parse the body of the inferred submodule.
- bool Done = false;
- do {
- switch (Tok.Kind) {
- case MMToken::EndOfFile:
- case MMToken::RBrace:
- Done = true;
- break;
-
- case MMToken::ExportKeyword: {
- consumeToken();
- if (Tok.is(MMToken::Star))
- ActiveModule->InferExportWildcard = true;
- else
- Diags.Report(Tok.getLocation(),
- diag::err_mmap_expected_export_wildcard);
- consumeToken();
- break;
- }
-
- case MMToken::ExplicitKeyword:
- case MMToken::ModuleKeyword:
- case MMToken::HeaderKeyword:
- case MMToken::UmbrellaKeyword:
- default:
- Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member);
- consumeToken();
- break;
- }
- } while (!Done);
-
- if (Tok.is(MMToken::RBrace))
- consumeToken();
- else {
- Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
- Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
- HadError = true;
- }
- }
- /// \brief Parse a module map file.
- ///
- /// module-map-file:
- /// module-declaration*
- bool ModuleMapParser::parseModuleMapFile() {
- do {
- switch (Tok.Kind) {
- case MMToken::EndOfFile:
- return HadError;
-
- case MMToken::ExplicitKeyword:
- case MMToken::ModuleKeyword:
- case MMToken::FrameworkKeyword:
- parseModuleDecl();
- break;
-
- case MMToken::ExportKeyword:
- case MMToken::HeaderKeyword:
- case MMToken::Identifier:
- case MMToken::LBrace:
- case MMToken::Period:
- case MMToken::RBrace:
- case MMToken::Star:
- case MMToken::StringLiteral:
- case MMToken::UmbrellaKeyword:
- Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
- HadError = true;
- consumeToken();
- break;
- }
- } while (true);
-
- return HadError;
- }
- bool ModuleMap::parseModuleMapFile(const FileEntry *File) {
- FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
- const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
- if (!Buffer)
- return true;
-
- // Parse this module map file.
- Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, LangOpts);
- Diags->getClient()->BeginSourceFile(LangOpts);
- ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir());
- bool Result = Parser.parseModuleMapFile();
- Diags->getClient()->EndSourceFile();
-
- return Result;
- }
|