Ilya Biryukov | 1712bc7 | 2018-12-03 15:21:49 +0000 | [diff] [blame] | 1 | //===--- FSProvider.cpp - VFS provider for ClangdServer -------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Ilya Biryukov | 1712bc7 | 2018-12-03 15:21:49 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "FSProvider.h" |
| 10 | #include "llvm/ADT/STLExtras.h" |
| 11 | #include "llvm/ADT/SmallString.h" |
| 12 | #include "llvm/ADT/StringRef.h" |
| 13 | #include "llvm/Support/Path.h" |
| 14 | #include "llvm/Support/VirtualFileSystem.h" |
| 15 | #include <memory> |
| 16 | |
Ilya Biryukov | 1712bc7 | 2018-12-03 15:21:49 +0000 | [diff] [blame] | 17 | namespace clang { |
| 18 | namespace clangd { |
| 19 | |
| 20 | namespace { |
| 21 | /// Always opens files in the underlying filesystem as "volatile", meaning they |
| 22 | /// won't be memory-mapped. This avoid locking the files on Windows. |
| 23 | class VolatileFileSystem : public llvm::vfs::ProxyFileSystem { |
| 24 | public: |
| 25 | explicit VolatileFileSystem(llvm::IntrusiveRefCntPtr<FileSystem> FS) |
| 26 | : ProxyFileSystem(std::move(FS)) {} |
| 27 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 28 | llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> |
| 29 | openFileForRead(const llvm::Twine &InPath) override { |
| 30 | llvm::SmallString<128> Path; |
Ilya Biryukov | 1712bc7 | 2018-12-03 15:21:49 +0000 | [diff] [blame] | 31 | InPath.toVector(Path); |
| 32 | |
| 33 | auto File = getUnderlyingFS().openFileForRead(Path); |
| 34 | if (!File) |
| 35 | return File; |
| 36 | // Try to guess preamble files, they can be memory-mapped even on Windows as |
| 37 | // clangd has exclusive access to those. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 38 | llvm::StringRef FileName = llvm::sys::path::filename(Path); |
Ilya Biryukov | 1712bc7 | 2018-12-03 15:21:49 +0000 | [diff] [blame] | 39 | if (FileName.startswith("preamble-") && FileName.endswith(".pch")) |
| 40 | return File; |
Ilya Biryukov | 22fa465 | 2019-01-03 13:28:05 +0000 | [diff] [blame] | 41 | return std::unique_ptr<VolatileFile>(new VolatileFile(std::move(*File))); |
Ilya Biryukov | 1712bc7 | 2018-12-03 15:21:49 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | private: |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 45 | class VolatileFile : public llvm::vfs::File { |
Ilya Biryukov | 1712bc7 | 2018-12-03 15:21:49 +0000 | [diff] [blame] | 46 | public: |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 47 | VolatileFile(std::unique_ptr<llvm::vfs::File> Wrapped) |
Ilya Biryukov | 1712bc7 | 2018-12-03 15:21:49 +0000 | [diff] [blame] | 48 | : Wrapped(std::move(Wrapped)) { |
| 49 | assert(this->Wrapped); |
| 50 | } |
| 51 | |
| 52 | virtual llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 53 | getBuffer(const llvm::Twine &Name, int64_t FileSize, |
| 54 | bool RequiresNullTerminator, bool /*IsVolatile*/) override { |
Ilya Biryukov | 1712bc7 | 2018-12-03 15:21:49 +0000 | [diff] [blame] | 55 | return Wrapped->getBuffer(Name, FileSize, RequiresNullTerminator, |
| 56 | /*IsVolatile=*/true); |
| 57 | } |
| 58 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 59 | llvm::ErrorOr<llvm::vfs::Status> status() override { |
| 60 | return Wrapped->status(); |
| 61 | } |
Ilya Biryukov | 1712bc7 | 2018-12-03 15:21:49 +0000 | [diff] [blame] | 62 | llvm::ErrorOr<std::string> getName() override { return Wrapped->getName(); } |
| 63 | std::error_code close() override { return Wrapped->close(); } |
| 64 | |
| 65 | private: |
| 66 | std::unique_ptr<File> Wrapped; |
| 67 | }; |
| 68 | }; |
| 69 | } // namespace |
| 70 | |
| 71 | llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> |
| 72 | clang::clangd::RealFileSystemProvider::getFileSystem() const { |
| 73 | // Avoid using memory-mapped files on Windows, they cause file locking issues. |
| 74 | // FIXME: Try to use a similar approach in Sema instead of relying on |
| 75 | // propagation of the 'isVolatile' flag through all layers. |
| 76 | #ifdef _WIN32 |
Sam McCall | 0446b40 | 2019-02-15 11:04:25 +0000 | [diff] [blame^] | 77 | return new VolatileFileSystem( |
| 78 | llvm::vfs::createPhysicalFileSystem().release()); |
Ilya Biryukov | 1712bc7 | 2018-12-03 15:21:49 +0000 | [diff] [blame] | 79 | #else |
Sam McCall | 0446b40 | 2019-02-15 11:04:25 +0000 | [diff] [blame^] | 80 | return llvm::vfs::createPhysicalFileSystem().release(); |
Ilya Biryukov | 1712bc7 | 2018-12-03 15:21:49 +0000 | [diff] [blame] | 81 | #endif |
| 82 | } |
| 83 | } // namespace clangd |
| 84 | } // namespace clang |