]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gcc.dg/Wreturn-local-addr-3.c
1 /* PR c/71924 - missing -Wreturn-local-addr returning alloca result
3 { dg-options "-O2 -Wall" }
4 { dg-require-effective-target alloca } */
6 #define ATTR(...) __attribute__ ((__VA_ARGS__))
8 typedef __INTPTR_TYPE__
intptr_t;
10 struct A
{ int a
, b
, c
; };
11 struct B
{ int a
, b
, c
[]; };
13 extern int g1
[5], g2
[5], g3
[5], g4
[5], g5
[5];
15 void sink (void*, ...);
17 /* Verify that a pointer difference expression is handled correctly
18 even when converted to a pointer. */
21 return_local_diff_cst (void)
24 void *p
= (void*)(&a
[4] - &a
[1]);
29 return_local_diff_var (int i
, int j
)
32 void *p
= (void*)(&a
[j
] - &a
[i
]);
37 return_2_locals (int i
)
39 int a
[1]; /* { dg-message "declared here" } */
40 int b
[2]; /* { dg-message "declared here" } */
41 void *p
= i
< 0 ? a
: b
;
42 return p
; /* { dg-warning "function returns address of local" } */
45 /* Verify that returning the address of a local converted to intptr_t
46 is not diagnosed (see bug 90737 for a case the front-end gets wrong). */
49 return_int_2_locals (int i
)
53 void *p
= i
< 0 ? a
: b
;
57 /* Verify that a conditional expression with a pointer first operand
58 is handled correctly. */
61 return_2_locals_ptrcond (void *q
)
63 int a
[1]; /* { dg-message "declared here" } */
64 int b
[2]; /* { dg-message "declared here" } */
66 return p
; /* { dg-warning "function returns address of local" } */
69 /* Verify that a preincrement expression with a pointer operand is
73 return_2_locals_ptrinc (void *q
)
75 int a
[1]; /* { dg-message "declared here" } */
76 int b
[2]; /* { dg-message "declared here" } */
78 return ++p
; /* { dg-warning "function returns address of local" } */
82 return_3_locals (int i
)
84 int a
[1]; /* { dg-message "declared here" } */
85 int b
[2]; /* { dg-message "declared here" } */
86 int c
[3]; /* { dg-message "declared here" } */
88 void *p
= i
< 0 ? a
: 0 < i
? c
: b
;
89 return p
; /* { dg-warning "function returns address of local" } */
92 /* Verify that a conditional expression with a pointer first operand
93 is handled correctly. */
96 return_3_locals_ptrcond (void *p
, void *q
)
98 int a
[1]; /* { dg-message "declared here" } */
99 int b
[2]; /* { dg-message "declared here" } */
100 int c
[3]; /* { dg-message "declared here" } */
102 void *r
= q
? r
? a
: b
: c
;
103 return r
; /* { dg-warning "function returns address of local" } */
107 return_5_locals (int i
)
109 int a
[1]; /* { dg-message "declared here" } */
110 int b
[2]; /* { dg-message "declared here" } */
111 int c
[3]; /* { dg-message "declared here" } */
112 int d
[4]; /* { dg-message "declared here" } */
113 int e
[5]; /* { dg-message "declared here" } */
115 void *p
= i
< -1 ? a
: i
< 0 ? b
: 1 < i
? e
: 0 < i
? d
: c
;
116 return p
; /* { dg-warning "function returns address of local" } */
120 return_1_global_4_locals (int i
)
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" } */
127 void *p
= i
< -1 ? a
: i
< 0 ? b
: 1 < i
? g1
: 0 < i
? d
: c
;
128 return p
; /* { dg-warning "function may return address of local" } */
132 return_2_globals_3_locals (int i
)
134 int a
[1]; /* { dg-message "declared here" } */
135 int b
[2]; /* { dg-message "declared here" } */
136 int c
[3]; /* { dg-message "declared here" } */
138 void *p
= i
< -1 ? a
: i
< 0 ? b
: 1 < i
? g1
: 0 < i
? g2
: c
;
139 return p
; /* { dg-warning "function may return address of local" } */
143 return_3_globals_2_locals (int i
)
145 int a
[1]; /* { dg-message "declared here" } */
146 int b
[2]; /* { dg-message "declared here" } */
148 void *p
= i
< -1 ? a
: i
< 0 ? b
: 1 < i
? g1
: 0 < i
? g2
: g3
;
149 return p
; /* { dg-warning "function may return address of local" } */
153 return_4_globals_1_local (int i
)
155 int a
[1]; /* { dg-message "declared here" } */
157 void *p
= i
< -1 ? a
: i
< 0 ? g1
: 1 < i
? g2
: 0 < i
? g4
: g3
;
158 return p
; /* { dg-warning "function may return address of local" } */
162 return_all_globals (int i
)
164 void *p
= i
< -1 ? g1
: i
< 0 ? g2
: 1 < i
? g3
: 0 < i
? g5
: g4
;
170 return_2_alloca_local_cstoff (int n
, int i
)
172 int *a
= __builtin_alloca (n
); /* { dg-message "declared here" } */
173 int *b
= __builtin_alloca (n
); /* { dg-message "declared here" } */
174 int *p
= i
< 0 ? a
: b
;
177 return p
; /* { dg-warning "function returns address of local" } */
181 return_alloca_local_cstoff (int n
, int i
)
183 int *a
= __builtin_alloca (n
); /* { dg-message "declared here" } */
184 int b
[2]; /* { dg-message "declared here" } */
185 int *p
= i
< 0 ? a
: b
;
188 return p
; /* { dg-warning "function returns address of local" } */
192 return_local_alloca_cstoff (int n
, int i
)
194 int a
[2]; /* { dg-message "declared here" } */
195 int *b
= __builtin_alloca (n
); /* { dg-message "declared here" } */
196 int *p
= i
< 0 ? a
: b
;
199 return p
; /* { dg-warning "function returns address of local" } */
203 return_2_locals_cstoff (int i
)
205 int a
[1]; /* { dg-message "declared here" } */
206 int b
[2]; /* { dg-message "declared here" } */
207 int *p
= i
< 0 ? a
: b
;
210 return p
; /* { dg-warning "function returns address of local" } */
214 return_2_globals_3_locals_cstoff (int i
)
216 int a
[1]; /* { dg-message "declared here" } */
217 int b
[2]; /* { dg-message "declared here" } */
218 int c
[3]; /* { dg-message "declared here" } */
220 int *p
= i
< -1 ? a
: i
< 0 ? b
: 1 < i
? g1
: 0 < i
? g2
: c
;
223 return p
; /* { dg-warning "function may return address of local" } */
227 return_3_globals_alloca_local_varoff (int n
, int i
, int j
)
229 int *a
= __builtin_alloca (n
); /* { dg-message "declared here" } */
230 int b
[2]; /* { dg-message "declared here" } */
232 int *p
= i
< -1 ? a
: i
< 0 ? b
: 1 < i
? g1
: 0 < i
? g2
: g3
;
235 return p
; /* { dg-warning "function may return address of local" } */
239 return_3_globals_2_locals_varoff (int i
, int j
)
241 int a
[1]; /* { dg-message "declared here" } */
242 int b
[2]; /* { dg-message "declared here" } */
244 int *p
= i
< -1 ? a
: i
< 0 ? b
: 1 < i
? g1
: 0 < i
? g2
: g3
;
247 return p
; /* { dg-warning "function may return address of local" } */