blob: 99e7596e83a3a2f0e11949216d10b9b69ab25dd6 [file] [log] [blame]
Ilya Biryukov1712bc72018-12-03 15:21:49 +00001//===--- FSProvider.cpp - VFS provider for ClangdServer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "FSProvider.h"
11#include "llvm/ADT/STLExtras.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
18using namespace llvm;
19
20namespace clang {
21namespace clangd {
22
23namespace {
24/// Always opens files in the underlying filesystem as "volatile", meaning they
25/// won't be memory-mapped. This avoid locking the files on Windows.
26class VolatileFileSystem : public llvm::vfs::ProxyFileSystem {
27public:
28 explicit VolatileFileSystem(llvm::IntrusiveRefCntPtr<FileSystem> FS)
29 : ProxyFileSystem(std::move(FS)) {}
30
31 llvm::ErrorOr<std::unique_ptr<vfs::File>>
32 openFileForRead(const Twine &InPath) override {
33 SmallString<128> Path;
34 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
40 // clangd has exclusive access to those.
41 StringRef FileName = llvm::sys::path::filename(Path);
42 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:
48 class VolatileFile : public vfs::File {
49 public:
50 VolatileFile(std::unique_ptr<vfs::File> Wrapped)
51 : Wrapped(std::move(Wrapped)) {
52 assert(this->Wrapped);
53 }
54
55 virtual llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
56 getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
57 bool /*IsVolatile*/) override {
58 return Wrapped->getBuffer(Name, FileSize, RequiresNullTerminator,
59 /*IsVolatile=*/true);
60 }
61
62 llvm::ErrorOr<vfs::Status> status() override { return Wrapped->status(); }
63 llvm::ErrorOr<std::string> getName() override { return Wrapped->getName(); }
64 std::error_code close() override { return Wrapped->close(); }
65
66 private:
67 std::unique_ptr<File> Wrapped;
68 };
69};
70} // namespace
71
72llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
73clang::clangd::RealFileSystemProvider::getFileSystem() const {
74// Avoid using memory-mapped files on Windows, they cause file locking issues.
75// FIXME: Try to use a similar approach in Sema instead of relying on
76// propagation of the 'isVolatile' flag through all layers.
77#ifdef _WIN32
Zachary Turneraa2711f2018-12-03 19:59:00 +000078 return new VolatileFileSystem(vfs::getRealFileSystem());
Ilya Biryukov1712bc72018-12-03 15:21:49 +000079#else
80 return vfs::getRealFileSystem();
81#endif
82}
83} // namespace clangd
84} // namespace clang