Path: blob/main/contrib/llvm-project/clang/lib/Serialization/ModuleManager.cpp
35232 views
//===- ModuleManager.cpp - Module Manager ---------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines the ModuleManager class, which manages a set of loaded9// modules for the ASTReader.10//11//===----------------------------------------------------------------------===//1213#include "clang/Serialization/ModuleManager.h"14#include "clang/Basic/FileManager.h"15#include "clang/Basic/LLVM.h"16#include "clang/Lex/HeaderSearch.h"17#include "clang/Lex/ModuleMap.h"18#include "clang/Serialization/GlobalModuleIndex.h"19#include "clang/Serialization/InMemoryModuleCache.h"20#include "clang/Serialization/ModuleFile.h"21#include "clang/Serialization/PCHContainerOperations.h"22#include "llvm/ADT/STLExtras.h"23#include "llvm/ADT/SetVector.h"24#include "llvm/ADT/SmallPtrSet.h"25#include "llvm/ADT/SmallVector.h"26#include "llvm/ADT/StringRef.h"27#include "llvm/ADT/iterator.h"28#include "llvm/Support/Chrono.h"29#include "llvm/Support/DOTGraphTraits.h"30#include "llvm/Support/ErrorOr.h"31#include "llvm/Support/GraphWriter.h"32#include "llvm/Support/MemoryBuffer.h"33#include "llvm/Support/VirtualFileSystem.h"34#include <algorithm>35#include <cassert>36#include <memory>37#include <string>38#include <system_error>3940using namespace clang;41using namespace serialization;4243ModuleFile *ModuleManager::lookupByFileName(StringRef Name) const {44auto Entry = FileMgr.getFile(Name, /*OpenFile=*/false,45/*CacheFailure=*/false);46if (Entry)47return lookup(*Entry);4849return nullptr;50}5152ModuleFile *ModuleManager::lookupByModuleName(StringRef Name) const {53if (const Module *Mod = HeaderSearchInfo.getModuleMap().findModule(Name))54if (OptionalFileEntryRef File = Mod->getASTFile())55return lookup(*File);5657return nullptr;58}5960ModuleFile *ModuleManager::lookup(const FileEntry *File) const {61return Modules.lookup(File);62}6364std::unique_ptr<llvm::MemoryBuffer>65ModuleManager::lookupBuffer(StringRef Name) {66auto Entry = FileMgr.getFile(Name, /*OpenFile=*/false,67/*CacheFailure=*/false);68if (!Entry)69return nullptr;70return std::move(InMemoryBuffers[*Entry]);71}7273static bool checkSignature(ASTFileSignature Signature,74ASTFileSignature ExpectedSignature,75std::string &ErrorStr) {76if (!ExpectedSignature || Signature == ExpectedSignature)77return false;7879ErrorStr =80Signature ? "signature mismatch" : "could not read module signature";81return true;82}8384static void updateModuleImports(ModuleFile &MF, ModuleFile *ImportedBy,85SourceLocation ImportLoc) {86if (ImportedBy) {87MF.ImportedBy.insert(ImportedBy);88ImportedBy->Imports.insert(&MF);89} else {90if (!MF.DirectlyImported)91MF.ImportLoc = ImportLoc;9293MF.DirectlyImported = true;94}95}9697ModuleManager::AddModuleResult98ModuleManager::addModule(StringRef FileName, ModuleKind Type,99SourceLocation ImportLoc, ModuleFile *ImportedBy,100unsigned Generation,101off_t ExpectedSize, time_t ExpectedModTime,102ASTFileSignature ExpectedSignature,103ASTFileSignatureReader ReadSignature,104ModuleFile *&Module,105std::string &ErrorStr) {106Module = nullptr;107108// Look for the file entry. This only fails if the expected size or109// modification time differ.110OptionalFileEntryRef Entry;111if (Type == MK_ExplicitModule || Type == MK_PrebuiltModule) {112// If we're not expecting to pull this file out of the module cache, it113// might have a different mtime due to being moved across filesystems in114// a distributed build. The size must still match, though. (As must the115// contents, but we can't check that.)116ExpectedModTime = 0;117}118// Note: ExpectedSize and ExpectedModTime will be 0 for MK_ImplicitModule119// when using an ASTFileSignature.120if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {121ErrorStr = "module file out of date";122return OutOfDate;123}124125if (!Entry) {126ErrorStr = "module file not found";127return Missing;128}129130// The ModuleManager's use of FileEntry nodes as the keys for its map of131// loaded modules is less than ideal. Uniqueness for FileEntry nodes is132// maintained by FileManager, which in turn uses inode numbers on hosts133// that support that. When coupled with the module cache's proclivity for134// turning over and deleting stale PCMs, this means entries for different135// module files can wind up reusing the same underlying inode. When this136// happens, subsequent accesses to the Modules map will disagree on the137// ModuleFile associated with a given file. In general, it is not sufficient138// to resolve this conundrum with a type like FileEntryRef that stores the139// name of the FileEntry node on first access because of path canonicalization140// issues. However, the paths constructed for implicit module builds are141// fully under Clang's control. We *can*, therefore, rely on their structure142// being consistent across operating systems and across subsequent accesses143// to the Modules map.144auto implicitModuleNamesMatch = [](ModuleKind Kind, const ModuleFile *MF,145FileEntryRef Entry) -> bool {146if (Kind != MK_ImplicitModule)147return true;148return Entry.getName() == MF->FileName;149};150151// Check whether we already loaded this module, before152if (ModuleFile *ModuleEntry = Modules.lookup(*Entry)) {153if (implicitModuleNamesMatch(Type, ModuleEntry, *Entry)) {154// Check the stored signature.155if (checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr))156return OutOfDate;157158Module = ModuleEntry;159updateModuleImports(*ModuleEntry, ImportedBy, ImportLoc);160return AlreadyLoaded;161}162}163164// Allocate a new module.165auto NewModule = std::make_unique<ModuleFile>(Type, *Entry, Generation);166NewModule->Index = Chain.size();167NewModule->FileName = FileName.str();168NewModule->ImportLoc = ImportLoc;169NewModule->InputFilesValidationTimestamp = 0;170171if (NewModule->Kind == MK_ImplicitModule) {172std::string TimestampFilename = NewModule->getTimestampFilename();173llvm::vfs::Status Status;174// A cached stat value would be fine as well.175if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))176NewModule->InputFilesValidationTimestamp =177llvm::sys::toTimeT(Status.getLastModificationTime());178}179180// Load the contents of the module181if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {182// The buffer was already provided for us.183NewModule->Buffer = &ModuleCache->addBuiltPCM(FileName, std::move(Buffer));184// Since the cached buffer is reused, it is safe to close the file185// descriptor that was opened while stat()ing the PCM in186// lookupModuleFile() above, it won't be needed any longer.187Entry->closeFile();188} else if (llvm::MemoryBuffer *Buffer =189getModuleCache().lookupPCM(FileName)) {190NewModule->Buffer = Buffer;191// As above, the file descriptor is no longer needed.192Entry->closeFile();193} else if (getModuleCache().shouldBuildPCM(FileName)) {194// Report that the module is out of date, since we tried (and failed) to195// import it earlier.196Entry->closeFile();197return OutOfDate;198} else {199// Get a buffer of the file and close the file descriptor when done.200// The file is volatile because in a parallel build we expect multiple201// compiler processes to use the same module file rebuilding it if needed.202//203// RequiresNullTerminator is false because module files don't need it, and204// this allows the file to still be mmapped.205auto Buf = FileMgr.getBufferForFile(NewModule->File,206/*IsVolatile=*/true,207/*RequiresNullTerminator=*/false);208209if (!Buf) {210ErrorStr = Buf.getError().message();211return Missing;212}213214NewModule->Buffer = &getModuleCache().addPCM(FileName, std::move(*Buf));215}216217// Initialize the stream.218NewModule->Data = PCHContainerRdr.ExtractPCH(*NewModule->Buffer);219220// Read the signature eagerly now so that we can check it. Avoid calling221// ReadSignature unless there's something to check though.222if (ExpectedSignature && checkSignature(ReadSignature(NewModule->Data),223ExpectedSignature, ErrorStr))224return OutOfDate;225226// We're keeping this module. Store it everywhere.227Module = Modules[*Entry] = NewModule.get();228229updateModuleImports(*NewModule, ImportedBy, ImportLoc);230231if (!NewModule->isModule())232PCHChain.push_back(NewModule.get());233if (!ImportedBy)234Roots.push_back(NewModule.get());235236Chain.push_back(std::move(NewModule));237return NewlyLoaded;238}239240void ModuleManager::removeModules(ModuleIterator First) {241auto Last = end();242if (First == Last)243return;244245// Explicitly clear VisitOrder since we might not notice it is stale.246VisitOrder.clear();247248// Collect the set of module file pointers that we'll be removing.249llvm::SmallPtrSet<ModuleFile *, 4> victimSet(250(llvm::pointer_iterator<ModuleIterator>(First)),251(llvm::pointer_iterator<ModuleIterator>(Last)));252253auto IsVictim = [&](ModuleFile *MF) {254return victimSet.count(MF);255};256// Remove any references to the now-destroyed modules.257for (auto I = begin(); I != First; ++I) {258I->Imports.remove_if(IsVictim);259I->ImportedBy.remove_if(IsVictim);260}261llvm::erase_if(Roots, IsVictim);262263// Remove the modules from the PCH chain.264for (auto I = First; I != Last; ++I) {265if (!I->isModule()) {266PCHChain.erase(llvm::find(PCHChain, &*I), PCHChain.end());267break;268}269}270271// Delete the modules.272for (ModuleIterator victim = First; victim != Last; ++victim)273Modules.erase(victim->File);274275Chain.erase(Chain.begin() + (First - begin()), Chain.end());276}277278void279ModuleManager::addInMemoryBuffer(StringRef FileName,280std::unique_ptr<llvm::MemoryBuffer> Buffer) {281const FileEntry *Entry =282FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);283InMemoryBuffers[Entry] = std::move(Buffer);284}285286std::unique_ptr<ModuleManager::VisitState> ModuleManager::allocateVisitState() {287// Fast path: if we have a cached state, use it.288if (FirstVisitState) {289auto Result = std::move(FirstVisitState);290FirstVisitState = std::move(Result->NextState);291return Result;292}293294// Allocate and return a new state.295return std::make_unique<VisitState>(size());296}297298void ModuleManager::returnVisitState(std::unique_ptr<VisitState> State) {299assert(State->NextState == nullptr && "Visited state is in list?");300State->NextState = std::move(FirstVisitState);301FirstVisitState = std::move(State);302}303304void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {305GlobalIndex = Index;306if (!GlobalIndex) {307ModulesInCommonWithGlobalIndex.clear();308return;309}310311// Notify the global module index about all of the modules we've already312// loaded.313for (ModuleFile &M : *this)314if (!GlobalIndex->loadedModuleFile(&M))315ModulesInCommonWithGlobalIndex.push_back(&M);316}317318void ModuleManager::moduleFileAccepted(ModuleFile *MF) {319if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))320return;321322ModulesInCommonWithGlobalIndex.push_back(MF);323}324325ModuleManager::ModuleManager(FileManager &FileMgr,326InMemoryModuleCache &ModuleCache,327const PCHContainerReader &PCHContainerRdr,328const HeaderSearch &HeaderSearchInfo)329: FileMgr(FileMgr), ModuleCache(&ModuleCache),330PCHContainerRdr(PCHContainerRdr), HeaderSearchInfo(HeaderSearchInfo) {}331332void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,333llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {334// If the visitation order vector is the wrong size, recompute the order.335if (VisitOrder.size() != Chain.size()) {336unsigned N = size();337VisitOrder.clear();338VisitOrder.reserve(N);339340// Record the number of incoming edges for each module. When we341// encounter a module with no incoming edges, push it into the queue342// to seed the queue.343SmallVector<ModuleFile *, 4> Queue;344Queue.reserve(N);345llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;346UnusedIncomingEdges.resize(size());347for (ModuleFile &M : llvm::reverse(*this)) {348unsigned Size = M.ImportedBy.size();349UnusedIncomingEdges[M.Index] = Size;350if (!Size)351Queue.push_back(&M);352}353354// Traverse the graph, making sure to visit a module before visiting any355// of its dependencies.356while (!Queue.empty()) {357ModuleFile *CurrentModule = Queue.pop_back_val();358VisitOrder.push_back(CurrentModule);359360// For any module that this module depends on, push it on the361// stack (if it hasn't already been marked as visited).362for (ModuleFile *M : llvm::reverse(CurrentModule->Imports)) {363// Remove our current module as an impediment to visiting the364// module we depend on. If we were the last unvisited module365// that depends on this particular module, push it into the366// queue to be visited.367unsigned &NumUnusedEdges = UnusedIncomingEdges[M->Index];368if (NumUnusedEdges && (--NumUnusedEdges == 0))369Queue.push_back(M);370}371}372373assert(VisitOrder.size() == N && "Visitation order is wrong?");374375FirstVisitState = nullptr;376}377378auto State = allocateVisitState();379unsigned VisitNumber = State->NextVisitNumber++;380381// If the caller has provided us with a hit-set that came from the global382// module index, mark every module file in common with the global module383// index that is *not* in that set as 'visited'.384if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {385for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)386{387ModuleFile *M = ModulesInCommonWithGlobalIndex[I];388if (!ModuleFilesHit->count(M))389State->VisitNumber[M->Index] = VisitNumber;390}391}392393for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {394ModuleFile *CurrentModule = VisitOrder[I];395// Should we skip this module file?396if (State->VisitNumber[CurrentModule->Index] == VisitNumber)397continue;398399// Visit the module.400assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);401State->VisitNumber[CurrentModule->Index] = VisitNumber;402if (!Visitor(*CurrentModule))403continue;404405// The visitor has requested that cut off visitation of any406// module that the current module depends on. To indicate this407// behavior, we mark all of the reachable modules as having been visited.408ModuleFile *NextModule = CurrentModule;409do {410// For any module that this module depends on, push it on the411// stack (if it hasn't already been marked as visited).412for (llvm::SetVector<ModuleFile *>::iterator413M = NextModule->Imports.begin(),414MEnd = NextModule->Imports.end();415M != MEnd; ++M) {416if (State->VisitNumber[(*M)->Index] != VisitNumber) {417State->Stack.push_back(*M);418State->VisitNumber[(*M)->Index] = VisitNumber;419}420}421422if (State->Stack.empty())423break;424425// Pop the next module off the stack.426NextModule = State->Stack.pop_back_val();427} while (true);428}429430returnVisitState(std::move(State));431}432433bool ModuleManager::lookupModuleFile(StringRef FileName, off_t ExpectedSize,434time_t ExpectedModTime,435OptionalFileEntryRef &File) {436if (FileName == "-") {437File = expectedToOptional(FileMgr.getSTDIN());438return false;439}440441// Open the file immediately to ensure there is no race between stat'ing and442// opening the file.443File = FileMgr.getOptionalFileRef(FileName, /*OpenFile=*/true,444/*CacheFailure=*/false);445446if (File &&447((ExpectedSize && ExpectedSize != File->getSize()) ||448(ExpectedModTime && ExpectedModTime != File->getModificationTime())))449// Do not destroy File, as it may be referenced. If we need to rebuild it,450// it will be destroyed by removeModules.451return true;452453return false;454}455456#ifndef NDEBUG457namespace llvm {458459template<>460struct GraphTraits<ModuleManager> {461using NodeRef = ModuleFile *;462using ChildIteratorType = llvm::SetVector<ModuleFile *>::const_iterator;463using nodes_iterator = pointer_iterator<ModuleManager::ModuleConstIterator>;464465static ChildIteratorType child_begin(NodeRef Node) {466return Node->Imports.begin();467}468469static ChildIteratorType child_end(NodeRef Node) {470return Node->Imports.end();471}472473static nodes_iterator nodes_begin(const ModuleManager &Manager) {474return nodes_iterator(Manager.begin());475}476477static nodes_iterator nodes_end(const ModuleManager &Manager) {478return nodes_iterator(Manager.end());479}480};481482template<>483struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {484explicit DOTGraphTraits(bool IsSimple = false)485: DefaultDOTGraphTraits(IsSimple) {}486487static bool renderGraphFromBottomUp() { return true; }488489std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {490return M->ModuleName;491}492};493494} // namespace llvm495496void ModuleManager::viewGraph() {497llvm::ViewGraph(*this, "Modules");498}499#endif500501502