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