1 // RUN: clang-cc -fsyntax-only -verify %s 2 3 int* ret_local() { 4 int x = 1; 5 return &x; // expected-warning {{address of stack memory}} 6 } 7 8 int* ret_local_array() { 9 int x[10]; 10 return x; // expected-warning {{address of stack memory}} 11 } 12 13 int* ret_local_array_element(int i) { 14 int x[10]; 15 return &x[i]; // expected-warning {{address of stack memory}} 16 } 17 18 int *ret_local_array_element_reversed(int i) { 19 int x[10]; 20 return &i[x]; // expected-warning {{address of stack memory}} 21 } 22 23 int* ret_local_array_element_const_index() { 24 int x[10]; 25 return &x[2]; // expected-warning {{address of stack memory}} 26 } 27 28 int& ret_local_ref() { 29 int x = 1; 30 return x; // expected-warning {{reference to stack memory}} 31 } 32 33 int* ret_local_addrOf() { 34 int x = 1; 35 return &*&x; // expected-warning {{address of stack memory}} 36 } 37 38 int* ret_local_addrOf_paren() { 39 int x = 1; 40 return (&(*(&x))); // expected-warning {{address of stack memory}} 41 } 42 43 int* ret_local_addrOf_ptr_arith() { 44 int x = 1; 45 return &*(&x+1); // expected-warning {{address of stack memory}} 46 } 47 48 int* ret_local_addrOf_ptr_arith2() { 49 int x = 1; 50 return &*(&x+1); // expected-warning {{address of stack memory}} 51 } 52 53 int* ret_local_field() { 54 struct { int x; } a; 55 return &a.x; // expected-warning {{address of stack memory}} 56 } 57 58 int& ret_local_field_ref() { 59 struct { int x; } a; 60 return a.x; // expected-warning {{reference to stack memory}} 61 } 62 63 int* ret_conditional(bool cond) { 64 int x = 1; 65 int y = 2; 66 return cond ? &x : &y; // expected-warning {{address of stack memory}} 67 } 68 69 int* ret_conditional_rhs(int *x, bool cond) { 70 int y = 1; 71 return cond ? x : &y; // expected-warning {{address of stack memory}} 72 } 73 74 void* ret_c_cast() { 75 int x = 1; 76 return (void*) &x; // expected-warning {{address of stack memory}} 77 } 78 79 int* ret_static_var() { 80 static int x = 1; 81 return &x; // no warning. 82 } 83 84 int z = 1; 85 86 int* ret_global() { 87 return &z; // no warning. 88 } 89 90 int* ret_parameter(int x) { 91 return &x; // expected-warning {{address of stack memory}} 92 } 93 94 95 void* ret_cpp_static_cast(short x) { 96 return static_cast<void*>(&x); // expected-warning {{address of stack memory}} 97 } 98 99 int* ret_cpp_reinterpret_cast(double x) { 100 return reinterpret_cast<int*>(&x); // expected-warning {{address of stack me}} 101 } 102 103 int* ret_cpp_reinterpret_cast_no_warning(long x) { 104 return reinterpret_cast<int*>(x); // no-warning 105 } 106 107 int* ret_cpp_const_cast(const int x) { 108 return const_cast<int*>(&x); // expected-warning {{address of stack memory}} 109 } 110 111 // TODO: test case for dynamic_cast. clang does not yet have 112 // support for C++ classes to write such a test case. 113