clang 24.0.0git
ModuleCache.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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_MODULECACHE_H
10#define LLVM_CLANG_SERIALIZATION_MODULECACHE_H
11
12#include "clang/Basic/LLVM.h"
13#include "llvm/ADT/DenseMap.h"
14#include "llvm/ADT/STLFunctionalExtras.h"
15#include "llvm/ADT/StringMap.h"
16#include "llvm/Support/FileSystem/UniqueID.h"
17
18#include <ctime>
19#include <memory>
20#include <optional>
21#include <sys/types.h>
22#include <system_error>
23
24namespace llvm {
25class AdvisoryLock;
26class MemoryBuffer;
27class MemoryBufferRef;
28} // namespace llvm
29
30namespace clang {
33
34/// The address of an instance of this class represents the identity of a module
35/// cache directory.
37
38/// The module cache used for compiling modules implicitly. This centralizes the
39/// operations the compiler might want to perform on the cache.
41 /// Mapping from a path to the module cache directory identity.
42 llvm::StringMap<const ModuleCacheDirectory *> ByPath;
43
44 /// Mapping from the filesystem entity to the module cache directory identity.
45 llvm::DenseMap<llvm::sys::fs::UniqueID, std::unique_ptr<ModuleCacheDirectory>>
46 ByUID;
47
48protected:
49 /// A logger to record timestamp read/write and file read/write.
51
52public:
54
55 /// Returns an opaque pointer representing the module cache directory. This
56 /// returns the same pointer regardless of the path spelling, as long as it
57 /// resolves to the same file system entity. This also resolves links in the
58 /// path. This may return nullptr if the module cache does not exist.
59 virtual const ModuleCacheDirectory *getDirectoryPtr(StringRef Path);
60
61 /// Returns lock for the given module file. The lock is initially unlocked.
62 virtual std::unique_ptr<llvm::AdvisoryLock>
63 getLock(StringRef ModuleFilename) = 0;
64
65 // TODO: Abstract away timestamps with isUpToDate() and markUpToDate().
66 // TODO: Consider exposing a "validation lock" API to prevent multiple clients
67 // concurrently noticing an out-of-date module file and validating its inputs.
68
69 /// Returns the timestamp denoting the last time inputs of the module file
70 /// were validated.
71 virtual std::time_t getModuleTimestamp(StringRef ModuleFilename) = 0;
72
73 /// Updates the timestamp denoting the last time inputs of the module file
74 /// were validated.
75 virtual void updateModuleTimestamp(StringRef ModuleFilename) = 0;
76
77 /// Whether the build system reported that \p Directory changed before the
78 /// build session started, or std::nullopt if this cache can't tell and the
79 /// file system has to be checked instead.
80 virtual std::optional<bool> isDirectoryInvalidated(StringRef Directory) {
81 return std::nullopt;
82 }
83
84 /// Prune module files that haven't been accessed in a long time.
85 virtual void maybePrune(StringRef Path, time_t PruneInterval,
86 time_t PruneAfter) = 0;
87
88 /// Returns this process's view of the module cache.
90 virtual const InMemoryModuleCache &getInMemoryModuleCache() const = 0;
91
92 /// Write the PCM contents to the given path in the module cache.
93 virtual std::error_code write(StringRef Path, llvm::MemoryBufferRef Buffer,
94 off_t &Size, time_t &ModTime) = 0;
95
97 read(StringRef FileName, off_t &Size, time_t &ModTime) = 0;
98
99 virtual ~ModuleCache() = default;
100
102};
103
104/// Creates new \c ModuleCache backed by a file system directory that may be
105/// operated on by multiple processes. This instance must be used across all
106/// \c CompilerInstance instances participating in building modules for single
107/// translation unit in order to share the same \c InMemoryModuleCache.
108std::shared_ptr<ModuleCache> createCrossProcessModuleCache();
109
110/// Shared implementation of `ModuleCache::maybePrune()`.
111///
112/// If \p OnPrune is non-empty, it is invoked once per file or directory that
113/// is successfully removed from the cache. The path passed to \p OnPrune is
114/// absolute.
115void maybePruneImpl(StringRef Path, time_t PruneInterval, time_t PruneAfter,
116 bool PruneTopLevel = false,
117 llvm::function_ref<void(StringRef)> OnPrune = {});
118
119/// Shared implementation of `ModuleCache::write()`.
120std::error_code writeImpl(StringRef Path, llvm::MemoryBufferRef Buffer,
121 off_t &Size, time_t &ModTime);
122
123/// Shared implementation of `ModuleCache::read()`.
125readImpl(StringRef FileName, off_t &Size, time_t &ModTime);
126} // namespace clang
127
128#endif
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
In-memory cache for modules.
The address of an instance of this class represents the identity of a module cache directory.
Definition ModuleCache.h:36
virtual std::optional< bool > isDirectoryInvalidated(StringRef Directory)
Whether the build system reported that Directory changed before the build session started,...
Definition ModuleCache.h:80
AtomicLineLogger & getLogger()
virtual ~ModuleCache()=default
virtual std::error_code write(StringRef Path, llvm::MemoryBufferRef Buffer, off_t &Size, time_t &ModTime)=0
Write the PCM contents to the given path in the module cache.
virtual const InMemoryModuleCache & getInMemoryModuleCache() const =0
virtual std::time_t getModuleTimestamp(StringRef ModuleFilename)=0
Returns the timestamp denoting the last time inputs of the module file were validated.
AtomicLineLogger & Logger
A logger to record timestamp read/write and file read/write.
Definition ModuleCache.h:50
virtual InMemoryModuleCache & getInMemoryModuleCache()=0
Returns this process's view of the module cache.
virtual void updateModuleTimestamp(StringRef ModuleFilename)=0
Updates the timestamp denoting the last time inputs of the module file were validated.
virtual Expected< std::unique_ptr< llvm::MemoryBuffer > > read(StringRef FileName, off_t &Size, time_t &ModTime)=0
ModuleCache(AtomicLineLogger &Logger)
Definition ModuleCache.h:53
virtual const ModuleCacheDirectory * getDirectoryPtr(StringRef Path)
Returns an opaque pointer representing the module cache directory.
virtual void maybePrune(StringRef Path, time_t PruneInterval, time_t PruneAfter)=0
Prune module files that haven't been accessed in a long time.
virtual std::unique_ptr< llvm::AdvisoryLock > getLock(StringRef ModuleFilename)=0
Returns lock for the given module file. The lock is initially unlocked.
Top level wrappers for InstallAPI frontend operations.
Expected< std::unique_ptr< llvm::MemoryBuffer > > readImpl(StringRef FileName, off_t &Size, time_t &ModTime)
Shared implementation of ModuleCache::read().
std::shared_ptr< ModuleCache > createCrossProcessModuleCache()
Creates new ModuleCache backed by a file system directory that may be operated on by multiple process...
std::error_code writeImpl(StringRef Path, llvm::MemoryBufferRef Buffer, off_t &Size, time_t &ModTime)
Shared implementation of ModuleCache::write().
void maybePruneImpl(StringRef Path, time_t PruneInterval, time_t PruneAfter, bool PruneTopLevel=false, llvm::function_ref< void(StringRef)> OnPrune={})
Shared implementation of ModuleCache::maybePrune().
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30