Skip to content

Conversation

jongmyeong-choi
Copy link
Contributor

In C89, for-init variables have function scope, so cleanup should occur at function exit, not loop exit. This implements deferred cleanup registration for C89 mode while preserving C99+ behavior.

Fixes #154624

In C89, for-init variables have function scope, so cleanup should occur
at function exit, not loop exit. This implements deferred cleanup
registration for C89 mode while preserving C99+ behavior.

Fixes llvm#154624
@jongmyeong-choi
Copy link
Contributor Author

I've implemented the change using a deferred cleanup approach.
Initially, I considered pushing cleanups to the EHStack while manipulating PrologueCleanupDepth, but I felt this approach was too hacky.
If there's a better approach for this fix, please let me know.

Copy link

github-actions bot commented Sep 3, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Collaborator

@efriedma-quic efriedma-quic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"End of the function" is not really correct. It's still the end of the scope... just the scope in question is different. This matters if you have, for example, a nested loop.

Probably need to change CodeGenFunction::EmitForStmt to push the scope at the right point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

__attribute__((cleanup)) on variable declaration in for loop initialization doesn't work correctly in c89/90
2 participants