blob: 74f75a103f7a7edc9e47713a1aa8f4f7a10584c7 [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//===----------------------------------------------------------------------===//
Adrian Prantlbb165fb2015-06-20 18:53:08 +000014#include "clang/Frontend/PCHContainerOperations.h"
Ben Langmuirbeee15e2014-04-14 18:00:01 +000015#include "clang/Lex/HeaderSearch.h"
Douglas Gregor7029ce12013-03-19 00:28:20 +000016#include "clang/Lex/ModuleMap.h"
Douglas Gregor7211ac12013-01-25 23:32:03 +000017#include "clang/Serialization/GlobalModuleIndex.h"
Benjamin Krameraf04f982013-08-24 13:16:22 +000018#include "clang/Serialization/ModuleManager.h"
Douglas Gregord44252e2011-08-25 20:47:51 +000019#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola552c1692013-06-11 22:15:02 +000020#include "llvm/Support/Path.h"
Douglas Gregord44252e2011-08-25 20:47:51 +000021#include "llvm/Support/raw_ostream.h"
Rafael Espindola8a8e5542014-06-12 17:19:42 +000022#include <system_error>
Douglas Gregord44252e2011-08-25 20:47:51 +000023
Douglas Gregor9d7c1a22011-10-11 19:27:55 +000024#ifndef NDEBUG
25#include "llvm/Support/GraphWriter.h"
26#endif
27
Douglas Gregord44252e2011-08-25 20:47:51 +000028using namespace clang;
29using namespace serialization;
30
Douglas Gregorde3ef502011-11-30 23:21:26 +000031ModuleFile *ModuleManager::lookup(StringRef Name) {
Douglas Gregordadd85d2013-02-08 21:27:45 +000032 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
33 /*cacheFailure=*/false);
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000034 if (Entry)
35 return lookup(Entry);
36
Craig Toppera13603a2014-05-22 05:54:18 +000037 return nullptr;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000038}
39
40ModuleFile *ModuleManager::lookup(const FileEntry *File) {
41 llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known
42 = Modules.find(File);
43 if (Known == Modules.end())
Craig Toppera13603a2014-05-22 05:54:18 +000044 return nullptr;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000045
46 return Known->second;
Douglas Gregord44252e2011-08-25 20:47:51 +000047}
48
Rafael Espindola5cd06f22014-08-18 19:16:31 +000049std::unique_ptr<llvm::MemoryBuffer>
50ModuleManager::lookupBuffer(StringRef Name) {
Douglas Gregordadd85d2013-02-08 21:27:45 +000051 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
52 /*cacheFailure=*/false);
Rafael Espindola5cd06f22014-08-18 19:16:31 +000053 return std::move(InMemoryBuffers[Entry]);
Douglas Gregord44252e2011-08-25 20:47:51 +000054}
55
Douglas Gregor7029ce12013-03-19 00:28:20 +000056ModuleManager::AddModuleResult
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000057ModuleManager::addModule(StringRef FileName, ModuleKind Type,
58 SourceLocation ImportLoc, ModuleFile *ImportedBy,
Douglas Gregor7029ce12013-03-19 00:28:20 +000059 unsigned Generation,
60 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +000061 ASTFileSignature ExpectedSignature,
Ben Langmuir70a1b812015-03-24 04:43:52 +000062 ASTFileSignatureReader ReadSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +000063 ModuleFile *&Module,
64 std::string &ErrorStr) {
Craig Toppera13603a2014-05-22 05:54:18 +000065 Module = nullptr;
Douglas Gregor7029ce12013-03-19 00:28:20 +000066
67 // Look for the file entry. This only fails if the expected size or
68 // modification time differ.
69 const FileEntry *Entry;
Richard Smith5b390752014-11-21 05:37:20 +000070 if (Type == MK_ExplicitModule) {
71 // If we're not expecting to pull this file out of the module cache, it
72 // might have a different mtime due to being moved across filesystems in
73 // a distributed build. The size must still match, though. (As must the
74 // contents, but we can't check that.)
75 ExpectedModTime = 0;
76 }
Eli Friedmanc27d0d52013-09-05 23:50:58 +000077 if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
78 ErrorStr = "module file out of date";
Douglas Gregor7029ce12013-03-19 00:28:20 +000079 return OutOfDate;
Eli Friedmanc27d0d52013-09-05 23:50:58 +000080 }
Douglas Gregor7029ce12013-03-19 00:28:20 +000081
Douglas Gregord44252e2011-08-25 20:47:51 +000082 if (!Entry && FileName != "-") {
Eli Friedmanc27d0d52013-09-05 23:50:58 +000083 ErrorStr = "module file not found";
Douglas Gregor7029ce12013-03-19 00:28:20 +000084 return Missing;
Douglas Gregord44252e2011-08-25 20:47:51 +000085 }
Douglas Gregor7029ce12013-03-19 00:28:20 +000086
87 // Check whether we already loaded this module, before
Douglas Gregorde3ef502011-11-30 23:21:26 +000088 ModuleFile *&ModuleEntry = Modules[Entry];
Douglas Gregord44252e2011-08-25 20:47:51 +000089 bool NewModule = false;
90 if (!ModuleEntry) {
91 // Allocate a new module.
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +000092 ModuleFile *New = new ModuleFile(Type, Generation);
Douglas Gregorbdb259d2013-01-21 20:07:12 +000093 New->Index = Chain.size();
Douglas Gregord44252e2011-08-25 20:47:51 +000094 New->FileName = FileName.str();
Argyrios Kyrtzidisaedf7142012-10-03 01:58:42 +000095 New->File = Entry;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000096 New->ImportLoc = ImportLoc;
Douglas Gregord44252e2011-08-25 20:47:51 +000097 Chain.push_back(New);
Richard Smith33e0f7e2015-07-22 02:08:40 +000098 if (!New->isModule())
99 PCHChain.push_back(New);
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000100 if (!ImportedBy)
101 Roots.push_back(New);
Douglas Gregord44252e2011-08-25 20:47:51 +0000102 NewModule = true;
103 ModuleEntry = New;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +0000104
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000105 New->InputFilesValidationTimestamp = 0;
Richard Smithe842a472014-10-22 02:05:46 +0000106 if (New->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000107 std::string TimestampFilename = New->getTimestampFilename();
Ben Langmuirc8130a72014-02-20 21:59:23 +0000108 vfs::Status Status;
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000109 // A cached stat value would be fine as well.
110 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
111 New->InputFilesValidationTimestamp =
112 Status.getLastModificationTime().toEpochTime();
113 }
114
Douglas Gregord44252e2011-08-25 20:47:51 +0000115 // Load the contents of the module
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000116 if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000117 // The buffer was already provided for us.
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000118 New->Buffer = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000119 } else {
120 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +0000121 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf(
122 (std::error_code()));
Douglas Gregord44252e2011-08-25 20:47:51 +0000123 if (FileName == "-") {
Benjamin Kramera8857962014-10-26 22:44:13 +0000124 Buf = llvm::MemoryBuffer::getSTDIN();
Ben Langmuir9801b252014-06-20 00:24:56 +0000125 } else {
126 // Leave the FileEntry open so if it gets read again by another
127 // ModuleManager it must be the same underlying file.
128 // FIXME: Because FileManager::getFile() doesn't guarantee that it will
129 // give us an open file, this may not be 100% reliable.
Benjamin Kramera8857962014-10-26 22:44:13 +0000130 Buf = FileMgr.getBufferForFile(New->File,
131 /*IsVolatile=*/false,
132 /*ShouldClose=*/false);
Ben Langmuir9801b252014-06-20 00:24:56 +0000133 }
Benjamin Kramera8857962014-10-26 22:44:13 +0000134
135 if (!Buf) {
136 ErrorStr = Buf.getError().message();
Douglas Gregor7029ce12013-03-19 00:28:20 +0000137 return Missing;
Benjamin Kramera8857962014-10-26 22:44:13 +0000138 }
139
140 New->Buffer = std::move(*Buf);
Douglas Gregord44252e2011-08-25 20:47:51 +0000141 }
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000142
143 // Initialize the stream.
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000144 PCHContainerRdr.ExtractPCH(New->Buffer->getMemBufferRef(), New->StreamFile);
Ben Langmuired982582014-11-08 00:34:30 +0000145 }
Ben Langmuir487ea142014-10-23 18:05:36 +0000146
Ben Langmuired982582014-11-08 00:34:30 +0000147 if (ExpectedSignature) {
148 if (NewModule)
149 ModuleEntry->Signature = ReadSignature(ModuleEntry->StreamFile);
150 else
151 assert(ModuleEntry->Signature == ReadSignature(ModuleEntry->StreamFile));
Ben Langmuir487ea142014-10-23 18:05:36 +0000152
Ben Langmuired982582014-11-08 00:34:30 +0000153 if (ModuleEntry->Signature != ExpectedSignature) {
154 ErrorStr = ModuleEntry->Signature ? "signature mismatch"
155 : "could not read module signature";
156
157 if (NewModule) {
Ben Langmuir487ea142014-10-23 18:05:36 +0000158 // Remove the module file immediately, since removeModules might try to
159 // invalidate the file cache for Entry, and that is not safe if this
160 // module is *itself* up to date, but has an out-of-date importer.
161 Modules.erase(Entry);
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000162 assert(Chain.back() == ModuleEntry);
Ben Langmuir487ea142014-10-23 18:05:36 +0000163 Chain.pop_back();
Richard Smith33e0f7e2015-07-22 02:08:40 +0000164 if (!ModuleEntry->isModule())
165 PCHChain.pop_back();
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000166 if (Roots.back() == ModuleEntry)
167 Roots.pop_back();
168 else
169 assert(ImportedBy);
Ben Langmuired982582014-11-08 00:34:30 +0000170 delete ModuleEntry;
Ben Langmuir487ea142014-10-23 18:05:36 +0000171 }
Ben Langmuired982582014-11-08 00:34:30 +0000172 return OutOfDate;
Ben Langmuir487ea142014-10-23 18:05:36 +0000173 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000174 }
Douglas Gregord44252e2011-08-25 20:47:51 +0000175
176 if (ImportedBy) {
177 ModuleEntry->ImportedBy.insert(ImportedBy);
178 ImportedBy->Imports.insert(ModuleEntry);
179 } else {
Douglas Gregor6fb03ae2012-11-30 19:28:05 +0000180 if (!ModuleEntry->DirectlyImported)
181 ModuleEntry->ImportLoc = ImportLoc;
182
Douglas Gregord44252e2011-08-25 20:47:51 +0000183 ModuleEntry->DirectlyImported = true;
184 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000185
186 Module = ModuleEntry;
187 return NewModule? NewlyLoaded : AlreadyLoaded;
Douglas Gregord44252e2011-08-25 20:47:51 +0000188}
189
Ben Langmuir9801b252014-06-20 00:24:56 +0000190void ModuleManager::removeModules(
191 ModuleIterator first, ModuleIterator last,
192 llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
193 ModuleMap *modMap) {
Douglas Gregor188dbef2012-11-07 17:46:15 +0000194 if (first == last)
195 return;
196
Ben Langmuira50dbb22015-10-21 23:12:45 +0000197 // Explicitly clear VisitOrder since we might not notice it is stale.
198 VisitOrder.clear();
199
Douglas Gregor188dbef2012-11-07 17:46:15 +0000200 // Collect the set of module file pointers that we'll be removing.
201 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
202
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000203 auto IsVictim = [&](ModuleFile *MF) {
204 return victimSet.count(MF);
205 };
Douglas Gregor188dbef2012-11-07 17:46:15 +0000206 // Remove any references to the now-destroyed modules.
Douglas Gregor188dbef2012-11-07 17:46:15 +0000207 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000208 Chain[i]->ImportedBy.remove_if(IsVictim);
Douglas Gregor188dbef2012-11-07 17:46:15 +0000209 }
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000210 Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim),
211 Roots.end());
Douglas Gregor188dbef2012-11-07 17:46:15 +0000212
Richard Smith16fe4d12015-07-22 22:51:15 +0000213 // Remove the modules from the PCH chain.
214 for (auto I = first; I != last; ++I) {
215 if (!(*I)->isModule()) {
216 PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), *I),
217 PCHChain.end());
218 break;
219 }
220 }
221
Douglas Gregor188dbef2012-11-07 17:46:15 +0000222 // Delete the modules and erase them from the various structures.
223 for (ModuleIterator victim = first; victim != last; ++victim) {
Ben Langmuircaea1312014-05-19 17:04:28 +0000224 Modules.erase((*victim)->File);
Ben Langmuirca392142014-05-19 16:13:45 +0000225
Douglas Gregor7029ce12013-03-19 00:28:20 +0000226 if (modMap) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000227 StringRef ModuleName = (*victim)->ModuleName;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000228 if (Module *mod = modMap->findModule(ModuleName)) {
Craig Toppera13603a2014-05-22 05:54:18 +0000229 mod->setASTFile(nullptr);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000230 }
231 }
Ben Langmuir4f054782014-05-30 21:20:54 +0000232
Ben Langmuir9801b252014-06-20 00:24:56 +0000233 // Files that didn't make it through ReadASTCore successfully will be
234 // rebuilt (or there was an error). Invalidate them so that we can load the
235 // new files that will be renamed over the old ones.
236 if (LoadedSuccessfully.count(*victim) == 0)
Ben Langmuir4f054782014-05-30 21:20:54 +0000237 FileMgr.invalidateCache((*victim)->File);
238
Douglas Gregor188dbef2012-11-07 17:46:15 +0000239 delete *victim;
240 }
241
242 // Remove the modules from the chain.
243 Chain.erase(first, last);
244}
245
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000246void
247ModuleManager::addInMemoryBuffer(StringRef FileName,
248 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
249
250 const FileEntry *Entry =
251 FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
252 InMemoryBuffers[Entry] = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000253}
254
Douglas Gregore97cd902013-01-28 16:46:33 +0000255ModuleManager::VisitState *ModuleManager::allocateVisitState() {
256 // Fast path: if we have a cached state, use it.
257 if (FirstVisitState) {
258 VisitState *Result = FirstVisitState;
259 FirstVisitState = FirstVisitState->NextState;
Craig Toppera13603a2014-05-22 05:54:18 +0000260 Result->NextState = nullptr;
Douglas Gregore97cd902013-01-28 16:46:33 +0000261 return Result;
262 }
263
264 // Allocate and return a new state.
265 return new VisitState(size());
266}
267
268void ModuleManager::returnVisitState(VisitState *State) {
Craig Toppera13603a2014-05-22 05:54:18 +0000269 assert(State->NextState == nullptr && "Visited state is in list?");
Douglas Gregore97cd902013-01-28 16:46:33 +0000270 State->NextState = FirstVisitState;
271 FirstVisitState = State;
272}
273
Douglas Gregor7211ac12013-01-25 23:32:03 +0000274void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
275 GlobalIndex = Index;
Douglas Gregor603cd862013-03-22 18:50:14 +0000276 if (!GlobalIndex) {
277 ModulesInCommonWithGlobalIndex.clear();
278 return;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000279 }
Douglas Gregor603cd862013-03-22 18:50:14 +0000280
281 // Notify the global module index about all of the modules we've already
282 // loaded.
283 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
284 if (!GlobalIndex->loadedModuleFile(Chain[I])) {
285 ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
286 }
287 }
288}
289
290void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
291 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
292 return;
293
294 ModulesInCommonWithGlobalIndex.push_back(MF);
Douglas Gregor7211ac12013-01-25 23:32:03 +0000295}
296
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000297ModuleManager::ModuleManager(FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000298 const PCHContainerReader &PCHContainerRdr)
299 : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr), GlobalIndex(),
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000300 FirstVisitState(nullptr) {}
Douglas Gregord44252e2011-08-25 20:47:51 +0000301
302ModuleManager::~ModuleManager() {
303 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
304 delete Chain[e - i - 1];
Douglas Gregore97cd902013-01-28 16:46:33 +0000305 delete FirstVisitState;
Douglas Gregord44252e2011-08-25 20:47:51 +0000306}
307
Benjamin Kramer9a9efba2015-07-25 12:14:04 +0000308void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
309 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
Douglas Gregor7211ac12013-01-25 23:32:03 +0000310 // If the visitation order vector is the wrong size, recompute the order.
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000311 if (VisitOrder.size() != Chain.size()) {
312 unsigned N = size();
313 VisitOrder.clear();
314 VisitOrder.reserve(N);
315
316 // Record the number of incoming edges for each module. When we
317 // encounter a module with no incoming edges, push it into the queue
318 // to seed the queue.
319 SmallVector<ModuleFile *, 4> Queue;
320 Queue.reserve(N);
321 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
Richard Smitha7c535b2015-07-22 01:28:05 +0000322 UnusedIncomingEdges.resize(size());
323 for (auto M = rbegin(), MEnd = rend(); M != MEnd; ++M) {
324 unsigned Size = (*M)->ImportedBy.size();
325 UnusedIncomingEdges[(*M)->Index] = Size;
326 if (!Size)
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000327 Queue.push_back(*M);
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000328 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000329
330 // Traverse the graph, making sure to visit a module before visiting any
331 // of its dependencies.
Richard Smitha7c535b2015-07-22 01:28:05 +0000332 while (!Queue.empty()) {
333 ModuleFile *CurrentModule = Queue.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000334 VisitOrder.push_back(CurrentModule);
335
336 // For any module that this module depends on, push it on the
337 // stack (if it hasn't already been marked as visited).
Richard Smitha7c535b2015-07-22 01:28:05 +0000338 for (auto M = CurrentModule->Imports.rbegin(),
339 MEnd = CurrentModule->Imports.rend();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000340 M != MEnd; ++M) {
341 // Remove our current module as an impediment to visiting the
342 // module we depend on. If we were the last unvisited module
343 // that depends on this particular module, push it into the
344 // queue to be visited.
345 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
346 if (NumUnusedEdges && (--NumUnusedEdges == 0))
347 Queue.push_back(*M);
348 }
349 }
350
351 assert(VisitOrder.size() == N && "Visitation order is wrong?");
Douglas Gregor7211ac12013-01-25 23:32:03 +0000352
Douglas Gregore97cd902013-01-28 16:46:33 +0000353 delete FirstVisitState;
Craig Toppera13603a2014-05-22 05:54:18 +0000354 FirstVisitState = nullptr;
Douglas Gregord44252e2011-08-25 20:47:51 +0000355 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000356
Douglas Gregore97cd902013-01-28 16:46:33 +0000357 VisitState *State = allocateVisitState();
358 unsigned VisitNumber = State->NextVisitNumber++;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000359
Douglas Gregor7211ac12013-01-25 23:32:03 +0000360 // If the caller has provided us with a hit-set that came from the global
361 // module index, mark every module file in common with the global module
362 // index that is *not* in that set as 'visited'.
363 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
364 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
365 {
366 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
Douglas Gregor7029ce12013-03-19 00:28:20 +0000367 if (!ModuleFilesHit->count(M))
Douglas Gregore97cd902013-01-28 16:46:33 +0000368 State->VisitNumber[M->Index] = VisitNumber;
Douglas Gregor7211ac12013-01-25 23:32:03 +0000369 }
370 }
371
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000372 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
373 ModuleFile *CurrentModule = VisitOrder[I];
374 // Should we skip this module file?
Douglas Gregore97cd902013-01-28 16:46:33 +0000375 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
Douglas Gregord44252e2011-08-25 20:47:51 +0000376 continue;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000377
378 // Visit the module.
Douglas Gregore97cd902013-01-28 16:46:33 +0000379 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
380 State->VisitNumber[CurrentModule->Index] = VisitNumber;
Benjamin Kramer9a9efba2015-07-25 12:14:04 +0000381 if (!Visitor(*CurrentModule))
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000382 continue;
383
384 // The visitor has requested that cut off visitation of any
385 // module that the current module depends on. To indicate this
386 // behavior, we mark all of the reachable modules as having been visited.
387 ModuleFile *NextModule = CurrentModule;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000388 do {
389 // For any module that this module depends on, push it on the
390 // stack (if it hasn't already been marked as visited).
391 for (llvm::SetVector<ModuleFile *>::iterator
392 M = NextModule->Imports.begin(),
393 MEnd = NextModule->Imports.end();
394 M != MEnd; ++M) {
Douglas Gregore97cd902013-01-28 16:46:33 +0000395 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
396 State->Stack.push_back(*M);
397 State->VisitNumber[(*M)->Index] = VisitNumber;
Douglas Gregord44252e2011-08-25 20:47:51 +0000398 }
399 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000400
Douglas Gregore97cd902013-01-28 16:46:33 +0000401 if (State->Stack.empty())
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000402 break;
403
404 // Pop the next module off the stack.
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000405 NextModule = State->Stack.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000406 } while (true);
Douglas Gregord44252e2011-08-25 20:47:51 +0000407 }
Douglas Gregore97cd902013-01-28 16:46:33 +0000408
409 returnVisitState(State);
Douglas Gregord44252e2011-08-25 20:47:51 +0000410}
411
Douglas Gregor7029ce12013-03-19 00:28:20 +0000412bool ModuleManager::lookupModuleFile(StringRef FileName,
413 off_t ExpectedSize,
414 time_t ExpectedModTime,
415 const FileEntry *&File) {
Ben Langmuir05f82ba2014-05-01 03:33:36 +0000416 // Open the file immediately to ensure there is no race between stat'ing and
417 // opening the file.
418 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000419
420 if (!File && FileName != "-") {
421 return false;
422 }
423
424 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
Ben Langmuir027731d2014-05-04 05:20:54 +0000425 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
426 // Do not destroy File, as it may be referenced. If we need to rebuild it,
427 // it will be destroyed by removeModules.
Douglas Gregor7029ce12013-03-19 00:28:20 +0000428 return true;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000429
430 return false;
431}
432
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000433#ifndef NDEBUG
434namespace llvm {
435 template<>
436 struct GraphTraits<ModuleManager> {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000437 typedef ModuleFile NodeType;
438 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000439 typedef ModuleManager::ModuleConstIterator nodes_iterator;
440
441 static ChildIteratorType child_begin(NodeType *Node) {
442 return Node->Imports.begin();
443 }
444
445 static ChildIteratorType child_end(NodeType *Node) {
446 return Node->Imports.end();
447 }
448
449 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
450 return Manager.begin();
451 }
452
453 static nodes_iterator nodes_end(const ModuleManager &Manager) {
454 return Manager.end();
455 }
456 };
457
458 template<>
459 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
460 explicit DOTGraphTraits(bool IsSimple = false)
461 : DefaultDOTGraphTraits(IsSimple) { }
462
463 static bool renderGraphFromBottomUp() {
464 return true;
465 }
466
Douglas Gregorde3ef502011-11-30 23:21:26 +0000467 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000468 return M->ModuleName;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000469 }
470 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000471}
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000472
473void ModuleManager::viewGraph() {
474 llvm::ViewGraph(*this, "Modules");
475}
476#endif