clang-tools  14.0.0git
FS.h
Go to the documentation of this file.
1 //===--- FS.h - File system related utils ------------------------*- C++-*-===//
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_TOOLS_EXTRA_CLANGD_FS_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FS_H
11 
12 #include "support/Path.h"
13 #include "clang/Basic/LLVM.h"
14 #include "llvm/ADT/Optional.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/Support/VirtualFileSystem.h"
17 
18 namespace clang {
19 namespace clangd {
20 
21 /// Records status information for files open()ed or stat()ed during preamble
22 /// build (except for the main file), so we can avoid stat()s on the underlying
23 /// FS when reusing the preamble. For example, code completion can re-stat files
24 /// when getting FileID for source locations stored in preamble (e.g. checking
25 /// whether a location is in the main file).
26 ///
27 /// The cache is keyed by absolute path of file name in cached status, as this
28 /// is what preamble stores.
29 ///
30 /// The cache is not thread-safe when updates happen, so the use pattern should
31 /// be:
32 /// - One FS writes to the cache from one thread (or several but strictly
33 /// sequenced), e.g. when building preamble.
34 /// - Sequence point (no writes after this point, no reads before).
35 /// - Several FSs can read from the cache, e.g. code completions.
36 ///
37 /// Note that the cache is only valid when reusing preamble.
39 public:
40  /// \p MainFilePath is the absolute path of the main source file this preamble
41  /// corresponds to. The stat for the main file will not be cached.
42  PreambleFileStatusCache(llvm::StringRef MainFilePath);
43 
44  void update(const llvm::vfs::FileSystem &FS, llvm::vfs::Status S);
45 
46  /// \p Path is a path stored in preamble.
47  llvm::Optional<llvm::vfs::Status> lookup(llvm::StringRef Path) const;
48 
49  /// Returns a VFS that collects file status.
50  /// Only cache stats for files that exist because
51  /// 1) we only care about existing files when reusing preamble, unlike
52  /// building preamble.
53  /// 2) we use the file name in the Status as the cache key.
54  ///
55  /// Note that the returned VFS should not outlive the cache.
56  IntrusiveRefCntPtr<llvm::vfs::FileSystem>
57  getProducingFS(IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS);
58 
59  /// Returns a VFS that uses the cache collected.
60  ///
61  /// Note that the returned VFS should not outlive the cache.
62  IntrusiveRefCntPtr<llvm::vfs::FileSystem>
63  getConsumingFS(IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS) const;
64 
65 private:
66  std::string MainFilePath;
67  llvm::StringMap<llvm::vfs::Status> StatCache;
68 };
69 
70 /// Returns a version of \p File that doesn't contain dots and dot dots.
71 /// e.g /a/b/../c -> /a/c
72 /// /a/b/./c -> /a/b/c
73 /// FIXME: We should avoid encountering such paths in clangd internals by
74 /// filtering everything we get over LSP, CDB, etc.
75 Path removeDots(PathRef File);
76 
77 } // namespace clangd
78 } // namespace clang
79 
80 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_FS_H
clang::clangd::Path
std::string Path
A typedef to represent a file path.
Definition: Path.h:26
Path.h
clang::clangd::PreambleFileStatusCache::lookup
llvm::Optional< llvm::vfs::Status > lookup(llvm::StringRef Path) const
Path is a path stored in preamble.
Definition: FS.cpp:40
clang::clangd::PreambleFileStatusCache::getProducingFS
IntrusiveRefCntPtr< llvm::vfs::FileSystem > getProducingFS(IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS)
Returns a VFS that collects file status.
Definition: FS.cpp:54
clang::clangd::PreambleFileStatusCache::update
void update(const llvm::vfs::FileSystem &FS, llvm::vfs::Status S)
Definition: FS.cpp:25
clang::clangd::PreambleFileStatusCache::getConsumingFS
IntrusiveRefCntPtr< llvm::vfs::FileSystem > getConsumingFS(IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS) const
Returns a VFS that uses the cache collected.
Definition: FS.cpp:94
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:29
clang::clangd::PreambleFileStatusCache::PreambleFileStatusCache
PreambleFileStatusCache(llvm::StringRef MainFilePath)
MainFilePath is the absolute path of the main source file this preamble corresponds to.
Definition: FS.cpp:18
clang::clangd::PreambleFileStatusCache
Records status information for files open()ed or stat()ed during preamble build (except for the main ...
Definition: FS.h:38
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::removeDots
Path removeDots(PathRef File)
Returns a version of File that doesn't contain dots and dot dots.
Definition: FS.cpp:114