10b57cec5SDimitry Andric //===- ModuleManager.cpp - Module Manager ---------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file defines the ModuleManager class, which manages a set of loaded 100b57cec5SDimitry Andric // modules for the ASTReader. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include "clang/Serialization/ModuleManager.h" 150b57cec5SDimitry Andric #include "clang/Basic/FileManager.h" 160b57cec5SDimitry Andric #include "clang/Basic/LLVM.h" 170b57cec5SDimitry Andric #include "clang/Lex/HeaderSearch.h" 180b57cec5SDimitry Andric #include "clang/Lex/ModuleMap.h" 190b57cec5SDimitry Andric #include "clang/Serialization/GlobalModuleIndex.h" 200b57cec5SDimitry Andric #include "clang/Serialization/InMemoryModuleCache.h" 21480093f4SDimitry Andric #include "clang/Serialization/ModuleFile.h" 220b57cec5SDimitry Andric #include "clang/Serialization/PCHContainerOperations.h" 230b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 240b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h" 250b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 260b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 270b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 280b57cec5SDimitry Andric #include "llvm/ADT/iterator.h" 290b57cec5SDimitry Andric #include "llvm/Support/Chrono.h" 300b57cec5SDimitry Andric #include "llvm/Support/DOTGraphTraits.h" 310b57cec5SDimitry Andric #include "llvm/Support/ErrorOr.h" 320b57cec5SDimitry Andric #include "llvm/Support/GraphWriter.h" 330b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h" 340b57cec5SDimitry Andric #include "llvm/Support/VirtualFileSystem.h" 350b57cec5SDimitry Andric #include <algorithm> 360b57cec5SDimitry Andric #include <cassert> 370b57cec5SDimitry Andric #include <memory> 380b57cec5SDimitry Andric #include <string> 390b57cec5SDimitry Andric #include <system_error> 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric using namespace clang; 420b57cec5SDimitry Andric using namespace serialization; 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric ModuleFile *ModuleManager::lookupByFileName(StringRef Name) const { 45a7dea167SDimitry Andric auto Entry = FileMgr.getFile(Name, /*OpenFile=*/false, 460b57cec5SDimitry Andric /*CacheFailure=*/false); 470b57cec5SDimitry Andric if (Entry) 48a7dea167SDimitry Andric return lookup(*Entry); 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric return nullptr; 510b57cec5SDimitry Andric } 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric ModuleFile *ModuleManager::lookupByModuleName(StringRef Name) const { 540b57cec5SDimitry Andric if (const Module *Mod = HeaderSearchInfo.getModuleMap().findModule(Name)) 550b57cec5SDimitry Andric if (const FileEntry *File = Mod->getASTFile()) 560b57cec5SDimitry Andric return lookup(File); 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric return nullptr; 590b57cec5SDimitry Andric } 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric ModuleFile *ModuleManager::lookup(const FileEntry *File) const { 62*fe013be4SDimitry Andric return Modules.lookup(File); 630b57cec5SDimitry Andric } 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric std::unique_ptr<llvm::MemoryBuffer> 660b57cec5SDimitry Andric ModuleManager::lookupBuffer(StringRef Name) { 67a7dea167SDimitry Andric auto Entry = FileMgr.getFile(Name, /*OpenFile=*/false, 680b57cec5SDimitry Andric /*CacheFailure=*/false); 69a7dea167SDimitry Andric if (!Entry) 70a7dea167SDimitry Andric return nullptr; 71a7dea167SDimitry Andric return std::move(InMemoryBuffers[*Entry]); 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric static bool checkSignature(ASTFileSignature Signature, 750b57cec5SDimitry Andric ASTFileSignature ExpectedSignature, 760b57cec5SDimitry Andric std::string &ErrorStr) { 770b57cec5SDimitry Andric if (!ExpectedSignature || Signature == ExpectedSignature) 780b57cec5SDimitry Andric return false; 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric ErrorStr = 810b57cec5SDimitry Andric Signature ? "signature mismatch" : "could not read module signature"; 820b57cec5SDimitry Andric return true; 830b57cec5SDimitry Andric } 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric static void updateModuleImports(ModuleFile &MF, ModuleFile *ImportedBy, 860b57cec5SDimitry Andric SourceLocation ImportLoc) { 870b57cec5SDimitry Andric if (ImportedBy) { 880b57cec5SDimitry Andric MF.ImportedBy.insert(ImportedBy); 890b57cec5SDimitry Andric ImportedBy->Imports.insert(&MF); 900b57cec5SDimitry Andric } else { 910b57cec5SDimitry Andric if (!MF.DirectlyImported) 920b57cec5SDimitry Andric MF.ImportLoc = ImportLoc; 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric MF.DirectlyImported = true; 950b57cec5SDimitry Andric } 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric ModuleManager::AddModuleResult 990b57cec5SDimitry Andric ModuleManager::addModule(StringRef FileName, ModuleKind Type, 1000b57cec5SDimitry Andric SourceLocation ImportLoc, ModuleFile *ImportedBy, 1010b57cec5SDimitry Andric unsigned Generation, 1020b57cec5SDimitry Andric off_t ExpectedSize, time_t ExpectedModTime, 1030b57cec5SDimitry Andric ASTFileSignature ExpectedSignature, 1040b57cec5SDimitry Andric ASTFileSignatureReader ReadSignature, 1050b57cec5SDimitry Andric ModuleFile *&Module, 1060b57cec5SDimitry Andric std::string &ErrorStr) { 1070b57cec5SDimitry Andric Module = nullptr; 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric // Look for the file entry. This only fails if the expected size or 1100b57cec5SDimitry Andric // modification time differ. 111e8d8bef9SDimitry Andric OptionalFileEntryRefDegradesToFileEntryPtr Entry; 1120b57cec5SDimitry Andric if (Type == MK_ExplicitModule || Type == MK_PrebuiltModule) { 1130b57cec5SDimitry Andric // If we're not expecting to pull this file out of the module cache, it 1140b57cec5SDimitry Andric // might have a different mtime due to being moved across filesystems in 1150b57cec5SDimitry Andric // a distributed build. The size must still match, though. (As must the 1160b57cec5SDimitry Andric // contents, but we can't check that.) 1170b57cec5SDimitry Andric ExpectedModTime = 0; 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric // Note: ExpectedSize and ExpectedModTime will be 0 for MK_ImplicitModule 1200b57cec5SDimitry Andric // when using an ASTFileSignature. 1210b57cec5SDimitry Andric if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) { 1220b57cec5SDimitry Andric ErrorStr = "module file out of date"; 1230b57cec5SDimitry Andric return OutOfDate; 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric if (!Entry && FileName != "-") { 1270b57cec5SDimitry Andric ErrorStr = "module file not found"; 1280b57cec5SDimitry Andric return Missing; 1290b57cec5SDimitry Andric } 1300b57cec5SDimitry Andric 131e8d8bef9SDimitry Andric // The ModuleManager's use of FileEntry nodes as the keys for its map of 132e8d8bef9SDimitry Andric // loaded modules is less than ideal. Uniqueness for FileEntry nodes is 133e8d8bef9SDimitry Andric // maintained by FileManager, which in turn uses inode numbers on hosts 134e8d8bef9SDimitry Andric // that support that. When coupled with the module cache's proclivity for 135e8d8bef9SDimitry Andric // turning over and deleting stale PCMs, this means entries for different 136e8d8bef9SDimitry Andric // module files can wind up reusing the same underlying inode. When this 137e8d8bef9SDimitry Andric // happens, subsequent accesses to the Modules map will disagree on the 138e8d8bef9SDimitry Andric // ModuleFile associated with a given file. In general, it is not sufficient 139e8d8bef9SDimitry Andric // to resolve this conundrum with a type like FileEntryRef that stores the 140e8d8bef9SDimitry Andric // name of the FileEntry node on first access because of path canonicalization 141e8d8bef9SDimitry Andric // issues. However, the paths constructed for implicit module builds are 142e8d8bef9SDimitry Andric // fully under Clang's control. We *can*, therefore, rely on their structure 143e8d8bef9SDimitry Andric // being consistent across operating systems and across subsequent accesses 144e8d8bef9SDimitry Andric // to the Modules map. 145e8d8bef9SDimitry Andric auto implicitModuleNamesMatch = [](ModuleKind Kind, const ModuleFile *MF, 146e8d8bef9SDimitry Andric const FileEntry *Entry) -> bool { 147e8d8bef9SDimitry Andric if (Kind != MK_ImplicitModule) 148e8d8bef9SDimitry Andric return true; 149e8d8bef9SDimitry Andric return Entry->getName() == MF->FileName; 150e8d8bef9SDimitry Andric }; 151e8d8bef9SDimitry Andric 1520b57cec5SDimitry Andric // Check whether we already loaded this module, before 1530b57cec5SDimitry Andric if (ModuleFile *ModuleEntry = Modules.lookup(Entry)) { 154e8d8bef9SDimitry Andric if (implicitModuleNamesMatch(Type, ModuleEntry, Entry)) { 1550b57cec5SDimitry Andric // Check the stored signature. 1560b57cec5SDimitry Andric if (checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr)) 1570b57cec5SDimitry Andric return OutOfDate; 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric Module = ModuleEntry; 1600b57cec5SDimitry Andric updateModuleImports(*ModuleEntry, ImportedBy, ImportLoc); 1610b57cec5SDimitry Andric return AlreadyLoaded; 1620b57cec5SDimitry Andric } 163e8d8bef9SDimitry Andric } 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric // Allocate a new module. 166a7dea167SDimitry Andric auto NewModule = std::make_unique<ModuleFile>(Type, Generation); 1670b57cec5SDimitry Andric NewModule->Index = Chain.size(); 1680b57cec5SDimitry Andric NewModule->FileName = FileName.str(); 1690b57cec5SDimitry Andric NewModule->File = Entry; 1700b57cec5SDimitry Andric NewModule->ImportLoc = ImportLoc; 1710b57cec5SDimitry Andric NewModule->InputFilesValidationTimestamp = 0; 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric if (NewModule->Kind == MK_ImplicitModule) { 1740b57cec5SDimitry Andric std::string TimestampFilename = NewModule->getTimestampFilename(); 1750b57cec5SDimitry Andric llvm::vfs::Status Status; 1760b57cec5SDimitry Andric // A cached stat value would be fine as well. 1770b57cec5SDimitry Andric if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status)) 1780b57cec5SDimitry Andric NewModule->InputFilesValidationTimestamp = 1790b57cec5SDimitry Andric llvm::sys::toTimeT(Status.getLastModificationTime()); 1800b57cec5SDimitry Andric } 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric // Load the contents of the module 1830b57cec5SDimitry Andric if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) { 1840b57cec5SDimitry Andric // The buffer was already provided for us. 1850b57cec5SDimitry Andric NewModule->Buffer = &ModuleCache->addBuiltPCM(FileName, std::move(Buffer)); 1860b57cec5SDimitry Andric // Since the cached buffer is reused, it is safe to close the file 1870b57cec5SDimitry Andric // descriptor that was opened while stat()ing the PCM in 1880b57cec5SDimitry Andric // lookupModuleFile() above, it won't be needed any longer. 1890b57cec5SDimitry Andric Entry->closeFile(); 1900b57cec5SDimitry Andric } else if (llvm::MemoryBuffer *Buffer = 1910b57cec5SDimitry Andric getModuleCache().lookupPCM(FileName)) { 1920b57cec5SDimitry Andric NewModule->Buffer = Buffer; 1930b57cec5SDimitry Andric // As above, the file descriptor is no longer needed. 1940b57cec5SDimitry Andric Entry->closeFile(); 1950b57cec5SDimitry Andric } else if (getModuleCache().shouldBuildPCM(FileName)) { 1960b57cec5SDimitry Andric // Report that the module is out of date, since we tried (and failed) to 1970b57cec5SDimitry Andric // import it earlier. 1980b57cec5SDimitry Andric Entry->closeFile(); 1990b57cec5SDimitry Andric return OutOfDate; 2000b57cec5SDimitry Andric } else { 2010b57cec5SDimitry Andric // Open the AST file. 2020b57cec5SDimitry Andric llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf((std::error_code())); 2030b57cec5SDimitry Andric if (FileName == "-") { 2040b57cec5SDimitry Andric Buf = llvm::MemoryBuffer::getSTDIN(); 2050b57cec5SDimitry Andric } else { 2060b57cec5SDimitry Andric // Get a buffer of the file and close the file descriptor when done. 2075ffd83dbSDimitry Andric // The file is volatile because in a parallel build we expect multiple 2085ffd83dbSDimitry Andric // compiler processes to use the same module file rebuilding it if needed. 2095ffd83dbSDimitry Andric // 2105ffd83dbSDimitry Andric // RequiresNullTerminator is false because module files don't need it, and 2115ffd83dbSDimitry Andric // this allows the file to still be mmapped. 2125ffd83dbSDimitry Andric Buf = FileMgr.getBufferForFile(NewModule->File, 2135ffd83dbSDimitry Andric /*IsVolatile=*/true, 2145ffd83dbSDimitry Andric /*RequiresNullTerminator=*/false); 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric if (!Buf) { 2180b57cec5SDimitry Andric ErrorStr = Buf.getError().message(); 2190b57cec5SDimitry Andric return Missing; 2200b57cec5SDimitry Andric } 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric NewModule->Buffer = &getModuleCache().addPCM(FileName, std::move(*Buf)); 2230b57cec5SDimitry Andric } 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric // Initialize the stream. 2260b57cec5SDimitry Andric NewModule->Data = PCHContainerRdr.ExtractPCH(*NewModule->Buffer); 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric // Read the signature eagerly now so that we can check it. Avoid calling 2290b57cec5SDimitry Andric // ReadSignature unless there's something to check though. 2300b57cec5SDimitry Andric if (ExpectedSignature && checkSignature(ReadSignature(NewModule->Data), 231a7dea167SDimitry Andric ExpectedSignature, ErrorStr)) 2320b57cec5SDimitry Andric return OutOfDate; 2330b57cec5SDimitry Andric 2340b57cec5SDimitry Andric // We're keeping this module. Store it everywhere. 2350b57cec5SDimitry Andric Module = Modules[Entry] = NewModule.get(); 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric updateModuleImports(*NewModule, ImportedBy, ImportLoc); 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric if (!NewModule->isModule()) 2400b57cec5SDimitry Andric PCHChain.push_back(NewModule.get()); 2410b57cec5SDimitry Andric if (!ImportedBy) 2420b57cec5SDimitry Andric Roots.push_back(NewModule.get()); 2430b57cec5SDimitry Andric 2440b57cec5SDimitry Andric Chain.push_back(std::move(NewModule)); 2450b57cec5SDimitry Andric return NewlyLoaded; 2460b57cec5SDimitry Andric } 2470b57cec5SDimitry Andric 248bdd1243dSDimitry Andric void ModuleManager::removeModules(ModuleIterator First) { 2490b57cec5SDimitry Andric auto Last = end(); 2500b57cec5SDimitry Andric if (First == Last) 2510b57cec5SDimitry Andric return; 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric // Explicitly clear VisitOrder since we might not notice it is stale. 2540b57cec5SDimitry Andric VisitOrder.clear(); 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric // Collect the set of module file pointers that we'll be removing. 2570b57cec5SDimitry Andric llvm::SmallPtrSet<ModuleFile *, 4> victimSet( 2580b57cec5SDimitry Andric (llvm::pointer_iterator<ModuleIterator>(First)), 2590b57cec5SDimitry Andric (llvm::pointer_iterator<ModuleIterator>(Last))); 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric auto IsVictim = [&](ModuleFile *MF) { 2620b57cec5SDimitry Andric return victimSet.count(MF); 2630b57cec5SDimitry Andric }; 2640b57cec5SDimitry Andric // Remove any references to the now-destroyed modules. 2650b57cec5SDimitry Andric for (auto I = begin(); I != First; ++I) { 2660b57cec5SDimitry Andric I->Imports.remove_if(IsVictim); 2670b57cec5SDimitry Andric I->ImportedBy.remove_if(IsVictim); 2680b57cec5SDimitry Andric } 269349cc55cSDimitry Andric llvm::erase_if(Roots, IsVictim); 2700b57cec5SDimitry Andric 2710b57cec5SDimitry Andric // Remove the modules from the PCH chain. 2720b57cec5SDimitry Andric for (auto I = First; I != Last; ++I) { 2730b57cec5SDimitry Andric if (!I->isModule()) { 2740b57cec5SDimitry Andric PCHChain.erase(llvm::find(PCHChain, &*I), PCHChain.end()); 2750b57cec5SDimitry Andric break; 2760b57cec5SDimitry Andric } 2770b57cec5SDimitry Andric } 2780b57cec5SDimitry Andric 279bdd1243dSDimitry Andric // Delete the modules. 280bdd1243dSDimitry Andric for (ModuleIterator victim = First; victim != Last; ++victim) 2810b57cec5SDimitry Andric Modules.erase(victim->File); 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric Chain.erase(Chain.begin() + (First - begin()), Chain.end()); 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric void 2870b57cec5SDimitry Andric ModuleManager::addInMemoryBuffer(StringRef FileName, 2880b57cec5SDimitry Andric std::unique_ptr<llvm::MemoryBuffer> Buffer) { 2890b57cec5SDimitry Andric const FileEntry *Entry = 2900b57cec5SDimitry Andric FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0); 2910b57cec5SDimitry Andric InMemoryBuffers[Entry] = std::move(Buffer); 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric 29404eeddc0SDimitry Andric std::unique_ptr<ModuleManager::VisitState> ModuleManager::allocateVisitState() { 2950b57cec5SDimitry Andric // Fast path: if we have a cached state, use it. 2960b57cec5SDimitry Andric if (FirstVisitState) { 29704eeddc0SDimitry Andric auto Result = std::move(FirstVisitState); 29804eeddc0SDimitry Andric FirstVisitState = std::move(Result->NextState); 2990b57cec5SDimitry Andric return Result; 3000b57cec5SDimitry Andric } 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric // Allocate and return a new state. 30304eeddc0SDimitry Andric return std::make_unique<VisitState>(size()); 3040b57cec5SDimitry Andric } 3050b57cec5SDimitry Andric 30604eeddc0SDimitry Andric void ModuleManager::returnVisitState(std::unique_ptr<VisitState> State) { 3070b57cec5SDimitry Andric assert(State->NextState == nullptr && "Visited state is in list?"); 30804eeddc0SDimitry Andric State->NextState = std::move(FirstVisitState); 30904eeddc0SDimitry Andric FirstVisitState = std::move(State); 3100b57cec5SDimitry Andric } 3110b57cec5SDimitry Andric 3120b57cec5SDimitry Andric void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) { 3130b57cec5SDimitry Andric GlobalIndex = Index; 3140b57cec5SDimitry Andric if (!GlobalIndex) { 3150b57cec5SDimitry Andric ModulesInCommonWithGlobalIndex.clear(); 3160b57cec5SDimitry Andric return; 3170b57cec5SDimitry Andric } 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric // Notify the global module index about all of the modules we've already 3200b57cec5SDimitry Andric // loaded. 3210b57cec5SDimitry Andric for (ModuleFile &M : *this) 3220b57cec5SDimitry Andric if (!GlobalIndex->loadedModuleFile(&M)) 3230b57cec5SDimitry Andric ModulesInCommonWithGlobalIndex.push_back(&M); 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric void ModuleManager::moduleFileAccepted(ModuleFile *MF) { 3270b57cec5SDimitry Andric if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF)) 3280b57cec5SDimitry Andric return; 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric ModulesInCommonWithGlobalIndex.push_back(MF); 3310b57cec5SDimitry Andric } 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric ModuleManager::ModuleManager(FileManager &FileMgr, 3340b57cec5SDimitry Andric InMemoryModuleCache &ModuleCache, 3350b57cec5SDimitry Andric const PCHContainerReader &PCHContainerRdr, 3360b57cec5SDimitry Andric const HeaderSearch &HeaderSearchInfo) 3370b57cec5SDimitry Andric : FileMgr(FileMgr), ModuleCache(&ModuleCache), 3380b57cec5SDimitry Andric PCHContainerRdr(PCHContainerRdr), HeaderSearchInfo(HeaderSearchInfo) {} 3390b57cec5SDimitry Andric 3400b57cec5SDimitry Andric void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor, 3410b57cec5SDimitry Andric llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) { 3420b57cec5SDimitry Andric // If the visitation order vector is the wrong size, recompute the order. 3430b57cec5SDimitry Andric if (VisitOrder.size() != Chain.size()) { 3440b57cec5SDimitry Andric unsigned N = size(); 3450b57cec5SDimitry Andric VisitOrder.clear(); 3460b57cec5SDimitry Andric VisitOrder.reserve(N); 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric // Record the number of incoming edges for each module. When we 3490b57cec5SDimitry Andric // encounter a module with no incoming edges, push it into the queue 3500b57cec5SDimitry Andric // to seed the queue. 3510b57cec5SDimitry Andric SmallVector<ModuleFile *, 4> Queue; 3520b57cec5SDimitry Andric Queue.reserve(N); 3530b57cec5SDimitry Andric llvm::SmallVector<unsigned, 4> UnusedIncomingEdges; 3540b57cec5SDimitry Andric UnusedIncomingEdges.resize(size()); 3550b57cec5SDimitry Andric for (ModuleFile &M : llvm::reverse(*this)) { 3560b57cec5SDimitry Andric unsigned Size = M.ImportedBy.size(); 3570b57cec5SDimitry Andric UnusedIncomingEdges[M.Index] = Size; 3580b57cec5SDimitry Andric if (!Size) 3590b57cec5SDimitry Andric Queue.push_back(&M); 3600b57cec5SDimitry Andric } 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric // Traverse the graph, making sure to visit a module before visiting any 3630b57cec5SDimitry Andric // of its dependencies. 3640b57cec5SDimitry Andric while (!Queue.empty()) { 3650b57cec5SDimitry Andric ModuleFile *CurrentModule = Queue.pop_back_val(); 3660b57cec5SDimitry Andric VisitOrder.push_back(CurrentModule); 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric // For any module that this module depends on, push it on the 3690b57cec5SDimitry Andric // stack (if it hasn't already been marked as visited). 370349cc55cSDimitry Andric for (ModuleFile *M : llvm::reverse(CurrentModule->Imports)) { 3710b57cec5SDimitry Andric // Remove our current module as an impediment to visiting the 3720b57cec5SDimitry Andric // module we depend on. If we were the last unvisited module 3730b57cec5SDimitry Andric // that depends on this particular module, push it into the 3740b57cec5SDimitry Andric // queue to be visited. 375349cc55cSDimitry Andric unsigned &NumUnusedEdges = UnusedIncomingEdges[M->Index]; 3760b57cec5SDimitry Andric if (NumUnusedEdges && (--NumUnusedEdges == 0)) 377349cc55cSDimitry Andric Queue.push_back(M); 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric assert(VisitOrder.size() == N && "Visitation order is wrong?"); 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric FirstVisitState = nullptr; 3840b57cec5SDimitry Andric } 3850b57cec5SDimitry Andric 38604eeddc0SDimitry Andric auto State = allocateVisitState(); 3870b57cec5SDimitry Andric unsigned VisitNumber = State->NextVisitNumber++; 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric // If the caller has provided us with a hit-set that came from the global 3900b57cec5SDimitry Andric // module index, mark every module file in common with the global module 3910b57cec5SDimitry Andric // index that is *not* in that set as 'visited'. 3920b57cec5SDimitry Andric if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) { 3930b57cec5SDimitry Andric for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I) 3940b57cec5SDimitry Andric { 3950b57cec5SDimitry Andric ModuleFile *M = ModulesInCommonWithGlobalIndex[I]; 3960b57cec5SDimitry Andric if (!ModuleFilesHit->count(M)) 3970b57cec5SDimitry Andric State->VisitNumber[M->Index] = VisitNumber; 3980b57cec5SDimitry Andric } 3990b57cec5SDimitry Andric } 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) { 4020b57cec5SDimitry Andric ModuleFile *CurrentModule = VisitOrder[I]; 4030b57cec5SDimitry Andric // Should we skip this module file? 4040b57cec5SDimitry Andric if (State->VisitNumber[CurrentModule->Index] == VisitNumber) 4050b57cec5SDimitry Andric continue; 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric // Visit the module. 4080b57cec5SDimitry Andric assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1); 4090b57cec5SDimitry Andric State->VisitNumber[CurrentModule->Index] = VisitNumber; 4100b57cec5SDimitry Andric if (!Visitor(*CurrentModule)) 4110b57cec5SDimitry Andric continue; 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric // The visitor has requested that cut off visitation of any 4140b57cec5SDimitry Andric // module that the current module depends on. To indicate this 4150b57cec5SDimitry Andric // behavior, we mark all of the reachable modules as having been visited. 4160b57cec5SDimitry Andric ModuleFile *NextModule = CurrentModule; 4170b57cec5SDimitry Andric do { 4180b57cec5SDimitry Andric // For any module that this module depends on, push it on the 4190b57cec5SDimitry Andric // stack (if it hasn't already been marked as visited). 4200b57cec5SDimitry Andric for (llvm::SetVector<ModuleFile *>::iterator 4210b57cec5SDimitry Andric M = NextModule->Imports.begin(), 4220b57cec5SDimitry Andric MEnd = NextModule->Imports.end(); 4230b57cec5SDimitry Andric M != MEnd; ++M) { 4240b57cec5SDimitry Andric if (State->VisitNumber[(*M)->Index] != VisitNumber) { 4250b57cec5SDimitry Andric State->Stack.push_back(*M); 4260b57cec5SDimitry Andric State->VisitNumber[(*M)->Index] = VisitNumber; 4270b57cec5SDimitry Andric } 4280b57cec5SDimitry Andric } 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andric if (State->Stack.empty()) 4310b57cec5SDimitry Andric break; 4320b57cec5SDimitry Andric 4330b57cec5SDimitry Andric // Pop the next module off the stack. 4340b57cec5SDimitry Andric NextModule = State->Stack.pop_back_val(); 4350b57cec5SDimitry Andric } while (true); 4360b57cec5SDimitry Andric } 4370b57cec5SDimitry Andric 43804eeddc0SDimitry Andric returnVisitState(std::move(State)); 4390b57cec5SDimitry Andric } 4400b57cec5SDimitry Andric 441e8d8bef9SDimitry Andric bool ModuleManager::lookupModuleFile(StringRef FileName, off_t ExpectedSize, 4420b57cec5SDimitry Andric time_t ExpectedModTime, 443bdd1243dSDimitry Andric OptionalFileEntryRef &File) { 444bdd1243dSDimitry Andric File = std::nullopt; 445e8d8bef9SDimitry Andric if (FileName == "-") 4460b57cec5SDimitry Andric return false; 4470b57cec5SDimitry Andric 4480b57cec5SDimitry Andric // Open the file immediately to ensure there is no race between stat'ing and 4490b57cec5SDimitry Andric // opening the file. 450bdd1243dSDimitry Andric OptionalFileEntryRef FileOrErr = 451e8d8bef9SDimitry Andric expectedToOptional(FileMgr.getFileRef(FileName, /*OpenFile=*/true, 452e8d8bef9SDimitry Andric /*CacheFailure=*/false)); 453e8d8bef9SDimitry Andric if (!FileOrErr) 4540b57cec5SDimitry Andric return false; 455e8d8bef9SDimitry Andric 456a7dea167SDimitry Andric File = *FileOrErr; 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric if ((ExpectedSize && ExpectedSize != File->getSize()) || 4590b57cec5SDimitry Andric (ExpectedModTime && ExpectedModTime != File->getModificationTime())) 4600b57cec5SDimitry Andric // Do not destroy File, as it may be referenced. If we need to rebuild it, 4610b57cec5SDimitry Andric // it will be destroyed by removeModules. 4620b57cec5SDimitry Andric return true; 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric return false; 4650b57cec5SDimitry Andric } 4660b57cec5SDimitry Andric 4670b57cec5SDimitry Andric #ifndef NDEBUG 4680b57cec5SDimitry Andric namespace llvm { 4690b57cec5SDimitry Andric 4700b57cec5SDimitry Andric template<> 4710b57cec5SDimitry Andric struct GraphTraits<ModuleManager> { 4720b57cec5SDimitry Andric using NodeRef = ModuleFile *; 4730b57cec5SDimitry Andric using ChildIteratorType = llvm::SetVector<ModuleFile *>::const_iterator; 4740b57cec5SDimitry Andric using nodes_iterator = pointer_iterator<ModuleManager::ModuleConstIterator>; 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric static ChildIteratorType child_begin(NodeRef Node) { 4770b57cec5SDimitry Andric return Node->Imports.begin(); 4780b57cec5SDimitry Andric } 4790b57cec5SDimitry Andric 4800b57cec5SDimitry Andric static ChildIteratorType child_end(NodeRef Node) { 4810b57cec5SDimitry Andric return Node->Imports.end(); 4820b57cec5SDimitry Andric } 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric static nodes_iterator nodes_begin(const ModuleManager &Manager) { 4850b57cec5SDimitry Andric return nodes_iterator(Manager.begin()); 4860b57cec5SDimitry Andric } 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric static nodes_iterator nodes_end(const ModuleManager &Manager) { 4890b57cec5SDimitry Andric return nodes_iterator(Manager.end()); 4900b57cec5SDimitry Andric } 4910b57cec5SDimitry Andric }; 4920b57cec5SDimitry Andric 4930b57cec5SDimitry Andric template<> 4940b57cec5SDimitry Andric struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits { 4950b57cec5SDimitry Andric explicit DOTGraphTraits(bool IsSimple = false) 4960b57cec5SDimitry Andric : DefaultDOTGraphTraits(IsSimple) {} 4970b57cec5SDimitry Andric 4980b57cec5SDimitry Andric static bool renderGraphFromBottomUp() { return true; } 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andric std::string getNodeLabel(ModuleFile *M, const ModuleManager&) { 5010b57cec5SDimitry Andric return M->ModuleName; 5020b57cec5SDimitry Andric } 5030b57cec5SDimitry Andric }; 5040b57cec5SDimitry Andric 5050b57cec5SDimitry Andric } // namespace llvm 5060b57cec5SDimitry Andric 5070b57cec5SDimitry Andric void ModuleManager::viewGraph() { 5080b57cec5SDimitry Andric llvm::ViewGraph(*this, "Modules"); 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric #endif 511