1f4a2713aSLionel Sambuc //===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file defines the ModuleManager class, which manages a set of loaded
11f4a2713aSLionel Sambuc // modules for the ASTReader.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14*0a6a1f1dSLionel Sambuc #include "clang/Lex/HeaderSearch.h"
15f4a2713aSLionel Sambuc #include "clang/Lex/ModuleMap.h"
16f4a2713aSLionel Sambuc #include "clang/Serialization/GlobalModuleIndex.h"
17f4a2713aSLionel Sambuc #include "clang/Serialization/ModuleManager.h"
18f4a2713aSLionel Sambuc #include "llvm/Support/MemoryBuffer.h"
19f4a2713aSLionel Sambuc #include "llvm/Support/Path.h"
20f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
21*0a6a1f1dSLionel Sambuc #include <system_error>
22f4a2713aSLionel Sambuc
23f4a2713aSLionel Sambuc #ifndef NDEBUG
24f4a2713aSLionel Sambuc #include "llvm/Support/GraphWriter.h"
25f4a2713aSLionel Sambuc #endif
26f4a2713aSLionel Sambuc
27f4a2713aSLionel Sambuc using namespace clang;
28f4a2713aSLionel Sambuc using namespace serialization;
29f4a2713aSLionel Sambuc
lookup(StringRef Name)30f4a2713aSLionel Sambuc ModuleFile *ModuleManager::lookup(StringRef Name) {
31f4a2713aSLionel Sambuc const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
32f4a2713aSLionel Sambuc /*cacheFailure=*/false);
33f4a2713aSLionel Sambuc if (Entry)
34f4a2713aSLionel Sambuc return lookup(Entry);
35f4a2713aSLionel Sambuc
36*0a6a1f1dSLionel Sambuc return nullptr;
37f4a2713aSLionel Sambuc }
38f4a2713aSLionel Sambuc
lookup(const FileEntry * File)39f4a2713aSLionel Sambuc ModuleFile *ModuleManager::lookup(const FileEntry *File) {
40f4a2713aSLionel Sambuc llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known
41f4a2713aSLionel Sambuc = Modules.find(File);
42f4a2713aSLionel Sambuc if (Known == Modules.end())
43*0a6a1f1dSLionel Sambuc return nullptr;
44f4a2713aSLionel Sambuc
45f4a2713aSLionel Sambuc return Known->second;
46f4a2713aSLionel Sambuc }
47f4a2713aSLionel Sambuc
48*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::MemoryBuffer>
lookupBuffer(StringRef Name)49*0a6a1f1dSLionel Sambuc ModuleManager::lookupBuffer(StringRef Name) {
50f4a2713aSLionel Sambuc const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
51f4a2713aSLionel Sambuc /*cacheFailure=*/false);
52*0a6a1f1dSLionel Sambuc return std::move(InMemoryBuffers[Entry]);
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc
55f4a2713aSLionel Sambuc ModuleManager::AddModuleResult
addModule(StringRef FileName,ModuleKind Type,SourceLocation ImportLoc,ModuleFile * ImportedBy,unsigned Generation,off_t ExpectedSize,time_t ExpectedModTime,ASTFileSignature ExpectedSignature,std::function<ASTFileSignature (llvm::BitstreamReader &)> ReadSignature,ModuleFile * & Module,std::string & ErrorStr)56f4a2713aSLionel Sambuc ModuleManager::addModule(StringRef FileName, ModuleKind Type,
57f4a2713aSLionel Sambuc SourceLocation ImportLoc, ModuleFile *ImportedBy,
58f4a2713aSLionel Sambuc unsigned Generation,
59f4a2713aSLionel Sambuc off_t ExpectedSize, time_t ExpectedModTime,
60*0a6a1f1dSLionel Sambuc ASTFileSignature ExpectedSignature,
61*0a6a1f1dSLionel Sambuc std::function<ASTFileSignature(llvm::BitstreamReader &)>
62*0a6a1f1dSLionel Sambuc ReadSignature,
63f4a2713aSLionel Sambuc ModuleFile *&Module,
64f4a2713aSLionel Sambuc std::string &ErrorStr) {
65*0a6a1f1dSLionel Sambuc Module = nullptr;
66f4a2713aSLionel Sambuc
67f4a2713aSLionel Sambuc // Look for the file entry. This only fails if the expected size or
68f4a2713aSLionel Sambuc // modification time differ.
69f4a2713aSLionel Sambuc const FileEntry *Entry;
70*0a6a1f1dSLionel Sambuc if (Type == MK_ExplicitModule) {
71*0a6a1f1dSLionel Sambuc // If we're not expecting to pull this file out of the module cache, it
72*0a6a1f1dSLionel Sambuc // might have a different mtime due to being moved across filesystems in
73*0a6a1f1dSLionel Sambuc // a distributed build. The size must still match, though. (As must the
74*0a6a1f1dSLionel Sambuc // contents, but we can't check that.)
75*0a6a1f1dSLionel Sambuc ExpectedModTime = 0;
76*0a6a1f1dSLionel Sambuc }
77f4a2713aSLionel Sambuc if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
78f4a2713aSLionel Sambuc ErrorStr = "module file out of date";
79f4a2713aSLionel Sambuc return OutOfDate;
80f4a2713aSLionel Sambuc }
81f4a2713aSLionel Sambuc
82f4a2713aSLionel Sambuc if (!Entry && FileName != "-") {
83f4a2713aSLionel Sambuc ErrorStr = "module file not found";
84f4a2713aSLionel Sambuc return Missing;
85f4a2713aSLionel Sambuc }
86f4a2713aSLionel Sambuc
87f4a2713aSLionel Sambuc // Check whether we already loaded this module, before
88f4a2713aSLionel Sambuc ModuleFile *&ModuleEntry = Modules[Entry];
89f4a2713aSLionel Sambuc bool NewModule = false;
90f4a2713aSLionel Sambuc if (!ModuleEntry) {
91f4a2713aSLionel Sambuc // Allocate a new module.
92f4a2713aSLionel Sambuc ModuleFile *New = new ModuleFile(Type, Generation);
93f4a2713aSLionel Sambuc New->Index = Chain.size();
94f4a2713aSLionel Sambuc New->FileName = FileName.str();
95f4a2713aSLionel Sambuc New->File = Entry;
96f4a2713aSLionel Sambuc New->ImportLoc = ImportLoc;
97f4a2713aSLionel Sambuc Chain.push_back(New);
98f4a2713aSLionel Sambuc NewModule = true;
99f4a2713aSLionel Sambuc ModuleEntry = New;
100f4a2713aSLionel Sambuc
101*0a6a1f1dSLionel Sambuc New->InputFilesValidationTimestamp = 0;
102*0a6a1f1dSLionel Sambuc if (New->Kind == MK_ImplicitModule) {
103*0a6a1f1dSLionel Sambuc std::string TimestampFilename = New->getTimestampFilename();
104*0a6a1f1dSLionel Sambuc vfs::Status Status;
105*0a6a1f1dSLionel Sambuc // A cached stat value would be fine as well.
106*0a6a1f1dSLionel Sambuc if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
107*0a6a1f1dSLionel Sambuc New->InputFilesValidationTimestamp =
108*0a6a1f1dSLionel Sambuc Status.getLastModificationTime().toEpochTime();
109*0a6a1f1dSLionel Sambuc }
110*0a6a1f1dSLionel Sambuc
111f4a2713aSLionel Sambuc // Load the contents of the module
112*0a6a1f1dSLionel Sambuc if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
113f4a2713aSLionel Sambuc // The buffer was already provided for us.
114*0a6a1f1dSLionel Sambuc New->Buffer = std::move(Buffer);
115f4a2713aSLionel Sambuc } else {
116f4a2713aSLionel Sambuc // Open the AST file.
117*0a6a1f1dSLionel Sambuc llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf(
118*0a6a1f1dSLionel Sambuc (std::error_code()));
119f4a2713aSLionel Sambuc if (FileName == "-") {
120*0a6a1f1dSLionel Sambuc Buf = llvm::MemoryBuffer::getSTDIN();
121*0a6a1f1dSLionel Sambuc } else {
122*0a6a1f1dSLionel Sambuc // Leave the FileEntry open so if it gets read again by another
123*0a6a1f1dSLionel Sambuc // ModuleManager it must be the same underlying file.
124*0a6a1f1dSLionel Sambuc // FIXME: Because FileManager::getFile() doesn't guarantee that it will
125*0a6a1f1dSLionel Sambuc // give us an open file, this may not be 100% reliable.
126*0a6a1f1dSLionel Sambuc Buf = FileMgr.getBufferForFile(New->File,
127*0a6a1f1dSLionel Sambuc /*IsVolatile=*/false,
128*0a6a1f1dSLionel Sambuc /*ShouldClose=*/false);
129*0a6a1f1dSLionel Sambuc }
130f4a2713aSLionel Sambuc
131*0a6a1f1dSLionel Sambuc if (!Buf) {
132*0a6a1f1dSLionel Sambuc ErrorStr = Buf.getError().message();
133f4a2713aSLionel Sambuc return Missing;
134f4a2713aSLionel Sambuc }
135f4a2713aSLionel Sambuc
136*0a6a1f1dSLionel Sambuc New->Buffer = std::move(*Buf);
137*0a6a1f1dSLionel Sambuc }
138*0a6a1f1dSLionel Sambuc
139f4a2713aSLionel Sambuc // Initialize the stream
140f4a2713aSLionel Sambuc New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
141f4a2713aSLionel Sambuc (const unsigned char *)New->Buffer->getBufferEnd());
142f4a2713aSLionel Sambuc }
143f4a2713aSLionel Sambuc
144*0a6a1f1dSLionel Sambuc if (ExpectedSignature) {
145*0a6a1f1dSLionel Sambuc if (NewModule)
146*0a6a1f1dSLionel Sambuc ModuleEntry->Signature = ReadSignature(ModuleEntry->StreamFile);
147*0a6a1f1dSLionel Sambuc else
148*0a6a1f1dSLionel Sambuc assert(ModuleEntry->Signature == ReadSignature(ModuleEntry->StreamFile));
149*0a6a1f1dSLionel Sambuc
150*0a6a1f1dSLionel Sambuc if (ModuleEntry->Signature != ExpectedSignature) {
151*0a6a1f1dSLionel Sambuc ErrorStr = ModuleEntry->Signature ? "signature mismatch"
152*0a6a1f1dSLionel Sambuc : "could not read module signature";
153*0a6a1f1dSLionel Sambuc
154*0a6a1f1dSLionel Sambuc if (NewModule) {
155*0a6a1f1dSLionel Sambuc // Remove the module file immediately, since removeModules might try to
156*0a6a1f1dSLionel Sambuc // invalidate the file cache for Entry, and that is not safe if this
157*0a6a1f1dSLionel Sambuc // module is *itself* up to date, but has an out-of-date importer.
158*0a6a1f1dSLionel Sambuc Modules.erase(Entry);
159*0a6a1f1dSLionel Sambuc Chain.pop_back();
160*0a6a1f1dSLionel Sambuc delete ModuleEntry;
161*0a6a1f1dSLionel Sambuc }
162*0a6a1f1dSLionel Sambuc return OutOfDate;
163*0a6a1f1dSLionel Sambuc }
164*0a6a1f1dSLionel Sambuc }
165*0a6a1f1dSLionel Sambuc
166f4a2713aSLionel Sambuc if (ImportedBy) {
167f4a2713aSLionel Sambuc ModuleEntry->ImportedBy.insert(ImportedBy);
168f4a2713aSLionel Sambuc ImportedBy->Imports.insert(ModuleEntry);
169f4a2713aSLionel Sambuc } else {
170f4a2713aSLionel Sambuc if (!ModuleEntry->DirectlyImported)
171f4a2713aSLionel Sambuc ModuleEntry->ImportLoc = ImportLoc;
172f4a2713aSLionel Sambuc
173f4a2713aSLionel Sambuc ModuleEntry->DirectlyImported = true;
174f4a2713aSLionel Sambuc }
175f4a2713aSLionel Sambuc
176f4a2713aSLionel Sambuc Module = ModuleEntry;
177f4a2713aSLionel Sambuc return NewModule? NewlyLoaded : AlreadyLoaded;
178f4a2713aSLionel Sambuc }
179f4a2713aSLionel Sambuc
removeModules(ModuleIterator first,ModuleIterator last,llvm::SmallPtrSetImpl<ModuleFile * > & LoadedSuccessfully,ModuleMap * modMap)180*0a6a1f1dSLionel Sambuc void ModuleManager::removeModules(
181*0a6a1f1dSLionel Sambuc ModuleIterator first, ModuleIterator last,
182*0a6a1f1dSLionel Sambuc llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
183f4a2713aSLionel Sambuc ModuleMap *modMap) {
184f4a2713aSLionel Sambuc if (first == last)
185f4a2713aSLionel Sambuc return;
186f4a2713aSLionel Sambuc
187f4a2713aSLionel Sambuc // Collect the set of module file pointers that we'll be removing.
188f4a2713aSLionel Sambuc llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
189f4a2713aSLionel Sambuc
190f4a2713aSLionel Sambuc // Remove any references to the now-destroyed modules.
191f4a2713aSLionel Sambuc for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
192*0a6a1f1dSLionel Sambuc Chain[i]->ImportedBy.remove_if([&](ModuleFile *MF) {
193*0a6a1f1dSLionel Sambuc return victimSet.count(MF);
194*0a6a1f1dSLionel Sambuc });
195f4a2713aSLionel Sambuc }
196f4a2713aSLionel Sambuc
197f4a2713aSLionel Sambuc // Delete the modules and erase them from the various structures.
198f4a2713aSLionel Sambuc for (ModuleIterator victim = first; victim != last; ++victim) {
199f4a2713aSLionel Sambuc Modules.erase((*victim)->File);
200f4a2713aSLionel Sambuc
201f4a2713aSLionel Sambuc if (modMap) {
202*0a6a1f1dSLionel Sambuc StringRef ModuleName = (*victim)->ModuleName;
203f4a2713aSLionel Sambuc if (Module *mod = modMap->findModule(ModuleName)) {
204*0a6a1f1dSLionel Sambuc mod->setASTFile(nullptr);
205f4a2713aSLionel Sambuc }
206f4a2713aSLionel Sambuc }
207*0a6a1f1dSLionel Sambuc
208*0a6a1f1dSLionel Sambuc // Files that didn't make it through ReadASTCore successfully will be
209*0a6a1f1dSLionel Sambuc // rebuilt (or there was an error). Invalidate them so that we can load the
210*0a6a1f1dSLionel Sambuc // new files that will be renamed over the old ones.
211*0a6a1f1dSLionel Sambuc if (LoadedSuccessfully.count(*victim) == 0)
212*0a6a1f1dSLionel Sambuc FileMgr.invalidateCache((*victim)->File);
213*0a6a1f1dSLionel Sambuc
214f4a2713aSLionel Sambuc delete *victim;
215f4a2713aSLionel Sambuc }
216f4a2713aSLionel Sambuc
217f4a2713aSLionel Sambuc // Remove the modules from the chain.
218f4a2713aSLionel Sambuc Chain.erase(first, last);
219f4a2713aSLionel Sambuc }
220f4a2713aSLionel Sambuc
221*0a6a1f1dSLionel Sambuc void
addInMemoryBuffer(StringRef FileName,std::unique_ptr<llvm::MemoryBuffer> Buffer)222*0a6a1f1dSLionel Sambuc ModuleManager::addInMemoryBuffer(StringRef FileName,
223*0a6a1f1dSLionel Sambuc std::unique_ptr<llvm::MemoryBuffer> Buffer) {
224f4a2713aSLionel Sambuc
225*0a6a1f1dSLionel Sambuc const FileEntry *Entry =
226*0a6a1f1dSLionel Sambuc FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
227*0a6a1f1dSLionel Sambuc InMemoryBuffers[Entry] = std::move(Buffer);
228f4a2713aSLionel Sambuc }
229f4a2713aSLionel Sambuc
allocateVisitState()230f4a2713aSLionel Sambuc ModuleManager::VisitState *ModuleManager::allocateVisitState() {
231f4a2713aSLionel Sambuc // Fast path: if we have a cached state, use it.
232f4a2713aSLionel Sambuc if (FirstVisitState) {
233f4a2713aSLionel Sambuc VisitState *Result = FirstVisitState;
234f4a2713aSLionel Sambuc FirstVisitState = FirstVisitState->NextState;
235*0a6a1f1dSLionel Sambuc Result->NextState = nullptr;
236f4a2713aSLionel Sambuc return Result;
237f4a2713aSLionel Sambuc }
238f4a2713aSLionel Sambuc
239f4a2713aSLionel Sambuc // Allocate and return a new state.
240f4a2713aSLionel Sambuc return new VisitState(size());
241f4a2713aSLionel Sambuc }
242f4a2713aSLionel Sambuc
returnVisitState(VisitState * State)243f4a2713aSLionel Sambuc void ModuleManager::returnVisitState(VisitState *State) {
244*0a6a1f1dSLionel Sambuc assert(State->NextState == nullptr && "Visited state is in list?");
245f4a2713aSLionel Sambuc State->NextState = FirstVisitState;
246f4a2713aSLionel Sambuc FirstVisitState = State;
247f4a2713aSLionel Sambuc }
248f4a2713aSLionel Sambuc
setGlobalIndex(GlobalModuleIndex * Index)249f4a2713aSLionel Sambuc void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
250f4a2713aSLionel Sambuc GlobalIndex = Index;
251f4a2713aSLionel Sambuc if (!GlobalIndex) {
252f4a2713aSLionel Sambuc ModulesInCommonWithGlobalIndex.clear();
253f4a2713aSLionel Sambuc return;
254f4a2713aSLionel Sambuc }
255f4a2713aSLionel Sambuc
256f4a2713aSLionel Sambuc // Notify the global module index about all of the modules we've already
257f4a2713aSLionel Sambuc // loaded.
258f4a2713aSLionel Sambuc for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
259f4a2713aSLionel Sambuc if (!GlobalIndex->loadedModuleFile(Chain[I])) {
260f4a2713aSLionel Sambuc ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
261f4a2713aSLionel Sambuc }
262f4a2713aSLionel Sambuc }
263f4a2713aSLionel Sambuc }
264f4a2713aSLionel Sambuc
moduleFileAccepted(ModuleFile * MF)265f4a2713aSLionel Sambuc void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
266f4a2713aSLionel Sambuc if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
267f4a2713aSLionel Sambuc return;
268f4a2713aSLionel Sambuc
269f4a2713aSLionel Sambuc ModulesInCommonWithGlobalIndex.push_back(MF);
270f4a2713aSLionel Sambuc }
271f4a2713aSLionel Sambuc
ModuleManager(FileManager & FileMgr)272f4a2713aSLionel Sambuc ModuleManager::ModuleManager(FileManager &FileMgr)
273*0a6a1f1dSLionel Sambuc : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(nullptr) {}
274f4a2713aSLionel Sambuc
~ModuleManager()275f4a2713aSLionel Sambuc ModuleManager::~ModuleManager() {
276f4a2713aSLionel Sambuc for (unsigned i = 0, e = Chain.size(); i != e; ++i)
277f4a2713aSLionel Sambuc delete Chain[e - i - 1];
278f4a2713aSLionel Sambuc delete FirstVisitState;
279f4a2713aSLionel Sambuc }
280f4a2713aSLionel Sambuc
281f4a2713aSLionel Sambuc void
visit(bool (* Visitor)(ModuleFile & M,void * UserData),void * UserData,llvm::SmallPtrSetImpl<ModuleFile * > * ModuleFilesHit)282f4a2713aSLionel Sambuc ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
283f4a2713aSLionel Sambuc void *UserData,
284*0a6a1f1dSLionel Sambuc llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
285f4a2713aSLionel Sambuc // If the visitation order vector is the wrong size, recompute the order.
286f4a2713aSLionel Sambuc if (VisitOrder.size() != Chain.size()) {
287f4a2713aSLionel Sambuc unsigned N = size();
288f4a2713aSLionel Sambuc VisitOrder.clear();
289f4a2713aSLionel Sambuc VisitOrder.reserve(N);
290f4a2713aSLionel Sambuc
291f4a2713aSLionel Sambuc // Record the number of incoming edges for each module. When we
292f4a2713aSLionel Sambuc // encounter a module with no incoming edges, push it into the queue
293f4a2713aSLionel Sambuc // to seed the queue.
294f4a2713aSLionel Sambuc SmallVector<ModuleFile *, 4> Queue;
295f4a2713aSLionel Sambuc Queue.reserve(N);
296f4a2713aSLionel Sambuc llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
297f4a2713aSLionel Sambuc UnusedIncomingEdges.reserve(size());
298f4a2713aSLionel Sambuc for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
299f4a2713aSLionel Sambuc if (unsigned Size = (*M)->ImportedBy.size())
300f4a2713aSLionel Sambuc UnusedIncomingEdges.push_back(Size);
301f4a2713aSLionel Sambuc else {
302f4a2713aSLionel Sambuc UnusedIncomingEdges.push_back(0);
303f4a2713aSLionel Sambuc Queue.push_back(*M);
304f4a2713aSLionel Sambuc }
305f4a2713aSLionel Sambuc }
306f4a2713aSLionel Sambuc
307f4a2713aSLionel Sambuc // Traverse the graph, making sure to visit a module before visiting any
308f4a2713aSLionel Sambuc // of its dependencies.
309f4a2713aSLionel Sambuc unsigned QueueStart = 0;
310f4a2713aSLionel Sambuc while (QueueStart < Queue.size()) {
311f4a2713aSLionel Sambuc ModuleFile *CurrentModule = Queue[QueueStart++];
312f4a2713aSLionel Sambuc VisitOrder.push_back(CurrentModule);
313f4a2713aSLionel Sambuc
314f4a2713aSLionel Sambuc // For any module that this module depends on, push it on the
315f4a2713aSLionel Sambuc // stack (if it hasn't already been marked as visited).
316f4a2713aSLionel Sambuc for (llvm::SetVector<ModuleFile *>::iterator
317f4a2713aSLionel Sambuc M = CurrentModule->Imports.begin(),
318f4a2713aSLionel Sambuc MEnd = CurrentModule->Imports.end();
319f4a2713aSLionel Sambuc M != MEnd; ++M) {
320f4a2713aSLionel Sambuc // Remove our current module as an impediment to visiting the
321f4a2713aSLionel Sambuc // module we depend on. If we were the last unvisited module
322f4a2713aSLionel Sambuc // that depends on this particular module, push it into the
323f4a2713aSLionel Sambuc // queue to be visited.
324f4a2713aSLionel Sambuc unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
325f4a2713aSLionel Sambuc if (NumUnusedEdges && (--NumUnusedEdges == 0))
326f4a2713aSLionel Sambuc Queue.push_back(*M);
327f4a2713aSLionel Sambuc }
328f4a2713aSLionel Sambuc }
329f4a2713aSLionel Sambuc
330f4a2713aSLionel Sambuc assert(VisitOrder.size() == N && "Visitation order is wrong?");
331f4a2713aSLionel Sambuc
332f4a2713aSLionel Sambuc delete FirstVisitState;
333*0a6a1f1dSLionel Sambuc FirstVisitState = nullptr;
334f4a2713aSLionel Sambuc }
335f4a2713aSLionel Sambuc
336f4a2713aSLionel Sambuc VisitState *State = allocateVisitState();
337f4a2713aSLionel Sambuc unsigned VisitNumber = State->NextVisitNumber++;
338f4a2713aSLionel Sambuc
339f4a2713aSLionel Sambuc // If the caller has provided us with a hit-set that came from the global
340f4a2713aSLionel Sambuc // module index, mark every module file in common with the global module
341f4a2713aSLionel Sambuc // index that is *not* in that set as 'visited'.
342f4a2713aSLionel Sambuc if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
343f4a2713aSLionel Sambuc for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
344f4a2713aSLionel Sambuc {
345f4a2713aSLionel Sambuc ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
346f4a2713aSLionel Sambuc if (!ModuleFilesHit->count(M))
347f4a2713aSLionel Sambuc State->VisitNumber[M->Index] = VisitNumber;
348f4a2713aSLionel Sambuc }
349f4a2713aSLionel Sambuc }
350f4a2713aSLionel Sambuc
351f4a2713aSLionel Sambuc for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
352f4a2713aSLionel Sambuc ModuleFile *CurrentModule = VisitOrder[I];
353f4a2713aSLionel Sambuc // Should we skip this module file?
354f4a2713aSLionel Sambuc if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
355f4a2713aSLionel Sambuc continue;
356f4a2713aSLionel Sambuc
357f4a2713aSLionel Sambuc // Visit the module.
358f4a2713aSLionel Sambuc assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
359f4a2713aSLionel Sambuc State->VisitNumber[CurrentModule->Index] = VisitNumber;
360f4a2713aSLionel Sambuc if (!Visitor(*CurrentModule, UserData))
361f4a2713aSLionel Sambuc continue;
362f4a2713aSLionel Sambuc
363f4a2713aSLionel Sambuc // The visitor has requested that cut off visitation of any
364f4a2713aSLionel Sambuc // module that the current module depends on. To indicate this
365f4a2713aSLionel Sambuc // behavior, we mark all of the reachable modules as having been visited.
366f4a2713aSLionel Sambuc ModuleFile *NextModule = CurrentModule;
367f4a2713aSLionel Sambuc do {
368f4a2713aSLionel Sambuc // For any module that this module depends on, push it on the
369f4a2713aSLionel Sambuc // stack (if it hasn't already been marked as visited).
370f4a2713aSLionel Sambuc for (llvm::SetVector<ModuleFile *>::iterator
371f4a2713aSLionel Sambuc M = NextModule->Imports.begin(),
372f4a2713aSLionel Sambuc MEnd = NextModule->Imports.end();
373f4a2713aSLionel Sambuc M != MEnd; ++M) {
374f4a2713aSLionel Sambuc if (State->VisitNumber[(*M)->Index] != VisitNumber) {
375f4a2713aSLionel Sambuc State->Stack.push_back(*M);
376f4a2713aSLionel Sambuc State->VisitNumber[(*M)->Index] = VisitNumber;
377f4a2713aSLionel Sambuc }
378f4a2713aSLionel Sambuc }
379f4a2713aSLionel Sambuc
380f4a2713aSLionel Sambuc if (State->Stack.empty())
381f4a2713aSLionel Sambuc break;
382f4a2713aSLionel Sambuc
383f4a2713aSLionel Sambuc // Pop the next module off the stack.
384f4a2713aSLionel Sambuc NextModule = State->Stack.pop_back_val();
385f4a2713aSLionel Sambuc } while (true);
386f4a2713aSLionel Sambuc }
387f4a2713aSLionel Sambuc
388f4a2713aSLionel Sambuc returnVisitState(State);
389f4a2713aSLionel Sambuc }
390f4a2713aSLionel Sambuc
391f4a2713aSLionel Sambuc /// \brief Perform a depth-first visit of the current module.
visitDepthFirst(ModuleFile & M,bool (* Visitor)(ModuleFile & M,bool Preorder,void * UserData),void * UserData,SmallVectorImpl<bool> & Visited)392f4a2713aSLionel Sambuc static bool visitDepthFirst(ModuleFile &M,
393f4a2713aSLionel Sambuc bool (*Visitor)(ModuleFile &M, bool Preorder,
394f4a2713aSLionel Sambuc void *UserData),
395f4a2713aSLionel Sambuc void *UserData,
396f4a2713aSLionel Sambuc SmallVectorImpl<bool> &Visited) {
397f4a2713aSLionel Sambuc // Preorder visitation
398f4a2713aSLionel Sambuc if (Visitor(M, /*Preorder=*/true, UserData))
399f4a2713aSLionel Sambuc return true;
400f4a2713aSLionel Sambuc
401f4a2713aSLionel Sambuc // Visit children
402f4a2713aSLionel Sambuc for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
403f4a2713aSLionel Sambuc IMEnd = M.Imports.end();
404f4a2713aSLionel Sambuc IM != IMEnd; ++IM) {
405f4a2713aSLionel Sambuc if (Visited[(*IM)->Index])
406f4a2713aSLionel Sambuc continue;
407f4a2713aSLionel Sambuc Visited[(*IM)->Index] = true;
408f4a2713aSLionel Sambuc
409f4a2713aSLionel Sambuc if (visitDepthFirst(**IM, Visitor, UserData, Visited))
410f4a2713aSLionel Sambuc return true;
411f4a2713aSLionel Sambuc }
412f4a2713aSLionel Sambuc
413f4a2713aSLionel Sambuc // Postorder visitation
414f4a2713aSLionel Sambuc return Visitor(M, /*Preorder=*/false, UserData);
415f4a2713aSLionel Sambuc }
416f4a2713aSLionel Sambuc
visitDepthFirst(bool (* Visitor)(ModuleFile & M,bool Preorder,void * UserData),void * UserData)417f4a2713aSLionel Sambuc void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
418f4a2713aSLionel Sambuc void *UserData),
419f4a2713aSLionel Sambuc void *UserData) {
420f4a2713aSLionel Sambuc SmallVector<bool, 16> Visited(size(), false);
421f4a2713aSLionel Sambuc for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
422f4a2713aSLionel Sambuc if (Visited[Chain[I]->Index])
423f4a2713aSLionel Sambuc continue;
424f4a2713aSLionel Sambuc Visited[Chain[I]->Index] = true;
425f4a2713aSLionel Sambuc
426f4a2713aSLionel Sambuc if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
427f4a2713aSLionel Sambuc return;
428f4a2713aSLionel Sambuc }
429f4a2713aSLionel Sambuc }
430f4a2713aSLionel Sambuc
lookupModuleFile(StringRef FileName,off_t ExpectedSize,time_t ExpectedModTime,const FileEntry * & File)431f4a2713aSLionel Sambuc bool ModuleManager::lookupModuleFile(StringRef FileName,
432f4a2713aSLionel Sambuc off_t ExpectedSize,
433f4a2713aSLionel Sambuc time_t ExpectedModTime,
434f4a2713aSLionel Sambuc const FileEntry *&File) {
435*0a6a1f1dSLionel Sambuc // Open the file immediately to ensure there is no race between stat'ing and
436*0a6a1f1dSLionel Sambuc // opening the file.
437*0a6a1f1dSLionel Sambuc File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
438f4a2713aSLionel Sambuc
439f4a2713aSLionel Sambuc if (!File && FileName != "-") {
440f4a2713aSLionel Sambuc return false;
441f4a2713aSLionel Sambuc }
442f4a2713aSLionel Sambuc
443f4a2713aSLionel Sambuc if ((ExpectedSize && ExpectedSize != File->getSize()) ||
444*0a6a1f1dSLionel Sambuc (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
445*0a6a1f1dSLionel Sambuc // Do not destroy File, as it may be referenced. If we need to rebuild it,
446*0a6a1f1dSLionel Sambuc // it will be destroyed by removeModules.
447f4a2713aSLionel Sambuc return true;
448f4a2713aSLionel Sambuc
449f4a2713aSLionel Sambuc return false;
450f4a2713aSLionel Sambuc }
451f4a2713aSLionel Sambuc
452f4a2713aSLionel Sambuc #ifndef NDEBUG
453f4a2713aSLionel Sambuc namespace llvm {
454f4a2713aSLionel Sambuc template<>
455f4a2713aSLionel Sambuc struct GraphTraits<ModuleManager> {
456f4a2713aSLionel Sambuc typedef ModuleFile NodeType;
457f4a2713aSLionel Sambuc typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
458f4a2713aSLionel Sambuc typedef ModuleManager::ModuleConstIterator nodes_iterator;
459f4a2713aSLionel Sambuc
child_beginllvm::GraphTraits460f4a2713aSLionel Sambuc static ChildIteratorType child_begin(NodeType *Node) {
461f4a2713aSLionel Sambuc return Node->Imports.begin();
462f4a2713aSLionel Sambuc }
463f4a2713aSLionel Sambuc
child_endllvm::GraphTraits464f4a2713aSLionel Sambuc static ChildIteratorType child_end(NodeType *Node) {
465f4a2713aSLionel Sambuc return Node->Imports.end();
466f4a2713aSLionel Sambuc }
467f4a2713aSLionel Sambuc
nodes_beginllvm::GraphTraits468f4a2713aSLionel Sambuc static nodes_iterator nodes_begin(const ModuleManager &Manager) {
469f4a2713aSLionel Sambuc return Manager.begin();
470f4a2713aSLionel Sambuc }
471f4a2713aSLionel Sambuc
nodes_endllvm::GraphTraits472f4a2713aSLionel Sambuc static nodes_iterator nodes_end(const ModuleManager &Manager) {
473f4a2713aSLionel Sambuc return Manager.end();
474f4a2713aSLionel Sambuc }
475f4a2713aSLionel Sambuc };
476f4a2713aSLionel Sambuc
477f4a2713aSLionel Sambuc template<>
478f4a2713aSLionel Sambuc struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
DOTGraphTraitsllvm::DOTGraphTraits479f4a2713aSLionel Sambuc explicit DOTGraphTraits(bool IsSimple = false)
480f4a2713aSLionel Sambuc : DefaultDOTGraphTraits(IsSimple) { }
481f4a2713aSLionel Sambuc
renderGraphFromBottomUpllvm::DOTGraphTraits482f4a2713aSLionel Sambuc static bool renderGraphFromBottomUp() {
483f4a2713aSLionel Sambuc return true;
484f4a2713aSLionel Sambuc }
485f4a2713aSLionel Sambuc
getNodeLabelllvm::DOTGraphTraits486f4a2713aSLionel Sambuc std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
487*0a6a1f1dSLionel Sambuc return M->ModuleName;
488f4a2713aSLionel Sambuc }
489f4a2713aSLionel Sambuc };
490f4a2713aSLionel Sambuc }
491f4a2713aSLionel Sambuc
viewGraph()492f4a2713aSLionel Sambuc void ModuleManager::viewGraph() {
493f4a2713aSLionel Sambuc llvm::ViewGraph(*this, "Modules");
494f4a2713aSLionel Sambuc }
495f4a2713aSLionel Sambuc #endif
496