2011-08-26 04:47:51 +08:00
|
|
|
//===--- ModuleManager.cpp - Module Manager ---------------------*- 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 ModuleManager class, which manages a set of loaded
|
|
|
|
// modules for the ASTReader.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Serialization/ModuleManager.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Support/system_error.h"
|
|
|
|
|
2011-10-12 03:27:55 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include "llvm/Support/GraphWriter.h"
|
|
|
|
#endif
|
|
|
|
|
2011-08-26 04:47:51 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace serialization;
|
|
|
|
|
2011-12-01 07:21:26 +08:00
|
|
|
ModuleFile *ModuleManager::lookup(StringRef Name) {
|
2011-08-26 04:47:51 +08:00
|
|
|
const FileEntry *Entry = FileMgr.getFile(Name);
|
|
|
|
return Modules[Entry];
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
|
|
|
|
const FileEntry *Entry = FileMgr.getFile(Name);
|
|
|
|
return InMemoryBuffers[Entry];
|
|
|
|
}
|
|
|
|
|
2011-12-01 07:21:26 +08:00
|
|
|
std::pair<ModuleFile *, bool>
|
2011-08-26 04:47:51 +08:00
|
|
|
ModuleManager::addModule(StringRef FileName, ModuleKind Type,
|
2012-01-19 04:56:22 +08:00
|
|
|
ModuleFile *ImportedBy, unsigned Generation,
|
|
|
|
std::string &ErrorStr) {
|
2011-08-26 04:47:51 +08:00
|
|
|
const FileEntry *Entry = FileMgr.getFile(FileName);
|
|
|
|
if (!Entry && FileName != "-") {
|
|
|
|
ErrorStr = "file not found";
|
2011-12-01 07:21:26 +08:00
|
|
|
return std::make_pair(static_cast<ModuleFile*>(0), false);
|
2011-08-26 04:47:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether we already loaded this module, before
|
2011-12-01 07:21:26 +08:00
|
|
|
ModuleFile *&ModuleEntry = Modules[Entry];
|
2011-08-26 04:47:51 +08:00
|
|
|
bool NewModule = false;
|
|
|
|
if (!ModuleEntry) {
|
|
|
|
// Allocate a new module.
|
2012-01-19 04:56:22 +08:00
|
|
|
ModuleFile *New = new ModuleFile(Type, Generation);
|
2011-08-26 04:47:51 +08:00
|
|
|
New->FileName = FileName.str();
|
|
|
|
Chain.push_back(New);
|
|
|
|
NewModule = true;
|
|
|
|
ModuleEntry = New;
|
|
|
|
|
|
|
|
// Load the contents of the module
|
|
|
|
if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) {
|
|
|
|
// The buffer was already provided for us.
|
|
|
|
assert(Buffer && "Passed null buffer");
|
|
|
|
New->Buffer.reset(Buffer);
|
|
|
|
} else {
|
|
|
|
// Open the AST file.
|
|
|
|
llvm::error_code ec;
|
|
|
|
if (FileName == "-") {
|
|
|
|
ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
|
|
|
|
if (ec)
|
|
|
|
ErrorStr = ec.message();
|
|
|
|
} else
|
|
|
|
New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr));
|
|
|
|
|
|
|
|
if (!New->Buffer)
|
2011-12-01 07:21:26 +08:00
|
|
|
return std::make_pair(static_cast<ModuleFile*>(0), false);
|
2011-08-26 04:47:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the stream
|
|
|
|
New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
|
|
|
|
(const unsigned char *)New->Buffer->getBufferEnd()); }
|
|
|
|
|
|
|
|
if (ImportedBy) {
|
|
|
|
ModuleEntry->ImportedBy.insert(ImportedBy);
|
|
|
|
ImportedBy->Imports.insert(ModuleEntry);
|
|
|
|
} else {
|
|
|
|
ModuleEntry->DirectlyImported = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_pair(ModuleEntry, NewModule);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleManager::addInMemoryBuffer(StringRef FileName,
|
|
|
|
llvm::MemoryBuffer *Buffer) {
|
|
|
|
|
|
|
|
const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
|
|
|
|
Buffer->getBufferSize(), 0);
|
|
|
|
InMemoryBuffers[Entry] = Buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModuleManager::ModuleManager(const FileSystemOptions &FSO) : FileMgr(FSO) { }
|
|
|
|
|
|
|
|
ModuleManager::~ModuleManager() {
|
|
|
|
for (unsigned i = 0, e = Chain.size(); i != e; ++i)
|
|
|
|
delete Chain[e - i - 1];
|
|
|
|
}
|
|
|
|
|
2011-12-01 07:21:26 +08:00
|
|
|
void ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
|
2011-08-26 04:47:51 +08:00
|
|
|
void *UserData) {
|
|
|
|
unsigned N = size();
|
|
|
|
|
|
|
|
// Record the number of incoming edges for each module. When we
|
|
|
|
// encounter a module with no incoming edges, push it into the queue
|
|
|
|
// to seed the queue.
|
2011-12-01 07:21:26 +08:00
|
|
|
SmallVector<ModuleFile *, 4> Queue;
|
2011-08-26 04:47:51 +08:00
|
|
|
Queue.reserve(N);
|
2011-12-01 07:21:26 +08:00
|
|
|
llvm::DenseMap<ModuleFile *, unsigned> UnusedIncomingEdges;
|
2011-08-26 04:47:51 +08:00
|
|
|
for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
|
|
|
|
if (unsigned Size = (*M)->ImportedBy.size())
|
|
|
|
UnusedIncomingEdges[*M] = Size;
|
|
|
|
else
|
|
|
|
Queue.push_back(*M);
|
|
|
|
}
|
|
|
|
|
2011-12-01 07:21:26 +08:00
|
|
|
llvm::SmallPtrSet<ModuleFile *, 4> Skipped;
|
2011-08-26 04:47:51 +08:00
|
|
|
unsigned QueueStart = 0;
|
|
|
|
while (QueueStart < Queue.size()) {
|
2011-12-01 07:21:26 +08:00
|
|
|
ModuleFile *CurrentModule = Queue[QueueStart++];
|
2011-08-26 04:47:51 +08:00
|
|
|
|
|
|
|
// Check whether this module should be skipped.
|
|
|
|
if (Skipped.count(CurrentModule))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (Visitor(*CurrentModule, UserData)) {
|
|
|
|
// The visitor has requested that cut off visitation of any
|
|
|
|
// module that the current module depends on. To indicate this
|
|
|
|
// behavior, we mark all of the reachable modules as having N
|
|
|
|
// incoming edges (which is impossible otherwise).
|
2011-12-01 07:21:26 +08:00
|
|
|
SmallVector<ModuleFile *, 4> Stack;
|
2011-08-26 04:47:51 +08:00
|
|
|
Stack.push_back(CurrentModule);
|
|
|
|
Skipped.insert(CurrentModule);
|
|
|
|
while (!Stack.empty()) {
|
2011-12-01 07:21:26 +08:00
|
|
|
ModuleFile *NextModule = Stack.back();
|
2011-08-26 04:47:51 +08:00
|
|
|
Stack.pop_back();
|
|
|
|
|
|
|
|
// For any module that this module depends on, push it on the
|
|
|
|
// stack (if it hasn't already been marked as visited).
|
2011-12-01 07:21:26 +08:00
|
|
|
for (llvm::SetVector<ModuleFile *>::iterator
|
2011-08-26 04:47:51 +08:00
|
|
|
M = NextModule->Imports.begin(),
|
|
|
|
MEnd = NextModule->Imports.end();
|
|
|
|
M != MEnd; ++M) {
|
|
|
|
if (Skipped.insert(*M))
|
|
|
|
Stack.push_back(*M);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For any module that this module depends on, push it on the
|
|
|
|
// stack (if it hasn't already been marked as visited).
|
2011-12-01 07:21:26 +08:00
|
|
|
for (llvm::SetVector<ModuleFile *>::iterator M = CurrentModule->Imports.begin(),
|
2011-08-26 04:47:51 +08:00
|
|
|
MEnd = CurrentModule->Imports.end();
|
|
|
|
M != MEnd; ++M) {
|
|
|
|
|
|
|
|
// Remove our current module as an impediment to visiting the
|
|
|
|
// module we depend on. If we were the last unvisited module
|
|
|
|
// that depends on this particular module, push it into the
|
|
|
|
// queue to be visited.
|
|
|
|
unsigned &NumUnusedEdges = UnusedIncomingEdges[*M];
|
|
|
|
if (NumUnusedEdges && (--NumUnusedEdges == 0))
|
|
|
|
Queue.push_back(*M);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Perform a depth-first visit of the current module.
|
2011-12-01 07:21:26 +08:00
|
|
|
static bool visitDepthFirst(ModuleFile &M,
|
|
|
|
bool (*Visitor)(ModuleFile &M, bool Preorder,
|
2011-08-26 04:47:51 +08:00
|
|
|
void *UserData),
|
|
|
|
void *UserData,
|
2011-12-01 07:21:26 +08:00
|
|
|
llvm::SmallPtrSet<ModuleFile *, 4> &Visited) {
|
2011-08-26 04:47:51 +08:00
|
|
|
// Preorder visitation
|
|
|
|
if (Visitor(M, /*Preorder=*/true, UserData))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Visit children
|
2011-12-01 07:21:26 +08:00
|
|
|
for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
|
2011-08-26 04:47:51 +08:00
|
|
|
IMEnd = M.Imports.end();
|
|
|
|
IM != IMEnd; ++IM) {
|
|
|
|
if (!Visited.insert(*IM))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (visitDepthFirst(**IM, Visitor, UserData, Visited))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Postorder visitation
|
|
|
|
return Visitor(M, /*Preorder=*/false, UserData);
|
|
|
|
}
|
|
|
|
|
2011-12-01 07:21:26 +08:00
|
|
|
void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
|
2011-08-26 04:47:51 +08:00
|
|
|
void *UserData),
|
|
|
|
void *UserData) {
|
2011-12-01 07:21:26 +08:00
|
|
|
llvm::SmallPtrSet<ModuleFile *, 4> Visited;
|
2011-08-26 04:47:51 +08:00
|
|
|
for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
|
|
|
|
if (!Visited.insert(Chain[I]))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-10-12 03:27:55 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
struct GraphTraits<ModuleManager> {
|
2011-12-01 07:21:26 +08:00
|
|
|
typedef ModuleFile NodeType;
|
|
|
|
typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
|
2011-10-12 03:27:55 +08:00
|
|
|
typedef ModuleManager::ModuleConstIterator nodes_iterator;
|
|
|
|
|
|
|
|
static ChildIteratorType child_begin(NodeType *Node) {
|
|
|
|
return Node->Imports.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
static ChildIteratorType child_end(NodeType *Node) {
|
|
|
|
return Node->Imports.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
static nodes_iterator nodes_begin(const ModuleManager &Manager) {
|
|
|
|
return Manager.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
static nodes_iterator nodes_end(const ModuleManager &Manager) {
|
|
|
|
return Manager.end();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
|
|
|
|
explicit DOTGraphTraits(bool IsSimple = false)
|
|
|
|
: DefaultDOTGraphTraits(IsSimple) { }
|
|
|
|
|
|
|
|
static bool renderGraphFromBottomUp() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-01 07:21:26 +08:00
|
|
|
std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
|
2011-10-12 03:27:55 +08:00
|
|
|
return llvm::sys::path::stem(M->FileName);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleManager::viewGraph() {
|
|
|
|
llvm::ViewGraph(*this, "Modules");
|
|
|
|
}
|
|
|
|
#endif
|