Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruleguard,analyzer: change Report() callback argument #327

Merged
merged 1 commit into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package analyzer
import (
"bytes"
"fmt"
"go/ast"
"go/token"
"io/ioutil"
"os"
Expand Down Expand Up @@ -105,17 +104,19 @@ func runAnalyzer(pass *analysis.Pass) (interface{}, error) {
Sizes: pass.TypesSizes,
Fset: pass.Fset,
GoVersion: goVersion,
Report: func(info ruleguard.GoRuleInfo, n ast.Node, msg string, s *ruleguard.Suggestion) {
fullMessage := msg
Report: func(data *ruleguard.ReportData) {
fullMessage := data.Message
info := data.RuleInfo
if printRuleLocation {
fullMessage = fmt.Sprintf("%s: %s (%s:%d)",
info.Group.Name, msg, filepath.Base(info.Group.Filename), info.Line)
info.Group.Name, data.Message, filepath.Base(info.Group.Filename), info.Line)
}
diag := analysis.Diagnostic{
Pos: n.Pos(),
Pos: data.Node.Pos(),
Message: fullMessage,
}
if s != nil {
if data.Suggestion != nil {
s := data.Suggestion
diag.SuggestedFixes = []analysis.SuggestedFix{
{
Message: "suggested replacement",
Expand Down
7 changes: 4 additions & 3 deletions analyzer/testanalyzer/testanalyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package testanalyzer

import (
"fmt"
"go/ast"
"go/token"
"path/filepath"

Expand All @@ -26,11 +25,13 @@ func New(ruleSet *ir.File) *analysis.Analyzer {
Types: pass.TypesInfo,
Sizes: pass.TypesSizes,
Fset: pass.Fset,
Report: func(info ruleguard.GoRuleInfo, n ast.Node, msg string, s *ruleguard.Suggestion) {
Report: func(data *ruleguard.ReportData) {
info := data.RuleInfo
msg := data.Message
fullMessage := fmt.Sprintf("%s: %s (%s:%d)",
info.Group.Name, msg, filepath.Base(info.Group.Filename), info.Line)
pass.Report(analysis.Diagnostic{
Pos: n.Pos(),
Pos: data.Node.Pos(),
Message: fullMessage,
})
},
Expand Down
2 changes: 2 additions & 0 deletions ruleguard/ast_walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ func (w *astWalker) walk(n ast.Node) {
if n.Doc != nil {
w.walk(n.Doc)
}
prevFunc := w.filterParams.currentFunc
if n.Recv != nil {
w.walk(n.Recv)
}
Expand All @@ -314,6 +315,7 @@ func (w *astWalker) walk(n ast.Node) {
if n.Body != nil {
w.walk(n.Body)
}
w.filterParams.currentFunc = prevFunc

case *ast.File:
w.walk(n.Name)
Expand Down
2 changes: 1 addition & 1 deletion ruleguard/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func newDebugTestRunner(input string) (*debugTestRunner, error) {
Types: &info,
Sizes: types.SizesFor("gc", runtime.GOARCH),
Fset: fset,
Report: func(info GoRuleInfo, n ast.Node, msg string, s *Suggestion) {
Report: func(data *ReportData) {
// Do nothing.
},
}
Expand Down
2 changes: 2 additions & 0 deletions ruleguard/gorule.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type filterParams struct {

deadcode bool

currentFunc *ast.FuncDecl

// varname is set only for custom filters before bytecode function is called.
varname string
}
Expand Down
2 changes: 1 addition & 1 deletion ruleguard/perf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func benchRunContext(b *testing.B, src string) (*RunContext, *ast.File) {
Types: typesInfo,
Sizes: types.SizesFor("gc", runtime.GOARCH),
Fset: fset,
Report: func(info GoRuleInfo, n ast.Node, msg string, s *Suggestion) {
Report: func(data *ReportData) {
// Do nothing.
},
}
Expand Down
26 changes: 21 additions & 5 deletions ruleguard/ruleguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,31 @@ type RunContext struct {
DebugImports bool
DebugPrint func(string)

Types *types.Info
Sizes types.Sizes
Fset *token.FileSet
Report func(rule GoRuleInfo, n ast.Node, msg string, s *Suggestion)
Pkg *types.Package
Types *types.Info
Sizes types.Sizes
Fset *token.FileSet
Pkg *types.Package

// Report is a function that is called for every successful ruleguard match.
// The pointer to ReportData is reused, it should not be kept.
// If you want to keep it after Report() returns, make a copy.
Report func(*ReportData)

GoVersion GoVersion
}

type ReportData struct {
RuleInfo GoRuleInfo
Node ast.Node
Message string
Suggestion *Suggestion

// Experimental: fields below are part of the experiment.
// They'll probably be removed or changed over time.

Func *ast.FuncDecl
}

type Suggestion struct {
From token.Pos
To token.Pos
Expand Down
18 changes: 16 additions & 2 deletions ruleguard/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type rulesRunner struct {
ctx *RunContext
rules *goRuleSet

reportData ReportData

gogrepState gogrep.MatcherState

importer *goImporter
Expand Down Expand Up @@ -291,7 +293,12 @@ func (rr *rulesRunner) handleCommentMatch(rule goCommentRule, m commentMatchData
Group: rule.base.group,
Line: rule.base.line,
}
rr.ctx.Report(info, node, message, suggestion)
rr.reportData.RuleInfo = info
rr.reportData.Node = node
rr.reportData.Message = message
rr.reportData.Suggestion = suggestion

rr.ctx.Report(&rr.reportData)
return true
}

Expand Down Expand Up @@ -322,7 +329,14 @@ func (rr *rulesRunner) handleMatch(rule goRule, m gogrep.MatchData) bool {
Group: rule.group,
Line: rule.line,
}
rr.ctx.Report(info, node, message, suggestion)
rr.reportData.RuleInfo = info
rr.reportData.Node = node
rr.reportData.Message = message
rr.reportData.Suggestion = suggestion

rr.reportData.Func = rr.filterParams.currentFunc

rr.ctx.Report(&rr.reportData)
return true
}

Expand Down