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