/
util.go
225 lines (184 loc) · 4.32 KB
/
util.go
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package sqlrowsutil
import (
"go/types"
"golang.org/x/tools/go/ssa"
)
// CalledChecker checks a function is called.
// See From and Func.
type CalledChecker struct {
Ignore func(instr ssa.Instruction) bool
}
// Func returns true when f is called in the instr.
// If recv is not nil, Called also checks the receiver.
func (c *CalledChecker) Func(instr ssa.Instruction, recv ssa.Value, f *types.Func) bool {
if c.Ignore != nil && c.Ignore(instr) {
return false
}
call, ok := instr.(ssa.CallInstruction)
if !ok {
return false
}
common := call.Common()
if common == nil {
return false
}
callee := common.StaticCallee()
if callee == nil {
return false
}
fn, ok := callee.Object().(*types.Func)
if !ok {
return false
}
if recv != nil &&
common.Signature().Recv() != nil &&
(len(common.Args) == 0 || common.Args[0] != recv) {
return false
}
return fn == f
}
// From checks whether receiver's method is called in an instruction
// which belongs to after i-th instructions, or in successor blocks of b.
// The first result is above value.
// The second result is whether type of i-th instruction does not much receiver
// or matches with ignore cases.
func (c *CalledChecker) From(b *ssa.BasicBlock, i int, receiver types.Type, methods ...*types.Func) (called, ok bool) {
if b == nil || i < 0 || i >= len(b.Instrs) ||
receiver == nil || len(methods) == 0 {
return false, false
}
v, ok := b.Instrs[i].(ssa.Value)
if !ok {
return false, false
}
if !identical(v.Type(), receiver) {
return false, false
}
from := &calledFrom{recv: v, fs: methods, ignore: c.Ignore}
if from.ignored() {
return false, false
}
if from.instrs(b.Instrs[i:]) ||
from.succs(b) {
return true, true
}
return false, true
}
type calledFrom struct {
recv ssa.Value
fs []*types.Func
done map[*ssa.BasicBlock]bool
ignore func(ssa.Instruction) bool
}
func (c *calledFrom) ignored() bool {
refs := c.recv.Referrers()
if refs == nil {
return false
}
for _, ref := range *refs {
if !c.isOwn(ref) &&
((c.ignore != nil && c.ignore(ref)) ||
c.isRet(ref) || c.isArg(ref)) {
return true
}
}
return false
}
func (c *calledFrom) isOwn(instr ssa.Instruction) bool {
v, ok := instr.(ssa.Value)
if !ok {
return false
}
return v == c.recv
}
func (c *calledFrom) isRet(instr ssa.Instruction) bool {
ret, ok := instr.(*ssa.Return)
if !ok {
return false
}
for _, r := range ret.Results {
if r == c.recv {
return true
}
}
return false
}
func (c *calledFrom) isArg(instr ssa.Instruction) bool {
call, ok := instr.(ssa.CallInstruction)
if !ok {
return false
}
common := call.Common()
if common == nil {
return false
}
args := common.Args
if common.Signature().Recv() != nil {
args = args[1:]
}
for i := range args {
if args[i] == c.recv {
return true
}
}
return false
}
func (c *calledFrom) instrs(instrs []ssa.Instruction) bool {
for _, instr := range instrs {
for _, f := range c.fs {
if Called(instr, c.recv, f) {
return true
}
}
}
return false
}
func (c *calledFrom) succs(b *ssa.BasicBlock) bool {
if c.done == nil {
c.done = map[*ssa.BasicBlock]bool{}
}
if c.done[b] {
return false
}
c.done[b] = true
if len(b.Succs) == 0 {
return false
}
var called bool
for _, s := range b.Succs {
if c.instrs(s.Instrs) || c.succs(s) {
called = true
}
}
return called
}
// CalledFrom checks whether receiver's method is called in an instruction
// which belongs to after i-th instructions, or in successor blocks of b.
// The first result is above value.
// The second result is whether type of i-th instruction does not much receiver
// or matches with ignore cases.
func CalledFrom(b *ssa.BasicBlock, i int, receiver types.Type, methods ...*types.Func) (called, ok bool) {
return new(CalledChecker).From(b, i, receiver, methods...)
}
// Called returns true when f is called in the instr.
// If recv is not nil, Called also checks the receiver.
func Called(instr ssa.Instruction, recv ssa.Value, f *types.Func) bool {
return new(CalledChecker).Func(instr, recv, f)
}
// see: https://github.com/golang/go/issues/19670
func identical(x, y types.Type) (ret bool) {
defer func() {
r := recover()
switch r := r.(type) {
case string:
if r == "unreachable" {
ret = false
return
}
case nil:
return
}
panic(r)
}()
return types.Identical(x, y)
}