blob: 18fe035456d44eb90b3f51454bb4bd8be68d4e0f [file] [log] [blame]
Douglas Gregord44252e2011-08-25 20:47:51 +00001//===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ModuleManager class, which manages a set of loaded
11// modules for the ASTReader.
12//
13//===----------------------------------------------------------------------===//
Ben Langmuirbeee15e2014-04-14 18:00:01 +000014#include "clang/Lex/HeaderSearch.h"
Douglas Gregor7029ce12013-03-19 00:28:20 +000015#include "clang/Lex/ModuleMap.h"
Douglas Gregor7211ac12013-01-25 23:32:03 +000016#include "clang/Serialization/GlobalModuleIndex.h"
Benjamin Krameraf04f982013-08-24 13:16:22 +000017#include "clang/Serialization/ModuleManager.h"
Douglas Gregord44252e2011-08-25 20:47:51 +000018#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola552c1692013-06-11 22:15:02 +000019#include "llvm/Support/Path.h"
Douglas Gregord44252e2011-08-25 20:47:51 +000020#include "llvm/Support/raw_ostream.h"
Rafael Espindola8a8e5542014-06-12 17:19:42 +000021#include <system_error>
Douglas Gregord44252e2011-08-25 20:47:51 +000022
Douglas Gregor9d7c1a22011-10-11 19:27:55 +000023#ifndef NDEBUG
24#include "llvm/Support/GraphWriter.h"
25#endif
26
Douglas Gregord44252e2011-08-25 20:47:51 +000027using namespace clang;
28using namespace serialization;
29
Douglas Gregorde3ef502011-11-30 23:21:26 +000030ModuleFile *ModuleManager::lookup(StringRef Name) {
Douglas Gregordadd85d2013-02-08 21:27:45 +000031 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
32 /*cacheFailure=*/false);
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000033 if (Entry)
34 return lookup(Entry);
35
Craig Toppera13603a2014-05-22 05:54:18 +000036 return nullptr;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000037}
38
39ModuleFile *ModuleManager::lookup(const FileEntry *File) {
40 llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known
41 = Modules.find(File);
42 if (Known == Modules.end())
Craig Toppera13603a2014-05-22 05:54:18 +000043 return nullptr;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000044
45 return Known->second;
Douglas Gregord44252e2011-08-25 20:47:51 +000046}
47
Rafael Espindola5cd06f22014-08-18 19:16:31 +000048std::unique_ptr<llvm::MemoryBuffer>
49ModuleManager::lookupBuffer(StringRef Name) {
Douglas Gregordadd85d2013-02-08 21:27:45 +000050 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
51 /*cacheFailure=*/false);
Rafael Espindola5cd06f22014-08-18 19:16:31 +000052 return std::move(InMemoryBuffers[Entry]);
Douglas Gregord44252e2011-08-25 20:47:51 +000053}
54
Douglas Gregor7029ce12013-03-19 00:28:20 +000055ModuleManager::AddModuleResult
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000056ModuleManager::addModule(StringRef FileName, ModuleKind Type,
57 SourceLocation ImportLoc, ModuleFile *ImportedBy,
Douglas Gregor7029ce12013-03-19 00:28:20 +000058 unsigned Generation,
59 off_t ExpectedSize, time_t ExpectedModTime,
60 ModuleFile *&Module,
61 std::string &ErrorStr) {
Craig Toppera13603a2014-05-22 05:54:18 +000062 Module = nullptr;
Douglas Gregor7029ce12013-03-19 00:28:20 +000063
64 // Look for the file entry. This only fails if the expected size or
65 // modification time differ.
66 const FileEntry *Entry;
Eli Friedmanc27d0d52013-09-05 23:50:58 +000067 if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
68 ErrorStr = "module file out of date";
Douglas Gregor7029ce12013-03-19 00:28:20 +000069 return OutOfDate;
Eli Friedmanc27d0d52013-09-05 23:50:58 +000070 }
Douglas Gregor7029ce12013-03-19 00:28:20 +000071
Douglas Gregord44252e2011-08-25 20:47:51 +000072 if (!Entry && FileName != "-") {
Eli Friedmanc27d0d52013-09-05 23:50:58 +000073 ErrorStr = "module file not found";
Douglas Gregor7029ce12013-03-19 00:28:20 +000074 return Missing;
Douglas Gregord44252e2011-08-25 20:47:51 +000075 }
Douglas Gregor7029ce12013-03-19 00:28:20 +000076
77 // Check whether we already loaded this module, before
Douglas Gregorde3ef502011-11-30 23:21:26 +000078 ModuleFile *&ModuleEntry = Modules[Entry];
Douglas Gregord44252e2011-08-25 20:47:51 +000079 bool NewModule = false;
80 if (!ModuleEntry) {
81 // Allocate a new module.
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +000082 ModuleFile *New = new ModuleFile(Type, Generation);
Douglas Gregorbdb259d2013-01-21 20:07:12 +000083 New->Index = Chain.size();
Douglas Gregord44252e2011-08-25 20:47:51 +000084 New->FileName = FileName.str();
Argyrios Kyrtzidisaedf7142012-10-03 01:58:42 +000085 New->File = Entry;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000086 New->ImportLoc = ImportLoc;
Douglas Gregord44252e2011-08-25 20:47:51 +000087 Chain.push_back(New);
88 NewModule = true;
89 ModuleEntry = New;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000090
Dmitri Gribenkof430da42014-02-12 10:33:14 +000091 New->InputFilesValidationTimestamp = 0;
92 if (New->Kind == MK_Module) {
93 std::string TimestampFilename = New->getTimestampFilename();
Ben Langmuirc8130a72014-02-20 21:59:23 +000094 vfs::Status Status;
Dmitri Gribenkof430da42014-02-12 10:33:14 +000095 // A cached stat value would be fine as well.
96 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
97 New->InputFilesValidationTimestamp =
98 Status.getLastModificationTime().toEpochTime();
99 }
100
Douglas Gregord44252e2011-08-25 20:47:51 +0000101 // Load the contents of the module
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000102 if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000103 // The buffer was already provided for us.
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000104 New->Buffer = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000105 } else {
106 // Open the AST file.
Rafael Espindolac0809172014-06-12 14:02:15 +0000107 std::error_code ec;
Douglas Gregord44252e2011-08-25 20:47:51 +0000108 if (FileName == "-") {
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000109 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf =
110 llvm::MemoryBuffer::getSTDIN();
111 ec = Buf.getError();
Douglas Gregord44252e2011-08-25 20:47:51 +0000112 if (ec)
113 ErrorStr = ec.message();
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000114 else
115 New->Buffer = std::move(Buf.get());
Ben Langmuir9801b252014-06-20 00:24:56 +0000116 } else {
117 // Leave the FileEntry open so if it gets read again by another
118 // ModuleManager it must be the same underlying file.
119 // FIXME: Because FileManager::getFile() doesn't guarantee that it will
120 // give us an open file, this may not be 100% reliable.
Rafael Espindola6406f7b2014-08-26 19:54:40 +0000121 New->Buffer = FileMgr.getBufferForFile(New->File, &ErrorStr,
122 /*IsVolatile*/ false,
123 /*ShouldClose*/ false);
Ben Langmuir9801b252014-06-20 00:24:56 +0000124 }
Douglas Gregord44252e2011-08-25 20:47:51 +0000125
126 if (!New->Buffer)
Douglas Gregor7029ce12013-03-19 00:28:20 +0000127 return Missing;
Douglas Gregord44252e2011-08-25 20:47:51 +0000128 }
129
130 // Initialize the stream
131 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
Douglas Gregor7029ce12013-03-19 00:28:20 +0000132 (const unsigned char *)New->Buffer->getBufferEnd());
Douglas Gregor7029ce12013-03-19 00:28:20 +0000133 }
Douglas Gregord44252e2011-08-25 20:47:51 +0000134
135 if (ImportedBy) {
136 ModuleEntry->ImportedBy.insert(ImportedBy);
137 ImportedBy->Imports.insert(ModuleEntry);
138 } else {
Douglas Gregor6fb03ae2012-11-30 19:28:05 +0000139 if (!ModuleEntry->DirectlyImported)
140 ModuleEntry->ImportLoc = ImportLoc;
141
Douglas Gregord44252e2011-08-25 20:47:51 +0000142 ModuleEntry->DirectlyImported = true;
143 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000144
145 Module = ModuleEntry;
146 return NewModule? NewlyLoaded : AlreadyLoaded;
Douglas Gregord44252e2011-08-25 20:47:51 +0000147}
148
Ben Langmuir9801b252014-06-20 00:24:56 +0000149void ModuleManager::removeModules(
150 ModuleIterator first, ModuleIterator last,
151 llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
152 ModuleMap *modMap) {
Douglas Gregor188dbef2012-11-07 17:46:15 +0000153 if (first == last)
154 return;
155
156 // Collect the set of module file pointers that we'll be removing.
157 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
158
159 // Remove any references to the now-destroyed modules.
Douglas Gregor188dbef2012-11-07 17:46:15 +0000160 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
Chandler Carruthb55d0222014-03-03 19:36:27 +0000161 Chain[i]->ImportedBy.remove_if([&](ModuleFile *MF) {
162 return victimSet.count(MF);
163 });
Douglas Gregor188dbef2012-11-07 17:46:15 +0000164 }
165
166 // Delete the modules and erase them from the various structures.
167 for (ModuleIterator victim = first; victim != last; ++victim) {
Ben Langmuircaea1312014-05-19 17:04:28 +0000168 Modules.erase((*victim)->File);
Ben Langmuirca392142014-05-19 16:13:45 +0000169
Douglas Gregor7029ce12013-03-19 00:28:20 +0000170 if (modMap) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000171 StringRef ModuleName = (*victim)->ModuleName;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000172 if (Module *mod = modMap->findModule(ModuleName)) {
Craig Toppera13603a2014-05-22 05:54:18 +0000173 mod->setASTFile(nullptr);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000174 }
175 }
Ben Langmuir4f054782014-05-30 21:20:54 +0000176
Ben Langmuir9801b252014-06-20 00:24:56 +0000177 // Files that didn't make it through ReadASTCore successfully will be
178 // rebuilt (or there was an error). Invalidate them so that we can load the
179 // new files that will be renamed over the old ones.
180 if (LoadedSuccessfully.count(*victim) == 0)
Ben Langmuir4f054782014-05-30 21:20:54 +0000181 FileMgr.invalidateCache((*victim)->File);
182
Douglas Gregor188dbef2012-11-07 17:46:15 +0000183 delete *victim;
184 }
185
186 // Remove the modules from the chain.
187 Chain.erase(first, last);
188}
189
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000190void
191ModuleManager::addInMemoryBuffer(StringRef FileName,
192 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
193
194 const FileEntry *Entry =
195 FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
196 InMemoryBuffers[Entry] = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000197}
198
Douglas Gregore97cd902013-01-28 16:46:33 +0000199ModuleManager::VisitState *ModuleManager::allocateVisitState() {
200 // Fast path: if we have a cached state, use it.
201 if (FirstVisitState) {
202 VisitState *Result = FirstVisitState;
203 FirstVisitState = FirstVisitState->NextState;
Craig Toppera13603a2014-05-22 05:54:18 +0000204 Result->NextState = nullptr;
Douglas Gregore97cd902013-01-28 16:46:33 +0000205 return Result;
206 }
207
208 // Allocate and return a new state.
209 return new VisitState(size());
210}
211
212void ModuleManager::returnVisitState(VisitState *State) {
Craig Toppera13603a2014-05-22 05:54:18 +0000213 assert(State->NextState == nullptr && "Visited state is in list?");
Douglas Gregore97cd902013-01-28 16:46:33 +0000214 State->NextState = FirstVisitState;
215 FirstVisitState = State;
216}
217
Douglas Gregor7211ac12013-01-25 23:32:03 +0000218void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
219 GlobalIndex = Index;
Douglas Gregor603cd862013-03-22 18:50:14 +0000220 if (!GlobalIndex) {
221 ModulesInCommonWithGlobalIndex.clear();
222 return;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000223 }
Douglas Gregor603cd862013-03-22 18:50:14 +0000224
225 // Notify the global module index about all of the modules we've already
226 // loaded.
227 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
228 if (!GlobalIndex->loadedModuleFile(Chain[I])) {
229 ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
230 }
231 }
232}
233
234void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
235 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
236 return;
237
238 ModulesInCommonWithGlobalIndex.push_back(MF);
Douglas Gregor7211ac12013-01-25 23:32:03 +0000239}
240
241ModuleManager::ModuleManager(FileManager &FileMgr)
Craig Toppera13603a2014-05-22 05:54:18 +0000242 : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(nullptr) {}
Douglas Gregord44252e2011-08-25 20:47:51 +0000243
244ModuleManager::~ModuleManager() {
245 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
246 delete Chain[e - i - 1];
Douglas Gregore97cd902013-01-28 16:46:33 +0000247 delete FirstVisitState;
Douglas Gregord44252e2011-08-25 20:47:51 +0000248}
249
Douglas Gregor7211ac12013-01-25 23:32:03 +0000250void
251ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
252 void *UserData,
Craig Topper4dd9b432014-08-17 23:49:53 +0000253 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
Douglas Gregor7211ac12013-01-25 23:32:03 +0000254 // If the visitation order vector is the wrong size, recompute the order.
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000255 if (VisitOrder.size() != Chain.size()) {
256 unsigned N = size();
257 VisitOrder.clear();
258 VisitOrder.reserve(N);
259
260 // Record the number of incoming edges for each module. When we
261 // encounter a module with no incoming edges, push it into the queue
262 // to seed the queue.
263 SmallVector<ModuleFile *, 4> Queue;
264 Queue.reserve(N);
265 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
266 UnusedIncomingEdges.reserve(size());
267 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
268 if (unsigned Size = (*M)->ImportedBy.size())
269 UnusedIncomingEdges.push_back(Size);
270 else {
271 UnusedIncomingEdges.push_back(0);
272 Queue.push_back(*M);
273 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000274 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000275
276 // Traverse the graph, making sure to visit a module before visiting any
277 // of its dependencies.
278 unsigned QueueStart = 0;
279 while (QueueStart < Queue.size()) {
280 ModuleFile *CurrentModule = Queue[QueueStart++];
281 VisitOrder.push_back(CurrentModule);
282
283 // For any module that this module depends on, push it on the
284 // stack (if it hasn't already been marked as visited).
285 for (llvm::SetVector<ModuleFile *>::iterator
286 M = CurrentModule->Imports.begin(),
287 MEnd = CurrentModule->Imports.end();
288 M != MEnd; ++M) {
289 // Remove our current module as an impediment to visiting the
290 // module we depend on. If we were the last unvisited module
291 // that depends on this particular module, push it into the
292 // queue to be visited.
293 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
294 if (NumUnusedEdges && (--NumUnusedEdges == 0))
295 Queue.push_back(*M);
296 }
297 }
298
299 assert(VisitOrder.size() == N && "Visitation order is wrong?");
Douglas Gregor7211ac12013-01-25 23:32:03 +0000300
Douglas Gregore97cd902013-01-28 16:46:33 +0000301 delete FirstVisitState;
Craig Toppera13603a2014-05-22 05:54:18 +0000302 FirstVisitState = nullptr;
Douglas Gregord44252e2011-08-25 20:47:51 +0000303 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000304
Douglas Gregore97cd902013-01-28 16:46:33 +0000305 VisitState *State = allocateVisitState();
306 unsigned VisitNumber = State->NextVisitNumber++;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000307
Douglas Gregor7211ac12013-01-25 23:32:03 +0000308 // If the caller has provided us with a hit-set that came from the global
309 // module index, mark every module file in common with the global module
310 // index that is *not* in that set as 'visited'.
311 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
312 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
313 {
314 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
Douglas Gregor7029ce12013-03-19 00:28:20 +0000315 if (!ModuleFilesHit->count(M))
Douglas Gregore97cd902013-01-28 16:46:33 +0000316 State->VisitNumber[M->Index] = VisitNumber;
Douglas Gregor7211ac12013-01-25 23:32:03 +0000317 }
318 }
319
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000320 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
321 ModuleFile *CurrentModule = VisitOrder[I];
322 // Should we skip this module file?
Douglas Gregore97cd902013-01-28 16:46:33 +0000323 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
Douglas Gregord44252e2011-08-25 20:47:51 +0000324 continue;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000325
326 // Visit the module.
Douglas Gregore97cd902013-01-28 16:46:33 +0000327 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
328 State->VisitNumber[CurrentModule->Index] = VisitNumber;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000329 if (!Visitor(*CurrentModule, UserData))
330 continue;
331
332 // The visitor has requested that cut off visitation of any
333 // module that the current module depends on. To indicate this
334 // behavior, we mark all of the reachable modules as having been visited.
335 ModuleFile *NextModule = CurrentModule;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000336 do {
337 // For any module that this module depends on, push it on the
338 // stack (if it hasn't already been marked as visited).
339 for (llvm::SetVector<ModuleFile *>::iterator
340 M = NextModule->Imports.begin(),
341 MEnd = NextModule->Imports.end();
342 M != MEnd; ++M) {
Douglas Gregore97cd902013-01-28 16:46:33 +0000343 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
344 State->Stack.push_back(*M);
345 State->VisitNumber[(*M)->Index] = VisitNumber;
Douglas Gregord44252e2011-08-25 20:47:51 +0000346 }
347 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000348
Douglas Gregore97cd902013-01-28 16:46:33 +0000349 if (State->Stack.empty())
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000350 break;
351
352 // Pop the next module off the stack.
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000353 NextModule = State->Stack.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000354 } while (true);
Douglas Gregord44252e2011-08-25 20:47:51 +0000355 }
Douglas Gregore97cd902013-01-28 16:46:33 +0000356
357 returnVisitState(State);
Douglas Gregord44252e2011-08-25 20:47:51 +0000358}
359
360/// \brief Perform a depth-first visit of the current module.
Douglas Gregorde3ef502011-11-30 23:21:26 +0000361static bool visitDepthFirst(ModuleFile &M,
362 bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregord44252e2011-08-25 20:47:51 +0000363 void *UserData),
364 void *UserData,
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000365 SmallVectorImpl<bool> &Visited) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000366 // Preorder visitation
367 if (Visitor(M, /*Preorder=*/true, UserData))
368 return true;
369
370 // Visit children
Douglas Gregorde3ef502011-11-30 23:21:26 +0000371 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000372 IMEnd = M.Imports.end();
Douglas Gregord44252e2011-08-25 20:47:51 +0000373 IM != IMEnd; ++IM) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000374 if (Visited[(*IM)->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000375 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000376 Visited[(*IM)->Index] = true;
377
Douglas Gregord44252e2011-08-25 20:47:51 +0000378 if (visitDepthFirst(**IM, Visitor, UserData, Visited))
379 return true;
380 }
381
382 // Postorder visitation
383 return Visitor(M, /*Preorder=*/false, UserData);
384}
385
Douglas Gregorde3ef502011-11-30 23:21:26 +0000386void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregord44252e2011-08-25 20:47:51 +0000387 void *UserData),
388 void *UserData) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000389 SmallVector<bool, 16> Visited(size(), false);
Douglas Gregord44252e2011-08-25 20:47:51 +0000390 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000391 if (Visited[Chain[I]->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000392 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000393 Visited[Chain[I]->Index] = true;
394
Douglas Gregord44252e2011-08-25 20:47:51 +0000395 if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
396 return;
397 }
398}
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000399
Douglas Gregor7029ce12013-03-19 00:28:20 +0000400bool ModuleManager::lookupModuleFile(StringRef FileName,
401 off_t ExpectedSize,
402 time_t ExpectedModTime,
403 const FileEntry *&File) {
Ben Langmuir05f82ba2014-05-01 03:33:36 +0000404 // Open the file immediately to ensure there is no race between stat'ing and
405 // opening the file.
406 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000407
408 if (!File && FileName != "-") {
409 return false;
410 }
411
412 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
Ben Langmuir027731d2014-05-04 05:20:54 +0000413 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
414 // Do not destroy File, as it may be referenced. If we need to rebuild it,
415 // it will be destroyed by removeModules.
Douglas Gregor7029ce12013-03-19 00:28:20 +0000416 return true;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000417
418 return false;
419}
420
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000421#ifndef NDEBUG
422namespace llvm {
423 template<>
424 struct GraphTraits<ModuleManager> {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000425 typedef ModuleFile NodeType;
426 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000427 typedef ModuleManager::ModuleConstIterator nodes_iterator;
428
429 static ChildIteratorType child_begin(NodeType *Node) {
430 return Node->Imports.begin();
431 }
432
433 static ChildIteratorType child_end(NodeType *Node) {
434 return Node->Imports.end();
435 }
436
437 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
438 return Manager.begin();
439 }
440
441 static nodes_iterator nodes_end(const ModuleManager &Manager) {
442 return Manager.end();
443 }
444 };
445
446 template<>
447 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
448 explicit DOTGraphTraits(bool IsSimple = false)
449 : DefaultDOTGraphTraits(IsSimple) { }
450
451 static bool renderGraphFromBottomUp() {
452 return true;
453 }
454
Douglas Gregorde3ef502011-11-30 23:21:26 +0000455 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000456 return M->ModuleName;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000457 }
458 };
459}
460
461void ModuleManager::viewGraph() {
462 llvm::ViewGraph(*this, "Modules");
463}
464#endif