-
Notifications
You must be signed in to change notification settings - Fork 4
/
walker.go
277 lines (246 loc) · 7.13 KB
/
walker.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
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
// use this file except in compliance with the License. A copy of the License is
// located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed on
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package main
import (
"go/ast"
"go/token"
"go/types"
"strconv"
"strings"
"golang.org/x/tools/go/loader"
)
// queryTarget holds intermediate state about a query while it runs
type queryTarget struct {
all []types.Object
pkg *types.Package
unfound map[types.Object]bool
}
func newTarget(objs []types.Object) queryTarget {
tgt := queryTarget{
all: objs,
pkg: objs[0].Pkg(),
unfound: make(map[types.Object]bool, len(objs)),
}
for _, o := range objs {
tgt.unfound[o] = true
}
return tgt
}
func (t queryTarget) allFound() bool {
return len(t.unfound) == 0
}
// check sees if o is one of t's targets. It returns true if so.
func (t queryTarget) isTarget(o types.Object) bool {
for _, targetO := range t.all {
if o == targetO {
return true
}
}
return false
}
// discover marks o as found, if it's in t's set of unfound objects. It returns
// true if it was looking for o.
func (t queryTarget) discover(o types.Object) bool {
if o == nil {
return false
}
for lookingFor := range t.unfound {
debugf("(%v) == (%v): %t", o, lookingFor, o == lookingFor)
if o == lookingFor {
debugf("found %v", o.Name())
delete(t.unfound, o)
return true
}
if tn, ok := lookingFor.(*types.TypeName); ok {
debugf("type name: (%v) (%T) == (%v): %t", o.Type(), o.Type(), tn.Type(), o.Type() == tn.Type())
if tn.Type() == o.Type() {
delete(t.unfound, lookingFor)
return true
}
// Dereference pointers, too, if required.
ot := o.Type()
for true {
ptr, ok := ot.(*types.Pointer)
if !ok {
break
}
ot = ptr.Elem()
if tn.Type() == ot {
delete(t.unfound, lookingFor)
return true
}
}
if _, ok := o.Type().(*types.Pointer); ok {
}
}
}
return false
}
// useFinder implements ast.Visitor. It walks an AST, checking to see whether
// its query targets are used. All query targets should be from the same
// package.
type useFinder struct {
target queryTarget
// queryPkgLocalName is the name of the queryPkg in the scope of the file
// currently being visited.
queryPkgLocalName string
// Should we skip vendor paths?
skipVendor bool
// Stored type checker info
typeInfo types.Info
position func(token.Pos) token.Position
}
func findUnused(prog *loader.Program, targets []types.Object) (unusedObjs []types.Object) {
finder := &useFinder{
target: newTarget(targets),
skipVendor: true,
position: prog.Fset.Position,
}
for _, pkgInfo := range prog.AllPackages {
// Skip the original package that contained the targeted objects.
if pkgInfo.Pkg == finder.target.pkg {
continue
}
// Skip standard library packages.
if !strings.Contains(pkgInfo.Pkg.Path(), ".") {
continue
}
// Skip vendor directories, if told to
if finder.skipVendor && strings.Contains(pkgInfo.Pkg.Path(), "vendor") {
continue
}
debugf("checking in %v for %d targets", pkgInfo, len(targets))
finder.typeInfo = pkgInfo.Info
// Walk the AST of each file in the package.
for _, file := range pkgInfo.Files {
ast.Walk(finder, file)
// If all query objects have been found, we're done, and can return.
debugf("after walking %v: %v", file, finder.target.unfound)
if finder.target.allFound() {
return
}
}
if finder.target.allFound() {
return
}
}
for o := range finder.target.unfound {
unusedObjs = append(unusedObjs, o)
}
return unusedObjs
}
func (f *useFinder) Visit(node ast.Node) ast.Visitor {
if node != nil {
debugf("node=%v\ttype=%T", f.position(node.Pos()), node)
}
switch v := node.(type) {
case *ast.File:
return f.visitFile(v)
case nil:
return nil
case *ast.ImportSpec:
return f.visitImportSpec(v)
case *ast.SelectorExpr:
return f.visitSelector(v)
case *ast.Ident:
return f.visitIdent(v)
case *ast.Field, *ast.FieldList:
// Stuff we want to go into, but have no special behavior for
return f
case *ast.Comment, *ast.CommentGroup, *ast.BadExpr:
// Skippable nodes
return nil
default:
return f
}
}
// visitFile enters a file if it imports the package that the query is targeting
func (f *useFinder) visitFile(file *ast.File) ast.Visitor {
f.queryPkgLocalName = ""
for _, importSpec := range file.Imports {
path, _ := strconv.Unquote(importSpec.Path.Value)
if path == f.target.pkg.Path() {
debugf("file %v imports pkg %v, entering", f.position(file.Name.Pos()), f.target.pkg.Path())
return f
}
}
return nil
}
// visitImportSpec sets f.queryPkgLocalName if spec refers to f.queryPkg.
// Otherwise, it does nothing. It always returns nil.
func (f *useFinder) visitImportSpec(spec *ast.ImportSpec) ast.Visitor {
path, _ := strconv.Unquote(spec.Path.Value)
if path == f.target.pkg.Path() {
debugf("found import of pkg %v", path)
if spec.Name != nil {
debugf("pkg alias used: %v", spec.Name.Name)
f.queryPkgLocalName = spec.Name.Name
} else {
debugf("no pkg alias used: %v", f.target.pkg.Name())
f.queryPkgLocalName = f.target.pkg.Name()
}
}
return nil
}
// visitSelector visits a selector expression. This is one of the main chances
// we have to identify uses of the query targets.
func (f *useFinder) visitSelector(sel *ast.SelectorExpr) ast.Visitor {
debugf("pos(sel): %v", f.position(sel.Pos()))
debugf("sel.X: %v", sel.X)
debugf("sel.Sel: %v", sel.Sel)
rcvrIdent, ok := sel.X.(*ast.Ident)
if !ok {
debugf("nil rcvrIdent, type=%T", sel.X)
return nil
}
rcvrObj := f.typeInfo.ObjectOf(rcvrIdent)
switch rcvr := rcvrObj.(type) {
case *types.PkgName:
// The receiver is a package. Is it the package we're querying for?
if rcvr.Imported() == f.target.pkg {
// Yes! Check if the selection is one of the objects we're hunting for.
selObj := f.typeInfo.ObjectOf(sel.Sel)
discovered := f.target.discover(selObj)
debugf("discovered: %v", discovered)
return f
} else {
debugf("wrong pkg")
return nil
}
case *types.Var:
debugf("rcvrObj is a var")
// The receiver is a var. Is it one of the objects we're looking for?
found := f.target.discover(rcvrObj)
if !found {
}
default:
debugf("unexpected rcvrObj.(type): %T", rcvrObj)
}
// Now time to look at the selected value.
selObj := f.typeInfo.ObjectOf(sel.Sel)
debugf("sel obj=%v", selObj)
f.target.discover(selObj)
return nil
}
// visitIdent visits an identifier. If the current file imported queried package
// with a dot import, then we're interested, and will check the name of the
// identifier against the queryObjects set. Otherwise, we don't care about this
// node.
func (f *useFinder) visitIdent(ident *ast.Ident) ast.Visitor {
if f.queryPkgLocalName != "." {
return nil
}
obj := f.typeInfo.ObjectOf(ident)
f.target.discover(obj)
return nil
}