comparison gcc/testsuite/gcc.dg/Wreturn-local-addr-3.c @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents
children
comparison
equal deleted inserted replaced
131:84e7813d76e9 145:1830386684a0
1 /* PR c/71924 - missing -Wreturn-local-addr returning alloca result
2 { dg-do compile }
3 { dg-options "-O2 -Wall" } */
4
5 #define ATTR(...) __attribute__ ((__VA_ARGS__))
6
7 typedef __INTPTR_TYPE__ intptr_t;
8
9 struct A { int a, b, c; };
10 struct B { int a, b, c[]; };
11
12 extern int g1[5], g2[5], g3[5], g4[5], g5[5];
13
14 void sink (void*, ...);
15
16 /* Verify that a pointer difference expression is handled correctly
17 even when converted to a pointer. */
18
19 ATTR (noipa) void*
20 return_local_diff_cst (void)
21 {
22 int a[5];
23 void *p = (void*)(&a[4] - &a[1]);
24 return p;
25 }
26
27 ATTR (noipa) void*
28 return_local_diff_var (int i, int j)
29 {
30 int a[5];
31 void *p = (void*)(&a[j] - &a[i]);
32 return p;
33 }
34
35 ATTR (noipa) void*
36 return_2_locals (int i)
37 {
38 int a[1]; /* { dg-message "declared here" } */
39 int b[2]; /* { dg-message "declared here" } */
40 void *p = i < 0 ? a : b;
41 return p; /* { dg-warning "function returns address of local" } */
42 }
43
44 /* Verify that returning the address of a local converted to intptr_t
45 is not diagnosed (see bug 90737 for a case the front-end gets wrong). */
46
47 ATTR (noipa) intptr_t
48 return_int_2_locals (int i)
49 {
50 int a[1];
51 int b[2];
52 void *p = i < 0 ? a : b;
53 return (intptr_t)p;
54 }
55
56 /* Verify that a conditional expression with a pointer first operand
57 is handled correctly. */
58
59 ATTR (noipa) void*
60 return_2_locals_ptrcond (void *q)
61 {
62 int a[1]; /* { dg-message "declared here" } */
63 int b[2]; /* { dg-message "declared here" } */
64 void *p = q ? a : b;
65 return p; /* { dg-warning "function returns address of local" } */
66 }
67
68 /* Verify that a preincrement expression with a pointer operand is
69 handled correctly. */
70
71 ATTR (noipa) void*
72 return_2_locals_ptrinc (void *q)
73 {
74 int a[1]; /* { dg-message "declared here" } */
75 int b[2]; /* { dg-message "declared here" } */
76 int *p = q ? a : b;
77 return ++p; /* { dg-warning "function returns address of local" } */
78 }
79
80 ATTR (noipa) void*
81 return_3_locals (int i)
82 {
83 int a[1]; /* { dg-message "declared here" } */
84 int b[2]; /* { dg-message "declared here" } */
85 int c[3]; /* { dg-message "declared here" } */
86
87 void *p = i < 0 ? a : 0 < i ? c : b;
88 return p; /* { dg-warning "function returns address of local" } */
89 }
90
91 /* Verify that a conditional expression with a pointer first operand
92 is handled correctly. */
93
94 ATTR (noipa) void*
95 return_3_locals_ptrcond (void *p, void *q)
96 {
97 int a[1]; /* { dg-message "declared here" } */
98 int b[2]; /* { dg-message "declared here" } */
99 int c[3]; /* { dg-message "declared here" } */
100
101 void *r = q ? r ? a : b : c;
102 return r; /* { dg-warning "function returns address of local" } */
103 }
104
105 ATTR (noipa) void*
106 return_5_locals (int i)
107 {
108 int a[1]; /* { dg-message "declared here" } */
109 int b[2]; /* { dg-message "declared here" } */
110 int c[3]; /* { dg-message "declared here" } */
111 int d[4]; /* { dg-message "declared here" } */
112 int e[5]; /* { dg-message "declared here" } */
113
114 void *p = i < -1 ? a : i < 0 ? b : 1 < i ? e : 0 < i ? d : c;
115 return p; /* { dg-warning "function returns address of local" } */
116 }
117
118 ATTR (noipa) void*
119 return_1_global_4_locals (int i)
120 {
121 int a[1]; /* { dg-message "declared here" } */
122 int b[2]; /* { dg-message "declared here" } */
123 int c[3]; /* { dg-message "declared here" } */
124 int d[4]; /* { dg-message "declared here" } */
125
126 void *p = i < -1 ? a : i < 0 ? b : 1 < i ? g1 : 0 < i ? d : c;
127 return p; /* { dg-warning "function may return address of local" } */
128 }
129
130 ATTR (noipa) void*
131 return_2_globals_3_locals (int i)
132 {
133 int a[1]; /* { dg-message "declared here" } */
134 int b[2]; /* { dg-message "declared here" } */
135 int c[3]; /* { dg-message "declared here" } */
136
137 void *p = i < -1 ? a : i < 0 ? b : 1 < i ? g1 : 0 < i ? g2 : c;
138 return p; /* { dg-warning "function may return address of local" } */
139 }
140
141 ATTR (noipa) void*
142 return_3_globals_2_locals (int i)
143 {
144 int a[1]; /* { dg-message "declared here" } */
145 int b[2]; /* { dg-message "declared here" } */
146
147 void *p = i < -1 ? a : i < 0 ? b : 1 < i ? g1 : 0 < i ? g2 : g3;
148 return p; /* { dg-warning "function may return address of local" } */
149 }
150
151 ATTR (noipa) void*
152 return_4_globals_1_local (int i)
153 {
154 int a[1]; /* { dg-message "declared here" } */
155
156 void *p = i < -1 ? a : i < 0 ? g1 : 1 < i ? g2 : 0 < i ? g4 : g3;
157 return p; /* { dg-warning "function may return address of local" } */
158 }
159
160 ATTR (noipa) void*
161 return_all_globals (int i)
162 {
163 void *p = i < -1 ? g1 : i < 0 ? g2 : 1 < i ? g3 : 0 < i ? g5 : g4;
164 return p;
165 }
166
167
168 ATTR (noipa) void*
169 return_2_alloca_local_cstoff (int n, int i)
170 {
171 int *a = __builtin_alloca (n); /* { dg-message "declared here" } */
172 int *b = __builtin_alloca (n); /* { dg-message "declared here" } */
173 int *p = i < 0 ? a : b;
174 p += 1;
175 sink (p);
176 return p; /* { dg-warning "function returns address of local" } */
177 }
178
179 ATTR (noipa) void*
180 return_alloca_local_cstoff (int n, int i)
181 {
182 int *a = __builtin_alloca (n); /* { dg-message "declared here" } */
183 int b[2]; /* { dg-message "declared here" } */
184 int *p = i < 0 ? a : b;
185 p += 1;
186 sink (p);
187 return p; /* { dg-warning "function returns address of local" } */
188 }
189
190 ATTR (noipa) void*
191 return_local_alloca_cstoff (int n, int i)
192 {
193 int a[2]; /* { dg-message "declared here" } */
194 int *b = __builtin_alloca (n); /* { dg-message "declared here" } */
195 int *p = i < 0 ? a : b;
196 p += 1;
197 sink (p);
198 return p; /* { dg-warning "function returns address of local" } */
199 }
200
201 ATTR (noipa) void*
202 return_2_locals_cstoff (int i)
203 {
204 int a[1]; /* { dg-message "declared here" } */
205 int b[2]; /* { dg-message "declared here" } */
206 int *p = i < 0 ? a : b;
207 p += 1;
208 sink (p);
209 return p; /* { dg-warning "function returns address of local" } */
210 }
211
212 ATTR (noipa) void*
213 return_2_globals_3_locals_cstoff (int i)
214 {
215 int a[1]; /* { dg-message "declared here" } */
216 int b[2]; /* { dg-message "declared here" } */
217 int c[3]; /* { dg-message "declared here" } */
218
219 int *p = i < -1 ? a : i < 0 ? b : 1 < i ? g1 : 0 < i ? g2 : c;
220 p += 1;
221 sink (p);
222 return p; /* { dg-warning "function may return address of local" } */
223 }
224
225 ATTR (noipa) void*
226 return_3_globals_alloca_local_varoff (int n, int i, int j)
227 {
228 int *a = __builtin_alloca (n); /* { dg-message "declared here" } */
229 int b[2]; /* { dg-message "declared here" } */
230
231 int *p = i < -1 ? a : i < 0 ? b : 1 < i ? g1 : 0 < i ? g2 : g3;
232 p += j;
233 sink (p);
234 return p; /* { dg-warning "function may return address of local" } */
235 }
236
237 ATTR (noipa) void*
238 return_3_globals_2_locals_varoff (int i, int j)
239 {
240 int a[1]; /* { dg-message "declared here" } */
241 int b[2]; /* { dg-message "declared here" } */
242
243 int *p = i < -1 ? a : i < 0 ? b : 1 < i ? g1 : 0 < i ? g2 : g3;
244 p += j;
245 sink (p);
246 return p; /* { dg-warning "function may return address of local" } */
247 }
248