ZAM fix for inlining empty function bodies as the sole elements of an if-else

This commit is contained in:
Vern Paxson 2024-11-04 15:45:27 -08:00
parent a3a0155825
commit 286c4dc776
3 changed files with 30 additions and 2 deletions

View file

@ -2613,9 +2613,16 @@ ExprPtr InlineExpr::Reduce(Reducer* c, StmtPtr& red_stmt) {
body = body->Reduce(c);
c->PopInlineBlock();
auto catch_ret = with_location_of(make_intrusive<CatchReturnStmt>(sf, body, ret_val), this);
StmtPtr repl_stmt;
red_stmt = MergeStmts(red_stmt, catch_ret);
if ( body->Tag() == STMT_NULL )
// The inlined body reduced down to nothing, expose that fact
// rather than masking it with an empty catch-return.
repl_stmt = make_intrusive<NullStmt>();
else
repl_stmt = make_intrusive<CatchReturnStmt>(sf, body, ret_val);
red_stmt = MergeStmts(red_stmt, with_location_of(repl_stmt, this));
return ret_val ? ret_val->Duplicate() : nullptr;
}

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
got through the conditional

View file

@ -0,0 +1,19 @@
# @TEST-DOC: Regression test for past ZAM issues with inlining empty functions in conditionals
#
# @TEST-EXEC: zeek -b -O ZAM %INPUT >output
# @TEST-EXEC: btest-diff output
function empty_func() {}
# Use a global to avoid constant propagation optimizing out the conditional.
global bar = F;
event zeek_init()
{
if ( bar )
empty_func();
else
empty_func();
print "got through the conditional";
}