blob: 80d6be005cc41941e36fa41e5c4aa5cc8654d05f [file] [log] [blame]
Ilya Biryukov1712bc72018-12-03 15:21:49 +00001//===--- FSProvider.cpp - VFS provider for ClangdServer -------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Biryukov1712bc72018-12-03 15:21:49 +00006//
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 Biryukov1712bc72018-12-03 15:21:49 +000017namespace clang {
18namespace clangd {
19
20namespace {
21/// Always opens files in the underlying filesystem as "volatile", meaning they
Sam McCallb500c492020-01-29 12:22:03 +010022/// won't be memory-mapped. Memory-mapping isn't desirable for clangd:
23/// - edits to the underlying files change contents MemoryBuffers owned by
24// SourceManager, breaking its invariants and leading to crashes
25/// - it locks files on windows, preventing edits
Ilya Biryukov1712bc72018-12-03 15:21:49 +000026class VolatileFileSystem : public llvm::vfs::ProxyFileSystem {
27public:
28 explicit VolatileFileSystem(llvm::IntrusiveRefCntPtr<FileSystem> FS)
29 : ProxyFileSystem(std::move(FS)) {}
30
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000031 llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
32 openFileForRead(const llvm::Twine &InPath) override {
33 llvm::SmallString<128> Path;
Ilya Biryukov1712bc72018-12-03 15:21:49 +000034 InPath.toVector(Path);
35
36 auto File = getUnderlyingFS().openFileForRead(Path);
37 if (!File)
38 return File;
39 // Try to guess preamble files, they can be memory-mapped even on Windows as
Sam McCallb500c492020-01-29 12:22:03 +010040 // clangd has exclusive access to those and nothing else should touch them.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000041 llvm::StringRef FileName = llvm::sys::path::filename(Path);
Ilya Biryukov1712bc72018-12-03 15:21:49 +000042 if (FileName.startswith("preamble-") && FileName.endswith(".pch"))
43 return File;
Ilya Biryukov22fa4652019-01-03 13:28:05 +000044 return std::unique_ptr<VolatileFile>(new VolatileFile(std::move(*File)));
Ilya Biryukov1712bc72018-12-03 15:21:49 +000045 }
46
47private:
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000048 class VolatileFile : public llvm::vfs::File {
Ilya Biryukov1712bc72018-12-03 15:21:49 +000049 public:
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000050 VolatileFile(std::unique_ptr<llvm::vfs::File> Wrapped)
Ilya Biryukov1712bc72018-12-03 15:21:49 +000051 : Wrapped(std::move(Wrapped)) {
52 assert(this->Wrapped);
53 }
54
55 virtual llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000056 getBuffer(const llvm::Twine &Name, int64_t FileSize,
57 bool RequiresNullTerminator, bool /*IsVolatile*/) override {
Ilya Biryukov1712bc72018-12-03 15:21:49 +000058 return Wrapped->getBuffer(Name, FileSize, RequiresNullTerminator,
59 /*IsVolatile=*/true);
60 }
61
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000062 llvm::ErrorOr<llvm::vfs::Status> status() override {
63 return Wrapped->status();
64 }
Ilya Biryukov1712bc72018-12-03 15:21:49 +000065 llvm::ErrorOr<std::string> getName() override { return Wrapped->getName(); }
66 std::error_code close() override { return Wrapped->close(); }
67
68 private:
69 std::unique_ptr<File> Wrapped;
70 };
71};
72} // namespace
73
74llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
75clang::clangd::RealFileSystemProvider::getFileSystem() const {
Sam McCallb500c492020-01-29 12:22:03 +010076 // Avoid using memory-mapped files.
77 // FIXME: Try to use a similar approach in Sema instead of relying on
78 // propagation of the 'isVolatile' flag through all layers.
Sam McCall0446b402019-02-15 11:04:25 +000079 return new VolatileFileSystem(
80 llvm::vfs::createPhysicalFileSystem().release());
Ilya Biryukov1712bc72018-12-03 15:21:49 +000081}
82} // namespace clangd
83} // namespace clang