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
2 changes: 1 addition & 1 deletion analyzer/testdata/src/filtertest/rules.go
Expand Up @@ -268,6 +268,6 @@ func testRules(m dsl.Matcher) {
`var $x = time.Now().String()`,
`var $x $_ = time.Now().String()`,
`$x := time.Now().String()`).
Where(m["x"].Global).
Where(m["x"].Object.IsGlobal()).
Report(`global var`)
}
6 changes: 3 additions & 3 deletions dsl/dsl.go
Expand Up @@ -145,9 +145,6 @@ 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
}

// Filter applies a custom predicate function on a submatch.
Expand Down Expand Up @@ -203,6 +200,9 @@ type TypesObject struct{}
// See https://golang.org/pkg/go/types/.
func (TypesObject) Is(typ string) bool { return boolResult }

// IsGlobal reports whether an associated types.Object is defined in global scope.
func (TypesObject) IsGlobal() bool { return boolResult }

// ExprType describes a type of a matcher expr.
type ExprType struct {
// Size represents expression type size in bytes.
Expand Down
2 changes: 1 addition & 1 deletion ruleguard/filters.go
Expand Up @@ -364,7 +364,7 @@ func makeLineFilter(src, varname string, op token.Token, rhsVarname string) filt
}
}

func makeGlobalFilter(src, varname string) filterFunc {
func makeObjectIsGlobalFilter(src, varname string) filterFunc {
return func(params *filterParams) matchFilterResult {
obj := params.ctx.Types.ObjectOf(identOf(params.subExpr(varname)))
globalScope := params.ctx.Pkg.Scope()
Expand Down
24 changes: 12 additions & 12 deletions ruleguard/ir/filter_op.gen.go

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

3 changes: 2 additions & 1 deletion ruleguard/ir/gen_filter_op.go
@@ -1,3 +1,4 @@
//go:build generate
// +build generate

package main
Expand Down Expand Up @@ -45,14 +46,14 @@ 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},

{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},
{name: "VarObjectIs", comment: "m[$Value].Object.Is($Args[0])", valueType: "string", flags: flagHasVar},
{name: "VarObjectIsGlobal", comment: "m[$Value].Object.IsGlobal()", valueType: "bool", flags: flagHasVar},
quasilyte marked this conversation as resolved.
Show resolved Hide resolved
{name: "VarTypeIs", comment: "m[$Value].Type.Is($Args[0])", valueType: "string", flags: flagHasVar},
{name: "VarTypeIdenticalTo", comment: "m[$Value].Type.IdenticalTo($Args[0])", valueType: "string", flags: flagHasVar},
{name: "VarTypeUnderlyingIs", comment: "m[$Value].Type.Underlying().Is($Args[0])", valueType: "string", flags: flagHasVar},
Expand Down
4 changes: 2 additions & 2 deletions ruleguard/ir_loader.go
Expand Up @@ -676,8 +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.FilterVarObjectIsGlobalOp:
result.fn = makeObjectIsGlobalFilter(result.src, filter.Value.(string))
case ir.FilterVarConstSliceOp:
result.fn = makeConstSliceFilter(result.src, filter.Value.(string))
case ir.FilterVarAddressableOp:
Expand Down
4 changes: 2 additions & 2 deletions ruleguard/irconv/irconv.go
Expand Up @@ -638,8 +638,6 @@ 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 Expand Up @@ -717,6 +715,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 "Object.IsGlobal":
return ir.FilterExpr{Op: ir.FilterVarObjectIsGlobalOp, Value: op.varName}
case "Type.HasPointers":
return ir.FilterExpr{Op: ir.FilterVarTypeHasPointersOp, Value: op.varName}
case "Type.Is":
Expand Down