227 lines · cpp
1// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify=expected,cxx23 %s2// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=expected,cxx11_20 %s3// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify=expected,cxx11_20,cxx11 %s4 5int* ret_local() {6 int x = 1;7 return &x; // expected-warning {{address of stack memory}}8}9 10int* ret_local_array() {11 int x[10];12 return x; // expected-warning {{address of stack memory}}13}14 15int* ret_local_array_element(int i) {16 int x[10];17 return &x[i]; // expected-warning {{address of stack memory}}18}19 20int *ret_local_array_element_reversed(int i) {21 int x[10];22 return &i[x]; // expected-warning {{address of stack memory}}23}24 25int* ret_local_array_element_const_index() {26 int x[10];27 return &x[2]; // expected-warning {{address of stack memory}}28}29 30int& ret_local_ref() {31 int x = 1;32 return x; // cxx11_20-warning {{reference to stack memory}}33 // cxx23-error@-1 {{non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'}}34}35 36int* ret_local_addrOf() {37 int x = 1;38 return &*&x; // expected-warning {{address of stack memory}}39}40 41int* ret_local_addrOf_paren() {42 int x = 1;43 return (&(*(&x))); // expected-warning {{address of stack memory}}44}45 46int* ret_local_addrOf_ptr_arith() {47 int x = 1;48 return &*(&x+1); // expected-warning {{address of stack memory}}49}50 51int* ret_local_addrOf_ptr_arith2() {52 int x = 1;53 return &*(&x+1); // expected-warning {{address of stack memory}}54}55 56int* ret_local_field() {57 struct { int x; } a;58 return &a.x; // expected-warning {{address of stack memory}}59}60 61int& ret_local_field_ref() {62 struct { int x; } a;63 return a.x; // expected-warning {{reference to stack memory}}64}65 66int* 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 73int* ret_conditional_rhs(int *x, bool cond) {74 int y = 1;75 return cond ? x : &y; // expected-warning {{address of stack memory}}76}77 78void* ret_c_cast() {79 int x = 1;80 return (void*) &x; // expected-warning {{address of stack memory}}81}82 83int* ret_static_var() {84 static int x = 1;85 return &x; // no warning.86}87 88int z = 1;89 90int* ret_global() {91 return &z; // no warning.92}93 94int* ret_parameter(int x) {95 return &x; // expected-warning {{address of stack memory}}96}97 98 99void* ret_cpp_static_cast(short x) {100 return static_cast<void*>(&x); // expected-warning {{address of stack memory}}101}102 103int* ret_cpp_reinterpret_cast(double x) {104 return reinterpret_cast<int*>(&x); // expected-warning {{address of stack me}}105}106 107int* ret_cpp_reinterpret_cast_no_warning(long x) {108 return reinterpret_cast<int*>(x); // no-warning109}110 111int* ret_cpp_const_cast(const int x) {112 return const_cast<int*>(&x); // expected-warning {{address of stack memory}}113}114 115struct A { virtual ~A(); }; struct B : A {};116A* 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.121template <typename T> struct PR7999 {122 PR7999(T& t) : value(t) {}123 T& value;124};125 126struct PR7999_X {};127 128PR7999_X& PR7999_f(PR7999<PR7999_X> s) { return s.value; } // no-warning129void test_PR7999(PR7999_X& x) { (void)PR7999_f(x); } // no-warning130 131// PR 8774: Don't try to evaluate parameters with default arguments like132// variables with an initializer, especially in templates where the default133// argument may not be an expression (yet).134namespace 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;138 }139 template <> struct B<const char *> {140 typedef const char *type;141 static const char *value();142 };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 if150// we're within a lambda-expression.151void 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 // cxx23-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 // cxx23-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 197struct HoldsPointer { int *p; };198 199HoldsPointer ret_via_member_1() {200 int n;201 return {&n}; // expected-warning {{address of stack memory associated with local variable 'n' returned}}202}203HoldsPointer 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.208HoldsPointer ret_via_member_3() {209 int n;210 const HoldsPointer hp = HoldsPointer{&n};211 return hp;212}213 214namespace mem_ptr {215 struct X {};216 int X::*f();217 int &r(X *p) { return p->*f(); }218}219 220namespace 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