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