1d44252ecSDouglas Gregor //===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===//
2d44252ecSDouglas Gregor //
3d44252ecSDouglas Gregor //                     The LLVM Compiler Infrastructure
4d44252ecSDouglas Gregor //
5d44252ecSDouglas Gregor // This file is distributed under the University of Illinois Open Source
6d44252ecSDouglas Gregor // License. See LICENSE.TXT for details.
7d44252ecSDouglas Gregor //
8d44252ecSDouglas Gregor //===----------------------------------------------------------------------===//
9d44252ecSDouglas Gregor //
10d44252ecSDouglas Gregor //  This file defines the ModuleManager class, which manages a set of loaded
11d44252ecSDouglas Gregor //  modules for the ASTReader.
12d44252ecSDouglas Gregor //
13d44252ecSDouglas Gregor //===----------------------------------------------------------------------===//
14d44252ecSDouglas Gregor #include "clang/Serialization/ModuleManager.h"
15*7211ac15SDouglas Gregor #include "clang/Serialization/GlobalModuleIndex.h"
16d44252ecSDouglas Gregor #include "llvm/Support/MemoryBuffer.h"
17d44252ecSDouglas Gregor #include "llvm/Support/raw_ostream.h"
18d44252ecSDouglas Gregor #include "llvm/Support/system_error.h"
19d44252ecSDouglas Gregor 
209d7c1a2aSDouglas Gregor #ifndef NDEBUG
219d7c1a2aSDouglas Gregor #include "llvm/Support/GraphWriter.h"
229d7c1a2aSDouglas Gregor #endif
239d7c1a2aSDouglas Gregor 
24d44252ecSDouglas Gregor using namespace clang;
25d44252ecSDouglas Gregor using namespace serialization;
26d44252ecSDouglas Gregor 
27de3ef502SDouglas Gregor ModuleFile *ModuleManager::lookup(StringRef Name) {
28d44252ecSDouglas Gregor   const FileEntry *Entry = FileMgr.getFile(Name);
29d44252ecSDouglas Gregor   return Modules[Entry];
30d44252ecSDouglas Gregor }
31d44252ecSDouglas Gregor 
32d44252ecSDouglas Gregor llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
33d44252ecSDouglas Gregor   const FileEntry *Entry = FileMgr.getFile(Name);
34d44252ecSDouglas Gregor   return InMemoryBuffers[Entry];
35d44252ecSDouglas Gregor }
36d44252ecSDouglas Gregor 
37de3ef502SDouglas Gregor std::pair<ModuleFile *, bool>
38d44252ecSDouglas Gregor ModuleManager::addModule(StringRef FileName, ModuleKind Type,
396fb03aeaSDouglas Gregor                          SourceLocation ImportLoc, ModuleFile *ImportedBy,
406fb03aeaSDouglas Gregor                          unsigned Generation, std::string &ErrorStr) {
41d44252ecSDouglas Gregor   const FileEntry *Entry = FileMgr.getFile(FileName);
42d44252ecSDouglas Gregor   if (!Entry && FileName != "-") {
43d44252ecSDouglas Gregor     ErrorStr = "file not found";
44de3ef502SDouglas Gregor     return std::make_pair(static_cast<ModuleFile*>(0), false);
45d44252ecSDouglas Gregor   }
46d44252ecSDouglas Gregor 
47d44252ecSDouglas Gregor   // Check whether we already loaded this module, before
48de3ef502SDouglas Gregor   ModuleFile *&ModuleEntry = Modules[Entry];
49d44252ecSDouglas Gregor   bool NewModule = false;
50d44252ecSDouglas Gregor   if (!ModuleEntry) {
51d44252ecSDouglas Gregor     // Allocate a new module.
524fc9f3e8SDouglas Gregor     ModuleFile *New = new ModuleFile(Type, Generation);
53bdb259d2SDouglas Gregor     New->Index = Chain.size();
54d44252ecSDouglas Gregor     New->FileName = FileName.str();
55aedf7144SArgyrios Kyrtzidis     New->File = Entry;
566fb03aeaSDouglas Gregor     New->ImportLoc = ImportLoc;
57d44252ecSDouglas Gregor     Chain.push_back(New);
58d44252ecSDouglas Gregor     NewModule = true;
59d44252ecSDouglas Gregor     ModuleEntry = New;
60d44252ecSDouglas Gregor 
61d44252ecSDouglas Gregor     // Load the contents of the module
62d44252ecSDouglas Gregor     if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) {
63d44252ecSDouglas Gregor       // The buffer was already provided for us.
64d44252ecSDouglas Gregor       assert(Buffer && "Passed null buffer");
65d44252ecSDouglas Gregor       New->Buffer.reset(Buffer);
66d44252ecSDouglas Gregor     } else {
67d44252ecSDouglas Gregor       // Open the AST file.
68d44252ecSDouglas Gregor       llvm::error_code ec;
69d44252ecSDouglas Gregor       if (FileName == "-") {
70d44252ecSDouglas Gregor         ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
71d44252ecSDouglas Gregor         if (ec)
72d44252ecSDouglas Gregor           ErrorStr = ec.message();
73d44252ecSDouglas Gregor       } else
74d44252ecSDouglas Gregor         New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr));
75d44252ecSDouglas Gregor 
76d44252ecSDouglas Gregor       if (!New->Buffer)
77de3ef502SDouglas Gregor         return std::make_pair(static_cast<ModuleFile*>(0), false);
78d44252ecSDouglas Gregor     }
79d44252ecSDouglas Gregor 
80d44252ecSDouglas Gregor     // Initialize the stream
81d44252ecSDouglas Gregor     New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
82d44252ecSDouglas Gregor                          (const unsigned char *)New->Buffer->getBufferEnd());     }
83d44252ecSDouglas Gregor 
84d44252ecSDouglas Gregor   if (ImportedBy) {
85d44252ecSDouglas Gregor     ModuleEntry->ImportedBy.insert(ImportedBy);
86d44252ecSDouglas Gregor     ImportedBy->Imports.insert(ModuleEntry);
87d44252ecSDouglas Gregor   } else {
886fb03aeaSDouglas Gregor     if (!ModuleEntry->DirectlyImported)
896fb03aeaSDouglas Gregor       ModuleEntry->ImportLoc = ImportLoc;
906fb03aeaSDouglas Gregor 
91d44252ecSDouglas Gregor     ModuleEntry->DirectlyImported = true;
92d44252ecSDouglas Gregor   }
93d44252ecSDouglas Gregor 
94d44252ecSDouglas Gregor   return std::make_pair(ModuleEntry, NewModule);
95d44252ecSDouglas Gregor }
96d44252ecSDouglas Gregor 
97188dbef2SDouglas Gregor namespace {
98188dbef2SDouglas Gregor   /// \brief Predicate that checks whether a module file occurs within
99188dbef2SDouglas Gregor   /// the given set.
100188dbef2SDouglas Gregor   class IsInModuleFileSet : public std::unary_function<ModuleFile *, bool> {
101188dbef2SDouglas Gregor     llvm::SmallPtrSet<ModuleFile *, 4> &Removed;
102188dbef2SDouglas Gregor 
103188dbef2SDouglas Gregor   public:
104188dbef2SDouglas Gregor     IsInModuleFileSet(llvm::SmallPtrSet<ModuleFile *, 4> &Removed)
105188dbef2SDouglas Gregor     : Removed(Removed) { }
106188dbef2SDouglas Gregor 
107188dbef2SDouglas Gregor     bool operator()(ModuleFile *MF) const {
108188dbef2SDouglas Gregor       return Removed.count(MF);
109188dbef2SDouglas Gregor     }
110188dbef2SDouglas Gregor   };
111188dbef2SDouglas Gregor }
112188dbef2SDouglas Gregor 
113188dbef2SDouglas Gregor void ModuleManager::removeModules(ModuleIterator first, ModuleIterator last) {
114188dbef2SDouglas Gregor   if (first == last)
115188dbef2SDouglas Gregor     return;
116188dbef2SDouglas Gregor 
117188dbef2SDouglas Gregor   // Collect the set of module file pointers that we'll be removing.
118188dbef2SDouglas Gregor   llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
119188dbef2SDouglas Gregor 
120188dbef2SDouglas Gregor   // Remove any references to the now-destroyed modules.
121188dbef2SDouglas Gregor   IsInModuleFileSet checkInSet(victimSet);
122188dbef2SDouglas Gregor   for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
123188dbef2SDouglas Gregor     Chain[i]->ImportedBy.remove_if(checkInSet);
124188dbef2SDouglas Gregor   }
125188dbef2SDouglas Gregor 
126188dbef2SDouglas Gregor   // Delete the modules and erase them from the various structures.
127188dbef2SDouglas Gregor   for (ModuleIterator victim = first; victim != last; ++victim) {
128188dbef2SDouglas Gregor     Modules.erase((*victim)->File);
129188dbef2SDouglas Gregor     delete *victim;
130188dbef2SDouglas Gregor   }
131188dbef2SDouglas Gregor 
132188dbef2SDouglas Gregor   // Remove the modules from the chain.
133188dbef2SDouglas Gregor   Chain.erase(first, last);
134188dbef2SDouglas Gregor }
135188dbef2SDouglas Gregor 
136d44252ecSDouglas Gregor void ModuleManager::addInMemoryBuffer(StringRef FileName,
137d44252ecSDouglas Gregor                                       llvm::MemoryBuffer *Buffer) {
138d44252ecSDouglas Gregor 
139d44252ecSDouglas Gregor   const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
140d44252ecSDouglas Gregor                                                   Buffer->getBufferSize(), 0);
141d44252ecSDouglas Gregor   InMemoryBuffers[Entry] = Buffer;
142d44252ecSDouglas Gregor }
143d44252ecSDouglas Gregor 
144*7211ac15SDouglas Gregor void ModuleManager::updateModulesInCommonWithGlobalIndex() {
145*7211ac15SDouglas Gregor   ModulesInCommonWithGlobalIndex.clear();
146*7211ac15SDouglas Gregor 
147*7211ac15SDouglas Gregor   if (!GlobalIndex)
148*7211ac15SDouglas Gregor     return;
149*7211ac15SDouglas Gregor 
150*7211ac15SDouglas Gregor   // Collect the set of modules known to the global index.
151*7211ac15SDouglas Gregor   SmallVector<const FileEntry *, 16> KnownModules;
152*7211ac15SDouglas Gregor   GlobalIndex->getKnownModules(KnownModules);
153*7211ac15SDouglas Gregor 
154*7211ac15SDouglas Gregor   // Map those modules to AST files known to the module manager.
155*7211ac15SDouglas Gregor   for (unsigned I = 0, N = KnownModules.size(); I != N; ++I) {
156*7211ac15SDouglas Gregor     llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known
157*7211ac15SDouglas Gregor       = Modules.find(KnownModules[I]);
158*7211ac15SDouglas Gregor     if (Known == Modules.end())
159*7211ac15SDouglas Gregor       continue;
160*7211ac15SDouglas Gregor 
161*7211ac15SDouglas Gregor     ModulesInCommonWithGlobalIndex.push_back(Known->second);
162*7211ac15SDouglas Gregor   }
163*7211ac15SDouglas Gregor }
164*7211ac15SDouglas Gregor 
165*7211ac15SDouglas Gregor void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
166*7211ac15SDouglas Gregor   GlobalIndex = Index;
167*7211ac15SDouglas Gregor   updateModulesInCommonWithGlobalIndex();
168*7211ac15SDouglas Gregor }
169*7211ac15SDouglas Gregor 
170*7211ac15SDouglas Gregor ModuleManager::ModuleManager(FileManager &FileMgr)
171*7211ac15SDouglas Gregor   : FileMgr(FileMgr), GlobalIndex() { }
172d44252ecSDouglas Gregor 
173d44252ecSDouglas Gregor ModuleManager::~ModuleManager() {
174d44252ecSDouglas Gregor   for (unsigned i = 0, e = Chain.size(); i != e; ++i)
175d44252ecSDouglas Gregor     delete Chain[e - i - 1];
176d44252ecSDouglas Gregor }
177d44252ecSDouglas Gregor 
178*7211ac15SDouglas Gregor void
179*7211ac15SDouglas Gregor ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
180*7211ac15SDouglas Gregor                      void *UserData,
181*7211ac15SDouglas Gregor                      llvm::SmallPtrSet<const FileEntry *, 4> *ModuleFilesHit) {
182*7211ac15SDouglas Gregor   // If the visitation order vector is the wrong size, recompute the order.
183e41d7feaSDouglas Gregor   if (VisitOrder.size() != Chain.size()) {
184d44252ecSDouglas Gregor     unsigned N = size();
185e41d7feaSDouglas Gregor     VisitOrder.clear();
186e41d7feaSDouglas Gregor     VisitOrder.reserve(N);
187d44252ecSDouglas Gregor 
188d44252ecSDouglas Gregor     // Record the number of incoming edges for each module. When we
189d44252ecSDouglas Gregor     // encounter a module with no incoming edges, push it into the queue
190d44252ecSDouglas Gregor     // to seed the queue.
191de3ef502SDouglas Gregor     SmallVector<ModuleFile *, 4> Queue;
192d44252ecSDouglas Gregor     Queue.reserve(N);
193bdb259d2SDouglas Gregor     llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
194bdb259d2SDouglas Gregor     UnusedIncomingEdges.reserve(size());
195d44252ecSDouglas Gregor     for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
196d44252ecSDouglas Gregor       if (unsigned Size = (*M)->ImportedBy.size())
197bdb259d2SDouglas Gregor         UnusedIncomingEdges.push_back(Size);
198bdb259d2SDouglas Gregor       else {
199bdb259d2SDouglas Gregor         UnusedIncomingEdges.push_back(0);
200d44252ecSDouglas Gregor         Queue.push_back(*M);
201d44252ecSDouglas Gregor       }
202bdb259d2SDouglas Gregor     }
203d44252ecSDouglas Gregor 
204e41d7feaSDouglas Gregor     // Traverse the graph, making sure to visit a module before visiting any
205e41d7feaSDouglas Gregor     // of its dependencies.
206d44252ecSDouglas Gregor     unsigned QueueStart = 0;
207d44252ecSDouglas Gregor     while (QueueStart < Queue.size()) {
208de3ef502SDouglas Gregor       ModuleFile *CurrentModule = Queue[QueueStart++];
209e41d7feaSDouglas Gregor       VisitOrder.push_back(CurrentModule);
210d44252ecSDouglas Gregor 
211d44252ecSDouglas Gregor       // For any module that this module depends on, push it on the
212d44252ecSDouglas Gregor       // stack (if it hasn't already been marked as visited).
213bdb259d2SDouglas Gregor       for (llvm::SetVector<ModuleFile *>::iterator
214bdb259d2SDouglas Gregor              M = CurrentModule->Imports.begin(),
215d44252ecSDouglas Gregor              MEnd = CurrentModule->Imports.end();
216d44252ecSDouglas Gregor            M != MEnd; ++M) {
217d44252ecSDouglas Gregor         // Remove our current module as an impediment to visiting the
218d44252ecSDouglas Gregor         // module we depend on. If we were the last unvisited module
219d44252ecSDouglas Gregor         // that depends on this particular module, push it into the
220d44252ecSDouglas Gregor         // queue to be visited.
221bdb259d2SDouglas Gregor         unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
222d44252ecSDouglas Gregor         if (NumUnusedEdges && (--NumUnusedEdges == 0))
223d44252ecSDouglas Gregor           Queue.push_back(*M);
224d44252ecSDouglas Gregor       }
225d44252ecSDouglas Gregor     }
226e41d7feaSDouglas Gregor 
227e41d7feaSDouglas Gregor     assert(VisitOrder.size() == N && "Visitation order is wrong?");
228*7211ac15SDouglas Gregor 
229*7211ac15SDouglas Gregor     // We may need to update the set of modules we have in common with the
230*7211ac15SDouglas Gregor     // global module index, since modules could have been added to the module
231*7211ac15SDouglas Gregor     // manager since we loaded the global module index.
232*7211ac15SDouglas Gregor     updateModulesInCommonWithGlobalIndex();
233e41d7feaSDouglas Gregor   }
234e41d7feaSDouglas Gregor 
235e41d7feaSDouglas Gregor   SmallVector<ModuleFile *, 4> Stack;
236e41d7feaSDouglas Gregor   SmallVector<bool, 4> Visited(size(), false);
237e41d7feaSDouglas Gregor 
238*7211ac15SDouglas Gregor   // If the caller has provided us with a hit-set that came from the global
239*7211ac15SDouglas Gregor   // module index, mark every module file in common with the global module
240*7211ac15SDouglas Gregor   // index that is *not* in that set as 'visited'.
241*7211ac15SDouglas Gregor   if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
242*7211ac15SDouglas Gregor     for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
243*7211ac15SDouglas Gregor     {
244*7211ac15SDouglas Gregor       ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
245*7211ac15SDouglas Gregor       if (!ModuleFilesHit->count(M->File))
246*7211ac15SDouglas Gregor         Visited[M->Index] = true;
247*7211ac15SDouglas Gregor     }
248*7211ac15SDouglas Gregor   }
249*7211ac15SDouglas Gregor 
250e41d7feaSDouglas Gregor   for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
251e41d7feaSDouglas Gregor     ModuleFile *CurrentModule = VisitOrder[I];
252e41d7feaSDouglas Gregor     // Should we skip this module file?
253e41d7feaSDouglas Gregor     if (Visited[CurrentModule->Index])
254e41d7feaSDouglas Gregor       continue;
255e41d7feaSDouglas Gregor 
256e41d7feaSDouglas Gregor     // Visit the module.
257e41d7feaSDouglas Gregor     Visited[CurrentModule->Index] = true;
258e41d7feaSDouglas Gregor     if (!Visitor(*CurrentModule, UserData))
259e41d7feaSDouglas Gregor       continue;
260e41d7feaSDouglas Gregor 
261e41d7feaSDouglas Gregor     // The visitor has requested that cut off visitation of any
262e41d7feaSDouglas Gregor     // module that the current module depends on. To indicate this
263e41d7feaSDouglas Gregor     // behavior, we mark all of the reachable modules as having been visited.
264e41d7feaSDouglas Gregor     ModuleFile *NextModule = CurrentModule;
265e41d7feaSDouglas Gregor     Stack.reserve(size());
266e41d7feaSDouglas Gregor     do {
267e41d7feaSDouglas Gregor       // For any module that this module depends on, push it on the
268e41d7feaSDouglas Gregor       // stack (if it hasn't already been marked as visited).
269e41d7feaSDouglas Gregor       for (llvm::SetVector<ModuleFile *>::iterator
270e41d7feaSDouglas Gregor              M = NextModule->Imports.begin(),
271e41d7feaSDouglas Gregor              MEnd = NextModule->Imports.end();
272e41d7feaSDouglas Gregor            M != MEnd; ++M) {
273e41d7feaSDouglas Gregor         if (!Visited[(*M)->Index]) {
274e41d7feaSDouglas Gregor           Stack.push_back(*M);
275e41d7feaSDouglas Gregor           Visited[(*M)->Index] = true;
276e41d7feaSDouglas Gregor         }
277e41d7feaSDouglas Gregor       }
278e41d7feaSDouglas Gregor 
279e41d7feaSDouglas Gregor       if (Stack.empty())
280e41d7feaSDouglas Gregor         break;
281e41d7feaSDouglas Gregor 
282e41d7feaSDouglas Gregor       // Pop the next module off the stack.
283e41d7feaSDouglas Gregor       NextModule = Stack.back();
284e41d7feaSDouglas Gregor       Stack.pop_back();
285e41d7feaSDouglas Gregor     } while (true);
286e41d7feaSDouglas Gregor   }
287d44252ecSDouglas Gregor }
288d44252ecSDouglas Gregor 
289d44252ecSDouglas Gregor /// \brief Perform a depth-first visit of the current module.
290de3ef502SDouglas Gregor static bool visitDepthFirst(ModuleFile &M,
291de3ef502SDouglas Gregor                             bool (*Visitor)(ModuleFile &M, bool Preorder,
292d44252ecSDouglas Gregor                                             void *UserData),
293d44252ecSDouglas Gregor                             void *UserData,
294bdb259d2SDouglas Gregor                             SmallVectorImpl<bool> &Visited) {
295d44252ecSDouglas Gregor   // Preorder visitation
296d44252ecSDouglas Gregor   if (Visitor(M, /*Preorder=*/true, UserData))
297d44252ecSDouglas Gregor     return true;
298d44252ecSDouglas Gregor 
299d44252ecSDouglas Gregor   // Visit children
300de3ef502SDouglas Gregor   for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
301d44252ecSDouglas Gregor                                             IMEnd = M.Imports.end();
302d44252ecSDouglas Gregor        IM != IMEnd; ++IM) {
303bdb259d2SDouglas Gregor     if (Visited[(*IM)->Index])
304d44252ecSDouglas Gregor       continue;
305bdb259d2SDouglas Gregor     Visited[(*IM)->Index] = true;
306d44252ecSDouglas Gregor 
307d44252ecSDouglas Gregor     if (visitDepthFirst(**IM, Visitor, UserData, Visited))
308d44252ecSDouglas Gregor       return true;
309d44252ecSDouglas Gregor   }
310d44252ecSDouglas Gregor 
311d44252ecSDouglas Gregor   // Postorder visitation
312d44252ecSDouglas Gregor   return Visitor(M, /*Preorder=*/false, UserData);
313d44252ecSDouglas Gregor }
314d44252ecSDouglas Gregor 
315de3ef502SDouglas Gregor void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
316d44252ecSDouglas Gregor                                                     void *UserData),
317d44252ecSDouglas Gregor                                     void *UserData) {
318bdb259d2SDouglas Gregor   SmallVector<bool, 16> Visited(size(), false);
319d44252ecSDouglas Gregor   for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
320bdb259d2SDouglas Gregor     if (Visited[Chain[I]->Index])
321d44252ecSDouglas Gregor       continue;
322bdb259d2SDouglas Gregor     Visited[Chain[I]->Index] = true;
323d44252ecSDouglas Gregor 
324d44252ecSDouglas Gregor     if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
325d44252ecSDouglas Gregor       return;
326d44252ecSDouglas Gregor   }
327d44252ecSDouglas Gregor }
3289d7c1a2aSDouglas Gregor 
3299d7c1a2aSDouglas Gregor #ifndef NDEBUG
3309d7c1a2aSDouglas Gregor namespace llvm {
3319d7c1a2aSDouglas Gregor   template<>
3329d7c1a2aSDouglas Gregor   struct GraphTraits<ModuleManager> {
333de3ef502SDouglas Gregor     typedef ModuleFile NodeType;
334de3ef502SDouglas Gregor     typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
3359d7c1a2aSDouglas Gregor     typedef ModuleManager::ModuleConstIterator nodes_iterator;
3369d7c1a2aSDouglas Gregor 
3379d7c1a2aSDouglas Gregor     static ChildIteratorType child_begin(NodeType *Node) {
3389d7c1a2aSDouglas Gregor       return Node->Imports.begin();
3399d7c1a2aSDouglas Gregor     }
3409d7c1a2aSDouglas Gregor 
3419d7c1a2aSDouglas Gregor     static ChildIteratorType child_end(NodeType *Node) {
3429d7c1a2aSDouglas Gregor       return Node->Imports.end();
3439d7c1a2aSDouglas Gregor     }
3449d7c1a2aSDouglas Gregor 
3459d7c1a2aSDouglas Gregor     static nodes_iterator nodes_begin(const ModuleManager &Manager) {
3469d7c1a2aSDouglas Gregor       return Manager.begin();
3479d7c1a2aSDouglas Gregor     }
3489d7c1a2aSDouglas Gregor 
3499d7c1a2aSDouglas Gregor     static nodes_iterator nodes_end(const ModuleManager &Manager) {
3509d7c1a2aSDouglas Gregor       return Manager.end();
3519d7c1a2aSDouglas Gregor     }
3529d7c1a2aSDouglas Gregor   };
3539d7c1a2aSDouglas Gregor 
3549d7c1a2aSDouglas Gregor   template<>
3559d7c1a2aSDouglas Gregor   struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
3569d7c1a2aSDouglas Gregor     explicit DOTGraphTraits(bool IsSimple = false)
3579d7c1a2aSDouglas Gregor       : DefaultDOTGraphTraits(IsSimple) { }
3589d7c1a2aSDouglas Gregor 
3599d7c1a2aSDouglas Gregor     static bool renderGraphFromBottomUp() {
3609d7c1a2aSDouglas Gregor       return true;
3619d7c1a2aSDouglas Gregor     }
3629d7c1a2aSDouglas Gregor 
363de3ef502SDouglas Gregor     std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
3649d7c1a2aSDouglas Gregor       return llvm::sys::path::stem(M->FileName);
3659d7c1a2aSDouglas Gregor     }
3669d7c1a2aSDouglas Gregor   };
3679d7c1a2aSDouglas Gregor }
3689d7c1a2aSDouglas Gregor 
3699d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() {
3709d7c1a2aSDouglas Gregor   llvm::ViewGraph(*this, "Modules");
3719d7c1a2aSDouglas Gregor }
3729d7c1a2aSDouglas Gregor #endif
373