clang-tools  14.0.0git
FileCache.cpp
Go to the documentation of this file.
1 //===--- FileCache.cpp ----------------------------------------------------===//
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 #include "support/FileCache.h"
10 
11 namespace clang {
12 namespace clangd {
13 
14 // Sentinel values for the Size cache key. In both cases, a successful stat of
15 // the file will never result in the cached value being reused.
16 
17 // The cached value does not reflect the current content on disk.
18 static constexpr uint64_t CacheDiskMismatch =
19  std::numeric_limits<uint64_t>::max();
20 // The cached value reflects that the file doesn't exist.
21 static constexpr uint64_t FileNotFound = CacheDiskMismatch - 1;
22 
23 FileCache::FileCache(llvm::StringRef Path)
24  : Path(Path), ValidTime(std::chrono::steady_clock::time_point::min()),
25  ModifiedTime(), Size(CacheDiskMismatch) {
26  assert(llvm::sys::path::is_absolute(Path));
27 }
28 
30  const ThreadsafeFS &TFS, std::chrono::steady_clock::time_point FreshTime,
31  llvm::function_ref<void(llvm::Optional<llvm::StringRef>)> Parse,
32  llvm::function_ref<void()> Read) const {
33 
34  std::lock_guard<std::mutex> Lock(Mu);
35  // We're going to update the cache and return whatever's in it.
36  auto Return = llvm::make_scope_exit(Read);
37 
38  // Return any sufficiently recent result without doing any further work.
39  if (ValidTime > FreshTime)
40  return;
41 
42  // Ensure we always bump ValidTime, so that FreshTime imposes a hard limit on
43  // how often we do IO.
44  auto BumpValidTime = llvm::make_scope_exit(
45  [&] { ValidTime = std::chrono::steady_clock::now(); });
46 
47  // stat is cheaper than opening the file. It's usually unchanged.
48  assert(llvm::sys::path::is_absolute(Path));
49  auto FS = TFS.view(/*CWD=*/llvm::None);
50  auto Stat = FS->status(Path);
51  if (!Stat || !Stat->isRegularFile()) {
52  if (Size != FileNotFound) // Allow "not found" value to be cached.
53  Parse(llvm::None);
54  // Ensure the cache key won't match any future stat().
55  Size = FileNotFound;
56  return;
57  }
58  // If the modified-time and size match, assume the content does too.
59  if (Size == Stat->getSize() &&
60  ModifiedTime == Stat->getLastModificationTime())
61  return;
62 
63  // OK, the file has actually changed. Update cache key, compute new value.
64  Size = Stat->getSize();
65  ModifiedTime = Stat->getLastModificationTime();
66  // Now read the file from disk.
67  if (auto Buf = FS->getBufferForFile(Path)) {
68  Parse(Buf->get()->getBuffer());
69  // Result is cacheable if the actual read size matches the new cache key.
70  // (We can't update the cache key, because we don't know the new mtime).
71  if (Buf->get()->getBufferSize() != Size)
72  Size = CacheDiskMismatch;
73  } else {
74  // File was unreadable. Keep the old value and try again next time.
75  Size = CacheDiskMismatch;
76  }
77 }
78 
79 } // namespace clangd
80 } // namespace clang
clang::clangd::Path
std::string Path
A typedef to represent a file path.
Definition: Path.h:26
FileCache.h
clang::clangd::FileNotFound
static constexpr uint64_t FileNotFound
Definition: FileCache.cpp:21
clang::clangd::ThreadsafeFS
Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
Definition: ThreadsafeFS.h:28
clang::clangd::ThreadsafeFS::view
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > view(llvm::NoneType CWD) const
Obtain a vfs::FileSystem with an arbitrary initial working directory.
Definition: ThreadsafeFS.h:34
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::DocumentHighlightKind::Read
@ Read
clang::clangd::FileCache::read
void read(const ThreadsafeFS &TFS, std::chrono::steady_clock::time_point FreshTime, llvm::function_ref< void(llvm::Optional< llvm::StringRef >)> Parse, llvm::function_ref< void()> Read) const
Definition: FileCache.cpp:29
clang::clangd::CacheDiskMismatch
static constexpr uint64_t CacheDiskMismatch
Definition: FileCache.cpp:18
clang::clangd::FileCache::FileCache
FileCache(PathRef Path)
Definition: FileCache.cpp:23