-
Notifications
You must be signed in to change notification settings - Fork 4
/
apply.go
177 lines (144 loc) · 4.53 KB
/
apply.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
// Copyright 2018-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package fordefer
import (
"go/ast"
"go/token"
"golang.org/x/tools/go/ast/astutil"
)
func pre(c *astutil.Cursor) bool {
currentNode := c.Node()
parentNode := c.Parent()
// Search for RangeStmt or ForStmt
switch n := currentNode.(type) {
case *ast.FuncLit, *ast.FuncDecl:
// We encountered a "func() {}" or "func main() {"
forLevels.add(n)
case *ast.ExprStmt:
// "fordefer" statement
fordef := isExprStmtAForDefer(n)
if fordef != other {
forLoop := forloops.current()
if forLoop != nil {
forIdentifier := forLoop.id
nextIdx := c.Index() + 1
nextStmt := parentNode.(*ast.BlockStmt).List[nextIdx]
lookup[forLoop.node].count++
if fordef == fordeferStd {
morphFordefer(forIdentifier, n, nextStmt, parentNode, false, nextIdx)
} else if fordef == fordeferGo {
morphFordefer(forIdentifier, n, nextStmt, parentNode, true, nextIdx)
}
}
}
case *ast.SelectStmt, *ast.SwitchStmt:
breakParents.add(n)
case *ast.BranchStmt:
switch n.Tok {
case token.BREAK:
if n.Label == nil && breakParents.current() == notForStmt {
// If there is no label and it's inside a switch/select, then ignore it.
return true
}
fallthrough
case token.CONTINUE:
if n.Label == nil {
// no label for break or continue
forLoop := forloops.current()
if forLoop != nil {
// Add an unwind above the break/continue statement
c.InsertBefore(&ast.ExprStmt{X: stackUnwind(&ast.Ident{Name: forLoop.id})})
}
} else {
// label scenario
label := n.Label.Name
forLoop := forloops.current()
if forLoop != nil {
cForInfo := lookup[forLoop.node]
if cForInfo != nil {
c.InsertBefore(&ast.ExprStmt{X: stackUnwind(&ast.Ident{Name: cForInfo.identifier})})
cForInfo = cForInfo.parent
for cForInfo != nil {
// Inspect the parent
if cForInfo.label == nil {
c.InsertBefore(&ast.ExprStmt{X: stackUnwind(&ast.Ident{Name: cForInfo.identifier})})
cForInfo = cForInfo.parent
} else {
c.InsertBefore(&ast.ExprStmt{X: stackUnwind(&ast.Ident{Name: cForInfo.identifier})})
if *cForInfo.label == label {
// We arrived at last "for" loop
break
} else {
cForInfo = cForInfo.parent
}
}
}
}
}
}
case token.GOTO:
// TODO: Very complex. See: https://github.com/golang/go/issues/26058
}
case *ast.LabeledStmt:
// Check if child is RangeStmt or ForStmt
switch forStmt := n.Stmt.(type) {
case *ast.RangeStmt, *ast.ForStmt:
forID := insertStackAssignment(c) // Insert stack creation above "for" loop
addForLoopToLookup(forStmt, forLevels.current(), forloops.current(), &n.Label.Name, forID.Name)
// Add forloop to stack
forloops.add(forID.Name, forStmt)
breakParents.add(forStmt)
// Insert Unwind @ end of "for" loop
if rs, ok := forStmt.(*ast.RangeStmt); ok {
rs.Body.List = append(rs.Body.List, &ast.ExprStmt{X: stackUnwind(forID)})
}
if fs, ok := forStmt.(*ast.ForStmt); ok {
fs.Body.List = append(fs.Body.List, &ast.ExprStmt{X: stackUnwind(forID)})
}
}
case *ast.RangeStmt, *ast.ForStmt:
// Check if the for loop has a label above it
labelAbove := checkIfLabelAbove(parentNode)
if !labelAbove {
forID := insertStackAssignment(c) // Insert stack creation above "for" loop
addForLoopToLookup(n, forLevels.current(), forloops.current(), nil, forID.Name)
// Add forloop to stack
forloops.add(forID.Name, n)
breakParents.add(n)
// Insert Unwind @ end of "for" loop
if rs, ok := n.(*ast.RangeStmt); ok {
rs.Body.List = append(rs.Body.List, &ast.ExprStmt{X: stackUnwind(forID)})
}
if fs, ok := n.(*ast.ForStmt); ok {
fs.Body.List = append(fs.Body.List, &ast.ExprStmt{X: stackUnwind(forID)})
}
}
}
return true
}
func post(c *astutil.Cursor) bool {
currentNode := c.Node()
parentNode := c.Parent()
switch n := currentNode.(type) {
case *ast.FuncLit, *ast.FuncDecl:
forLevels.remove()
case *ast.SelectStmt, *ast.SwitchStmt:
breakParents.remove()
case *ast.LabeledStmt:
// Check if child is RangeStmt or ForStmt
switch n.Stmt.(type) {
case *ast.RangeStmt, *ast.ForStmt:
// Pop "for" loop from stack
forloops.remove()
breakParents.remove()
}
case *ast.RangeStmt, *ast.ForStmt:
// Check if the for loop has a label above it
labelAbove := checkIfLabelAbove(parentNode)
if !labelAbove {
// Pop "for" loop from stack
forloops.remove()
breakParents.remove()
}
}
return true
}