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: implement ExprType.HasPointers() predicate #337

Merged
merged 1 commit into from
Jan 1, 2022
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
75 changes: 75 additions & 0 deletions analyzer/testdata/src/filtertest/f1.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package filtertest
import (
"errors"
"fmt"
"io"
"os"
"time"
"unsafe"
Expand Down Expand Up @@ -406,6 +407,80 @@ func detectType() {
typeTest(float64(5.3), "is uint")
typeTest(float32(5.3), "is uint")
}

{
const untypedStr = "123"
type myString string

type scalarObject struct {
x int
y int
}

type withPointers struct {
x int
y *int
}

var intarr [4]int
var intptrarr [4]*int
var r io.Reader
var ch chan int

typeTest(1, "pointer-free") // want `true`
typeTest(1.6, "pointer-free") // want `true`
typeTest(true, "pointer-free") // want `true`
typeTest(scalarObject{1, 2}, "pointer-free") // want `true`
typeTest(intarr, "pointer-free") // want `true`
typeTest([2]scalarObject{}, "pointer-free") // want `true`

typeTest(withPointers{}, "pointer-free")
typeTest(&withPointers{}, "pointer-free")
typeTest(ch, "pointer-free")
typeTest(r, "pointer-free")
typeTest(&r, "pointer-free")
typeTest(&intarr, "pointer-free")
typeTest(intptrarr, "pointer-free")
typeTest(&intptrarr, "pointer-free")
typeTest(&scalarObject{1, 2}, "pointer-free")
typeTest("str", "pointer-free")
typeTest(untypedStr, "pointer-free")
typeTest(myString("123"), "pointer-free")
typeTest(unsafe.Pointer(nil), "pointer-free")
typeTest(nil, "pointer-free")
typeTest([]int{1}, "pointer-free")
typeTest([]string{""}, "pointer-free")
typeTest(map[string]string{}, "pointer-free")
typeTest(new(int), "pointer-free")
typeTest(new(string), "pointer-free")

typeTest(1, "has pointers")
typeTest(1.6, "has pointers")
typeTest(true, "has pointers")
typeTest(scalarObject{1, 2}, "has pointers")
typeTest(intarr, "has pointers")
typeTest([2]scalarObject{}, "has pointers")

typeTest(withPointers{}, "has pointers") // want `true`
typeTest(&withPointers{}, "has pointers") // want `true`
typeTest(ch, "has pointers") // want `true`
typeTest(r, "has pointers") // want `true`
typeTest(&r, "has pointers") // want `true`
typeTest(&intarr, "has pointers") // want `true`
typeTest(intptrarr, "has pointers") // want `true`
typeTest(&intptrarr, "has pointers") // want `true`
typeTest(&scalarObject{1, 2}, "has pointers") // want `true`
typeTest("str", "has pointers") // want `true`
typeTest(untypedStr, "has pointers") // want `true`
typeTest(myString("123"), "has pointers") // want `true`
typeTest(unsafe.Pointer(nil), "has pointers") // want `true`
typeTest(nil, "has pointers") // want `true`
typeTest([]int{1}, "has pointers") // want `true`
typeTest([]string{""}, "has pointers") // want `true`
typeTest(map[string]string{}, "has pointers") // want `true`
typeTest(new(int), "has pointers") // want `true`
typeTest(new(string), "has pointers") // want `true`
}
}

func detectAddressable(x int, xs []int) {
Expand Down
8 changes: 8 additions & 0 deletions analyzer/testdata/src/filtertest/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,12 @@ func testRules(m dsl.Matcher) {
m.Match(`typeTest($x, "is uint")`).
Where(m["x"].Type.Underlying().OfKind("uint")).
Report(`true`)

m.Match(`typeTest($x, "pointer-free")`).
Where(!m["x"].Type.HasPointers()).
Report(`true`)

m.Match(`typeTest($x, "has pointers")`).
Where(m["x"].Type.HasPointers()).
Report(`true`)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.15
require (
github.com/go-toolsmith/astcopy v1.0.0
github.com/google/go-cmp v0.5.6
github.com/quasilyte/go-ruleguard/dsl v0.3.11
github.com/quasilyte/go-ruleguard/dsl v0.3.12-0.20220101150716-969a394a9451
github.com/quasilyte/go-ruleguard/rules v0.0.0-20211022131956-028d6511ab71
github.com/quasilyte/gogrep v0.0.0-20211226113550-e12a97c7d96d
golang.org/x/tools v0.0.0-20201230224404-63754364767c
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ github.com/quasilyte/go-ruleguard/dsl v0.3.10 h1:4tVlVVcBT+nNWoF+t/zrAMO13sHAqYo
github.com/quasilyte/go-ruleguard/dsl v0.3.10/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQPulDV6YMIXmpQss17rU=
github.com/quasilyte/go-ruleguard/dsl v0.3.11 h1:TOyCsB86VE915JMP8ErXf12k+OvIYNkBN6D/waKQih4=
github.com/quasilyte/go-ruleguard/dsl v0.3.11/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQPulDV6YMIXmpQss17rU=
github.com/quasilyte/go-ruleguard/dsl v0.3.12-0.20220101143450-184fad673365 h1:4yuPNY0w97V+2V8dJyiw07sPMpDvtaOEXEmZUOdUHcQ=
github.com/quasilyte/go-ruleguard/dsl v0.3.12-0.20220101143450-184fad673365/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQPulDV6YMIXmpQss17rU=
github.com/quasilyte/go-ruleguard/dsl v0.3.12-0.20220101150716-969a394a9451 h1:hNInk1SGCCKYcNoVib0KFjwiZZEA/gO75zUxSuLtVJ0=
github.com/quasilyte/go-ruleguard/dsl v0.3.12-0.20220101150716-969a394a9451/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQPulDV6YMIXmpQss17rU=
github.com/quasilyte/go-ruleguard/rules v0.0.0-20201231183845-9e62ed36efe1/go.mod h1:7JTjp89EGyU1d6XfBiXihJNG37wB2VRkd125Q1u7Plc=
github.com/quasilyte/go-ruleguard/rules v0.0.0-20211022131956-028d6511ab71 h1:CNooiryw5aisadVfzneSZPswRWvnVW8hF1bS/vo8ReI=
github.com/quasilyte/go-ruleguard/rules v0.0.0-20211022131956-028d6511ab71/go.mod h1:4cgAphtvu7Ftv7vOT2ZOYhC6CvBxZixcasr8qIOTA50=
Expand Down
38 changes: 38 additions & 0 deletions ruleguard/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ func makeTypeImplementsFilter(src, varname string, iface *types.Interface) filte
}
}

func makeTypeHasPointersFilter(src, varname string) filterFunc {
return func(params *filterParams) matchFilterResult {
typ := params.typeofNode(params.subExpr(varname))
if typeHasPointers(typ) {
return filterSuccess
}
return filterFailure(src)
}
}

func makeTypeIsIntUintFilter(src, varname string, underlying bool, kind types.BasicKind) filterFunc {
return func(params *filterParams) matchFilterResult {
typ := params.typeofNode(params.subExpr(varname))
Expand Down Expand Up @@ -531,3 +541,31 @@ func nodeIs(n ast.Node, tag nodetag.Value) bool {
}
return matched
}

func typeHasPointers(typ types.Type) bool {
switch typ := typ.(type) {
case *types.Basic:
switch typ.Kind() {
case types.UnsafePointer, types.String, types.UntypedNil, types.UntypedString:
return true
}
return false

case *types.Named:
return typeHasPointers(typ.Underlying())

case *types.Struct:
for i := 0; i < typ.NumFields(); i++ {
if typeHasPointers(typ.Field(i).Type()) {
return true
}
}
return false

case *types.Array:
return typeHasPointers(typ.Elem())

default:
return true
}
}
54 changes: 30 additions & 24 deletions ruleguard/ir/filter_op.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ruleguard/ir/gen_filter_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func main() {
{name: "VarLine", comment: "m[$Value].Line", valueType: "string", flags: flagHasVar},
{name: "VarValueInt", comment: "m[$Value].Value.Int()", valueType: "string", flags: flagHasVar},
{name: "VarTypeSize", comment: "m[$Value].Type.Size", valueType: "string", flags: flagHasVar},
{name: "VarTypeHasPointers", comment: "m[$Value].Type.HasPointers()", valueType: "string", flags: flagHasVar},

{name: "VarFilter", comment: "m[$Value].Filter($Args[0])", valueType: "string", flags: flagHasVar},
{name: "VarNodeIs", comment: "m[$Value].Node.Is($Args[0])", valueType: "string", flags: flagHasVar},
Expand Down
3 changes: 3 additions & 0 deletions ruleguard/ir_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,9 @@ func (l *irLoader) newFilter(filter ir.FilterExpr, info *filterInfo) (matchFilte
}
result.fn = makeNodeIsFilter(result.src, filter.Value.(string), tag)

case ir.FilterVarTypeHasPointersOp:
result.fn = makeTypeHasPointersFilter(result.src, filter.Value.(string))

case ir.FilterVarTypeOfKindOp, ir.FilterVarTypeUnderlyingOfKindOp:
kindString := l.unwrapStringExpr(filter.Args[0])
if kindString == "" {
Expand Down
2 changes: 2 additions & 0 deletions ruleguard/irconv/irconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,8 @@ func (conv *converter) convertFilterExprImpl(e ast.Expr) ir.FilterExpr {
return ir.FilterExpr{Op: ir.FilterRootNodeParentIsOp, Args: args}
case "Object.Is":
return ir.FilterExpr{Op: ir.FilterVarObjectIsOp, Value: op.varName, Args: args}
case "Type.HasPointers":
return ir.FilterExpr{Op: ir.FilterVarTypeHasPointersOp, Value: op.varName}
case "Type.Is":
return ir.FilterExpr{Op: ir.FilterVarTypeIsOp, Value: op.varName, Args: args}
case "Type.Underlying.Is":
Expand Down