1 //===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ModuleManager class, which manages a set of loaded
11 // modules for the ASTReader.
13 //===----------------------------------------------------------------------===//
14 #include "clang/Serialization/ModuleManager.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include "llvm/Support/system_error.h"
20 #include "llvm/Support/GraphWriter.h"
23 using namespace clang;
24 using namespace serialization;
26 ModuleFile *ModuleManager::lookup(StringRef Name) {
27 const FileEntry *Entry = FileMgr.getFile(Name);
28 return Modules[Entry];
31 llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
32 const FileEntry *Entry = FileMgr.getFile(Name);
33 return InMemoryBuffers[Entry];
36 std::pair<ModuleFile *, bool>
37 ModuleManager::addModule(StringRef FileName, ModuleKind Type,
38 ModuleFile *ImportedBy, unsigned Generation,
39 std::string &ErrorStr) {
40 const FileEntry *Entry = FileMgr.getFile(FileName);
41 if (!Entry && FileName != "-") {
42 ErrorStr = "file not found";
43 return std::make_pair(static_cast<ModuleFile*>(0), false);
46 // Check whether we already loaded this module, before
47 ModuleFile *&ModuleEntry = Modules[Entry];
48 bool NewModule = false;
50 // Allocate a new module.
51 ModuleFile *New = new ModuleFile(Type, Generation);
52 New->FileName = FileName.str();
58 // Load the contents of the module
59 if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) {
60 // The buffer was already provided for us.
61 assert(Buffer && "Passed null buffer");
62 New->Buffer.reset(Buffer);
66 if (FileName == "-") {
67 ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
69 ErrorStr = ec.message();
71 New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr));
74 return std::make_pair(static_cast<ModuleFile*>(0), false);
77 // Initialize the stream
78 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
79 (const unsigned char *)New->Buffer->getBufferEnd()); }
82 ModuleEntry->ImportedBy.insert(ImportedBy);
83 ImportedBy->Imports.insert(ModuleEntry);
85 ModuleEntry->DirectlyImported = true;
88 return std::make_pair(ModuleEntry, NewModule);
92 /// \brief Predicate that checks whether a module file occurs within
94 class IsInModuleFileSet : public std::unary_function<ModuleFile *, bool> {
95 llvm::SmallPtrSet<ModuleFile *, 4> &Removed;
98 IsInModuleFileSet(llvm::SmallPtrSet<ModuleFile *, 4> &Removed)
99 : Removed(Removed) { }
101 bool operator()(ModuleFile *MF) const {
102 return Removed.count(MF);
107 void ModuleManager::removeModules(ModuleIterator first, ModuleIterator last) {
111 // Collect the set of module file pointers that we'll be removing.
112 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
114 // Remove any references to the now-destroyed modules.
115 IsInModuleFileSet checkInSet(victimSet);
116 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
117 Chain[i]->ImportedBy.remove_if(checkInSet);
120 // Delete the modules and erase them from the various structures.
121 for (ModuleIterator victim = first; victim != last; ++victim) {
122 Modules.erase((*victim)->File);
126 // Remove the modules from the chain.
127 Chain.erase(first, last);
130 void ModuleManager::addInMemoryBuffer(StringRef FileName,
131 llvm::MemoryBuffer *Buffer) {
133 const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
134 Buffer->getBufferSize(), 0);
135 InMemoryBuffers[Entry] = Buffer;
138 ModuleManager::ModuleManager(FileManager &FileMgr) : FileMgr(FileMgr) { }
140 ModuleManager::~ModuleManager() {
141 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
142 delete Chain[e - i - 1];
145 void ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
149 // Record the number of incoming edges for each module. When we
150 // encounter a module with no incoming edges, push it into the queue
151 // to seed the queue.
152 SmallVector<ModuleFile *, 4> Queue;
154 llvm::DenseMap<ModuleFile *, unsigned> UnusedIncomingEdges;
155 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
156 if (unsigned Size = (*M)->ImportedBy.size())
157 UnusedIncomingEdges[*M] = Size;
162 llvm::SmallPtrSet<ModuleFile *, 4> Skipped;
163 unsigned QueueStart = 0;
164 while (QueueStart < Queue.size()) {
165 ModuleFile *CurrentModule = Queue[QueueStart++];
167 // Check whether this module should be skipped.
168 if (Skipped.count(CurrentModule))
171 if (Visitor(*CurrentModule, UserData)) {
172 // The visitor has requested that cut off visitation of any
173 // module that the current module depends on. To indicate this
174 // behavior, we mark all of the reachable modules as having N
175 // incoming edges (which is impossible otherwise).
176 SmallVector<ModuleFile *, 4> Stack;
177 Stack.push_back(CurrentModule);
178 Skipped.insert(CurrentModule);
179 while (!Stack.empty()) {
180 ModuleFile *NextModule = Stack.back();
183 // For any module that this module depends on, push it on the
184 // stack (if it hasn't already been marked as visited).
185 for (llvm::SetVector<ModuleFile *>::iterator
186 M = NextModule->Imports.begin(),
187 MEnd = NextModule->Imports.end();
189 if (Skipped.insert(*M))
196 // For any module that this module depends on, push it on the
197 // stack (if it hasn't already been marked as visited).
198 for (llvm::SetVector<ModuleFile *>::iterator M = CurrentModule->Imports.begin(),
199 MEnd = CurrentModule->Imports.end();
202 // Remove our current module as an impediment to visiting the
203 // module we depend on. If we were the last unvisited module
204 // that depends on this particular module, push it into the
205 // queue to be visited.
206 unsigned &NumUnusedEdges = UnusedIncomingEdges[*M];
207 if (NumUnusedEdges && (--NumUnusedEdges == 0))
213 /// \brief Perform a depth-first visit of the current module.
214 static bool visitDepthFirst(ModuleFile &M,
215 bool (*Visitor)(ModuleFile &M, bool Preorder,
218 llvm::SmallPtrSet<ModuleFile *, 4> &Visited) {
219 // Preorder visitation
220 if (Visitor(M, /*Preorder=*/true, UserData))
224 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
225 IMEnd = M.Imports.end();
227 if (!Visited.insert(*IM))
230 if (visitDepthFirst(**IM, Visitor, UserData, Visited))
234 // Postorder visitation
235 return Visitor(M, /*Preorder=*/false, UserData);
238 void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
241 llvm::SmallPtrSet<ModuleFile *, 4> Visited;
242 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
243 if (!Visited.insert(Chain[I]))
246 if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
254 struct GraphTraits<ModuleManager> {
255 typedef ModuleFile NodeType;
256 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
257 typedef ModuleManager::ModuleConstIterator nodes_iterator;
259 static ChildIteratorType child_begin(NodeType *Node) {
260 return Node->Imports.begin();
263 static ChildIteratorType child_end(NodeType *Node) {
264 return Node->Imports.end();
267 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
268 return Manager.begin();
271 static nodes_iterator nodes_end(const ModuleManager &Manager) {
272 return Manager.end();
277 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
278 explicit DOTGraphTraits(bool IsSimple = false)
279 : DefaultDOTGraphTraits(IsSimple) { }
281 static bool renderGraphFromBottomUp() {
285 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
286 return llvm::sys::path::stem(M->FileName);
291 void ModuleManager::viewGraph() {
292 llvm::ViewGraph(*this, "Modules");