xref: /llvm-project/clang-tools-extra/clangd/support/ThreadsafeFS.h (revision f7dffc28b3f82e25a0e283d2b11ffb9c6a129340)
10628705eSKadir Cetinkaya //===--- ThreadsafeFS.h ------------------------------------------*- C++-*-===//
20628705eSKadir Cetinkaya //
30628705eSKadir Cetinkaya // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40628705eSKadir Cetinkaya // See https://llvm.org/LICENSE.txt for license information.
50628705eSKadir Cetinkaya // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60628705eSKadir Cetinkaya //
70628705eSKadir Cetinkaya //===----------------------------------------------------------------------===//
80628705eSKadir Cetinkaya 
90628705eSKadir Cetinkaya #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_THREADSAFEFS_H
100628705eSKadir Cetinkaya #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_SUPPORT_THREADSAFEFS_H
110628705eSKadir Cetinkaya 
120628705eSKadir Cetinkaya #include "Path.h"
130628705eSKadir Cetinkaya #include "llvm/ADT/IntrusiveRefCntPtr.h"
140628705eSKadir Cetinkaya #include "llvm/Support/VirtualFileSystem.h"
150628705eSKadir Cetinkaya #include <memory>
160628705eSKadir Cetinkaya 
170628705eSKadir Cetinkaya namespace clang {
180628705eSKadir Cetinkaya namespace clangd {
190628705eSKadir Cetinkaya 
200628705eSKadir Cetinkaya /// Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
210628705eSKadir Cetinkaya /// As FileSystem is not threadsafe, concurrent threads must each obtain one.
220628705eSKadir Cetinkaya /// Implementations may choose to depend on Context::current() e.g. to implement
230628705eSKadir Cetinkaya /// snapshot semantics. clangd will not create vfs::FileSystems for use in
240628705eSKadir Cetinkaya /// different contexts, so either ThreadsafeFS::view or the returned FS may
250628705eSKadir Cetinkaya /// contain this logic.
260628705eSKadir Cetinkaya class ThreadsafeFS {
270628705eSKadir Cetinkaya public:
280628705eSKadir Cetinkaya   virtual ~ThreadsafeFS() = default;
290628705eSKadir Cetinkaya 
300628705eSKadir Cetinkaya   /// Obtain a vfs::FileSystem with an arbitrary initial working directory.
3172568984SSam McCall   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
view(std::nullopt_t CWD)32*34bcadc3SKazu Hirata   view(std::nullopt_t CWD) const {
3372568984SSam McCall     return viewImpl();
3472568984SSam McCall   }
350628705eSKadir Cetinkaya 
360628705eSKadir Cetinkaya   /// Obtain a vfs::FileSystem with a specified working directory.
370628705eSKadir Cetinkaya   /// If the working directory can't be set (e.g. doesn't exist), logs and
380628705eSKadir Cetinkaya   /// returns the FS anyway.
3972568984SSam McCall   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> view(PathRef CWD) const;
4072568984SSam McCall 
4172568984SSam McCall private:
4272568984SSam McCall   /// Overridden by implementations to provide a vfs::FileSystem.
4372568984SSam McCall   /// This is distinct from view(NoneType) to avoid GCC's -Woverloaded-virtual.
4472568984SSam McCall   virtual llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> viewImpl() const = 0;
450628705eSKadir Cetinkaya };
460628705eSKadir Cetinkaya 
470628705eSKadir Cetinkaya class RealThreadsafeFS : public ThreadsafeFS {
4872568984SSam McCall private:
4972568984SSam McCall   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> viewImpl() const override;
500628705eSKadir Cetinkaya };
510628705eSKadir Cetinkaya 
520628705eSKadir Cetinkaya } // namespace clangd
530628705eSKadir Cetinkaya } // namespace clang
540628705eSKadir Cetinkaya 
550628705eSKadir Cetinkaya #endif
56