kadircet created this revision.
kadircet added a reviewer: ilya-biryukov.
Herald added subscribers: cfe-commits, usaxena95, arphaman, jkorous, MaskRay.
Herald added a project: clang.
kadircet added a parent revision: D69165: [clangd] Store Index in 
Tweak::Selection.

Incoming define out-of-line code action needs to access files that
weren't touched during compilation of the translation unit. This patch passes FS
into Tweak::Selection to make files accessible.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D69177

Files:
  clang-tools-extra/clangd/ClangdServer.cpp
  clang-tools-extra/clangd/refactor/Tweak.cpp
  clang-tools-extra/clangd/refactor/Tweak.h
  clang-tools-extra/clangd/unittests/TestTU.cpp
  clang-tools-extra/clangd/unittests/TestTU.h
  clang-tools-extra/clangd/unittests/TweakTesting.cpp

Index: clang-tools-extra/clangd/unittests/TweakTesting.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/TweakTesting.cpp
+++ clang-tools-extra/clangd/unittests/TweakTesting.cpp
@@ -72,7 +72,8 @@
   TU.Code = Input.code();
   TU.AdditionalFiles = std::move(ExtraFiles);
   ParsedAST AST = TU.build();
-  Tweak::Selection S(AST, Selection.first, Selection.second, Index);
+  auto FS = TU.getVFS();
+  Tweak::Selection S(AST, Selection.first, Selection.second, Index, FS.get());
   auto PrepareResult = prepareTweak(TweakID, S);
   if (PrepareResult)
     return true;
@@ -94,7 +95,9 @@
   TU.Code = Input.code();
   TU.ExtraArgs = ExtraArgs;
   ParsedAST AST = TU.build();
-  Tweak::Selection S(AST, Selection.first, Selection.second, Index);
+
+  auto FS = TU.getVFS();
+  Tweak::Selection S(AST, Selection.first, Selection.second, Index, FS.get());
 
   auto T = prepareTweak(TweakID, S);
   if (!T) {
Index: clang-tools-extra/clangd/unittests/TestTU.h
===================================================================
--- clang-tools-extra/clangd/unittests/TestTU.h
+++ clang-tools-extra/clangd/unittests/TestTU.h
@@ -20,7 +20,9 @@
 #include "ParsedAST.h"
 #include "Path.h"
 #include "index/Index.h"
+#include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/ADT/StringMap.h"
+#include "llvm/Support/VirtualFileSystem.h"
 #include "gtest/gtest.h"
 #include <string>
 #include <utility>
@@ -67,6 +69,7 @@
   ParsedAST build() const;
   SymbolSlab headerSymbols() const;
   std::unique_ptr<SymbolIndex> index() const;
+  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> getVFS() const;
 };
 
 // Look up an index symbol by qualified name, which must be unique.
Index: clang-tools-extra/clangd/unittests/TestTU.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/TestTU.cpp
+++ clang-tools-extra/clangd/unittests/TestTU.cpp
@@ -19,7 +19,7 @@
 namespace clang {
 namespace clangd {
 
-ParsedAST TestTU::build() const {
+llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> TestTU::getVFS() const {
   std::string FullFilename = testPath(Filename),
               FullHeaderName = testPath(HeaderFilename),
               ImportThunk = testPath("import_thunk.h");
@@ -32,6 +32,13 @@
   Files[FullFilename] = Code;
   Files[FullHeaderName] = HeaderCode;
   Files[ImportThunk] = ThunkContents;
+  return buildTestFS(Files);
+}
+
+ParsedAST TestTU::build() const {
+  std::string FullFilename = testPath(Filename),
+              FullHeaderName = testPath(HeaderFilename),
+              ImportThunk = testPath("import_thunk.h");
 
   std::vector<const char *> Cmd = {"clang"};
   // FIXME: this shouldn't need to be conditional, but it breaks a
@@ -54,7 +61,7 @@
   Inputs.CompileCommand.CommandLine = {Cmd.begin(), Cmd.end()};
   Inputs.CompileCommand.Directory = testRoot();
   Inputs.Contents = Code;
-  Inputs.FS = buildTestFS(Files);
+  Inputs.FS = getVFS();
   Inputs.Opts = ParseOptions();
   Inputs.Opts.ClangTidyOpts.Checks = ClangTidyChecks;
   Inputs.Opts.ClangTidyOpts.WarningsAsErrors = ClangTidyWarningsAsErrors;
Index: clang-tools-extra/clangd/refactor/Tweak.h
===================================================================
--- clang-tools-extra/clangd/refactor/Tweak.h
+++ clang-tools-extra/clangd/refactor/Tweak.h
@@ -30,6 +30,7 @@
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/VirtualFileSystem.h"
 #include <string>
 
 namespace clang {
@@ -48,7 +49,7 @@
   /// Input to prepare and apply tweaks.
   struct Selection {
     Selection(ParsedAST &AST, unsigned RangeBegin, unsigned RangeEnd,
-              const SymbolIndex *Index);
+              const SymbolIndex *Index, llvm::vfs::FileSystem *FS);
     /// The text of the active document.
     llvm::StringRef Code;
     /// Parsed AST of the active file.
@@ -64,6 +65,9 @@
     SelectionTree ASTSelection;
     /// The Index being used by ClangdServer.
     const SymbolIndex *Index = nullptr;
+    /// The FS to be used by actions that will need access to files that are not
+    /// touched while building AST.
+    llvm::vfs::FileSystem *FS = nullptr;
     // FIXME: provide a way to get sources and ASTs for other files.
   };
 
Index: clang-tools-extra/clangd/refactor/Tweak.cpp
===================================================================
--- clang-tools-extra/clangd/refactor/Tweak.cpp
+++ clang-tools-extra/clangd/refactor/Tweak.cpp
@@ -17,6 +17,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/Registry.h"
+#include "llvm/Support/VirtualFileSystem.h"
 #include <functional>
 #include <memory>
 #include <utility>
@@ -46,10 +47,11 @@
 } // namespace
 
 Tweak::Selection::Selection(ParsedAST &AST, unsigned RangeBegin,
-                            unsigned RangeEnd, const SymbolIndex *Index)
+                            unsigned RangeEnd, const SymbolIndex *Index,
+                            llvm::vfs::FileSystem *FS)
     : AST(AST), SelectionBegin(RangeBegin), SelectionEnd(RangeEnd),
       ASTSelection(AST.getASTContext(), AST.getTokens(), RangeBegin, RangeEnd),
-      Index(Index) {
+      Index(Index), FS(FS) {
   auto &SM = AST.getSourceManager();
   Code = SM.getBufferData(SM.getMainFileID());
   Cursor = SM.getComposedLoc(SM.getMainFileID(), RangeBegin);
Index: clang-tools-extra/clangd/ClangdServer.cpp
===================================================================
--- clang-tools-extra/clangd/ClangdServer.cpp
+++ clang-tools-extra/clangd/ClangdServer.cpp
@@ -367,7 +367,8 @@
   auto End = positionToOffset(AST.Inputs.Contents, Sel.end);
   if (!End)
     return End.takeError();
-  return Tweak::Selection(AST.AST, *Begin, *End, AST.Inputs.Index);
+  return Tweak::Selection(AST.AST, *Begin, *End, AST.Inputs.Index,
+                          AST.Inputs.FS.get());
 }
 
 void ClangdServer::enumerateTweaks(PathRef File, Range Sel,
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to