1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
|
//===-- ExtractVariableTests.cpp --------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "TweakTesting.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace clang {
namespace clangd {
namespace {
TWEAK_TEST(ExtractVariable);
TEST_F(ExtractVariableTest, Test) {
const char *AvailableCases = R"cpp(
int xyz(int a = 1) {
struct T {
int bar(int a = 1);
int z;
} t;
// return statement
return [[[[t.b[[a]]r]]([[t.z]])]];
}
void f() {
int a = 5 + [[4 * [[[[xyz]]()]]]];
// multivariable initialization
if(1)
int x = [[1]] + 1, y = a + [[1]], a = [[1]] + 2, z = a + 1;
// if without else
if([[1]])
a = [[1]] + 1;
// if with else
if(a < [[3]])
if(a == [[4]])
a = [[5]] + 1;
else
a = [[5]] + 1;
else if (a < [[4]])
a = [[4]] + 1;
else
a = [[5]] + 1;
// for loop
for(a = [[1]] + 1; a > [[[[3]] + [[4]]]]; a++)
a = [[2]] + 1;
// while
while(a < [[1]])
a = [[1]] + 1;
// do while
do
a = [[1]] + 1;
while(a < [[3]]);
}
)cpp";
EXPECT_AVAILABLE(AvailableCases);
ExtraArgs = {"-xc"};
const char *AvailableC = R"cpp(
void foo() {
int x = [[1]] + 1;
})cpp";
EXPECT_AVAILABLE(AvailableC);
ExtraArgs = {"-xc"};
const char *NoCrashCasesC = R"cpp(
// error-ok: broken code, but shouldn't crash
int x = [[foo()]];
)cpp";
EXPECT_UNAVAILABLE(NoCrashCasesC);
ExtraArgs = {"-xc"};
const char *NoCrashDesignator = R"cpp(
struct A {
struct {
int x;
};
};
struct B {
int y;
};
void foo(struct B *b) {
struct A a = {.x=b[[->]]y};
}
)cpp";
EXPECT_AVAILABLE(NoCrashDesignator);
ExtraArgs = {"-xobjective-c"};
const char *AvailableObjC = R"cpp(
__attribute__((objc_root_class))
@interface Foo
@end
@implementation Foo
- (void)method {
int x = [[1]] + 2;
}
@end)cpp";
EXPECT_AVAILABLE(AvailableObjC);
ExtraArgs = {};
const char *NoCrashCases = R"cpp(
// error-ok: broken code, but shouldn't crash
template<typename T, typename ...Args>
struct Test<T, Args...> {
Test(const T &v) :val[[(^]]) {}
T val;
};
)cpp";
EXPECT_UNAVAILABLE(NoCrashCases);
const char *UnavailableCases = R"cpp(
int xyz(int a = [[1]]) {
struct T {
int bar(int a = [[1]]) {
int b = [[z]];
}
int z = [[1]];
} t;
int x = [[1 + 2]];
int y;
y = [[1 + 2]];
return [[t]].bar([[t]].z);
}
void v() { return; }
// function default argument
void f(int b = [[1]]) {
// empty selection
int a = ^1 ^+ ^2;
// void expressions
auto i = new int, j = new int;
[[[[delete i]], delete j]];
[[v]]();
// if
if(1)
int x = 1, y = a + 1, a = 1, z = [[a + 1]];
if(int a = 1)
if([[a + 1]] == 4)
a = [[[[a]] +]] 1;
// for loop
for(int a = 1, b = 2, c = 3; a > [[b + c]]; [[a++]])
a = [[a + 1]];
// lambda
auto lamb = [&[[a]], &[[b]]](int r = [[1]]) {return 1;};
// assignment
xyz([[a = 5]]);
xyz([[a *= 5]]);
// Variable DeclRefExpr
a = [[b]];
a = [[xyz()]];
// statement expression
[[xyz()]];
while (a)
[[++a]];
// label statement
goto label;
label:
a = [[1]];
// lambdas: captures
int x = 0;
[ [[=]] ](){};
[ [[&]] ](){};
[ [[x]] ](){};
[ [[&x]] ](){};
[y = [[x]] ](){};
[ [[y = x]] ](){};
// lambdas: default args, cannot extract into function-local scope
[](int x = [[10]]){};
[](auto h = [[ [i = [](){}](){} ]]) {};
// lambdas: default args
// Extracting from capture initializers is usually fine,
// but not if the capture itself is nested inside a default argument
[](auto h = [i = [[ [](){} ]]](){}) {};
[](auto h = [i = [[ 42 ]]](){}) {};
// lambdas: scope
if (int a = 1)
if ([[ [&](){ return a + 1; } ]]() == 4)
a = a + 1;
for (int c = 0; [[ [&]() { return c < b; } ]](); ++c) {
}
for (int c = 0; [[ [&]() { return c < b; } () ]]; ++c) {
}
// lambdas: scope with structured binding
struct Coordinates {
int x{};
int y{};
};
Coordinates c{};
if (const auto [x, y] = c; x > y)
auto f = [[ [&]() { return x + y; } ]];
// lambdas: referencing outside variables that block extraction
// in trailing return type or in a decltype used
// by a parameter
if (int a = 1)
if ([[ []() -> decltype(a) { return 1; } ]] () == 4)
a = a + 1;
if (int a = 1)
if ([[ [](int x = decltype(a){}) { return 1; } ]] () == 4)
a = a + 1;
if (int a = 1)
if ([[ [](decltype(a) x) { return 1; } ]] (42) == 4)
a = a + 1;
}
)cpp";
EXPECT_UNAVAILABLE(UnavailableCases);
ExtraArgs = {"-std=c++20"};
const char *UnavailableCasesCXX20 = R"cpp(
template <typename T>
concept Constraint = requires (T t) { true; };
void foo() {
// lambdas: referencing outside variables that block extraction
// in requires clause or defaulted explicit template parameters
if (int a = 1)
if ([[ [](auto b) requires (Constraint<decltype(a)> && Constraint<decltype(b)>) { return true; } ]] (a))
a = a + 1;
if (int a = 1)
if ([[ []<typename T = decltype(a)>(T b) { return true; } ]] (a))
a = a + 1;
}
)cpp";
EXPECT_UNAVAILABLE(UnavailableCasesCXX20);
ExtraArgs.clear();
// vector of pairs of input and output strings
std::vector<std::pair<std::string, std::string>> InputOutputs = {
// extraction from variable declaration/assignment
{R"cpp(void varDecl() {
int a = 5 * (4 + (3 [[- 1)]]);
})cpp",
R"cpp(void varDecl() {
auto placeholder = (3 - 1); int a = 5 * (4 + placeholder);
})cpp"},
// FIXME: extraction from switch case
/*{R"cpp(void f(int a) {
if(1)
while(a < 1)
switch (1) {
case 1:
a = [[1 + 2]];
break;
default:
break;
}
})cpp",
R"cpp(void f(int a) {
auto placeholder = 1 + 2; if(1)
while(a < 1)
switch (1) {
case 1:
a = placeholder;
break;
default:
break;
}
})cpp"},*/
// Macros
{R"cpp(#define PLUS(x) x++
void f(int a) {
int y = PLUS([[1+a]]);
})cpp",
/*FIXME: It should be extracted like this.
R"cpp(#define PLUS(x) x++
void f(int a) {
auto placeholder = 1+a; int y = PLUS(placeholder);
})cpp"},*/
R"cpp(#define PLUS(x) x++
void f(int a) {
auto placeholder = PLUS(1+a); int y = placeholder;
})cpp"},
// ensure InsertionPoint isn't inside a macro
{R"cpp(#define LOOP(x) while (1) {a = x;}
void f(int a) {
if(1)
LOOP(5 + [[3]])
})cpp",
R"cpp(#define LOOP(x) while (1) {a = x;}
void f(int a) {
auto placeholder = 3; if(1)
LOOP(5 + placeholder)
})cpp"},
{R"cpp(#define LOOP(x) do {x;} while(1);
void f(int a) {
if(1)
LOOP(5 + [[3]])
})cpp",
R"cpp(#define LOOP(x) do {x;} while(1);
void f(int a) {
auto placeholder = 3; if(1)
LOOP(5 + placeholder)
})cpp"},
// attribute testing
{R"cpp(void f(int a) {
[ [gsl::suppress("type")] ] for (;;) a = [[1]] + 1;
})cpp",
R"cpp(void f(int a) {
auto placeholder = 1; [ [gsl::suppress("type")] ] for (;;) a = placeholder + 1;
})cpp"},
// MemberExpr
{R"cpp(class T {
T f() {
return [[T().f()]].f();
}
};)cpp",
R"cpp(class T {
T f() {
auto placeholder = T().f(); return placeholder.f();
}
};)cpp"},
// Function DeclRefExpr
{R"cpp(int f() {
return [[f]]();
})cpp",
R"cpp(int f() {
auto placeholder = f(); return placeholder;
})cpp"},
// FIXME: Wrong result for \[\[clang::uninitialized\]\] int b = [[1]];
// since the attr is inside the DeclStmt and the bounds of
// DeclStmt don't cover the attribute.
// Binary subexpressions
{R"cpp(void f() {
int x = 1 + [[2 + 3 + 4]] + 5;
})cpp",
R"cpp(void f() {
auto placeholder = 2 + 3 + 4; int x = 1 + placeholder + 5;
})cpp"},
{R"cpp(void f() {
int x = [[1 + 2 + 3]] + 4 + 5;
})cpp",
R"cpp(void f() {
auto placeholder = 1 + 2 + 3; int x = placeholder + 4 + 5;
})cpp"},
{R"cpp(void f() {
int x = 1 + 2 + [[3 + 4 + 5]];
})cpp",
R"cpp(void f() {
auto placeholder = 3 + 4 + 5; int x = 1 + 2 + placeholder;
})cpp"},
// Non-associative operations have no special support
{R"cpp(void f() {
int x = 1 - [[2 - 3 - 4]] - 5;
})cpp",
R"cpp(void f() {
auto placeholder = 1 - 2 - 3 - 4; int x = placeholder - 5;
})cpp"},
// A mix of associative operators isn't associative.
{R"cpp(void f() {
int x = 0 + 1 * [[2 + 3]] * 4 + 5;
})cpp",
R"cpp(void f() {
auto placeholder = 1 * 2 + 3 * 4; int x = 0 + placeholder + 5;
})cpp"},
// Overloaded operators are supported, we assume associativity
// as if they were built-in.
{R"cpp(struct S {
S(int);
};
S operator+(S, S);
void f() {
S x = S(1) + [[S(2) + S(3) + S(4)]] + S(5);
})cpp",
R"cpp(struct S {
S(int);
};
S operator+(S, S);
void f() {
auto placeholder = S(2) + S(3) + S(4); S x = S(1) + placeholder + S(5);
})cpp"},
// lambda expressions
{R"cpp(template <typename T> void f(T) {}
void f2() {
f([[ [](){ return 42; }]]);
}
)cpp",
R"cpp(template <typename T> void f(T) {}
void f2() {
auto placeholder = [](){ return 42; }; f( placeholder);
}
)cpp"},
{R"cpp(template <typename T> void f(T) {}
void f2() {
f([x = [[40 + 2]] ](){ return 42; });
}
)cpp",
R"cpp(template <typename T> void f(T) {}
void f2() {
auto placeholder = 40 + 2; f([x = placeholder ](){ return 42; });
}
)cpp"},
{R"cpp(auto foo(int VarA) {
return [VarA]() {
return [[ [VarA, VarC = 42 + VarA](int VarB) { return VarA + VarB + VarC; }]];
};
}
)cpp",
R"cpp(auto foo(int VarA) {
return [VarA]() {
auto placeholder = [VarA, VarC = 42 + VarA](int VarB) { return VarA + VarB + VarC; }; return placeholder;
};
}
)cpp"},
{R"cpp(template <typename T> void f(T) {}
void f2(int var) {
f([[ [&var](){ auto internal_val = 42; return var + internal_val; }]]);
}
)cpp",
R"cpp(template <typename T> void f(T) {}
void f2(int var) {
auto placeholder = [&var](){ auto internal_val = 42; return var + internal_val; }; f( placeholder);
}
)cpp"},
{R"cpp(template <typename T> void f(T) { }
struct A {
void f2(int& var) {
auto local_var = 42;
f([[ [&var, &local_var, this]() {
auto internal_val = 42;
return var + local_var + internal_val + member;
}]]);
}
int member = 42;
};
)cpp",
R"cpp(template <typename T> void f(T) { }
struct A {
void f2(int& var) {
auto local_var = 42;
auto placeholder = [&var, &local_var, this]() {
auto internal_val = 42;
return var + local_var + internal_val + member;
}; f( placeholder);
}
int member = 42;
};
)cpp"},
{R"cpp(void f() { auto x = +[[ [](){ return 42; }]]; })cpp",
R"cpp(void f() { auto placeholder = [](){ return 42; }; auto x = + placeholder; })cpp"},
{R"cpp(
template <typename T>
auto sink(T f) { return f(); }
int bar() {
return sink([[ []() { return 42; }]]);
}
)cpp",
R"cpp(
template <typename T>
auto sink(T f) { return f(); }
int bar() {
auto placeholder = []() { return 42; }; return sink( placeholder);
}
)cpp"},
{R"cpp(
int main() {
if (int a = 1) {
if ([[ [&](){ return a + 1; } ]]() == 4)
a = a + 1;
}
})cpp",
R"cpp(
int main() {
if (int a = 1) {
auto placeholder = [&](){ return a + 1; }; if ( placeholder () == 4)
a = a + 1;
}
})cpp"},
{R"cpp(
int main() {
if (int a = 1) {
if ([[ [&](){ return a + 1; }() ]] == 4)
a = a + 1;
}
})cpp",
R"cpp(
int main() {
if (int a = 1) {
auto placeholder = [&](){ return a + 1; }(); if ( placeholder == 4)
a = a + 1;
}
})cpp"},
{R"cpp(
template <typename T>
auto call(T t) { return t(); }
int main() {
return [[ call([](){ int a = 1; return a + 1; }) ]] + 5;
})cpp",
R"cpp(
template <typename T>
auto call(T t) { return t(); }
int main() {
auto placeholder = call([](){ int a = 1; return a + 1; }); return placeholder + 5;
})cpp"},
{R"cpp(
class Foo {
int bar() {
return [f = [[ [this](int g) { return g + x; } ]] ]() { return 42; }();
}
int x;
};
)cpp",
R"cpp(
class Foo {
int bar() {
auto placeholder = [this](int g) { return g + x; }; return [f = placeholder ]() { return 42; }();
}
int x;
};
)cpp"},
{R"cpp(
int main() {
return [[ []() { return 42; }() ]];
})cpp",
R"cpp(
int main() {
auto placeholder = []() { return 42; }(); return placeholder ;
})cpp"},
{R"cpp(
template <typename ...Ts>
void foo(Ts ...args) {
auto x = +[[ [&args...]() {} ]];
}
)cpp",
R"cpp(
template <typename ...Ts>
void foo(Ts ...args) {
auto placeholder = [&args...]() {}; auto x = + placeholder ;
}
)cpp"},
{R"cpp(
struct Coordinates {
int x{};
int y{};
};
int main() {
Coordinates c = {};
const auto [x, y] = c;
auto f = [[ [&]() { return x + y; } ]]();
}
)cpp",
R"cpp(
struct Coordinates {
int x{};
int y{};
};
int main() {
Coordinates c = {};
const auto [x, y] = c;
auto placeholder = [&]() { return x + y; }; auto f = placeholder ();
}
)cpp"},
{R"cpp(
struct Coordinates {
int x{};
int y{};
};
int main() {
Coordinates c = {};
if (const auto [x, y] = c; x > y) {
auto f = [[ [&]() { return x + y; } ]]();
}
}
)cpp",
R"cpp(
struct Coordinates {
int x{};
int y{};
};
int main() {
Coordinates c = {};
if (const auto [x, y] = c; x > y) {
auto placeholder = [&]() { return x + y; }; auto f = placeholder ();
}
}
)cpp"},
// Don't try to analyze across macro boundaries
// FIXME: it'd be nice to do this someday (in a safe way)
{R"cpp(#define ECHO(X) X
void f() {
int x = 1 + [[ECHO(2 + 3) + 4]] + 5;
})cpp",
R"cpp(#define ECHO(X) X
void f() {
auto placeholder = 1 + ECHO(2 + 3) + 4; int x = placeholder + 5;
})cpp"},
{R"cpp(#define ECHO(X) X
void f() {
int x = 1 + [[ECHO(2) + ECHO(3) + 4]] + 5;
})cpp",
R"cpp(#define ECHO(X) X
void f() {
auto placeholder = 1 + ECHO(2) + ECHO(3) + 4; int x = placeholder + 5;
})cpp"},
};
for (const auto &IO : InputOutputs) {
EXPECT_EQ(IO.second, apply(IO.first)) << IO.first;
}
ExtraArgs = {"-xc"};
InputOutputs = {
// Function Pointers
{R"cpp(struct Handlers {
void (*handlerFunc)(int);
};
void runFunction(void (*func)(int)) {}
void f(struct Handlers *handler) {
runFunction([[handler->handlerFunc]]);
})cpp",
R"cpp(struct Handlers {
void (*handlerFunc)(int);
};
void runFunction(void (*func)(int)) {}
void f(struct Handlers *handler) {
void (*placeholder)(int) = handler->handlerFunc; runFunction(placeholder);
})cpp"},
{R"cpp(int (*foo(char))(int);
void bar() {
(void)[[foo('c')]];
})cpp",
R"cpp(int (*foo(char))(int);
void bar() {
int (*placeholder)(int) = foo('c'); (void)placeholder;
})cpp"},
// Arithmetic on typedef types preserves typedef types
{R"cpp(typedef long NSInteger;
void varDecl() {
NSInteger a = 2 * 5;
NSInteger b = [[a * 7]] + 3;
})cpp",
R"cpp(typedef long NSInteger;
void varDecl() {
NSInteger a = 2 * 5;
NSInteger placeholder = a * 7; NSInteger b = placeholder + 3;
})cpp"},
};
for (const auto &IO : InputOutputs) {
EXPECT_EQ(IO.second, apply(IO.first)) << IO.first;
}
ExtraArgs = {"-xobjective-c"};
EXPECT_UNAVAILABLE(R"cpp(
__attribute__((objc_root_class))
@interface Foo
- (void)setMethod1:(int)a;
- (int)method1;
@property int prop1;
@end
@implementation Foo
- (void)method {
[[self.method1]] = 1;
[[self.method1]] += 1;
[[self.prop1]] = 1;
[[self.prop1]] += 1;
}
@end)cpp");
InputOutputs = {
// Support ObjC property references (explicit property getter).
{R"cpp(__attribute__((objc_root_class))
@interface Foo
@property int prop1;
@end
@implementation Foo
- (void)method {
int x = [[self.prop1]] + 1;
}
@end)cpp",
R"cpp(__attribute__((objc_root_class))
@interface Foo
@property int prop1;
@end
@implementation Foo
- (void)method {
int placeholder = self.prop1; int x = placeholder + 1;
}
@end)cpp"},
// Support ObjC property references (implicit property getter).
{R"cpp(__attribute__((objc_root_class))
@interface Foo
- (int)method1;
@end
@implementation Foo
- (void)method {
int x = [[self.method1]] + 1;
}
@end)cpp",
R"cpp(__attribute__((objc_root_class))
@interface Foo
- (int)method1;
@end
@implementation Foo
- (void)method {
int placeholder = self.method1; int x = placeholder + 1;
}
@end)cpp"},
};
for (const auto &IO : InputOutputs) {
EXPECT_EQ(IO.second, apply(IO.first)) << IO.first;
}
}
} // namespace
} // namespace clangd
} // namespace clang
|