| 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 |
struct A { int a, b, c; }; |
| 8 |
struct B { int a, b, c[]; }; |
| 9 |
|
| 10 |
extern int g1[5], g2[5], g3[5], g4[5], g5[5]; |
| 11 |
|
| 12 |
void sink (void*, ...); |
| 13 |
|
| 14 |
ATTR (noipa) void* |
| 15 |
return_2_locals (int i) |
| 16 |
{ |
| 17 |
int a[1]; /* { dg-message "declared here" } */ |
| 18 |
int b[2]; /* { dg-message "declared here" } */ |
| 19 |
void *p = b; |
| 20 |
if (i < 0) |
| 21 |
p = a; |
| 22 |
|
| 23 |
sink (p); |
| 24 |
|
| 25 |
return p; /* { dg-warning "function returns address of local" } */ |
| 26 |
} |
| 27 |
|
| 28 |
ATTR (noipa) void* |
| 29 |
return_2_locals_after_2_globals (int i, int j) |
| 30 |
{ |
| 31 |
int a[1]; /* { dg-message "declared here" } */ |
| 32 |
int b[2]; /* { dg-message "declared here" } */ |
| 33 |
|
| 34 |
int *p; |
| 35 |
if (i < 0) |
| 36 |
p = g1; |
| 37 |
else |
| 38 |
p = g2; |
| 39 |
|
| 40 |
sink (p); |
| 41 |
|
| 42 |
if (j < 0) |
| 43 |
p = a; |
| 44 |
else |
| 45 |
p = b; |
| 46 |
|
| 47 |
sink (p); |
| 48 |
|
| 49 |
return p; /* { dg-warning "function returns address of local" } */ |
| 50 |
} |
| 51 |
|
| 52 |
ATTR (noipa) void* |
| 53 |
return_3_locals (int i) |
| 54 |
{ |
| 55 |
int a[1]; /* { dg-message "declared here" } */ |
| 56 |
int b[2]; /* { dg-message "declared here" } */ |
| 57 |
int c[3]; /* { dg-message "declared here" } */ |
| 58 |
|
| 59 |
void *p = b + 1; |
| 60 |
if (i < 0) |
| 61 |
p = a; |
| 62 |
else if (0 < i) |
| 63 |
p = c + 2; |
| 64 |
|
| 65 |
sink (p); |
| 66 |
|
| 67 |
return p; /* { dg-warning "function returns address of local" } */ |
| 68 |
} |
| 69 |
|
| 70 |
ATTR (noipa) void* |
| 71 |
return_5_locals (int i) |
| 72 |
{ |
| 73 |
int a[1]; /* { dg-message "declared here" } */ |
| 74 |
int b[2]; /* { dg-message "declared here" } */ |
| 75 |
int c[3]; /* { dg-message "declared here" } */ |
| 76 |
int d[4]; /* { dg-message "declared here" } */ |
| 77 |
int e[5]; /* { dg-message "declared here" } */ |
| 78 |
|
| 79 |
void *p = &c[2]; |
| 80 |
if (i < -1) |
| 81 |
p = a; |
| 82 |
else if (i < 0) |
| 83 |
p = &b[1]; |
| 84 |
else if (1 < i) |
| 85 |
p = &e[4]; |
| 86 |
else if (0 < i) |
| 87 |
p = &d[3]; |
| 88 |
|
| 89 |
sink (p); |
| 90 |
|
| 91 |
return p; /* { dg-warning "function returns address of local" } */ |
| 92 |
} |
| 93 |
|
| 94 |
ATTR (noipa) void* |
| 95 |
return_5_locals_switch (int i) |
| 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 |
int d[4]; /* { dg-message "declared here" } */ |
| 101 |
int e[5]; /* { dg-message "declared here" } */ |
| 102 |
|
| 103 |
void *p = 0; |
| 104 |
|
| 105 |
switch (i) |
| 106 |
{ |
| 107 |
case 0: p = &a[1]; break; |
| 108 |
case 1: p = &b[2]; break; |
| 109 |
case 2: p = &c[3]; break; |
| 110 |
case 3: p = &d[4]; break; |
| 111 |
default: p = &e[5]; break; |
| 112 |
} |
| 113 |
|
| 114 |
sink (p); |
| 115 |
|
| 116 |
return p; /* { dg-warning "function returns address of local" } */ |
| 117 |
} |
| 118 |
|
| 119 |
ATTR (noipa) void* |
| 120 |
return_1_global_4_locals (int i) |
| 121 |
{ |
| 122 |
int a[1]; /* { dg-message "declared here" } */ |
| 123 |
int b[2]; /* { dg-message "declared here" } */ |
| 124 |
int c[3]; /* { dg-message "declared here" } */ |
| 125 |
int d[4]; /* { dg-message "declared here" } */ |
| 126 |
|
| 127 |
void *p = c; |
| 128 |
if (i < -1) |
| 129 |
sink (p = a); |
| 130 |
else if (i < 0) |
| 131 |
sink (p = b); |
| 132 |
else if (1 < i) |
| 133 |
sink (p = g1); |
| 134 |
else if (0 < i) |
| 135 |
sink (p = d); |
| 136 |
|
| 137 |
sink (p, a, b, c, d); |
| 138 |
|
| 139 |
return p; /* { dg-warning "function may return address of local" } */ |
| 140 |
} |
| 141 |
|
| 142 |
ATTR (noipa) void* |
| 143 |
return_1_global_4_locals_switch (int i) |
| 144 |
{ |
| 145 |
int a[1]; /* { dg-message "declared here" } */ |
| 146 |
int b[2]; /* { dg-message "declared here" } */ |
| 147 |
int c[3]; /* { dg-message "declared here" } */ |
| 148 |
int d[4]; /* { dg-message "declared here" } */ |
| 149 |
|
| 150 |
void *p = 0; |
| 151 |
|
| 152 |
switch (i) |
| 153 |
{ |
| 154 |
case 0: p = &a[0]; break; |
| 155 |
case 1: p = &b[1]; break; |
| 156 |
case 2: p = &c[2]; break; |
| 157 |
case 3: p = &d[3]; break; |
| 158 |
} |
| 159 |
|
| 160 |
sink (p); |
| 161 |
|
| 162 |
return p; /* { dg-warning "function may return address of local" } */ |
| 163 |
} |
| 164 |
|
| 165 |
ATTR (noipa) void* |
| 166 |
return_2_globals_3_locals (int i) |
| 167 |
{ |
| 168 |
int a[1]; /* { dg-message "declared here" } */ |
| 169 |
int b[2]; /* { dg-message "declared here" } */ |
| 170 |
int c[3]; /* { dg-message "declared here" } */ |
| 171 |
|
| 172 |
void *p = c; |
| 173 |
if (i < -1) |
| 174 |
p = a; |
| 175 |
else if (i < 0) |
| 176 |
p = b; |
| 177 |
else if (1 < i) |
| 178 |
p = g1; |
| 179 |
else if (0 < i) |
| 180 |
p = g2; |
| 181 |
|
| 182 |
sink (p); |
| 183 |
|
| 184 |
return p; /* { dg-warning "function may return address of local" } */ |
| 185 |
} |
| 186 |
|
| 187 |
ATTR (noipa) void* |
| 188 |
return_3_globals_2_locals (int i) |
| 189 |
{ |
| 190 |
int a[1]; /* { dg-message "declared here" } */ |
| 191 |
int b[2]; /* { dg-message "declared here" } */ |
| 192 |
|
| 193 |
void *p = g3; |
| 194 |
if (i < -1) |
| 195 |
p = a; |
| 196 |
else if (i < 0) |
| 197 |
p = b; |
| 198 |
else if (1 < i) |
| 199 |
p = g1; |
| 200 |
else if (0 < i) |
| 201 |
p = g2; |
| 202 |
|
| 203 |
sink (p); |
| 204 |
|
| 205 |
return p; /* { dg-warning "function may return address of local" } */ |
| 206 |
} |
| 207 |
|
| 208 |
ATTR (noipa) void* |
| 209 |
return_4_globals_1_local (int i) |
| 210 |
{ |
| 211 |
int a[1]; /* { dg-message "declared here" } */ |
| 212 |
|
| 213 |
void *p = g3; |
| 214 |
if (i < -1) |
| 215 |
p = a; |
| 216 |
else if (i < 0) |
| 217 |
p = g1; |
| 218 |
else if (1 < i) |
| 219 |
p = g2; |
| 220 |
else if (0 < i) |
| 221 |
p = g4; |
| 222 |
|
| 223 |
sink (p); |
| 224 |
|
| 225 |
return p; /* { dg-warning "function may return address of local" } */ |
| 226 |
} |
| 227 |
|
| 228 |
ATTR (noipa) void* |
| 229 |
return_all_globals (int i) |
| 230 |
{ |
| 231 |
void *p = g4; |
| 232 |
if (i < -1) |
| 233 |
p = g1; |
| 234 |
else if (i < 0) |
| 235 |
p = g2; |
| 236 |
else if (1 < i) |
| 237 |
p = g3; |
| 238 |
else if (0 < i) |
| 239 |
p = g5; |
| 240 |
return p; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
ATTR (noipa) void* |
| 245 |
return_2_alloca_local_cstoff (int n, int i) |
| 246 |
{ |
| 247 |
int *a = __builtin_alloca (n); /* { dg-message "declared here" } */ |
| 248 |
int *b = __builtin_alloca (n); /* { dg-message "declared here" } */ |
| 249 |
int *p = i < 0 ? a : b; |
| 250 |
|
| 251 |
p += 1; |
| 252 |
sink (p); |
| 253 |
|
| 254 |
return p; /* { dg-warning "function returns address of local" } */ |
| 255 |
} |
| 256 |
|
| 257 |
ATTR (noipa) void* |
| 258 |
return_alloca_local_cstoff (int n, int i) |
| 259 |
{ |
| 260 |
int *a = __builtin_alloca (n); /* { dg-message "declared here" } */ |
| 261 |
int b[2]; /* { dg-message "declared here" } */ |
| 262 |
|
| 263 |
int *p = b; |
| 264 |
if (i < 0) |
| 265 |
p = a; |
| 266 |
|
| 267 |
p += 1; |
| 268 |
sink (p); |
| 269 |
|
| 270 |
return p; /* { dg-warning "function returns address of local" } */ |
| 271 |
} |
| 272 |
|
| 273 |
ATTR (noipa) void* |
| 274 |
return_local_alloca_cstoff (int n, int i) |
| 275 |
{ |
| 276 |
int a[2]; /* { dg-message "declared here" } */ |
| 277 |
int *b = __builtin_alloca (n); /* { dg-message "declared here" } */ |
| 278 |
int *p = b; |
| 279 |
if (i < 0) |
| 280 |
p = a; |
| 281 |
|
| 282 |
p += 1; |
| 283 |
sink (p); |
| 284 |
|
| 285 |
return p; /* { dg-warning "function returns address of local" } */ |
| 286 |
} |
| 287 |
|
| 288 |
ATTR (noipa) void* |
| 289 |
return_2_locals_cstoff (int i) |
| 290 |
{ |
| 291 |
int a[1]; /* { dg-message "declared here" } */ |
| 292 |
int b[2]; /* { dg-message "declared here" } */ |
| 293 |
|
| 294 |
int *p = b; |
| 295 |
if (i < 0) |
| 296 |
p = a; |
| 297 |
|
| 298 |
p += 1; |
| 299 |
sink (p); |
| 300 |
|
| 301 |
return p; /* { dg-warning "function returns address of local" } */ |
| 302 |
} |
| 303 |
|
| 304 |
ATTR (noipa) void* |
| 305 |
return_2_globals_3_locals_cstoff (int i) |
| 306 |
{ |
| 307 |
int a[1]; /* { dg-message "declared here" } */ |
| 308 |
int b[2]; /* { dg-message "declared here" } */ |
| 309 |
int c[3]; /* { dg-message "declared here" } */ |
| 310 |
|
| 311 |
int *p = c; |
| 312 |
if (i < -1) |
| 313 |
p = a; |
| 314 |
else if (i < 0) |
| 315 |
p = b; |
| 316 |
else if (1 < i) |
| 317 |
p = g1; |
| 318 |
else if (0 < i) |
| 319 |
p = g2; |
| 320 |
|
| 321 |
p += 1; |
| 322 |
sink (p); |
| 323 |
|
| 324 |
return p; /* { dg-warning "function may return address of local" } */ |
| 325 |
} |
| 326 |
|
| 327 |
ATTR (noipa) void* |
| 328 |
return_3_globals_alloca_local_varoff (int n, int i, int j) |
| 329 |
{ |
| 330 |
int *a = __builtin_alloca (n); /* { dg-message "declared here" } */ |
| 331 |
int b[2]; /* { dg-message "declared here" } */ |
| 332 |
|
| 333 |
int *p = g3; |
| 334 |
if (i < -1) |
| 335 |
p = a; |
| 336 |
else if (i < 0) |
| 337 |
p = b; |
| 338 |
else if (1 < i) |
| 339 |
p = g1; |
| 340 |
else if (0 < i) |
| 341 |
p = g2; |
| 342 |
|
| 343 |
p += j; |
| 344 |
sink (p); |
| 345 |
|
| 346 |
return p; /* { dg-warning "function may return address of local" } */ |
| 347 |
} |
| 348 |
|
| 349 |
ATTR (noipa) void* |
| 350 |
return_3_globals_2_locals_varoff (int i, int j) |
| 351 |
{ |
| 352 |
int a[1]; /* { dg-message "declared here" } */ |
| 353 |
int b[2]; /* { dg-message "declared here" } */ |
| 354 |
|
| 355 |
int *p = g3; |
| 356 |
if (i < -1) |
| 357 |
p = a; |
| 358 |
else if (i < 0) |
| 359 |
p = b; |
| 360 |
else if (1 < i) |
| 361 |
p = g1; |
| 362 |
else if (0 < i) |
| 363 |
p = g2; |
| 364 |
|
| 365 |
p += j; |
| 366 |
sink (p); |
| 367 |
|
| 368 |
return p; /* { dg-warning "function may return address of local" } */ |
| 369 |
} |
| 370 |
|