mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 22:58:20 +00:00
Merge branch 'topic/amazingpp/modifiable-const-via-loops' of ssh://github.com/AmazingPP/zeek
Edits: Slight tweaking, plus a simple test. * 'topic/amazingpp/modifiable-const-via-loops' of ssh://github.com/AmazingPP/zeek: Add help function to check loop variable Fix local const variables can be modified via loops
This commit is contained in:
commit
f40ca42590
3 changed files with 35 additions and 17 deletions
34
src/parse.y
34
src/parse.y
|
@ -247,6 +247,15 @@ static bool expr_is_table_type_name(const Expr* expr)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void check_loop_var(const IDPtr& var)
|
||||||
|
{
|
||||||
|
if ( var->IsGlobal() )
|
||||||
|
var->Error("global variable used in 'for' loop");
|
||||||
|
|
||||||
|
if ( var->IsConst() )
|
||||||
|
var->Error("constant used in 'for' loop");
|
||||||
|
}
|
||||||
|
|
||||||
static void build_global(ID* id, Type* t, InitClass ic, Expr* e,
|
static void build_global(ID* id, Type* t, InitClass ic, Expr* e,
|
||||||
std::vector<AttrPtr>* attrs, DeclType dt)
|
std::vector<AttrPtr>* attrs, DeclType dt)
|
||||||
{
|
{
|
||||||
|
@ -1908,11 +1917,7 @@ for_head:
|
||||||
auto loop_var = lookup_ID($3, current_module.c_str());
|
auto loop_var = lookup_ID($3, current_module.c_str());
|
||||||
|
|
||||||
if ( loop_var )
|
if ( loop_var )
|
||||||
{
|
check_loop_var(loop_var);
|
||||||
if ( loop_var->IsGlobal() )
|
|
||||||
loop_var->Error("global variable used in for loop");
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
loop_var = install_ID($3, current_module.c_str(),
|
loop_var = install_ID($3, current_module.c_str(),
|
||||||
|
@ -1942,18 +1947,12 @@ for_head:
|
||||||
|
|
||||||
// Validate previous definitions as needed.
|
// Validate previous definitions as needed.
|
||||||
if ( key_var )
|
if ( key_var )
|
||||||
{
|
check_loop_var(key_var);
|
||||||
if ( key_var->IsGlobal() )
|
|
||||||
key_var->Error("global variable used in for loop");
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
key_var = install_ID($3, module, false, false);
|
key_var = install_ID($3, module, false, false);
|
||||||
|
|
||||||
if ( val_var )
|
if ( val_var )
|
||||||
{
|
check_loop_var(val_var);
|
||||||
if ( val_var->IsGlobal() )
|
|
||||||
val_var->Error("global variable used in for loop");
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
val_var = install_ID($5, module, false, false);
|
val_var = install_ID($5, module, false, false);
|
||||||
|
|
||||||
|
@ -1972,10 +1971,7 @@ for_head:
|
||||||
auto val_var = lookup_ID($7, module);
|
auto val_var = lookup_ID($7, module);
|
||||||
|
|
||||||
if ( val_var )
|
if ( val_var )
|
||||||
{
|
check_loop_var(val_var);
|
||||||
if ( val_var->IsGlobal() )
|
|
||||||
val_var->Error("global variable used in for loop");
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
val_var = install_ID($7, module, false, false);
|
val_var = install_ID($7, module, false, false);
|
||||||
|
|
||||||
|
@ -2004,6 +2000,10 @@ local_id:
|
||||||
{
|
{
|
||||||
if ( $$->IsGlobal() )
|
if ( $$->IsGlobal() )
|
||||||
$$->Error("already a global identifier");
|
$$->Error("already a global identifier");
|
||||||
|
|
||||||
|
if ( $$->IsConst() )
|
||||||
|
$$->Error("already a const identifier");
|
||||||
|
|
||||||
delete [] $1;
|
delete [] $1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
5
testing/btest/Baseline/language.for-var-check/out
Normal file
5
testing/btest/Baseline/language.for-var-check/out
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
|
||||||
|
error in <...>/for-var-check.zeek, line 4: global variable used in 'for' loop (x)
|
||||||
|
error in error and count: type clash in iteration (error and count)
|
||||||
|
error in <...>/for-var-check.zeek, line 8: constant used in 'for' loop (y)
|
||||||
|
error in error and count: type clash in iteration (error and count)
|
13
testing/btest/language/for-var-check.zeek
Normal file
13
testing/btest/language/for-var-check.zeek
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
# @TEST-EXEC-FAIL: zeek -b %INPUT >out 2>&1
|
||||||
|
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out
|
||||||
|
|
||||||
|
global x: count = 0;
|
||||||
|
|
||||||
|
event zeek_init()
|
||||||
|
{
|
||||||
|
const y: count = 0;
|
||||||
|
|
||||||
|
for ( x in set(1, 2, 3) ) {}
|
||||||
|
for ( y in set(1, 2, 3) ) {}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue