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