blob: 080bd06a811be7c9a2256fa0a1569c0312b359a4 [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
Sam McCallad97ccf2020-04-28 17:49:17 +02009#include "support/FSProvider.h"
Kadir Cetinkaya2dc2e472020-06-16 12:16:24 +020010#include "Logger.h"
11#include "llvm/ADT/None.h"
12#include "llvm/ADT/Optional.h"
Ilya Biryukov1712bc72018-12-03 15:21:49 +000013#include "llvm/ADT/STLExtras.h"
14#include "llvm/ADT/SmallString.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/Support/Path.h"
17#include "llvm/Support/VirtualFileSystem.h"
18#include <memory>
19
Ilya Biryukov1712bc72018-12-03 15:21:49 +000020namespace clang {
21namespace clangd {
22
23namespace {
24/// Always opens files in the underlying filesystem as "volatile", meaning they
Sam McCallb500c492020-01-29 12:22:03 +010025/// won't be memory-mapped. Memory-mapping isn't desirable for clangd:
26/// - edits to the underlying files change contents MemoryBuffers owned by
27// SourceManager, breaking its invariants and leading to crashes
28/// - it locks files on windows, preventing edits
Ilya Biryukov1712bc72018-12-03 15:21:49 +000029class VolatileFileSystem : public llvm::vfs::ProxyFileSystem {
30public:
31 explicit VolatileFileSystem(llvm::IntrusiveRefCntPtr<FileSystem> FS)
32 : ProxyFileSystem(std::move(FS)) {}
33
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000034 llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
35 openFileForRead(const llvm::Twine &InPath) override {
36 llvm::SmallString<128> Path;
Ilya Biryukov1712bc72018-12-03 15:21:49 +000037 InPath.toVector(Path);
38
39 auto File = getUnderlyingFS().openFileForRead(Path);
40 if (!File)
41 return File;
42 // Try to guess preamble files, they can be memory-mapped even on Windows as
Sam McCallb500c492020-01-29 12:22:03 +010043 // clangd has exclusive access to those and nothing else should touch them.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000044 llvm::StringRef FileName = llvm::sys::path::filename(Path);
Ilya Biryukov1712bc72018-12-03 15:21:49 +000045 if (FileName.startswith("preamble-") && FileName.endswith(".pch"))
46 return File;
Ilya Biryukov22fa4652019-01-03 13:28:05 +000047 return std::unique_ptr<VolatileFile>(new VolatileFile(std::move(*File)));
Ilya Biryukov1712bc72018-12-03 15:21:49 +000048 }
49
50private:
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000051 class VolatileFile : public llvm::vfs::File {
Ilya Biryukov1712bc72018-12-03 15:21:49 +000052 public:
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000053 VolatileFile(std::unique_ptr<llvm::vfs::File> Wrapped)
Ilya Biryukov1712bc72018-12-03 15:21:49 +000054 : Wrapped(std::move(Wrapped)) {
55 assert(this->Wrapped);
56 }
57
58 virtual llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000059 getBuffer(const llvm::Twine &Name, int64_t FileSize,
60 bool RequiresNullTerminator, bool /*IsVolatile*/) override {
Ilya Biryukov1712bc72018-12-03 15:21:49 +000061 return Wrapped->getBuffer(Name, FileSize, RequiresNullTerminator,
62 /*IsVolatile=*/true);
63 }
64
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000065 llvm::ErrorOr<llvm::vfs::Status> status() override {
66 return Wrapped->status();
67 }
Ilya Biryukov1712bc72018-12-03 15:21:49 +000068 llvm::ErrorOr<std::string> getName() override { return Wrapped->getName(); }
69 std::error_code close() override { return Wrapped->close(); }
70
71 private:
72 std::unique_ptr<File> Wrapped;
73 };
74};
75} // namespace
76
77llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
Kadir Cetinkaya2dc2e472020-06-16 12:16:24 +020078FileSystemProvider::getFileSystem(PathRef CWD) const {
79 auto FS = getFileSystem(/*CWD=*/llvm::None);
80 if (auto EC = FS->setCurrentWorkingDirectory(CWD))
81 elog("VFS: failed to set CWD to {0}: {1}", CWD, EC.message());
82 return FS;
83}
84
85llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
86clang::clangd::RealFileSystemProvider::getFileSystem(llvm::NoneType) const {
Sam McCallb500c492020-01-29 12:22:03 +010087 // Avoid using memory-mapped files.
88 // FIXME: Try to use a similar approach in Sema instead of relying on
89 // propagation of the 'isVolatile' flag through all layers.
Sam McCall0446b402019-02-15 11:04:25 +000090 return new VolatileFileSystem(
91 llvm::vfs::createPhysicalFileSystem().release());
Ilya Biryukov1712bc72018-12-03 15:21:49 +000092}
93} // namespace clangd
94} // namespace clang