1d44252ecSDouglas Gregor //===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===// 2d44252ecSDouglas Gregor // 3d44252ecSDouglas Gregor // The LLVM Compiler Infrastructure 4d44252ecSDouglas Gregor // 5d44252ecSDouglas Gregor // This file is distributed under the University of Illinois Open Source 6d44252ecSDouglas Gregor // License. See LICENSE.TXT for details. 7d44252ecSDouglas Gregor // 8d44252ecSDouglas Gregor //===----------------------------------------------------------------------===// 9d44252ecSDouglas Gregor // 10d44252ecSDouglas Gregor // This file defines the ModuleManager class, which manages a set of loaded 11d44252ecSDouglas Gregor // modules for the ASTReader. 12d44252ecSDouglas Gregor // 13d44252ecSDouglas Gregor //===----------------------------------------------------------------------===// 149670f847SMehdi Amini #include "clang/Serialization/ModuleManager.h" 15bb165fb0SAdrian Prantl #include "clang/Frontend/PCHContainerOperations.h" 16beee15e7SBen Langmuir #include "clang/Lex/HeaderSearch.h" 177029ce1aSDouglas Gregor #include "clang/Lex/ModuleMap.h" 187211ac15SDouglas Gregor #include "clang/Serialization/GlobalModuleIndex.h" 19d44252ecSDouglas Gregor #include "llvm/Support/MemoryBuffer.h" 20552c169eSRafael Espindola #include "llvm/Support/Path.h" 218a8e554aSRafael Espindola #include <system_error> 22d44252ecSDouglas Gregor 239d7c1a2aSDouglas Gregor #ifndef NDEBUG 249d7c1a2aSDouglas Gregor #include "llvm/Support/GraphWriter.h" 259d7c1a2aSDouglas Gregor #endif 269d7c1a2aSDouglas Gregor 27d44252ecSDouglas Gregor using namespace clang; 28d44252ecSDouglas Gregor using namespace serialization; 29d44252ecSDouglas Gregor 30*37a93df3SRichard Smith ModuleFile *ModuleManager::lookup(StringRef Name) const { 31dadd85dcSDouglas Gregor const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false, 32dadd85dcSDouglas Gregor /*cacheFailure=*/false); 33bf7fc9c5SDouglas Gregor if (Entry) 34bf7fc9c5SDouglas Gregor return lookup(Entry); 35bf7fc9c5SDouglas Gregor 36a13603a2SCraig Topper return nullptr; 37bf7fc9c5SDouglas Gregor } 38bf7fc9c5SDouglas Gregor 39*37a93df3SRichard Smith ModuleFile *ModuleManager::lookup(const FileEntry *File) const { 40*37a93df3SRichard Smith auto Known = Modules.find(File); 41bf7fc9c5SDouglas Gregor if (Known == Modules.end()) 42a13603a2SCraig Topper return nullptr; 43bf7fc9c5SDouglas Gregor 44bf7fc9c5SDouglas Gregor return Known->second; 45d44252ecSDouglas Gregor } 46d44252ecSDouglas Gregor 475cd06f26SRafael Espindola std::unique_ptr<llvm::MemoryBuffer> 485cd06f26SRafael Espindola ModuleManager::lookupBuffer(StringRef Name) { 49dadd85dcSDouglas Gregor const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false, 50dadd85dcSDouglas Gregor /*cacheFailure=*/false); 515cd06f26SRafael Espindola return std::move(InMemoryBuffers[Entry]); 52d44252ecSDouglas Gregor } 53d44252ecSDouglas Gregor 5414afc8e7SDuncan P. N. Exon Smith static bool checkSignature(ASTFileSignature Signature, 5514afc8e7SDuncan P. N. Exon Smith ASTFileSignature ExpectedSignature, 5614afc8e7SDuncan P. N. Exon Smith std::string &ErrorStr) { 5714afc8e7SDuncan P. N. Exon Smith if (!ExpectedSignature || Signature == ExpectedSignature) 5814afc8e7SDuncan P. N. Exon Smith return false; 5914afc8e7SDuncan P. N. Exon Smith 6014afc8e7SDuncan P. N. Exon Smith ErrorStr = 6114afc8e7SDuncan P. N. Exon Smith Signature ? "signature mismatch" : "could not read module signature"; 6214afc8e7SDuncan P. N. Exon Smith return true; 6314afc8e7SDuncan P. N. Exon Smith } 6414afc8e7SDuncan P. N. Exon Smith 6526308a68SDuncan P. N. Exon Smith static void updateModuleImports(ModuleFile &MF, ModuleFile *ImportedBy, 6626308a68SDuncan P. N. Exon Smith SourceLocation ImportLoc) { 6726308a68SDuncan P. N. Exon Smith if (ImportedBy) { 6826308a68SDuncan P. N. Exon Smith MF.ImportedBy.insert(ImportedBy); 6926308a68SDuncan P. N. Exon Smith ImportedBy->Imports.insert(&MF); 7026308a68SDuncan P. N. Exon Smith } else { 7126308a68SDuncan P. N. Exon Smith if (!MF.DirectlyImported) 7226308a68SDuncan P. N. Exon Smith MF.ImportLoc = ImportLoc; 7326308a68SDuncan P. N. Exon Smith 7426308a68SDuncan P. N. Exon Smith MF.DirectlyImported = true; 7526308a68SDuncan P. N. Exon Smith } 7626308a68SDuncan P. N. Exon Smith } 7726308a68SDuncan P. N. Exon Smith 787029ce1aSDouglas Gregor ModuleManager::AddModuleResult 79d44252ecSDouglas Gregor ModuleManager::addModule(StringRef FileName, ModuleKind Type, 806fb03aeaSDouglas Gregor SourceLocation ImportLoc, ModuleFile *ImportedBy, 817029ce1aSDouglas Gregor unsigned Generation, 827029ce1aSDouglas Gregor off_t ExpectedSize, time_t ExpectedModTime, 83487ea14aSBen Langmuir ASTFileSignature ExpectedSignature, 8470a1b816SBen Langmuir ASTFileSignatureReader ReadSignature, 857029ce1aSDouglas Gregor ModuleFile *&Module, 867029ce1aSDouglas Gregor std::string &ErrorStr) { 87a13603a2SCraig Topper Module = nullptr; 887029ce1aSDouglas Gregor 897029ce1aSDouglas Gregor // Look for the file entry. This only fails if the expected size or 907029ce1aSDouglas Gregor // modification time differ. 917029ce1aSDouglas Gregor const FileEntry *Entry; 9211f2a477SManman Ren if (Type == MK_ExplicitModule || Type == MK_PrebuiltModule) { 935b390756SRichard Smith // If we're not expecting to pull this file out of the module cache, it 945b390756SRichard Smith // might have a different mtime due to being moved across filesystems in 955b390756SRichard Smith // a distributed build. The size must still match, though. (As must the 965b390756SRichard Smith // contents, but we can't check that.) 975b390756SRichard Smith ExpectedModTime = 0; 985b390756SRichard Smith } 99c27d0d5eSEli Friedman if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) { 100c27d0d5eSEli Friedman ErrorStr = "module file out of date"; 1017029ce1aSDouglas Gregor return OutOfDate; 102c27d0d5eSEli Friedman } 1037029ce1aSDouglas Gregor 104d44252ecSDouglas Gregor if (!Entry && FileName != "-") { 105c27d0d5eSEli Friedman ErrorStr = "module file not found"; 1067029ce1aSDouglas Gregor return Missing; 107d44252ecSDouglas Gregor } 108d44252ecSDouglas Gregor 109d44252ecSDouglas Gregor // Check whether we already loaded this module, before 11026308a68SDuncan P. N. Exon Smith if (ModuleFile *ModuleEntry = Modules.lookup(Entry)) { 11126308a68SDuncan P. N. Exon Smith // Check the stored signature. 11226308a68SDuncan P. N. Exon Smith if (checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr)) 11326308a68SDuncan P. N. Exon Smith return OutOfDate; 11426308a68SDuncan P. N. Exon Smith 11526308a68SDuncan P. N. Exon Smith Module = ModuleEntry; 11626308a68SDuncan P. N. Exon Smith updateModuleImports(*ModuleEntry, ImportedBy, ImportLoc); 11726308a68SDuncan P. N. Exon Smith return AlreadyLoaded; 11826308a68SDuncan P. N. Exon Smith } 11926308a68SDuncan P. N. Exon Smith 120d44252ecSDouglas Gregor // Allocate a new module. 12126308a68SDuncan P. N. Exon Smith auto NewModule = llvm::make_unique<ModuleFile>(Type, Generation); 122a897f7cdSDuncan P. N. Exon Smith NewModule->Index = Chain.size(); 123a897f7cdSDuncan P. N. Exon Smith NewModule->FileName = FileName.str(); 124a897f7cdSDuncan P. N. Exon Smith NewModule->File = Entry; 125a897f7cdSDuncan P. N. Exon Smith NewModule->ImportLoc = ImportLoc; 126a897f7cdSDuncan P. N. Exon Smith NewModule->InputFilesValidationTimestamp = 0; 127d44252ecSDouglas Gregor 128a897f7cdSDuncan P. N. Exon Smith if (NewModule->Kind == MK_ImplicitModule) { 129a897f7cdSDuncan P. N. Exon Smith std::string TimestampFilename = NewModule->getTimestampFilename(); 130c8130a74SBen Langmuir vfs::Status Status; 131f430da4dSDmitri Gribenko // A cached stat value would be fine as well. 132f430da4dSDmitri Gribenko if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status)) 133a897f7cdSDuncan P. N. Exon Smith NewModule->InputFilesValidationTimestamp = 134ac71c8e2SPavel Labath llvm::sys::toTimeT(Status.getLastModificationTime()); 135f430da4dSDmitri Gribenko } 136f430da4dSDmitri Gribenko 137d44252ecSDouglas Gregor // Load the contents of the module 1385cd06f26SRafael Espindola if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) { 139d44252ecSDouglas Gregor // The buffer was already provided for us. 140a897f7cdSDuncan P. N. Exon Smith NewModule->Buffer = std::move(Buffer); 141d44252ecSDouglas Gregor } else { 142d44252ecSDouglas Gregor // Open the AST file. 14326308a68SDuncan P. N. Exon Smith llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf((std::error_code())); 144d44252ecSDouglas Gregor if (FileName == "-") { 145a885796dSBenjamin Kramer Buf = llvm::MemoryBuffer::getSTDIN(); 1469801b253SBen Langmuir } else { 1479801b253SBen Langmuir // Leave the FileEntry open so if it gets read again by another 1489801b253SBen Langmuir // ModuleManager it must be the same underlying file. 1499801b253SBen Langmuir // FIXME: Because FileManager::getFile() doesn't guarantee that it will 1509801b253SBen Langmuir // give us an open file, this may not be 100% reliable. 151a897f7cdSDuncan P. N. Exon Smith Buf = FileMgr.getBufferForFile(NewModule->File, 152a885796dSBenjamin Kramer /*IsVolatile=*/false, 153a885796dSBenjamin Kramer /*ShouldClose=*/false); 1549801b253SBen Langmuir } 155d44252ecSDouglas Gregor 156a885796dSBenjamin Kramer if (!Buf) { 157a885796dSBenjamin Kramer ErrorStr = Buf.getError().message(); 1587029ce1aSDouglas Gregor return Missing; 159d44252ecSDouglas Gregor } 160d44252ecSDouglas Gregor 161a897f7cdSDuncan P. N. Exon Smith NewModule->Buffer = std::move(*Buf); 162a885796dSBenjamin Kramer } 163a885796dSBenjamin Kramer 164bb165fb0SAdrian Prantl // Initialize the stream. 165a897f7cdSDuncan P. N. Exon Smith NewModule->Data = PCHContainerRdr.ExtractPCH(*NewModule->Buffer); 166487ea14aSBen Langmuir 167688b69adSDuncan P. N. Exon Smith // Read the signature eagerly now so that we can check it. Avoid calling 168688b69adSDuncan P. N. Exon Smith // ReadSignature unless there's something to check though. 169688b69adSDuncan P. N. Exon Smith if (ExpectedSignature && checkSignature(ReadSignature(NewModule->Data), 170688b69adSDuncan P. N. Exon Smith ExpectedSignature, ErrorStr)) 171ed982584SBen Langmuir return OutOfDate; 172a897f7cdSDuncan P. N. Exon Smith 17326308a68SDuncan P. N. Exon Smith // We're keeping this module. Store it everywhere. 17426308a68SDuncan P. N. Exon Smith Module = Modules[Entry] = NewModule.get(); 175d44252ecSDouglas Gregor 17626308a68SDuncan P. N. Exon Smith updateModuleImports(*NewModule, ImportedBy, ImportLoc); 1776fb03aeaSDouglas Gregor 17826308a68SDuncan P. N. Exon Smith if (!NewModule->isModule()) 17926308a68SDuncan P. N. Exon Smith PCHChain.push_back(NewModule.get()); 18026308a68SDuncan P. N. Exon Smith if (!ImportedBy) 18126308a68SDuncan P. N. Exon Smith Roots.push_back(NewModule.get()); 1823b99db55SRichard Smith 183a897f7cdSDuncan P. N. Exon Smith Chain.push_back(std::move(NewModule)); 1843b99db55SRichard Smith return NewlyLoaded; 185d44252ecSDouglas Gregor } 186d44252ecSDouglas Gregor 1879801b253SBen Langmuir void ModuleManager::removeModules( 1888e6bc197SDuncan P. N. Exon Smith ModuleIterator First, 1899801b253SBen Langmuir llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully, 1907029ce1aSDouglas Gregor ModuleMap *modMap) { 1918e6bc197SDuncan P. N. Exon Smith auto Last = end(); 1928e6bc197SDuncan P. N. Exon Smith if (First == Last) 193188dbef2SDouglas Gregor return; 194188dbef2SDouglas Gregor 1958e6bc197SDuncan P. N. Exon Smith 196a50dbb20SBen Langmuir // Explicitly clear VisitOrder since we might not notice it is stale. 197a50dbb20SBen Langmuir VisitOrder.clear(); 198a50dbb20SBen Langmuir 199188dbef2SDouglas Gregor // Collect the set of module file pointers that we'll be removing. 20096a06e0eSDuncan P. N. Exon Smith llvm::SmallPtrSet<ModuleFile *, 4> victimSet( 2018e6bc197SDuncan P. N. Exon Smith (llvm::pointer_iterator<ModuleIterator>(First)), 2028e6bc197SDuncan P. N. Exon Smith (llvm::pointer_iterator<ModuleIterator>(Last))); 203188dbef2SDouglas Gregor 2049eff8b14SManuel Klimek auto IsVictim = [&](ModuleFile *MF) { 2059eff8b14SManuel Klimek return victimSet.count(MF); 2069eff8b14SManuel Klimek }; 207188dbef2SDouglas Gregor // Remove any references to the now-destroyed modules. 208073ec350SDuncan P. N. Exon Smith for (auto I = begin(); I != First; ++I) { 209073ec350SDuncan P. N. Exon Smith I->Imports.remove_if(IsVictim); 2108e6bc197SDuncan P. N. Exon Smith I->ImportedBy.remove_if(IsVictim); 211073ec350SDuncan P. N. Exon Smith } 2129eff8b14SManuel Klimek Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim), 2139eff8b14SManuel Klimek Roots.end()); 214188dbef2SDouglas Gregor 21516fe4d17SRichard Smith // Remove the modules from the PCH chain. 2168e6bc197SDuncan P. N. Exon Smith for (auto I = First; I != Last; ++I) { 21796a06e0eSDuncan P. N. Exon Smith if (!I->isModule()) { 21896a06e0eSDuncan P. N. Exon Smith PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), &*I), 21916fe4d17SRichard Smith PCHChain.end()); 22016fe4d17SRichard Smith break; 22116fe4d17SRichard Smith } 22216fe4d17SRichard Smith } 22316fe4d17SRichard Smith 224188dbef2SDouglas Gregor // Delete the modules and erase them from the various structures. 2258e6bc197SDuncan P. N. Exon Smith for (ModuleIterator victim = First; victim != Last; ++victim) { 22696a06e0eSDuncan P. N. Exon Smith Modules.erase(victim->File); 227ca39214fSBen Langmuir 2287029ce1aSDouglas Gregor if (modMap) { 22996a06e0eSDuncan P. N. Exon Smith StringRef ModuleName = victim->ModuleName; 2307029ce1aSDouglas Gregor if (Module *mod = modMap->findModule(ModuleName)) { 231a13603a2SCraig Topper mod->setASTFile(nullptr); 2327029ce1aSDouglas Gregor } 2337029ce1aSDouglas Gregor } 2344f05478cSBen Langmuir 2359801b253SBen Langmuir // Files that didn't make it through ReadASTCore successfully will be 2369801b253SBen Langmuir // rebuilt (or there was an error). Invalidate them so that we can load the 2379801b253SBen Langmuir // new files that will be renamed over the old ones. 23896a06e0eSDuncan P. N. Exon Smith if (LoadedSuccessfully.count(&*victim) == 0) 23996a06e0eSDuncan P. N. Exon Smith FileMgr.invalidateCache(victim->File); 240188dbef2SDouglas Gregor } 241188dbef2SDouglas Gregor 242a897f7cdSDuncan P. N. Exon Smith // Delete the modules. 2438e6bc197SDuncan P. N. Exon Smith Chain.erase(Chain.begin() + (First - begin()), Chain.end()); 244188dbef2SDouglas Gregor } 245188dbef2SDouglas Gregor 2465cd06f26SRafael Espindola void 2475cd06f26SRafael Espindola ModuleManager::addInMemoryBuffer(StringRef FileName, 2485cd06f26SRafael Espindola std::unique_ptr<llvm::MemoryBuffer> Buffer) { 249d44252ecSDouglas Gregor 2505cd06f26SRafael Espindola const FileEntry *Entry = 2515cd06f26SRafael Espindola FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0); 2525cd06f26SRafael Espindola InMemoryBuffers[Entry] = std::move(Buffer); 253d44252ecSDouglas Gregor } 254d44252ecSDouglas Gregor 255e97cd90aSDouglas Gregor ModuleManager::VisitState *ModuleManager::allocateVisitState() { 256e97cd90aSDouglas Gregor // Fast path: if we have a cached state, use it. 257e97cd90aSDouglas Gregor if (FirstVisitState) { 258e97cd90aSDouglas Gregor VisitState *Result = FirstVisitState; 259e97cd90aSDouglas Gregor FirstVisitState = FirstVisitState->NextState; 260a13603a2SCraig Topper Result->NextState = nullptr; 261e97cd90aSDouglas Gregor return Result; 262e97cd90aSDouglas Gregor } 263e97cd90aSDouglas Gregor 264e97cd90aSDouglas Gregor // Allocate and return a new state. 265e97cd90aSDouglas Gregor return new VisitState(size()); 266e97cd90aSDouglas Gregor } 267e97cd90aSDouglas Gregor 268e97cd90aSDouglas Gregor void ModuleManager::returnVisitState(VisitState *State) { 269a13603a2SCraig Topper assert(State->NextState == nullptr && "Visited state is in list?"); 270e97cd90aSDouglas Gregor State->NextState = FirstVisitState; 271e97cd90aSDouglas Gregor FirstVisitState = State; 272e97cd90aSDouglas Gregor } 273e97cd90aSDouglas Gregor 2747211ac15SDouglas Gregor void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) { 2757211ac15SDouglas Gregor GlobalIndex = Index; 276603cd869SDouglas Gregor if (!GlobalIndex) { 277603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.clear(); 278603cd869SDouglas Gregor return; 2797029ce1aSDouglas Gregor } 280603cd869SDouglas Gregor 281603cd869SDouglas Gregor // Notify the global module index about all of the modules we've already 282603cd869SDouglas Gregor // loaded. 283a897f7cdSDuncan P. N. Exon Smith for (ModuleFile &M : *this) 284a897f7cdSDuncan P. N. Exon Smith if (!GlobalIndex->loadedModuleFile(&M)) 285a897f7cdSDuncan P. N. Exon Smith ModulesInCommonWithGlobalIndex.push_back(&M); 286603cd869SDouglas Gregor } 287603cd869SDouglas Gregor 288603cd869SDouglas Gregor void ModuleManager::moduleFileAccepted(ModuleFile *MF) { 289603cd869SDouglas Gregor if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF)) 290603cd869SDouglas Gregor return; 291603cd869SDouglas Gregor 292603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(MF); 2937211ac15SDouglas Gregor } 2947211ac15SDouglas Gregor 295bb165fb0SAdrian Prantl ModuleManager::ModuleManager(FileManager &FileMgr, 296fb2398d0SAdrian Prantl const PCHContainerReader &PCHContainerRdr) 297fb2398d0SAdrian Prantl : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr), GlobalIndex(), 298bb165fb0SAdrian Prantl FirstVisitState(nullptr) {} 299d44252ecSDouglas Gregor 300a897f7cdSDuncan P. N. Exon Smith ModuleManager::~ModuleManager() { delete FirstVisitState; } 301d44252ecSDouglas Gregor 3029a9efbafSBenjamin Kramer void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor, 3034dd9b43cSCraig Topper llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) { 3047211ac15SDouglas Gregor // If the visitation order vector is the wrong size, recompute the order. 305e41d7feaSDouglas Gregor if (VisitOrder.size() != Chain.size()) { 306d44252ecSDouglas Gregor unsigned N = size(); 307e41d7feaSDouglas Gregor VisitOrder.clear(); 308e41d7feaSDouglas Gregor VisitOrder.reserve(N); 309d44252ecSDouglas Gregor 310d44252ecSDouglas Gregor // Record the number of incoming edges for each module. When we 311d44252ecSDouglas Gregor // encounter a module with no incoming edges, push it into the queue 312d44252ecSDouglas Gregor // to seed the queue. 313de3ef502SDouglas Gregor SmallVector<ModuleFile *, 4> Queue; 314d44252ecSDouglas Gregor Queue.reserve(N); 315bdb259d2SDouglas Gregor llvm::SmallVector<unsigned, 4> UnusedIncomingEdges; 316a7c535b3SRichard Smith UnusedIncomingEdges.resize(size()); 31796a06e0eSDuncan P. N. Exon Smith for (ModuleFile &M : llvm::reverse(*this)) { 31896a06e0eSDuncan P. N. Exon Smith unsigned Size = M.ImportedBy.size(); 31996a06e0eSDuncan P. N. Exon Smith UnusedIncomingEdges[M.Index] = Size; 320a7c535b3SRichard Smith if (!Size) 32196a06e0eSDuncan P. N. Exon Smith Queue.push_back(&M); 322d44252ecSDouglas Gregor } 323d44252ecSDouglas Gregor 324e41d7feaSDouglas Gregor // Traverse the graph, making sure to visit a module before visiting any 325e41d7feaSDouglas Gregor // of its dependencies. 326a7c535b3SRichard Smith while (!Queue.empty()) { 327a7c535b3SRichard Smith ModuleFile *CurrentModule = Queue.pop_back_val(); 328e41d7feaSDouglas Gregor VisitOrder.push_back(CurrentModule); 329d44252ecSDouglas Gregor 330d44252ecSDouglas Gregor // For any module that this module depends on, push it on the 331d44252ecSDouglas Gregor // stack (if it hasn't already been marked as visited). 332a7c535b3SRichard Smith for (auto M = CurrentModule->Imports.rbegin(), 333a7c535b3SRichard Smith MEnd = CurrentModule->Imports.rend(); 334d44252ecSDouglas Gregor M != MEnd; ++M) { 335d44252ecSDouglas Gregor // Remove our current module as an impediment to visiting the 336d44252ecSDouglas Gregor // module we depend on. If we were the last unvisited module 337d44252ecSDouglas Gregor // that depends on this particular module, push it into the 338d44252ecSDouglas Gregor // queue to be visited. 339bdb259d2SDouglas Gregor unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index]; 340d44252ecSDouglas Gregor if (NumUnusedEdges && (--NumUnusedEdges == 0)) 341d44252ecSDouglas Gregor Queue.push_back(*M); 342d44252ecSDouglas Gregor } 343d44252ecSDouglas Gregor } 344e41d7feaSDouglas Gregor 345e41d7feaSDouglas Gregor assert(VisitOrder.size() == N && "Visitation order is wrong?"); 3467211ac15SDouglas Gregor 347e97cd90aSDouglas Gregor delete FirstVisitState; 348a13603a2SCraig Topper FirstVisitState = nullptr; 349e41d7feaSDouglas Gregor } 350e41d7feaSDouglas Gregor 351e97cd90aSDouglas Gregor VisitState *State = allocateVisitState(); 352e97cd90aSDouglas Gregor unsigned VisitNumber = State->NextVisitNumber++; 353e41d7feaSDouglas Gregor 3547211ac15SDouglas Gregor // If the caller has provided us with a hit-set that came from the global 3557211ac15SDouglas Gregor // module index, mark every module file in common with the global module 3567211ac15SDouglas Gregor // index that is *not* in that set as 'visited'. 3577211ac15SDouglas Gregor if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) { 3587211ac15SDouglas Gregor for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I) 3597211ac15SDouglas Gregor { 3607211ac15SDouglas Gregor ModuleFile *M = ModulesInCommonWithGlobalIndex[I]; 3617029ce1aSDouglas Gregor if (!ModuleFilesHit->count(M)) 362e97cd90aSDouglas Gregor State->VisitNumber[M->Index] = VisitNumber; 3637211ac15SDouglas Gregor } 3647211ac15SDouglas Gregor } 3657211ac15SDouglas Gregor 366e41d7feaSDouglas Gregor for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) { 367e41d7feaSDouglas Gregor ModuleFile *CurrentModule = VisitOrder[I]; 368e41d7feaSDouglas Gregor // Should we skip this module file? 369e97cd90aSDouglas Gregor if (State->VisitNumber[CurrentModule->Index] == VisitNumber) 370e41d7feaSDouglas Gregor continue; 371e41d7feaSDouglas Gregor 372e41d7feaSDouglas Gregor // Visit the module. 373e97cd90aSDouglas Gregor assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1); 374e97cd90aSDouglas Gregor State->VisitNumber[CurrentModule->Index] = VisitNumber; 3759a9efbafSBenjamin Kramer if (!Visitor(*CurrentModule)) 376e41d7feaSDouglas Gregor continue; 377e41d7feaSDouglas Gregor 378e41d7feaSDouglas Gregor // The visitor has requested that cut off visitation of any 379e41d7feaSDouglas Gregor // module that the current module depends on. To indicate this 380e41d7feaSDouglas Gregor // behavior, we mark all of the reachable modules as having been visited. 381e41d7feaSDouglas Gregor ModuleFile *NextModule = CurrentModule; 382e41d7feaSDouglas Gregor do { 383e41d7feaSDouglas Gregor // For any module that this module depends on, push it on the 384e41d7feaSDouglas Gregor // stack (if it hasn't already been marked as visited). 385e41d7feaSDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 386e41d7feaSDouglas Gregor M = NextModule->Imports.begin(), 387e41d7feaSDouglas Gregor MEnd = NextModule->Imports.end(); 388e41d7feaSDouglas Gregor M != MEnd; ++M) { 389e97cd90aSDouglas Gregor if (State->VisitNumber[(*M)->Index] != VisitNumber) { 390e97cd90aSDouglas Gregor State->Stack.push_back(*M); 391e97cd90aSDouglas Gregor State->VisitNumber[(*M)->Index] = VisitNumber; 392e41d7feaSDouglas Gregor } 393e41d7feaSDouglas Gregor } 394e41d7feaSDouglas Gregor 395e97cd90aSDouglas Gregor if (State->Stack.empty()) 396e41d7feaSDouglas Gregor break; 397e41d7feaSDouglas Gregor 398e41d7feaSDouglas Gregor // Pop the next module off the stack. 39925284cc9SRobert Wilhelm NextModule = State->Stack.pop_back_val(); 400e41d7feaSDouglas Gregor } while (true); 401e41d7feaSDouglas Gregor } 402e97cd90aSDouglas Gregor 403e97cd90aSDouglas Gregor returnVisitState(State); 404d44252ecSDouglas Gregor } 405d44252ecSDouglas Gregor 4067029ce1aSDouglas Gregor bool ModuleManager::lookupModuleFile(StringRef FileName, 4077029ce1aSDouglas Gregor off_t ExpectedSize, 4087029ce1aSDouglas Gregor time_t ExpectedModTime, 4097029ce1aSDouglas Gregor const FileEntry *&File) { 4103bd6d7fbSRichard Smith if (FileName == "-") { 4113bd6d7fbSRichard Smith File = nullptr; 4123bd6d7fbSRichard Smith return false; 4133bd6d7fbSRichard Smith } 4143bd6d7fbSRichard Smith 41505f82ba2SBen Langmuir // Open the file immediately to ensure there is no race between stat'ing and 41605f82ba2SBen Langmuir // opening the file. 41705f82ba2SBen Langmuir File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false); 4183bd6d7fbSRichard Smith if (!File) 4197029ce1aSDouglas Gregor return false; 4207029ce1aSDouglas Gregor 4217029ce1aSDouglas Gregor if ((ExpectedSize && ExpectedSize != File->getSize()) || 422027731d7SBen Langmuir (ExpectedModTime && ExpectedModTime != File->getModificationTime())) 423027731d7SBen Langmuir // Do not destroy File, as it may be referenced. If we need to rebuild it, 424027731d7SBen Langmuir // it will be destroyed by removeModules. 4257029ce1aSDouglas Gregor return true; 4267029ce1aSDouglas Gregor 4277029ce1aSDouglas Gregor return false; 4287029ce1aSDouglas Gregor } 4297029ce1aSDouglas Gregor 4309d7c1a2aSDouglas Gregor #ifndef NDEBUG 4319d7c1a2aSDouglas Gregor namespace llvm { 4329d7c1a2aSDouglas Gregor template<> 4339d7c1a2aSDouglas Gregor struct GraphTraits<ModuleManager> { 4342931d171STim Shen typedef ModuleFile *NodeRef; 435de3ef502SDouglas Gregor typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType; 43696a06e0eSDuncan P. N. Exon Smith typedef pointer_iterator<ModuleManager::ModuleConstIterator> nodes_iterator; 4379d7c1a2aSDouglas Gregor 438f2187ed3STim Shen static ChildIteratorType child_begin(NodeRef Node) { 4399d7c1a2aSDouglas Gregor return Node->Imports.begin(); 4409d7c1a2aSDouglas Gregor } 4419d7c1a2aSDouglas Gregor 442f2187ed3STim Shen static ChildIteratorType child_end(NodeRef Node) { 4439d7c1a2aSDouglas Gregor return Node->Imports.end(); 4449d7c1a2aSDouglas Gregor } 4459d7c1a2aSDouglas Gregor 4469d7c1a2aSDouglas Gregor static nodes_iterator nodes_begin(const ModuleManager &Manager) { 44796a06e0eSDuncan P. N. Exon Smith return nodes_iterator(Manager.begin()); 4489d7c1a2aSDouglas Gregor } 4499d7c1a2aSDouglas Gregor 4509d7c1a2aSDouglas Gregor static nodes_iterator nodes_end(const ModuleManager &Manager) { 45196a06e0eSDuncan P. N. Exon Smith return nodes_iterator(Manager.end()); 4529d7c1a2aSDouglas Gregor } 4539d7c1a2aSDouglas Gregor }; 4549d7c1a2aSDouglas Gregor 4559d7c1a2aSDouglas Gregor template<> 4569d7c1a2aSDouglas Gregor struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits { 4579d7c1a2aSDouglas Gregor explicit DOTGraphTraits(bool IsSimple = false) 4589d7c1a2aSDouglas Gregor : DefaultDOTGraphTraits(IsSimple) { } 4599d7c1a2aSDouglas Gregor 4609d7c1a2aSDouglas Gregor static bool renderGraphFromBottomUp() { 4619d7c1a2aSDouglas Gregor return true; 4629d7c1a2aSDouglas Gregor } 4639d7c1a2aSDouglas Gregor 464de3ef502SDouglas Gregor std::string getNodeLabel(ModuleFile *M, const ModuleManager&) { 465beee15e7SBen Langmuir return M->ModuleName; 4669d7c1a2aSDouglas Gregor } 4679d7c1a2aSDouglas Gregor }; 468ab9db510SAlexander Kornienko } 4699d7c1a2aSDouglas Gregor 4709d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() { 4719d7c1a2aSDouglas Gregor llvm::ViewGraph(*this, "Modules"); 4729d7c1a2aSDouglas Gregor } 4739d7c1a2aSDouglas Gregor #endif 474