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
4 
ret_local()5 int* ret_local() {
6   int x = 1;
7   return &x; // expected-warning {{address of stack memory}}
8 }
9 
ret_local_array()10 int* ret_local_array() {
11   int x[10];
12   return x; // expected-warning {{address of stack memory}}
13 }
14 
ret_local_array_element(int i)15 int* ret_local_array_element(int i) {
16   int x[10];
17   return &x[i]; // expected-warning {{address of stack memory}}
18 }
19 
ret_local_array_element_reversed(int i)20 int *ret_local_array_element_reversed(int i) {
21   int x[10];
22   return &i[x]; // expected-warning {{address of stack memory}}
23 }
24 
ret_local_array_element_const_index()25 int* ret_local_array_element_const_index() {
26   int x[10];
27   return &x[2];  // expected-warning {{address of stack memory}}
28 }
29 
ret_local_ref()30 int& ret_local_ref() {
31   int x = 1;
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'}}
34 }
35 
ret_local_addrOf()36 int* ret_local_addrOf() {
37   int x = 1;
38   return &*&x; // expected-warning {{address of stack memory}}
39 }
40 
ret_local_addrOf_paren()41 int* ret_local_addrOf_paren() {
42   int x = 1;
43   return (&(*(&x))); // expected-warning {{address of stack memory}}
44 }
45 
ret_local_addrOf_ptr_arith()46 int* ret_local_addrOf_ptr_arith() {
47   int x = 1;
48   return &*(&x+1); // expected-warning {{address of stack memory}}
49 }
50 
ret_local_addrOf_ptr_arith2()51 int* ret_local_addrOf_ptr_arith2() {
52   int x = 1;
53   return &*(&x+1); // expected-warning {{address of stack memory}}
54 }
55 
ret_local_field()56 int* ret_local_field() {
57   struct { int x; } a;
58   return &a.x; // expected-warning {{address of stack memory}}
59 }
60 
ret_local_field_ref()61 int& ret_local_field_ref() {
62   struct { int x; } a;
63   return a.x; // expected-warning {{reference to stack memory}}
64 }
65 
ret_conditional(bool cond)66 int* ret_conditional(bool cond) {
67   int x = 1;
68   int y = 2;
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}}
71 }
72 
ret_conditional_rhs(int * x,bool cond)73 int* ret_conditional_rhs(int *x, bool cond) {
74   int y = 1;
75   return cond ? x : &y;  // expected-warning {{address of stack memory}}
76 }
77 
ret_c_cast()78 void* ret_c_cast() {
79   int x = 1;
80   return (void*) &x;  // expected-warning {{address of stack memory}}
81 }
82 
ret_static_var()83 int* ret_static_var() {
84   static int x = 1;
85   return &x;  // no warning.
86 }
87 
88 int z = 1;
89 
ret_global()90 int* ret_global() {
91   return &z;  // no warning.
92 }
93 
ret_parameter(int x)94 int* ret_parameter(int x) {
95   return &x;  // expected-warning {{address of stack memory}}
96 }
97 
98 
ret_cpp_static_cast(short x)99 void* ret_cpp_static_cast(short x) {
100   return static_cast<void*>(&x); // expected-warning {{address of stack memory}}
101 }
102 
ret_cpp_reinterpret_cast(double x)103 int* ret_cpp_reinterpret_cast(double x) {
104   return reinterpret_cast<int*>(&x); // expected-warning {{address of stack me}}
105 }
106 
ret_cpp_reinterpret_cast_no_warning(long x)107 int* ret_cpp_reinterpret_cast_no_warning(long x) {
108   return reinterpret_cast<int*>(x); // no-warning
109 }
110 
ret_cpp_const_cast(const int x)111 int* ret_cpp_const_cast(const int x) {
112   return const_cast<int*>(&x);  // expected-warning {{address of stack memory}}
113 }
114 
115 struct A { virtual ~A(); }; struct B : A {};
ret_cpp_dynamic_cast(B b)116 A* ret_cpp_dynamic_cast(B b) {
117   return dynamic_cast<A*>(&b); // expected-warning {{address of stack memory}}
118 }
119 
120 // PR 7999 - handle the case where a field is itself a reference.
121 template <typename T> struct PR7999 {
PR7999PR7999122   PR7999(T& t) : value(t) {}
123   T& value;
124 };
125 
126 struct PR7999_X {};
127 
PR7999_f(PR7999<PR7999_X> s)128 PR7999_X& PR7999_f(PR7999<PR7999_X> s) { return s.value; } // no-warning
test_PR7999(PR7999_X & x)129 void test_PR7999(PR7999_X& x) { (void)PR7999_f(x); } // no-warning
130 
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).
134 namespace PR8774 {
135   template <typename U> struct B { };
f(typename B<V>::type const & v=B<V>::value ())136   template <typename V> V f(typename B<V>::type const &v = B<V>::value()) {
137     return v;
138   }
139   template <> struct B<const char *> {
140     typedef const char *type;
141     static const char *value();
142   };
g()143   void g() {
144     const char *t;
145     f<const char*>(t);
146   }
147 }
148 
149 // Don't warn about returning a local variable from a surrounding function if
150 // we're within a lambda-expression.
ret_from_lambda()151 void ret_from_lambda() {
152   int a;
153   int &b = a;
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; };
164 
165   (void) [] {
166     int 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}}
169     };
170   };
171   (void) [] {
172     int a;
173     return [&a] {}; // expected-warning {{address of stack memory associated with local variable 'a' returned}} expected-note {{captured by reference here}}
174   };
175   (void) [] {
176     int a;
177     return [=] {
178       return a;
179     };
180   };
181   (void) [] {
182     int a;
183     return [a] {};
184   };
185   (void) [] {
186     int a;
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'}}
189   };
190   (void) [] {
191     int a;
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'}}
194   };
195 }
196 
197 struct HoldsPointer { int *p; };
198 
ret_via_member_1()199 HoldsPointer ret_via_member_1() {
200   int n;
201   return {&n}; // expected-warning {{address of stack memory associated with local variable 'n' returned}}
202 }
ret_via_member_2()203 HoldsPointer ret_via_member_2() {
204   int n;
205   return HoldsPointer(HoldsPointer{&n}); // cxx11-warning {{address of stack memory associated with local variable 'n' returned}}
206 }
207 // FIXME: We could diagnose this too.
ret_via_member_3()208 HoldsPointer ret_via_member_3() {
209   int n;
210   const HoldsPointer hp = HoldsPointer{&n};
211   return hp;
212 }
213 
214 namespace mem_ptr {
215   struct X {};
216   int X::*f();
r(X * p)217   int &r(X *p) { return p->*f(); }
218 }
219 
220 namespace PR47861 {
221   struct A {
222     A(int i);
223     A &operator+=(int i);
224   };
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}}
226 }
227