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 //===----------------------------------------------------------------------===//
14*beee15e7SBen Langmuir #include "clang/Lex/HeaderSearch.h"
157029ce1aSDouglas Gregor #include "clang/Lex/ModuleMap.h"
167211ac15SDouglas Gregor #include "clang/Serialization/GlobalModuleIndex.h"
17af04f98cSBenjamin Kramer #include "clang/Serialization/ModuleManager.h"
18d44252ecSDouglas Gregor #include "llvm/Support/MemoryBuffer.h"
19552c169eSRafael Espindola #include "llvm/Support/Path.h"
20d44252ecSDouglas Gregor #include "llvm/Support/raw_ostream.h"
21d44252ecSDouglas Gregor #include "llvm/Support/system_error.h"
22d44252ecSDouglas Gregor 
239d7c1a2aSDouglas Gregor #ifndef NDEBUG
249d7c1a2aSDouglas Gregor #include "llvm/Support/GraphWriter.h"
259d7c1a2aSDouglas Gregor #endif
269d7c1a2aSDouglas Gregor 
27d44252ecSDouglas Gregor using namespace clang;
28d44252ecSDouglas Gregor using namespace serialization;
29d44252ecSDouglas Gregor 
30de3ef502SDouglas Gregor ModuleFile *ModuleManager::lookup(StringRef Name) {
31dadd85dcSDouglas Gregor   const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
32dadd85dcSDouglas Gregor                                            /*cacheFailure=*/false);
33bf7fc9c5SDouglas Gregor   if (Entry)
34bf7fc9c5SDouglas Gregor     return lookup(Entry);
35bf7fc9c5SDouglas Gregor 
36bf7fc9c5SDouglas Gregor   return 0;
37bf7fc9c5SDouglas Gregor }
38bf7fc9c5SDouglas Gregor 
39bf7fc9c5SDouglas Gregor ModuleFile *ModuleManager::lookup(const FileEntry *File) {
40bf7fc9c5SDouglas Gregor   llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known
41bf7fc9c5SDouglas Gregor     = Modules.find(File);
42bf7fc9c5SDouglas Gregor   if (Known == Modules.end())
43bf7fc9c5SDouglas Gregor     return 0;
44bf7fc9c5SDouglas Gregor 
45bf7fc9c5SDouglas Gregor   return Known->second;
46d44252ecSDouglas Gregor }
47d44252ecSDouglas Gregor 
48d44252ecSDouglas Gregor llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
49dadd85dcSDouglas Gregor   const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
50dadd85dcSDouglas Gregor                                            /*cacheFailure=*/false);
51d44252ecSDouglas Gregor   return InMemoryBuffers[Entry];
52d44252ecSDouglas Gregor }
53d44252ecSDouglas Gregor 
547029ce1aSDouglas Gregor ModuleManager::AddModuleResult
55d44252ecSDouglas Gregor ModuleManager::addModule(StringRef FileName, ModuleKind Type,
566fb03aeaSDouglas Gregor                          SourceLocation ImportLoc, ModuleFile *ImportedBy,
577029ce1aSDouglas Gregor                          unsigned Generation,
587029ce1aSDouglas Gregor                          off_t ExpectedSize, time_t ExpectedModTime,
597029ce1aSDouglas Gregor                          ModuleFile *&Module,
607029ce1aSDouglas Gregor                          std::string &ErrorStr) {
617029ce1aSDouglas Gregor   Module = 0;
627029ce1aSDouglas Gregor 
637029ce1aSDouglas Gregor   // Look for the file entry. This only fails if the expected size or
647029ce1aSDouglas Gregor   // modification time differ.
657029ce1aSDouglas Gregor   const FileEntry *Entry;
66c27d0d5eSEli Friedman   if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
67c27d0d5eSEli Friedman     ErrorStr = "module file out of date";
687029ce1aSDouglas Gregor     return OutOfDate;
69c27d0d5eSEli Friedman   }
707029ce1aSDouglas Gregor 
71d44252ecSDouglas Gregor   if (!Entry && FileName != "-") {
72c27d0d5eSEli Friedman     ErrorStr = "module file not found";
737029ce1aSDouglas Gregor     return Missing;
74d44252ecSDouglas Gregor   }
75d44252ecSDouglas Gregor 
76d44252ecSDouglas Gregor   // Check whether we already loaded this module, before
77de3ef502SDouglas Gregor   ModuleFile *&ModuleEntry = Modules[Entry];
78d44252ecSDouglas Gregor   bool NewModule = false;
79d44252ecSDouglas Gregor   if (!ModuleEntry) {
80d44252ecSDouglas Gregor     // Allocate a new module.
814fc9f3e8SDouglas Gregor     ModuleFile *New = new ModuleFile(Type, Generation);
82bdb259d2SDouglas Gregor     New->Index = Chain.size();
83d44252ecSDouglas Gregor     New->FileName = FileName.str();
84aedf7144SArgyrios Kyrtzidis     New->File = Entry;
856fb03aeaSDouglas Gregor     New->ImportLoc = ImportLoc;
86d44252ecSDouglas Gregor     Chain.push_back(New);
87d44252ecSDouglas Gregor     NewModule = true;
88d44252ecSDouglas Gregor     ModuleEntry = New;
89d44252ecSDouglas Gregor 
90f430da4dSDmitri Gribenko     New->InputFilesValidationTimestamp = 0;
91f430da4dSDmitri Gribenko     if (New->Kind == MK_Module) {
92f430da4dSDmitri Gribenko       std::string TimestampFilename = New->getTimestampFilename();
93c8130a74SBen Langmuir       vfs::Status Status;
94f430da4dSDmitri Gribenko       // A cached stat value would be fine as well.
95f430da4dSDmitri Gribenko       if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
96f430da4dSDmitri Gribenko         New->InputFilesValidationTimestamp =
97f430da4dSDmitri Gribenko             Status.getLastModificationTime().toEpochTime();
98f430da4dSDmitri Gribenko     }
99f430da4dSDmitri Gribenko 
100d44252ecSDouglas Gregor     // Load the contents of the module
101d44252ecSDouglas Gregor     if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) {
102d44252ecSDouglas Gregor       // The buffer was already provided for us.
103d44252ecSDouglas Gregor       assert(Buffer && "Passed null buffer");
104d44252ecSDouglas Gregor       New->Buffer.reset(Buffer);
105d44252ecSDouglas Gregor     } else {
106d44252ecSDouglas Gregor       // Open the AST file.
107d44252ecSDouglas Gregor       llvm::error_code ec;
108d44252ecSDouglas Gregor       if (FileName == "-") {
109d44252ecSDouglas Gregor         ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
110d44252ecSDouglas Gregor         if (ec)
111d44252ecSDouglas Gregor           ErrorStr = ec.message();
112d44252ecSDouglas Gregor       } else
113d44252ecSDouglas Gregor         New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr));
114d44252ecSDouglas Gregor 
115d44252ecSDouglas Gregor       if (!New->Buffer)
1167029ce1aSDouglas Gregor         return Missing;
117d44252ecSDouglas Gregor     }
118d44252ecSDouglas Gregor 
119d44252ecSDouglas Gregor     // Initialize the stream
120d44252ecSDouglas Gregor     New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
1217029ce1aSDouglas Gregor                          (const unsigned char *)New->Buffer->getBufferEnd());
1227029ce1aSDouglas Gregor   }
123d44252ecSDouglas Gregor 
124d44252ecSDouglas Gregor   if (ImportedBy) {
125d44252ecSDouglas Gregor     ModuleEntry->ImportedBy.insert(ImportedBy);
126d44252ecSDouglas Gregor     ImportedBy->Imports.insert(ModuleEntry);
127d44252ecSDouglas Gregor   } else {
1286fb03aeaSDouglas Gregor     if (!ModuleEntry->DirectlyImported)
1296fb03aeaSDouglas Gregor       ModuleEntry->ImportLoc = ImportLoc;
1306fb03aeaSDouglas Gregor 
131d44252ecSDouglas Gregor     ModuleEntry->DirectlyImported = true;
132d44252ecSDouglas Gregor   }
133d44252ecSDouglas Gregor 
1347029ce1aSDouglas Gregor   Module = ModuleEntry;
1357029ce1aSDouglas Gregor   return NewModule? NewlyLoaded : AlreadyLoaded;
136d44252ecSDouglas Gregor }
137d44252ecSDouglas Gregor 
1387029ce1aSDouglas Gregor void ModuleManager::removeModules(ModuleIterator first, ModuleIterator last,
1397029ce1aSDouglas Gregor                                   ModuleMap *modMap) {
140188dbef2SDouglas Gregor   if (first == last)
141188dbef2SDouglas Gregor     return;
142188dbef2SDouglas Gregor 
143188dbef2SDouglas Gregor   // Collect the set of module file pointers that we'll be removing.
144188dbef2SDouglas Gregor   llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
145188dbef2SDouglas Gregor 
146188dbef2SDouglas Gregor   // Remove any references to the now-destroyed modules.
147188dbef2SDouglas Gregor   for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
148b55d0226SChandler Carruth     Chain[i]->ImportedBy.remove_if([&](ModuleFile *MF) {
149b55d0226SChandler Carruth       return victimSet.count(MF);
150b55d0226SChandler Carruth     });
151188dbef2SDouglas Gregor   }
152188dbef2SDouglas Gregor 
153188dbef2SDouglas Gregor   // Delete the modules and erase them from the various structures.
154188dbef2SDouglas Gregor   for (ModuleIterator victim = first; victim != last; ++victim) {
155188dbef2SDouglas Gregor     Modules.erase((*victim)->File);
1567029ce1aSDouglas Gregor 
1577029ce1aSDouglas Gregor     FileMgr.invalidateCache((*victim)->File);
1587029ce1aSDouglas Gregor     if (modMap) {
159*beee15e7SBen Langmuir       StringRef ModuleName = (*victim)->ModuleName;
1607029ce1aSDouglas Gregor       if (Module *mod = modMap->findModule(ModuleName)) {
1617029ce1aSDouglas Gregor         mod->setASTFile(0);
1627029ce1aSDouglas Gregor       }
1637029ce1aSDouglas Gregor     }
164188dbef2SDouglas Gregor     delete *victim;
165188dbef2SDouglas Gregor   }
166188dbef2SDouglas Gregor 
167188dbef2SDouglas Gregor   // Remove the modules from the chain.
168188dbef2SDouglas Gregor   Chain.erase(first, last);
169188dbef2SDouglas Gregor }
170188dbef2SDouglas Gregor 
171d44252ecSDouglas Gregor void ModuleManager::addInMemoryBuffer(StringRef FileName,
172d44252ecSDouglas Gregor                                       llvm::MemoryBuffer *Buffer) {
173d44252ecSDouglas Gregor 
174d44252ecSDouglas Gregor   const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
175d44252ecSDouglas Gregor                                                   Buffer->getBufferSize(), 0);
176d44252ecSDouglas Gregor   InMemoryBuffers[Entry] = Buffer;
177d44252ecSDouglas Gregor }
178d44252ecSDouglas Gregor 
179e97cd90aSDouglas Gregor ModuleManager::VisitState *ModuleManager::allocateVisitState() {
180e97cd90aSDouglas Gregor   // Fast path: if we have a cached state, use it.
181e97cd90aSDouglas Gregor   if (FirstVisitState) {
182e97cd90aSDouglas Gregor     VisitState *Result = FirstVisitState;
183e97cd90aSDouglas Gregor     FirstVisitState = FirstVisitState->NextState;
184e97cd90aSDouglas Gregor     Result->NextState = 0;
185e97cd90aSDouglas Gregor     return Result;
186e97cd90aSDouglas Gregor   }
187e97cd90aSDouglas Gregor 
188e97cd90aSDouglas Gregor   // Allocate and return a new state.
189e97cd90aSDouglas Gregor   return new VisitState(size());
190e97cd90aSDouglas Gregor }
191e97cd90aSDouglas Gregor 
192e97cd90aSDouglas Gregor void ModuleManager::returnVisitState(VisitState *State) {
193e97cd90aSDouglas Gregor   assert(State->NextState == 0 && "Visited state is in list?");
194e97cd90aSDouglas Gregor   State->NextState = FirstVisitState;
195e97cd90aSDouglas Gregor   FirstVisitState = State;
196e97cd90aSDouglas Gregor }
197e97cd90aSDouglas Gregor 
1987211ac15SDouglas Gregor void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
1997211ac15SDouglas Gregor   GlobalIndex = Index;
200603cd869SDouglas Gregor   if (!GlobalIndex) {
201603cd869SDouglas Gregor     ModulesInCommonWithGlobalIndex.clear();
202603cd869SDouglas Gregor     return;
2037029ce1aSDouglas Gregor   }
204603cd869SDouglas Gregor 
205603cd869SDouglas Gregor   // Notify the global module index about all of the modules we've already
206603cd869SDouglas Gregor   // loaded.
207603cd869SDouglas Gregor   for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
208603cd869SDouglas Gregor     if (!GlobalIndex->loadedModuleFile(Chain[I])) {
209603cd869SDouglas Gregor       ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
210603cd869SDouglas Gregor     }
211603cd869SDouglas Gregor   }
212603cd869SDouglas Gregor }
213603cd869SDouglas Gregor 
214603cd869SDouglas Gregor void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
215603cd869SDouglas Gregor   if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
216603cd869SDouglas Gregor     return;
217603cd869SDouglas Gregor 
218603cd869SDouglas Gregor   ModulesInCommonWithGlobalIndex.push_back(MF);
2197211ac15SDouglas Gregor }
2207211ac15SDouglas Gregor 
2217211ac15SDouglas Gregor ModuleManager::ModuleManager(FileManager &FileMgr)
222e97cd90aSDouglas Gregor   : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(0) { }
223d44252ecSDouglas Gregor 
224d44252ecSDouglas Gregor ModuleManager::~ModuleManager() {
225d44252ecSDouglas Gregor   for (unsigned i = 0, e = Chain.size(); i != e; ++i)
226d44252ecSDouglas Gregor     delete Chain[e - i - 1];
227e97cd90aSDouglas Gregor   delete FirstVisitState;
228d44252ecSDouglas Gregor }
229d44252ecSDouglas Gregor 
2307211ac15SDouglas Gregor void
2317211ac15SDouglas Gregor ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
2327211ac15SDouglas Gregor                      void *UserData,
2337029ce1aSDouglas Gregor                      llvm::SmallPtrSet<ModuleFile *, 4> *ModuleFilesHit) {
2347211ac15SDouglas Gregor   // If the visitation order vector is the wrong size, recompute the order.
235e41d7feaSDouglas Gregor   if (VisitOrder.size() != Chain.size()) {
236d44252ecSDouglas Gregor     unsigned N = size();
237e41d7feaSDouglas Gregor     VisitOrder.clear();
238e41d7feaSDouglas Gregor     VisitOrder.reserve(N);
239d44252ecSDouglas Gregor 
240d44252ecSDouglas Gregor     // Record the number of incoming edges for each module. When we
241d44252ecSDouglas Gregor     // encounter a module with no incoming edges, push it into the queue
242d44252ecSDouglas Gregor     // to seed the queue.
243de3ef502SDouglas Gregor     SmallVector<ModuleFile *, 4> Queue;
244d44252ecSDouglas Gregor     Queue.reserve(N);
245bdb259d2SDouglas Gregor     llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
246bdb259d2SDouglas Gregor     UnusedIncomingEdges.reserve(size());
247d44252ecSDouglas Gregor     for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
248d44252ecSDouglas Gregor       if (unsigned Size = (*M)->ImportedBy.size())
249bdb259d2SDouglas Gregor         UnusedIncomingEdges.push_back(Size);
250bdb259d2SDouglas Gregor       else {
251bdb259d2SDouglas Gregor         UnusedIncomingEdges.push_back(0);
252d44252ecSDouglas Gregor         Queue.push_back(*M);
253d44252ecSDouglas Gregor       }
254bdb259d2SDouglas Gregor     }
255d44252ecSDouglas Gregor 
256e41d7feaSDouglas Gregor     // Traverse the graph, making sure to visit a module before visiting any
257e41d7feaSDouglas Gregor     // of its dependencies.
258d44252ecSDouglas Gregor     unsigned QueueStart = 0;
259d44252ecSDouglas Gregor     while (QueueStart < Queue.size()) {
260de3ef502SDouglas Gregor       ModuleFile *CurrentModule = Queue[QueueStart++];
261e41d7feaSDouglas Gregor       VisitOrder.push_back(CurrentModule);
262d44252ecSDouglas Gregor 
263d44252ecSDouglas Gregor       // For any module that this module depends on, push it on the
264d44252ecSDouglas Gregor       // stack (if it hasn't already been marked as visited).
265bdb259d2SDouglas Gregor       for (llvm::SetVector<ModuleFile *>::iterator
266bdb259d2SDouglas Gregor              M = CurrentModule->Imports.begin(),
267d44252ecSDouglas Gregor              MEnd = CurrentModule->Imports.end();
268d44252ecSDouglas Gregor            M != MEnd; ++M) {
269d44252ecSDouglas Gregor         // Remove our current module as an impediment to visiting the
270d44252ecSDouglas Gregor         // module we depend on. If we were the last unvisited module
271d44252ecSDouglas Gregor         // that depends on this particular module, push it into the
272d44252ecSDouglas Gregor         // queue to be visited.
273bdb259d2SDouglas Gregor         unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
274d44252ecSDouglas Gregor         if (NumUnusedEdges && (--NumUnusedEdges == 0))
275d44252ecSDouglas Gregor           Queue.push_back(*M);
276d44252ecSDouglas Gregor       }
277d44252ecSDouglas Gregor     }
278e41d7feaSDouglas Gregor 
279e41d7feaSDouglas Gregor     assert(VisitOrder.size() == N && "Visitation order is wrong?");
2807211ac15SDouglas Gregor 
281e97cd90aSDouglas Gregor     delete FirstVisitState;
282e97cd90aSDouglas Gregor     FirstVisitState = 0;
283e41d7feaSDouglas Gregor   }
284e41d7feaSDouglas Gregor 
285e97cd90aSDouglas Gregor   VisitState *State = allocateVisitState();
286e97cd90aSDouglas Gregor   unsigned VisitNumber = State->NextVisitNumber++;
287e41d7feaSDouglas Gregor 
2887211ac15SDouglas Gregor   // If the caller has provided us with a hit-set that came from the global
2897211ac15SDouglas Gregor   // module index, mark every module file in common with the global module
2907211ac15SDouglas Gregor   // index that is *not* in that set as 'visited'.
2917211ac15SDouglas Gregor   if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
2927211ac15SDouglas Gregor     for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
2937211ac15SDouglas Gregor     {
2947211ac15SDouglas Gregor       ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
2957029ce1aSDouglas Gregor       if (!ModuleFilesHit->count(M))
296e97cd90aSDouglas Gregor         State->VisitNumber[M->Index] = VisitNumber;
2977211ac15SDouglas Gregor     }
2987211ac15SDouglas Gregor   }
2997211ac15SDouglas Gregor 
300e41d7feaSDouglas Gregor   for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
301e41d7feaSDouglas Gregor     ModuleFile *CurrentModule = VisitOrder[I];
302e41d7feaSDouglas Gregor     // Should we skip this module file?
303e97cd90aSDouglas Gregor     if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
304e41d7feaSDouglas Gregor       continue;
305e41d7feaSDouglas Gregor 
306e41d7feaSDouglas Gregor     // Visit the module.
307e97cd90aSDouglas Gregor     assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
308e97cd90aSDouglas Gregor     State->VisitNumber[CurrentModule->Index] = VisitNumber;
309e41d7feaSDouglas Gregor     if (!Visitor(*CurrentModule, UserData))
310e41d7feaSDouglas Gregor       continue;
311e41d7feaSDouglas Gregor 
312e41d7feaSDouglas Gregor     // The visitor has requested that cut off visitation of any
313e41d7feaSDouglas Gregor     // module that the current module depends on. To indicate this
314e41d7feaSDouglas Gregor     // behavior, we mark all of the reachable modules as having been visited.
315e41d7feaSDouglas Gregor     ModuleFile *NextModule = CurrentModule;
316e41d7feaSDouglas Gregor     do {
317e41d7feaSDouglas Gregor       // For any module that this module depends on, push it on the
318e41d7feaSDouglas Gregor       // stack (if it hasn't already been marked as visited).
319e41d7feaSDouglas Gregor       for (llvm::SetVector<ModuleFile *>::iterator
320e41d7feaSDouglas Gregor              M = NextModule->Imports.begin(),
321e41d7feaSDouglas Gregor              MEnd = NextModule->Imports.end();
322e41d7feaSDouglas Gregor            M != MEnd; ++M) {
323e97cd90aSDouglas Gregor         if (State->VisitNumber[(*M)->Index] != VisitNumber) {
324e97cd90aSDouglas Gregor           State->Stack.push_back(*M);
325e97cd90aSDouglas Gregor           State->VisitNumber[(*M)->Index] = VisitNumber;
326e41d7feaSDouglas Gregor         }
327e41d7feaSDouglas Gregor       }
328e41d7feaSDouglas Gregor 
329e97cd90aSDouglas Gregor       if (State->Stack.empty())
330e41d7feaSDouglas Gregor         break;
331e41d7feaSDouglas Gregor 
332e41d7feaSDouglas Gregor       // Pop the next module off the stack.
33325284cc9SRobert Wilhelm       NextModule = State->Stack.pop_back_val();
334e41d7feaSDouglas Gregor     } while (true);
335e41d7feaSDouglas Gregor   }
336e97cd90aSDouglas Gregor 
337e97cd90aSDouglas Gregor   returnVisitState(State);
338d44252ecSDouglas Gregor }
339d44252ecSDouglas Gregor 
340d44252ecSDouglas Gregor /// \brief Perform a depth-first visit of the current module.
341de3ef502SDouglas Gregor static bool visitDepthFirst(ModuleFile &M,
342de3ef502SDouglas Gregor                             bool (*Visitor)(ModuleFile &M, bool Preorder,
343d44252ecSDouglas Gregor                                             void *UserData),
344d44252ecSDouglas Gregor                             void *UserData,
345bdb259d2SDouglas Gregor                             SmallVectorImpl<bool> &Visited) {
346d44252ecSDouglas Gregor   // Preorder visitation
347d44252ecSDouglas Gregor   if (Visitor(M, /*Preorder=*/true, UserData))
348d44252ecSDouglas Gregor     return true;
349d44252ecSDouglas Gregor 
350d44252ecSDouglas Gregor   // Visit children
351de3ef502SDouglas Gregor   for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
352d44252ecSDouglas Gregor                                             IMEnd = M.Imports.end();
353d44252ecSDouglas Gregor        IM != IMEnd; ++IM) {
354bdb259d2SDouglas Gregor     if (Visited[(*IM)->Index])
355d44252ecSDouglas Gregor       continue;
356bdb259d2SDouglas Gregor     Visited[(*IM)->Index] = true;
357d44252ecSDouglas Gregor 
358d44252ecSDouglas Gregor     if (visitDepthFirst(**IM, Visitor, UserData, Visited))
359d44252ecSDouglas Gregor       return true;
360d44252ecSDouglas Gregor   }
361d44252ecSDouglas Gregor 
362d44252ecSDouglas Gregor   // Postorder visitation
363d44252ecSDouglas Gregor   return Visitor(M, /*Preorder=*/false, UserData);
364d44252ecSDouglas Gregor }
365d44252ecSDouglas Gregor 
366de3ef502SDouglas Gregor void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
367d44252ecSDouglas Gregor                                                     void *UserData),
368d44252ecSDouglas Gregor                                     void *UserData) {
369bdb259d2SDouglas Gregor   SmallVector<bool, 16> Visited(size(), false);
370d44252ecSDouglas Gregor   for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
371bdb259d2SDouglas Gregor     if (Visited[Chain[I]->Index])
372d44252ecSDouglas Gregor       continue;
373bdb259d2SDouglas Gregor     Visited[Chain[I]->Index] = true;
374d44252ecSDouglas Gregor 
375d44252ecSDouglas Gregor     if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
376d44252ecSDouglas Gregor       return;
377d44252ecSDouglas Gregor   }
378d44252ecSDouglas Gregor }
3799d7c1a2aSDouglas Gregor 
3807029ce1aSDouglas Gregor bool ModuleManager::lookupModuleFile(StringRef FileName,
3817029ce1aSDouglas Gregor                                      off_t ExpectedSize,
3827029ce1aSDouglas Gregor                                      time_t ExpectedModTime,
3837029ce1aSDouglas Gregor                                      const FileEntry *&File) {
3847029ce1aSDouglas Gregor   File = FileMgr.getFile(FileName, /*openFile=*/false, /*cacheFailure=*/false);
3857029ce1aSDouglas Gregor 
3867029ce1aSDouglas Gregor   if (!File && FileName != "-") {
3877029ce1aSDouglas Gregor     return false;
3887029ce1aSDouglas Gregor   }
3897029ce1aSDouglas Gregor 
3907029ce1aSDouglas Gregor   if ((ExpectedSize && ExpectedSize != File->getSize()) ||
3917029ce1aSDouglas Gregor       (ExpectedModTime && ExpectedModTime != File->getModificationTime())) {
3927029ce1aSDouglas Gregor     return true;
3937029ce1aSDouglas Gregor   }
3947029ce1aSDouglas Gregor 
3957029ce1aSDouglas Gregor   return false;
3967029ce1aSDouglas Gregor }
3977029ce1aSDouglas Gregor 
3989d7c1a2aSDouglas Gregor #ifndef NDEBUG
3999d7c1a2aSDouglas Gregor namespace llvm {
4009d7c1a2aSDouglas Gregor   template<>
4019d7c1a2aSDouglas Gregor   struct GraphTraits<ModuleManager> {
402de3ef502SDouglas Gregor     typedef ModuleFile NodeType;
403de3ef502SDouglas Gregor     typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
4049d7c1a2aSDouglas Gregor     typedef ModuleManager::ModuleConstIterator nodes_iterator;
4059d7c1a2aSDouglas Gregor 
4069d7c1a2aSDouglas Gregor     static ChildIteratorType child_begin(NodeType *Node) {
4079d7c1a2aSDouglas Gregor       return Node->Imports.begin();
4089d7c1a2aSDouglas Gregor     }
4099d7c1a2aSDouglas Gregor 
4109d7c1a2aSDouglas Gregor     static ChildIteratorType child_end(NodeType *Node) {
4119d7c1a2aSDouglas Gregor       return Node->Imports.end();
4129d7c1a2aSDouglas Gregor     }
4139d7c1a2aSDouglas Gregor 
4149d7c1a2aSDouglas Gregor     static nodes_iterator nodes_begin(const ModuleManager &Manager) {
4159d7c1a2aSDouglas Gregor       return Manager.begin();
4169d7c1a2aSDouglas Gregor     }
4179d7c1a2aSDouglas Gregor 
4189d7c1a2aSDouglas Gregor     static nodes_iterator nodes_end(const ModuleManager &Manager) {
4199d7c1a2aSDouglas Gregor       return Manager.end();
4209d7c1a2aSDouglas Gregor     }
4219d7c1a2aSDouglas Gregor   };
4229d7c1a2aSDouglas Gregor 
4239d7c1a2aSDouglas Gregor   template<>
4249d7c1a2aSDouglas Gregor   struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
4259d7c1a2aSDouglas Gregor     explicit DOTGraphTraits(bool IsSimple = false)
4269d7c1a2aSDouglas Gregor       : DefaultDOTGraphTraits(IsSimple) { }
4279d7c1a2aSDouglas Gregor 
4289d7c1a2aSDouglas Gregor     static bool renderGraphFromBottomUp() {
4299d7c1a2aSDouglas Gregor       return true;
4309d7c1a2aSDouglas Gregor     }
4319d7c1a2aSDouglas Gregor 
432de3ef502SDouglas Gregor     std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
433*beee15e7SBen Langmuir       return M->ModuleName;
4349d7c1a2aSDouglas Gregor     }
4359d7c1a2aSDouglas Gregor   };
4369d7c1a2aSDouglas Gregor }
4379d7c1a2aSDouglas Gregor 
4389d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() {
4399d7c1a2aSDouglas Gregor   llvm::ViewGraph(*this, "Modules");
4409d7c1a2aSDouglas Gregor }
4419d7c1a2aSDouglas Gregor #endif
442