blob: efe442101bb61a0a1a4c09042a5f50b2108acb81 [file] [log] [blame]
Douglas Gregord44252e2011-08-25 20:47:51 +00001//===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ModuleManager class, which manages a set of loaded
11// modules for the ASTReader.
12//
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"
18
Douglas Gregor9d7c1a22011-10-11 19:27:55 +000019#ifndef NDEBUG
20#include "llvm/Support/GraphWriter.h"
21#endif
22
Douglas Gregord44252e2011-08-25 20:47:51 +000023using namespace clang;
24using namespace serialization;
25
Douglas Gregorde3ef502011-11-30 23:21:26 +000026ModuleFile *ModuleManager::lookup(StringRef Name) {
Douglas Gregord44252e2011-08-25 20:47:51 +000027 const FileEntry *Entry = FileMgr.getFile(Name);
28 return Modules[Entry];
29}
30
31llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
32 const FileEntry *Entry = FileMgr.getFile(Name);
33 return InMemoryBuffers[Entry];
34}
35
Douglas Gregorde3ef502011-11-30 23:21:26 +000036std::pair<ModuleFile *, bool>
Douglas Gregord44252e2011-08-25 20:47:51 +000037ModuleManager::addModule(StringRef FileName, ModuleKind Type,
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +000038 ModuleFile *ImportedBy, unsigned Generation,
39 std::string &ErrorStr) {
Douglas Gregord44252e2011-08-25 20:47:51 +000040 const FileEntry *Entry = FileMgr.getFile(FileName);
41 if (!Entry && FileName != "-") {
42 ErrorStr = "file not found";
Douglas Gregorde3ef502011-11-30 23:21:26 +000043 return std::make_pair(static_cast<ModuleFile*>(0), false);
Douglas Gregord44252e2011-08-25 20:47:51 +000044 }
45
46 // Check whether we already loaded this module, before
Douglas Gregorde3ef502011-11-30 23:21:26 +000047 ModuleFile *&ModuleEntry = Modules[Entry];
Douglas Gregord44252e2011-08-25 20:47:51 +000048 bool NewModule = false;
49 if (!ModuleEntry) {
50 // Allocate a new module.
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +000051 ModuleFile *New = new ModuleFile(Type, Generation);
Douglas Gregord44252e2011-08-25 20:47:51 +000052 New->FileName = FileName.str();
Argyrios Kyrtzidisaedf7142012-10-03 01:58:42 +000053 New->File = Entry;
Douglas Gregord44252e2011-08-25 20:47:51 +000054 Chain.push_back(New);
55 NewModule = true;
56 ModuleEntry = New;
57
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);
63 } else {
64 // Open the AST file.
65 llvm::error_code ec;
66 if (FileName == "-") {
67 ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
68 if (ec)
69 ErrorStr = ec.message();
70 } else
71 New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr));
72
73 if (!New->Buffer)
Douglas Gregorde3ef502011-11-30 23:21:26 +000074 return std::make_pair(static_cast<ModuleFile*>(0), false);
Douglas Gregord44252e2011-08-25 20:47:51 +000075 }
76
77 // Initialize the stream
78 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
79 (const unsigned char *)New->Buffer->getBufferEnd()); }
80
81 if (ImportedBy) {
82 ModuleEntry->ImportedBy.insert(ImportedBy);
83 ImportedBy->Imports.insert(ModuleEntry);
84 } else {
85 ModuleEntry->DirectlyImported = true;
86 }
87
88 return std::make_pair(ModuleEntry, NewModule);
89}
90
Douglas Gregor188dbef2012-11-07 17:46:15 +000091namespace {
92 /// \brief Predicate that checks whether a module file occurs within
93 /// the given set.
94 class IsInModuleFileSet : public std::unary_function<ModuleFile *, bool> {
95 llvm::SmallPtrSet<ModuleFile *, 4> &Removed;
96
97 public:
98 IsInModuleFileSet(llvm::SmallPtrSet<ModuleFile *, 4> &Removed)
99 : Removed(Removed) { }
100
101 bool operator()(ModuleFile *MF) const {
102 return Removed.count(MF);
103 }
104 };
105}
106
107void ModuleManager::removeModules(ModuleIterator first, ModuleIterator last) {
108 if (first == last)
109 return;
110
111 // Collect the set of module file pointers that we'll be removing.
112 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
113
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);
118 }
119
120 // Delete the modules and erase them from the various structures.
121 for (ModuleIterator victim = first; victim != last; ++victim) {
122 Modules.erase((*victim)->File);
123 delete *victim;
124 }
125
126 // Remove the modules from the chain.
127 Chain.erase(first, last);
128}
129
Douglas Gregord44252e2011-08-25 20:47:51 +0000130void ModuleManager::addInMemoryBuffer(StringRef FileName,
131 llvm::MemoryBuffer *Buffer) {
132
133 const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
134 Buffer->getBufferSize(), 0);
135 InMemoryBuffers[Entry] = Buffer;
136}
137
Argyrios Kyrtzidisaedf7142012-10-03 01:58:42 +0000138ModuleManager::ModuleManager(FileManager &FileMgr) : FileMgr(FileMgr) { }
Douglas Gregord44252e2011-08-25 20:47:51 +0000139
140ModuleManager::~ModuleManager() {
141 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
142 delete Chain[e - i - 1];
143}
144
Douglas Gregorde3ef502011-11-30 23:21:26 +0000145void ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
Douglas Gregord44252e2011-08-25 20:47:51 +0000146 void *UserData) {
147 unsigned N = size();
148
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.
Douglas Gregorde3ef502011-11-30 23:21:26 +0000152 SmallVector<ModuleFile *, 4> Queue;
Douglas Gregord44252e2011-08-25 20:47:51 +0000153 Queue.reserve(N);
Douglas Gregorde3ef502011-11-30 23:21:26 +0000154 llvm::DenseMap<ModuleFile *, unsigned> UnusedIncomingEdges;
Douglas Gregord44252e2011-08-25 20:47:51 +0000155 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
156 if (unsigned Size = (*M)->ImportedBy.size())
157 UnusedIncomingEdges[*M] = Size;
158 else
159 Queue.push_back(*M);
160 }
161
Douglas Gregorde3ef502011-11-30 23:21:26 +0000162 llvm::SmallPtrSet<ModuleFile *, 4> Skipped;
Douglas Gregord44252e2011-08-25 20:47:51 +0000163 unsigned QueueStart = 0;
164 while (QueueStart < Queue.size()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000165 ModuleFile *CurrentModule = Queue[QueueStart++];
Douglas Gregord44252e2011-08-25 20:47:51 +0000166
167 // Check whether this module should be skipped.
168 if (Skipped.count(CurrentModule))
169 continue;
170
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).
Douglas Gregorde3ef502011-11-30 23:21:26 +0000176 SmallVector<ModuleFile *, 4> Stack;
Douglas Gregord44252e2011-08-25 20:47:51 +0000177 Stack.push_back(CurrentModule);
178 Skipped.insert(CurrentModule);
179 while (!Stack.empty()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000180 ModuleFile *NextModule = Stack.back();
Douglas Gregord44252e2011-08-25 20:47:51 +0000181 Stack.pop_back();
182
183 // For any module that this module depends on, push it on the
184 // stack (if it hasn't already been marked as visited).
Douglas Gregorde3ef502011-11-30 23:21:26 +0000185 for (llvm::SetVector<ModuleFile *>::iterator
Douglas Gregord44252e2011-08-25 20:47:51 +0000186 M = NextModule->Imports.begin(),
187 MEnd = NextModule->Imports.end();
188 M != MEnd; ++M) {
189 if (Skipped.insert(*M))
190 Stack.push_back(*M);
191 }
192 }
193 continue;
194 }
195
196 // For any module that this module depends on, push it on the
197 // stack (if it hasn't already been marked as visited).
Douglas Gregorde3ef502011-11-30 23:21:26 +0000198 for (llvm::SetVector<ModuleFile *>::iterator M = CurrentModule->Imports.begin(),
Douglas Gregord44252e2011-08-25 20:47:51 +0000199 MEnd = CurrentModule->Imports.end();
200 M != MEnd; ++M) {
201
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))
208 Queue.push_back(*M);
209 }
210 }
211}
212
213/// \brief Perform a depth-first visit of the current module.
Douglas Gregorde3ef502011-11-30 23:21:26 +0000214static bool visitDepthFirst(ModuleFile &M,
215 bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregord44252e2011-08-25 20:47:51 +0000216 void *UserData),
217 void *UserData,
Douglas Gregorde3ef502011-11-30 23:21:26 +0000218 llvm::SmallPtrSet<ModuleFile *, 4> &Visited) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000219 // Preorder visitation
220 if (Visitor(M, /*Preorder=*/true, UserData))
221 return true;
222
223 // Visit children
Douglas Gregorde3ef502011-11-30 23:21:26 +0000224 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
Douglas Gregord44252e2011-08-25 20:47:51 +0000225 IMEnd = M.Imports.end();
226 IM != IMEnd; ++IM) {
227 if (!Visited.insert(*IM))
228 continue;
229
230 if (visitDepthFirst(**IM, Visitor, UserData, Visited))
231 return true;
232 }
233
234 // Postorder visitation
235 return Visitor(M, /*Preorder=*/false, UserData);
236}
237
Douglas Gregorde3ef502011-11-30 23:21:26 +0000238void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregord44252e2011-08-25 20:47:51 +0000239 void *UserData),
240 void *UserData) {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000241 llvm::SmallPtrSet<ModuleFile *, 4> Visited;
Douglas Gregord44252e2011-08-25 20:47:51 +0000242 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
243 if (!Visited.insert(Chain[I]))
244 continue;
245
246 if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
247 return;
248 }
249}
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000250
251#ifndef NDEBUG
252namespace llvm {
253 template<>
254 struct GraphTraits<ModuleManager> {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000255 typedef ModuleFile NodeType;
256 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000257 typedef ModuleManager::ModuleConstIterator nodes_iterator;
258
259 static ChildIteratorType child_begin(NodeType *Node) {
260 return Node->Imports.begin();
261 }
262
263 static ChildIteratorType child_end(NodeType *Node) {
264 return Node->Imports.end();
265 }
266
267 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
268 return Manager.begin();
269 }
270
271 static nodes_iterator nodes_end(const ModuleManager &Manager) {
272 return Manager.end();
273 }
274 };
275
276 template<>
277 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
278 explicit DOTGraphTraits(bool IsSimple = false)
279 : DefaultDOTGraphTraits(IsSimple) { }
280
281 static bool renderGraphFromBottomUp() {
282 return true;
283 }
284
Douglas Gregorde3ef502011-11-30 23:21:26 +0000285 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000286 return llvm::sys::path::stem(M->FileName);
287 }
288 };
289}
290
291void ModuleManager::viewGraph() {
292 llvm::ViewGraph(*this, "Modules");
293}
294#endif