<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/154624>154624</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
__attribute__((cleanup)) on variable declaration in for loop initialization doesn't work correctly in c89/90
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang:codegen
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ahatanak
</td>
</tr>
</table>
<pre>
clang has an extension that allows variable declarations in initialization statements of for loops before C99. The variable doesn't go out of scope at the end of the for loop.
```
// This compiles without errors with `-std=c89`.
void foo() {
for (int i = 1; 0;)
;
printf("i=%d\n", i);
}
However, when the declared variable has a cleanup, the cleanup function is run at the end of the for loop rather than at the end of function `foo`.
$ cat test.c
```
#include <stdio.h>
void cleaner(int *a) { printf("run cleanup\n"); }
void foo() {
for (__attribute__((cleanup(cleaner))) int i = 1; 0;)
;
printf("i=%d\n", i);
}
int main() {
foo();
return 0;
}
```
$ clang test.c -std=c89
$ ./a.out
run cleanup
i=1
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy0lMGOozgQhp-muJQaGQMJHDikOxvtA8y9ZUwleMexI7vo7OzTr2zodO_Mqm8jWUoMVb__-sqUitFcHNEA7TO0x0ItPPswqFmxcup7Mfrpx6CtchecVUTlkP5mctF4hzwrRmWtv0d8U8Go0RJOpK0Kio13EY1D4wwbZc0_-RFGVkxXchzRn_HsA1rvbxFHOvtA-NL3JX6b6ZOep-hA7hkvHv3CKS1qfyNUjDwTkpvSs_T3Xa4EcUhrJ7YlDiBPIE_4bTYRtb_ejKWId8NzkqQQfFi3CDvxFHmC-qi7HnYiab15M-HZe5AdyB5h_wzigPk4kJ1xjAahPmIF9TMKqJ9B9jkCMW3EAW_BOD7nfGmgPoJsJ2hfHEgJ8gVNSsiRsD-u5v_0d3qjkN7eZ3K5vpUtTR90ck9QW1JuuaXYFLZt8bw4naGbiGFxXwDDoHimkDr6c9hDBHYiIViJZKIN6hRLkUv9C-7aOG2XiRDql8iT8eUM9R9rauaZXaYCM0CQB7Wx_Q-s5Pu9vHdeCRU-QH3dnNdXxRzMuDC9vuaQ7kGre1jo14W_p5VJ9aqM-8Xh5nkTxkC8BLee-kj_DHWDnr_GFTt-uqvr2xLkSZV-YRCHz_DEIZmtimmop77uVUFDtW_bvWyb_a6Yh71SndZVdW5HoUdqVaVko4VW56pWeqwLM0ghW9FJIau6qtqy6khMTdNNvRorqidoBF2VsaW1b9fSh0thYlxoqNpmJ5vCqpFszHNGylwD1AftJ7pQZtceizCk1KdxuURohDWR44cYG7Y0fNXP3ELv_ncWpVH0uO0_zaSPEXP34TtqHwJptj9STgIrT70olmCHmfkWod6GycXwvIyl9leQp2Rz-3m6Bf8XaQZ5ygAiyNPG4G2Q_wYAAP__G4SpOw">