/
dataflow.go
280 lines (256 loc) · 7.67 KB
/
dataflow.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
// Copyright 2015 Auburn University. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package dataflow provides data flow analyses that can be performed on a
// previously constructed control flow graph, including a reaching definitions
// analysis and a live variables analysis for local variables.
package dataflow
// This file contains functions common to all data flow analyses, as well as
// one exported function.
import (
"go/ast"
"go/token"
"go/types"
"golang.org/x/tools/go/packages"
)
// ReferencedVars returns the sets of local variables that are defined or used
// within the given list of statements (based on syntax).
func ReferencedVars(stmts []ast.Stmt, info *packages.Package) (asgt, updt, decl, use map[*types.Var]struct{}) {
asgt = make(map[*types.Var]struct{})
updt = make(map[*types.Var]struct{})
decl = make(map[*types.Var]struct{})
use = make(map[*types.Var]struct{})
for _, stmt := range stmts {
for _, d := range assignments(stmt, info, false) {
asgt[d] = struct{}{}
}
for _, d := range assignments(stmt, info, true) {
updt[d] = struct{}{}
}
for _, d := range decls(stmt, info) {
decl[d] = struct{}{}
}
for _, u := range uses(stmt, info) {
use[u] = struct{}{}
}
}
return asgt, updt, decl, use
}
// defs returns the set of variables that are assigned a value in the given
// statement, either by being assigned or declared
func defs(stmt ast.Stmt, info *packages.Package) []*types.Var {
union := make(map[*types.Var]struct{})
for _, v := range assignments(stmt, info, true) {
union[v] = struct{}{}
}
for _, v := range assignments(stmt, info, false) {
union[v] = struct{}{}
}
for _, v := range decls(stmt, info) {
union[v] = struct{}{}
}
result := []*types.Var{}
for v := range union {
result = append(result, v)
}
return result
}
// assignments extracts any local variables whose values are assigned in the given statement.
func assignments(stmt ast.Stmt, info *packages.Package, wantMembers bool) []*types.Var {
idnts := make(map[*ast.Ident]struct{})
switch stmt := stmt.(type) {
case *ast.AssignStmt: // =, &=, etc. except x[i] (IndexExpr)
if stmt.Tok != token.DEFINE {
for _, x := range stmt.Lhs {
switch x.(type) {
case *ast.IndexExpr, *ast.SelectorExpr, *ast.StarExpr:
if wantMembers {
idnts = union(idnts, assigned(x))
}
default:
idnts = union(idnts, idents(x))
}
}
}
case *ast.IncDecStmt: // i++, i--
switch x := stmt.X.(type) {
case *ast.IndexExpr, *ast.SelectorExpr, *ast.StarExpr:
if wantMembers {
idnts = union(idnts, assigned(x))
}
default:
idnts = union(idnts, idents(x))
}
}
return collectVars(idnts, info)
}
// idents returns the set of identifiers assigned in a given node.
func assigned(node ast.Expr) map[*ast.Ident]struct{} {
switch expr := node.(type) {
case *ast.IndexExpr:
return assigned(expr.X)
case *ast.SelectorExpr:
field := map[*ast.Ident]struct{}{expr.Sel: {}}
return union(assigned(expr.X), field)
case *ast.StarExpr:
return map[*ast.Ident]struct{}{}
default:
return idents(node)
}
}
// decls extracts any local variables that are declared (and implicitly or
// explicitly assigned a value) in the given statement.
func decls(stmt ast.Stmt, info *packages.Package) []*types.Var {
idnts := make(map[*ast.Ident]struct{})
switch stmt := stmt.(type) {
case *ast.DeclStmt: // vars (1+) in decl; zero values
ast.Inspect(stmt, func(n ast.Node) bool {
if v, ok := n.(*ast.ValueSpec); ok {
idnts = union(idnts, idents(v))
}
return true
})
case *ast.AssignStmt: // := except x[i] (IndexExpr)
if stmt.Tok == token.DEFINE {
for _, x := range stmt.Lhs {
indExp := false
ast.Inspect(x, func(n ast.Node) bool {
if _, ok := n.(*ast.IndexExpr); ok {
indExp = true
return false
}
return true
})
if !indExp {
idnts = union(idnts, idents(x))
}
}
}
case *ast.RangeStmt: // only [ x, y ] on Lhs
idnts = union(idents(stmt.Key), idents(stmt.Value))
case *ast.TypeSwitchStmt:
// The assigned variable does not have a types.Var
// associated in this stmt; rather, the uses of that
// variable in the case clauses have several different
// types.Vars associated with them, according to type
var vars []*types.Var
ast.Inspect(stmt.Body, func(n ast.Node) bool {
switch cc := n.(type) {
case *ast.CaseClause:
v := typeCaseVar(info, cc)
if v != nil {
vars = append(vars, v)
}
return false
default:
return true
}
})
return vars
}
return collectVars(idnts, info)
}
func collectVars(idnts map[*ast.Ident]struct{}, info *packages.Package) []*types.Var {
var vars []*types.Var
// should all map to types.Var's, if not we don't want anyway
for i := range idnts {
if v, ok := info.TypesInfo.ObjectOf(i).(*types.Var); ok {
if v.Pkg() == info.Types && !v.IsField() {
vars = append(vars, v)
}
}
}
return vars
}
// typeCaseVar returns the implicit variable associated with a case clause in a
// type switch statement.
func typeCaseVar(info *packages.Package, cc *ast.CaseClause) *types.Var {
// Removed from go/loader
if v := info.TypesInfo.Implicits[cc]; v != nil {
return v.(*types.Var)
}
return nil
}
// uses extracts local variables whose values are used in the given statement.
func uses(stmt ast.Stmt, info *packages.Package) []*types.Var {
idnts := make(map[*ast.Ident]struct{})
ast.Inspect(stmt, func(n ast.Node) bool {
switch stmt := stmt.(type) {
case *ast.AssignStmt: // mostly rhs of =, :=, &=, etc.
// some LHS are uses, e.g. x[i]
for _, x := range stmt.Lhs {
indExp := false
switch T := x.(type) {
case *ast.IndexExpr:
indExp = true
case *ast.SelectorExpr:
idnts = union(idnts, idents(T))
}
if indExp || // x[i] is a uses of x and i
(stmt.Tok != token.ASSIGN &&
stmt.Tok != token.DEFINE) { // e.g. +=, ^=, etc.
idnts = union(idnts, idents(x))
}
}
// all RHS are uses
for _, s := range stmt.Rhs {
idnts = union(idnts, idents(s))
}
case *ast.BlockStmt: // no uses, skip - should not appear in cfg
case *ast.BranchStmt: // no uses, skip
case *ast.CaseClause:
for _, i := range stmt.List {
idnts = union(idnts, idents(i))
}
case *ast.CommClause: // no uses, skip
case *ast.DeclStmt: // no uses, skip
case *ast.DeferStmt:
idnts = union(idnts, idents(stmt.Call))
case *ast.ForStmt:
idnts = union(idnts, idents(stmt.Cond))
case *ast.IfStmt:
idnts = union(idnts, idents(stmt.Cond))
case *ast.LabeledStmt: // no uses, skip
case *ast.RangeStmt: // list in _, _ = range [ list ]
idnts = union(idnts, idents(stmt.X))
case *ast.SelectStmt: // no uses, skip
case *ast.SwitchStmt:
idnts = union(idnts, idents(stmt.Tag))
case *ast.TypeSwitchStmt:
idnts = union(idnts, idents(stmt.Assign))
case ast.Stmt: // everything else is all uses
idnts = union(idnts, idents(stmt))
}
return true
})
return collectVars(idnts, info)
}
// idents returns the set of all identifiers in given node.
func idents(node ast.Node) map[*ast.Ident]struct{} {
idents := make(map[*ast.Ident]struct{})
if node == nil {
return idents
}
ast.Inspect(node, func(n ast.Node) bool {
switch n := n.(type) {
case *ast.Ident:
idents[n] = struct{}{}
}
return true
})
return idents
}
func union(one, two map[*ast.Ident]struct{}) map[*ast.Ident]struct{} {
for o := range one {
two[o] = struct{}{}
}
return two
}
// Vars returns the set of variables appearing in node
func Vars(node ast.Node, info *packages.Package) map[*types.Var]struct{} {
result := map[*types.Var]struct{}{}
for _, variable := range collectVars(idents(node), info) {
result[variable] = struct{}{}
}
return result
}