-
Notifications
You must be signed in to change notification settings - Fork 6
/
call.go
405 lines (338 loc) · 8.19 KB
/
call.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package analysisutil
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
}
// NotIn checks whether receiver's method is called in a function.
// If there is no methods calling at a path from an instruction
// which type is receiver to all return instruction, NotIn returns these instructions.
func (c *CalledChecker) NotIn(f *ssa.Function, receiver types.Type, methods ...*types.Func) []ssa.Instruction {
done := map[ssa.Value]bool{}
var instrs []ssa.Instruction
for _, b := range f.Blocks {
for i, instr := range b.Instrs {
v, _ := instr.(ssa.Value)
if v == nil || done[v] {
continue
}
if v, _ := v.(*ssa.UnOp); v != nil && done[v.X] {
continue
}
called, ok := c.From(b, i, receiver, methods...)
if ok && !called {
instrs = append(instrs, instr)
done[v] = true
if v, _ := v.(*ssa.UnOp); v != nil {
done[v.X] = true
}
}
}
}
return instrs
}
// Func returns true when f is called in the instr.
// If recv is not nil, Func 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 && recv != nil || common.Args[0] != recv &&
!referrer(recv, common.Args[0])) {
return false
}
return fn == f
}
func referrer(a, b ssa.Value) bool {
return isReferrerOf(a, b) || isReferrerOf(b, a)
}
func isReferrerOf(a, b ssa.Value) bool {
if a == nil || b == nil {
return false
}
if b.Referrers() != nil {
brs := *b.Referrers()
for _, br := range brs {
brv, ok := br.(ssa.Value)
if !ok {
continue
}
if brv == a {
return true
}
}
}
return false
}
// From checks whether receiver's method is called in an instruction
// which belogns to after i-th instructions, or in succsor 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
}
from := &calledFrom{recv: v, fs: methods, ignore: c.Ignore}
if !from.isRecv(receiver, v.Type()) {
return false, false
}
if from.ignored() {
return false, false
}
if from.instrs(b.Instrs[i+1:]) ||
from.succs(b) {
return true, true
}
from.done = nil
if from.storedInInstrs(b.Instrs[i+1:]) ||
from.storedInSuccs(b) {
return false, false
}
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 {
switch v := c.recv.(type) {
case *ssa.UnOp:
switch v.X.(type) {
case *ssa.FreeVar, *ssa.Global:
return true
}
}
refs := c.recv.Referrers()
if refs == nil {
return false
}
for _, ref := range *refs {
done := map[ssa.Instruction]bool{}
if !c.isOwn(ref) &&
((c.ignore != nil && c.ignore(ref)) ||
c.isRet(ref, done) || 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, done map[ssa.Instruction]bool) bool {
if done[instr] {
return false
}
done[instr] = true
switch instr := instr.(type) {
case *ssa.Return:
return true
case *ssa.MapUpdate:
return c.isRetInRefs(instr.Map, done)
case *ssa.Store:
if instr, _ := instr.Addr.(ssa.Instruction); instr != nil {
return c.isRet(instr, done)
}
return c.isRetInRefs(instr.Addr, done)
case *ssa.FieldAddr:
return c.isRetInRefs(instr.X, done)
case ssa.Value:
return c.isRetInRefs(instr, done)
default:
return false
}
}
func (c *calledFrom) isRetInRefs(v ssa.Value, done map[ssa.Instruction]bool) bool {
refs := v.Referrers()
if refs == nil {
return false
}
for _, ref := range *refs {
if c.isRet(ref, done) {
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 true
}
c.done[b] = true
if len(b.Succs) == 0 {
return false
}
for _, s := range b.Succs {
if !c.instrs(s.Instrs) && !c.succs(s) {
return false
}
}
return true
}
func (c *calledFrom) storedInInstrs(instrs []ssa.Instruction) bool {
for _, instr := range instrs {
switch instr := instr.(type) {
case *ssa.Store:
if instr.Val == c.recv {
return true
}
}
}
return false
}
func (c *calledFrom) storedInSuccs(b *ssa.BasicBlock) bool {
if c.done == nil {
c.done = map[*ssa.BasicBlock]bool{}
}
if c.done[b] {
return true
}
c.done[b] = true
if len(b.Succs) == 0 {
return false
}
for _, s := range b.Succs {
if !c.storedInInstrs(s.Instrs) && !c.succs(s) {
return false
}
}
return true
}
func (c *calledFrom) isRecv(recv, typ types.Type) bool {
return recv == typ || identical(recv, typ) ||
c.isRecvInTuple(recv, typ) || c.isRecvInEmbedded(recv, typ)
}
func (c *calledFrom) isRecvInTuple(recv, typ types.Type) bool {
tuple, _ := typ.(*types.Tuple)
if tuple == nil {
return false
}
for i := 0; i < tuple.Len(); i++ {
if c.isRecv(recv, tuple.At(i).Type()) {
return true
}
}
return false
}
func (c *calledFrom) isRecvInEmbedded(recv, typ types.Type) bool {
var st *types.Struct
switch typ := typ.(type) {
case *types.Struct:
st = typ
case *types.Pointer:
return c.isRecvInEmbedded(recv, typ.Elem())
case *types.Named:
return c.isRecvInEmbedded(recv, typ.Underlying())
default:
return false
}
for i := 0; i < st.NumFields(); i++ {
field := st.Field(i)
if !field.Embedded() {
continue
}
ft := field.Type()
if c.isRecv(recv, ft) {
return true
}
var ptrOrUnptr types.Type
switch ft := ft.(type) {
case *types.Pointer:
// struct { *T } -> T
ptrOrUnptr = ft.Elem()
default:
// struct { T } -> *T
ptrOrUnptr = types.NewPointer(ft)
}
if c.isRecv(recv, ptrOrUnptr) {
return true
}
}
return false
}
// NotCalledIn checks whether receiver's method is called in a function.
// If there is no methods calling at a path from an instruction
// which type is receiver to all return instruction, NotCalledIn returns these instructions.
func NotCalledIn(f *ssa.Function, receiver types.Type, methods ...*types.Func) []ssa.Instruction {
return new(CalledChecker).NotIn(f, receiver, methods...)
}
// CalledFrom checks whether receiver's method is called in an instruction
// which belogns to after i-th instructions, or in succsor 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)
}