10#include "gmock/gmock.h"
11#include "gtest/gtest.h"
19TEST_F(ExtractVariableTest, Test) {
20 const char *AvailableCases = R
"cpp(
27 return [[[[t.b[[a]]r]]([[t.z]])]];
30 int a = 5 + [[4 * [[[[xyz]]()]]]];
31 // multivariable initialization
33 int x = [[1]] + 1, y = a + [[1]], a = [[1]] + 2, z = a + 1;
48 for(a = [[1]] + 1; a > [[[[3]] + [[4]]]]; a++)
62 const char *AvailableC = R
"cpp(
69 const char *NoCrashCasesC = R
"cpp(
70 // error-ok: broken code, but shouldn't crash
76 const char *NoCrashDesignator = R
"cpp(
85 void foo(struct B *b) {
86 struct A a = {.x=b[[->]]y};
91 ExtraArgs = {"-xobjective-c"};
92 const char *AvailableObjC = R
"cpp(
93 __attribute__((objc_root_class))
104 const char *NoCrashCases = R
"cpp(
105 // error-ok: broken code, but shouldn't crash
106 template<typename T, typename ...Args>
107 struct Test<T, Args...> {
108 Test(const T &v) :val[[(^]]) {}
114 const char *UnavailableCases = R
"cpp(
115 int xyz(int a = [[1]]) {
117 int bar(int a = [[1]]) {
125 return [[t]].bar([[t]].z);
129 // function default argument
130 void f(int b = [[1]]) {
134 auto i = new int, j = new int;
135 [[[[delete i]], delete j]];
139 int x = 1, y = a + 1, a = 1, z = [[a + 1]];
144 for(int a = 1, b = 2, c = 3; a > [[b + c]]; [[a++]])
147 auto lamb = [&[[a]], &[[b]]](int r = [[1]]) {return 1;};
151 // Variable DeclRefExpr
154 // expression statement of type void
172 // lambdas: default args, cannot extract into function-local scope
173 [](int x = [[10]]){};
174 [](auto h = [[ [i = [](){}](){} ]]) {};
176 // lambdas: default args
177 // Extracting from capture initializers is usually fine,
178 // but not if the capture itself is nested inside a default argument
179 [](auto h = [i = [[ [](){} ]]](){}) {};
180 [](auto h = [i = [[ 42 ]]](){}) {};
184 if ([[ [&](){ return a + 1; } ]]() == 4)
187 for (int c = 0; [[ [&]() { return c < b; } ]](); ++c) {
189 for (int c = 0; [[ [&]() { return c < b; } () ]]; ++c) {
192 // lambdas: scope with structured binding
198 if (const auto [x, y] = c; x > y)
199 auto f = [[ [&]() { return x + y; } ]];
201 // lambdas: referencing outside variables that block extraction
202 // in trailing return type or in a decltype used
205 if ([[ []() -> decltype(a) { return 1; } ]] () == 4)
208 if ([[ [](int x = decltype(a){}) { return 1; } ]] () == 4)
211 if ([[ [](decltype(a) x) { return 1; } ]] (42) == 4)
217 ExtraArgs = {"-std=c++20"};
218 const char *UnavailableCasesCXX20 = R
"cpp(
219 template <typename T>
220 concept Constraint = requires (T t) { true; };
222 // lambdas: referencing outside variables that block extraction
223 // in requires clause or defaulted explicit template parameters
225 if ([[ [](auto b) requires (Constraint<decltype(a)> && Constraint<decltype(b)>) { return true; } ]] (a))
229 if ([[ []<typename T = decltype(a)>(T b) { return true; } ]] (a))
237 std::vector<std::pair<std::string, std::string>> InputOutputs = {
239 {R
"cpp(void varDecl() {
240 int a = 5 * (4 + (3 [[- 1)]]);
242 R"cpp(void varDecl() {
243 auto placeholder = (3 - 1); int a = 5 * (4 + placeholder);
269 {R
"cpp(#define PLUS(x) x++
271 int y = PLUS([[1+a]]);
278 R
"cpp(#define PLUS(x) x++
280 auto placeholder = PLUS(1+a); int y = placeholder;
283 {R
"cpp(#define LOOP(x) while (1) {a = x;}
288 R"cpp(#define LOOP(x) while (1) {a = x;}
290 auto placeholder = 3; if(1)
291 LOOP(5 + placeholder)
293 {R"cpp(#define LOOP(x) do {x;} while(1);
298 R"cpp(#define LOOP(x) do {x;} while(1);
300 auto placeholder = 3; if(1)
301 LOOP(5 + placeholder)
304 {R
"cpp(void f(int a) {
305 [ [gsl::suppress("type")] ] for (;;) a = [[1]] + 1;
307 R"cpp(void f(int a) {
308 auto placeholder = 1; [ [gsl::suppress("type")] ] for (;;) a = placeholder + 1;
313 return [[T().f()]].f();
318 auto placeholder = T().f(); return placeholder.f();
326 auto placeholder = f(); return placeholder;
334 int x = 1 + [[2 + 3 + 4]] + 5;
337 auto placeholder = 2 + 3 + 4; int x = 1 + placeholder + 5;
340 int x = [[1 + 2 + 3]] + 4 + 5;
343 auto placeholder = 1 + 2 + 3; int x = placeholder + 4 + 5;
346 int x = 1 + 2 + [[3 + 4 + 5]];
349 auto placeholder = 3 + 4 + 5; int x = 1 + 2 + placeholder;
353 int x = 1 - [[2 - 3 - 4]] - 5;
356 auto placeholder = 1 - 2 - 3 - 4; int x = placeholder - 5;
360 int x = 0 + 1 * [[2 + 3]] * 4 + 5;
363 auto placeholder = 1 * 2 + 3 * 4; int x = 0 + placeholder + 5;
373 S x = S(1) + [[S(2) + S(3) + S(4)]] + S(5);
381 auto placeholder = S(2) + S(3) + S(4); S x = S(1) + placeholder + S(5);
384 {R
"cpp(template <typename T> void f(T) {}
386 f([[ [](){ return 42; }]]);
389 R"cpp(template <typename T> void f(T) {}
391 auto placeholder = [](){ return 42; }; f( placeholder);
394 {R"cpp(template <typename T> void f(T) {}
396 f([x = [[40 + 2]] ](){ return 42; });
399 R"cpp(template <typename T> void f(T) {}
401 auto placeholder = 40 + 2; f([x = placeholder ](){ return 42; });
404 {R"cpp(auto foo(int VarA) {
406 return [[ [VarA, VarC = 42 + VarA](int VarB) { return VarA + VarB + VarC; }]];
410 R"cpp(auto foo(int VarA) {
412 auto placeholder = [VarA, VarC = 42 + VarA](int VarB) { return VarA + VarB + VarC; }; return placeholder;
416 {R"cpp(template <typename T> void f(T) {}
418 f([[ [&var](){ auto internal_val = 42; return var + internal_val; }]]);
421 R"cpp(template <typename T> void f(T) {}
423 auto placeholder = [&var](){ auto internal_val = 42; return var + internal_val; }; f( placeholder);
426 {R"cpp(template <typename T> void f(T) { }
430 f([[ [&var, &local_var, this]() {
431 auto internal_val = 42;
432 return var + local_var + internal_val + member;
439 R"cpp(template <typename T> void f(T) { }
443 auto placeholder = [&var, &local_var, this]() {
444 auto internal_val = 42;
445 return var + local_var + internal_val + member;
452 {R"cpp(void f() { auto x = +[[ [](){ return 42; }]]; })cpp",
453 R"cpp(void f() { auto placeholder = [](){ return 42; }; auto x = + placeholder; })cpp"},
455 template <typename T>
456 auto sink(T f) { return f(); }
458 return sink([[ []() { return 42; }]]);
462 template <typename T>
463 auto sink(T f) { return f(); }
465 auto placeholder = []() { return 42; }; return sink( placeholder);
471 if ([[ [&](){ return a + 1; } ]]() == 4)
478 auto placeholder = [&](){ return a + 1; }; if ( placeholder () == 4)
485 if ([[ [&](){ return a + 1; }() ]] == 4)
492 auto placeholder = [&](){ return a + 1; }(); if ( placeholder == 4)
497 int func() { return 0; }
502 int func() { return 0; }
504 auto placeholder = func();
507 template <typename T>
508 auto call(T t) { return t(); }
511 return [[ call([](){ int a = 1; return a + 1; }) ]] + 5;
514 template <typename T>
515 auto call(T t) { return t(); }
518 auto placeholder = call([](){ int a = 1; return a + 1; }); return placeholder + 5;
523 return [f = [[ [this](int g) { return g + x; } ]] ]() { return 42; }();
531 auto placeholder = [this](int g) { return g + x; }; return [f = placeholder ]() { return 42; }();
538 return [[ []() { return 42; }() ]];
542 auto placeholder = []() { return 42; }(); return placeholder ;
545 template <typename ...Ts>
546 void foo(Ts ...args) {
547 auto x = +[[ [&args...]() {} ]];
551 template <typename ...Ts>
552 void foo(Ts ...args) {
553 auto placeholder = [&args...]() {}; auto x = + placeholder ;
564 const auto [x, y] = c;
565 auto f = [[ [&]() { return x + y; } ]]();
576 const auto [x, y] = c;
577 auto placeholder = [&]() { return x + y; }; auto f = placeholder ();
588 if (const auto [x, y] = c; x > y) {
589 auto f = [[ [&]() { return x + y; } ]]();
601 if (const auto [x, y] = c; x > y) {
602 auto placeholder = [&]() { return x + y; }; auto f = placeholder ();
608 {R
"cpp(#define ECHO(X) X
610 int x = 1 + [[ECHO(2 + 3) + 4]] + 5;
612 R"cpp(#define ECHO(X) X
614 auto placeholder = 1 + ECHO(2 + 3) + 4; int x = placeholder + 5;
616 {R"cpp(#define ECHO(X) X
618 int x = 1 + [[ECHO(2) + ECHO(3) + 4]] + 5;
620 R"cpp(#define ECHO(X) X
622 auto placeholder = 1 + ECHO(2) + ECHO(3) + 4; int x = placeholder + 5;
625 for (
const auto &IO : InputOutputs) {
626 EXPECT_EQ(IO.second, apply(IO.first)) << IO.first;
632 {R
"cpp(struct Handlers {
633 void (*handlerFunc)(int);
635 void runFunction(void (*func)(int)) {}
636 void f(struct Handlers *handler) {
637 runFunction([[handler->handlerFunc]]);
639 R"cpp(struct Handlers {
640 void (*handlerFunc)(int);
642 void runFunction(void (*func)(int)) {}
643 void f(struct Handlers *handler) {
644 void (*placeholder)(int) = handler->handlerFunc; runFunction(placeholder);
646 {R"cpp(int (*foo(char))(int);
650 R"cpp(int (*foo(char))(int);
652 int (*placeholder)(int) = foo('c'); (void)placeholder;
655 {R
"cpp(typedef long NSInteger;
658 NSInteger b = [[a * 7]] + 3;
660 R"cpp(typedef long NSInteger;
663 NSInteger placeholder = a * 7; NSInteger b = placeholder + 3;
666 for (
const auto &IO : InputOutputs) {
667 EXPECT_EQ(IO.second, apply(IO.first)) << IO.first;
670 ExtraArgs = {
"-xobjective-c"};
672 __attribute__((objc_root_class))
674 - (void)setMethod1:(int)a;
680 [[self.method1]] = 1;
681 [[self.method1]] += 1;
688 {R
"cpp(__attribute__((objc_root_class))
694 int x = [[self.prop1]] + 1;
697 R"cpp(__attribute__((objc_root_class))
703 int placeholder = self.prop1; int x = placeholder + 1;
707 {R
"cpp(__attribute__((objc_root_class))
713 int x = [[self.method1]] + 1;
716 R"cpp(__attribute__((objc_root_class))
722 int placeholder = self.method1; int x = placeholder + 1;
726 for (
const auto &IO : InputOutputs) {
727 EXPECT_EQ(IO.second, apply(IO.first)) << IO.first;
#define TWEAK_TEST(TweakID)
#define EXPECT_AVAILABLE(MarkedCode)
#define EXPECT_UNAVAILABLE(MarkedCode)
TEST_F(BackgroundIndexTest, NoCrashOnErrorFile)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//