[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / return-stack-addr.cpp
blob2f3b5b5d6c66000ff9c4a424e36ab48d42c04ba7
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
5 int* ret_local() {
6 int x = 1;
7 return &x; // expected-warning {{address of stack memory}}
10 int* ret_local_array() {
11 int x[10];
12 return x; // expected-warning {{address of stack memory}}
15 int* ret_local_array_element(int i) {
16 int x[10];
17 return &x[i]; // expected-warning {{address of stack memory}}
20 int *ret_local_array_element_reversed(int i) {
21 int x[10];
22 return &i[x]; // expected-warning {{address of stack memory}}
25 int* ret_local_array_element_const_index() {
26 int x[10];
27 return &x[2]; // expected-warning {{address of stack memory}}
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'}}
36 int* ret_local_addrOf() {
37 int x = 1;
38 return &*&x; // expected-warning {{address of stack memory}}
41 int* ret_local_addrOf_paren() {
42 int x = 1;
43 return (&(*(&x))); // expected-warning {{address of stack memory}}
46 int* ret_local_addrOf_ptr_arith() {
47 int x = 1;
48 return &*(&x+1); // expected-warning {{address of stack memory}}
51 int* ret_local_addrOf_ptr_arith2() {
52 int x = 1;
53 return &*(&x+1); // expected-warning {{address of stack memory}}
56 int* ret_local_field() {
57 struct { int x; } a;
58 return &a.x; // expected-warning {{address of stack memory}}
61 int& ret_local_field_ref() {
62 struct { int x; } a;
63 return a.x; // expected-warning {{reference to stack memory}}
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}}
73 int* ret_conditional_rhs(int *x, bool cond) {
74 int y = 1;
75 return cond ? x : &y; // expected-warning {{address of stack memory}}
78 void* ret_c_cast() {
79 int x = 1;
80 return (void*) &x; // expected-warning {{address of stack memory}}
83 int* ret_static_var() {
84 static int x = 1;
85 return &x; // no warning.
88 int z = 1;
90 int* ret_global() {
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) {}
123 T& value;
126 struct PR7999_X {};
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).
134 namespace PR8774 {
135 template <typename U> struct B { };
136 template <typename V> V f(typename B<V>::type const &v = B<V>::value()) {
137 return v;
139 template <> struct B<const char *> {
140 typedef const char *type;
141 static const char *value();
143 void g() {
144 const char *t;
145 f<const char*>(t);
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() {
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; };
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}}
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}}
175 (void) [] {
176 int a;
177 return [=] {
178 return a;
181 (void) [] {
182 int a;
183 return [a] {};
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'}}
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'}}
197 struct HoldsPointer { int *p; };
199 HoldsPointer ret_via_member_1() {
200 int n;
201 return {&n}; // expected-warning {{address of stack memory associated with local variable 'n' returned}}
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}}
207 // FIXME: We could diagnose this too.
208 HoldsPointer ret_via_member_3() {
209 int n;
210 const HoldsPointer hp = HoldsPointer{&n};
211 return hp;
214 namespace mem_ptr {
215 struct X {};
216 int X::*f();
217 int &r(X *p) { return p->*f(); }
220 namespace PR47861 {
221 struct A {
222 A(int i);
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}}