-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathreason.go
More file actions
229 lines (198 loc) · 6.26 KB
/
reason.go
File metadata and controls
229 lines (198 loc) · 6.26 KB
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
package determinism
import (
"encoding/gob"
"fmt"
"go/token"
"go/types"
"os"
"path/filepath"
"strconv"
"strings"
)
// PackageNonDeterminisms contains func/var non-determinisms keyed by name.
type PackageNonDeterminisms map[string]NonDeterminisms
// AFact is for implementing golang.org/x/tools/go/analysis.Fact.
func (*PackageNonDeterminisms) AFact() {}
func (n *PackageNonDeterminisms) String() string {
if n == nil || len(*n) == 0 {
return "0 non-deterministic vars/funcs"
} else if len(*n) == 1 {
return "1 non-deterministic var/func"
}
return strconv.Itoa(len(*n)) + " non-deterministic vars/funcs"
}
// NonDeterminisms is a set of reasons why a function/var is non-deterministic.
type NonDeterminisms []Reason
// AFact is for implementing golang.org/x/tools/go/analysis.Fact.
func (*NonDeterminisms) AFact() {}
// String returns all reasons as a comma-delimited string.
func (n *NonDeterminisms) String() string {
if n == nil {
return "<none>"
}
var str string
for _, reason := range *n {
if str != "" {
str += ", "
}
str += reason.String()
}
return str
}
// AppendChildReasonLines appends to lines the set of reasons in this slice.
// This will include newlines and indention based on depth.
func (n NonDeterminisms) AppendChildReasonLines(
subject string,
s []string,
depth int,
depthRepeat string,
includePos bool,
pkg *types.Package,
lookupCache *PackageLookupCache,
seenPos map[string]bool,
) []string {
for _, reason := range n {
reasonStr := reason.String()
// Relativize path if it at least starts with working dir
pos := reason.Pos()
filename := pos.Filename
if wd, err := os.Getwd(); err == nil && strings.HasPrefix(filename, wd) {
if relFilename, err := filepath.Rel(wd, filename); err == nil {
filename = relFilename
}
}
posStr := fmt.Sprintf("%v:%v:%v", filename, pos.Line, pos.Column)
if includePos {
reasonStr += " at " + posStr
}
s = append(s, fmt.Sprintf("%v is non-deterministic, reason: %v",
strings.Repeat(depthRepeat, depth)+subject, reasonStr))
// Recurse if func call and we haven't seen this pos str before
seen := seenPos[posStr]
seenPos[posStr] = true
if funcCall, _ := reason.(*ReasonFuncCall); funcCall != nil && !seen {
childPkg, childPkgNonDet := lookupCache.PackageNonDeterminismsFromName(pkg, funcCall.PackageName())
if childNonDet := childPkgNonDet[funcCall.FuncName]; len(childNonDet) > 0 {
s = childNonDet.AppendChildReasonLines(funcCall.FuncName, s, depth+1,
depthRepeat, includePos, childPkg, lookupCache, seenPos)
}
}
}
return s
}
// Reason represents a reason for non-determinism.
type Reason interface {
Pos() *token.Position
// String is expected to just include the brief reason, not any child reasons.
String() string
}
// ReasonDecl represents a function or var that was explicitly marked
// non-deterministic via config.
type ReasonDecl struct {
SourcePos *token.Position
}
// Pos returns the source position.
func (r *ReasonDecl) Pos() *token.Position { return r.SourcePos }
// String returns the reason.
func (r *ReasonDecl) String() string {
return "declared non-deterministic"
}
// ReasonFuncCall represents a call to a non-deterministic function.
type ReasonFuncCall struct {
SourcePos *token.Position
// Fully qualified name
FuncName string
}
// Pos returns the source position.
func (r *ReasonFuncCall) Pos() *token.Position { return r.SourcePos }
// String returns the reason.
func (r *ReasonFuncCall) String() string {
return "calls non-deterministic function " + r.FuncName
}
func (r *ReasonFuncCall) PackageName() string {
pkgPrefixedName := r.FuncName
// If there is an ending parenthesis, it's a method; take the receiver as the name
if endParen := strings.Index(r.FuncName, ")"); endParen >= 0 {
pkgPrefixedName = strings.TrimLeft(r.FuncName[:endParen], "(*")
}
// Take up until the last dot as the package name
lastDot := strings.LastIndex(pkgPrefixedName, ".")
if lastDot == -1 {
return pkgPrefixedName
}
return pkgPrefixedName[:lastDot]
}
// ReasonVarAccess represents accessing a non-deterministic global variable.
type ReasonVarAccess struct {
SourcePos *token.Position
// Fully qualified name
VarName string
}
// Pos returns the source position.
func (r *ReasonVarAccess) Pos() *token.Position { return r.SourcePos }
// String returns the reason.
func (r *ReasonVarAccess) String() string {
return "accesses non-deterministic var " + r.VarName
}
// ReasonConcurrency represents a non-deterministic concurrency construct.
type ReasonConcurrency struct {
SourcePos *token.Position
Kind ConcurrencyKind
}
// Pos returns the source position.
func (r *ReasonConcurrency) Pos() *token.Position { return r.SourcePos }
// String returns the reason.
func (r *ReasonConcurrency) String() string {
switch r.Kind {
case ConcurrencyKindGo:
return "starts goroutine"
case ConcurrencyKindRecv:
return "receives from channel"
case ConcurrencyKindSend:
return "sends to channel"
case ConcurrencyKindRange:
return "iterates over channel"
default:
return "<unknown-kind>"
}
}
// ConcurrencyKind is a construct that is non-deterministic for
// ReasonConcurrency.
type ConcurrencyKind int
const (
ConcurrencyKindGo ConcurrencyKind = iota
ConcurrencyKindRecv
ConcurrencyKindSend
ConcurrencyKindRange
)
// ReasonMapRange represents iterating over a map via range.
type ReasonMapRange struct {
SourcePos *token.Position
}
// Pos returns the source position.
func (r *ReasonMapRange) Pos() *token.Position { return r.SourcePos }
// String returns the reason.
func (r *ReasonMapRange) String() string {
return "iterates over map"
}
// ReasonAnonymousFunc represents passing an anonymous function to an API
// that requires deterministic function names.
type ReasonAnonymousFunc struct {
SourcePos *token.Position
FuncName string
}
// Pos returns the source position.
func (r *ReasonAnonymousFunc) Pos() *token.Position { return r.SourcePos }
// String returns the reason.
func (r *ReasonAnonymousFunc) String() string {
return "anonymous function passed to " + r.FuncName + " that has a non-deterministic function name"
}
func init() {
// Needed for go vet usage
gob.Register(&ReasonDecl{})
gob.Register(&ReasonFuncCall{})
gob.Register(&ReasonVarAccess{})
gob.Register(&ReasonConcurrency{})
gob.Register(&ReasonMapRange{})
gob.Register(&ReasonAnonymousFunc{})
}