mirror of
https://github.com/zeek/zeek.git
synced 2025-10-17 14:08:20 +00:00
Add "fallthrough" keyword, require a flow statement to end case blocks.
Case blocks in switch statements now must end in a break, return, or fallthrough statement to give best mix of safety, readability, and flexibility. The new fallthrough keyword explicitly allows control to be passed to the next case block in a switch statement. Addresses #754.
This commit is contained in:
parent
8695053e27
commit
be71a42f4c
8 changed files with 150 additions and 33 deletions
|
@ -148,18 +148,19 @@ function switch_empty(v: count): string
|
|||
return "n/a";
|
||||
}
|
||||
|
||||
function switch_break(v: count): string
|
||||
function switch_fallthrough(v: count): string
|
||||
{
|
||||
local rval = "";
|
||||
switch ( v ) {
|
||||
case 1:
|
||||
rval += "test";
|
||||
fallthrough;
|
||||
case 2:
|
||||
rval += "testing";
|
||||
break;
|
||||
rval += "ERROR";
|
||||
fallthrough;
|
||||
case 3:
|
||||
rval += "tested";
|
||||
break;
|
||||
}
|
||||
return rval + "return";
|
||||
}
|
||||
|
@ -170,12 +171,16 @@ function switch_default(v: count): string
|
|||
switch ( v ) {
|
||||
case 1:
|
||||
rval += "1";
|
||||
fallthrough;
|
||||
case 2:
|
||||
rval += "2";
|
||||
break;
|
||||
case 3:
|
||||
rval += "3";
|
||||
fallthrough;
|
||||
default:
|
||||
rval += "d";
|
||||
break;
|
||||
}
|
||||
return rval + "r";
|
||||
}
|
||||
|
@ -186,13 +191,16 @@ function switch_default_placement(v: count): string
|
|||
switch ( v ) {
|
||||
case 1:
|
||||
rval += "1";
|
||||
fallthrough;
|
||||
default:
|
||||
rval += "d";
|
||||
fallthrough;
|
||||
case 2:
|
||||
rval += "2";
|
||||
break;
|
||||
case 3:
|
||||
rval += "3";
|
||||
break;
|
||||
}
|
||||
return rval + "r";
|
||||
}
|
||||
|
@ -252,17 +260,17 @@ event new_connection(c: connection)
|
|||
test_switch( switch_subnet([fe80::1]/96) , "[fe80::0]" );
|
||||
test_switch( switch_subnet(192.168.1.100/16) , "192.168.0.0/16" );
|
||||
test_switch( switch_empty(2) , "n/a" );
|
||||
test_switch( switch_break(1) , "testreturn" );
|
||||
test_switch( switch_break(2) , "testingreturn" );
|
||||
test_switch( switch_break(3) , "testedreturn" );
|
||||
test_switch( switch_default(1) , "1r" );
|
||||
test_switch( switch_fallthrough(1) , "testtestingtestedreturn" );
|
||||
test_switch( switch_fallthrough(2) , "testingtestedreturn" );
|
||||
test_switch( switch_fallthrough(3) , "testedreturn" );
|
||||
test_switch( switch_default(1) , "12r" );
|
||||
test_switch( switch_default(2) , "2r" );
|
||||
test_switch( switch_default(3) , "3r" );
|
||||
test_switch( switch_default(3) , "3dr" );
|
||||
test_switch( switch_default(4) , "dr" );
|
||||
test_switch( switch_default_placement(1) , "1r" );
|
||||
test_switch( switch_default_placement(1) , "1d2r" );
|
||||
test_switch( switch_default_placement(2) , "2r" );
|
||||
test_switch( switch_default_placement(3) , "3r" );
|
||||
test_switch( switch_default_placement(4) , "dr" );
|
||||
test_switch( switch_default_placement(4) , "d2r" );
|
||||
|
||||
local v = vector(0,1,2,3,4,5,6,7,9,10);
|
||||
local expect: string;
|
||||
|
@ -272,12 +280,16 @@ event new_connection(c: connection)
|
|||
switch ( v[i] ) {
|
||||
case 1, 2:
|
||||
expect = "1,2";
|
||||
break;
|
||||
case 3, 4, 5:
|
||||
expect = "3,4,5";
|
||||
break;
|
||||
case 6, 7, 8, 9:
|
||||
expect = "6,7,8,9";
|
||||
break;
|
||||
default:
|
||||
expect = "n/a";
|
||||
break;
|
||||
}
|
||||
test_switch( switch_case_list(v[i]) , expect );
|
||||
}
|
||||
|
|
|
@ -143,18 +143,19 @@ function switch_empty(v: count): string
|
|||
return "n/a";
|
||||
}
|
||||
|
||||
function switch_break(v: count): string
|
||||
function switch_fallthrough(v: count): string
|
||||
{
|
||||
local rval = "";
|
||||
switch ( v ) {
|
||||
case 1:
|
||||
rval += "test";
|
||||
fallthrough;
|
||||
case 2:
|
||||
rval += "testing";
|
||||
break;
|
||||
rval += "ERROR";
|
||||
fallthrough;
|
||||
case 3:
|
||||
rval += "tested";
|
||||
break;
|
||||
}
|
||||
return rval + "return";
|
||||
}
|
||||
|
@ -165,12 +166,16 @@ function switch_default(v: count): string
|
|||
switch ( v ) {
|
||||
case 1:
|
||||
rval += "1";
|
||||
fallthrough;
|
||||
case 2:
|
||||
rval += "2";
|
||||
break;
|
||||
case 3:
|
||||
rval += "3";
|
||||
fallthrough;
|
||||
default:
|
||||
rval += "d";
|
||||
break;
|
||||
}
|
||||
return rval + "r";
|
||||
}
|
||||
|
@ -181,13 +186,16 @@ function switch_default_placement(v: count): string
|
|||
switch ( v ) {
|
||||
case 1:
|
||||
rval += "1";
|
||||
fallthrough;
|
||||
default:
|
||||
rval += "d";
|
||||
fallthrough;
|
||||
case 2:
|
||||
rval += "2";
|
||||
break;
|
||||
case 3:
|
||||
rval += "3";
|
||||
break;
|
||||
}
|
||||
return rval + "r";
|
||||
}
|
||||
|
@ -247,17 +255,17 @@ event bro_init()
|
|||
test_switch( switch_subnet([fe80::1]/96) , "[fe80::0]" );
|
||||
test_switch( switch_subnet(192.168.1.100/16) , "192.168.0.0/16" );
|
||||
test_switch( switch_empty(2) , "n/a" );
|
||||
test_switch( switch_break(1) , "testreturn" );
|
||||
test_switch( switch_break(2) , "testingreturn" );
|
||||
test_switch( switch_break(3) , "testedreturn" );
|
||||
test_switch( switch_default(1) , "1r" );
|
||||
test_switch( switch_fallthrough(1) , "testtestingtestedreturn" );
|
||||
test_switch( switch_fallthrough(2) , "testingtestedreturn" );
|
||||
test_switch( switch_fallthrough(3) , "testedreturn" );
|
||||
test_switch( switch_default(1) , "12r" );
|
||||
test_switch( switch_default(2) , "2r" );
|
||||
test_switch( switch_default(3) , "3r" );
|
||||
test_switch( switch_default(3) , "3dr" );
|
||||
test_switch( switch_default(4) , "dr" );
|
||||
test_switch( switch_default_placement(1) , "1r" );
|
||||
test_switch( switch_default_placement(1) , "1d2r" );
|
||||
test_switch( switch_default_placement(2) , "2r" );
|
||||
test_switch( switch_default_placement(3) , "3r" );
|
||||
test_switch( switch_default_placement(4) , "dr" );
|
||||
test_switch( switch_default_placement(4) , "d2r" );
|
||||
|
||||
local v = vector(0,1,2,3,4,5,6,7,9,10);
|
||||
local expect: string;
|
||||
|
@ -267,12 +275,16 @@ event bro_init()
|
|||
switch ( v[i] ) {
|
||||
case 1, 2:
|
||||
expect = "1,2";
|
||||
break;
|
||||
case 3, 4, 5:
|
||||
expect = "3,4,5";
|
||||
break;
|
||||
case 6, 7, 8, 9:
|
||||
expect = "6,7,8,9";
|
||||
break;
|
||||
default:
|
||||
expect = "n/a";
|
||||
break;
|
||||
}
|
||||
test_switch( switch_case_list(v[i]) , expect );
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue