blob: fbbaf836f1abf6fb40ca0125f7e54ceb9d0fec27 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Ted Kremenek4774b4d2007-08-17 22:17:23 +00002
Ted Kremenek4774b4d2007-08-17 22:17:23 +00003int* ret_local() {
4 int x = 1;
5 return &x; // expected-warning {{address of stack memory}}
6}
7
8int* ret_local_array() {
9 int x[10];
10 return x; // expected-warning {{address of stack memory}}
11}
12
13int* ret_local_array_element(int i) {
14 int x[10];
15 return &x[i]; // expected-warning {{address of stack memory}}
16}
17
18int *ret_local_array_element_reversed(int i) {
19 int x[10];
20 return &i[x]; // expected-warning {{address of stack memory}}
21}
22
23int* ret_local_array_element_const_index() {
24 int x[10];
25 return &x[2]; // expected-warning {{address of stack memory}}
26}
27
28int& ret_local_ref() {
29 int x = 1;
30 return x; // expected-warning {{reference to stack memory}}
31}
32
33int* ret_local_addrOf() {
34 int x = 1;
35 return &*&x; // expected-warning {{address of stack memory}}
36}
37
38int* ret_local_addrOf_paren() {
39 int x = 1;
40 return (&(*(&x))); // expected-warning {{address of stack memory}}
41}
42
43int* ret_local_addrOf_ptr_arith() {
44 int x = 1;
45 return &*(&x+1); // expected-warning {{address of stack memory}}
46}
47
48int* ret_local_addrOf_ptr_arith2() {
49 int x = 1;
50 return &*(&x+1); // expected-warning {{address of stack memory}}
51}
52
53int* ret_local_field() {
54 struct { int x; } a;
55 return &a.x; // expected-warning {{address of stack memory}}
56}
57
58int& ret_local_field_ref() {
59 struct { int x; } a;
60 return a.x; // expected-warning {{reference to stack memory}}
61}
62
63int* 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
69int* ret_conditional_rhs(int *x, bool cond) {
70 int y = 1;
71 return cond ? x : &y; // expected-warning {{address of stack memory}}
72}
73
74void* ret_c_cast() {
75 int x = 1;
76 return (void*) &x; // expected-warning {{address of stack memory}}
77}
78
79int* ret_static_var() {
80 static int x = 1;
81 return &x; // no warning.
82}
83
84int z = 1;
85
86int* ret_global() {
87 return &z; // no warning.
Ted Kremenekf8f95172007-08-20 16:28:05 +000088}
89
90int* ret_parameter(int x) {
91 return &x; // expected-warning {{address of stack memory}}
92}
93
94
Sebastian Redl8b3f6b52008-10-27 16:34:21 +000095void* ret_cpp_static_cast(short x) {
96 return static_cast<void*>(&x); // expected-warning {{address of stack memory}}
Ted Kremenekf8f95172007-08-20 16:28:05 +000097}
98
99int* ret_cpp_reinterpret_cast(double x) {
100 return reinterpret_cast<int*>(&x); // expected-warning {{address of stack me}}
101}
102
Sebastian Redl8b3f6b52008-10-27 16:34:21 +0000103int* ret_cpp_reinterpret_cast_no_warning(long x) {
Ted Kremenekf8f95172007-08-20 16:28:05 +0000104 return reinterpret_cast<int*>(x); // no-warning
105}
106
Chris Lattner868d08f2008-01-31 06:06:29 +0000107int* ret_cpp_const_cast(const int x) {
Ted Kremenekf8f95172007-08-20 16:28:05 +0000108 return const_cast<int*>(&x); // expected-warning {{address of stack memory}}
109}
110
Ted Kremeneka423e812010-09-02 01:12:13 +0000111// PR 7999 - handle the case where a field is itself a reference.
112template <typename T> struct PR7999 {
113 PR7999(T& t) : value(t) {}
114 T& value;
115};
116
117struct PR7999_X {};
118
119PR7999_X& PR7999_f(PR7999<PR7999_X> s) { return s.value; } // no-warning
120void test_PR7999(PR7999_X& x) { (void)PR7999_f(x); } // no-warning
121
Chandler Carruthe7f85042010-12-13 07:40:47 +0000122// PR 8774: Don't try to evaluate parameters with default arguments like
123// variables with an initializer, especially in templates where the default
124// argument may not be an expression (yet).
125namespace PR8774 {
Chandler Carruthe7f85042010-12-13 07:40:47 +0000126 template <typename U> struct B { };
127 template <typename V> V f(typename B<V>::type const &v = B<V>::value()) {
128 return v;
129 }
130 template <> struct B<const char *> {
131 typedef const char *type;
132 static const char *value();
133 };
134 void g() {
135 const char *t;
136 f<const char*>(t);
137 }
138}
139
Ted Kremenekf8f95172007-08-20 16:28:05 +0000140// TODO: test case for dynamic_cast. clang does not yet have
Ted Kremenek9f3d9422007-09-26 20:14:22 +0000141// support for C++ classes to write such a test case.