parse.y: Traverse AST post parsing to detect break/next usage issues

Seemed easiest to do it via the traversal infrastructure as we do not
otherwise track enough context/scope when instantiating break or next
statements.

Might be worth moving this out of src/parse.y, but didn't exactly know
where. Or maybe we wait until there's more such trivial validations
popping up

Fixes #2440
This commit is contained in:
Arne Welzel 2022-10-26 13:47:54 +02:00
parent 2ed42ef771
commit 850aaaa5a8
15 changed files with 227 additions and 2 deletions

View file

@ -0,0 +1,85 @@
# @TEST-DOC: Check break and next usage within for, while, switch and hooks.
# @TEST-EXEC-FAIL: zeek -b %INPUT
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr
function f()
{
next;
}
event zeek_init() { f(); };
@TEST-START-NEXT
function f()
{
break;
}
event zeek_init() { f(); };
@TEST-START-NEXT
event zeek_init()
{
next;
}
@TEST-START-NEXT
event zeek_init()
{
break;
}
@TEST-START-NEXT
event zeek_init()
{
if ( T )
break;
}
@TEST-START-NEXT
event zeek_init()
{
local history = "Sr";
switch history {
case "S":
print history;
next;
break;
}
}
@TEST-START-NEXT
global the_hook: hook(c: count);
hook the_hook(c: count)
{
next;
}
@TEST-START-NEXT
global the_hook: hook(c: count);
hook the_hook(c: count)
{
if ( T )
next;
}
@TEST-START-NEXT
# Should report 3 errors.
global the_hook: hook(c: count);
hook the_hook(c: count)
{
next;
}
event zeek_init()
{
break;
}
event zeek_init()
{
next;
}