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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
package irutil
import (
"go/types"
"strings"
"honnef.co/go/tools/go/ir"
"honnef.co/go/tools/go/types/typeutil"
)
func Reachable(from, to *ir.BasicBlock) bool {
if from == to {
return true
}
if from.Dominates(to) {
return true
}
found := false
Walk(from, func(b *ir.BasicBlock) bool {
if b == to {
found = true
return false
}
return true
})
return found
}
func Walk(b *ir.BasicBlock, fn func(*ir.BasicBlock) bool) {
seen := map[*ir.BasicBlock]bool{}
wl := []*ir.BasicBlock{b}
for len(wl) > 0 {
b := wl[len(wl)-1]
wl = wl[:len(wl)-1]
if seen[b] {
continue
}
seen[b] = true
if !fn(b) {
continue
}
wl = append(wl, b.Succs...)
}
}
func Vararg(x *ir.Slice) ([]ir.Value, bool) {
var out []ir.Value
alloc, ok := ir.Unwrap(x.X).(*ir.Alloc)
if !ok {
return nil, false
}
var checkAlloc func(alloc ir.Value) bool
checkAlloc = func(alloc ir.Value) bool {
for _, ref := range *alloc.Referrers() {
if ref == x {
continue
}
if ref.Block() != x.Block() {
return false
}
switch ref := ref.(type) {
case *ir.IndexAddr:
idx := ref
if len(*idx.Referrers()) != 1 {
return false
}
store, ok := (*idx.Referrers())[0].(*ir.Store)
if !ok {
return false
}
out = append(out, store.Val)
case *ir.Copy:
if !checkAlloc(ref) {
return false
}
default:
return false
}
}
return true
}
if !checkAlloc(alloc) {
return nil, false
}
return out, true
}
func CallName(call *ir.CallCommon) string {
if call.IsInvoke() {
return ""
}
switch v := call.Value.(type) {
case *ir.Function:
fn, ok := v.Object().(*types.Func)
if !ok {
return ""
}
return typeutil.FuncName(fn)
case *ir.Builtin:
return v.Name()
}
return ""
}
func IsCallTo(call *ir.CallCommon, name string) bool { return CallName(call) == name }
func IsCallToAny(call *ir.CallCommon, names ...string) bool {
q := CallName(call)
for _, name := range names {
if q == name {
return true
}
}
return false
}
func FilterDebug(instr []ir.Instruction) []ir.Instruction {
var out []ir.Instruction
for _, ins := range instr {
if _, ok := ins.(*ir.DebugRef); !ok {
out = append(out, ins)
}
}
return out
}
func IsExample(fn *ir.Function) bool {
if !strings.HasPrefix(fn.Name(), "Example") {
return false
}
f := fn.Prog.Fset.File(fn.Pos())
if f == nil {
return false
}
return strings.HasSuffix(f.Name(), "_test.go")
}
// Flatten recursively returns the underlying value of an ir.Sigma or
// ir.Phi node. If all edges in an ir.Phi node are the same (after
// flattening), the flattened edge will get returned. If flattening is
// not possible, nil is returned.
func Flatten(v ir.Value) ir.Value {
failed := false
seen := map[ir.Value]struct{}{}
var out ir.Value
var dfs func(v ir.Value)
dfs = func(v ir.Value) {
if failed {
return
}
if _, ok := seen[v]; ok {
return
}
seen[v] = struct{}{}
switch v := v.(type) {
case *ir.Sigma:
dfs(v.X)
case *ir.Phi:
for _, e := range v.Edges {
dfs(e)
}
default:
if out == nil {
out = v
} else if out != v {
failed = true
}
}
}
dfs(v)
if failed {
return nil
}
return out
}
|