1 // RUN: %clang_cc1 -std=c++2b -fsyntax-only -verify=expected,cxx2b %s
2 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=expected,cxx11_20 %s
3 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify=expected,cxx11_20,cxx11 %s
7 return &x
; // expected-warning {{address of stack memory}}
10 int* ret_local_array() {
12 return x
; // expected-warning {{address of stack memory}}
15 int* ret_local_array_element(int i
) {
17 return &x
[i
]; // expected-warning {{address of stack memory}}
20 int *ret_local_array_element_reversed(int i
) {
22 return &i
[x
]; // expected-warning {{address of stack memory}}
25 int* ret_local_array_element_const_index() {
27 return &x
[2]; // expected-warning {{address of stack memory}}
30 int& ret_local_ref() {
32 return x
; // cxx11_20-warning {{reference to stack memory}}
33 // cxx2b-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
36 int* ret_local_addrOf() {
38 return &*&x
; // expected-warning {{address of stack memory}}
41 int* ret_local_addrOf_paren() {
43 return (&(*(&x
))); // expected-warning {{address of stack memory}}
46 int* ret_local_addrOf_ptr_arith() {
48 return &*(&x
+1); // expected-warning {{address of stack memory}}
51 int* ret_local_addrOf_ptr_arith2() {
53 return &*(&x
+1); // expected-warning {{address of stack memory}}
56 int* ret_local_field() {
58 return &a
.x
; // expected-warning {{address of stack memory}}
61 int& ret_local_field_ref() {
63 return a
.x
; // expected-warning {{reference to stack memory}}
66 int* ret_conditional(bool cond
) {
69 return cond
? &x
// expected-warning {{address of stack memory associated with local variable 'x' returned}}
70 : &y
; // expected-warning {{address of stack memory associated with local variable 'y' returned}}
73 int* ret_conditional_rhs(int *x
, bool cond
) {
75 return cond
? x
: &y
; // expected-warning {{address of stack memory}}
80 return (void*) &x
; // expected-warning {{address of stack memory}}
83 int* ret_static_var() {
85 return &x
; // no warning.
91 return &z
; // no warning.
94 int* ret_parameter(int x
) {
95 return &x
; // expected-warning {{address of stack memory}}
99 void* ret_cpp_static_cast(short x
) {
100 return static_cast<void*>(&x
); // expected-warning {{address of stack memory}}
103 int* ret_cpp_reinterpret_cast(double x
) {
104 return reinterpret_cast<int*>(&x
); // expected-warning {{address of stack me}}
107 int* ret_cpp_reinterpret_cast_no_warning(long x
) {
108 return reinterpret_cast<int*>(x
); // no-warning
111 int* ret_cpp_const_cast(const int x
) {
112 return const_cast<int*>(&x
); // expected-warning {{address of stack memory}}
115 struct A
{ virtual ~A(); }; struct B
: A
{};
116 A
* ret_cpp_dynamic_cast(B b
) {
117 return dynamic_cast<A
*>(&b
); // expected-warning {{address of stack memory}}
120 // PR 7999 - handle the case where a field is itself a reference.
121 template <typename T
> struct PR7999
{
122 PR7999(T
& t
) : value(t
) {}
128 PR7999_X
& PR7999_f(PR7999
<PR7999_X
> s
) { return s
.value
; } // no-warning
129 void test_PR7999(PR7999_X
& x
) { (void)PR7999_f(x
); } // no-warning
131 // PR 8774: Don't try to evaluate parameters with default arguments like
132 // variables with an initializer, especially in templates where the default
133 // argument may not be an expression (yet).
135 template <typename U
> struct B
{ };
136 template <typename V
> V
f(typename B
<V
>::type
const &v
= B
<V
>::value()) {
139 template <> struct B
<const char *> {
140 typedef const char *type
;
141 static const char *value();
149 // Don't warn about returning a local variable from a surrounding function if
150 // we're within a lambda-expression.
151 void ret_from_lambda() {
154 (void) [&]() -> int& { return a
; };
155 (void) [&]() -> int& { return b
; };
156 (void) [=]() mutable -> int& { return a
; };
157 (void) [=]() mutable -> int& { return b
; };
158 (void) [&]() -> int& { int a
; return a
; }; // cxx11_20-warning {{reference to stack}}
159 // cxx2b-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
160 (void) [=]() -> int& { int a
; return a
; }; // cxx11_20-warning {{reference to stack}}
161 // cxx2b-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}
162 (void) [&]() -> int& { int &a
= b
; return a
; };
163 (void) [=]() mutable -> int& { int &a
= b
; return a
; };
167 return [&] { // expected-warning {{address of stack memory associated with local variable 'a' returned}}
168 return a
; // expected-note {{implicitly captured by reference due to use here}}
173 return [&a
] {}; // expected-warning {{address of stack memory associated with local variable 'a' returned}} expected-note {{captured by reference here}}
187 // cxx11-warning@+1 {{C++14}}
188 return [&b
= a
] {}; // expected-warning {{address of stack memory associated with local variable 'a' returned}} expected-note {{captured by reference via initialization of lambda capture 'b'}}
192 // cxx11-warning@+1 {{C++14}}
193 return [b
= &a
] {}; // expected-warning {{address of stack memory associated with local variable 'a' returned}} expected-note {{captured via initialization of lambda capture 'b'}}
197 struct HoldsPointer
{ int *p
; };
199 HoldsPointer
ret_via_member_1() {
201 return {&n
}; // expected-warning {{address of stack memory associated with local variable 'n' returned}}
203 HoldsPointer
ret_via_member_2() {
205 return HoldsPointer(HoldsPointer
{&n
}); // cxx11-warning {{address of stack memory associated with local variable 'n' returned}}
207 // FIXME: We could diagnose this too.
208 HoldsPointer
ret_via_member_3() {
210 const HoldsPointer hp
= HoldsPointer
{&n
};
217 int &r(X
*p
) { return p
->*f(); }
223 A
&operator+=(int i
);
225 A
const &b
= A(5) += 5; // expected-warning {{temporary bound to local reference 'b' will be destroyed at the end of the full-expression}}