clang-tools  16.0.0git
ThreadsafeFS.h
Go to the documentation of this file.
1 //===--- ThreadsafeFS.h ------------------------------------------*- 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_SUPPORT_THREADSAFEFS_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_THREADSAFEFS_H
11 
12 #include "Path.h"
13 #include "llvm/ADT/IntrusiveRefCntPtr.h"
14 #include "llvm/ADT/None.h"
15 #include "llvm/Support/VirtualFileSystem.h"
16 #include <memory>
17 
18 namespace clang {
19 namespace clangd {
20 
21 /// Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
22 /// As FileSystem is not threadsafe, concurrent threads must each obtain one.
23 /// Implementations may choose to depend on Context::current() e.g. to implement
24 /// snapshot semantics. clangd will not create vfs::FileSystems for use in
25 /// different contexts, so either ThreadsafeFS::view or the returned FS may
26 /// contain this logic.
27 class ThreadsafeFS {
28 public:
29  virtual ~ThreadsafeFS() = default;
30 
31  /// Obtain a vfs::FileSystem with an arbitrary initial working directory.
32  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
33  view(std::nullopt_t CWD) const {
34  return viewImpl();
35  }
36 
37  /// Obtain a vfs::FileSystem with a specified working directory.
38  /// If the working directory can't be set (e.g. doesn't exist), logs and
39  /// returns the FS anyway.
40  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> view(PathRef CWD) const;
41 
42 private:
43  /// Overridden by implementations to provide a vfs::FileSystem.
44  /// This is distinct from view(NoneType) to avoid GCC's -Woverloaded-virtual.
45  virtual llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> viewImpl() const = 0;
46 };
47 
49 private:
50  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> viewImpl() const override;
51 };
52 
53 } // namespace clangd
54 } // namespace clang
55 
56 #endif
Path.h
clang::clangd::RealThreadsafeFS
Definition: ThreadsafeFS.h:48
clang::clangd::ThreadsafeFS::~ThreadsafeFS
virtual ~ThreadsafeFS()=default
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:29
clang::clangd::ThreadsafeFS
Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
Definition: ThreadsafeFS.h:27
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::ThreadsafeFS::view
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > view(std::nullopt_t CWD) const
Obtain a vfs::FileSystem with an arbitrary initial working directory.
Definition: ThreadsafeFS.h:33