-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom filters are simple Go functions defined in ruleguard rule files
that can be used as Where clause predicates. They're compiled to a bytecode that is then interpreted when the matched rule needs to apply its filters. The performance is good enough for now (faster than `yaegi`) and can be further improved in the future. There are various limitations in what can be used in custom filters and what's not. It's not well-documented right now, but the compile errors try to be helpful. What can be compiled will be executed like a normal Go would. One use case for this new feature can be found in #129 Here is the solution to #129 which is not possible: ```go func implementsStringer(ctx *dsl.VarFilterContext) bool { stringer := ctx.GetInterface(`fmt.Stringer`) return types.Implements(ctx.Type, stringer) || types.Implements(types.NewPointer(ctx.Type), stringer) } func stringerLiteral(m dsl.Matcher) { m.Match(`$x{$*_}`). Where(m["x"].Filter(implementsStringer)). Report(`$x implements fmt.Stringer`) } ``` Custom filters are more flexible than predefined filters, but they generally require more coding. As a rule of thumb: they should be used only when there is no matching builtin filter. Note: right now many simple operations, like integer multiplication, are not implemented. They can be added trivially, but I wanted to present a working concept with a minimal amount of code for this first PR. Upcoming changes that extend the supported features set are going to be easier to review. It's also possible to allow calling byte-compiled functions from other byte-compiled functions. But it's not there yet (I also need examples where it can be applied to get a better understanding of the subject).
- Loading branch information
Showing
46 changed files
with
3,495 additions
and
361 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/root/target.go:8:10: exprUnparen: the parentheses around 1 are superfluous (rules.go:15) | ||
/root/target.go:9:10: exprUnparen: the parentheses around 2 are superfluous (rules.go:15) | ||
/root/target.go:8:10: exprUnparen: the parentheses around 1 are superfluous (rules.go:21) | ||
/root/target.go:9:10: exprUnparen: the parentheses around 2 are superfluous (rules.go:21) | ||
/root/target.go:11:10: boolComparison: omit bool literal in expression (rules1.go:8) | ||
/root/target.go:12:10: boolExprSimplify: suggestion: b (rules2.go:6) | ||
/root/target.go:15:10: interfaceAddr: taking address of interface-typed value (rules.go:27) |
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 |
---|---|---|
|
@@ -10,4 +10,7 @@ func test(b bool) { | |
|
||
println(b == true) | ||
println(!!b) | ||
|
||
var eface interface{} | ||
println(&eface) | ||
} |
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,4 +1,5 @@ | ||
/root/target.go:8:10: exprUnparen: the parentheses around 1 are superfluous (rules.go:15) | ||
/root/target.go:9:10: exprUnparen: the parentheses around 2 are superfluous (rules.go:15) | ||
/root/target.go:11:10: testrules/boolComparison: omit bool literal in expression (rules1.go:8) | ||
/root/target.go:12:10: testrules/boolExprSimplify: suggestion: b (rules2.go:6) | ||
/root/target.go:8:10: exprUnparen: the parentheses around 1 are superfluous (rules.go:21) | ||
/root/target.go:9:10: exprUnparen: the parentheses around 2 are superfluous (rules.go:21) | ||
/root/target.go:11:10: boolComparison: omit bool literal in expression (rules1.go:8) | ||
/root/target.go:12:10: boolExprSimplify: suggestion: b (rules2.go:6) | ||
/root/target.go:15:10: interfaceAddr: taking address of interface-typed value (rules.go:27) |
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 |
---|---|---|
|
@@ -10,4 +10,7 @@ func test(b bool) { | |
|
||
println(b == true) | ||
println(!!b) | ||
|
||
var eface interface{} | ||
println(&eface) | ||
} |
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,2 +1,2 @@ | ||
/root/target.go:8:10: exprUnparen: the parentheses around 1 are superfluous | ||
/root/target.go:9:10: exprUnparen: the parentheses around 2 are superfluous | ||
/root/target.go:8:10: exprUnparen: the parentheses around 1 are superfluous (rules.go:10) | ||
/root/target.go:9:10: exprUnparen: the parentheses around 2 are superfluous (rules.go:10) |
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
Oops, something went wrong.