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