Skip to content

Commit

Permalink
feature: add debug functionality and false positive test
Browse files Browse the repository at this point in the history
  • Loading branch information
butuzov committed May 5, 2023
1 parent 01f9e12 commit 6d01c4f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
8 changes: 7 additions & 1 deletion analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func (a *analyzer) run(pass *analysis.Pass) (interface{}, error) {
imports := checker.LoadImports(pass.Fset, ins)
issues := []*analysis.Diagnostic{}

debugFn := debugNoOp

// --- Preorder Checker ------------------------------------------------------
ins.Preorder([]ast.Node{(*ast.CallExpr)(nil)}, func(n ast.Node) {
callExpr := n.(*ast.CallExpr)
Expand All @@ -70,11 +72,15 @@ func (a *analyzer) run(pass *analysis.Pass) (interface{}, error) {
return
}

if a.withDebug {
debugFn = debug(pass.Fset)
}

fileImports := imports.LookupImports(fileName)

// Run checkers against call expression.
for _, check := range a.checkers {
violation := check.With(pass.TypesInfo, fileImports).Check(callExpr)
violation := check.With(pass.TypesInfo, fileImports, debugFn).Check(callExpr)
if violation != nil {
issues = append(issues, violation.Diagnostic(n.Pos(), n.End()))
}
Expand Down
22 changes: 22 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mirror

import (
"fmt"
"go/ast"
"go/printer"
"go/token"
"os"
"strings"
)

// --- debug -------------------------------------------------------------------

func debug(f *token.FileSet) func(ast.Expr, string, ...any) {
return func(node ast.Expr, format string, a ...any) {
printer.Fprint(os.Stderr, f, node)
fmt.Fprintln(os.Stderr, "\n"+strings.Repeat("^", 80))
fmt.Fprintf(os.Stderr, format, a...)
}
}

func debugNoOp(_ ast.Expr, _ string, _ ...any) {}
4 changes: 3 additions & 1 deletion internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Checker struct {

types *types.Info // used for checking types
imports []Import // imports (of current file)
debug func(ast.Expr, string, ...any)
}

// New will accept a name for package (like `text/template` or `strings`) and
Expand Down Expand Up @@ -151,8 +152,9 @@ func cleanName(name string) string {
return name
}

func (c *Checker) With(types *types.Info, i []Import) *Checker {
func (c *Checker) With(types *types.Info, i []Import, debugFn func(ast.Expr, string, ...any)) *Checker {
c.types = types
c.imports = i
c.debug = debugFn
return c
}
19 changes: 19 additions & 0 deletions testdata/false_positive1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"bytes"
"crypto/sha256"
"encoding/hex"
"log"
)

// using function that accepts string and process it and then return []bytes
func S1() {
h := sha256.New()
if _, err := h.Write([]byte("foobar")); err != nil {
log.Fatal(err)
}

contentBuf := bytes.NewBufferString("foo-bar\n")
contentBuf.WriteString(hex.EncodeToString(h.Sum(nil))) // false postitve
}

0 comments on commit 6d01c4f

Please sign in to comment.