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 <sys/types.h>
21#include <system_error>
22
23namespace llvm {
24class AdvisoryLock;
25class MemoryBuffer;
26class MemoryBufferRef;
27} // namespace llvm
28
29namespace clang {
32
33/// The address of an instance of this class represents the identity of a module
34/// cache directory.
36
37/// The module cache used for compiling modules implicitly. This centralizes the
38/// operations the compiler might want to perform on the cache.
40 /// Mapping from a path to the module cache directory identity.
41 llvm::StringMap<const ModuleCacheDirectory *> ByPath;
42
43 /// Mapping from the filesystem entity to the module cache directory identity.
44 llvm::DenseMap<llvm::sys::fs::UniqueID, std::unique_ptr<ModuleCacheDirectory>>
45 ByUID;
46
47protected:
48 /// A logger to record timestamp read/write and file read/write.
50
51public:
53
54 /// Returns an opaque pointer representing the module cache directory. This
55 /// returns the same pointer regardless of the path spelling, as long as it
56 /// resolves to the same file system entity. This also resolves links in the
57 /// path. This may return nullptr if the module cache does not exist.
58 virtual const ModuleCacheDirectory *getDirectoryPtr(StringRef Path);
59
60 /// Returns lock for the given module file. The lock is initially unlocked.
61 virtual std::unique_ptr<llvm::AdvisoryLock>
62 getLock(StringRef ModuleFilename) = 0;
63
64 // TODO: Abstract away timestamps with isUpToDate() and markUpToDate().
65 // TODO: Consider exposing a "validation lock" API to prevent multiple clients
66 // concurrently noticing an out-of-date module file and validating its inputs.
67
68 /// Returns the timestamp denoting the last time inputs of the module file
69 /// were validated.
70 virtual std::time_t getModuleTimestamp(StringRef ModuleFilename) = 0;
71
72 /// Updates the timestamp denoting the last time inputs of the module file
73 /// were validated.
74 virtual void updateModuleTimestamp(StringRef ModuleFilename) = 0;
75
76 /// Prune module files that haven't been accessed in a long time.
77 virtual void maybePrune(StringRef Path, time_t PruneInterval,
78 time_t PruneAfter) = 0;
79
80 /// Returns this process's view of the module cache.
82 virtual const InMemoryModuleCache &getInMemoryModuleCache() const = 0;
83
84 /// Write the PCM contents to the given path in the module cache.
85 virtual std::error_code write(StringRef Path, llvm::MemoryBufferRef Buffer,
86 off_t &Size, time_t &ModTime) = 0;
87
89 read(StringRef FileName, off_t &Size, time_t &ModTime) = 0;
90
91 virtual ~ModuleCache() = default;
92
94};
95
96/// Creates new \c ModuleCache backed by a file system directory that may be
97/// operated on by multiple processes. This instance must be used across all
98/// \c CompilerInstance instances participating in building modules for single
99/// translation unit in order to share the same \c InMemoryModuleCache.
100std::shared_ptr<ModuleCache> createCrossProcessModuleCache();
101
102/// Shared implementation of `ModuleCache::maybePrune()`.
103///
104/// If \p OnPrune is non-empty, it is invoked once per file or directory that
105/// is successfully removed from the cache. The path passed to \p OnPrune is
106/// absolute.
107void maybePruneImpl(StringRef Path, time_t PruneInterval, time_t PruneAfter,
108 bool PruneTopLevel = false,
109 llvm::function_ref<void(StringRef)> OnPrune = {});
110
111/// Shared implementation of `ModuleCache::write()`.
112std::error_code writeImpl(StringRef Path, llvm::MemoryBufferRef Buffer,
113 off_t &Size, time_t &ModTime);
114
115/// Shared implementation of `ModuleCache::read()`.
117readImpl(StringRef FileName, off_t &Size, time_t &ModTime);
118} // namespace clang
119
120#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:35
AtomicLineLogger & getLogger()
Definition ModuleCache.h:93
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:49
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:52
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.
The JSON file list parser is used to communicate input to InstallAPI.
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