-
Notifications
You must be signed in to change notification settings - Fork 2
/
trace.go
482 lines (437 loc) · 9.38 KB
/
trace.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package main
import (
"go/ast"
"go/token"
"strings"
)
var contextMarker = &StateUpdate{isContext: true}
type ExecTrace struct {
md *MethodDecl
parent *ExecTrace
fs *File
traced map[string]*StateUpdate
usages map[string]struct{}
conds []ast.Expr
inverted bool
migration ast.Expr
errorHandler ast.Expr
stepFlags ast.Expr
}
func (p *ExecTrace) isTraced(n string) bool {
switch {
case p.traced != nil:
return p.traced[n] != nil
case p.parent != nil:
return p.parent.isTraced(n)
default:
return p.md.CtxArg == n
}
}
func (p *ExecTrace) getTraced(n string) *StateUpdate {
switch {
case p.traced != nil:
return p.traced[n]
case p.parent != nil:
return p.parent.getTraced(n)
case p.md.CtxArg == n:
return contextMarker
default:
return nil
}
}
func (p *ExecTrace) _copyInherited() map[string]*StateUpdate {
switch {
case p.traced != nil:
cp := make(map[string]*StateUpdate)
for k, v := range p.traced {
if v != nil {
cp[k] = v
}
}
return cp
case p.parent != nil:
return p.parent._copyInherited()
case p.md.CtxArg != "":
return map[string]*StateUpdate{p.md.CtxArg: contextMarker}
default:
return map[string]*StateUpdate{}
}
}
func (p *ExecTrace) setTraced(n string, upd *StateUpdate) {
if p.traced == nil {
if upd == nil && !p.isTraced(n) {
return
}
p.traced = p._copyInherited()
}
if upd == nil {
delete(p.traced, n)
} else {
p.traced[n] = upd
}
}
func (p *ExecTrace) remapContextNames(lhs []string, rhs []exprResult) {
switch {
case len(lhs) == 0:
return
case len(rhs) > 0:
hasRhs := false
for i, n := range lhs {
switch {
case n == "":
continue
case i >= len(rhs):
// multi-arg return from func
break
case rhs[i].name != nil:
if traced := p.getTraced(*rhs[i].name); traced != nil {
hasRhs = true
rhs[i].upd = traced
}
}
}
if !hasRhs {
break
}
for i, n := range lhs {
switch {
case n == "":
continue
case len(rhs) == 1:
// multi-arg return from func
p.setTraced(n, rhs[0].upd)
default:
p.setTraced(n, rhs[i].upd)
}
}
return
}
for _, n := range lhs {
if n != "" {
p.setTraced(n, nil)
}
}
}
func (p *ExecTrace) spawn() *ExecTrace {
return &ExecTrace{md: p.md, parent: p, fs: p.fs, migration: p.migration, errorHandler: p.errorHandler, stepFlags: p.stepFlags}
}
func (p *ExecTrace) spawnCase(conds []ast.Expr) *ExecTrace {
et := p.spawn()
et.conds = conds
return et
}
func (p *ExecTrace) spawnIf(cond ast.Expr, inverted bool) *ExecTrace {
et := p.spawn()
et.conds = []ast.Expr{cond}
et.inverted = inverted
return et
}
func (p *ExecTrace) nearestCond() *ExecTrace {
if len(p.conds) > 0 {
return p
}
if p.parent != nil {
return p.parent.nearestCond()
}
return nil
}
func (p *ExecTrace) parseBlockStmt(stmt *ast.BlockStmt) {
if stmt == nil {
return
}
p.parseStatements(stmt.List)
}
func (p *ExecTrace) parseStatements(list []ast.Stmt) {
if len(list) == 0 {
return
}
et := p.spawn()
su := et._parseStatements(list)
p.collectUsages(et)
p.migration, p.errorHandler, p.stepFlags = et.migration, et.errorHandler, et.stepFlags
if su != nil {
p.addTransition(su)
}
}
func (p *ExecTrace) collectUsages(from *ExecTrace) {
for k := range from.usages {
if p.usages == nil {
p.usages = make(map[string]struct{})
}
p.usages[k] = struct{}{}
}
}
func (p *ExecTrace) _parseDecl(decl *ast.GenDecl) {
switch decl.Tok {
case token.CONST:
for _, spec := range decl.Specs {
vSpec := spec.(*ast.ValueSpec)
for _, n := range vSpec.Names {
p.setTraced(n.Name, nil)
}
}
case token.VAR:
for _, spec := range decl.Specs {
vSpec := spec.(*ast.ValueSpec)
p.remapContextNames(p.identToNames(vSpec.Names), p.exprToValues(vSpec.Values))
}
case token.TYPE:
for _, spec := range decl.Specs {
vSpec := spec.(*ast.TypeSpec)
if vSpec.Name != nil {
p.setTraced(vSpec.Name.Name, nil)
}
}
}
}
func (p *ExecTrace) _parseStatements(list []ast.Stmt) *StateUpdate {
for _, stmt := range list {
for {
switch op := stmt.(type) {
case *ast.DeclStmt:
switch decl := op.Decl.(type) {
case *ast.GenDecl:
p._parseDecl(decl)
case *ast.FuncDecl:
// skip
}
case *ast.LabeledStmt:
stmt = op.Stmt
continue
case *ast.ExprStmt:
p.parseCallToCtx(op.X)
case *ast.AssignStmt:
p.remapContextNames(p.exprToNames(op.Lhs), p.exprToValues(op.Rhs))
case *ast.ReturnStmt:
switch {
case len(op.Results) == 0:
// named return params
// TODO not implemented
case p.md.UpdateIdx == 0:
// this is a non-context func
return p.exprToResult(op.Results[0])
case p.md.UpdateIdx > len(op.Results):
default:
return p.exprToResult(op.Results[p.md.UpdateIdx-1])
}
return nil
case *ast.BranchStmt:
// BREAK, CONTINUE, GOTO, FALLTHROUGH
// These will be handled by other traces, yet we can loose info about setting
// TODO warn when specific context settings are present
return nil
case *ast.BlockStmt:
p.parseStatements(op.List)
case *ast.IfStmt:
if op.Body != nil && len(op.Body.List) > 0 {
p.spawnIf(op.Cond, false).parseStatements(op.Body.List)
}
if op.Else != nil {
p.spawnIf(op.Cond, true).parseStatements([]ast.Stmt{op.Else})
}
case *ast.CaseClause:
if len(op.Body) > 0 {
p.spawnCase(op.List).parseStatements(op.Body)
}
case *ast.SwitchStmt:
p.parseBlockStmt(op.Body)
case *ast.TypeSwitchStmt:
p.parseBlockStmt(op.Body)
case *ast.CommClause:
body := op.Body
switch {
case op.Comm == nil:
case len(body) == 0:
body = []ast.Stmt{op.Comm}
default:
body = make([]ast.Stmt, 1, len(op.Body)+1)
body[0] = op.Comm
body = append(body, op.Body...)
}
p.parseStatements(body)
case *ast.SelectStmt:
p.parseBlockStmt(op.Body)
case *ast.ForStmt:
p.parseBlockStmt(op.Body)
case *ast.RangeStmt:
p.parseBlockStmt(op.Body)
case *ast.DeferStmt:
// op.Call
// TODO defer
}
break
}
}
return nil
}
type exprResult struct {
name *string
upd *StateUpdate
}
func (p *ExecTrace) exprToNames(exprs []ast.Expr) []string {
if len(exprs) == 0 {
return nil
}
s := make([]string, len(exprs))
count := 0
for i, ex := range exprs {
switch x, sel := getSelectorOfExpr(ex); {
case x != "":
case sel == "":
default:
count++
s[i] = sel
}
}
if count == 0 {
return nil
}
return s
}
func (p *ExecTrace) exprToResult(expr ast.Expr) *StateUpdate {
switch mt := p.md.MType; {
case mt.HasStateUpdate():
case mt.HasContextArg():
if op, ok := expr.(*ast.UnaryExpr); ok {
if op.Op != token.AND {
break
}
expr = op.X
}
if op, ok := expr.(*ast.CompositeLit); ok {
sel := p.getInlineFuncExpr(op, 0)
if sel != "" {
return newStateUpdate(nil, sel)
}
}
}
return p.exprToValue(expr)
}
func (p *ExecTrace) exprToValue(expr ast.Expr) *StateUpdate {
switch arg := expr.(type) {
case *ast.SelectorExpr:
sel := ""
if arg.Sel != nil {
sel = arg.Sel.Name
}
if parent := p.exprToValue(arg.X); parent != nil {
return newStateUpdate(parent, sel)
}
return nil
case *ast.CallExpr:
call := p.exprToValue(arg.Fun)
// TODO check for context in args
if call != nil {
call.isCall = true
call.args = arg.Args
}
return call
case *ast.Ident:
su := p.getTraced(arg.Name)
if su != nil {
return su
}
return &StateUpdate{name: arg.Name}
}
return nil
}
func (p *ExecTrace) parseCallToCtx(expr ast.Expr) {
switch arg := expr.(type) {
case *ast.CallExpr:
call := p.exprToValue(arg.Fun)
switch {
case call == nil:
return
case call.parent != contextMarker:
p.lookForAdapterCall(call)
return
case len(arg.Args) != 1:
return
}
switch call.name {
case "SetDefaultMigration":
p.migration = arg.Args[0]
case "SetDefaultFlags":
p.stepFlags = arg.Args[0]
case "SetDefaultErrorHandler":
p.errorHandler = arg.Args[0]
}
if p.usages == nil {
p.usages = make(map[string]struct{})
}
p.usages[call.name] = struct{}{}
}
}
func (p *ExecTrace) lookForAdapterCall(su *StateUpdate) {
if len(su.args) != 0 {
return
}
switch su.name {
case "Start":
case "Send":
default:
return
}
if prep, adapter := p._extractAdapterCall(su); prep != nil {
p.addAdapterCall(prep, adapter)
}
}
func (p *ExecTrace) _extractAdapterCall(su *StateUpdate) (*StateUpdate, *StateUpdate) {
top := su
for su = su.parent; su.HasName(); su = su.parent {
if strings.HasPrefix(su.name, "Prepare") {
if p.hasContextArg(su.args) >= 0 {
return top, su
}
return nil, nil
}
}
return nil, nil
}
func (p *ExecTrace) hasContextArg(args []ast.Expr) int {
for i, arg := range args {
if p.isContextArg(arg) {
return i
}
}
return -1
}
func (p *ExecTrace) isContextArg(arg ast.Expr) bool {
return p.exprToValue(arg) == contextMarker
}
func (p *ExecTrace) exprToValues(exprs []ast.Expr) []exprResult {
if len(exprs) == 0 {
return nil
}
s := make([]exprResult, len(exprs))
count := 0
for i, expr := range exprs {
su := p.exprToValue(expr)
if su != nil {
count++
s[i].upd = su
continue
}
switch x, sel := getSelectorOfExpr(expr); {
case x != "":
case sel == "":
default:
count++
s[i].name = &sel
}
}
if count == 0 {
return nil
}
return s
}
func (p *ExecTrace) identToNames(names []*ast.Ident) []string {
if len(names) == 0 {
return nil
}
s := make([]string, len(names))
for i, n := range names {
s[i] = n.Name
}
return s
}