blob: 0a9ea69d695c3a339df82b58804b6c3887223c41 [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
48llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
Douglas Gregordadd85d2013-02-08 21:27:45 +000049 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
50 /*cacheFailure=*/false);
Douglas Gregord44252e2011-08-25 20:47:51 +000051 return InMemoryBuffers[Entry];
52}
53
Douglas Gregor7029ce12013-03-19 00:28:20 +000054ModuleManager::AddModuleResult
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000055ModuleManager::addModule(StringRef FileName, ModuleKind Type,
56 SourceLocation ImportLoc, ModuleFile *ImportedBy,
Douglas Gregor7029ce12013-03-19 00:28:20 +000057 unsigned Generation,
58 off_t ExpectedSize, time_t ExpectedModTime,
59 ModuleFile *&Module,
60 std::string &ErrorStr) {
Craig Toppera13603a2014-05-22 05:54:18 +000061 Module = nullptr;
Douglas Gregor7029ce12013-03-19 00:28:20 +000062
63 // Look for the file entry. This only fails if the expected size or
64 // modification time differ.
65 const FileEntry *Entry;
Eli Friedmanc27d0d52013-09-05 23:50:58 +000066 if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
67 ErrorStr = "module file out of date";
Douglas Gregor7029ce12013-03-19 00:28:20 +000068 return OutOfDate;
Eli Friedmanc27d0d52013-09-05 23:50:58 +000069 }
Douglas Gregor7029ce12013-03-19 00:28:20 +000070
Douglas Gregord44252e2011-08-25 20:47:51 +000071 if (!Entry && FileName != "-") {
Eli Friedmanc27d0d52013-09-05 23:50:58 +000072 ErrorStr = "module file not found";
Douglas Gregor7029ce12013-03-19 00:28:20 +000073 return Missing;
Douglas Gregord44252e2011-08-25 20:47:51 +000074 }
Douglas Gregor7029ce12013-03-19 00:28:20 +000075
76 // Check whether we already loaded this module, before
Douglas Gregorde3ef502011-11-30 23:21:26 +000077 ModuleFile *&ModuleEntry = Modules[Entry];
Douglas Gregord44252e2011-08-25 20:47:51 +000078 bool NewModule = false;
79 if (!ModuleEntry) {
80 // Allocate a new module.
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +000081 ModuleFile *New = new ModuleFile(Type, Generation);
Douglas Gregorbdb259d2013-01-21 20:07:12 +000082 New->Index = Chain.size();
Douglas Gregord44252e2011-08-25 20:47:51 +000083 New->FileName = FileName.str();
Argyrios Kyrtzidisaedf7142012-10-03 01:58:42 +000084 New->File = Entry;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000085 New->ImportLoc = ImportLoc;
Douglas Gregord44252e2011-08-25 20:47:51 +000086 Chain.push_back(New);
87 NewModule = true;
88 ModuleEntry = New;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000089
Dmitri Gribenkof430da42014-02-12 10:33:14 +000090 New->InputFilesValidationTimestamp = 0;
91 if (New->Kind == MK_Module) {
92 std::string TimestampFilename = New->getTimestampFilename();
Ben Langmuirc8130a72014-02-20 21:59:23 +000093 vfs::Status Status;
Dmitri Gribenkof430da42014-02-12 10:33:14 +000094 // A cached stat value would be fine as well.
95 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
96 New->InputFilesValidationTimestamp =
97 Status.getLastModificationTime().toEpochTime();
98 }
99
Douglas Gregord44252e2011-08-25 20:47:51 +0000100 // Load the contents of the module
101 if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) {
102 // The buffer was already provided for us.
103 assert(Buffer && "Passed null buffer");
104 New->Buffer.reset(Buffer);
105 } 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 == "-") {
109 ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
110 if (ec)
111 ErrorStr = ec.message();
Ben Langmuir9801b252014-06-20 00:24:56 +0000112 } else {
113 // Leave the FileEntry open so if it gets read again by another
114 // ModuleManager it must be the same underlying file.
115 // FIXME: Because FileManager::getFile() doesn't guarantee that it will
116 // give us an open file, this may not be 100% reliable.
117 New->Buffer.reset(FileMgr.getBufferForFile(New->File, &ErrorStr,
118 /*IsVolatile*/false,
119 /*ShouldClose*/false));
120 }
Douglas Gregord44252e2011-08-25 20:47:51 +0000121
122 if (!New->Buffer)
Douglas Gregor7029ce12013-03-19 00:28:20 +0000123 return Missing;
Douglas Gregord44252e2011-08-25 20:47:51 +0000124 }
125
126 // Initialize the stream
127 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
Douglas Gregor7029ce12013-03-19 00:28:20 +0000128 (const unsigned char *)New->Buffer->getBufferEnd());
Douglas Gregor7029ce12013-03-19 00:28:20 +0000129 }
Douglas Gregord44252e2011-08-25 20:47:51 +0000130
131 if (ImportedBy) {
132 ModuleEntry->ImportedBy.insert(ImportedBy);
133 ImportedBy->Imports.insert(ModuleEntry);
134 } else {
Douglas Gregor6fb03ae2012-11-30 19:28:05 +0000135 if (!ModuleEntry->DirectlyImported)
136 ModuleEntry->ImportLoc = ImportLoc;
137
Douglas Gregord44252e2011-08-25 20:47:51 +0000138 ModuleEntry->DirectlyImported = true;
139 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000140
141 Module = ModuleEntry;
142 return NewModule? NewlyLoaded : AlreadyLoaded;
Douglas Gregord44252e2011-08-25 20:47:51 +0000143}
144
Ben Langmuir9801b252014-06-20 00:24:56 +0000145void ModuleManager::removeModules(
146 ModuleIterator first, ModuleIterator last,
147 llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
148 ModuleMap *modMap) {
Douglas Gregor188dbef2012-11-07 17:46:15 +0000149 if (first == last)
150 return;
151
152 // Collect the set of module file pointers that we'll be removing.
153 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
154
155 // Remove any references to the now-destroyed modules.
Douglas Gregor188dbef2012-11-07 17:46:15 +0000156 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
Chandler Carruthb55d0222014-03-03 19:36:27 +0000157 Chain[i]->ImportedBy.remove_if([&](ModuleFile *MF) {
158 return victimSet.count(MF);
159 });
Douglas Gregor188dbef2012-11-07 17:46:15 +0000160 }
161
162 // Delete the modules and erase them from the various structures.
163 for (ModuleIterator victim = first; victim != last; ++victim) {
Ben Langmuircaea1312014-05-19 17:04:28 +0000164 Modules.erase((*victim)->File);
Ben Langmuirca392142014-05-19 16:13:45 +0000165
Douglas Gregor7029ce12013-03-19 00:28:20 +0000166 if (modMap) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000167 StringRef ModuleName = (*victim)->ModuleName;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000168 if (Module *mod = modMap->findModule(ModuleName)) {
Craig Toppera13603a2014-05-22 05:54:18 +0000169 mod->setASTFile(nullptr);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000170 }
171 }
Ben Langmuir4f054782014-05-30 21:20:54 +0000172
Ben Langmuir9801b252014-06-20 00:24:56 +0000173 // Files that didn't make it through ReadASTCore successfully will be
174 // rebuilt (or there was an error). Invalidate them so that we can load the
175 // new files that will be renamed over the old ones.
176 if (LoadedSuccessfully.count(*victim) == 0)
Ben Langmuir4f054782014-05-30 21:20:54 +0000177 FileMgr.invalidateCache((*victim)->File);
178
Douglas Gregor188dbef2012-11-07 17:46:15 +0000179 delete *victim;
180 }
181
182 // Remove the modules from the chain.
183 Chain.erase(first, last);
184}
185
Douglas Gregord44252e2011-08-25 20:47:51 +0000186void ModuleManager::addInMemoryBuffer(StringRef FileName,
187 llvm::MemoryBuffer *Buffer) {
188
189 const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
190 Buffer->getBufferSize(), 0);
191 InMemoryBuffers[Entry] = Buffer;
192}
193
Douglas Gregore97cd902013-01-28 16:46:33 +0000194ModuleManager::VisitState *ModuleManager::allocateVisitState() {
195 // Fast path: if we have a cached state, use it.
196 if (FirstVisitState) {
197 VisitState *Result = FirstVisitState;
198 FirstVisitState = FirstVisitState->NextState;
Craig Toppera13603a2014-05-22 05:54:18 +0000199 Result->NextState = nullptr;
Douglas Gregore97cd902013-01-28 16:46:33 +0000200 return Result;
201 }
202
203 // Allocate and return a new state.
204 return new VisitState(size());
205}
206
207void ModuleManager::returnVisitState(VisitState *State) {
Craig Toppera13603a2014-05-22 05:54:18 +0000208 assert(State->NextState == nullptr && "Visited state is in list?");
Douglas Gregore97cd902013-01-28 16:46:33 +0000209 State->NextState = FirstVisitState;
210 FirstVisitState = State;
211}
212
Douglas Gregor7211ac12013-01-25 23:32:03 +0000213void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
214 GlobalIndex = Index;
Douglas Gregor603cd862013-03-22 18:50:14 +0000215 if (!GlobalIndex) {
216 ModulesInCommonWithGlobalIndex.clear();
217 return;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000218 }
Douglas Gregor603cd862013-03-22 18:50:14 +0000219
220 // Notify the global module index about all of the modules we've already
221 // loaded.
222 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
223 if (!GlobalIndex->loadedModuleFile(Chain[I])) {
224 ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
225 }
226 }
227}
228
229void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
230 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
231 return;
232
233 ModulesInCommonWithGlobalIndex.push_back(MF);
Douglas Gregor7211ac12013-01-25 23:32:03 +0000234}
235
236ModuleManager::ModuleManager(FileManager &FileMgr)
Craig Toppera13603a2014-05-22 05:54:18 +0000237 : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(nullptr) {}
Douglas Gregord44252e2011-08-25 20:47:51 +0000238
239ModuleManager::~ModuleManager() {
240 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
241 delete Chain[e - i - 1];
Douglas Gregore97cd902013-01-28 16:46:33 +0000242 delete FirstVisitState;
Douglas Gregord44252e2011-08-25 20:47:51 +0000243}
244
Douglas Gregor7211ac12013-01-25 23:32:03 +0000245void
246ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
247 void *UserData,
Douglas Gregor7029ce12013-03-19 00:28:20 +0000248 llvm::SmallPtrSet<ModuleFile *, 4> *ModuleFilesHit) {
Douglas Gregor7211ac12013-01-25 23:32:03 +0000249 // If the visitation order vector is the wrong size, recompute the order.
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000250 if (VisitOrder.size() != Chain.size()) {
251 unsigned N = size();
252 VisitOrder.clear();
253 VisitOrder.reserve(N);
254
255 // Record the number of incoming edges for each module. When we
256 // encounter a module with no incoming edges, push it into the queue
257 // to seed the queue.
258 SmallVector<ModuleFile *, 4> Queue;
259 Queue.reserve(N);
260 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
261 UnusedIncomingEdges.reserve(size());
262 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
263 if (unsigned Size = (*M)->ImportedBy.size())
264 UnusedIncomingEdges.push_back(Size);
265 else {
266 UnusedIncomingEdges.push_back(0);
267 Queue.push_back(*M);
268 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000269 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000270
271 // Traverse the graph, making sure to visit a module before visiting any
272 // of its dependencies.
273 unsigned QueueStart = 0;
274 while (QueueStart < Queue.size()) {
275 ModuleFile *CurrentModule = Queue[QueueStart++];
276 VisitOrder.push_back(CurrentModule);
277
278 // For any module that this module depends on, push it on the
279 // stack (if it hasn't already been marked as visited).
280 for (llvm::SetVector<ModuleFile *>::iterator
281 M = CurrentModule->Imports.begin(),
282 MEnd = CurrentModule->Imports.end();
283 M != MEnd; ++M) {
284 // Remove our current module as an impediment to visiting the
285 // module we depend on. If we were the last unvisited module
286 // that depends on this particular module, push it into the
287 // queue to be visited.
288 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
289 if (NumUnusedEdges && (--NumUnusedEdges == 0))
290 Queue.push_back(*M);
291 }
292 }
293
294 assert(VisitOrder.size() == N && "Visitation order is wrong?");
Douglas Gregor7211ac12013-01-25 23:32:03 +0000295
Douglas Gregore97cd902013-01-28 16:46:33 +0000296 delete FirstVisitState;
Craig Toppera13603a2014-05-22 05:54:18 +0000297 FirstVisitState = nullptr;
Douglas Gregord44252e2011-08-25 20:47:51 +0000298 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000299
Douglas Gregore97cd902013-01-28 16:46:33 +0000300 VisitState *State = allocateVisitState();
301 unsigned VisitNumber = State->NextVisitNumber++;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000302
Douglas Gregor7211ac12013-01-25 23:32:03 +0000303 // If the caller has provided us with a hit-set that came from the global
304 // module index, mark every module file in common with the global module
305 // index that is *not* in that set as 'visited'.
306 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
307 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
308 {
309 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
Douglas Gregor7029ce12013-03-19 00:28:20 +0000310 if (!ModuleFilesHit->count(M))
Douglas Gregore97cd902013-01-28 16:46:33 +0000311 State->VisitNumber[M->Index] = VisitNumber;
Douglas Gregor7211ac12013-01-25 23:32:03 +0000312 }
313 }
314
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000315 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
316 ModuleFile *CurrentModule = VisitOrder[I];
317 // Should we skip this module file?
Douglas Gregore97cd902013-01-28 16:46:33 +0000318 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
Douglas Gregord44252e2011-08-25 20:47:51 +0000319 continue;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000320
321 // Visit the module.
Douglas Gregore97cd902013-01-28 16:46:33 +0000322 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
323 State->VisitNumber[CurrentModule->Index] = VisitNumber;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000324 if (!Visitor(*CurrentModule, UserData))
325 continue;
326
327 // The visitor has requested that cut off visitation of any
328 // module that the current module depends on. To indicate this
329 // behavior, we mark all of the reachable modules as having been visited.
330 ModuleFile *NextModule = CurrentModule;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000331 do {
332 // For any module that this module depends on, push it on the
333 // stack (if it hasn't already been marked as visited).
334 for (llvm::SetVector<ModuleFile *>::iterator
335 M = NextModule->Imports.begin(),
336 MEnd = NextModule->Imports.end();
337 M != MEnd; ++M) {
Douglas Gregore97cd902013-01-28 16:46:33 +0000338 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
339 State->Stack.push_back(*M);
340 State->VisitNumber[(*M)->Index] = VisitNumber;
Douglas Gregord44252e2011-08-25 20:47:51 +0000341 }
342 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000343
Douglas Gregore97cd902013-01-28 16:46:33 +0000344 if (State->Stack.empty())
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000345 break;
346
347 // Pop the next module off the stack.
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000348 NextModule = State->Stack.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000349 } while (true);
Douglas Gregord44252e2011-08-25 20:47:51 +0000350 }
Douglas Gregore97cd902013-01-28 16:46:33 +0000351
352 returnVisitState(State);
Douglas Gregord44252e2011-08-25 20:47:51 +0000353}
354
355/// \brief Perform a depth-first visit of the current module.
Douglas Gregorde3ef502011-11-30 23:21:26 +0000356static bool visitDepthFirst(ModuleFile &M,
357 bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregord44252e2011-08-25 20:47:51 +0000358 void *UserData),
359 void *UserData,
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000360 SmallVectorImpl<bool> &Visited) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000361 // Preorder visitation
362 if (Visitor(M, /*Preorder=*/true, UserData))
363 return true;
364
365 // Visit children
Douglas Gregorde3ef502011-11-30 23:21:26 +0000366 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000367 IMEnd = M.Imports.end();
Douglas Gregord44252e2011-08-25 20:47:51 +0000368 IM != IMEnd; ++IM) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000369 if (Visited[(*IM)->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000370 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000371 Visited[(*IM)->Index] = true;
372
Douglas Gregord44252e2011-08-25 20:47:51 +0000373 if (visitDepthFirst(**IM, Visitor, UserData, Visited))
374 return true;
375 }
376
377 // Postorder visitation
378 return Visitor(M, /*Preorder=*/false, UserData);
379}
380
Douglas Gregorde3ef502011-11-30 23:21:26 +0000381void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregord44252e2011-08-25 20:47:51 +0000382 void *UserData),
383 void *UserData) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000384 SmallVector<bool, 16> Visited(size(), false);
Douglas Gregord44252e2011-08-25 20:47:51 +0000385 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000386 if (Visited[Chain[I]->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000387 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000388 Visited[Chain[I]->Index] = true;
389
Douglas Gregord44252e2011-08-25 20:47:51 +0000390 if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
391 return;
392 }
393}
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000394
Douglas Gregor7029ce12013-03-19 00:28:20 +0000395bool ModuleManager::lookupModuleFile(StringRef FileName,
396 off_t ExpectedSize,
397 time_t ExpectedModTime,
398 const FileEntry *&File) {
Ben Langmuir05f82ba2014-05-01 03:33:36 +0000399 // Open the file immediately to ensure there is no race between stat'ing and
400 // opening the file.
401 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000402
403 if (!File && FileName != "-") {
404 return false;
405 }
406
407 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
Ben Langmuir027731d2014-05-04 05:20:54 +0000408 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
409 // Do not destroy File, as it may be referenced. If we need to rebuild it,
410 // it will be destroyed by removeModules.
Douglas Gregor7029ce12013-03-19 00:28:20 +0000411 return true;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000412
413 return false;
414}
415
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000416#ifndef NDEBUG
417namespace llvm {
418 template<>
419 struct GraphTraits<ModuleManager> {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000420 typedef ModuleFile NodeType;
421 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000422 typedef ModuleManager::ModuleConstIterator nodes_iterator;
423
424 static ChildIteratorType child_begin(NodeType *Node) {
425 return Node->Imports.begin();
426 }
427
428 static ChildIteratorType child_end(NodeType *Node) {
429 return Node->Imports.end();
430 }
431
432 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
433 return Manager.begin();
434 }
435
436 static nodes_iterator nodes_end(const ModuleManager &Manager) {
437 return Manager.end();
438 }
439 };
440
441 template<>
442 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
443 explicit DOTGraphTraits(bool IsSimple = false)
444 : DefaultDOTGraphTraits(IsSimple) { }
445
446 static bool renderGraphFromBottomUp() {
447 return true;
448 }
449
Douglas Gregorde3ef502011-11-30 23:21:26 +0000450 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000451 return M->ModuleName;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000452 }
453 };
454}
455
456void ModuleManager::viewGraph() {
457 llvm::ViewGraph(*this, "Modules");
458}
459#endif