Skip to content

Commit

Permalink
pkg/analyzer: deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
hallabro committed Aug 26, 2023
1 parent 879d843 commit 6fb7d0b
Showing 1 changed file with 52 additions and 62 deletions.
114 changes: 52 additions & 62 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ func run(pass *analysis.Pass) (interface{}, error) {
for _, b := range f.Blocks {
for i := range b.Instrs {
// Check if instruction is call that returns a target pointer type
pointerTargetValues := getTargetTypesValues(b, i, targetTypes)
if len(pointerTargetValues) == 0 {
targetValues := getTargetTypesValues(b, i, targetTypes)
if len(targetValues) == 0 {
continue
}

// For each found target check if they are closed and deferred
for _, targetValue := range pointerTargetValues {
for _, targetValue := range targetValues {
refs := (*targetValue.value).Referrers()
isClosed := checkClosed(refs, targetTypes)
if !isClosed {
Expand Down Expand Up @@ -169,51 +169,36 @@ func getTargetTypesValues(b *ssa.BasicBlock, i int, targetTypes []any) []targetV
varType := v.Type()

for _, targetType := range targetTypes {
switch tt := targetType.(type) {
case *types.Pointer:
if !types.Identical(varType, tt) {
continue
}
var tt types.Type

for _, cRef := range *call.Referrers() {
switch instr := cRef.(type) {
case *ssa.Call:
if len(instr.Call.Args) >= 1 && types.Identical(instr.Call.Args[0].Type(), tt) {
targetValues = append(targetValues, targetValue{
value: &instr.Call.Args[0],
instr: call,
})
}
case ssa.Value:
if types.Identical(instr.Type(), tt) {
targetValues = append(targetValues, targetValue{
value: &instr,
instr: call,
})
}
}
}
switch targetType.(type) {
case *types.Pointer:
tt = targetType.(*types.Pointer)
case *types.Named:
if !types.Identical(varType, tt) {
continue
}
tt = targetType.(*types.Named)
default:
continue
}

for _, cRef := range *call.Referrers() {
switch instr := cRef.(type) {
case *ssa.Call:
if len(instr.Call.Args) >= 1 && types.Identical(instr.Call.Args[0].Type(), tt) {
targetValues = append(targetValues, targetValue{
value: &instr.Call.Args[0],
instr: call,
})
}
case ssa.Value:
if types.Identical(instr.Type(), tt) {
targetValues = append(targetValues, targetValue{
value: &instr,
instr: call,
})
}
if !types.Identical(varType, tt) {
continue
}

for _, cRef := range *call.Referrers() {
switch instr := cRef.(type) {
case *ssa.Call:
if len(instr.Call.Args) >= 1 && types.Identical(instr.Call.Args[0].Type(), tt) {
targetValues = append(targetValues, targetValue{
value: &instr.Call.Args[0],
instr: call,
})
}
case ssa.Value:
if types.Identical(instr.Type(), tt) {
targetValues = append(targetValues, targetValue{
value: &instr,
instr: call,
})
}
}
}
Expand Down Expand Up @@ -310,18 +295,20 @@ func getAction(instr ssa.Instruction, targetTypes []any) action {
case *ssa.UnOp:
instrType := instr.Type()
for _, targetType := range targetTypes {
switch tt := targetType.(type) {
var tt types.Type

switch targetType.(type) {
case *types.Pointer:
if types.Identical(instrType, tt) {
if checkClosed(instr.Referrers(), targetTypes) {
return actionHandled
}
}
tt = targetType.(*types.Pointer)
case *types.Named:
if types.Identical(instrType, tt) {
if checkClosed(instr.Referrers(), targetTypes) {
return actionHandled
}
tt = targetType.(*types.Named)
default:
continue
}

if types.Identical(instrType, tt) {
if checkClosed(instr.Referrers(), targetTypes) {
return actionHandled
}
}
}
Expand Down Expand Up @@ -372,17 +359,20 @@ func checkDeferred(pass *analysis.Pass, instrs *[]ssa.Instruction, targetTypes [
case *ssa.UnOp:
instrType := instr.Type()
for _, targetType := range targetTypes {
switch tt := targetType.(type) {
var tt types.Type

switch targetType.(type) {
case *types.Pointer:
if types.Identical(instrType, tt) {
checkDeferred(pass, instr.Referrers(), targetTypes, inDefer)
}
tt = targetType.(*types.Pointer)
case *types.Named:
if types.Identical(instrType, tt) {
checkDeferred(pass, instr.Referrers(), targetTypes, inDefer)
}
tt = targetType.(*types.Named)
default:
continue
}

if types.Identical(instrType, tt) {
checkDeferred(pass, instr.Referrers(), targetTypes, inDefer)
}
}
case *ssa.FieldAddr:
checkDeferred(pass, instr.Referrers(), targetTypes, inDefer)
Expand Down

0 comments on commit 6fb7d0b

Please sign in to comment.