clang 24.0.0git
InMemoryModuleCache.h
Go to the documentation of this file.
1//===- InMemoryModuleCache.h - In-memory cache for modules ------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_SERIALIZATION_INMEMORYMODULECACHE_H
10#define LLVM_CLANG_SERIALIZATION_INMEMORYMODULECACHE_H
11
12#include "llvm/ADT/IntrusiveRefCntPtr.h"
13#include "llvm/ADT/StringMap.h"
14#include "llvm/Support/MemoryBuffer.h"
15#include <memory>
16
17namespace clang {
19
20/// In-memory cache for modules.
21///
22/// This is a cache for modules for use across a compilation, sharing state
23/// between the CompilerInstances in a modules build. It must be shared by each
24/// CompilerInstance, ASTReader, ASTWriter, and ModuleManager that are
25/// coordinating.
26///
27/// Critically, it ensures that a single process has a consistent view of each
28/// implicitly-built PCM. This is used by \a CompilerInstance when building PCMs
29/// to ensure that each \a ModuleManager sees the same files.
30class InMemoryModuleCache : public llvm::RefCountedBase<InMemoryModuleCache> {
31 struct PCM {
32 /// The contents of the PCM as produced by \c ASTWriter.
33 std::unique_ptr<llvm::MemoryBuffer> Buffer;
34
35 /// The size of this PCM. This may be different from the size of \c Buffer
36 /// when it's wrapped in an object file.
37 off_t Size = 0;
38
39 /// The modification time of this PCM.
40 time_t ModTime = 0;
41
42 /// Track whether this PCM is known to be good (either built or
43 /// successfully imported by a CompilerInstance/ASTReader using this
44 /// cache).
45 bool IsFinal = false;
46
47 PCM() = default;
48 PCM(std::unique_ptr<llvm::MemoryBuffer> Buffer, off_t Size, time_t ModTime)
49 : Buffer(std::move(Buffer)), Size(Size), ModTime(ModTime) {}
50 };
51
52 /// Cache of buffers.
53 llvm::StringMap<PCM> PCMs;
54
55 AtomicLineLogger &Logger;
56
57public:
58 explicit InMemoryModuleCache(AtomicLineLogger &Logger) : Logger(Logger) {}
59
60 /// There are four states for a PCM. It must monotonically increase.
61 ///
62 /// 1. Unknown: the PCM has neither been read from disk nor built.
63 /// 2. Tentative: the PCM has been read from disk but not yet imported or
64 /// built. It might work.
65 /// 3. ToBuild: the PCM read from disk did not work but a new one has not
66 /// been built yet.
67 /// 4. Final: indicating that the current PCM was either built in this
68 /// process or has been successfully imported.
70
71 /// Get the state of the PCM.
72 State getPCMState(llvm::StringRef Filename) const;
73
74 /// Store the PCM under the Filename.
75 ///
76 /// \pre state is Unknown
77 /// \post state is Tentative
78 /// \return a reference to the buffer as a convenience.
79 llvm::MemoryBuffer &addPCM(llvm::StringRef Filename,
80 std::unique_ptr<llvm::MemoryBuffer> Buffer,
81 off_t Size, time_t ModTime);
82
83 /// Store a just-built PCM under the Filename.
84 ///
85 /// \pre state is Unknown or ToBuild.
86 /// \pre state is not Tentative.
87 /// \return a reference to the buffer as a convenience.
88 llvm::MemoryBuffer &addBuiltPCM(llvm::StringRef Filename,
89 std::unique_ptr<llvm::MemoryBuffer> Buffer,
90 off_t Size, time_t ModTime);
91
92 /// Try to remove a buffer from the cache. No effect if state is Final.
93 ///
94 /// \pre state is Tentative/Final.
95 /// \post Tentative => ToBuild or Final => Final.
96 /// \return false on success, i.e. if Tentative => ToBuild.
97 bool tryToDropPCM(llvm::StringRef Filename);
98
99 /// Mark a PCM as final.
100 ///
101 /// \pre state is Tentative or Final.
102 /// \post state is Final.
103 void finalizePCM(llvm::StringRef Filename);
104
105 /// Get a pointer to the PCM if it exists and set \c Size and \c ModTime to
106 /// its on-disk size and modification time. Otherwise, return nullptr and
107 /// don't change \c Size and \c ModTime.
108 llvm::MemoryBuffer *lookupPCM(llvm::StringRef Filename, off_t &Size,
109 time_t &ModTime) const;
110
111 /// Check whether the PCM is final and has been shown to work.
112 ///
113 /// \return true iff state is Final.
114 bool isPCMFinal(llvm::StringRef Filename) const;
115
116 /// Check whether the PCM is waiting to be built.
117 ///
118 /// \return true iff state is ToBuild.
119 bool shouldBuildPCM(llvm::StringRef Filename) const;
120};
121
122} // end namespace clang
123
124#endif // LLVM_CLANG_SERIALIZATION_INMEMORYMODULECACHE_H
llvm::MemoryBuffer * lookupPCM(llvm::StringRef Filename, off_t &Size, time_t &ModTime) const
Get a pointer to the PCM if it exists and set Size and ModTime to its on-disk size and modification t...
llvm::MemoryBuffer & addPCM(llvm::StringRef Filename, std::unique_ptr< llvm::MemoryBuffer > Buffer, off_t Size, time_t ModTime)
Store the PCM under the Filename.
State getPCMState(llvm::StringRef Filename) const
Get the state of the PCM.
State
There are four states for a PCM.
bool isPCMFinal(llvm::StringRef Filename) const
Check whether the PCM is final and has been shown to work.
InMemoryModuleCache(AtomicLineLogger &Logger)
void finalizePCM(llvm::StringRef Filename)
Mark a PCM as final.
bool tryToDropPCM(llvm::StringRef Filename)
Try to remove a buffer from the cache.
bool shouldBuildPCM(llvm::StringRef Filename) const
Check whether the PCM is waiting to be built.
llvm::MemoryBuffer & addBuiltPCM(llvm::StringRef Filename, std::unique_ptr< llvm::MemoryBuffer > Buffer, off_t Size, time_t ModTime)
Store a just-built PCM under the Filename.
The JSON file list parser is used to communicate input to InstallAPI.