blob: 406c5c0a381f204e06c0ee8a9b80a6f58599d0b4 [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
197 // Collect the set of module file pointers that we'll be removing.
198 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
199
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000200 auto IsVictim = [&](ModuleFile *MF) {
201 return victimSet.count(MF);
202 };
Douglas Gregor188dbef2012-11-07 17:46:15 +0000203 // Remove any references to the now-destroyed modules.
Douglas Gregor188dbef2012-11-07 17:46:15 +0000204 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000205 Chain[i]->ImportedBy.remove_if(IsVictim);
Douglas Gregor188dbef2012-11-07 17:46:15 +0000206 }
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000207 Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim),
208 Roots.end());
Douglas Gregor188dbef2012-11-07 17:46:15 +0000209
Richard Smith16fe4d12015-07-22 22:51:15 +0000210 // Remove the modules from the PCH chain.
211 for (auto I = first; I != last; ++I) {
212 if (!(*I)->isModule()) {
213 PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), *I),
214 PCHChain.end());
215 break;
216 }
217 }
218
Douglas Gregor188dbef2012-11-07 17:46:15 +0000219 // Delete the modules and erase them from the various structures.
220 for (ModuleIterator victim = first; victim != last; ++victim) {
Ben Langmuircaea1312014-05-19 17:04:28 +0000221 Modules.erase((*victim)->File);
Ben Langmuirca392142014-05-19 16:13:45 +0000222
Douglas Gregor7029ce12013-03-19 00:28:20 +0000223 if (modMap) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000224 StringRef ModuleName = (*victim)->ModuleName;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000225 if (Module *mod = modMap->findModule(ModuleName)) {
Craig Toppera13603a2014-05-22 05:54:18 +0000226 mod->setASTFile(nullptr);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000227 }
228 }
Ben Langmuir4f054782014-05-30 21:20:54 +0000229
Ben Langmuir9801b252014-06-20 00:24:56 +0000230 // Files that didn't make it through ReadASTCore successfully will be
231 // rebuilt (or there was an error). Invalidate them so that we can load the
232 // new files that will be renamed over the old ones.
233 if (LoadedSuccessfully.count(*victim) == 0)
Ben Langmuir4f054782014-05-30 21:20:54 +0000234 FileMgr.invalidateCache((*victim)->File);
235
Douglas Gregor188dbef2012-11-07 17:46:15 +0000236 delete *victim;
237 }
238
239 // Remove the modules from the chain.
240 Chain.erase(first, last);
241}
242
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000243void
244ModuleManager::addInMemoryBuffer(StringRef FileName,
245 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
246
247 const FileEntry *Entry =
248 FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
249 InMemoryBuffers[Entry] = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000250}
251
Richard Smith7f330cd2015-03-18 01:42:29 +0000252bool ModuleManager::addKnownModuleFile(StringRef FileName) {
253 const FileEntry *File;
254 if (lookupModuleFile(FileName, 0, 0, File))
255 return true;
256 if (!Modules.count(File))
257 AdditionalKnownModuleFiles.insert(File);
258 return false;
259}
260
Douglas Gregore97cd902013-01-28 16:46:33 +0000261ModuleManager::VisitState *ModuleManager::allocateVisitState() {
262 // Fast path: if we have a cached state, use it.
263 if (FirstVisitState) {
264 VisitState *Result = FirstVisitState;
265 FirstVisitState = FirstVisitState->NextState;
Craig Toppera13603a2014-05-22 05:54:18 +0000266 Result->NextState = nullptr;
Douglas Gregore97cd902013-01-28 16:46:33 +0000267 return Result;
268 }
269
270 // Allocate and return a new state.
271 return new VisitState(size());
272}
273
274void ModuleManager::returnVisitState(VisitState *State) {
Craig Toppera13603a2014-05-22 05:54:18 +0000275 assert(State->NextState == nullptr && "Visited state is in list?");
Douglas Gregore97cd902013-01-28 16:46:33 +0000276 State->NextState = FirstVisitState;
277 FirstVisitState = State;
278}
279
Douglas Gregor7211ac12013-01-25 23:32:03 +0000280void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
281 GlobalIndex = Index;
Douglas Gregor603cd862013-03-22 18:50:14 +0000282 if (!GlobalIndex) {
283 ModulesInCommonWithGlobalIndex.clear();
284 return;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000285 }
Douglas Gregor603cd862013-03-22 18:50:14 +0000286
287 // Notify the global module index about all of the modules we've already
288 // loaded.
289 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
290 if (!GlobalIndex->loadedModuleFile(Chain[I])) {
291 ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
292 }
293 }
294}
295
296void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
Richard Smith7f330cd2015-03-18 01:42:29 +0000297 AdditionalKnownModuleFiles.remove(MF->File);
298
Douglas Gregor603cd862013-03-22 18:50:14 +0000299 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
300 return;
301
302 ModulesInCommonWithGlobalIndex.push_back(MF);
Douglas Gregor7211ac12013-01-25 23:32:03 +0000303}
304
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000305ModuleManager::ModuleManager(FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000306 const PCHContainerReader &PCHContainerRdr)
307 : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr), GlobalIndex(),
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000308 FirstVisitState(nullptr) {}
Douglas Gregord44252e2011-08-25 20:47:51 +0000309
310ModuleManager::~ModuleManager() {
311 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
312 delete Chain[e - i - 1];
Douglas Gregore97cd902013-01-28 16:46:33 +0000313 delete FirstVisitState;
Douglas Gregord44252e2011-08-25 20:47:51 +0000314}
315
Benjamin Kramer9a9efba2015-07-25 12:14:04 +0000316void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
317 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
Douglas Gregor7211ac12013-01-25 23:32:03 +0000318 // If the visitation order vector is the wrong size, recompute the order.
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000319 if (VisitOrder.size() != Chain.size()) {
320 unsigned N = size();
321 VisitOrder.clear();
322 VisitOrder.reserve(N);
323
324 // Record the number of incoming edges for each module. When we
325 // encounter a module with no incoming edges, push it into the queue
326 // to seed the queue.
327 SmallVector<ModuleFile *, 4> Queue;
328 Queue.reserve(N);
329 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
Richard Smitha7c535b2015-07-22 01:28:05 +0000330 UnusedIncomingEdges.resize(size());
331 for (auto M = rbegin(), MEnd = rend(); M != MEnd; ++M) {
332 unsigned Size = (*M)->ImportedBy.size();
333 UnusedIncomingEdges[(*M)->Index] = Size;
334 if (!Size)
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000335 Queue.push_back(*M);
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000336 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000337
338 // Traverse the graph, making sure to visit a module before visiting any
339 // of its dependencies.
Richard Smitha7c535b2015-07-22 01:28:05 +0000340 while (!Queue.empty()) {
341 ModuleFile *CurrentModule = Queue.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000342 VisitOrder.push_back(CurrentModule);
343
344 // For any module that this module depends on, push it on the
345 // stack (if it hasn't already been marked as visited).
Richard Smitha7c535b2015-07-22 01:28:05 +0000346 for (auto M = CurrentModule->Imports.rbegin(),
347 MEnd = CurrentModule->Imports.rend();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000348 M != MEnd; ++M) {
349 // Remove our current module as an impediment to visiting the
350 // module we depend on. If we were the last unvisited module
351 // that depends on this particular module, push it into the
352 // queue to be visited.
353 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
354 if (NumUnusedEdges && (--NumUnusedEdges == 0))
355 Queue.push_back(*M);
356 }
357 }
358
359 assert(VisitOrder.size() == N && "Visitation order is wrong?");
Douglas Gregor7211ac12013-01-25 23:32:03 +0000360
Douglas Gregore97cd902013-01-28 16:46:33 +0000361 delete FirstVisitState;
Craig Toppera13603a2014-05-22 05:54:18 +0000362 FirstVisitState = nullptr;
Douglas Gregord44252e2011-08-25 20:47:51 +0000363 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000364
Douglas Gregore97cd902013-01-28 16:46:33 +0000365 VisitState *State = allocateVisitState();
366 unsigned VisitNumber = State->NextVisitNumber++;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000367
Douglas Gregor7211ac12013-01-25 23:32:03 +0000368 // If the caller has provided us with a hit-set that came from the global
369 // module index, mark every module file in common with the global module
370 // index that is *not* in that set as 'visited'.
371 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
372 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
373 {
374 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
Douglas Gregor7029ce12013-03-19 00:28:20 +0000375 if (!ModuleFilesHit->count(M))
Douglas Gregore97cd902013-01-28 16:46:33 +0000376 State->VisitNumber[M->Index] = VisitNumber;
Douglas Gregor7211ac12013-01-25 23:32:03 +0000377 }
378 }
379
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000380 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
381 ModuleFile *CurrentModule = VisitOrder[I];
382 // Should we skip this module file?
Douglas Gregore97cd902013-01-28 16:46:33 +0000383 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
Douglas Gregord44252e2011-08-25 20:47:51 +0000384 continue;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000385
386 // Visit the module.
Douglas Gregore97cd902013-01-28 16:46:33 +0000387 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
388 State->VisitNumber[CurrentModule->Index] = VisitNumber;
Benjamin Kramer9a9efba2015-07-25 12:14:04 +0000389 if (!Visitor(*CurrentModule))
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000390 continue;
391
392 // The visitor has requested that cut off visitation of any
393 // module that the current module depends on. To indicate this
394 // behavior, we mark all of the reachable modules as having been visited.
395 ModuleFile *NextModule = CurrentModule;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000396 do {
397 // For any module that this module depends on, push it on the
398 // stack (if it hasn't already been marked as visited).
399 for (llvm::SetVector<ModuleFile *>::iterator
400 M = NextModule->Imports.begin(),
401 MEnd = NextModule->Imports.end();
402 M != MEnd; ++M) {
Douglas Gregore97cd902013-01-28 16:46:33 +0000403 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
404 State->Stack.push_back(*M);
405 State->VisitNumber[(*M)->Index] = VisitNumber;
Douglas Gregord44252e2011-08-25 20:47:51 +0000406 }
407 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000408
Douglas Gregore97cd902013-01-28 16:46:33 +0000409 if (State->Stack.empty())
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000410 break;
411
412 // Pop the next module off the stack.
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000413 NextModule = State->Stack.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000414 } while (true);
Douglas Gregord44252e2011-08-25 20:47:51 +0000415 }
Douglas Gregore97cd902013-01-28 16:46:33 +0000416
417 returnVisitState(State);
Douglas Gregord44252e2011-08-25 20:47:51 +0000418}
419
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000420static void markVisitedDepthFirst(ModuleFile &M,
421 SmallVectorImpl<bool> &Visited) {
422 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
423 IMEnd = M.Imports.end();
424 IM != IMEnd; ++IM) {
425 if (Visited[(*IM)->Index])
426 continue;
427 Visited[(*IM)->Index] = true;
428 if (!M.DirectlyImported)
429 markVisitedDepthFirst(**IM, Visited);
430 }
431}
432
Douglas Gregord44252e2011-08-25 20:47:51 +0000433/// \brief Perform a depth-first visit of the current module.
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000434static bool visitDepthFirst(
435 ModuleFile &M,
436 ModuleManager::DFSPreorderControl (*PreorderVisitor)(ModuleFile &M,
437 void *UserData),
438 bool (*PostorderVisitor)(ModuleFile &M, void *UserData), void *UserData,
439 SmallVectorImpl<bool> &Visited) {
440 if (PreorderVisitor) {
441 switch (PreorderVisitor(M, UserData)) {
442 case ModuleManager::Abort:
443 return true;
444 case ModuleManager::SkipImports:
445 markVisitedDepthFirst(M, Visited);
446 return false;
447 case ModuleManager::Continue:
448 break;
449 }
450 }
451
Douglas Gregord44252e2011-08-25 20:47:51 +0000452 // Visit children
Douglas Gregorde3ef502011-11-30 23:21:26 +0000453 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000454 IMEnd = M.Imports.end();
Douglas Gregord44252e2011-08-25 20:47:51 +0000455 IM != IMEnd; ++IM) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000456 if (Visited[(*IM)->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000457 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000458 Visited[(*IM)->Index] = true;
459
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000460 if (visitDepthFirst(**IM, PreorderVisitor, PostorderVisitor, UserData, Visited))
Douglas Gregord44252e2011-08-25 20:47:51 +0000461 return true;
462 }
463
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000464 if (PostorderVisitor)
465 return PostorderVisitor(M, UserData);
466
467 return false;
Douglas Gregord44252e2011-08-25 20:47:51 +0000468}
469
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000470void ModuleManager::visitDepthFirst(
471 ModuleManager::DFSPreorderControl (*PreorderVisitor)(ModuleFile &M,
472 void *UserData),
473 bool (*PostorderVisitor)(ModuleFile &M, void *UserData), void *UserData) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000474 SmallVector<bool, 16> Visited(size(), false);
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000475 for (unsigned I = 0, N = Roots.size(); I != N; ++I) {
476 if (Visited[Roots[I]->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000477 continue;
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000478 Visited[Roots[I]->Index] = true;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000479
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000480 if (::visitDepthFirst(*Roots[I], PreorderVisitor, PostorderVisitor, UserData, Visited))
Douglas Gregord44252e2011-08-25 20:47:51 +0000481 return;
482 }
483}
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000484
Douglas Gregor7029ce12013-03-19 00:28:20 +0000485bool ModuleManager::lookupModuleFile(StringRef FileName,
486 off_t ExpectedSize,
487 time_t ExpectedModTime,
488 const FileEntry *&File) {
Ben Langmuir05f82ba2014-05-01 03:33:36 +0000489 // Open the file immediately to ensure there is no race between stat'ing and
490 // opening the file.
491 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000492
493 if (!File && FileName != "-") {
494 return false;
495 }
496
497 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
Ben Langmuir027731d2014-05-04 05:20:54 +0000498 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
499 // Do not destroy File, as it may be referenced. If we need to rebuild it,
500 // it will be destroyed by removeModules.
Douglas Gregor7029ce12013-03-19 00:28:20 +0000501 return true;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000502
503 return false;
504}
505
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000506#ifndef NDEBUG
507namespace llvm {
508 template<>
509 struct GraphTraits<ModuleManager> {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000510 typedef ModuleFile NodeType;
511 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000512 typedef ModuleManager::ModuleConstIterator nodes_iterator;
513
514 static ChildIteratorType child_begin(NodeType *Node) {
515 return Node->Imports.begin();
516 }
517
518 static ChildIteratorType child_end(NodeType *Node) {
519 return Node->Imports.end();
520 }
521
522 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
523 return Manager.begin();
524 }
525
526 static nodes_iterator nodes_end(const ModuleManager &Manager) {
527 return Manager.end();
528 }
529 };
530
531 template<>
532 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
533 explicit DOTGraphTraits(bool IsSimple = false)
534 : DefaultDOTGraphTraits(IsSimple) { }
535
536 static bool renderGraphFromBottomUp() {
537 return true;
538 }
539
Douglas Gregorde3ef502011-11-30 23:21:26 +0000540 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000541 return M->ModuleName;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000542 }
543 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000544}
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000545
546void ModuleManager::viewGraph() {
547 llvm::ViewGraph(*this, "Modules");
548}
549#endif