/
source.go
157 lines (133 loc) · 3.54 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
package tma
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"os"
"runtime"
)
func getCallSource() (messageCallSource, error) {
_, filename, line, ok := runtime.Caller(2)
if !ok {
panic("failed to get call stack")
}
src, err := readFile(filename)
if err != nil {
return messageCallSource{}, fmt.Errorf("failed to read Go source file: %w", err)
}
callSource, err := getNodeAtLine(src, line)
if err != nil {
return messageCallSource{}, fmt.Errorf("failed to lookup call expression: %w", err)
}
return callSource, nil
}
type fileSource struct {
FileSet *token.FileSet
AST *ast.File
}
// TODO: cache with sync.DoOnce
func readFile(filename string) (fileSource, error) {
fileset := token.NewFileSet()
astFile, err := parser.ParseFile(fileset, filename, nil, parser.AllErrors|parser.ParseComments)
if err != nil {
return fileSource{}, fmt.Errorf("failed to read source file %s: %w", filename, err)
}
return fileSource{FileSet: fileset, AST: astFile}, nil
}
type messageCallSource struct {
FileSet *token.FileSet
File *ast.File
CallExpr *ast.CallExpr
CallComments []*ast.CommentGroup
IfStmt *ast.IfStmt
IfStmtComments []*ast.CommentGroup
}
// TODO: defer support was removed
func getNodeAtLine(src fileSource, lineNum int) (messageCallSource, error) {
result := scanToLine(src, lineNum)
if result.CallExpr == nil || result.IfStmt == nil {
return result, fmt.Errorf("failed to find an expression on line")
}
debug("found node: %s", debugFormatNode{result.IfStmt})
return result, nil
}
func scanToLine(src fileSource, lineNum int) messageCallSource {
fileset := src.FileSet
cmap := ast.NewCommentMap(src.FileSet, src.AST, src.AST.Comments)
var result messageCallSource
result.File = src.AST
result.FileSet = src.FileSet
pre := func(current ast.Node) bool {
if current == nil || result.CallExpr != nil {
return false
}
if fileset.Position(current.Pos()).Line > lineNum {
return false // past the relevant scope
}
if fileset.Position(current.End()).Line < lineNum {
return true // before the relevant scope
}
if ifStmt, ok := current.(*ast.IfStmt); ok {
result.IfStmt = ifStmt
result.IfStmtComments = cmap[ifStmt]
}
if fileset.Position(current.End()).Line != lineNum {
return true // not yet at call expression
}
if len(result.CallComments) == 0 {
result.CallComments = cmap[current]
}
ce, ok := current.(*ast.CallExpr)
if !ok {
return true // not yet at call expression
}
// TODO: use type system instead of name match
se, ok := ce.Fun.(*ast.SelectorExpr)
if !ok {
return true // not a call to vt.Message
}
switch se.Sel.Name {
case "Got", "GotWant":
default:
return true
}
x, ok := se.X.(*ast.Ident)
if !ok {
return true
}
if x.Name != "tma" {
return true
}
result.CallExpr = ce
return false
}
ast.Inspect(src.AST, pre)
return result
}
func debug(format string, args ...interface{}) {
if os.Getenv("TEST_DEBUG") != "" {
fmt.Fprintf(os.Stderr, "DEBUG: "+format+"\n", args...)
}
}
type debugFormatNode struct {
ast.Node
}
func (n debugFormatNode) String() string {
if n.Node == nil {
return "none"
}
out, err := formatNode(n.Node)
if err != nil {
return fmt.Sprintf("failed to format %s: %s", n.Node, err)
}
return fmt.Sprintf("(%T) %s", n.Node, out)
}
// formatNode formats the node using go/format.Node and return the result as a string
func formatNode(node ast.Node) (string, error) {
buf := new(bytes.Buffer)
err := format.Node(buf, token.NewFileSet(), node)
return buf.String(), err
}