Skip to content

Commit da5258c

Browse files
committed
[Clangd] Changed ExtractVariable to only work on non empty selections
Summary: - For now, we don't trigger in any case if it's an empty selection - Fixed unittests Reviewers: kadircet, sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D64912 llvm-svn: 366451
1 parent fc3aa2a commit da5258c

File tree

4 files changed

+45
-35
lines changed

4 files changed

+45
-35
lines changed

clang-tools-extra/clangd/refactor/Tweak.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ void validateRegistry() {
4040

4141
Tweak::Selection::Selection(ParsedAST &AST, unsigned RangeBegin,
4242
unsigned RangeEnd)
43-
: AST(AST), ASTSelection(AST.getASTContext(), RangeBegin, RangeEnd) {
43+
: AST(AST), SelectionBegin(RangeBegin), SelectionEnd(RangeEnd),
44+
ASTSelection(AST.getASTContext(), RangeBegin, RangeEnd) {
4445
auto &SM = AST.getSourceManager();
4546
Code = SM.getBufferData(SM.getMainFileID());
4647
Cursor = SM.getComposedLoc(SM.getMainFileID(), RangeBegin);

clang-tools-extra/clangd/refactor/Tweak.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ class Tweak {
4646
/// Parsed AST of the active file.
4747
ParsedAST *
4848
/// A location of the cursor in the editor.
49+
// FIXME: Cursor is redundant and should be removed
4950
SourceLocation Cursor;
51+
/// The begin offset of the selection
52+
unsigned SelectionBegin;
53+
/// The end offset of the selection
54+
unsigned SelectionEnd;
5055
/// The AST nodes that were selected.
5156
SelectionTree ASTSelection;
5257
// FIXME: provide a way to get sources and ASTs for other files.

clang-tools-extra/clangd/refactor/tweaks/ExtractVariable.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ bool ExtractVariable::prepare(const Selection &Inputs) {
219219
const ASTContext &Ctx = Inputs.AST.getASTContext();
220220
const SourceManager &SM = Inputs.AST.getSourceManager();
221221
const SelectionTree::Node *N = Inputs.ASTSelection.commonAncestor();
222-
if (!N)
222+
// we don't trigger on empty selections for now
223+
if (!N || Inputs.SelectionBegin == Inputs.SelectionEnd)
223224
return false;
224225
Target = llvm::make_unique<ExtractionContext>(N, SM, Ctx);
225226
return Target->isExtractable();

clang-tools-extra/clangd/unittests/TweakTests.cpp

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -296,35 +296,36 @@ TEST(TweakTest, ExtractVariable) {
296296
checkAvailable(ID, R"cpp(
297297
int xyz() {
298298
// return statement
299-
return ^1;
299+
return [[1]];
300300
}
301301
void f() {
302-
int a = 5 + [[4 ^* ^xyz^()]];
302+
int a = [[5 +]] [[4 * [[[[xyz]]()]]]];
303303
// multivariable initialization
304304
if(1)
305-
int x = ^1, y = ^a + 1, a = ^1, z = a + 1;
305+
int x = [[1]], y = [[a]] + 1, a = [[1]], z = a + 1;
306306
// if without else
307-
if(^1) {}
307+
if([[1]])
308+
a = [[1]];
308309
// if with else
309-
if(a < ^3)
310-
if(a == ^4)
311-
a = ^5;
310+
if(a < [[3]])
311+
if(a == [[4]])
312+
a = [[5]];
312313
else
313-
a = ^6;
314-
else if (a < ^4)
315-
a = ^4;
314+
a = [[5]];
315+
else if (a < [[4]])
316+
a = [[4]];
316317
else
317-
a = ^5;
318+
a = [[5]];
318319
// for loop
319-
for(a = ^1; a > ^3^+^4; a++)
320-
a = ^2;
320+
for(a = [[1]]; a > [[[[3]] + [[4]]]]; a++)
321+
a = [[2]];
321322
// while
322-
while(a < ^1)
323-
^a++;
323+
while(a < [[1]])
324+
[[a]]++;
324325
// do while
325326
do
326-
a = ^1;
327-
while(a < ^3);
327+
a = [[1]];
328+
while(a < [[3]]);
328329
}
329330
)cpp");
330331
// Should not crash.
@@ -336,29 +337,31 @@ TEST(TweakTest, ExtractVariable) {
336337
};
337338
)cpp");
338339
checkNotAvailable(ID, R"cpp(
339-
int xyz(int a = ^1) {
340+
int xyz(int a = [[1]]) {
340341
return 1;
341342
class T {
342-
T(int a = ^1) {};
343-
int xyz = ^1;
343+
T(int a = [[1]]) {};
344+
int xyz = [[1]];
344345
};
345346
}
346347
// function default argument
347-
void f(int b = ^1) {
348+
void f(int b = [[1]]) {
349+
// empty selection
350+
int a = ^1 ^+ ^2;
348351
// void expressions
349352
auto i = new int, j = new int;
350-
de^lete i^, del^ete j;
353+
[[[[delete i]], delete j]];
351354
// if
352355
if(1)
353-
int x = 1, y = a + 1, a = 1, z = ^a + 1;
356+
int x = 1, y = a + 1, a = 1, z = [[a + 1]];
354357
if(int a = 1)
355-
if(^a == 4)
356-
a = ^a ^+ 1;
358+
if([[a]] == 4)
359+
a = [[[[a]] +]] 1;
357360
// for loop
358-
for(int a = 1, b = 2, c = 3; ^a > ^b ^+ ^c; ^a++)
359-
a = ^a ^+ 1;
361+
for(int a = 1, b = 2, c = 3; [[a]] > [[b + c]]; [[a]]++)
362+
a = [[a + 1]];
360363
// lambda
361-
auto lamb = [&^a, &^b](int r = ^1) {return 1;}
364+
auto lamb = [&[[a]], &[[b]]](int r = [[1]]) {return 1;}
362365
}
363366
)cpp");
364367
// vector of pairs of input and output strings
@@ -398,7 +401,7 @@ TEST(TweakTest, ExtractVariable) {
398401
{R"cpp(#define LOOP(x) {int a = x + 1;}
399402
void f(int a) {
400403
if(1)
401-
LOOP(5 + ^3)
404+
LOOP(5 + [[3]])
402405
})cpp",
403406
R"cpp(#define LOOP(x) {int a = x + 1;}
404407
void f(int a) {
@@ -407,22 +410,22 @@ TEST(TweakTest, ExtractVariable) {
407410
})cpp"},
408411
// label and attribute testing
409412
{R"cpp(void f(int a) {
410-
label: [ [gsl::suppress("type")] ] for (;;) a = ^1;
413+
label: [ [gsl::suppress("type")] ] for (;;) a = [[1]];
411414
})cpp",
412415
R"cpp(void f(int a) {
413416
auto dummy = 1; label: [ [gsl::suppress("type")] ] for (;;) a = dummy;
414417
})cpp"},
415418
// FIXME: Doesn't work because bug in selection tree
416419
/*{R"cpp(#define PLUS(x) x++
417420
void f(int a) {
418-
PLUS(^a);
421+
PLUS([[a]]);
419422
})cpp",
420423
R"cpp(#define PLUS(x) x++
421424
void f(int a) {
422425
auto dummy = a; PLUS(dummy);
423426
})cpp"},*/
424-
// FIXME: Doesn't work correctly for \[\[clang::uninitialized\]\] int b
425-
// = 1; since the attr is inside the DeclStmt and the bounds of
427+
// FIXME: Doesn't work correctly for \[\[clang::uninitialized\]\] int
428+
// b = [[1]]; since the attr is inside the DeclStmt and the bounds of
426429
// DeclStmt don't cover the attribute
427430
};
428431
for (const auto &IO : InputOutputs) {

0 commit comments

Comments
 (0)