2019-08-31 06:52:55 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++11 -Wno-c99-designator %s
|
|
|
|
// RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++2a -Wno-c99-designator %s
|
2021-03-24 21:26:02 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -Wno-unused-value -verify -std=c++2b -Wno-c99-designator %s
|
2011-08-04 23:30:47 +08:00
|
|
|
|
2012-03-12 16:56:40 +08:00
|
|
|
enum E { e };
|
|
|
|
|
2014-04-13 12:31:48 +08:00
|
|
|
constexpr int id(int n) { return n; }
|
|
|
|
|
2011-08-04 23:30:47 +08:00
|
|
|
class C {
|
|
|
|
|
|
|
|
int f() {
|
|
|
|
int foo, bar;
|
|
|
|
|
|
|
|
[]; // expected-error {{expected body of lambda expression}}
|
|
|
|
[+] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
|
|
|
|
[foo+] {}; // expected-error {{expected ',' or ']' in lambda capture list}}
|
|
|
|
[foo,&this] {}; // expected-error {{'this' cannot be captured by reference}}
|
|
|
|
[&this] {}; // expected-error {{'this' cannot be captured by reference}}
|
2011-12-15 08:38:15 +08:00
|
|
|
[&,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
|
|
|
|
[=,] {}; // expected-error {{expected variable name or 'this' in lambda capture list}}
|
2021-03-24 21:26:02 +08:00
|
|
|
[] {};
|
2012-02-09 16:26:42 +08:00
|
|
|
[=] (int i) {};
|
|
|
|
[&] (int) mutable -> void {};
|
|
|
|
[foo,bar] () { return 3; };
|
|
|
|
[=,&foo] () {};
|
|
|
|
[&,foo] () {};
|
|
|
|
[this] () {};
|
2012-03-12 16:56:40 +08:00
|
|
|
[] () -> class C { return C(); };
|
|
|
|
[] () -> enum E { return e; };
|
2011-08-04 23:30:47 +08:00
|
|
|
|
2021-03-24 21:26:02 +08:00
|
|
|
[] -> int { return 0; };
|
|
|
|
[] mutable -> int { return 0; };
|
|
|
|
#if __cplusplus <= 202002L
|
|
|
|
// expected-warning@-3 {{lambda without a parameter clause is a C++2b extension}}
|
|
|
|
// expected-warning@-3 {{is a C++2b extension}}
|
|
|
|
#endif
|
2012-08-30 21:13:20 +08:00
|
|
|
[](int) -> {}; // PR13652 expected-error {{expected a type}}
|
2011-08-04 23:30:47 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-17 11:49:44 +08:00
|
|
|
void designator_or_lambda() {
|
|
|
|
typedef int T;
|
|
|
|
const int b = 0;
|
|
|
|
const int c = 1;
|
2014-04-13 12:31:48 +08:00
|
|
|
int d;
|
2014-04-02 13:58:29 +08:00
|
|
|
int a1[1] = {[b] (T()) {}}; // expected-error{{no viable conversion from '(lambda}}
|
2012-02-17 11:49:44 +08:00
|
|
|
int a2[1] = {[b] = 1 };
|
2014-04-13 12:31:48 +08:00
|
|
|
int a3[1] = {[b,c] = 1 }; // expected-error{{expected ']'}} expected-note {{to match}}
|
2012-02-17 11:49:44 +08:00
|
|
|
int a4[1] = {[&b] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'const int *'}}
|
|
|
|
int a5[3] = { []{return 0;}() };
|
|
|
|
int a6[1] = {[this] = 1 }; // expected-error{{integral constant expression must have integral or unscoped enumeration type, not 'C *'}}
|
2019-05-19 23:07:58 +08:00
|
|
|
int a7[1] = {[d(0)] { return d; } ()};
|
|
|
|
int a8[1] = {[d = 0] { return d; } ()};
|
|
|
|
int a10[1] = {[id(0)] { return id; } ()};
|
|
|
|
#if __cplusplus <= 201103L
|
|
|
|
// expected-warning@-4{{extension}}
|
|
|
|
// expected-warning@-4{{extension}}
|
|
|
|
// expected-warning@-4{{extension}}
|
|
|
|
#endif
|
2014-04-13 12:31:48 +08:00
|
|
|
int a9[1] = {[d = 0] = 1}; // expected-error{{is not an integral constant expression}}
|
2019-05-19 23:07:58 +08:00
|
|
|
#if __cplusplus >= 201402L
|
|
|
|
// expected-note@-2{{constant expression cannot modify an object that is visible outside that expression}}
|
|
|
|
#endif
|
2014-04-13 12:31:48 +08:00
|
|
|
int a11[1] = {[id(0)] = 1};
|
2012-02-17 11:49:44 +08:00
|
|
|
}
|
2012-08-10 03:01:51 +08:00
|
|
|
|
|
|
|
void delete_lambda(int *p) {
|
|
|
|
delete [] p;
|
|
|
|
delete [] (int*) { new int }; // ok, compound-literal, not lambda
|
2019-05-19 23:07:58 +08:00
|
|
|
delete [] { return new int; } (); // expected-error {{'[]' after delete interpreted as 'delete[]'}}
|
2012-08-10 03:01:51 +08:00
|
|
|
delete [&] { return new int; } (); // ok, lambda
|
2019-05-19 23:07:58 +08:00
|
|
|
|
|
|
|
delete []() { return new int; }(); // expected-error{{'[]' after delete interpreted as 'delete[]'}}
|
|
|
|
delete [](E Enum) { return new int((int)Enum); }(e); // expected-error{{'[]' after delete interpreted as 'delete[]'}}
|
|
|
|
#if __cplusplus > 201703L
|
|
|
|
delete []<int = 0>() { return new int; }(); // expected-error{{'[]' after delete interpreted as 'delete[]'}}
|
|
|
|
#endif
|
2012-08-10 03:01:51 +08:00
|
|
|
}
|
2013-05-10 05:36:41 +08:00
|
|
|
|
|
|
|
// We support init-captures in C++11 as an extension.
|
|
|
|
int z;
|
|
|
|
void init_capture() {
|
2019-05-19 23:07:58 +08:00
|
|
|
[n(0)] () mutable -> int { return ++n; };
|
|
|
|
[n{0}] { return; };
|
|
|
|
[a([&b = z]{})](){};
|
|
|
|
[n = 0] { return ++n; }; // expected-error {{captured by copy in a non-mutable}}
|
|
|
|
[n = {0}] { return; }; // expected-error {{<initializer_list>}}
|
|
|
|
#if __cplusplus <= 201103L
|
|
|
|
// expected-warning@-6{{extension}}
|
|
|
|
// expected-warning@-6{{extension}}
|
|
|
|
// expected-warning@-6{{extension}}
|
|
|
|
// expected-warning@-7{{extension}}
|
|
|
|
// expected-warning@-7{{extension}}
|
|
|
|
// expected-warning@-7{{extension}}
|
|
|
|
#endif
|
2013-05-10 05:36:41 +08:00
|
|
|
|
2013-05-16 14:20:58 +08:00
|
|
|
int x = 4;
|
2019-05-19 23:07:58 +08:00
|
|
|
auto y = [&r = x, x = x + 1]() -> int {
|
|
|
|
#if __cplusplus <= 201103L
|
|
|
|
// expected-warning@-2{{extension}}
|
|
|
|
// expected-warning@-3{{extension}}
|
|
|
|
#endif
|
2013-09-28 12:02:39 +08:00
|
|
|
r += 2;
|
|
|
|
return x + 2;
|
2013-05-10 05:36:41 +08:00
|
|
|
} ();
|
|
|
|
}
|
2014-03-12 04:32:35 +08:00
|
|
|
|
|
|
|
void attributes() {
|
2021-03-24 21:26:02 +08:00
|
|
|
[] __attribute__((noreturn)){};
|
|
|
|
#if __cplusplus <= 202002L
|
|
|
|
// expected-warning@-2 {{is a C++2b extension}}
|
|
|
|
#endif
|
2014-03-12 04:32:35 +08:00
|
|
|
[]() [[]]
|
|
|
|
mutable {}; // expected-error {{expected body of lambda expression}}
|
|
|
|
|
|
|
|
[]() [[]] {};
|
|
|
|
[]() [[]] -> void {};
|
|
|
|
[]() mutable [[]] -> void {};
|
|
|
|
[]() mutable noexcept [[]] -> void {};
|
|
|
|
|
2014-03-12 08:01:07 +08:00
|
|
|
// Testing GNU-style attributes on lambdas -- the attribute is specified
|
|
|
|
// before the mutable specifier instead of after (unlike C++11).
|
|
|
|
[]() __attribute__((noreturn)) mutable { while(1); };
|
|
|
|
[]() mutable
|
|
|
|
__attribute__((noreturn)) { while(1); }; // expected-error {{expected body of lambda expression}}
|
2021-03-03 23:02:51 +08:00
|
|
|
|
|
|
|
// Testing support for P2173 on adding attributes to the declaration
|
|
|
|
// rather than the type.
|
2021-03-24 21:26:02 +08:00
|
|
|
[][[]](){};
|
|
|
|
#if __cplusplus <= 202002L
|
|
|
|
// expected-warning@-2 {{an attribute specifier sequence in this position is a C++2b extension}}
|
|
|
|
#endif
|
2021-03-03 23:02:51 +08:00
|
|
|
#if __cplusplus > 201703L
|
2021-03-24 21:26:02 +08:00
|
|
|
[]<typename>[[]](){};
|
|
|
|
#if __cplusplus <= 202002L
|
|
|
|
// expected-warning@-2 {{an attribute specifier sequence in this position is a C++2b extension}}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
[][[]]{};
|
|
|
|
#if __cplusplus <= 202002L
|
|
|
|
// expected-warning@-2 {{an attribute specifier sequence in this position is a C++2b extension}}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void missing_parens() {
|
|
|
|
[] mutable {};
|
|
|
|
[] noexcept {};
|
|
|
|
#if __cplusplus <= 202002L
|
|
|
|
// expected-warning@-3 {{is a C++2b extension}}
|
|
|
|
// expected-warning@-3 {{is a C++2b extension}}
|
2021-03-03 23:02:51 +08:00
|
|
|
#endif
|
2014-03-12 08:01:07 +08:00
|
|
|
}
|
2011-08-04 23:30:47 +08:00
|
|
|
};
|
2015-01-09 13:10:55 +08:00
|
|
|
|
|
|
|
template <typename>
|
|
|
|
void PR22122() {
|
|
|
|
[](int) -> {}; // expected-error {{expected a type}}
|
|
|
|
}
|
|
|
|
|
|
|
|
template void PR22122<int>();
|
2015-01-12 10:28:16 +08:00
|
|
|
|
2019-07-31 03:21:20 +08:00
|
|
|
namespace PR42778 {
|
|
|
|
struct A {
|
|
|
|
template <class F> A(F&&) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct S {
|
|
|
|
void mf() { A{[*this]{}}; }
|
|
|
|
#if __cplusplus < 201703L
|
|
|
|
// expected-warning@-2 {{C++17 extension}}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-01-12 10:28:16 +08:00
|
|
|
struct S {
|
|
|
|
template <typename T>
|
|
|
|
void m (T x =[0); // expected-error{{expected variable name or 'this' in lambda capture list}}
|
|
|
|
} s;
|
2015-01-12 11:14:18 +08:00
|
|
|
|
|
|
|
struct U {
|
|
|
|
template <typename T>
|
|
|
|
void m_fn1(T x = 0[0); // expected-error{{expected ']'}} expected-note{{to match this '['}}
|
|
|
|
} *U;
|