-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/topic/awelzel/2440-break-next-us…
…age-validation' * origin/topic/awelzel/2440-break-next-usage-validation: parse.y: Traverse AST post parsing to detect break/next usage issues
- Loading branch information
Showing
17 changed files
with
233 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
5.2.0-dev.171 | ||
5.2.0-dev.173 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#include "zeek/ScriptValidation.h" | ||
|
||
#include "zeek/Func.h" | ||
#include "zeek/Reporter.h" | ||
#include "zeek/Stmt.h" | ||
#include "zeek/Traverse.h" | ||
|
||
namespace zeek::detail | ||
{ | ||
|
||
// Validate context of break and next statement usage. | ||
class BreakNextScriptValidation : public TraversalCallback | ||
{ | ||
public: | ||
TraversalCode PreStmt(const Stmt* stmt) | ||
{ | ||
if ( ! StmtIsRelevant(stmt) ) | ||
return TC_CONTINUE; | ||
|
||
stmt_depths[stmt->Tag()] += 1; | ||
|
||
if ( stmt->Tag() == STMT_BREAK && ! BreakStmtIsValid() ) | ||
{ | ||
zeek::reporter->PushLocation(stmt->GetLocationInfo()); | ||
zeek::reporter->Error("break statement used outside of for, while or " | ||
"switch statement and not within a hook"); | ||
zeek::reporter->PopLocation(); | ||
} | ||
|
||
if ( stmt->Tag() == STMT_NEXT && ! NextStmtIsValid() ) | ||
{ | ||
zeek::reporter->PushLocation(stmt->GetLocationInfo()); | ||
zeek::reporter->Error("next statement used outside of for or while statement"); | ||
zeek::reporter->PopLocation(); | ||
} | ||
|
||
return TC_CONTINUE; | ||
} | ||
|
||
TraversalCode PostStmt(const Stmt* stmt) | ||
{ | ||
if ( ! StmtIsRelevant(stmt) ) | ||
return TC_CONTINUE; | ||
|
||
--stmt_depths[stmt->Tag()]; | ||
|
||
assert(stmt_depths[stmt->Tag()] >= 0); | ||
|
||
return TC_CONTINUE; | ||
} | ||
|
||
TraversalCode PreFunction(const zeek::Func* func) | ||
{ | ||
if ( func->Flavor() == zeek::FUNC_FLAVOR_HOOK ) | ||
++hook_depth; | ||
|
||
assert(hook_depth <= 1); | ||
|
||
return TC_CONTINUE; | ||
} | ||
|
||
TraversalCode PostFunction(const zeek::Func* func) | ||
{ | ||
if ( func->Flavor() == zeek::FUNC_FLAVOR_HOOK ) | ||
--hook_depth; | ||
|
||
assert(hook_depth >= 0); | ||
|
||
return TC_CONTINUE; | ||
} | ||
|
||
private: | ||
bool StmtIsRelevant(const Stmt* stmt) | ||
{ | ||
StmtTag tag = stmt->Tag(); | ||
return tag == STMT_FOR || tag == STMT_WHILE || tag == STMT_SWITCH || tag == STMT_BREAK || | ||
tag == STMT_NEXT; | ||
} | ||
|
||
bool BreakStmtIsValid() | ||
{ | ||
return hook_depth > 0 || stmt_depths[STMT_FOR] > 0 || stmt_depths[STMT_WHILE] > 0 || | ||
stmt_depths[STMT_SWITCH] > 0; | ||
} | ||
|
||
bool NextStmtIsValid() { return stmt_depths[STMT_FOR] > 0 || stmt_depths[STMT_WHILE] > 0; } | ||
|
||
std::unordered_map<const StmtTag, int> stmt_depths; | ||
int hook_depth = 0; | ||
}; | ||
|
||
void script_validation() | ||
{ | ||
zeek::detail::BreakNextScriptValidation bn_cb; | ||
zeek::detail::traverse_all(&bn_cb); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// See the file "COPYING" in the main distribution directory for copyright. | ||
#pragma once | ||
|
||
namespace zeek::detail | ||
{ | ||
|
||
/** | ||
* Run extra validations on the parsed AST after everything is initialized | ||
* and report any errors via zeek::reporter->Error(). | ||
*/ | ||
void script_validation(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
testing/btest/Baseline/language.next-break-context-errors-2/.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. | ||
error in <...>/next-break-context-errors.zeek, line 3: break statement used outside of for, while or switch statement and not within a hook |
2 changes: 2 additions & 0 deletions
2
testing/btest/Baseline/language.next-break-context-errors-3/.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. | ||
error in <...>/next-break-context-errors.zeek, line 3: next statement used outside of for or while statement |
2 changes: 2 additions & 0 deletions
2
testing/btest/Baseline/language.next-break-context-errors-4/.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. | ||
error in <...>/next-break-context-errors.zeek, line 3: break statement used outside of for, while or switch statement and not within a hook |
2 changes: 2 additions & 0 deletions
2
testing/btest/Baseline/language.next-break-context-errors-5/.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. | ||
error in <...>/next-break-context-errors.zeek, line 4: break statement used outside of for, while or switch statement and not within a hook |
2 changes: 2 additions & 0 deletions
2
testing/btest/Baseline/language.next-break-context-errors-6/.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. | ||
error in <...>/next-break-context-errors.zeek, line 7: next statement used outside of for or while statement |
2 changes: 2 additions & 0 deletions
2
testing/btest/Baseline/language.next-break-context-errors-7/.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. | ||
error in <...>/next-break-context-errors.zeek, line 5: next statement used outside of for or while statement |
2 changes: 2 additions & 0 deletions
2
testing/btest/Baseline/language.next-break-context-errors-8/.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. | ||
error in <...>/next-break-context-errors.zeek, line 6: next statement used outside of for or while statement |
4 changes: 4 additions & 0 deletions
4
testing/btest/Baseline/language.next-break-context-errors-9/.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. | ||
error in <...>/next-break-context-errors.zeek, line 6: next statement used outside of for or while statement | ||
error in <...>/next-break-context-errors.zeek, line 11: break statement used outside of for, while or switch statement and not within a hook | ||
error in <...>/next-break-context-errors.zeek, line 16: next statement used outside of for or while statement |
2 changes: 2 additions & 0 deletions
2
testing/btest/Baseline/language.next-break-context-errors/.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. | ||
error in <...>/next-break-context-errors.zeek, line 7: next statement used outside of for or while statement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |