diff --git a/clang-tools-extra/clangd/ClangdServer.cpp b/clang-tools-extra/clangd/ClangdServer.cpp --- a/clang-tools-extra/clangd/ClangdServer.cpp +++ b/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, diff --git a/clang-tools-extra/clangd/refactor/Tweak.h b/clang-tools-extra/clangd/refactor/Tweak.h --- a/clang-tools-extra/clangd/refactor/Tweak.h +++ b/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 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. }; diff --git a/clang-tools-extra/clangd/refactor/Tweak.cpp b/clang-tools-extra/clangd/refactor/Tweak.cpp --- a/clang-tools-extra/clangd/refactor/Tweak.cpp +++ b/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 #include #include @@ -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); diff --git a/clang-tools-extra/clangd/unittests/TestTU.h b/clang-tools-extra/clangd/unittests/TestTU.h --- a/clang-tools-extra/clangd/unittests/TestTU.h +++ b/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 #include @@ -67,6 +69,7 @@ ParsedAST build() const; SymbolSlab headerSymbols() const; std::unique_ptr index() const; + llvm::IntrusiveRefCntPtr getVFS() const; }; // Look up an index symbol by qualified name, which must be unique. diff --git a/clang-tools-extra/clangd/unittests/TestTU.cpp b/clang-tools-extra/clangd/unittests/TestTU.cpp --- a/clang-tools-extra/clangd/unittests/TestTU.cpp +++ b/clang-tools-extra/clangd/unittests/TestTU.cpp @@ -19,7 +19,7 @@ namespace clang { namespace clangd { -ParsedAST TestTU::build() const { +llvm::IntrusiveRefCntPtr 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 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; diff --git a/clang-tools-extra/clangd/unittests/TweakTesting.cpp b/clang-tools-extra/clangd/unittests/TweakTesting.cpp --- a/clang-tools-extra/clangd/unittests/TweakTesting.cpp +++ b/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) {