Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:06

0001 //===- InMemoryModuleCache.h - In-memory cache for modules ------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_SERIALIZATION_INMEMORYMODULECACHE_H
0010 #define LLVM_CLANG_SERIALIZATION_INMEMORYMODULECACHE_H
0011 
0012 #include "llvm/ADT/IntrusiveRefCntPtr.h"
0013 #include "llvm/ADT/StringMap.h"
0014 #include "llvm/Support/MemoryBuffer.h"
0015 #include <memory>
0016 
0017 namespace clang {
0018 
0019 /// In-memory cache for modules.
0020 ///
0021 /// This is a cache for modules for use across a compilation, sharing state
0022 /// between the CompilerInstances in an implicit modules build.  It must be
0023 /// shared by each CompilerInstance, ASTReader, ASTWriter, and ModuleManager
0024 /// that are coordinating.
0025 ///
0026 /// Critically, it ensures that a single process has a consistent view of each
0027 /// PCM.  This is used by \a CompilerInstance when building PCMs to ensure that
0028 /// each \a ModuleManager sees the same files.
0029 class InMemoryModuleCache : public llvm::RefCountedBase<InMemoryModuleCache> {
0030   struct PCM {
0031     std::unique_ptr<llvm::MemoryBuffer> Buffer;
0032 
0033     /// Track whether this PCM is known to be good (either built or
0034     /// successfully imported by a CompilerInstance/ASTReader using this
0035     /// cache).
0036     bool IsFinal = false;
0037 
0038     PCM() = default;
0039     PCM(std::unique_ptr<llvm::MemoryBuffer> Buffer)
0040         : Buffer(std::move(Buffer)) {}
0041   };
0042 
0043   /// Cache of buffers.
0044   llvm::StringMap<PCM> PCMs;
0045 
0046 public:
0047   /// There are four states for a PCM.  It must monotonically increase.
0048   ///
0049   ///  1. Unknown: the PCM has neither been read from disk nor built.
0050   ///  2. Tentative: the PCM has been read from disk but not yet imported or
0051   ///     built.  It might work.
0052   ///  3. ToBuild: the PCM read from disk did not work but a new one has not
0053   ///     been built yet.
0054   ///  4. Final: indicating that the current PCM was either built in this
0055   ///     process or has been successfully imported.
0056   enum State { Unknown, Tentative, ToBuild, Final };
0057 
0058   /// Get the state of the PCM.
0059   State getPCMState(llvm::StringRef Filename) const;
0060 
0061   /// Store the PCM under the Filename.
0062   ///
0063   /// \pre state is Unknown
0064   /// \post state is Tentative
0065   /// \return a reference to the buffer as a convenience.
0066   llvm::MemoryBuffer &addPCM(llvm::StringRef Filename,
0067                              std::unique_ptr<llvm::MemoryBuffer> Buffer);
0068 
0069   /// Store a just-built PCM under the Filename.
0070   ///
0071   /// \pre state is Unknown or ToBuild.
0072   /// \pre state is not Tentative.
0073   /// \return a reference to the buffer as a convenience.
0074   llvm::MemoryBuffer &addBuiltPCM(llvm::StringRef Filename,
0075                                   std::unique_ptr<llvm::MemoryBuffer> Buffer);
0076 
0077   /// Try to remove a buffer from the cache.  No effect if state is Final.
0078   ///
0079   /// \pre state is Tentative/Final.
0080   /// \post Tentative => ToBuild or Final => Final.
0081   /// \return false on success, i.e. if Tentative => ToBuild.
0082   bool tryToDropPCM(llvm::StringRef Filename);
0083 
0084   /// Mark a PCM as final.
0085   ///
0086   /// \pre state is Tentative or Final.
0087   /// \post state is Final.
0088   void finalizePCM(llvm::StringRef Filename);
0089 
0090   /// Get a pointer to the pCM if it exists; else nullptr.
0091   llvm::MemoryBuffer *lookupPCM(llvm::StringRef Filename) const;
0092 
0093   /// Check whether the PCM is final and has been shown to work.
0094   ///
0095   /// \return true iff state is Final.
0096   bool isPCMFinal(llvm::StringRef Filename) const;
0097 
0098   /// Check whether the PCM is waiting to be built.
0099   ///
0100   /// \return true iff state is ToBuild.
0101   bool shouldBuildPCM(llvm::StringRef Filename) const;
0102 };
0103 
0104 } // end namespace clang
0105 
0106 #endif // LLVM_CLANG_SERIALIZATION_INMEMORYMODULECACHE_H