<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/100868>100868</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
`[[gnu::cleanup()]]` function executed before return value evaluation
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
fuhsnn
</td>
</tr>
</table>
<pre>
The code use the `[[gnu::cleanup()]]` attribute to register a cleanup function. The cleanup function should be called after the return value being determined.
godbolt link: https://godbolt.org/z/zY5abr361
```
#include <stdio.h>
typedef struct {
int i;
} S;
void inc(S *obj) {
obj->i += 7;
}
S test(void) {
S s [[gnu::cleanup(inc)]];
printf("returning %d\n", s.i = 22);
return s;
}
int main()
{
printf("returned %d\n", test().i);
return 0;
}
```
GCC print:
```
returning 22
returned 22
```
Clang print:
```
returning 22
returned 29
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVMGO4zYM_Rr6QoxhU3ESH3zIJJt-wPTSo2zRtqaKFFhS2u3XF3KcTJLdBYoCsgRR5OOjnkXpvR4scwPVO1SHTMYwuqnp4-itzVqnvje_j4ydU4zRM4aREdbF7P0-2AhiB2LXGZY2noG2QDVUhzTWBcoQJt3GwBgcTjxoH3hCiYs79tF2QTub45zjxYp-dNEobBk7aQwrlH2KTxQmDnGyeJEmMras7YCKA08nbVnlUByg2F3nwanWmYBG2z9B7HAM4ewTazoCHZfT3E0D0PGf9P1RyXYS6_IRJFV8HdctCW07ExUjiL0PSrt8BPHtMSR8P7PiHn2YYhcQNu9XOyKitgE1iMUCmwN-fO3m-eK0Qm07oO0HAu1c-wlUP6O49vMNxDeNQO8gDrh5RHwE-8DAPgBtE-oPMB_o8ZdyzhRuij5TTLHnSdvQz7LTVZKkBFCloNpbIALao881Jn5pV99BbhL6X7FOl3SS2i4_1eJyJ_5jalb4mnqpG6jO9VPyW_biJ9mftf5tv7_mSlfzM4evuokeLYnOzfISsjfSDv8b9X4ZzxGZaoSqRS0zbsoNUbEtaxLZ2Kw2ohe8rgtZr1Zdr8q2kj2JvthWdV3LdaYbKmhVbGhTUlER5UpSWXaiX_U9dWpdwqrgk9QmN-ZySm8l095Hbsqi2K63mZEtGz93ECLLf-F8miSoDtnUpKC3Ng4eVoXRPvgvmKCD4eY_95N7Z-C_uYuBU2_o3fTSDjgtMvllcTLNy4PXYYxt3rkT0DHxWJa38-Q-uQtAx5m9Bzou5V0a-jcAAP__dG93xg">