Skip to content

Commit

Permalink
ruleguard,analyzer: change Report() callback argument
Browse files Browse the repository at this point in the history
Pass a structure with a set of fields instead of
passing them as separate arguments.

This makes it easier to add more (potentially experimental)
match context arguments without breaking the API.

But this time we have to break the API once.

AST walker now saves current function name (and type name if it's
a method). This can be useful in filters later, so we can match
the current function name/type in `Where()` expressions.
  • Loading branch information
quasilyte committed Dec 28, 2021
1 parent 934ac97 commit fbb8806
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 18 deletions.
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
25 changes: 25 additions & 0 deletions ruleguard/ast_walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ type astWalker struct {
visit func(ast.Node)
}

func (w *astWalker) getTypeName(typeExpr ast.Expr) string {
switch typ := typeExpr.(type) {
case *ast.Ident:
return typ.Name
case *ast.StarExpr:
return w.getTypeName(typ.X)
case *ast.ParenExpr:
return w.getTypeName(typ.X)

default:
return ""
}
}

func (w *astWalker) Walk(root ast.Node, visit func(ast.Node)) {
w.visit = visit
w.walk(root)
Expand Down Expand Up @@ -306,6 +320,15 @@ func (w *astWalker) walk(n ast.Node) {
if n.Doc != nil {
w.walk(n.Doc)
}
prevTypeName := w.filterParams.currentTypeName
prevFuncName := w.filterParams.currentFuncName
w.filterParams.currentFuncName = n.Name.Name
if n.Recv != nil {
if len(n.Recv.List) != 0 {
w.filterParams.currentTypeName = w.getTypeName(n.Recv.List[0].Type)
}
w.walk(n.Recv)
}
if n.Recv != nil {
w.walk(n.Recv)
}
Expand All @@ -314,6 +337,8 @@ func (w *astWalker) walk(n ast.Node) {
if n.Body != nil {
w.walk(n.Body)
}
w.filterParams.currentTypeName = prevTypeName
w.filterParams.currentFuncName = prevFuncName

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
3 changes: 3 additions & 0 deletions ruleguard/gorule.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ type filterParams struct {

deadcode bool

currentFuncName string
currentTypeName string

// 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
27 changes: 22 additions & 5 deletions ruleguard/ruleguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,32 @@ 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.

FuncName string
TypeName string
}

type Suggestion struct {
From token.Pos
To token.Pos
Expand Down
24 changes: 22 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,16 @@ 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

// Experimental stuff.
rr.reportData.TypeName = rr.filterParams.currentTypeName
rr.reportData.FuncName = rr.filterParams.currentFuncName

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

Expand Down Expand Up @@ -322,7 +333,16 @@ 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

// Experimental stuff.
rr.reportData.TypeName = rr.filterParams.currentTypeName
rr.reportData.FuncName = rr.filterParams.currentFuncName

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

Expand Down

0 comments on commit fbb8806

Please sign in to comment.