From baae7a46fd0924e2792883335ca469aa5d4bc0a5 Mon Sep 17 00:00:00 2001 From: lu4p Date: Tue, 7 Jul 2020 21:11:54 +0200 Subject: [PATCH] simplify detection of reflection --- main.go | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index c5ae0eb..4894482 100644 --- a/main.go +++ b/main.go @@ -644,16 +644,18 @@ func hashWith(salt, value string) string { // // The blacklist mainly contains named types and their field declarations. func buildBlacklist(files []*ast.File, info *types.Info, pkg *types.Package) map[types.Object]struct{} { - // Keep track of the current syntax tree level. If reflectCallLevel is - // non-negative, we are under a reflect call. - level := 0 - reflectCallLevel := -1 - blacklist := make(map[types.Object]struct{}) - addToBlacklist := func(named *types.Named) { + + reflectBlacklist := func(node ast.Node) bool { + expr, _ := node.(ast.Expr) + named := namedType(info.TypeOf(expr)) + if named == nil { + return true + } + obj := named.Obj() if obj == nil || obj.Pkg() != pkg { - return + return true } blacklist[obj] = struct{}{} @@ -663,27 +665,15 @@ func buildBlacklist(files []*ast.File, info *types.Info, pkg *types.Package) map blacklist[strct.Field(i)] = struct{}{} } } + + return true } + visit := func(node ast.Node) bool { if envGarbleLiterals { constBlacklist(node, info, blacklist) } - if node == nil { - if level == reflectCallLevel { - reflectCallLevel = -1 - } - level-- - return true - } - if reflectCallLevel >= 0 && level >= reflectCallLevel { - expr, _ := node.(ast.Expr) - named := namedType(info.TypeOf(expr)) - if named != nil { - addToBlacklist(named) - } - } - level++ call, ok := node.(*ast.CallExpr) if !ok { return true @@ -699,7 +689,9 @@ func buildBlacklist(files []*ast.File, info *types.Info, pkg *types.Package) map } if fnType.Pkg().Path() == "reflect" && (fnType.Name() == "TypeOf" || fnType.Name() == "ValueOf") { - reflectCallLevel = level + for _, arg := range call.Args { + ast.Inspect(arg, reflectBlacklist) + } } return true }