blob: 7cc1544259f9705f53e7fd0a72dcd04553e1c1a4 [file] [log] [blame]
Douglas Gregor98339b92011-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 Gregor2492c892011-10-11 19:27:55 +000019#ifndef NDEBUG
20#include "llvm/Support/GraphWriter.h"
21#endif
22
Douglas Gregor98339b92011-08-25 20:47:51 +000023using namespace clang;
24using namespace serialization;
25
Douglas Gregor1a4761e2011-11-30 23:21:26 +000026ModuleFile *ModuleManager::lookup(StringRef Name) {
Douglas Gregor98339b92011-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 Gregor1a4761e2011-11-30 23:21:26 +000036std::pair<ModuleFile *, bool>
Douglas Gregor87e2cfc2012-11-30 19:28:05 +000037ModuleManager::addModule(StringRef FileName, ModuleKind Type,
38 SourceLocation ImportLoc, ModuleFile *ImportedBy,
39 unsigned Generation, std::string &ErrorStr) {
Douglas Gregor98339b92011-08-25 20:47:51 +000040 const FileEntry *Entry = FileMgr.getFile(FileName);
41 if (!Entry && FileName != "-") {
42 ErrorStr = "file not found";
Douglas Gregor1a4761e2011-11-30 23:21:26 +000043 return std::make_pair(static_cast<ModuleFile*>(0), false);
Douglas Gregor98339b92011-08-25 20:47:51 +000044 }
45
46 // Check whether we already loaded this module, before
Douglas Gregor1a4761e2011-11-30 23:21:26 +000047 ModuleFile *&ModuleEntry = Modules[Entry];
Douglas Gregor98339b92011-08-25 20:47:51 +000048 bool NewModule = false;
49 if (!ModuleEntry) {
50 // Allocate a new module.
Douglas Gregor057df202012-01-18 20:56:22 +000051 ModuleFile *New = new ModuleFile(Type, Generation);
Douglas Gregorcc71dbe2013-01-21 20:07:12 +000052 New->Index = Chain.size();
Douglas Gregor98339b92011-08-25 20:47:51 +000053 New->FileName = FileName.str();
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +000054 New->File = Entry;
Douglas Gregor87e2cfc2012-11-30 19:28:05 +000055 New->ImportLoc = ImportLoc;
Douglas Gregor98339b92011-08-25 20:47:51 +000056 Chain.push_back(New);
57 NewModule = true;
58 ModuleEntry = New;
Douglas Gregor87e2cfc2012-11-30 19:28:05 +000059
Douglas Gregor98339b92011-08-25 20:47:51 +000060 // Load the contents of the module
61 if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) {
62 // The buffer was already provided for us.
63 assert(Buffer && "Passed null buffer");
64 New->Buffer.reset(Buffer);
65 } else {
66 // Open the AST file.
67 llvm::error_code ec;
68 if (FileName == "-") {
69 ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
70 if (ec)
71 ErrorStr = ec.message();
72 } else
73 New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr));
74
75 if (!New->Buffer)
Douglas Gregor1a4761e2011-11-30 23:21:26 +000076 return std::make_pair(static_cast<ModuleFile*>(0), false);
Douglas Gregor98339b92011-08-25 20:47:51 +000077 }
78
79 // Initialize the stream
80 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
81 (const unsigned char *)New->Buffer->getBufferEnd()); }
82
83 if (ImportedBy) {
84 ModuleEntry->ImportedBy.insert(ImportedBy);
85 ImportedBy->Imports.insert(ModuleEntry);
86 } else {
Douglas Gregor87e2cfc2012-11-30 19:28:05 +000087 if (!ModuleEntry->DirectlyImported)
88 ModuleEntry->ImportLoc = ImportLoc;
89
Douglas Gregor98339b92011-08-25 20:47:51 +000090 ModuleEntry->DirectlyImported = true;
91 }
92
93 return std::make_pair(ModuleEntry, NewModule);
94}
95
Douglas Gregor7cdd2812012-11-07 17:46:15 +000096namespace {
97 /// \brief Predicate that checks whether a module file occurs within
98 /// the given set.
99 class IsInModuleFileSet : public std::unary_function<ModuleFile *, bool> {
100 llvm::SmallPtrSet<ModuleFile *, 4> &Removed;
101
102 public:
103 IsInModuleFileSet(llvm::SmallPtrSet<ModuleFile *, 4> &Removed)
104 : Removed(Removed) { }
105
106 bool operator()(ModuleFile *MF) const {
107 return Removed.count(MF);
108 }
109 };
110}
111
112void ModuleManager::removeModules(ModuleIterator first, ModuleIterator last) {
113 if (first == last)
114 return;
115
116 // Collect the set of module file pointers that we'll be removing.
117 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
118
119 // Remove any references to the now-destroyed modules.
120 IsInModuleFileSet checkInSet(victimSet);
121 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
122 Chain[i]->ImportedBy.remove_if(checkInSet);
123 }
124
125 // Delete the modules and erase them from the various structures.
126 for (ModuleIterator victim = first; victim != last; ++victim) {
127 Modules.erase((*victim)->File);
128 delete *victim;
129 }
130
131 // Remove the modules from the chain.
132 Chain.erase(first, last);
133}
134
Douglas Gregor98339b92011-08-25 20:47:51 +0000135void ModuleManager::addInMemoryBuffer(StringRef FileName,
136 llvm::MemoryBuffer *Buffer) {
137
138 const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
139 Buffer->getBufferSize(), 0);
140 InMemoryBuffers[Entry] = Buffer;
141}
142
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +0000143ModuleManager::ModuleManager(FileManager &FileMgr) : FileMgr(FileMgr) { }
Douglas Gregor98339b92011-08-25 20:47:51 +0000144
145ModuleManager::~ModuleManager() {
146 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
147 delete Chain[e - i - 1];
148}
149
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000150void ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
Douglas Gregor98339b92011-08-25 20:47:51 +0000151 void *UserData) {
152 unsigned N = size();
153
154 // Record the number of incoming edges for each module. When we
155 // encounter a module with no incoming edges, push it into the queue
156 // to seed the queue.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000157 SmallVector<ModuleFile *, 4> Queue;
Douglas Gregor98339b92011-08-25 20:47:51 +0000158 Queue.reserve(N);
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000159 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
160 UnusedIncomingEdges.reserve(size());
Douglas Gregor98339b92011-08-25 20:47:51 +0000161 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
162 if (unsigned Size = (*M)->ImportedBy.size())
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000163 UnusedIncomingEdges.push_back(Size);
164 else {
165 UnusedIncomingEdges.push_back(0);
Douglas Gregor98339b92011-08-25 20:47:51 +0000166 Queue.push_back(*M);
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000167 }
Douglas Gregor98339b92011-08-25 20:47:51 +0000168 }
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000169
170 llvm::SmallVector<bool, 4> Skipped(size(), false);
Douglas Gregor98339b92011-08-25 20:47:51 +0000171 unsigned QueueStart = 0;
172 while (QueueStart < Queue.size()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000173 ModuleFile *CurrentModule = Queue[QueueStart++];
Douglas Gregor98339b92011-08-25 20:47:51 +0000174
175 // Check whether this module should be skipped.
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000176 if (Skipped[CurrentModule->Index])
Douglas Gregor98339b92011-08-25 20:47:51 +0000177 continue;
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000178 Skipped[CurrentModule->Index] = true;
Douglas Gregor98339b92011-08-25 20:47:51 +0000179
180 if (Visitor(*CurrentModule, UserData)) {
181 // The visitor has requested that cut off visitation of any
182 // module that the current module depends on. To indicate this
183 // behavior, we mark all of the reachable modules as having N
184 // incoming edges (which is impossible otherwise).
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000185 SmallVector<ModuleFile *, 4> Stack;
Douglas Gregor98339b92011-08-25 20:47:51 +0000186 Stack.push_back(CurrentModule);
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000187 Skipped[CurrentModule->Index] = true;
Douglas Gregor98339b92011-08-25 20:47:51 +0000188 while (!Stack.empty()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000189 ModuleFile *NextModule = Stack.back();
Douglas Gregor98339b92011-08-25 20:47:51 +0000190 Stack.pop_back();
191
192 // For any module that this module depends on, push it on the
193 // stack (if it hasn't already been marked as visited).
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000194 for (llvm::SetVector<ModuleFile *>::iterator
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000195 M = NextModule->Imports.begin(),
196 MEnd = NextModule->Imports.end();
Douglas Gregor98339b92011-08-25 20:47:51 +0000197 M != MEnd; ++M) {
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000198 if (!Skipped[(*M)->Index]) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000199 Stack.push_back(*M);
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000200 Skipped[(*M)->Index] = true;
201 }
Douglas Gregor98339b92011-08-25 20:47:51 +0000202 }
203 }
204 continue;
205 }
206
207 // For any module that this module depends on, push it on the
208 // stack (if it hasn't already been marked as visited).
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000209 for (llvm::SetVector<ModuleFile *>::iterator
210 M = CurrentModule->Imports.begin(),
211 MEnd = CurrentModule->Imports.end();
Douglas Gregor98339b92011-08-25 20:47:51 +0000212 M != MEnd; ++M) {
213
214 // Remove our current module as an impediment to visiting the
215 // module we depend on. If we were the last unvisited module
216 // that depends on this particular module, push it into the
217 // queue to be visited.
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000218 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
Douglas Gregor98339b92011-08-25 20:47:51 +0000219 if (NumUnusedEdges && (--NumUnusedEdges == 0))
220 Queue.push_back(*M);
221 }
222 }
223}
224
225/// \brief Perform a depth-first visit of the current module.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000226static bool visitDepthFirst(ModuleFile &M,
227 bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregor98339b92011-08-25 20:47:51 +0000228 void *UserData),
229 void *UserData,
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000230 SmallVectorImpl<bool> &Visited) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000231 // Preorder visitation
232 if (Visitor(M, /*Preorder=*/true, UserData))
233 return true;
234
235 // Visit children
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000236 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000237 IMEnd = M.Imports.end();
Douglas Gregor98339b92011-08-25 20:47:51 +0000238 IM != IMEnd; ++IM) {
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000239 if (Visited[(*IM)->Index])
Douglas Gregor98339b92011-08-25 20:47:51 +0000240 continue;
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000241 Visited[(*IM)->Index] = true;
242
Douglas Gregor98339b92011-08-25 20:47:51 +0000243 if (visitDepthFirst(**IM, Visitor, UserData, Visited))
244 return true;
245 }
246
247 // Postorder visitation
248 return Visitor(M, /*Preorder=*/false, UserData);
249}
250
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000251void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregor98339b92011-08-25 20:47:51 +0000252 void *UserData),
253 void *UserData) {
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000254 SmallVector<bool, 16> Visited(size(), false);
Douglas Gregor98339b92011-08-25 20:47:51 +0000255 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000256 if (Visited[Chain[I]->Index])
Douglas Gregor98339b92011-08-25 20:47:51 +0000257 continue;
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000258 Visited[Chain[I]->Index] = true;
259
Douglas Gregor98339b92011-08-25 20:47:51 +0000260 if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
261 return;
262 }
263}
Douglas Gregor2492c892011-10-11 19:27:55 +0000264
265#ifndef NDEBUG
266namespace llvm {
267 template<>
268 struct GraphTraits<ModuleManager> {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000269 typedef ModuleFile NodeType;
270 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Douglas Gregor2492c892011-10-11 19:27:55 +0000271 typedef ModuleManager::ModuleConstIterator nodes_iterator;
272
273 static ChildIteratorType child_begin(NodeType *Node) {
274 return Node->Imports.begin();
275 }
276
277 static ChildIteratorType child_end(NodeType *Node) {
278 return Node->Imports.end();
279 }
280
281 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
282 return Manager.begin();
283 }
284
285 static nodes_iterator nodes_end(const ModuleManager &Manager) {
286 return Manager.end();
287 }
288 };
289
290 template<>
291 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
292 explicit DOTGraphTraits(bool IsSimple = false)
293 : DefaultDOTGraphTraits(IsSimple) { }
294
295 static bool renderGraphFromBottomUp() {
296 return true;
297 }
298
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000299 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Douglas Gregor2492c892011-10-11 19:27:55 +0000300 return llvm::sys::path::stem(M->FileName);
301 }
302 };
303}
304
305void ModuleManager::viewGraph() {
306 llvm::ViewGraph(*this, "Modules");
307}
308#endif