[cfe-dev] Question about 'CodeGenFunction::EmitGotoStmt'

via cfe-dev cfe-dev at lists.llvm.org
Tue Jun 29 10:40:50 PDT 2021


> From a spec benchmark, I have seen that the ‘goto’ statement goes to
> its destination through the cleanup function as below.
> 
> void CodeGenFunction::EmitGotoStmt(const GotoStmt &S) {
>   // If this code is reachable then emit a stop point (if generating
>   // debug info). We have to do this ourselves because we are on the
>   // "simple" statement path.
>   if (HaveInsertPoint())
>     EmitStopPoint(&S);
>   EmitBranchThroughCleanup(getJumpDestForLabel(S.getLabel()));
> }
> 
> I guess we could emit the branch for the target directly. If possible,
> can someone let me know why the goto statement has to go through the
> cleanup function please? If I missed something, please let me know.

I haven't looked, but one reason would be if the 'goto' transfers out
of a block that has a local variable with a destructor; the destructor 
has to run before control transfers to the 'goto' label.  If there are
no such local variables, there is no cleanup to do, and the 'goto'
becomes a simple branch.
--paulr



More information about the cfe-dev mailing list