forked from go-delve/delve
/
source.go
315 lines (289 loc) · 7.73 KB
/
source.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
package source
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
)
type Searcher struct {
fileset *token.FileSet
visited map[string]*ast.File
}
func New() *Searcher {
return &Searcher{fileset: token.NewFileSet(), visited: make(map[string]*ast.File)}
}
type NoNodeError struct {
f string
l int
}
func (n NoNodeError) Error() string {
return fmt.Sprintf("could not find node at %s:%d", n.f, n.l)
}
// Returns the first node at the given file:line.
func (s *Searcher) FirstNodeAt(fname string, line int) (*ast.File, ast.Node, error) {
var node ast.Node
f, err := s.parse(fname)
if err != nil {
return nil, nil, err
}
ast.Inspect(f, func(n ast.Node) bool {
if n == nil {
return true
}
position := s.fileset.Position(n.Pos())
if position.Line == line {
node = n
return false
}
return true
})
if node == nil {
return nil, nil, NoNodeError{f: fname, l: line}
}
return f, node, nil
}
type Done string
func (d Done) Error() string {
return string(d)
}
// Returns all possible lines that could be executed after the given file:line,
// within the same source file.
func (s *Searcher) NextLines(fname string, line int) (lines []int, err error) {
parsedFile, n, err := s.FirstNodeAt(fname, line)
if err != nil {
return lines, nil
}
switch x := n.(type) {
// Follow if statements
case *ast.IfStmt:
lines = removeDuplicateLines(s.parseIfStmtBlock(x, parsedFile))
return
// Follow case statements.
// Append line for first statement following each 'case' condition.
case *ast.SwitchStmt:
var switchEnd int
ast.Inspect(x, func(n ast.Node) bool {
if stmt, ok := n.(*ast.SwitchStmt); ok {
switchEnd = s.fileset.Position(stmt.End()).Line
return true
}
if switchEnd < s.fileset.Position(x.Pos()).Line {
return false
}
if stmt, ok := n.(*ast.CaseClause); ok {
p := stmt.Body[0].Pos()
pos := s.fileset.Position(p)
lines = append(lines, pos.Line)
return false
}
return true
})
lines = removeDuplicateLines(lines)
return
// Default case - find next source line.
default:
lines = removeDuplicateLines(s.parseDefaultBlock(x, parsedFile, line))
return
}
return lines, nil
}
// Parses file named by fname, caching files it has already parsed.
func (s *Searcher) parse(fname string) (*ast.File, error) {
if f, ok := s.visited[fname]; ok {
return f, nil
}
f, err := parser.ParseFile(s.fileset, fname, nil, 0)
if err != nil {
return nil, err
}
s.visited[fname] = f
return f, nil
}
func (s *Searcher) nextLineAfter(parsedFile *ast.File, line int) (nextLine int) {
var done bool
ast.Inspect(parsedFile, func(n ast.Node) bool {
if done || n == nil {
return false
}
pos := s.fileset.Position(n.Pos())
if line < pos.Line {
nextLine = pos.Line
done = true
return false
}
return true
})
return
}
// If we are at an 'if' statement, employ the following algorithm:
// * Follow all 'else if' statements, appending their line number
// * Follow any 'else' statement if it exists, appending the line
// number of the statement following the 'else'.
// * If there is no 'else' statement, append line of first statement
// following the entire 'if' block.
func (s *Searcher) parseIfStmtBlock(ifRoot *ast.IfStmt, parsedFile *ast.File) []int {
var (
rbrace int
ifStmtLine = s.fileset.Position(ifRoot.Body.List[0].Pos()).Line
lines = []int{ifStmtLine}
)
for {
if ifRoot.Else == nil {
// Grab first line after entire 'if' block
rbrace = s.fileset.Position(ifRoot.Body.Rbrace).Line
return append(lines, s.nextLineAfter(parsedFile, rbrace))
}
// Continue following 'else if' branches.
if elseStmt, ok := ifRoot.Else.(*ast.IfStmt); ok {
lines = append(lines, s.fileset.Position(elseStmt.Pos()).Line)
ifRoot = elseStmt
continue
}
// Grab next line after final 'else'.
pos := s.fileset.Position(ifRoot.Else.Pos())
return append(lines, s.nextLineAfter(parsedFile, pos.Line))
}
}
// We are not at a branch, employ the following algorithm:
// * Traverse tree, storing any loop as a parent
// * Find next source line after the given line
// * Check and see if we've passed the scope of any parent we've
// stored. If so, pop them off the stack. The last parent that
// is left get's appending to our list of lines since we could
// end up at the top of the loop again.
func (s *Searcher) parseDefaultBlock(ifRoot ast.Node, parsedFile *ast.File, line int) []int {
var (
found bool
lines []int
parents []*ast.BlockStmt
parentBlockBeginLine int
deferEndLine int
)
ast.Inspect(parsedFile, func(n ast.Node) bool {
if found || n == nil {
return false
}
pos := s.fileset.Position(n.Pos())
if line < pos.Line && deferEndLine != 0 {
p := s.fileset.Position(n.Pos())
if deferEndLine < p.Line {
found = true
lines = append(lines, p.Line)
return false
}
}
if stmt, ok := n.(*ast.ForStmt); ok {
pos := s.fileset.Position(stmt.Pos())
if stmt.Cond == nil {
nextLine := s.fileset.Position(stmt.Body.List[0].Pos()).Line
if line < nextLine {
lines = []int{nextLine}
found = true
parents = nil
return false
}
}
parents = append(parents, stmt.Body)
parentBlockBeginLine = pos.Line
}
if _, ok := n.(*ast.GenDecl); ok {
return true
}
if dn, ok := n.(*ast.DeferStmt); ok && line < pos.Line {
endpos := s.fileset.Position(dn.End())
deferEndLine = endpos.Line
return false
}
if st, ok := n.(*ast.DeclStmt); ok {
beginpos := s.fileset.Position(st.Pos())
endpos := s.fileset.Position(st.End())
if beginpos.Line < endpos.Line {
return true
}
}
// Check to see if we've found the "next" line.
if line < pos.Line {
if _, ok := n.(*ast.BlockStmt); ok {
return true
}
var (
parent *ast.BlockStmt
parentEndLine int
)
for len(parents) > 0 {
parent = parents[len(parents)-1]
// Grab the line number of the right brace of the parent block.
parentEndLine = s.fileset.Position(parent.Rbrace).Line
// Check to see if we're still within the parents block.
// If we are, we're done and that is our parent.
if parentEndLine > line {
parentBlockBeginLine = s.fileset.Position(parent.Pos()).Line
break
}
// If we weren't, and there is only 1 parent, we no longer have one.
if len(parents) == 1 {
parent = nil
break
}
// Remove that parent from the stack.
parents = parents[0 : len(parents)-1]
}
if parent != nil {
var (
endfound bool
beginFound bool
beginLine int
)
ast.Inspect(parsedFile, func(n ast.Node) bool {
if n == nil || endfound {
return false
}
if _, ok := n.(*ast.BlockStmt); ok {
return true
}
pos := s.fileset.Position(n.Pos())
if parentBlockBeginLine < pos.Line && !beginFound {
beginFound = true
beginLine = pos.Line
return true
}
if parentEndLine < pos.Line {
if _, ok := n.(*ast.FuncDecl); !ok {
lines = append(lines, beginLine, pos.Line)
}
endfound = true
return false
}
return true
})
lines = append(lines, parentBlockBeginLine)
}
switch n.(type) {
case *ast.BranchStmt, *ast.FuncDecl:
default:
lines = append(lines, pos.Line)
}
found = true
return false
}
return true
})
if len(lines) == 0 && 0 < len(parents) {
parent := parents[len(parents)-1]
lbrace := s.fileset.Position(parent.Lbrace).Line
pos := s.fileset.Position(parent.List[0].Pos())
lines = append(lines, lbrace, pos.Line)
}
return lines
}
func removeDuplicateLines(lines []int) []int {
nl := make([]int, 0, len(lines))
fnd := make(map[int]bool)
for _, l := range lines {
if _, ok := fnd[l]; !ok {
fnd[l] = true
nl = append(nl, l)
}
}
return nl
}