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
lookupByFileName(StringRef Name) const440b57cec5SDimitry 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
lookupByModuleName(StringRef Name) const530b57cec5SDimitry Andric ModuleFile *ModuleManager::lookupByModuleName(StringRef Name) const {
540b57cec5SDimitry Andric if (const Module *Mod = HeaderSearchInfo.getModuleMap().findModule(Name))
55*5f757f3fSDimitry Andric if (OptionalFileEntryRef File = Mod->getASTFile())
56*5f757f3fSDimitry Andric return lookup(*File);
570b57cec5SDimitry Andric
580b57cec5SDimitry Andric return nullptr;
590b57cec5SDimitry Andric }
600b57cec5SDimitry Andric
lookup(const FileEntry * File) const610b57cec5SDimitry Andric ModuleFile *ModuleManager::lookup(const FileEntry *File) const {
6206c3fb27SDimitry Andric return Modules.lookup(File);
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric std::unique_ptr<llvm::MemoryBuffer>
lookupBuffer(StringRef Name)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
checkSignature(ASTFileSignature Signature,ASTFileSignature ExpectedSignature,std::string & ErrorStr)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
updateModuleImports(ModuleFile & MF,ModuleFile * ImportedBy,SourceLocation ImportLoc)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
addModule(StringRef FileName,ModuleKind Type,SourceLocation ImportLoc,ModuleFile * ImportedBy,unsigned Generation,off_t ExpectedSize,time_t ExpectedModTime,ASTFileSignature ExpectedSignature,ASTFileSignatureReader ReadSignature,ModuleFile * & Module,std::string & ErrorStr)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.
111*5f757f3fSDimitry Andric OptionalFileEntryRef 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
126*5f757f3fSDimitry Andric if (!Entry) {
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,
146*5f757f3fSDimitry Andric FileEntryRef Entry) -> bool {
147e8d8bef9SDimitry Andric if (Kind != MK_ImplicitModule)
148e8d8bef9SDimitry Andric return true;
149*5f757f3fSDimitry Andric return Entry.getName() == MF->FileName;
150e8d8bef9SDimitry Andric };
151e8d8bef9SDimitry Andric
1520b57cec5SDimitry Andric // Check whether we already loaded this module, before
153*5f757f3fSDimitry Andric if (ModuleFile *ModuleEntry = Modules.lookup(*Entry)) {
154*5f757f3fSDimitry 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.
166*5f757f3fSDimitry Andric auto NewModule = std::make_unique<ModuleFile>(Type, *Entry, Generation);
1670b57cec5SDimitry Andric NewModule->Index = Chain.size();
1680b57cec5SDimitry Andric NewModule->FileName = FileName.str();
1690b57cec5SDimitry Andric NewModule->ImportLoc = ImportLoc;
1700b57cec5SDimitry Andric NewModule->InputFilesValidationTimestamp = 0;
1710b57cec5SDimitry Andric
1720b57cec5SDimitry Andric if (NewModule->Kind == MK_ImplicitModule) {
1730b57cec5SDimitry Andric std::string TimestampFilename = NewModule->getTimestampFilename();
1740b57cec5SDimitry Andric llvm::vfs::Status Status;
1750b57cec5SDimitry Andric // A cached stat value would be fine as well.
1760b57cec5SDimitry Andric if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
1770b57cec5SDimitry Andric NewModule->InputFilesValidationTimestamp =
1780b57cec5SDimitry Andric llvm::sys::toTimeT(Status.getLastModificationTime());
1790b57cec5SDimitry Andric }
1800b57cec5SDimitry Andric
1810b57cec5SDimitry Andric // Load the contents of the module
1820b57cec5SDimitry Andric if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
1830b57cec5SDimitry Andric // The buffer was already provided for us.
1840b57cec5SDimitry Andric NewModule->Buffer = &ModuleCache->addBuiltPCM(FileName, std::move(Buffer));
1850b57cec5SDimitry Andric // Since the cached buffer is reused, it is safe to close the file
1860b57cec5SDimitry Andric // descriptor that was opened while stat()ing the PCM in
1870b57cec5SDimitry Andric // lookupModuleFile() above, it won't be needed any longer.
1880b57cec5SDimitry Andric Entry->closeFile();
1890b57cec5SDimitry Andric } else if (llvm::MemoryBuffer *Buffer =
1900b57cec5SDimitry Andric getModuleCache().lookupPCM(FileName)) {
1910b57cec5SDimitry Andric NewModule->Buffer = Buffer;
1920b57cec5SDimitry Andric // As above, the file descriptor is no longer needed.
1930b57cec5SDimitry Andric Entry->closeFile();
1940b57cec5SDimitry Andric } else if (getModuleCache().shouldBuildPCM(FileName)) {
1950b57cec5SDimitry Andric // Report that the module is out of date, since we tried (and failed) to
1960b57cec5SDimitry Andric // import it earlier.
1970b57cec5SDimitry Andric Entry->closeFile();
1980b57cec5SDimitry Andric return OutOfDate;
1990b57cec5SDimitry Andric } else {
2000b57cec5SDimitry Andric // Get a buffer of the file and close the file descriptor when done.
2015ffd83dbSDimitry Andric // The file is volatile because in a parallel build we expect multiple
2025ffd83dbSDimitry Andric // compiler processes to use the same module file rebuilding it if needed.
2035ffd83dbSDimitry Andric //
2045ffd83dbSDimitry Andric // RequiresNullTerminator is false because module files don't need it, and
2055ffd83dbSDimitry Andric // this allows the file to still be mmapped.
206*5f757f3fSDimitry Andric auto Buf = FileMgr.getBufferForFile(NewModule->File,
2075ffd83dbSDimitry Andric /*IsVolatile=*/true,
2085ffd83dbSDimitry Andric /*RequiresNullTerminator=*/false);
2090b57cec5SDimitry Andric
2100b57cec5SDimitry Andric if (!Buf) {
2110b57cec5SDimitry Andric ErrorStr = Buf.getError().message();
2120b57cec5SDimitry Andric return Missing;
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric
2150b57cec5SDimitry Andric NewModule->Buffer = &getModuleCache().addPCM(FileName, std::move(*Buf));
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric
2180b57cec5SDimitry Andric // Initialize the stream.
2190b57cec5SDimitry Andric NewModule->Data = PCHContainerRdr.ExtractPCH(*NewModule->Buffer);
2200b57cec5SDimitry Andric
2210b57cec5SDimitry Andric // Read the signature eagerly now so that we can check it. Avoid calling
2220b57cec5SDimitry Andric // ReadSignature unless there's something to check though.
2230b57cec5SDimitry Andric if (ExpectedSignature && checkSignature(ReadSignature(NewModule->Data),
224a7dea167SDimitry Andric ExpectedSignature, ErrorStr))
2250b57cec5SDimitry Andric return OutOfDate;
2260b57cec5SDimitry Andric
2270b57cec5SDimitry Andric // We're keeping this module. Store it everywhere.
228*5f757f3fSDimitry Andric Module = Modules[*Entry] = NewModule.get();
2290b57cec5SDimitry Andric
2300b57cec5SDimitry Andric updateModuleImports(*NewModule, ImportedBy, ImportLoc);
2310b57cec5SDimitry Andric
2320b57cec5SDimitry Andric if (!NewModule->isModule())
2330b57cec5SDimitry Andric PCHChain.push_back(NewModule.get());
2340b57cec5SDimitry Andric if (!ImportedBy)
2350b57cec5SDimitry Andric Roots.push_back(NewModule.get());
2360b57cec5SDimitry Andric
2370b57cec5SDimitry Andric Chain.push_back(std::move(NewModule));
2380b57cec5SDimitry Andric return NewlyLoaded;
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric
removeModules(ModuleIterator First)241bdd1243dSDimitry Andric void ModuleManager::removeModules(ModuleIterator First) {
2420b57cec5SDimitry Andric auto Last = end();
2430b57cec5SDimitry Andric if (First == Last)
2440b57cec5SDimitry Andric return;
2450b57cec5SDimitry Andric
2460b57cec5SDimitry Andric // Explicitly clear VisitOrder since we might not notice it is stale.
2470b57cec5SDimitry Andric VisitOrder.clear();
2480b57cec5SDimitry Andric
2490b57cec5SDimitry Andric // Collect the set of module file pointers that we'll be removing.
2500b57cec5SDimitry Andric llvm::SmallPtrSet<ModuleFile *, 4> victimSet(
2510b57cec5SDimitry Andric (llvm::pointer_iterator<ModuleIterator>(First)),
2520b57cec5SDimitry Andric (llvm::pointer_iterator<ModuleIterator>(Last)));
2530b57cec5SDimitry Andric
2540b57cec5SDimitry Andric auto IsVictim = [&](ModuleFile *MF) {
2550b57cec5SDimitry Andric return victimSet.count(MF);
2560b57cec5SDimitry Andric };
2570b57cec5SDimitry Andric // Remove any references to the now-destroyed modules.
2580b57cec5SDimitry Andric for (auto I = begin(); I != First; ++I) {
2590b57cec5SDimitry Andric I->Imports.remove_if(IsVictim);
2600b57cec5SDimitry Andric I->ImportedBy.remove_if(IsVictim);
2610b57cec5SDimitry Andric }
262349cc55cSDimitry Andric llvm::erase_if(Roots, IsVictim);
2630b57cec5SDimitry Andric
2640b57cec5SDimitry Andric // Remove the modules from the PCH chain.
2650b57cec5SDimitry Andric for (auto I = First; I != Last; ++I) {
2660b57cec5SDimitry Andric if (!I->isModule()) {
2670b57cec5SDimitry Andric PCHChain.erase(llvm::find(PCHChain, &*I), PCHChain.end());
2680b57cec5SDimitry Andric break;
2690b57cec5SDimitry Andric }
2700b57cec5SDimitry Andric }
2710b57cec5SDimitry Andric
272bdd1243dSDimitry Andric // Delete the modules.
273bdd1243dSDimitry Andric for (ModuleIterator victim = First; victim != Last; ++victim)
2740b57cec5SDimitry Andric Modules.erase(victim->File);
2750b57cec5SDimitry Andric
2760b57cec5SDimitry Andric Chain.erase(Chain.begin() + (First - begin()), Chain.end());
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric
2790b57cec5SDimitry Andric void
addInMemoryBuffer(StringRef FileName,std::unique_ptr<llvm::MemoryBuffer> Buffer)2800b57cec5SDimitry Andric ModuleManager::addInMemoryBuffer(StringRef FileName,
2810b57cec5SDimitry Andric std::unique_ptr<llvm::MemoryBuffer> Buffer) {
2820b57cec5SDimitry Andric const FileEntry *Entry =
2830b57cec5SDimitry Andric FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
2840b57cec5SDimitry Andric InMemoryBuffers[Entry] = std::move(Buffer);
2850b57cec5SDimitry Andric }
2860b57cec5SDimitry Andric
allocateVisitState()28704eeddc0SDimitry Andric std::unique_ptr<ModuleManager::VisitState> ModuleManager::allocateVisitState() {
2880b57cec5SDimitry Andric // Fast path: if we have a cached state, use it.
2890b57cec5SDimitry Andric if (FirstVisitState) {
29004eeddc0SDimitry Andric auto Result = std::move(FirstVisitState);
29104eeddc0SDimitry Andric FirstVisitState = std::move(Result->NextState);
2920b57cec5SDimitry Andric return Result;
2930b57cec5SDimitry Andric }
2940b57cec5SDimitry Andric
2950b57cec5SDimitry Andric // Allocate and return a new state.
29604eeddc0SDimitry Andric return std::make_unique<VisitState>(size());
2970b57cec5SDimitry Andric }
2980b57cec5SDimitry Andric
returnVisitState(std::unique_ptr<VisitState> State)29904eeddc0SDimitry Andric void ModuleManager::returnVisitState(std::unique_ptr<VisitState> State) {
3000b57cec5SDimitry Andric assert(State->NextState == nullptr && "Visited state is in list?");
30104eeddc0SDimitry Andric State->NextState = std::move(FirstVisitState);
30204eeddc0SDimitry Andric FirstVisitState = std::move(State);
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric
setGlobalIndex(GlobalModuleIndex * Index)3050b57cec5SDimitry Andric void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
3060b57cec5SDimitry Andric GlobalIndex = Index;
3070b57cec5SDimitry Andric if (!GlobalIndex) {
3080b57cec5SDimitry Andric ModulesInCommonWithGlobalIndex.clear();
3090b57cec5SDimitry Andric return;
3100b57cec5SDimitry Andric }
3110b57cec5SDimitry Andric
3120b57cec5SDimitry Andric // Notify the global module index about all of the modules we've already
3130b57cec5SDimitry Andric // loaded.
3140b57cec5SDimitry Andric for (ModuleFile &M : *this)
3150b57cec5SDimitry Andric if (!GlobalIndex->loadedModuleFile(&M))
3160b57cec5SDimitry Andric ModulesInCommonWithGlobalIndex.push_back(&M);
3170b57cec5SDimitry Andric }
3180b57cec5SDimitry Andric
moduleFileAccepted(ModuleFile * MF)3190b57cec5SDimitry Andric void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
3200b57cec5SDimitry Andric if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
3210b57cec5SDimitry Andric return;
3220b57cec5SDimitry Andric
3230b57cec5SDimitry Andric ModulesInCommonWithGlobalIndex.push_back(MF);
3240b57cec5SDimitry Andric }
3250b57cec5SDimitry Andric
ModuleManager(FileManager & FileMgr,InMemoryModuleCache & ModuleCache,const PCHContainerReader & PCHContainerRdr,const HeaderSearch & HeaderSearchInfo)3260b57cec5SDimitry Andric ModuleManager::ModuleManager(FileManager &FileMgr,
3270b57cec5SDimitry Andric InMemoryModuleCache &ModuleCache,
3280b57cec5SDimitry Andric const PCHContainerReader &PCHContainerRdr,
3290b57cec5SDimitry Andric const HeaderSearch &HeaderSearchInfo)
3300b57cec5SDimitry Andric : FileMgr(FileMgr), ModuleCache(&ModuleCache),
3310b57cec5SDimitry Andric PCHContainerRdr(PCHContainerRdr), HeaderSearchInfo(HeaderSearchInfo) {}
3320b57cec5SDimitry Andric
visit(llvm::function_ref<bool (ModuleFile & M)> Visitor,llvm::SmallPtrSetImpl<ModuleFile * > * ModuleFilesHit)3330b57cec5SDimitry Andric void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
3340b57cec5SDimitry Andric llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
3350b57cec5SDimitry Andric // If the visitation order vector is the wrong size, recompute the order.
3360b57cec5SDimitry Andric if (VisitOrder.size() != Chain.size()) {
3370b57cec5SDimitry Andric unsigned N = size();
3380b57cec5SDimitry Andric VisitOrder.clear();
3390b57cec5SDimitry Andric VisitOrder.reserve(N);
3400b57cec5SDimitry Andric
3410b57cec5SDimitry Andric // Record the number of incoming edges for each module. When we
3420b57cec5SDimitry Andric // encounter a module with no incoming edges, push it into the queue
3430b57cec5SDimitry Andric // to seed the queue.
3440b57cec5SDimitry Andric SmallVector<ModuleFile *, 4> Queue;
3450b57cec5SDimitry Andric Queue.reserve(N);
3460b57cec5SDimitry Andric llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
3470b57cec5SDimitry Andric UnusedIncomingEdges.resize(size());
3480b57cec5SDimitry Andric for (ModuleFile &M : llvm::reverse(*this)) {
3490b57cec5SDimitry Andric unsigned Size = M.ImportedBy.size();
3500b57cec5SDimitry Andric UnusedIncomingEdges[M.Index] = Size;
3510b57cec5SDimitry Andric if (!Size)
3520b57cec5SDimitry Andric Queue.push_back(&M);
3530b57cec5SDimitry Andric }
3540b57cec5SDimitry Andric
3550b57cec5SDimitry Andric // Traverse the graph, making sure to visit a module before visiting any
3560b57cec5SDimitry Andric // of its dependencies.
3570b57cec5SDimitry Andric while (!Queue.empty()) {
3580b57cec5SDimitry Andric ModuleFile *CurrentModule = Queue.pop_back_val();
3590b57cec5SDimitry Andric VisitOrder.push_back(CurrentModule);
3600b57cec5SDimitry Andric
3610b57cec5SDimitry Andric // For any module that this module depends on, push it on the
3620b57cec5SDimitry Andric // stack (if it hasn't already been marked as visited).
363349cc55cSDimitry Andric for (ModuleFile *M : llvm::reverse(CurrentModule->Imports)) {
3640b57cec5SDimitry Andric // Remove our current module as an impediment to visiting the
3650b57cec5SDimitry Andric // module we depend on. If we were the last unvisited module
3660b57cec5SDimitry Andric // that depends on this particular module, push it into the
3670b57cec5SDimitry Andric // queue to be visited.
368349cc55cSDimitry Andric unsigned &NumUnusedEdges = UnusedIncomingEdges[M->Index];
3690b57cec5SDimitry Andric if (NumUnusedEdges && (--NumUnusedEdges == 0))
370349cc55cSDimitry Andric Queue.push_back(M);
3710b57cec5SDimitry Andric }
3720b57cec5SDimitry Andric }
3730b57cec5SDimitry Andric
3740b57cec5SDimitry Andric assert(VisitOrder.size() == N && "Visitation order is wrong?");
3750b57cec5SDimitry Andric
3760b57cec5SDimitry Andric FirstVisitState = nullptr;
3770b57cec5SDimitry Andric }
3780b57cec5SDimitry Andric
37904eeddc0SDimitry Andric auto State = allocateVisitState();
3800b57cec5SDimitry Andric unsigned VisitNumber = State->NextVisitNumber++;
3810b57cec5SDimitry Andric
3820b57cec5SDimitry Andric // If the caller has provided us with a hit-set that came from the global
3830b57cec5SDimitry Andric // module index, mark every module file in common with the global module
3840b57cec5SDimitry Andric // index that is *not* in that set as 'visited'.
3850b57cec5SDimitry Andric if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
3860b57cec5SDimitry Andric for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
3870b57cec5SDimitry Andric {
3880b57cec5SDimitry Andric ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
3890b57cec5SDimitry Andric if (!ModuleFilesHit->count(M))
3900b57cec5SDimitry Andric State->VisitNumber[M->Index] = VisitNumber;
3910b57cec5SDimitry Andric }
3920b57cec5SDimitry Andric }
3930b57cec5SDimitry Andric
3940b57cec5SDimitry Andric for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
3950b57cec5SDimitry Andric ModuleFile *CurrentModule = VisitOrder[I];
3960b57cec5SDimitry Andric // Should we skip this module file?
3970b57cec5SDimitry Andric if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
3980b57cec5SDimitry Andric continue;
3990b57cec5SDimitry Andric
4000b57cec5SDimitry Andric // Visit the module.
4010b57cec5SDimitry Andric assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
4020b57cec5SDimitry Andric State->VisitNumber[CurrentModule->Index] = VisitNumber;
4030b57cec5SDimitry Andric if (!Visitor(*CurrentModule))
4040b57cec5SDimitry Andric continue;
4050b57cec5SDimitry Andric
4060b57cec5SDimitry Andric // The visitor has requested that cut off visitation of any
4070b57cec5SDimitry Andric // module that the current module depends on. To indicate this
4080b57cec5SDimitry Andric // behavior, we mark all of the reachable modules as having been visited.
4090b57cec5SDimitry Andric ModuleFile *NextModule = CurrentModule;
4100b57cec5SDimitry Andric do {
4110b57cec5SDimitry Andric // For any module that this module depends on, push it on the
4120b57cec5SDimitry Andric // stack (if it hasn't already been marked as visited).
4130b57cec5SDimitry Andric for (llvm::SetVector<ModuleFile *>::iterator
4140b57cec5SDimitry Andric M = NextModule->Imports.begin(),
4150b57cec5SDimitry Andric MEnd = NextModule->Imports.end();
4160b57cec5SDimitry Andric M != MEnd; ++M) {
4170b57cec5SDimitry Andric if (State->VisitNumber[(*M)->Index] != VisitNumber) {
4180b57cec5SDimitry Andric State->Stack.push_back(*M);
4190b57cec5SDimitry Andric State->VisitNumber[(*M)->Index] = VisitNumber;
4200b57cec5SDimitry Andric }
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric
4230b57cec5SDimitry Andric if (State->Stack.empty())
4240b57cec5SDimitry Andric break;
4250b57cec5SDimitry Andric
4260b57cec5SDimitry Andric // Pop the next module off the stack.
4270b57cec5SDimitry Andric NextModule = State->Stack.pop_back_val();
4280b57cec5SDimitry Andric } while (true);
4290b57cec5SDimitry Andric }
4300b57cec5SDimitry Andric
43104eeddc0SDimitry Andric returnVisitState(std::move(State));
4320b57cec5SDimitry Andric }
4330b57cec5SDimitry Andric
lookupModuleFile(StringRef FileName,off_t ExpectedSize,time_t ExpectedModTime,OptionalFileEntryRef & File)434e8d8bef9SDimitry Andric bool ModuleManager::lookupModuleFile(StringRef FileName, off_t ExpectedSize,
4350b57cec5SDimitry Andric time_t ExpectedModTime,
436bdd1243dSDimitry Andric OptionalFileEntryRef &File) {
437*5f757f3fSDimitry Andric if (FileName == "-") {
438*5f757f3fSDimitry Andric File = expectedToOptional(FileMgr.getSTDIN());
4390b57cec5SDimitry Andric return false;
440*5f757f3fSDimitry Andric }
4410b57cec5SDimitry Andric
4420b57cec5SDimitry Andric // Open the file immediately to ensure there is no race between stat'ing and
4430b57cec5SDimitry Andric // opening the file.
444*5f757f3fSDimitry Andric File = FileMgr.getOptionalFileRef(FileName, /*OpenFile=*/true,
445*5f757f3fSDimitry Andric /*CacheFailure=*/false);
446e8d8bef9SDimitry Andric
447*5f757f3fSDimitry Andric if (File &&
448*5f757f3fSDimitry Andric ((ExpectedSize && ExpectedSize != File->getSize()) ||
449*5f757f3fSDimitry Andric (ExpectedModTime && ExpectedModTime != File->getModificationTime())))
4500b57cec5SDimitry Andric // Do not destroy File, as it may be referenced. If we need to rebuild it,
4510b57cec5SDimitry Andric // it will be destroyed by removeModules.
4520b57cec5SDimitry Andric return true;
4530b57cec5SDimitry Andric
4540b57cec5SDimitry Andric return false;
4550b57cec5SDimitry Andric }
4560b57cec5SDimitry Andric
4570b57cec5SDimitry Andric #ifndef NDEBUG
4580b57cec5SDimitry Andric namespace llvm {
4590b57cec5SDimitry Andric
4600b57cec5SDimitry Andric template<>
4610b57cec5SDimitry Andric struct GraphTraits<ModuleManager> {
4620b57cec5SDimitry Andric using NodeRef = ModuleFile *;
4630b57cec5SDimitry Andric using ChildIteratorType = llvm::SetVector<ModuleFile *>::const_iterator;
4640b57cec5SDimitry Andric using nodes_iterator = pointer_iterator<ModuleManager::ModuleConstIterator>;
4650b57cec5SDimitry Andric
child_beginllvm::GraphTraits4660b57cec5SDimitry Andric static ChildIteratorType child_begin(NodeRef Node) {
4670b57cec5SDimitry Andric return Node->Imports.begin();
4680b57cec5SDimitry Andric }
4690b57cec5SDimitry Andric
child_endllvm::GraphTraits4700b57cec5SDimitry Andric static ChildIteratorType child_end(NodeRef Node) {
4710b57cec5SDimitry Andric return Node->Imports.end();
4720b57cec5SDimitry Andric }
4730b57cec5SDimitry Andric
nodes_beginllvm::GraphTraits4740b57cec5SDimitry Andric static nodes_iterator nodes_begin(const ModuleManager &Manager) {
4750b57cec5SDimitry Andric return nodes_iterator(Manager.begin());
4760b57cec5SDimitry Andric }
4770b57cec5SDimitry Andric
nodes_endllvm::GraphTraits4780b57cec5SDimitry Andric static nodes_iterator nodes_end(const ModuleManager &Manager) {
4790b57cec5SDimitry Andric return nodes_iterator(Manager.end());
4800b57cec5SDimitry Andric }
4810b57cec5SDimitry Andric };
4820b57cec5SDimitry Andric
4830b57cec5SDimitry Andric template<>
4840b57cec5SDimitry Andric struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
DOTGraphTraitsllvm::DOTGraphTraits4850b57cec5SDimitry Andric explicit DOTGraphTraits(bool IsSimple = false)
4860b57cec5SDimitry Andric : DefaultDOTGraphTraits(IsSimple) {}
4870b57cec5SDimitry Andric
renderGraphFromBottomUpllvm::DOTGraphTraits4880b57cec5SDimitry Andric static bool renderGraphFromBottomUp() { return true; }
4890b57cec5SDimitry Andric
getNodeLabelllvm::DOTGraphTraits4900b57cec5SDimitry Andric std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
4910b57cec5SDimitry Andric return M->ModuleName;
4920b57cec5SDimitry Andric }
4930b57cec5SDimitry Andric };
4940b57cec5SDimitry Andric
4950b57cec5SDimitry Andric } // namespace llvm
4960b57cec5SDimitry Andric
viewGraph()4970b57cec5SDimitry Andric void ModuleManager::viewGraph() {
4980b57cec5SDimitry Andric llvm::ViewGraph(*this, "Modules");
4990b57cec5SDimitry Andric }
5000b57cec5SDimitry Andric #endif
501