[PATCH] D54077: [clangd] Implemented DraftFileSystem

2018-11-03 Thread Lutsenko Danil via Phabricator via cfe-commits
LutsenkoDanil created this revision.
LutsenkoDanil added reviewers: sammccall, ilya-biryukov.
Herald added subscribers: cfe-commits, kadircet, arphaman, jkorous, MaskRay, 
ioeric.

Patch changes behavior of preamble building. If headers was changed in editor, 
preamble will be built with draft versions of them.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D54077

Files:
  clangd/ClangdLSPServer.cpp
  clangd/ClangdLSPServer.h
  clangd/DraftStore.cpp
  clangd/DraftStore.h
  clangd/FS.h
  clangd/FSProvider.h
  unittests/clangd/DraftStoreTests.cpp

Index: unittests/clangd/DraftStoreTests.cpp
===
--- unittests/clangd/DraftStoreTests.cpp
+++ unittests/clangd/DraftStoreTests.cpp
@@ -53,7 +53,7 @@
 Expected Result = DS.updateDraft(Path, {Event});
 ASSERT_TRUE(!!Result);
 EXPECT_EQ(*Result, SrcAfter.code());
-EXPECT_EQ(*DS.getDraft(Path), SrcAfter.code());
+EXPECT_EQ(DS.getDraft(Path)->Content, SrcAfter.code());
   }
 }
 
@@ -83,7 +83,7 @@
 
   ASSERT_TRUE(!!Result) << toString(Result.takeError());
   EXPECT_EQ(*Result, FinalSrc.code());
-  EXPECT_EQ(*DS.getDraft(Path), FinalSrc.code());
+  EXPECT_EQ(DS.getDraft(Path)->Content, FinalSrc.code());
 }
 
 TEST(DraftStoreIncrementalUpdateTest, Simple) {
@@ -339,9 +339,9 @@
   EXPECT_EQ(toString(Result.takeError()),
 "UTF-16 offset 100 is invalid for line 0");
 
-  Optional Contents = DS.getDraft(File);
-  EXPECT_TRUE(Contents);
-  EXPECT_EQ(*Contents, OriginalContents);
+  Optional NewDraft = DS.getDraft(File);
+  EXPECT_TRUE(NewDraft);
+  EXPECT_EQ(NewDraft->Content, OriginalContents);
 }
 
 } // namespace
Index: clangd/FSProvider.h
===
--- clangd/FSProvider.h
+++ clangd/FSProvider.h
@@ -10,6 +10,7 @@
 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FSPROVIDER_H
 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FSPROVIDER_H
 
+#include "FS.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/Support/VirtualFileSystem.h"
 
@@ -38,6 +39,20 @@
   }
 };
 
+class DraftFileSystemProvider : public FileSystemProvider {
+  DraftStore &DS;
+
+public:
+  DraftFileSystemProvider(DraftStore &DS) : DS(DS) {}
+
+  llvm::IntrusiveRefCntPtr
+  getFileSystem() const override {
+static llvm::IntrusiveRefCntPtr Result(
+new DraftFileSystem(llvm::vfs::getRealFileSystem(), DS));
+return Result;
+  }
+};
+
 } // namespace clangd
 } // namespace clang
 
Index: clangd/FS.h
===
--- clangd/FS.h
+++ clangd/FS.h
@@ -10,13 +10,84 @@
 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FS_H
 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FS_H
 
+#include "DraftStore.h"
+#include "Logger.h"
 #include "clang/Basic/LLVM.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/Support/VirtualFileSystem.h"
 
 namespace clang {
 namespace clangd {
 
+class DraftFile : public llvm::vfs::File {
+  std::unique_ptr F;
+  Draft DraftInfo;
+
+public:
+  DraftFile(std::unique_ptr F, Draft D)
+  : F(std::move(F)), DraftInfo(std::move(D)) {}
+
+  llvm::ErrorOr getName() override { return F->getName(); }
+
+  llvm::ErrorOr>
+  getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
+bool IsVolatile) override {
+(void)IsVolatile;
+(void)RequiresNullTerminator;
+
+if (FileSize < 0) {
+  FileSize = DraftInfo.Content.size();
+}
+
+return llvm::MemoryBuffer::getMemBufferCopy(
+llvm::StringRef(DraftInfo.Content).substr(0, FileSize), Name);
+  }
+
+  llvm::ErrorOr status() override {
+auto RealStatus = F->status();
+
+if (!RealStatus) {
+  return RealStatus;
+}
+
+llvm::vfs::Status Result(
+RealStatus->getName(), RealStatus->getUniqueID(), DraftInfo.ModifyTime,
+RealStatus->getUser(), RealStatus->getGroup(), DraftInfo.Content.size(),
+RealStatus->getType(), RealStatus->getPermissions());
+
+Result.IsVFSMapped = RealStatus->IsVFSMapped;
+return Result;
+  }
+
+  std::error_code close() override { return F->close(); }
+};
+
+/// Wrapper around real FS. If file was changed in the DraftStrore, returns
+/// changed content, instead of real
+class DraftFileSystem : public llvm::vfs::ProxyFileSystem {
+  DraftStore &DS;
+
+public:
+  DraftFileSystem(IntrusiveRefCntPtr FS, DraftStore &DS)
+  : llvm::vfs::ProxyFileSystem(FS), DS(DS) {}
+
+  llvm::ErrorOr>
+  openFileForRead(const Twine &Path) override {
+auto Result = getUnderlyingFS().openFileForRead(Path);
+
+if (!Result) {
+  return Result;
+}
+
+if (auto D = DS.getDraft(Path.str())) {
+  return std::unique_ptr(
+  new DraftFile(std::move(*Result), std::move(*D)));
+}
+
+return Result;
+  }
+};
+
 /// Records status information for files open()ed or stat()ed during preamble
 /// build (except for the main file), so we can avoid stat()s on the underlying
 /// FS when reusing the preamble. For example, co

[PATCH] D54077: [clangd] Implemented DraftFileSystem

2018-11-05 Thread Lutsenko Danil via Phabricator via cfe-commits
LutsenkoDanil planned changes to this revision.
LutsenkoDanil added a comment.

@ilya-biryukov, For example, VSCode saves all changed files by default when you 
press Ctrl+Shift+B (if build task configured for project).

@klimek If behavior will be configurable, is it ok for you?

@sammccall Current behavior may confuse new users, since, other IDEs mostly 
(all?) shows diagnostics for edited files instead of saved one. And it's 
unexpected that headers have 2 states - visible and saved (which cannot be 
viewed in IDE at all). Looks like performance will be same like usage of 'auto 
save after delay' feature in editor, if we make debounce delay configurable, 
what do you think?

I suggest inroduce following changes to the patch:

[ ] Changes proposed by @ilya-biryukov
[ ] Configurable behavior
[ ] Debounce timeout option




Comment at: clangd/FS.h:82
+
+if (auto D = DS.getDraft(Path.str())) {
+  return std::unique_ptr(

ilya-biryukov wrote:
> This assumes the `Path` is absolute and `vfs::FileSystem` can be called with 
> non-absolute paths too.
> One way to make it work with relative paths is to create an 
> `InMemoryFileSystem` with the headers (it handles the absolute paths 
> conversions) and create an `OverlayFileSystem` on top of it and the 
> `RealFileSystem`.
> 
> This would also make more complicated things work, e.g. getting the same 
> files when traversing parent directories, etc.
> 
> Could we try this approach? WDYT?
> 
Sounds good. Thank you for review!


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D54077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D54077: [clangd] Implemented DraftFileSystem

2018-11-06 Thread Lutsenko Danil via Phabricator via cfe-commits
LutsenkoDanil added a comment.

@sammccall Thank you for detailed explanation!

In https://reviews.llvm.org/D54077#1288387, @sammccall wrote:

> Someone mentioned to me that the interaction-between-features argument wasn't 
> clear here:
>
> - we **don't** currently update diagnostics for `A.cc` when `A.h` is edited
> - we should, this seems more obvious & important than what we do with drafts
> - this interacts badly with using draft state, as this patch proposes - there 
> are too many edits


I already made a patch which introduces such behavior (not uploaded here yet), 
and looks like it works well with draft fs: diagnostics updates for depended 
files in 'real-time' on typing for opened files and no seen performance 
glitches, in multi-threaded mode at least.
I suggest continue discussion when/if dependencies tracking will be implemented 
and real performance reduce introduced by this patch can be checked with real 
code.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D54077



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits