[clangd] Reland Store Index in Tweak::Selection

Summary:
Incoming define out-of-line tweak requires access to index.

This patch simply propogates the index in ClangdServer to Tweak::Selection while
passing the AST. Also updates TweakTest to accommodate this change.

Reviewers: ilya-biryukov

Subscribers: MaskRay, jkorous, arphaman, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D69165
This commit is contained in:
Kadir Cetinkaya 2019-10-18 14:57:11 +02:00
parent 3d65def1fd
commit f4d7caf6df
No known key found for this signature in database
GPG Key ID: E39E36B8D2057ED6
5 changed files with 20 additions and 9 deletions

View File

@ -367,7 +367,7 @@ tweakSelection(const Range &Sel, const InputsAndAST &AST) {
auto End = positionToOffset(AST.Inputs.Contents, Sel.end); auto End = positionToOffset(AST.Inputs.Contents, Sel.end);
if (!End) if (!End)
return End.takeError(); return End.takeError();
return Tweak::Selection(AST.AST, *Begin, *End); return Tweak::Selection(AST.Inputs.Index, AST.AST, *Begin, *End);
} }
void ClangdServer::enumerateTweaks(PathRef File, Range Sel, void ClangdServer::enumerateTweaks(PathRef File, Range Sel,

View File

@ -9,6 +9,7 @@
#include "Logger.h" #include "Logger.h"
#include "Path.h" #include "Path.h"
#include "SourceCode.h" #include "SourceCode.h"
#include "index/Index.h"
#include "llvm/ADT/None.h" #include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.h" #include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/STLExtras.h"
@ -44,9 +45,10 @@ void validateRegistry() {
} }
} // namespace } // namespace
Tweak::Selection::Selection(ParsedAST &AST, unsigned RangeBegin, Tweak::Selection::Selection(const SymbolIndex *Index, ParsedAST &AST,
unsigned RangeEnd) unsigned RangeBegin, unsigned RangeEnd)
: AST(AST), SelectionBegin(RangeBegin), SelectionEnd(RangeEnd), : Index(Index), AST(AST), SelectionBegin(RangeBegin),
SelectionEnd(RangeEnd),
ASTSelection(AST.getASTContext(), AST.getTokens(), RangeBegin, RangeEnd) { ASTSelection(AST.getASTContext(), AST.getTokens(), RangeBegin, RangeEnd) {
auto &SM = AST.getSourceManager(); auto &SM = AST.getSourceManager();
Code = SM.getBufferData(SM.getMainFileID()); Code = SM.getBufferData(SM.getMainFileID());

View File

@ -24,6 +24,7 @@
#include "Protocol.h" #include "Protocol.h"
#include "Selection.h" #include "Selection.h"
#include "SourceCode.h" #include "SourceCode.h"
#include "index/Index.h"
#include "clang/Tooling/Core/Replacement.h" #include "clang/Tooling/Core/Replacement.h"
#include "llvm/ADT/Optional.h" #include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringMap.h"
@ -46,9 +47,12 @@ class Tweak {
public: public:
/// Input to prepare and apply tweaks. /// Input to prepare and apply tweaks.
struct Selection { struct Selection {
Selection(ParsedAST &AST, unsigned RangeBegin, unsigned RangeEnd); Selection(const SymbolIndex *Index, ParsedAST &AST, unsigned RangeBegin,
unsigned RangeEnd);
/// The text of the active document. /// The text of the active document.
llvm::StringRef Code; llvm::StringRef Code;
/// The Index for handling codebase related queries.
const SymbolIndex *Index = nullptr;
/// Parsed AST of the active file. /// Parsed AST of the active file.
ParsedAST * ParsedAST *
/// A location of the cursor in the editor. /// A location of the cursor in the editor.

View File

@ -63,7 +63,7 @@ std::pair<unsigned, unsigned> rangeOrPoint(const Annotations &A) {
cantFail(positionToOffset(A.code(), SelectionRng.end))}; cantFail(positionToOffset(A.code(), SelectionRng.end))};
} }
MATCHER_P5(TweakIsAvailable, TweakID, Ctx, Header, ExtraArgs, ExtraFiles, MATCHER_P6(TweakIsAvailable, TweakID, Ctx, Header, ExtraArgs, ExtraFiles, Index,
(TweakID + (negation ? " is unavailable" : " is available")).str()) { (TweakID + (negation ? " is unavailable" : " is available")).str()) {
std::string WrappedCode = wrap(Ctx, arg); std::string WrappedCode = wrap(Ctx, arg);
Annotations Input(WrappedCode); Annotations Input(WrappedCode);
@ -74,7 +74,7 @@ MATCHER_P5(TweakIsAvailable, TweakID, Ctx, Header, ExtraArgs, ExtraFiles,
TU.ExtraArgs = ExtraArgs; TU.ExtraArgs = ExtraArgs;
TU.AdditionalFiles = std::move(ExtraFiles); TU.AdditionalFiles = std::move(ExtraFiles);
ParsedAST AST = TU.build(); ParsedAST AST = TU.build();
Tweak::Selection S(AST, Selection.first, Selection.second); Tweak::Selection S(Index, AST, Selection.first, Selection.second);
auto PrepareResult = prepareTweak(TweakID, S); auto PrepareResult = prepareTweak(TweakID, S);
if (PrepareResult) if (PrepareResult)
return true; return true;
@ -96,7 +96,7 @@ std::string TweakTest::apply(llvm::StringRef MarkedCode,
TU.Code = Input.code(); TU.Code = Input.code();
TU.ExtraArgs = ExtraArgs; TU.ExtraArgs = ExtraArgs;
ParsedAST AST = TU.build(); ParsedAST AST = TU.build();
Tweak::Selection S(AST, Selection.first, Selection.second); Tweak::Selection S(Index.get(), AST, Selection.first, Selection.second);
auto T = prepareTweak(TweakID, S); auto T = prepareTweak(TweakID, S);
if (!T) { if (!T) {
@ -132,7 +132,7 @@ std::string TweakTest::apply(llvm::StringRef MarkedCode,
::testing::Matcher<llvm::StringRef> TweakTest::isAvailable() const { ::testing::Matcher<llvm::StringRef> TweakTest::isAvailable() const {
return TweakIsAvailable(llvm::StringRef(TweakID), Context, Header, ExtraArgs, return TweakIsAvailable(llvm::StringRef(TweakID), Context, Header, ExtraArgs,
ExtraFiles); ExtraFiles, Index.get());
} }
std::vector<std::string> TweakTest::expandCases(llvm::StringRef MarkedCode) { std::vector<std::string> TweakTest::expandCases(llvm::StringRef MarkedCode) {

View File

@ -10,9 +10,11 @@
#define LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_TWEAKTESTING_H #define LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_TWEAKTESTING_H
#include "TestTU.h" #include "TestTU.h"
#include "index/Index.h"
#include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringMap.h"
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <memory>
#include <string> #include <string>
namespace clang { namespace clang {
@ -66,6 +68,9 @@ protected:
// Context in which snippets of code should be placed to run tweaks. // Context in which snippets of code should be placed to run tweaks.
CodeContext Context = File; CodeContext Context = File;
// Index to be passed into Tweak::Selection.
std::unique_ptr<const SymbolIndex> Index = nullptr;
// Apply the current tweak to the range (or point) in MarkedCode. // Apply the current tweak to the range (or point) in MarkedCode.
// MarkedCode will be wrapped according to the Context. // MarkedCode will be wrapped according to the Context.
// - if the tweak produces edits, returns the edited code (without markings) // - if the tweak produces edits, returns the edited code (without markings)