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

add global property for Var #370

Merged
merged 17 commits into from Feb 2, 2022
22 changes: 22 additions & 0 deletions analyzer/testdata/src/filtertest/f1.go
Expand Up @@ -872,3 +872,25 @@ func detectNode() {
nodeTest(rows[0][5], "IndexExpr") // want `true`
nodeTest("42", "IndexExpr")
}

var globalVar string
var globalVar2 string = time.Now().String() // want `\Qglobal var`
var globalVar3 = time.Now().String() // want `\Qglobal var`
var _ = time.Now().String() // false negative // TODO https://github.com/quasilyte/go-ruleguard/issues/369
var _ string = time.Now().String() // false negative // TODO https://github.com/quasilyte/go-ruleguard/issues/369
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all _ share the same "object" (none, nil object).
So they define "nothing".
Since you basically should use this IsGlobal not for a declaration but rather for the usage of some variable, I don't think these cases matter here.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't have different types for nil object either. Simply because there is nothing to associate a type with.

var (
globalVar4 string
)

func detectGlobal() {
globalVar = time.Now().String() // want `\Qglobal var`
globalVar4 = time.Now().String() // want `\Qglobal var`
{
globalVar := time.Now().String() // shadowed global var
print(globalVar)
}
{
var globalVar = time.Now().String() // shadowed global var
print(globalVar)
}
}
7 changes: 7 additions & 0 deletions analyzer/testdata/src/filtertest/rules.go
Expand Up @@ -263,4 +263,11 @@ func testRules(m dsl.Matcher) {
m.Match(`typeTest($x, $y, "identical types")`).
Where(m["x"].Type.IdenticalTo(m["y"])).
Report(`true`)

m.Match(`$x = time.Now().String()`,
`var $x = time.Now().String()`,
`var $x $_ = time.Now().String()`,
`$x := time.Now().String()`).
Where(m["x"].Global).
Report(`global var`)
}
3 changes: 3 additions & 0 deletions dsl/dsl.go
Expand Up @@ -145,6 +145,9 @@ type Var struct {
// Line is a source code line number that contains this match.
// If this match is multi-line, this is the first line number.
Line int

// Global reports whether the corresponding var is defined in global scope.
Global bool
quasilyte marked this conversation as resolved.
Show resolved Hide resolved
}

// Filter applies a custom predicate function on a submatch.
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Expand Up @@ -2,6 +2,10 @@ module github.com/quasilyte/go-ruleguard

go 1.17

replace (
github.com/quasilyte/go-ruleguard/dsl v0.3.15 => ./dsl
)

require (
github.com/go-toolsmith/astcopy v1.0.0
github.com/google/go-cmp v0.5.6
Expand Down
17 changes: 15 additions & 2 deletions ruleguard/filters.go
Expand Up @@ -7,12 +7,13 @@ import (
"go/types"
"path/filepath"

"github.com/quasilyte/gogrep"
"github.com/quasilyte/gogrep/nodetag"

"github.com/quasilyte/go-ruleguard/internal/xtypes"
"github.com/quasilyte/go-ruleguard/ruleguard/quasigo"
"github.com/quasilyte/go-ruleguard/ruleguard/textmatch"
"github.com/quasilyte/go-ruleguard/ruleguard/typematch"
"github.com/quasilyte/gogrep"
"github.com/quasilyte/gogrep/nodetag"
)

const filterSuccess = matchFilterResult("")
Expand Down Expand Up @@ -363,6 +364,18 @@ func makeLineFilter(src, varname string, op token.Token, rhsVarname string) filt
}
}

func makeGlobalFilter(src, varname string) filterFunc {
return func(params *filterParams) matchFilterResult {
obj := params.ctx.Types.ObjectOf(identOf(params.subExpr(varname)))
globalScope := params.ctx.Pkg.Scope()
if obj.Parent() == globalScope {
return filterSuccess
}

return filterFailure(src)
}
}

func makeGoVersionFilter(src string, op token.Token, version GoVersion) filterFunc {
return func(params *filterParams) matchFilterResult {
if params.ctx.GoVersion.IsAny() {
Expand Down
66 changes: 36 additions & 30 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
Expand Up @@ -45,6 +45,7 @@ func main() {
{name: "VarConstSlice", comment: "m[$Value].ConstSlice", valueType: "string", flags: flagHasVar},
{name: "VarText", comment: "m[$Value].Text", valueType: "string", flags: flagHasVar},
{name: "VarLine", comment: "m[$Value].Line", valueType: "string", flags: flagHasVar},
{name: "VarGlobal", comment: "m[$Value].Global", valueType: "bool", 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},
Expand Down
7 changes: 5 additions & 2 deletions ruleguard/ir_loader.go
Expand Up @@ -11,13 +11,14 @@ import (
"io/ioutil"
"regexp"

"github.com/quasilyte/gogrep"
"github.com/quasilyte/gogrep/nodetag"

"github.com/quasilyte/go-ruleguard/ruleguard/goutil"
"github.com/quasilyte/go-ruleguard/ruleguard/ir"
"github.com/quasilyte/go-ruleguard/ruleguard/quasigo"
"github.com/quasilyte/go-ruleguard/ruleguard/textmatch"
"github.com/quasilyte/go-ruleguard/ruleguard/typematch"
"github.com/quasilyte/gogrep"
"github.com/quasilyte/gogrep/nodetag"
)

type irLoaderConfig struct {
Expand Down Expand Up @@ -675,6 +676,8 @@ func (l *irLoader) newFilter(filter ir.FilterExpr, info *filterInfo) (matchFilte
result.fn = makePureFilter(result.src, filter.Value.(string))
case ir.FilterVarConstOp:
result.fn = makeConstFilter(result.src, filter.Value.(string))
case ir.FilterVarGlobalOp:
result.fn = makeGlobalFilter(result.src, filter.Value.(string))
case ir.FilterVarConstSliceOp:
result.fn = makeConstSliceFilter(result.src, filter.Value.(string))
case ir.FilterVarAddressableOp:
Expand Down
5 changes: 4 additions & 1 deletion ruleguard/irconv/irconv.go
Expand Up @@ -11,9 +11,10 @@ import (
"strings"

"github.com/go-toolsmith/astcopy"
"golang.org/x/tools/go/ast/astutil"

"github.com/quasilyte/go-ruleguard/ruleguard/goutil"
"github.com/quasilyte/go-ruleguard/ruleguard/ir"
"golang.org/x/tools/go/ast/astutil"
)

type Context struct {
Expand Down Expand Up @@ -637,6 +638,8 @@ func (conv *converter) convertFilterExprImpl(e ast.Expr) ir.FilterExpr {
return ir.FilterExpr{Op: ir.FilterVarAddressableOp, Value: op.varName}
case "Type.Size":
return ir.FilterExpr{Op: ir.FilterVarTypeSizeOp, Value: op.varName}
case "Global":
return ir.FilterExpr{Op: ir.FilterVarGlobalOp, Value: op.varName}
}

case *ast.CallExpr:
Expand Down