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