blob: 5613d372d20c54c0f2db2bd77fe6875e6be7d796 [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//===----------------------------------------------------------------------===//
Stephen Hines6bcf27b2014-05-29 04:14:42 -070014#include "clang/Lex/HeaderSearch.h"
Douglas Gregor677e15f2013-03-19 00:28:20 +000015#include "clang/Lex/ModuleMap.h"
Douglas Gregor188bdcd2013-01-25 23:32:03 +000016#include "clang/Serialization/GlobalModuleIndex.h"
Benjamin Kramerae0cdff2013-08-24 13:16:22 +000017#include "clang/Serialization/ModuleManager.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000018#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola8229d222013-06-11 22:15:02 +000019#include "llvm/Support/Path.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000020#include "llvm/Support/raw_ostream.h"
21#include "llvm/Support/system_error.h"
22
Douglas Gregor2492c892011-10-11 19:27:55 +000023#ifndef NDEBUG
24#include "llvm/Support/GraphWriter.h"
25#endif
26
Douglas Gregor98339b92011-08-25 20:47:51 +000027using namespace clang;
28using namespace serialization;
29
Douglas Gregor1a4761e2011-11-30 23:21:26 +000030ModuleFile *ModuleManager::lookup(StringRef Name) {
Douglas Gregorea14a872013-02-08 21:27:45 +000031 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
32 /*cacheFailure=*/false);
Douglas Gregorc544ba02013-03-27 16:47:18 +000033 if (Entry)
34 return lookup(Entry);
35
Stephen Hines6bcf27b2014-05-29 04:14:42 -070036 return nullptr;
Douglas Gregorc544ba02013-03-27 16:47:18 +000037}
38
39ModuleFile *ModuleManager::lookup(const FileEntry *File) {
40 llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known
41 = Modules.find(File);
42 if (Known == Modules.end())
Stephen Hines6bcf27b2014-05-29 04:14:42 -070043 return nullptr;
Douglas Gregorc544ba02013-03-27 16:47:18 +000044
45 return Known->second;
Douglas Gregor98339b92011-08-25 20:47:51 +000046}
47
48llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
Douglas Gregorea14a872013-02-08 21:27:45 +000049 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
50 /*cacheFailure=*/false);
Douglas Gregor98339b92011-08-25 20:47:51 +000051 return InMemoryBuffers[Entry];
52}
53
Douglas Gregor677e15f2013-03-19 00:28:20 +000054ModuleManager::AddModuleResult
Douglas Gregor87e2cfc2012-11-30 19:28:05 +000055ModuleManager::addModule(StringRef FileName, ModuleKind Type,
56 SourceLocation ImportLoc, ModuleFile *ImportedBy,
Douglas Gregor677e15f2013-03-19 00:28:20 +000057 unsigned Generation,
58 off_t ExpectedSize, time_t ExpectedModTime,
59 ModuleFile *&Module,
60 std::string &ErrorStr) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070061 Module = nullptr;
Douglas Gregor677e15f2013-03-19 00:28:20 +000062
63 // Look for the file entry. This only fails if the expected size or
64 // modification time differ.
65 const FileEntry *Entry;
Eli Friedmanedadb9a2013-09-05 23:50:58 +000066 if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
67 ErrorStr = "module file out of date";
Douglas Gregor677e15f2013-03-19 00:28:20 +000068 return OutOfDate;
Eli Friedmanedadb9a2013-09-05 23:50:58 +000069 }
Douglas Gregor677e15f2013-03-19 00:28:20 +000070
Douglas Gregor98339b92011-08-25 20:47:51 +000071 if (!Entry && FileName != "-") {
Eli Friedmanedadb9a2013-09-05 23:50:58 +000072 ErrorStr = "module file not found";
Douglas Gregor677e15f2013-03-19 00:28:20 +000073 return Missing;
Douglas Gregor98339b92011-08-25 20:47:51 +000074 }
Douglas Gregor677e15f2013-03-19 00:28:20 +000075
76 // Check whether we already loaded this module, before
Douglas Gregor1a4761e2011-11-30 23:21:26 +000077 ModuleFile *&ModuleEntry = Modules[Entry];
Douglas Gregor98339b92011-08-25 20:47:51 +000078 bool NewModule = false;
79 if (!ModuleEntry) {
80 // Allocate a new module.
Douglas Gregor057df202012-01-18 20:56:22 +000081 ModuleFile *New = new ModuleFile(Type, Generation);
Douglas Gregorcc71dbe2013-01-21 20:07:12 +000082 New->Index = Chain.size();
Douglas Gregor98339b92011-08-25 20:47:51 +000083 New->FileName = FileName.str();
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +000084 New->File = Entry;
Douglas Gregor87e2cfc2012-11-30 19:28:05 +000085 New->ImportLoc = ImportLoc;
Douglas Gregor98339b92011-08-25 20:47:51 +000086 Chain.push_back(New);
87 NewModule = true;
88 ModuleEntry = New;
Douglas Gregor87e2cfc2012-11-30 19:28:05 +000089
Stephen Hines651f13c2014-04-23 16:59:28 -070090 New->InputFilesValidationTimestamp = 0;
91 if (New->Kind == MK_Module) {
92 std::string TimestampFilename = New->getTimestampFilename();
93 vfs::Status Status;
94 // A cached stat value would be fine as well.
95 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
96 New->InputFilesValidationTimestamp =
97 Status.getLastModificationTime().toEpochTime();
98 }
99
Douglas Gregor98339b92011-08-25 20:47:51 +0000100 // Load the contents of the module
101 if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) {
102 // The buffer was already provided for us.
103 assert(Buffer && "Passed null buffer");
104 New->Buffer.reset(Buffer);
105 } else {
106 // Open the AST file.
107 llvm::error_code ec;
108 if (FileName == "-") {
109 ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
110 if (ec)
111 ErrorStr = ec.message();
112 } else
113 New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr));
114
115 if (!New->Buffer)
Douglas Gregor677e15f2013-03-19 00:28:20 +0000116 return Missing;
Douglas Gregor98339b92011-08-25 20:47:51 +0000117 }
118
119 // Initialize the stream
120 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
Douglas Gregor677e15f2013-03-19 00:28:20 +0000121 (const unsigned char *)New->Buffer->getBufferEnd());
Douglas Gregor677e15f2013-03-19 00:28:20 +0000122 }
Douglas Gregor98339b92011-08-25 20:47:51 +0000123
124 if (ImportedBy) {
125 ModuleEntry->ImportedBy.insert(ImportedBy);
126 ImportedBy->Imports.insert(ModuleEntry);
127 } else {
Douglas Gregor87e2cfc2012-11-30 19:28:05 +0000128 if (!ModuleEntry->DirectlyImported)
129 ModuleEntry->ImportLoc = ImportLoc;
130
Douglas Gregor98339b92011-08-25 20:47:51 +0000131 ModuleEntry->DirectlyImported = true;
132 }
Douglas Gregor677e15f2013-03-19 00:28:20 +0000133
134 Module = ModuleEntry;
135 return NewModule? NewlyLoaded : AlreadyLoaded;
Douglas Gregor98339b92011-08-25 20:47:51 +0000136}
137
Douglas Gregor677e15f2013-03-19 00:28:20 +0000138void ModuleManager::removeModules(ModuleIterator first, ModuleIterator last,
139 ModuleMap *modMap) {
Douglas Gregor7cdd2812012-11-07 17:46:15 +0000140 if (first == last)
141 return;
142
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700143 // The first file entry is about to be rebuilt (or there was an error), so
144 // there should be no references to it. Remove it from the cache to close it,
145 // as Windows doesn't seem to allow renaming over an open file.
146 FileMgr.invalidateCache((*first)->File);
147
Douglas Gregor7cdd2812012-11-07 17:46:15 +0000148 // Collect the set of module file pointers that we'll be removing.
149 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
150
151 // Remove any references to the now-destroyed modules.
Douglas Gregor7cdd2812012-11-07 17:46:15 +0000152 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700153 Chain[i]->ImportedBy.remove_if([&](ModuleFile *MF) {
154 return victimSet.count(MF);
155 });
Douglas Gregor7cdd2812012-11-07 17:46:15 +0000156 }
157
158 // Delete the modules and erase them from the various structures.
159 for (ModuleIterator victim = first; victim != last; ++victim) {
160 Modules.erase((*victim)->File);
Douglas Gregor677e15f2013-03-19 00:28:20 +0000161
Douglas Gregor677e15f2013-03-19 00:28:20 +0000162 if (modMap) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700163 StringRef ModuleName = (*victim)->ModuleName;
Douglas Gregor677e15f2013-03-19 00:28:20 +0000164 if (Module *mod = modMap->findModule(ModuleName)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700165 mod->setASTFile(nullptr);
Douglas Gregor677e15f2013-03-19 00:28:20 +0000166 }
167 }
Douglas Gregor7cdd2812012-11-07 17:46:15 +0000168 delete *victim;
169 }
170
171 // Remove the modules from the chain.
172 Chain.erase(first, last);
173}
174
Douglas Gregor98339b92011-08-25 20:47:51 +0000175void ModuleManager::addInMemoryBuffer(StringRef FileName,
176 llvm::MemoryBuffer *Buffer) {
177
178 const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
179 Buffer->getBufferSize(), 0);
180 InMemoryBuffers[Entry] = Buffer;
181}
182
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000183ModuleManager::VisitState *ModuleManager::allocateVisitState() {
184 // Fast path: if we have a cached state, use it.
185 if (FirstVisitState) {
186 VisitState *Result = FirstVisitState;
187 FirstVisitState = FirstVisitState->NextState;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700188 Result->NextState = nullptr;
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000189 return Result;
190 }
191
192 // Allocate and return a new state.
193 return new VisitState(size());
194}
195
196void ModuleManager::returnVisitState(VisitState *State) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700197 assert(State->NextState == nullptr && "Visited state is in list?");
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000198 State->NextState = FirstVisitState;
199 FirstVisitState = State;
200}
201
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000202void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
203 GlobalIndex = Index;
Douglas Gregorfa69fc12013-03-22 18:50:14 +0000204 if (!GlobalIndex) {
205 ModulesInCommonWithGlobalIndex.clear();
206 return;
Douglas Gregor677e15f2013-03-19 00:28:20 +0000207 }
Douglas Gregorfa69fc12013-03-22 18:50:14 +0000208
209 // Notify the global module index about all of the modules we've already
210 // loaded.
211 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
212 if (!GlobalIndex->loadedModuleFile(Chain[I])) {
213 ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
214 }
215 }
216}
217
218void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
219 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
220 return;
221
222 ModulesInCommonWithGlobalIndex.push_back(MF);
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000223}
224
225ModuleManager::ModuleManager(FileManager &FileMgr)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700226 : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(nullptr) {}
Douglas Gregor98339b92011-08-25 20:47:51 +0000227
228ModuleManager::~ModuleManager() {
229 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
230 delete Chain[e - i - 1];
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000231 delete FirstVisitState;
Douglas Gregor98339b92011-08-25 20:47:51 +0000232}
233
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000234void
235ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
236 void *UserData,
Douglas Gregor677e15f2013-03-19 00:28:20 +0000237 llvm::SmallPtrSet<ModuleFile *, 4> *ModuleFilesHit) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000238 // If the visitation order vector is the wrong size, recompute the order.
Douglas Gregord07865b2013-01-25 22:25:23 +0000239 if (VisitOrder.size() != Chain.size()) {
240 unsigned N = size();
241 VisitOrder.clear();
242 VisitOrder.reserve(N);
243
244 // Record the number of incoming edges for each module. When we
245 // encounter a module with no incoming edges, push it into the queue
246 // to seed the queue.
247 SmallVector<ModuleFile *, 4> Queue;
248 Queue.reserve(N);
249 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
250 UnusedIncomingEdges.reserve(size());
251 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
252 if (unsigned Size = (*M)->ImportedBy.size())
253 UnusedIncomingEdges.push_back(Size);
254 else {
255 UnusedIncomingEdges.push_back(0);
256 Queue.push_back(*M);
257 }
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000258 }
Douglas Gregord07865b2013-01-25 22:25:23 +0000259
260 // Traverse the graph, making sure to visit a module before visiting any
261 // of its dependencies.
262 unsigned QueueStart = 0;
263 while (QueueStart < Queue.size()) {
264 ModuleFile *CurrentModule = Queue[QueueStart++];
265 VisitOrder.push_back(CurrentModule);
266
267 // For any module that this module depends on, push it on the
268 // stack (if it hasn't already been marked as visited).
269 for (llvm::SetVector<ModuleFile *>::iterator
270 M = CurrentModule->Imports.begin(),
271 MEnd = CurrentModule->Imports.end();
272 M != MEnd; ++M) {
273 // Remove our current module as an impediment to visiting the
274 // module we depend on. If we were the last unvisited module
275 // that depends on this particular module, push it into the
276 // queue to be visited.
277 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
278 if (NumUnusedEdges && (--NumUnusedEdges == 0))
279 Queue.push_back(*M);
280 }
281 }
282
283 assert(VisitOrder.size() == N && "Visitation order is wrong?");
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000284
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000285 delete FirstVisitState;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700286 FirstVisitState = nullptr;
Douglas Gregor98339b92011-08-25 20:47:51 +0000287 }
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000288
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000289 VisitState *State = allocateVisitState();
290 unsigned VisitNumber = State->NextVisitNumber++;
Douglas Gregord07865b2013-01-25 22:25:23 +0000291
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000292 // If the caller has provided us with a hit-set that came from the global
293 // module index, mark every module file in common with the global module
294 // index that is *not* in that set as 'visited'.
295 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
296 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
297 {
298 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
Douglas Gregor677e15f2013-03-19 00:28:20 +0000299 if (!ModuleFilesHit->count(M))
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000300 State->VisitNumber[M->Index] = VisitNumber;
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000301 }
302 }
303
Douglas Gregord07865b2013-01-25 22:25:23 +0000304 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
305 ModuleFile *CurrentModule = VisitOrder[I];
306 // Should we skip this module file?
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000307 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
Douglas Gregor98339b92011-08-25 20:47:51 +0000308 continue;
Douglas Gregord07865b2013-01-25 22:25:23 +0000309
310 // Visit the module.
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000311 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
312 State->VisitNumber[CurrentModule->Index] = VisitNumber;
Douglas Gregord07865b2013-01-25 22:25:23 +0000313 if (!Visitor(*CurrentModule, UserData))
314 continue;
315
316 // The visitor has requested that cut off visitation of any
317 // module that the current module depends on. To indicate this
318 // behavior, we mark all of the reachable modules as having been visited.
319 ModuleFile *NextModule = CurrentModule;
Douglas Gregord07865b2013-01-25 22:25:23 +0000320 do {
321 // For any module that this module depends on, push it on the
322 // stack (if it hasn't already been marked as visited).
323 for (llvm::SetVector<ModuleFile *>::iterator
324 M = NextModule->Imports.begin(),
325 MEnd = NextModule->Imports.end();
326 M != MEnd; ++M) {
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000327 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
328 State->Stack.push_back(*M);
329 State->VisitNumber[(*M)->Index] = VisitNumber;
Douglas Gregor98339b92011-08-25 20:47:51 +0000330 }
331 }
Douglas Gregord07865b2013-01-25 22:25:23 +0000332
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000333 if (State->Stack.empty())
Douglas Gregord07865b2013-01-25 22:25:23 +0000334 break;
335
336 // Pop the next module off the stack.
Robert Wilhelm344472e2013-08-23 16:11:15 +0000337 NextModule = State->Stack.pop_back_val();
Douglas Gregord07865b2013-01-25 22:25:23 +0000338 } while (true);
Douglas Gregor98339b92011-08-25 20:47:51 +0000339 }
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000340
341 returnVisitState(State);
Douglas Gregor98339b92011-08-25 20:47:51 +0000342}
343
344/// \brief Perform a depth-first visit of the current module.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000345static bool visitDepthFirst(ModuleFile &M,
346 bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregor98339b92011-08-25 20:47:51 +0000347 void *UserData),
348 void *UserData,
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000349 SmallVectorImpl<bool> &Visited) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000350 // Preorder visitation
351 if (Visitor(M, /*Preorder=*/true, UserData))
352 return true;
353
354 // Visit children
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000355 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000356 IMEnd = M.Imports.end();
Douglas Gregor98339b92011-08-25 20:47:51 +0000357 IM != IMEnd; ++IM) {
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000358 if (Visited[(*IM)->Index])
Douglas Gregor98339b92011-08-25 20:47:51 +0000359 continue;
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000360 Visited[(*IM)->Index] = true;
361
Douglas Gregor98339b92011-08-25 20:47:51 +0000362 if (visitDepthFirst(**IM, Visitor, UserData, Visited))
363 return true;
364 }
365
366 // Postorder visitation
367 return Visitor(M, /*Preorder=*/false, UserData);
368}
369
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000370void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregor98339b92011-08-25 20:47:51 +0000371 void *UserData),
372 void *UserData) {
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000373 SmallVector<bool, 16> Visited(size(), false);
Douglas Gregor98339b92011-08-25 20:47:51 +0000374 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000375 if (Visited[Chain[I]->Index])
Douglas Gregor98339b92011-08-25 20:47:51 +0000376 continue;
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000377 Visited[Chain[I]->Index] = true;
378
Douglas Gregor98339b92011-08-25 20:47:51 +0000379 if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
380 return;
381 }
382}
Douglas Gregor2492c892011-10-11 19:27:55 +0000383
Douglas Gregor677e15f2013-03-19 00:28:20 +0000384bool ModuleManager::lookupModuleFile(StringRef FileName,
385 off_t ExpectedSize,
386 time_t ExpectedModTime,
387 const FileEntry *&File) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700388 // Open the file immediately to ensure there is no race between stat'ing and
389 // opening the file.
390 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
Douglas Gregor677e15f2013-03-19 00:28:20 +0000391
392 if (!File && FileName != "-") {
393 return false;
394 }
395
396 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700397 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
398 // Do not destroy File, as it may be referenced. If we need to rebuild it,
399 // it will be destroyed by removeModules.
Douglas Gregor677e15f2013-03-19 00:28:20 +0000400 return true;
Douglas Gregor677e15f2013-03-19 00:28:20 +0000401
402 return false;
403}
404
Douglas Gregor2492c892011-10-11 19:27:55 +0000405#ifndef NDEBUG
406namespace llvm {
407 template<>
408 struct GraphTraits<ModuleManager> {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000409 typedef ModuleFile NodeType;
410 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Douglas Gregor2492c892011-10-11 19:27:55 +0000411 typedef ModuleManager::ModuleConstIterator nodes_iterator;
412
413 static ChildIteratorType child_begin(NodeType *Node) {
414 return Node->Imports.begin();
415 }
416
417 static ChildIteratorType child_end(NodeType *Node) {
418 return Node->Imports.end();
419 }
420
421 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
422 return Manager.begin();
423 }
424
425 static nodes_iterator nodes_end(const ModuleManager &Manager) {
426 return Manager.end();
427 }
428 };
429
430 template<>
431 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
432 explicit DOTGraphTraits(bool IsSimple = false)
433 : DefaultDOTGraphTraits(IsSimple) { }
434
435 static bool renderGraphFromBottomUp() {
436 return true;
437 }
438
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000439 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700440 return M->ModuleName;
Douglas Gregor2492c892011-10-11 19:27:55 +0000441 }
442 };
443}
444
445void ModuleManager::viewGraph() {
446 llvm::ViewGraph(*this, "Modules");
447}
448#endif