Skip to content

Commit

Permalink
ruleguard: make sub-match gogrep state per-runner (#367)
Browse files Browse the repository at this point in the history
Fixes #366
  • Loading branch information
quasilyte committed Jan 20, 2022
1 parent c57998e commit 20831c4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
6 changes: 2 additions & 4 deletions ruleguard/filters.go
Expand Up @@ -160,16 +160,14 @@ func makeAddressableFilter(src, varname string) filterFunc {
}

func makeVarContainsFilter(src, varname string, pat *gogrep.Pattern) filterFunc {
// TODO: use a shared state here as well?
state := gogrep.NewMatcherState()
return func(params *filterParams) matchFilterResult {
state.CapturePreset = params.match.CaptureList()
params.gogrepSubState.CapturePreset = params.match.CaptureList()
matched := false
gogrep.Walk(params.subNode(varname), func(n ast.Node) bool {
if matched {
return false
}
pat.MatchNode(&state, n, func(m gogrep.MatchData) {
pat.MatchNode(params.gogrepSubState, n, func(m gogrep.MatchData) {
matched = true
})
return true
Expand Down
3 changes: 2 additions & 1 deletion ruleguard/gorule.go
Expand Up @@ -58,7 +58,8 @@ type filterParams struct {
imports map[string]struct{}
env *quasigo.EvalEnv

importer *goImporter
importer *goImporter
gogrepSubState *gogrep.MatcherState

match matchData
nodePath *nodePath
Expand Down
21 changes: 13 additions & 8 deletions ruleguard/runner.go
Expand Up @@ -33,7 +33,8 @@ type rulesRunner struct {

reportData ReportData

gogrepState gogrep.MatcherState
gogrepState gogrep.MatcherState
gogrepSubState gogrep.MatcherState

importer *goImporter

Expand Down Expand Up @@ -67,14 +68,17 @@ func newRulesRunner(ctx *RunContext, buildContext *build.Context, state *engineS
})
gogrepState := gogrep.NewMatcherState()
gogrepState.Types = ctx.Types
gogrepSubState := gogrep.NewMatcherState()
gogrepSubState.Types = ctx.Types
rr := &rulesRunner{
bgContext: context.Background(),
ctx: ctx,
importer: importer,
rules: rules,
gogrepState: gogrepState,
nodePath: newNodePath(),
truncateLen: ctx.TruncateLen,
bgContext: context.Background(),
ctx: ctx,
importer: importer,
rules: rules,
gogrepState: gogrepState,
gogrepSubState: gogrepSubState,
nodePath: newNodePath(),
truncateLen: ctx.TruncateLen,
filterParams: filterParams{
env: state.env.GetEvalEnv(),
importer: importer,
Expand All @@ -86,6 +90,7 @@ func newRulesRunner(ctx *RunContext, buildContext *build.Context, state *engineS
}
rr.filterParams.nodeText = rr.nodeText
rr.filterParams.nodePath = &rr.nodePath
rr.filterParams.gogrepSubState = &rr.gogrepSubState
return rr
}

Expand Down

0 comments on commit 20831c4

Please sign in to comment.