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) {
28*dadd85dcSDouglas Gregor   const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
29*dadd85dcSDouglas Gregor                                            /*cacheFailure=*/false);
30d44252ecSDouglas Gregor   return Modules[Entry];
31d44252ecSDouglas Gregor }
32d44252ecSDouglas Gregor 
33d44252ecSDouglas Gregor llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
34*dadd85dcSDouglas Gregor   const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
35*dadd85dcSDouglas Gregor                                            /*cacheFailure=*/false);
36d44252ecSDouglas Gregor   return InMemoryBuffers[Entry];
37d44252ecSDouglas Gregor }
38d44252ecSDouglas Gregor 
39de3ef502SDouglas Gregor std::pair<ModuleFile *, bool>
40d44252ecSDouglas Gregor ModuleManager::addModule(StringRef FileName, ModuleKind Type,
416fb03aeaSDouglas Gregor                          SourceLocation ImportLoc, ModuleFile *ImportedBy,
426fb03aeaSDouglas Gregor                          unsigned Generation, std::string &ErrorStr) {
43*dadd85dcSDouglas Gregor   const FileEntry *Entry = FileMgr.getFile(FileName, /*openFile=*/false,
44*dadd85dcSDouglas Gregor                                            /*cacheFailure=*/false);
45d44252ecSDouglas Gregor   if (!Entry && FileName != "-") {
46d44252ecSDouglas Gregor     ErrorStr = "file not found";
47de3ef502SDouglas Gregor     return std::make_pair(static_cast<ModuleFile*>(0), false);
48d44252ecSDouglas Gregor   }
49d44252ecSDouglas Gregor 
50d44252ecSDouglas Gregor   // Check whether we already loaded this module, before
51de3ef502SDouglas Gregor   ModuleFile *&ModuleEntry = Modules[Entry];
52d44252ecSDouglas Gregor   bool NewModule = false;
53d44252ecSDouglas Gregor   if (!ModuleEntry) {
54d44252ecSDouglas Gregor     // Allocate a new module.
554fc9f3e8SDouglas Gregor     ModuleFile *New = new ModuleFile(Type, Generation);
56bdb259d2SDouglas Gregor     New->Index = Chain.size();
57d44252ecSDouglas Gregor     New->FileName = FileName.str();
58aedf7144SArgyrios Kyrtzidis     New->File = Entry;
596fb03aeaSDouglas Gregor     New->ImportLoc = ImportLoc;
60d44252ecSDouglas Gregor     Chain.push_back(New);
61d44252ecSDouglas Gregor     NewModule = true;
62d44252ecSDouglas Gregor     ModuleEntry = New;
63d44252ecSDouglas Gregor 
64d44252ecSDouglas Gregor     // Load the contents of the module
65d44252ecSDouglas Gregor     if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) {
66d44252ecSDouglas Gregor       // The buffer was already provided for us.
67d44252ecSDouglas Gregor       assert(Buffer && "Passed null buffer");
68d44252ecSDouglas Gregor       New->Buffer.reset(Buffer);
69d44252ecSDouglas Gregor     } else {
70d44252ecSDouglas Gregor       // Open the AST file.
71d44252ecSDouglas Gregor       llvm::error_code ec;
72d44252ecSDouglas Gregor       if (FileName == "-") {
73d44252ecSDouglas Gregor         ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
74d44252ecSDouglas Gregor         if (ec)
75d44252ecSDouglas Gregor           ErrorStr = ec.message();
76d44252ecSDouglas Gregor       } else
77d44252ecSDouglas Gregor         New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr));
78d44252ecSDouglas Gregor 
79d44252ecSDouglas Gregor       if (!New->Buffer)
80de3ef502SDouglas Gregor         return std::make_pair(static_cast<ModuleFile*>(0), false);
81d44252ecSDouglas Gregor     }
82d44252ecSDouglas Gregor 
83d44252ecSDouglas Gregor     // Initialize the stream
84d44252ecSDouglas Gregor     New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
85d44252ecSDouglas Gregor                          (const unsigned char *)New->Buffer->getBufferEnd());     }
86d44252ecSDouglas Gregor 
87d44252ecSDouglas Gregor   if (ImportedBy) {
88d44252ecSDouglas Gregor     ModuleEntry->ImportedBy.insert(ImportedBy);
89d44252ecSDouglas Gregor     ImportedBy->Imports.insert(ModuleEntry);
90d44252ecSDouglas Gregor   } else {
916fb03aeaSDouglas Gregor     if (!ModuleEntry->DirectlyImported)
926fb03aeaSDouglas Gregor       ModuleEntry->ImportLoc = ImportLoc;
936fb03aeaSDouglas Gregor 
94d44252ecSDouglas Gregor     ModuleEntry->DirectlyImported = true;
95d44252ecSDouglas Gregor   }
96d44252ecSDouglas Gregor 
97d44252ecSDouglas Gregor   return std::make_pair(ModuleEntry, NewModule);
98d44252ecSDouglas Gregor }
99d44252ecSDouglas Gregor 
100188dbef2SDouglas Gregor namespace {
101188dbef2SDouglas Gregor   /// \brief Predicate that checks whether a module file occurs within
102188dbef2SDouglas Gregor   /// the given set.
103188dbef2SDouglas Gregor   class IsInModuleFileSet : public std::unary_function<ModuleFile *, bool> {
104188dbef2SDouglas Gregor     llvm::SmallPtrSet<ModuleFile *, 4> &Removed;
105188dbef2SDouglas Gregor 
106188dbef2SDouglas Gregor   public:
107188dbef2SDouglas Gregor     IsInModuleFileSet(llvm::SmallPtrSet<ModuleFile *, 4> &Removed)
108188dbef2SDouglas Gregor     : Removed(Removed) { }
109188dbef2SDouglas Gregor 
110188dbef2SDouglas Gregor     bool operator()(ModuleFile *MF) const {
111188dbef2SDouglas Gregor       return Removed.count(MF);
112188dbef2SDouglas Gregor     }
113188dbef2SDouglas Gregor   };
114188dbef2SDouglas Gregor }
115188dbef2SDouglas Gregor 
116188dbef2SDouglas Gregor void ModuleManager::removeModules(ModuleIterator first, ModuleIterator last) {
117188dbef2SDouglas Gregor   if (first == last)
118188dbef2SDouglas Gregor     return;
119188dbef2SDouglas Gregor 
120188dbef2SDouglas Gregor   // Collect the set of module file pointers that we'll be removing.
121188dbef2SDouglas Gregor   llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
122188dbef2SDouglas Gregor 
123188dbef2SDouglas Gregor   // Remove any references to the now-destroyed modules.
124188dbef2SDouglas Gregor   IsInModuleFileSet checkInSet(victimSet);
125188dbef2SDouglas Gregor   for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
126188dbef2SDouglas Gregor     Chain[i]->ImportedBy.remove_if(checkInSet);
127188dbef2SDouglas Gregor   }
128188dbef2SDouglas Gregor 
129188dbef2SDouglas Gregor   // Delete the modules and erase them from the various structures.
130188dbef2SDouglas Gregor   for (ModuleIterator victim = first; victim != last; ++victim) {
131188dbef2SDouglas Gregor     Modules.erase((*victim)->File);
132188dbef2SDouglas Gregor     delete *victim;
133188dbef2SDouglas Gregor   }
134188dbef2SDouglas Gregor 
135188dbef2SDouglas Gregor   // Remove the modules from the chain.
136188dbef2SDouglas Gregor   Chain.erase(first, last);
137188dbef2SDouglas Gregor }
138188dbef2SDouglas Gregor 
139d44252ecSDouglas Gregor void ModuleManager::addInMemoryBuffer(StringRef FileName,
140d44252ecSDouglas Gregor                                       llvm::MemoryBuffer *Buffer) {
141d44252ecSDouglas Gregor 
142d44252ecSDouglas Gregor   const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
143d44252ecSDouglas Gregor                                                   Buffer->getBufferSize(), 0);
144d44252ecSDouglas Gregor   InMemoryBuffers[Entry] = Buffer;
145d44252ecSDouglas Gregor }
146d44252ecSDouglas Gregor 
1477211ac15SDouglas Gregor void ModuleManager::updateModulesInCommonWithGlobalIndex() {
1487211ac15SDouglas Gregor   ModulesInCommonWithGlobalIndex.clear();
1497211ac15SDouglas Gregor 
1507211ac15SDouglas Gregor   if (!GlobalIndex)
1517211ac15SDouglas Gregor     return;
1527211ac15SDouglas Gregor 
1537211ac15SDouglas Gregor   // Collect the set of modules known to the global index.
1547211ac15SDouglas Gregor   SmallVector<const FileEntry *, 16> KnownModules;
1557211ac15SDouglas Gregor   GlobalIndex->getKnownModules(KnownModules);
1567211ac15SDouglas Gregor 
1577211ac15SDouglas Gregor   // Map those modules to AST files known to the module manager.
1587211ac15SDouglas Gregor   for (unsigned I = 0, N = KnownModules.size(); I != N; ++I) {
1597211ac15SDouglas Gregor     llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known
1607211ac15SDouglas Gregor       = Modules.find(KnownModules[I]);
1617211ac15SDouglas Gregor     if (Known == Modules.end())
1627211ac15SDouglas Gregor       continue;
1637211ac15SDouglas Gregor 
1647211ac15SDouglas Gregor     ModulesInCommonWithGlobalIndex.push_back(Known->second);
1657211ac15SDouglas Gregor   }
1667211ac15SDouglas Gregor }
1677211ac15SDouglas Gregor 
168e97cd90aSDouglas Gregor ModuleManager::VisitState *ModuleManager::allocateVisitState() {
169e97cd90aSDouglas Gregor   // Fast path: if we have a cached state, use it.
170e97cd90aSDouglas Gregor   if (FirstVisitState) {
171e97cd90aSDouglas Gregor     VisitState *Result = FirstVisitState;
172e97cd90aSDouglas Gregor     FirstVisitState = FirstVisitState->NextState;
173e97cd90aSDouglas Gregor     Result->NextState = 0;
174e97cd90aSDouglas Gregor     return Result;
175e97cd90aSDouglas Gregor   }
176e97cd90aSDouglas Gregor 
177e97cd90aSDouglas Gregor   // Allocate and return a new state.
178e97cd90aSDouglas Gregor   return new VisitState(size());
179e97cd90aSDouglas Gregor }
180e97cd90aSDouglas Gregor 
181e97cd90aSDouglas Gregor void ModuleManager::returnVisitState(VisitState *State) {
182e97cd90aSDouglas Gregor   assert(State->NextState == 0 && "Visited state is in list?");
183e97cd90aSDouglas Gregor   State->NextState = FirstVisitState;
184e97cd90aSDouglas Gregor   FirstVisitState = State;
185e97cd90aSDouglas Gregor }
186e97cd90aSDouglas Gregor 
1877211ac15SDouglas Gregor void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
1887211ac15SDouglas Gregor   GlobalIndex = Index;
1897211ac15SDouglas Gregor   updateModulesInCommonWithGlobalIndex();
1907211ac15SDouglas Gregor }
1917211ac15SDouglas Gregor 
1927211ac15SDouglas Gregor ModuleManager::ModuleManager(FileManager &FileMgr)
193e97cd90aSDouglas Gregor   : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(0) { }
194d44252ecSDouglas Gregor 
195d44252ecSDouglas Gregor ModuleManager::~ModuleManager() {
196d44252ecSDouglas Gregor   for (unsigned i = 0, e = Chain.size(); i != e; ++i)
197d44252ecSDouglas Gregor     delete Chain[e - i - 1];
198e97cd90aSDouglas Gregor   delete FirstVisitState;
199d44252ecSDouglas Gregor }
200d44252ecSDouglas Gregor 
2017211ac15SDouglas Gregor void
2027211ac15SDouglas Gregor ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
2037211ac15SDouglas Gregor                      void *UserData,
2047211ac15SDouglas Gregor                      llvm::SmallPtrSet<const FileEntry *, 4> *ModuleFilesHit) {
2057211ac15SDouglas Gregor   // If the visitation order vector is the wrong size, recompute the order.
206e41d7feaSDouglas Gregor   if (VisitOrder.size() != Chain.size()) {
207d44252ecSDouglas Gregor     unsigned N = size();
208e41d7feaSDouglas Gregor     VisitOrder.clear();
209e41d7feaSDouglas Gregor     VisitOrder.reserve(N);
210d44252ecSDouglas Gregor 
211d44252ecSDouglas Gregor     // Record the number of incoming edges for each module. When we
212d44252ecSDouglas Gregor     // encounter a module with no incoming edges, push it into the queue
213d44252ecSDouglas Gregor     // to seed the queue.
214de3ef502SDouglas Gregor     SmallVector<ModuleFile *, 4> Queue;
215d44252ecSDouglas Gregor     Queue.reserve(N);
216bdb259d2SDouglas Gregor     llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
217bdb259d2SDouglas Gregor     UnusedIncomingEdges.reserve(size());
218d44252ecSDouglas Gregor     for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
219d44252ecSDouglas Gregor       if (unsigned Size = (*M)->ImportedBy.size())
220bdb259d2SDouglas Gregor         UnusedIncomingEdges.push_back(Size);
221bdb259d2SDouglas Gregor       else {
222bdb259d2SDouglas Gregor         UnusedIncomingEdges.push_back(0);
223d44252ecSDouglas Gregor         Queue.push_back(*M);
224d44252ecSDouglas Gregor       }
225bdb259d2SDouglas Gregor     }
226d44252ecSDouglas Gregor 
227e41d7feaSDouglas Gregor     // Traverse the graph, making sure to visit a module before visiting any
228e41d7feaSDouglas Gregor     // of its dependencies.
229d44252ecSDouglas Gregor     unsigned QueueStart = 0;
230d44252ecSDouglas Gregor     while (QueueStart < Queue.size()) {
231de3ef502SDouglas Gregor       ModuleFile *CurrentModule = Queue[QueueStart++];
232e41d7feaSDouglas Gregor       VisitOrder.push_back(CurrentModule);
233d44252ecSDouglas Gregor 
234d44252ecSDouglas Gregor       // For any module that this module depends on, push it on the
235d44252ecSDouglas Gregor       // stack (if it hasn't already been marked as visited).
236bdb259d2SDouglas Gregor       for (llvm::SetVector<ModuleFile *>::iterator
237bdb259d2SDouglas Gregor              M = CurrentModule->Imports.begin(),
238d44252ecSDouglas Gregor              MEnd = CurrentModule->Imports.end();
239d44252ecSDouglas Gregor            M != MEnd; ++M) {
240d44252ecSDouglas Gregor         // Remove our current module as an impediment to visiting the
241d44252ecSDouglas Gregor         // module we depend on. If we were the last unvisited module
242d44252ecSDouglas Gregor         // that depends on this particular module, push it into the
243d44252ecSDouglas Gregor         // queue to be visited.
244bdb259d2SDouglas Gregor         unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
245d44252ecSDouglas Gregor         if (NumUnusedEdges && (--NumUnusedEdges == 0))
246d44252ecSDouglas Gregor           Queue.push_back(*M);
247d44252ecSDouglas Gregor       }
248d44252ecSDouglas Gregor     }
249e41d7feaSDouglas Gregor 
250e41d7feaSDouglas Gregor     assert(VisitOrder.size() == N && "Visitation order is wrong?");
2517211ac15SDouglas Gregor 
2527211ac15SDouglas Gregor     // We may need to update the set of modules we have in common with the
2537211ac15SDouglas Gregor     // global module index, since modules could have been added to the module
2547211ac15SDouglas Gregor     // manager since we loaded the global module index.
2557211ac15SDouglas Gregor     updateModulesInCommonWithGlobalIndex();
256e97cd90aSDouglas Gregor 
257e97cd90aSDouglas Gregor     delete FirstVisitState;
258e97cd90aSDouglas Gregor     FirstVisitState = 0;
259e41d7feaSDouglas Gregor   }
260e41d7feaSDouglas Gregor 
261e97cd90aSDouglas Gregor   VisitState *State = allocateVisitState();
262e97cd90aSDouglas Gregor   unsigned VisitNumber = State->NextVisitNumber++;
263e41d7feaSDouglas Gregor 
2647211ac15SDouglas Gregor   // If the caller has provided us with a hit-set that came from the global
2657211ac15SDouglas Gregor   // module index, mark every module file in common with the global module
2667211ac15SDouglas Gregor   // index that is *not* in that set as 'visited'.
2677211ac15SDouglas Gregor   if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
2687211ac15SDouglas Gregor     for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
2697211ac15SDouglas Gregor     {
2707211ac15SDouglas Gregor       ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
2717211ac15SDouglas Gregor       if (!ModuleFilesHit->count(M->File))
272e97cd90aSDouglas Gregor         State->VisitNumber[M->Index] = VisitNumber;
2737211ac15SDouglas Gregor     }
2747211ac15SDouglas Gregor   }
2757211ac15SDouglas Gregor 
276e41d7feaSDouglas Gregor   for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
277e41d7feaSDouglas Gregor     ModuleFile *CurrentModule = VisitOrder[I];
278e41d7feaSDouglas Gregor     // Should we skip this module file?
279e97cd90aSDouglas Gregor     if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
280e41d7feaSDouglas Gregor       continue;
281e41d7feaSDouglas Gregor 
282e41d7feaSDouglas Gregor     // Visit the module.
283e97cd90aSDouglas Gregor     assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
284e97cd90aSDouglas Gregor     State->VisitNumber[CurrentModule->Index] = VisitNumber;
285e41d7feaSDouglas Gregor     if (!Visitor(*CurrentModule, UserData))
286e41d7feaSDouglas Gregor       continue;
287e41d7feaSDouglas Gregor 
288e41d7feaSDouglas Gregor     // The visitor has requested that cut off visitation of any
289e41d7feaSDouglas Gregor     // module that the current module depends on. To indicate this
290e41d7feaSDouglas Gregor     // behavior, we mark all of the reachable modules as having been visited.
291e41d7feaSDouglas Gregor     ModuleFile *NextModule = CurrentModule;
292e41d7feaSDouglas Gregor     do {
293e41d7feaSDouglas Gregor       // For any module that this module depends on, push it on the
294e41d7feaSDouglas Gregor       // stack (if it hasn't already been marked as visited).
295e41d7feaSDouglas Gregor       for (llvm::SetVector<ModuleFile *>::iterator
296e41d7feaSDouglas Gregor              M = NextModule->Imports.begin(),
297e41d7feaSDouglas Gregor              MEnd = NextModule->Imports.end();
298e41d7feaSDouglas Gregor            M != MEnd; ++M) {
299e97cd90aSDouglas Gregor         if (State->VisitNumber[(*M)->Index] != VisitNumber) {
300e97cd90aSDouglas Gregor           State->Stack.push_back(*M);
301e97cd90aSDouglas Gregor           State->VisitNumber[(*M)->Index] = VisitNumber;
302e41d7feaSDouglas Gregor         }
303e41d7feaSDouglas Gregor       }
304e41d7feaSDouglas Gregor 
305e97cd90aSDouglas Gregor       if (State->Stack.empty())
306e41d7feaSDouglas Gregor         break;
307e41d7feaSDouglas Gregor 
308e41d7feaSDouglas Gregor       // Pop the next module off the stack.
309e97cd90aSDouglas Gregor       NextModule = State->Stack.back();
310e97cd90aSDouglas Gregor       State->Stack.pop_back();
311e41d7feaSDouglas Gregor     } while (true);
312e41d7feaSDouglas Gregor   }
313e97cd90aSDouglas Gregor 
314e97cd90aSDouglas Gregor   returnVisitState(State);
315d44252ecSDouglas Gregor }
316d44252ecSDouglas Gregor 
317d44252ecSDouglas Gregor /// \brief Perform a depth-first visit of the current module.
318de3ef502SDouglas Gregor static bool visitDepthFirst(ModuleFile &M,
319de3ef502SDouglas Gregor                             bool (*Visitor)(ModuleFile &M, bool Preorder,
320d44252ecSDouglas Gregor                                             void *UserData),
321d44252ecSDouglas Gregor                             void *UserData,
322bdb259d2SDouglas Gregor                             SmallVectorImpl<bool> &Visited) {
323d44252ecSDouglas Gregor   // Preorder visitation
324d44252ecSDouglas Gregor   if (Visitor(M, /*Preorder=*/true, UserData))
325d44252ecSDouglas Gregor     return true;
326d44252ecSDouglas Gregor 
327d44252ecSDouglas Gregor   // Visit children
328de3ef502SDouglas Gregor   for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
329d44252ecSDouglas Gregor                                             IMEnd = M.Imports.end();
330d44252ecSDouglas Gregor        IM != IMEnd; ++IM) {
331bdb259d2SDouglas Gregor     if (Visited[(*IM)->Index])
332d44252ecSDouglas Gregor       continue;
333bdb259d2SDouglas Gregor     Visited[(*IM)->Index] = true;
334d44252ecSDouglas Gregor 
335d44252ecSDouglas Gregor     if (visitDepthFirst(**IM, Visitor, UserData, Visited))
336d44252ecSDouglas Gregor       return true;
337d44252ecSDouglas Gregor   }
338d44252ecSDouglas Gregor 
339d44252ecSDouglas Gregor   // Postorder visitation
340d44252ecSDouglas Gregor   return Visitor(M, /*Preorder=*/false, UserData);
341d44252ecSDouglas Gregor }
342d44252ecSDouglas Gregor 
343de3ef502SDouglas Gregor void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
344d44252ecSDouglas Gregor                                                     void *UserData),
345d44252ecSDouglas Gregor                                     void *UserData) {
346bdb259d2SDouglas Gregor   SmallVector<bool, 16> Visited(size(), false);
347d44252ecSDouglas Gregor   for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
348bdb259d2SDouglas Gregor     if (Visited[Chain[I]->Index])
349d44252ecSDouglas Gregor       continue;
350bdb259d2SDouglas Gregor     Visited[Chain[I]->Index] = true;
351d44252ecSDouglas Gregor 
352d44252ecSDouglas Gregor     if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
353d44252ecSDouglas Gregor       return;
354d44252ecSDouglas Gregor   }
355d44252ecSDouglas Gregor }
3569d7c1a2aSDouglas Gregor 
3579d7c1a2aSDouglas Gregor #ifndef NDEBUG
3589d7c1a2aSDouglas Gregor namespace llvm {
3599d7c1a2aSDouglas Gregor   template<>
3609d7c1a2aSDouglas Gregor   struct GraphTraits<ModuleManager> {
361de3ef502SDouglas Gregor     typedef ModuleFile NodeType;
362de3ef502SDouglas Gregor     typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
3639d7c1a2aSDouglas Gregor     typedef ModuleManager::ModuleConstIterator nodes_iterator;
3649d7c1a2aSDouglas Gregor 
3659d7c1a2aSDouglas Gregor     static ChildIteratorType child_begin(NodeType *Node) {
3669d7c1a2aSDouglas Gregor       return Node->Imports.begin();
3679d7c1a2aSDouglas Gregor     }
3689d7c1a2aSDouglas Gregor 
3699d7c1a2aSDouglas Gregor     static ChildIteratorType child_end(NodeType *Node) {
3709d7c1a2aSDouglas Gregor       return Node->Imports.end();
3719d7c1a2aSDouglas Gregor     }
3729d7c1a2aSDouglas Gregor 
3739d7c1a2aSDouglas Gregor     static nodes_iterator nodes_begin(const ModuleManager &Manager) {
3749d7c1a2aSDouglas Gregor       return Manager.begin();
3759d7c1a2aSDouglas Gregor     }
3769d7c1a2aSDouglas Gregor 
3779d7c1a2aSDouglas Gregor     static nodes_iterator nodes_end(const ModuleManager &Manager) {
3789d7c1a2aSDouglas Gregor       return Manager.end();
3799d7c1a2aSDouglas Gregor     }
3809d7c1a2aSDouglas Gregor   };
3819d7c1a2aSDouglas Gregor 
3829d7c1a2aSDouglas Gregor   template<>
3839d7c1a2aSDouglas Gregor   struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
3849d7c1a2aSDouglas Gregor     explicit DOTGraphTraits(bool IsSimple = false)
3859d7c1a2aSDouglas Gregor       : DefaultDOTGraphTraits(IsSimple) { }
3869d7c1a2aSDouglas Gregor 
3879d7c1a2aSDouglas Gregor     static bool renderGraphFromBottomUp() {
3889d7c1a2aSDouglas Gregor       return true;
3899d7c1a2aSDouglas Gregor     }
3909d7c1a2aSDouglas Gregor 
391de3ef502SDouglas Gregor     std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
3929d7c1a2aSDouglas Gregor       return llvm::sys::path::stem(M->FileName);
3939d7c1a2aSDouglas Gregor     }
3949d7c1a2aSDouglas Gregor   };
3959d7c1a2aSDouglas Gregor }
3969d7c1a2aSDouglas Gregor 
3979d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() {
3989d7c1a2aSDouglas Gregor   llvm::ViewGraph(*this, "Modules");
3999d7c1a2aSDouglas Gregor }
4009d7c1a2aSDouglas Gregor #endif
401