aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gostaticanalysis/analysisutil/ssainspect.go
blob: 2f8a165765906372abeb97e57cf6e5b2d765dc3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package analysisutil

import "golang.org/x/tools/go/ssa"

// InspectInstr inspects from i-th instruction of start block to succsessor blocks.
func InspectInstr(start *ssa.BasicBlock, i int, f func(i int, instr ssa.Instruction) bool) {
	new(instrInspector).block(start, i, f)
}

type instrInspector struct {
	done map[*ssa.BasicBlock]bool
}

func (ins *instrInspector) block(b *ssa.BasicBlock, i int, f func(i int, instr ssa.Instruction) bool) {
	if ins.done == nil {
		ins.done = map[*ssa.BasicBlock]bool{}
	}

	if b == nil || ins.done[b] || len(b.Instrs) <= i {
		return
	}

	ins.done[b] = true
	ins.instrs(i, b.Instrs[i:], f)
	for _, s := range b.Succs {
		ins.block(s, 0, f)
	}

}

func (ins *instrInspector) instrs(offset int, instrs []ssa.Instruction, f func(i int, instr ssa.Instruction) bool) {
	for i, instr := range instrs {
		if !f(offset+i, instr) {
			break
		}
	}
}