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"
157211ac15SDouglas 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 
1447211ac15SDouglas Gregor void ModuleManager::updateModulesInCommonWithGlobalIndex() {
1457211ac15SDouglas Gregor   ModulesInCommonWithGlobalIndex.clear();
1467211ac15SDouglas Gregor 
1477211ac15SDouglas Gregor   if (!GlobalIndex)
1487211ac15SDouglas Gregor     return;
1497211ac15SDouglas Gregor 
1507211ac15SDouglas Gregor   // Collect the set of modules known to the global index.
1517211ac15SDouglas Gregor   SmallVector<const FileEntry *, 16> KnownModules;
1527211ac15SDouglas Gregor   GlobalIndex->getKnownModules(KnownModules);
1537211ac15SDouglas Gregor 
1547211ac15SDouglas Gregor   // Map those modules to AST files known to the module manager.
1557211ac15SDouglas Gregor   for (unsigned I = 0, N = KnownModules.size(); I != N; ++I) {
1567211ac15SDouglas Gregor     llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known
1577211ac15SDouglas Gregor       = Modules.find(KnownModules[I]);
1587211ac15SDouglas Gregor     if (Known == Modules.end())
1597211ac15SDouglas Gregor       continue;
1607211ac15SDouglas Gregor 
1617211ac15SDouglas Gregor     ModulesInCommonWithGlobalIndex.push_back(Known->second);
1627211ac15SDouglas Gregor   }
1637211ac15SDouglas Gregor }
1647211ac15SDouglas Gregor 
165*e97cd90aSDouglas Gregor ModuleManager::VisitState *ModuleManager::allocateVisitState() {
166*e97cd90aSDouglas Gregor   // Fast path: if we have a cached state, use it.
167*e97cd90aSDouglas Gregor   if (FirstVisitState) {
168*e97cd90aSDouglas Gregor     VisitState *Result = FirstVisitState;
169*e97cd90aSDouglas Gregor     FirstVisitState = FirstVisitState->NextState;
170*e97cd90aSDouglas Gregor     Result->NextState = 0;
171*e97cd90aSDouglas Gregor     return Result;
172*e97cd90aSDouglas Gregor   }
173*e97cd90aSDouglas Gregor 
174*e97cd90aSDouglas Gregor   // Allocate and return a new state.
175*e97cd90aSDouglas Gregor   return new VisitState(size());
176*e97cd90aSDouglas Gregor }
177*e97cd90aSDouglas Gregor 
178*e97cd90aSDouglas Gregor void ModuleManager::returnVisitState(VisitState *State) {
179*e97cd90aSDouglas Gregor   assert(State->NextState == 0 && "Visited state is in list?");
180*e97cd90aSDouglas Gregor   State->NextState = FirstVisitState;
181*e97cd90aSDouglas Gregor   FirstVisitState = State;
182*e97cd90aSDouglas Gregor }
183*e97cd90aSDouglas Gregor 
1847211ac15SDouglas Gregor void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
1857211ac15SDouglas Gregor   GlobalIndex = Index;
1867211ac15SDouglas Gregor   updateModulesInCommonWithGlobalIndex();
1877211ac15SDouglas Gregor }
1887211ac15SDouglas Gregor 
1897211ac15SDouglas Gregor ModuleManager::ModuleManager(FileManager &FileMgr)
190*e97cd90aSDouglas Gregor   : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(0) { }
191d44252ecSDouglas Gregor 
192d44252ecSDouglas Gregor ModuleManager::~ModuleManager() {
193d44252ecSDouglas Gregor   for (unsigned i = 0, e = Chain.size(); i != e; ++i)
194d44252ecSDouglas Gregor     delete Chain[e - i - 1];
195*e97cd90aSDouglas Gregor   delete FirstVisitState;
196d44252ecSDouglas Gregor }
197d44252ecSDouglas Gregor 
1987211ac15SDouglas Gregor void
1997211ac15SDouglas Gregor ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
2007211ac15SDouglas Gregor                      void *UserData,
2017211ac15SDouglas Gregor                      llvm::SmallPtrSet<const FileEntry *, 4> *ModuleFilesHit) {
2027211ac15SDouglas Gregor   // If the visitation order vector is the wrong size, recompute the order.
203e41d7feaSDouglas Gregor   if (VisitOrder.size() != Chain.size()) {
204d44252ecSDouglas Gregor     unsigned N = size();
205e41d7feaSDouglas Gregor     VisitOrder.clear();
206e41d7feaSDouglas Gregor     VisitOrder.reserve(N);
207d44252ecSDouglas Gregor 
208d44252ecSDouglas Gregor     // Record the number of incoming edges for each module. When we
209d44252ecSDouglas Gregor     // encounter a module with no incoming edges, push it into the queue
210d44252ecSDouglas Gregor     // to seed the queue.
211de3ef502SDouglas Gregor     SmallVector<ModuleFile *, 4> Queue;
212d44252ecSDouglas Gregor     Queue.reserve(N);
213bdb259d2SDouglas Gregor     llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
214bdb259d2SDouglas Gregor     UnusedIncomingEdges.reserve(size());
215d44252ecSDouglas Gregor     for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
216d44252ecSDouglas Gregor       if (unsigned Size = (*M)->ImportedBy.size())
217bdb259d2SDouglas Gregor         UnusedIncomingEdges.push_back(Size);
218bdb259d2SDouglas Gregor       else {
219bdb259d2SDouglas Gregor         UnusedIncomingEdges.push_back(0);
220d44252ecSDouglas Gregor         Queue.push_back(*M);
221d44252ecSDouglas Gregor       }
222bdb259d2SDouglas Gregor     }
223d44252ecSDouglas Gregor 
224e41d7feaSDouglas Gregor     // Traverse the graph, making sure to visit a module before visiting any
225e41d7feaSDouglas Gregor     // of its dependencies.
226d44252ecSDouglas Gregor     unsigned QueueStart = 0;
227d44252ecSDouglas Gregor     while (QueueStart < Queue.size()) {
228de3ef502SDouglas Gregor       ModuleFile *CurrentModule = Queue[QueueStart++];
229e41d7feaSDouglas Gregor       VisitOrder.push_back(CurrentModule);
230d44252ecSDouglas Gregor 
231d44252ecSDouglas Gregor       // For any module that this module depends on, push it on the
232d44252ecSDouglas Gregor       // stack (if it hasn't already been marked as visited).
233bdb259d2SDouglas Gregor       for (llvm::SetVector<ModuleFile *>::iterator
234bdb259d2SDouglas Gregor              M = CurrentModule->Imports.begin(),
235d44252ecSDouglas Gregor              MEnd = CurrentModule->Imports.end();
236d44252ecSDouglas Gregor            M != MEnd; ++M) {
237d44252ecSDouglas Gregor         // Remove our current module as an impediment to visiting the
238d44252ecSDouglas Gregor         // module we depend on. If we were the last unvisited module
239d44252ecSDouglas Gregor         // that depends on this particular module, push it into the
240d44252ecSDouglas Gregor         // queue to be visited.
241bdb259d2SDouglas Gregor         unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
242d44252ecSDouglas Gregor         if (NumUnusedEdges && (--NumUnusedEdges == 0))
243d44252ecSDouglas Gregor           Queue.push_back(*M);
244d44252ecSDouglas Gregor       }
245d44252ecSDouglas Gregor     }
246e41d7feaSDouglas Gregor 
247e41d7feaSDouglas Gregor     assert(VisitOrder.size() == N && "Visitation order is wrong?");
2487211ac15SDouglas Gregor 
2497211ac15SDouglas Gregor     // We may need to update the set of modules we have in common with the
2507211ac15SDouglas Gregor     // global module index, since modules could have been added to the module
2517211ac15SDouglas Gregor     // manager since we loaded the global module index.
2527211ac15SDouglas Gregor     updateModulesInCommonWithGlobalIndex();
253*e97cd90aSDouglas Gregor 
254*e97cd90aSDouglas Gregor     delete FirstVisitState;
255*e97cd90aSDouglas Gregor     FirstVisitState = 0;
256e41d7feaSDouglas Gregor   }
257e41d7feaSDouglas Gregor 
258*e97cd90aSDouglas Gregor   VisitState *State = allocateVisitState();
259*e97cd90aSDouglas Gregor   unsigned VisitNumber = State->NextVisitNumber++;
260e41d7feaSDouglas Gregor 
2617211ac15SDouglas Gregor   // If the caller has provided us with a hit-set that came from the global
2627211ac15SDouglas Gregor   // module index, mark every module file in common with the global module
2637211ac15SDouglas Gregor   // index that is *not* in that set as 'visited'.
2647211ac15SDouglas Gregor   if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
2657211ac15SDouglas Gregor     for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
2667211ac15SDouglas Gregor     {
2677211ac15SDouglas Gregor       ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
2687211ac15SDouglas Gregor       if (!ModuleFilesHit->count(M->File))
269*e97cd90aSDouglas Gregor         State->VisitNumber[M->Index] = VisitNumber;
2707211ac15SDouglas Gregor     }
2717211ac15SDouglas Gregor   }
2727211ac15SDouglas Gregor 
273e41d7feaSDouglas Gregor   for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
274e41d7feaSDouglas Gregor     ModuleFile *CurrentModule = VisitOrder[I];
275e41d7feaSDouglas Gregor     // Should we skip this module file?
276*e97cd90aSDouglas Gregor     if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
277e41d7feaSDouglas Gregor       continue;
278e41d7feaSDouglas Gregor 
279e41d7feaSDouglas Gregor     // Visit the module.
280*e97cd90aSDouglas Gregor     assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
281*e97cd90aSDouglas Gregor     State->VisitNumber[CurrentModule->Index] = VisitNumber;
282e41d7feaSDouglas Gregor     if (!Visitor(*CurrentModule, UserData))
283e41d7feaSDouglas Gregor       continue;
284e41d7feaSDouglas Gregor 
285e41d7feaSDouglas Gregor     // The visitor has requested that cut off visitation of any
286e41d7feaSDouglas Gregor     // module that the current module depends on. To indicate this
287e41d7feaSDouglas Gregor     // behavior, we mark all of the reachable modules as having been visited.
288e41d7feaSDouglas Gregor     ModuleFile *NextModule = CurrentModule;
289e41d7feaSDouglas Gregor     do {
290e41d7feaSDouglas Gregor       // For any module that this module depends on, push it on the
291e41d7feaSDouglas Gregor       // stack (if it hasn't already been marked as visited).
292e41d7feaSDouglas Gregor       for (llvm::SetVector<ModuleFile *>::iterator
293e41d7feaSDouglas Gregor              M = NextModule->Imports.begin(),
294e41d7feaSDouglas Gregor              MEnd = NextModule->Imports.end();
295e41d7feaSDouglas Gregor            M != MEnd; ++M) {
296*e97cd90aSDouglas Gregor         if (State->VisitNumber[(*M)->Index] != VisitNumber) {
297*e97cd90aSDouglas Gregor           State->Stack.push_back(*M);
298*e97cd90aSDouglas Gregor           State->VisitNumber[(*M)->Index] = VisitNumber;
299e41d7feaSDouglas Gregor         }
300e41d7feaSDouglas Gregor       }
301e41d7feaSDouglas Gregor 
302*e97cd90aSDouglas Gregor       if (State->Stack.empty())
303e41d7feaSDouglas Gregor         break;
304e41d7feaSDouglas Gregor 
305e41d7feaSDouglas Gregor       // Pop the next module off the stack.
306*e97cd90aSDouglas Gregor       NextModule = State->Stack.back();
307*e97cd90aSDouglas Gregor       State->Stack.pop_back();
308e41d7feaSDouglas Gregor     } while (true);
309e41d7feaSDouglas Gregor   }
310*e97cd90aSDouglas Gregor 
311*e97cd90aSDouglas Gregor   returnVisitState(State);
312d44252ecSDouglas Gregor }
313d44252ecSDouglas Gregor 
314d44252ecSDouglas Gregor /// \brief Perform a depth-first visit of the current module.
315de3ef502SDouglas Gregor static bool visitDepthFirst(ModuleFile &M,
316de3ef502SDouglas Gregor                             bool (*Visitor)(ModuleFile &M, bool Preorder,
317d44252ecSDouglas Gregor                                             void *UserData),
318d44252ecSDouglas Gregor                             void *UserData,
319bdb259d2SDouglas Gregor                             SmallVectorImpl<bool> &Visited) {
320d44252ecSDouglas Gregor   // Preorder visitation
321d44252ecSDouglas Gregor   if (Visitor(M, /*Preorder=*/true, UserData))
322d44252ecSDouglas Gregor     return true;
323d44252ecSDouglas Gregor 
324d44252ecSDouglas Gregor   // Visit children
325de3ef502SDouglas Gregor   for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
326d44252ecSDouglas Gregor                                             IMEnd = M.Imports.end();
327d44252ecSDouglas Gregor        IM != IMEnd; ++IM) {
328bdb259d2SDouglas Gregor     if (Visited[(*IM)->Index])
329d44252ecSDouglas Gregor       continue;
330bdb259d2SDouglas Gregor     Visited[(*IM)->Index] = true;
331d44252ecSDouglas Gregor 
332d44252ecSDouglas Gregor     if (visitDepthFirst(**IM, Visitor, UserData, Visited))
333d44252ecSDouglas Gregor       return true;
334d44252ecSDouglas Gregor   }
335d44252ecSDouglas Gregor 
336d44252ecSDouglas Gregor   // Postorder visitation
337d44252ecSDouglas Gregor   return Visitor(M, /*Preorder=*/false, UserData);
338d44252ecSDouglas Gregor }
339d44252ecSDouglas Gregor 
340de3ef502SDouglas Gregor void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
341d44252ecSDouglas Gregor                                                     void *UserData),
342d44252ecSDouglas Gregor                                     void *UserData) {
343bdb259d2SDouglas Gregor   SmallVector<bool, 16> Visited(size(), false);
344d44252ecSDouglas Gregor   for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
345bdb259d2SDouglas Gregor     if (Visited[Chain[I]->Index])
346d44252ecSDouglas Gregor       continue;
347bdb259d2SDouglas Gregor     Visited[Chain[I]->Index] = true;
348d44252ecSDouglas Gregor 
349d44252ecSDouglas Gregor     if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
350d44252ecSDouglas Gregor       return;
351d44252ecSDouglas Gregor   }
352d44252ecSDouglas Gregor }
3539d7c1a2aSDouglas Gregor 
3549d7c1a2aSDouglas Gregor #ifndef NDEBUG
3559d7c1a2aSDouglas Gregor namespace llvm {
3569d7c1a2aSDouglas Gregor   template<>
3579d7c1a2aSDouglas Gregor   struct GraphTraits<ModuleManager> {
358de3ef502SDouglas Gregor     typedef ModuleFile NodeType;
359de3ef502SDouglas Gregor     typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
3609d7c1a2aSDouglas Gregor     typedef ModuleManager::ModuleConstIterator nodes_iterator;
3619d7c1a2aSDouglas Gregor 
3629d7c1a2aSDouglas Gregor     static ChildIteratorType child_begin(NodeType *Node) {
3639d7c1a2aSDouglas Gregor       return Node->Imports.begin();
3649d7c1a2aSDouglas Gregor     }
3659d7c1a2aSDouglas Gregor 
3669d7c1a2aSDouglas Gregor     static ChildIteratorType child_end(NodeType *Node) {
3679d7c1a2aSDouglas Gregor       return Node->Imports.end();
3689d7c1a2aSDouglas Gregor     }
3699d7c1a2aSDouglas Gregor 
3709d7c1a2aSDouglas Gregor     static nodes_iterator nodes_begin(const ModuleManager &Manager) {
3719d7c1a2aSDouglas Gregor       return Manager.begin();
3729d7c1a2aSDouglas Gregor     }
3739d7c1a2aSDouglas Gregor 
3749d7c1a2aSDouglas Gregor     static nodes_iterator nodes_end(const ModuleManager &Manager) {
3759d7c1a2aSDouglas Gregor       return Manager.end();
3769d7c1a2aSDouglas Gregor     }
3779d7c1a2aSDouglas Gregor   };
3789d7c1a2aSDouglas Gregor 
3799d7c1a2aSDouglas Gregor   template<>
3809d7c1a2aSDouglas Gregor   struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
3819d7c1a2aSDouglas Gregor     explicit DOTGraphTraits(bool IsSimple = false)
3829d7c1a2aSDouglas Gregor       : DefaultDOTGraphTraits(IsSimple) { }
3839d7c1a2aSDouglas Gregor 
3849d7c1a2aSDouglas Gregor     static bool renderGraphFromBottomUp() {
3859d7c1a2aSDouglas Gregor       return true;
3869d7c1a2aSDouglas Gregor     }
3879d7c1a2aSDouglas Gregor 
388de3ef502SDouglas Gregor     std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
3899d7c1a2aSDouglas Gregor       return llvm::sys::path::stem(M->FileName);
3909d7c1a2aSDouglas Gregor     }
3919d7c1a2aSDouglas Gregor   };
3929d7c1a2aSDouglas Gregor }
3939d7c1a2aSDouglas Gregor 
3949d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() {
3959d7c1a2aSDouglas Gregor   llvm::ViewGraph(*this, "Modules");
3969d7c1a2aSDouglas Gregor }
3979d7c1a2aSDouglas Gregor #endif
398