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))
550b57cec5SDimitry Andric if (const FileEntry *File = Mod->getASTFile())
560b57cec5SDimitry 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 {
620b57cec5SDimitry Andric auto Known = Modules.find(File);
630b57cec5SDimitry Andric if (Known == Modules.end())
640b57cec5SDimitry Andric return nullptr;
650b57cec5SDimitry Andric
660b57cec5SDimitry Andric return Known->second;
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric std::unique_ptr<llvm::MemoryBuffer>
lookupBuffer(StringRef Name)700b57cec5SDimitry Andric ModuleManager::lookupBuffer(StringRef Name) {
71a7dea167SDimitry Andric auto Entry = FileMgr.getFile(Name, /*OpenFile=*/false,
720b57cec5SDimitry Andric /*CacheFailure=*/false);
73a7dea167SDimitry Andric if (!Entry)
74a7dea167SDimitry Andric return nullptr;
75a7dea167SDimitry Andric return std::move(InMemoryBuffers[*Entry]);
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric
checkSignature(ASTFileSignature Signature,ASTFileSignature ExpectedSignature,std::string & ErrorStr)780b57cec5SDimitry Andric static bool checkSignature(ASTFileSignature Signature,
790b57cec5SDimitry Andric ASTFileSignature ExpectedSignature,
800b57cec5SDimitry Andric std::string &ErrorStr) {
810b57cec5SDimitry Andric if (!ExpectedSignature || Signature == ExpectedSignature)
820b57cec5SDimitry Andric return false;
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric ErrorStr =
850b57cec5SDimitry Andric Signature ? "signature mismatch" : "could not read module signature";
860b57cec5SDimitry Andric return true;
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric
updateModuleImports(ModuleFile & MF,ModuleFile * ImportedBy,SourceLocation ImportLoc)890b57cec5SDimitry Andric static void updateModuleImports(ModuleFile &MF, ModuleFile *ImportedBy,
900b57cec5SDimitry Andric SourceLocation ImportLoc) {
910b57cec5SDimitry Andric if (ImportedBy) {
920b57cec5SDimitry Andric MF.ImportedBy.insert(ImportedBy);
930b57cec5SDimitry Andric ImportedBy->Imports.insert(&MF);
940b57cec5SDimitry Andric } else {
950b57cec5SDimitry Andric if (!MF.DirectlyImported)
960b57cec5SDimitry Andric MF.ImportLoc = ImportLoc;
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric MF.DirectlyImported = true;
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric
1020b57cec5SDimitry 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)1030b57cec5SDimitry Andric ModuleManager::addModule(StringRef FileName, ModuleKind Type,
1040b57cec5SDimitry Andric SourceLocation ImportLoc, ModuleFile *ImportedBy,
1050b57cec5SDimitry Andric unsigned Generation,
1060b57cec5SDimitry Andric off_t ExpectedSize, time_t ExpectedModTime,
1070b57cec5SDimitry Andric ASTFileSignature ExpectedSignature,
1080b57cec5SDimitry Andric ASTFileSignatureReader ReadSignature,
1090b57cec5SDimitry Andric ModuleFile *&Module,
1100b57cec5SDimitry Andric std::string &ErrorStr) {
1110b57cec5SDimitry Andric Module = nullptr;
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric // Look for the file entry. This only fails if the expected size or
1140b57cec5SDimitry Andric // modification time differ.
115*af732203SDimitry Andric OptionalFileEntryRefDegradesToFileEntryPtr Entry;
1160b57cec5SDimitry Andric if (Type == MK_ExplicitModule || Type == MK_PrebuiltModule) {
1170b57cec5SDimitry Andric // If we're not expecting to pull this file out of the module cache, it
1180b57cec5SDimitry Andric // might have a different mtime due to being moved across filesystems in
1190b57cec5SDimitry Andric // a distributed build. The size must still match, though. (As must the
1200b57cec5SDimitry Andric // contents, but we can't check that.)
1210b57cec5SDimitry Andric ExpectedModTime = 0;
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric // Note: ExpectedSize and ExpectedModTime will be 0 for MK_ImplicitModule
1240b57cec5SDimitry Andric // when using an ASTFileSignature.
1250b57cec5SDimitry Andric if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
1260b57cec5SDimitry Andric ErrorStr = "module file out of date";
1270b57cec5SDimitry Andric return OutOfDate;
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric
1300b57cec5SDimitry Andric if (!Entry && FileName != "-") {
1310b57cec5SDimitry Andric ErrorStr = "module file not found";
1320b57cec5SDimitry Andric return Missing;
1330b57cec5SDimitry Andric }
1340b57cec5SDimitry Andric
135*af732203SDimitry Andric // The ModuleManager's use of FileEntry nodes as the keys for its map of
136*af732203SDimitry Andric // loaded modules is less than ideal. Uniqueness for FileEntry nodes is
137*af732203SDimitry Andric // maintained by FileManager, which in turn uses inode numbers on hosts
138*af732203SDimitry Andric // that support that. When coupled with the module cache's proclivity for
139*af732203SDimitry Andric // turning over and deleting stale PCMs, this means entries for different
140*af732203SDimitry Andric // module files can wind up reusing the same underlying inode. When this
141*af732203SDimitry Andric // happens, subsequent accesses to the Modules map will disagree on the
142*af732203SDimitry Andric // ModuleFile associated with a given file. In general, it is not sufficient
143*af732203SDimitry Andric // to resolve this conundrum with a type like FileEntryRef that stores the
144*af732203SDimitry Andric // name of the FileEntry node on first access because of path canonicalization
145*af732203SDimitry Andric // issues. However, the paths constructed for implicit module builds are
146*af732203SDimitry Andric // fully under Clang's control. We *can*, therefore, rely on their structure
147*af732203SDimitry Andric // being consistent across operating systems and across subsequent accesses
148*af732203SDimitry Andric // to the Modules map.
149*af732203SDimitry Andric auto implicitModuleNamesMatch = [](ModuleKind Kind, const ModuleFile *MF,
150*af732203SDimitry Andric const FileEntry *Entry) -> bool {
151*af732203SDimitry Andric if (Kind != MK_ImplicitModule)
152*af732203SDimitry Andric return true;
153*af732203SDimitry Andric return Entry->getName() == MF->FileName;
154*af732203SDimitry Andric };
155*af732203SDimitry Andric
1560b57cec5SDimitry Andric // Check whether we already loaded this module, before
1570b57cec5SDimitry Andric if (ModuleFile *ModuleEntry = Modules.lookup(Entry)) {
158*af732203SDimitry Andric if (implicitModuleNamesMatch(Type, ModuleEntry, Entry)) {
1590b57cec5SDimitry Andric // Check the stored signature.
1600b57cec5SDimitry Andric if (checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr))
1610b57cec5SDimitry Andric return OutOfDate;
1620b57cec5SDimitry Andric
1630b57cec5SDimitry Andric Module = ModuleEntry;
1640b57cec5SDimitry Andric updateModuleImports(*ModuleEntry, ImportedBy, ImportLoc);
1650b57cec5SDimitry Andric return AlreadyLoaded;
1660b57cec5SDimitry Andric }
167*af732203SDimitry Andric }
1680b57cec5SDimitry Andric
1690b57cec5SDimitry Andric // Allocate a new module.
170a7dea167SDimitry Andric auto NewModule = std::make_unique<ModuleFile>(Type, Generation);
1710b57cec5SDimitry Andric NewModule->Index = Chain.size();
1720b57cec5SDimitry Andric NewModule->FileName = FileName.str();
1730b57cec5SDimitry Andric NewModule->File = Entry;
1740b57cec5SDimitry Andric NewModule->ImportLoc = ImportLoc;
1750b57cec5SDimitry Andric NewModule->InputFilesValidationTimestamp = 0;
1760b57cec5SDimitry Andric
1770b57cec5SDimitry Andric if (NewModule->Kind == MK_ImplicitModule) {
1780b57cec5SDimitry Andric std::string TimestampFilename = NewModule->getTimestampFilename();
1790b57cec5SDimitry Andric llvm::vfs::Status Status;
1800b57cec5SDimitry Andric // A cached stat value would be fine as well.
1810b57cec5SDimitry Andric if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
1820b57cec5SDimitry Andric NewModule->InputFilesValidationTimestamp =
1830b57cec5SDimitry Andric llvm::sys::toTimeT(Status.getLastModificationTime());
1840b57cec5SDimitry Andric }
1850b57cec5SDimitry Andric
1860b57cec5SDimitry Andric // Load the contents of the module
1870b57cec5SDimitry Andric if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
1880b57cec5SDimitry Andric // The buffer was already provided for us.
1890b57cec5SDimitry Andric NewModule->Buffer = &ModuleCache->addBuiltPCM(FileName, std::move(Buffer));
1900b57cec5SDimitry Andric // Since the cached buffer is reused, it is safe to close the file
1910b57cec5SDimitry Andric // descriptor that was opened while stat()ing the PCM in
1920b57cec5SDimitry Andric // lookupModuleFile() above, it won't be needed any longer.
1930b57cec5SDimitry Andric Entry->closeFile();
1940b57cec5SDimitry Andric } else if (llvm::MemoryBuffer *Buffer =
1950b57cec5SDimitry Andric getModuleCache().lookupPCM(FileName)) {
1960b57cec5SDimitry Andric NewModule->Buffer = Buffer;
1970b57cec5SDimitry Andric // As above, the file descriptor is no longer needed.
1980b57cec5SDimitry Andric Entry->closeFile();
1990b57cec5SDimitry Andric } else if (getModuleCache().shouldBuildPCM(FileName)) {
2000b57cec5SDimitry Andric // Report that the module is out of date, since we tried (and failed) to
2010b57cec5SDimitry Andric // import it earlier.
2020b57cec5SDimitry Andric Entry->closeFile();
2030b57cec5SDimitry Andric return OutOfDate;
2040b57cec5SDimitry Andric } else {
2050b57cec5SDimitry Andric // Open the AST file.
2060b57cec5SDimitry Andric llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf((std::error_code()));
2070b57cec5SDimitry Andric if (FileName == "-") {
2080b57cec5SDimitry Andric Buf = llvm::MemoryBuffer::getSTDIN();
2090b57cec5SDimitry Andric } else {
2100b57cec5SDimitry Andric // Get a buffer of the file and close the file descriptor when done.
2115ffd83dbSDimitry Andric // The file is volatile because in a parallel build we expect multiple
2125ffd83dbSDimitry Andric // compiler processes to use the same module file rebuilding it if needed.
2135ffd83dbSDimitry Andric //
2145ffd83dbSDimitry Andric // RequiresNullTerminator is false because module files don't need it, and
2155ffd83dbSDimitry Andric // this allows the file to still be mmapped.
2165ffd83dbSDimitry Andric Buf = FileMgr.getBufferForFile(NewModule->File,
2175ffd83dbSDimitry Andric /*IsVolatile=*/true,
2185ffd83dbSDimitry Andric /*RequiresNullTerminator=*/false);
2190b57cec5SDimitry Andric }
2200b57cec5SDimitry Andric
2210b57cec5SDimitry Andric if (!Buf) {
2220b57cec5SDimitry Andric ErrorStr = Buf.getError().message();
2230b57cec5SDimitry Andric return Missing;
2240b57cec5SDimitry Andric }
2250b57cec5SDimitry Andric
2260b57cec5SDimitry Andric NewModule->Buffer = &getModuleCache().addPCM(FileName, std::move(*Buf));
2270b57cec5SDimitry Andric }
2280b57cec5SDimitry Andric
2290b57cec5SDimitry Andric // Initialize the stream.
2300b57cec5SDimitry Andric NewModule->Data = PCHContainerRdr.ExtractPCH(*NewModule->Buffer);
2310b57cec5SDimitry Andric
2320b57cec5SDimitry Andric // Read the signature eagerly now so that we can check it. Avoid calling
2330b57cec5SDimitry Andric // ReadSignature unless there's something to check though.
2340b57cec5SDimitry Andric if (ExpectedSignature && checkSignature(ReadSignature(NewModule->Data),
235a7dea167SDimitry Andric ExpectedSignature, ErrorStr))
2360b57cec5SDimitry Andric return OutOfDate;
2370b57cec5SDimitry Andric
2380b57cec5SDimitry Andric // We're keeping this module. Store it everywhere.
2390b57cec5SDimitry Andric Module = Modules[Entry] = NewModule.get();
2400b57cec5SDimitry Andric
2410b57cec5SDimitry Andric updateModuleImports(*NewModule, ImportedBy, ImportLoc);
2420b57cec5SDimitry Andric
2430b57cec5SDimitry Andric if (!NewModule->isModule())
2440b57cec5SDimitry Andric PCHChain.push_back(NewModule.get());
2450b57cec5SDimitry Andric if (!ImportedBy)
2460b57cec5SDimitry Andric Roots.push_back(NewModule.get());
2470b57cec5SDimitry Andric
2480b57cec5SDimitry Andric Chain.push_back(std::move(NewModule));
2490b57cec5SDimitry Andric return NewlyLoaded;
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric
removeModules(ModuleIterator First,ModuleMap * modMap)252480093f4SDimitry Andric void ModuleManager::removeModules(ModuleIterator First, ModuleMap *modMap) {
2530b57cec5SDimitry Andric auto Last = end();
2540b57cec5SDimitry Andric if (First == Last)
2550b57cec5SDimitry Andric return;
2560b57cec5SDimitry Andric
2570b57cec5SDimitry Andric // Explicitly clear VisitOrder since we might not notice it is stale.
2580b57cec5SDimitry Andric VisitOrder.clear();
2590b57cec5SDimitry Andric
2600b57cec5SDimitry Andric // Collect the set of module file pointers that we'll be removing.
2610b57cec5SDimitry Andric llvm::SmallPtrSet<ModuleFile *, 4> victimSet(
2620b57cec5SDimitry Andric (llvm::pointer_iterator<ModuleIterator>(First)),
2630b57cec5SDimitry Andric (llvm::pointer_iterator<ModuleIterator>(Last)));
2640b57cec5SDimitry Andric
2650b57cec5SDimitry Andric auto IsVictim = [&](ModuleFile *MF) {
2660b57cec5SDimitry Andric return victimSet.count(MF);
2670b57cec5SDimitry Andric };
2680b57cec5SDimitry Andric // Remove any references to the now-destroyed modules.
2690b57cec5SDimitry Andric for (auto I = begin(); I != First; ++I) {
2700b57cec5SDimitry Andric I->Imports.remove_if(IsVictim);
2710b57cec5SDimitry Andric I->ImportedBy.remove_if(IsVictim);
2720b57cec5SDimitry Andric }
2730b57cec5SDimitry Andric Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim),
2740b57cec5SDimitry Andric Roots.end());
2750b57cec5SDimitry Andric
2760b57cec5SDimitry Andric // Remove the modules from the PCH chain.
2770b57cec5SDimitry Andric for (auto I = First; I != Last; ++I) {
2780b57cec5SDimitry Andric if (!I->isModule()) {
2790b57cec5SDimitry Andric PCHChain.erase(llvm::find(PCHChain, &*I), PCHChain.end());
2800b57cec5SDimitry Andric break;
2810b57cec5SDimitry Andric }
2820b57cec5SDimitry Andric }
2830b57cec5SDimitry Andric
2840b57cec5SDimitry Andric // Delete the modules and erase them from the various structures.
2850b57cec5SDimitry Andric for (ModuleIterator victim = First; victim != Last; ++victim) {
2860b57cec5SDimitry Andric Modules.erase(victim->File);
2870b57cec5SDimitry Andric
2880b57cec5SDimitry Andric if (modMap) {
2890b57cec5SDimitry Andric StringRef ModuleName = victim->ModuleName;
2900b57cec5SDimitry Andric if (Module *mod = modMap->findModule(ModuleName)) {
291*af732203SDimitry Andric mod->setASTFile(None);
2920b57cec5SDimitry Andric }
2930b57cec5SDimitry Andric }
2940b57cec5SDimitry Andric }
2950b57cec5SDimitry Andric
2960b57cec5SDimitry Andric // Delete the modules.
2970b57cec5SDimitry Andric Chain.erase(Chain.begin() + (First - begin()), Chain.end());
2980b57cec5SDimitry Andric }
2990b57cec5SDimitry Andric
3000b57cec5SDimitry Andric void
addInMemoryBuffer(StringRef FileName,std::unique_ptr<llvm::MemoryBuffer> Buffer)3010b57cec5SDimitry Andric ModuleManager::addInMemoryBuffer(StringRef FileName,
3020b57cec5SDimitry Andric std::unique_ptr<llvm::MemoryBuffer> Buffer) {
3030b57cec5SDimitry Andric const FileEntry *Entry =
3040b57cec5SDimitry Andric FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
3050b57cec5SDimitry Andric InMemoryBuffers[Entry] = std::move(Buffer);
3060b57cec5SDimitry Andric }
3070b57cec5SDimitry Andric
allocateVisitState()3080b57cec5SDimitry Andric ModuleManager::VisitState *ModuleManager::allocateVisitState() {
3090b57cec5SDimitry Andric // Fast path: if we have a cached state, use it.
3100b57cec5SDimitry Andric if (FirstVisitState) {
3110b57cec5SDimitry Andric VisitState *Result = FirstVisitState;
3120b57cec5SDimitry Andric FirstVisitState = FirstVisitState->NextState;
3130b57cec5SDimitry Andric Result->NextState = nullptr;
3140b57cec5SDimitry Andric return Result;
3150b57cec5SDimitry Andric }
3160b57cec5SDimitry Andric
3170b57cec5SDimitry Andric // Allocate and return a new state.
3180b57cec5SDimitry Andric return new VisitState(size());
3190b57cec5SDimitry Andric }
3200b57cec5SDimitry Andric
returnVisitState(VisitState * State)3210b57cec5SDimitry Andric void ModuleManager::returnVisitState(VisitState *State) {
3220b57cec5SDimitry Andric assert(State->NextState == nullptr && "Visited state is in list?");
3230b57cec5SDimitry Andric State->NextState = FirstVisitState;
3240b57cec5SDimitry Andric FirstVisitState = State;
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric
setGlobalIndex(GlobalModuleIndex * Index)3270b57cec5SDimitry Andric void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
3280b57cec5SDimitry Andric GlobalIndex = Index;
3290b57cec5SDimitry Andric if (!GlobalIndex) {
3300b57cec5SDimitry Andric ModulesInCommonWithGlobalIndex.clear();
3310b57cec5SDimitry Andric return;
3320b57cec5SDimitry Andric }
3330b57cec5SDimitry Andric
3340b57cec5SDimitry Andric // Notify the global module index about all of the modules we've already
3350b57cec5SDimitry Andric // loaded.
3360b57cec5SDimitry Andric for (ModuleFile &M : *this)
3370b57cec5SDimitry Andric if (!GlobalIndex->loadedModuleFile(&M))
3380b57cec5SDimitry Andric ModulesInCommonWithGlobalIndex.push_back(&M);
3390b57cec5SDimitry Andric }
3400b57cec5SDimitry Andric
moduleFileAccepted(ModuleFile * MF)3410b57cec5SDimitry Andric void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
3420b57cec5SDimitry Andric if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
3430b57cec5SDimitry Andric return;
3440b57cec5SDimitry Andric
3450b57cec5SDimitry Andric ModulesInCommonWithGlobalIndex.push_back(MF);
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric
ModuleManager(FileManager & FileMgr,InMemoryModuleCache & ModuleCache,const PCHContainerReader & PCHContainerRdr,const HeaderSearch & HeaderSearchInfo)3480b57cec5SDimitry Andric ModuleManager::ModuleManager(FileManager &FileMgr,
3490b57cec5SDimitry Andric InMemoryModuleCache &ModuleCache,
3500b57cec5SDimitry Andric const PCHContainerReader &PCHContainerRdr,
3510b57cec5SDimitry Andric const HeaderSearch &HeaderSearchInfo)
3520b57cec5SDimitry Andric : FileMgr(FileMgr), ModuleCache(&ModuleCache),
3530b57cec5SDimitry Andric PCHContainerRdr(PCHContainerRdr), HeaderSearchInfo(HeaderSearchInfo) {}
3540b57cec5SDimitry Andric
~ModuleManager()3550b57cec5SDimitry Andric ModuleManager::~ModuleManager() { delete FirstVisitState; }
3560b57cec5SDimitry Andric
visit(llvm::function_ref<bool (ModuleFile & M)> Visitor,llvm::SmallPtrSetImpl<ModuleFile * > * ModuleFilesHit)3570b57cec5SDimitry Andric void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
3580b57cec5SDimitry Andric llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
3590b57cec5SDimitry Andric // If the visitation order vector is the wrong size, recompute the order.
3600b57cec5SDimitry Andric if (VisitOrder.size() != Chain.size()) {
3610b57cec5SDimitry Andric unsigned N = size();
3620b57cec5SDimitry Andric VisitOrder.clear();
3630b57cec5SDimitry Andric VisitOrder.reserve(N);
3640b57cec5SDimitry Andric
3650b57cec5SDimitry Andric // Record the number of incoming edges for each module. When we
3660b57cec5SDimitry Andric // encounter a module with no incoming edges, push it into the queue
3670b57cec5SDimitry Andric // to seed the queue.
3680b57cec5SDimitry Andric SmallVector<ModuleFile *, 4> Queue;
3690b57cec5SDimitry Andric Queue.reserve(N);
3700b57cec5SDimitry Andric llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
3710b57cec5SDimitry Andric UnusedIncomingEdges.resize(size());
3720b57cec5SDimitry Andric for (ModuleFile &M : llvm::reverse(*this)) {
3730b57cec5SDimitry Andric unsigned Size = M.ImportedBy.size();
3740b57cec5SDimitry Andric UnusedIncomingEdges[M.Index] = Size;
3750b57cec5SDimitry Andric if (!Size)
3760b57cec5SDimitry Andric Queue.push_back(&M);
3770b57cec5SDimitry Andric }
3780b57cec5SDimitry Andric
3790b57cec5SDimitry Andric // Traverse the graph, making sure to visit a module before visiting any
3800b57cec5SDimitry Andric // of its dependencies.
3810b57cec5SDimitry Andric while (!Queue.empty()) {
3820b57cec5SDimitry Andric ModuleFile *CurrentModule = Queue.pop_back_val();
3830b57cec5SDimitry Andric VisitOrder.push_back(CurrentModule);
3840b57cec5SDimitry Andric
3850b57cec5SDimitry Andric // For any module that this module depends on, push it on the
3860b57cec5SDimitry Andric // stack (if it hasn't already been marked as visited).
3870b57cec5SDimitry Andric for (auto M = CurrentModule->Imports.rbegin(),
3880b57cec5SDimitry Andric MEnd = CurrentModule->Imports.rend();
3890b57cec5SDimitry Andric M != MEnd; ++M) {
3900b57cec5SDimitry Andric // Remove our current module as an impediment to visiting the
3910b57cec5SDimitry Andric // module we depend on. If we were the last unvisited module
3920b57cec5SDimitry Andric // that depends on this particular module, push it into the
3930b57cec5SDimitry Andric // queue to be visited.
3940b57cec5SDimitry Andric unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
3950b57cec5SDimitry Andric if (NumUnusedEdges && (--NumUnusedEdges == 0))
3960b57cec5SDimitry Andric Queue.push_back(*M);
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric }
3990b57cec5SDimitry Andric
4000b57cec5SDimitry Andric assert(VisitOrder.size() == N && "Visitation order is wrong?");
4010b57cec5SDimitry Andric
4020b57cec5SDimitry Andric delete FirstVisitState;
4030b57cec5SDimitry Andric FirstVisitState = nullptr;
4040b57cec5SDimitry Andric }
4050b57cec5SDimitry Andric
4060b57cec5SDimitry Andric VisitState *State = allocateVisitState();
4070b57cec5SDimitry Andric unsigned VisitNumber = State->NextVisitNumber++;
4080b57cec5SDimitry Andric
4090b57cec5SDimitry Andric // If the caller has provided us with a hit-set that came from the global
4100b57cec5SDimitry Andric // module index, mark every module file in common with the global module
4110b57cec5SDimitry Andric // index that is *not* in that set as 'visited'.
4120b57cec5SDimitry Andric if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
4130b57cec5SDimitry Andric for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
4140b57cec5SDimitry Andric {
4150b57cec5SDimitry Andric ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
4160b57cec5SDimitry Andric if (!ModuleFilesHit->count(M))
4170b57cec5SDimitry Andric State->VisitNumber[M->Index] = VisitNumber;
4180b57cec5SDimitry Andric }
4190b57cec5SDimitry Andric }
4200b57cec5SDimitry Andric
4210b57cec5SDimitry Andric for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
4220b57cec5SDimitry Andric ModuleFile *CurrentModule = VisitOrder[I];
4230b57cec5SDimitry Andric // Should we skip this module file?
4240b57cec5SDimitry Andric if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
4250b57cec5SDimitry Andric continue;
4260b57cec5SDimitry Andric
4270b57cec5SDimitry Andric // Visit the module.
4280b57cec5SDimitry Andric assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
4290b57cec5SDimitry Andric State->VisitNumber[CurrentModule->Index] = VisitNumber;
4300b57cec5SDimitry Andric if (!Visitor(*CurrentModule))
4310b57cec5SDimitry Andric continue;
4320b57cec5SDimitry Andric
4330b57cec5SDimitry Andric // The visitor has requested that cut off visitation of any
4340b57cec5SDimitry Andric // module that the current module depends on. To indicate this
4350b57cec5SDimitry Andric // behavior, we mark all of the reachable modules as having been visited.
4360b57cec5SDimitry Andric ModuleFile *NextModule = CurrentModule;
4370b57cec5SDimitry Andric do {
4380b57cec5SDimitry Andric // For any module that this module depends on, push it on the
4390b57cec5SDimitry Andric // stack (if it hasn't already been marked as visited).
4400b57cec5SDimitry Andric for (llvm::SetVector<ModuleFile *>::iterator
4410b57cec5SDimitry Andric M = NextModule->Imports.begin(),
4420b57cec5SDimitry Andric MEnd = NextModule->Imports.end();
4430b57cec5SDimitry Andric M != MEnd; ++M) {
4440b57cec5SDimitry Andric if (State->VisitNumber[(*M)->Index] != VisitNumber) {
4450b57cec5SDimitry Andric State->Stack.push_back(*M);
4460b57cec5SDimitry Andric State->VisitNumber[(*M)->Index] = VisitNumber;
4470b57cec5SDimitry Andric }
4480b57cec5SDimitry Andric }
4490b57cec5SDimitry Andric
4500b57cec5SDimitry Andric if (State->Stack.empty())
4510b57cec5SDimitry Andric break;
4520b57cec5SDimitry Andric
4530b57cec5SDimitry Andric // Pop the next module off the stack.
4540b57cec5SDimitry Andric NextModule = State->Stack.pop_back_val();
4550b57cec5SDimitry Andric } while (true);
4560b57cec5SDimitry Andric }
4570b57cec5SDimitry Andric
4580b57cec5SDimitry Andric returnVisitState(State);
4590b57cec5SDimitry Andric }
4600b57cec5SDimitry Andric
lookupModuleFile(StringRef FileName,off_t ExpectedSize,time_t ExpectedModTime,Optional<FileEntryRef> & File)461*af732203SDimitry Andric bool ModuleManager::lookupModuleFile(StringRef FileName, off_t ExpectedSize,
4620b57cec5SDimitry Andric time_t ExpectedModTime,
463*af732203SDimitry Andric Optional<FileEntryRef> &File) {
464*af732203SDimitry Andric File = None;
465*af732203SDimitry Andric if (FileName == "-")
4660b57cec5SDimitry Andric return false;
4670b57cec5SDimitry Andric
4680b57cec5SDimitry Andric // Open the file immediately to ensure there is no race between stat'ing and
4690b57cec5SDimitry Andric // opening the file.
470*af732203SDimitry Andric Optional<FileEntryRef> FileOrErr =
471*af732203SDimitry Andric expectedToOptional(FileMgr.getFileRef(FileName, /*OpenFile=*/true,
472*af732203SDimitry Andric /*CacheFailure=*/false));
473*af732203SDimitry Andric if (!FileOrErr)
4740b57cec5SDimitry Andric return false;
475*af732203SDimitry Andric
476a7dea167SDimitry Andric File = *FileOrErr;
4770b57cec5SDimitry Andric
4780b57cec5SDimitry Andric if ((ExpectedSize && ExpectedSize != File->getSize()) ||
4790b57cec5SDimitry Andric (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
4800b57cec5SDimitry Andric // Do not destroy File, as it may be referenced. If we need to rebuild it,
4810b57cec5SDimitry Andric // it will be destroyed by removeModules.
4820b57cec5SDimitry Andric return true;
4830b57cec5SDimitry Andric
4840b57cec5SDimitry Andric return false;
4850b57cec5SDimitry Andric }
4860b57cec5SDimitry Andric
4870b57cec5SDimitry Andric #ifndef NDEBUG
4880b57cec5SDimitry Andric namespace llvm {
4890b57cec5SDimitry Andric
4900b57cec5SDimitry Andric template<>
4910b57cec5SDimitry Andric struct GraphTraits<ModuleManager> {
4920b57cec5SDimitry Andric using NodeRef = ModuleFile *;
4930b57cec5SDimitry Andric using ChildIteratorType = llvm::SetVector<ModuleFile *>::const_iterator;
4940b57cec5SDimitry Andric using nodes_iterator = pointer_iterator<ModuleManager::ModuleConstIterator>;
4950b57cec5SDimitry Andric
child_beginllvm::GraphTraits4960b57cec5SDimitry Andric static ChildIteratorType child_begin(NodeRef Node) {
4970b57cec5SDimitry Andric return Node->Imports.begin();
4980b57cec5SDimitry Andric }
4990b57cec5SDimitry Andric
child_endllvm::GraphTraits5000b57cec5SDimitry Andric static ChildIteratorType child_end(NodeRef Node) {
5010b57cec5SDimitry Andric return Node->Imports.end();
5020b57cec5SDimitry Andric }
5030b57cec5SDimitry Andric
nodes_beginllvm::GraphTraits5040b57cec5SDimitry Andric static nodes_iterator nodes_begin(const ModuleManager &Manager) {
5050b57cec5SDimitry Andric return nodes_iterator(Manager.begin());
5060b57cec5SDimitry Andric }
5070b57cec5SDimitry Andric
nodes_endllvm::GraphTraits5080b57cec5SDimitry Andric static nodes_iterator nodes_end(const ModuleManager &Manager) {
5090b57cec5SDimitry Andric return nodes_iterator(Manager.end());
5100b57cec5SDimitry Andric }
5110b57cec5SDimitry Andric };
5120b57cec5SDimitry Andric
5130b57cec5SDimitry Andric template<>
5140b57cec5SDimitry Andric struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
DOTGraphTraitsllvm::DOTGraphTraits5150b57cec5SDimitry Andric explicit DOTGraphTraits(bool IsSimple = false)
5160b57cec5SDimitry Andric : DefaultDOTGraphTraits(IsSimple) {}
5170b57cec5SDimitry Andric
renderGraphFromBottomUpllvm::DOTGraphTraits5180b57cec5SDimitry Andric static bool renderGraphFromBottomUp() { return true; }
5190b57cec5SDimitry Andric
getNodeLabelllvm::DOTGraphTraits5200b57cec5SDimitry Andric std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
5210b57cec5SDimitry Andric return M->ModuleName;
5220b57cec5SDimitry Andric }
5230b57cec5SDimitry Andric };
5240b57cec5SDimitry Andric
5250b57cec5SDimitry Andric } // namespace llvm
5260b57cec5SDimitry Andric
viewGraph()5270b57cec5SDimitry Andric void ModuleManager::viewGraph() {
5280b57cec5SDimitry Andric llvm::ViewGraph(*this, "Modules");
5290b57cec5SDimitry Andric }
5300b57cec5SDimitry Andric #endif
531