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