github.com/golangci/go-tools@v0.0.0-20190318060251-af6baa5dc196/ssautil/ssautil.go (about) 1 package ssautil 2 3 import ( 4 "github.com/golangci/go-tools/ssa" 5 ) 6 7 func Reachable(from, to *ssa.BasicBlock) bool { 8 if from == to { 9 return true 10 } 11 if from.Dominates(to) { 12 return true 13 } 14 15 found := false 16 Walk(from, func(b *ssa.BasicBlock) bool { 17 if b == to { 18 found = true 19 return false 20 } 21 return true 22 }) 23 return found 24 } 25 26 func Walk(b *ssa.BasicBlock, fn func(*ssa.BasicBlock) bool) { 27 seen := map[*ssa.BasicBlock]bool{} 28 wl := []*ssa.BasicBlock{b} 29 for len(wl) > 0 { 30 b := wl[len(wl)-1] 31 wl = wl[:len(wl)-1] 32 if seen[b] { 33 continue 34 } 35 seen[b] = true 36 if !fn(b) { 37 continue 38 } 39 wl = append(wl, b.Succs...) 40 } 41 }