simplify detection of reflection

pull/53/head
lu4p 4 years ago committed by GitHub
parent f1bf6f91ee
commit baae7a46fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -644,16 +644,18 @@ func hashWith(salt, value string) string {
// //
// The blacklist mainly contains named types and their field declarations. // 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{} { 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{}) 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() obj := named.Obj()
if obj == nil || obj.Pkg() != pkg { if obj == nil || obj.Pkg() != pkg {
return return true
} }
blacklist[obj] = struct{}{} blacklist[obj] = struct{}{}
@ -663,27 +665,15 @@ func buildBlacklist(files []*ast.File, info *types.Info, pkg *types.Package) map
blacklist[strct.Field(i)] = struct{}{} blacklist[strct.Field(i)] = struct{}{}
} }
} }
return true
} }
visit := func(node ast.Node) bool { visit := func(node ast.Node) bool {
if envGarbleLiterals { if envGarbleLiterals {
constBlacklist(node, info, blacklist) 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) call, ok := node.(*ast.CallExpr)
if !ok { if !ok {
return true 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") { if fnType.Pkg().Path() == "reflect" && (fnType.Name() == "TypeOf" || fnType.Name() == "ValueOf") {
reflectCallLevel = level for _, arg := range call.Args {
ast.Inspect(arg, reflectBlacklist)
}
} }
return true return true
} }

Loading…
Cancel
Save