1 //===--- ThreadsafeFS.cpp -------------------------------------------------===//
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 #include "support/ThreadsafeFS.h"
10 #include "Logger.h"
11 #include "llvm/ADT/None.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/Path.h"
15 #include "llvm/Support/VirtualFileSystem.h"
16 #include <memory>
17
18 namespace clang {
19 namespace clangd {
20
21 namespace {
22 /// Always opens files in the underlying filesystem as "volatile", meaning they
23 /// won't be memory-mapped. Memory-mapping isn't desirable for clangd:
24 /// - edits to the underlying files change contents MemoryBuffers owned by
25 // SourceManager, breaking its invariants and leading to crashes
26 /// - it locks files on windows, preventing edits
27 class VolatileFileSystem : public llvm::vfs::ProxyFileSystem {
28 public:
VolatileFileSystem(llvm::IntrusiveRefCntPtr<FileSystem> FS)29 explicit VolatileFileSystem(llvm::IntrusiveRefCntPtr<FileSystem> FS)
30 : ProxyFileSystem(std::move(FS)) {}
31
32 llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
openFileForRead(const llvm::Twine & InPath)33 openFileForRead(const llvm::Twine &InPath) override {
34 llvm::SmallString<128> Path;
35 InPath.toVector(Path);
36
37 auto File = getUnderlyingFS().openFileForRead(Path);
38 if (!File)
39 return File;
40 // Try to guess preamble files, they can be memory-mapped even on Windows as
41 // clangd has exclusive access to those and nothing else should touch them.
42 llvm::StringRef FileName = llvm::sys::path::filename(Path);
43 if (FileName.startswith("preamble-") && FileName.endswith(".pch"))
44 return File;
45 return std::unique_ptr<VolatileFile>(new VolatileFile(std::move(*File)));
46 }
47
48 private:
49 class VolatileFile : public llvm::vfs::File {
50 public:
VolatileFile(std::unique_ptr<llvm::vfs::File> Wrapped)51 VolatileFile(std::unique_ptr<llvm::vfs::File> Wrapped)
52 : Wrapped(std::move(Wrapped)) {
53 assert(this->Wrapped);
54 }
55
56 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
getBuffer(const llvm::Twine & Name,int64_t FileSize,bool RequiresNullTerminator,bool)57 getBuffer(const llvm::Twine &Name, int64_t FileSize,
58 bool RequiresNullTerminator, bool /*IsVolatile*/) override {
59 return Wrapped->getBuffer(Name, FileSize, RequiresNullTerminator,
60 /*IsVolatile=*/true);
61 }
62
status()63 llvm::ErrorOr<llvm::vfs::Status> status() override {
64 return Wrapped->status();
65 }
getName()66 llvm::ErrorOr<std::string> getName() override { return Wrapped->getName(); }
close()67 std::error_code close() override { return Wrapped->close(); }
68
69 private:
70 std::unique_ptr<File> Wrapped;
71 };
72 };
73 } // namespace
74
75 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
view(PathRef CWD) const76 ThreadsafeFS::view(PathRef CWD) const {
77 auto FS = view(llvm::None);
78 if (auto EC = FS->setCurrentWorkingDirectory(CWD))
79 elog("VFS: failed to set CWD to {0}: {1}", CWD, EC.message());
80 return FS;
81 }
82
83 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
viewImpl() const84 RealThreadsafeFS::viewImpl() const {
85 // Avoid using memory-mapped files.
86 // FIXME: Try to use a similar approach in Sema instead of relying on
87 // propagation of the 'isVolatile' flag through all layers.
88 return new VolatileFileSystem(llvm::vfs::createPhysicalFileSystem());
89 }
90 } // namespace clangd
91 } // namespace clang
92