xref: /llvm-project/clang/lib/Serialization/ModuleManager.cpp (revision 96a06e0ec059330a18adc34531b6cc8125edf459)
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 static bool checkSignature(ASTFileSignature Signature,
56                            ASTFileSignature ExpectedSignature,
57                            std::string &ErrorStr) {
58   if (!ExpectedSignature || Signature == ExpectedSignature)
59     return false;
60 
61   ErrorStr =
62       Signature ? "signature mismatch" : "could not read module signature";
63   return true;
64 }
65 
66 ModuleManager::AddModuleResult
67 ModuleManager::addModule(StringRef FileName, ModuleKind Type,
68                          SourceLocation ImportLoc, ModuleFile *ImportedBy,
69                          unsigned Generation,
70                          off_t ExpectedSize, time_t ExpectedModTime,
71                          ASTFileSignature ExpectedSignature,
72                          ASTFileSignatureReader ReadSignature,
73                          ModuleFile *&Module,
74                          std::string &ErrorStr) {
75   Module = nullptr;
76 
77   // Look for the file entry. This only fails if the expected size or
78   // modification time differ.
79   const FileEntry *Entry;
80   if (Type == MK_ExplicitModule || Type == MK_PrebuiltModule) {
81     // If we're not expecting to pull this file out of the module cache, it
82     // might have a different mtime due to being moved across filesystems in
83     // a distributed build. The size must still match, though. (As must the
84     // contents, but we can't check that.)
85     ExpectedModTime = 0;
86   }
87   if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
88     ErrorStr = "module file out of date";
89     return OutOfDate;
90   }
91 
92   if (!Entry && FileName != "-") {
93     ErrorStr = "module file not found";
94     return Missing;
95   }
96 
97   // Check whether we already loaded this module, before
98   ModuleFile *ModuleEntry = Modules[Entry];
99   bool NewModule = false;
100   if (!ModuleEntry) {
101     // Allocate a new module.
102     NewModule = true;
103     ModuleEntry = new ModuleFile(Type, Generation);
104     ModuleEntry->Index = Chain.size();
105     ModuleEntry->FileName = FileName.str();
106     ModuleEntry->File = Entry;
107     ModuleEntry->ImportLoc = ImportLoc;
108     ModuleEntry->InputFilesValidationTimestamp = 0;
109 
110     if (ModuleEntry->Kind == MK_ImplicitModule) {
111       std::string TimestampFilename = ModuleEntry->getTimestampFilename();
112       vfs::Status Status;
113       // A cached stat value would be fine as well.
114       if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
115         ModuleEntry->InputFilesValidationTimestamp =
116             llvm::sys::toTimeT(Status.getLastModificationTime());
117     }
118 
119     // Load the contents of the module
120     if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
121       // The buffer was already provided for us.
122       ModuleEntry->Buffer = std::move(Buffer);
123     } else {
124       // Open the AST file.
125       llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf(
126           (std::error_code()));
127       if (FileName == "-") {
128         Buf = llvm::MemoryBuffer::getSTDIN();
129       } else {
130         // Leave the FileEntry open so if it gets read again by another
131         // ModuleManager it must be the same underlying file.
132         // FIXME: Because FileManager::getFile() doesn't guarantee that it will
133         // give us an open file, this may not be 100% reliable.
134         Buf = FileMgr.getBufferForFile(ModuleEntry->File,
135                                        /*IsVolatile=*/false,
136                                        /*ShouldClose=*/false);
137       }
138 
139       if (!Buf) {
140         ErrorStr = Buf.getError().message();
141         delete ModuleEntry;
142         return Missing;
143       }
144 
145       ModuleEntry->Buffer = std::move(*Buf);
146     }
147 
148     // Initialize the stream.
149     ModuleEntry->Data = PCHContainerRdr.ExtractPCH(*ModuleEntry->Buffer);
150 
151     // Read the signature eagerly now so that we can check it.
152     if (checkSignature(ReadSignature(ModuleEntry->Data), ExpectedSignature, ErrorStr)) {
153       delete ModuleEntry;
154       return OutOfDate;
155     }
156   } else if (checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr)) {
157     return OutOfDate;
158   }
159 
160   if (ImportedBy) {
161     ModuleEntry->ImportedBy.insert(ImportedBy);
162     ImportedBy->Imports.insert(ModuleEntry);
163   } else {
164     if (!ModuleEntry->DirectlyImported)
165       ModuleEntry->ImportLoc = ImportLoc;
166 
167     ModuleEntry->DirectlyImported = true;
168   }
169 
170   Module = ModuleEntry;
171 
172   if (!NewModule)
173     return AlreadyLoaded;
174 
175   assert(!Modules[Entry] && "module loaded twice");
176   Modules[Entry] = ModuleEntry;
177 
178   Chain.push_back(ModuleEntry);
179   if (!ModuleEntry->isModule())
180     PCHChain.push_back(ModuleEntry);
181   if (!ImportedBy)
182     Roots.push_back(ModuleEntry);
183 
184   return NewlyLoaded;
185 }
186 
187 void ModuleManager::removeModules(
188     ModuleIterator first, ModuleIterator last,
189     llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
190     ModuleMap *modMap) {
191   if (first == last)
192     return;
193 
194   // Explicitly clear VisitOrder since we might not notice it is stale.
195   VisitOrder.clear();
196 
197   // Collect the set of module file pointers that we'll be removing.
198   llvm::SmallPtrSet<ModuleFile *, 4> victimSet(
199       (llvm::pointer_iterator<ModuleIterator>(first)),
200       (llvm::pointer_iterator<ModuleIterator>(last)));
201 
202   auto IsVictim = [&](ModuleFile *MF) {
203     return victimSet.count(MF);
204   };
205   // Remove any references to the now-destroyed modules.
206   for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
207     Chain[i]->ImportedBy.remove_if(IsVictim);
208   }
209   Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim),
210               Roots.end());
211 
212   // Remove the modules from the PCH chain.
213   for (auto I = first; I != last; ++I) {
214     if (!I->isModule()) {
215       PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), &*I),
216                      PCHChain.end());
217       break;
218     }
219   }
220 
221   // Delete the modules and erase them from the various structures.
222   for (ModuleIterator victim = first; victim != last; ++victim) {
223     Modules.erase(victim->File);
224 
225     if (modMap) {
226       StringRef ModuleName = victim->ModuleName;
227       if (Module *mod = modMap->findModule(ModuleName)) {
228         mod->setASTFile(nullptr);
229       }
230     }
231 
232     // Files that didn't make it through ReadASTCore successfully will be
233     // rebuilt (or there was an error). Invalidate them so that we can load the
234     // new files that will be renamed over the old ones.
235     if (LoadedSuccessfully.count(&*victim) == 0)
236       FileMgr.invalidateCache(victim->File);
237 
238     delete &*victim;
239   }
240 
241   // Remove the modules from the chain.
242   Chain.erase(Chain.begin() + (first - begin()),
243               Chain.begin() + (last - begin()));
244 }
245 
246 void
247 ModuleManager::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);
253 }
254 
255 ModuleManager::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;
260     Result->NextState = nullptr;
261     return Result;
262   }
263 
264   // Allocate and return a new state.
265   return new VisitState(size());
266 }
267 
268 void ModuleManager::returnVisitState(VisitState *State) {
269   assert(State->NextState == nullptr && "Visited state is in list?");
270   State->NextState = FirstVisitState;
271   FirstVisitState = State;
272 }
273 
274 void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
275   GlobalIndex = Index;
276   if (!GlobalIndex) {
277     ModulesInCommonWithGlobalIndex.clear();
278     return;
279   }
280 
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 
290 void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
291   if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
292     return;
293 
294   ModulesInCommonWithGlobalIndex.push_back(MF);
295 }
296 
297 ModuleManager::ModuleManager(FileManager &FileMgr,
298                              const PCHContainerReader &PCHContainerRdr)
299     : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr), GlobalIndex(),
300       FirstVisitState(nullptr) {}
301 
302 ModuleManager::~ModuleManager() {
303   for (unsigned i = 0, e = Chain.size(); i != e; ++i)
304     delete Chain[e - i - 1];
305   delete FirstVisitState;
306 }
307 
308 void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
309                           llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
310   // If the visitation order vector is the wrong size, recompute the order.
311   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;
322     UnusedIncomingEdges.resize(size());
323     for (ModuleFile &M : llvm::reverse(*this)) {
324       unsigned Size = M.ImportedBy.size();
325       UnusedIncomingEdges[M.Index] = Size;
326       if (!Size)
327         Queue.push_back(&M);
328     }
329 
330     // Traverse the graph, making sure to visit a module before visiting any
331     // of its dependencies.
332     while (!Queue.empty()) {
333       ModuleFile *CurrentModule = Queue.pop_back_val();
334       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).
338       for (auto M = CurrentModule->Imports.rbegin(),
339                 MEnd = CurrentModule->Imports.rend();
340            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?");
352 
353     delete FirstVisitState;
354     FirstVisitState = nullptr;
355   }
356 
357   VisitState *State = allocateVisitState();
358   unsigned VisitNumber = State->NextVisitNumber++;
359 
360   // 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];
367       if (!ModuleFilesHit->count(M))
368         State->VisitNumber[M->Index] = VisitNumber;
369     }
370   }
371 
372   for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
373     ModuleFile *CurrentModule = VisitOrder[I];
374     // Should we skip this module file?
375     if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
376       continue;
377 
378     // Visit the module.
379     assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
380     State->VisitNumber[CurrentModule->Index] = VisitNumber;
381     if (!Visitor(*CurrentModule))
382       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;
388     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) {
395         if (State->VisitNumber[(*M)->Index] != VisitNumber) {
396           State->Stack.push_back(*M);
397           State->VisitNumber[(*M)->Index] = VisitNumber;
398         }
399       }
400 
401       if (State->Stack.empty())
402         break;
403 
404       // Pop the next module off the stack.
405       NextModule = State->Stack.pop_back_val();
406     } while (true);
407   }
408 
409   returnVisitState(State);
410 }
411 
412 bool ModuleManager::lookupModuleFile(StringRef FileName,
413                                      off_t ExpectedSize,
414                                      time_t ExpectedModTime,
415                                      const FileEntry *&File) {
416   if (FileName == "-") {
417     File = nullptr;
418     return false;
419   }
420 
421   // Open the file immediately to ensure there is no race between stat'ing and
422   // opening the file.
423   File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
424   if (!File)
425     return false;
426 
427   if ((ExpectedSize && ExpectedSize != File->getSize()) ||
428       (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
429     // Do not destroy File, as it may be referenced. If we need to rebuild it,
430     // it will be destroyed by removeModules.
431     return true;
432 
433   return false;
434 }
435 
436 #ifndef NDEBUG
437 namespace llvm {
438   template<>
439   struct GraphTraits<ModuleManager> {
440     typedef ModuleFile *NodeRef;
441     typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
442     typedef pointer_iterator<ModuleManager::ModuleConstIterator> nodes_iterator;
443 
444     static ChildIteratorType child_begin(NodeRef Node) {
445       return Node->Imports.begin();
446     }
447 
448     static ChildIteratorType child_end(NodeRef Node) {
449       return Node->Imports.end();
450     }
451 
452     static nodes_iterator nodes_begin(const ModuleManager &Manager) {
453       return nodes_iterator(Manager.begin());
454     }
455 
456     static nodes_iterator nodes_end(const ModuleManager &Manager) {
457       return nodes_iterator(Manager.end());
458     }
459   };
460 
461   template<>
462   struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
463     explicit DOTGraphTraits(bool IsSimple = false)
464       : DefaultDOTGraphTraits(IsSimple) { }
465 
466     static bool renderGraphFromBottomUp() {
467       return true;
468     }
469 
470     std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
471       return M->ModuleName;
472     }
473   };
474 }
475 
476 void ModuleManager::viewGraph() {
477   llvm::ViewGraph(*this, "Modules");
478 }
479 #endif
480