//===--- 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/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 modul