forked from dave/courtney
/
scanner.go
512 lines (467 loc) · 11 KB
/
scanner.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package scanner
import (
"go/ast"
"go/build"
"go/constant"
"go/token"
"go/parser"
"go/types"
"github.com/dave/astrid"
"github.com/dave/brenda"
"github.com/dave/courtney/shared"
"github.com/pkg/errors"
"golang.org/x/tools/go/loader"
)
// CodeMap scans a number of packages for code to exclude
type CodeMap struct {
setup *shared.Setup
prog *loader.Program
Excludes map[string]map[int]bool
}
// PackageMap scans a single package for code to exclude
type PackageMap struct {
*CodeMap
info *loader.PackageInfo
fset *token.FileSet
}
// FileMap scans a single file for code to exclude
type FileMap struct {
*PackageMap
file *ast.File
matcher *astrid.Matcher
}
type packageId struct {
path string
name string
}
// New returns a CoseMap with the provided setup
func New(setup *shared.Setup) *CodeMap {
return &CodeMap{
setup: setup,
Excludes: make(map[string]map[int]bool),
}
}
func (c *CodeMap) addExclude(fpath string, line int) {
if c.Excludes[fpath] == nil {
c.Excludes[fpath] = make(map[int]bool)
}
c.Excludes[fpath][line] = true
}
// LoadProgram uses the loader package to load and process the source for a
// number or packages.
func (c *CodeMap) LoadProgram() error {
ctxt := build.Default
ctxt.GOPATH = c.setup.Env.Getenv("GOPATH")
wd, err := c.setup.Env.Getwd()
if err != nil {
return err
}
conf := loader.Config{Build: &ctxt, Cwd: wd, ParserMode: parser.ParseComments}
for _, p := range c.setup.Packages {
conf.Import(p.Path)
}
prog, err := conf.Load()
if err != nil {
return errors.Wrap(err, "Error loading config")
}
c.prog = prog
return nil
}
// ScanPackages scans the imported packages
func (c *CodeMap) ScanPackages() error {
for _, p := range c.prog.Imported {
pm := &PackageMap{
CodeMap: c,
info: p,
fset: c.prog.Fset,
}
if err := pm.ScanPackage(); err != nil {
return err
}
}
return nil
}
// ScanPackage scans a single package
func (p *PackageMap) ScanPackage() error {
for _, f := range p.info.Files {
fm := &FileMap{
PackageMap: p,
file: f,
matcher: astrid.NewMatcher(p.info.Info.Uses, p.info.Info.Defs),
}
if err := fm.FindExcludes(); err != nil {
return err
}
}
return nil
}
// FindExcludes scans a single file to find code to exclude from coverage files
func (f *FileMap) FindExcludes() error {
var err error
ast.Inspect(f.file, func(node ast.Node) bool {
if err != nil {
// notest
return false
}
b, inner := f.inspectNode(node)
if inner != nil {
// notest
err = inner
return false
}
return b
})
if err != nil {
return err
}
for _, cg := range f.file.Comments {
f.inspectComment(cg)
}
return nil
}
func (f *FileMap) findScope(node ast.Node, filter func(ast.Node) bool) ast.Node {
inside := func(node, holder ast.Node) bool {
return node != nil && holder != nil && node.Pos() > holder.Pos() && node.Pos() <= holder.End()
}
var scopes []ast.Node
ast.Inspect(f.file, func(scope ast.Node) bool {
if inside(node, scope) {
scopes = append(scopes, scope)
return true
}
return false
})
// find the last matching scope
for i := len(scopes) - 1; i >= 0; i-- {
if filter == nil || filter(scopes[i]) {
return scopes[i]
}
}
// notest
return nil
}
func (f *FileMap) inspectComment(cg *ast.CommentGroup) {
for _, cm := range cg.List {
if cm.Text != "//notest" && cm.Text != "// notest" {
continue
}
// get the parent scope
scope := f.findScope(cm, nil)
// scope can be nil if the comment is in an empty file... in that
// case we don't need any excludes.
if scope != nil {
comment := f.fset.Position(cm.Pos())
start := f.fset.Position(scope.Pos())
end := f.fset.Position(scope.End())
endLine := end.Line
if _, ok := scope.(*ast.CaseClause); ok {
// case block needs an extra line...
endLine++
}
for line := comment.Line; line < endLine; line++ {
f.addExclude(start.Filename, line)
}
}
}
}
func (f *FileMap) inspectNode(node ast.Node) (bool, error) {
if node == nil {
return true, nil
}
switch n := node.(type) {
case *ast.CallExpr:
if id, ok := n.Fun.(*ast.Ident); ok && id.Name == "panic" {
pos := f.fset.Position(n.Pos())
f.addExclude(pos.Filename, pos.Line)
}
case *ast.IfStmt:
if err := f.inspectIf(n); err != nil {
return false, err
}
case *ast.SwitchStmt:
if n.Tag != nil {
// we are only concerned with switch statements with no tag
// expression e.g. switch { ... }
return true, nil
}
var falseExpr []ast.Expr
var defaultClause *ast.CaseClause
for _, s := range n.Body.List {
cc := s.(*ast.CaseClause)
if cc.List == nil {
// save the default clause until the end
defaultClause = cc
continue
}
if err := f.inspectCase(cc, falseExpr...); err != nil {
return false, err
}
falseExpr = append(falseExpr, f.boolOr(cc.List))
}
if defaultClause != nil {
if err := f.inspectCase(defaultClause, falseExpr...); err != nil {
return false, err
}
}
}
return true, nil
}
func (f *FileMap) inspectCase(stmt *ast.CaseClause, falseExpr ...ast.Expr) error {
s := brenda.NewSolver(f.fset, f.info.Info.Uses, f.info.Info.Defs, f.boolOr(stmt.List), falseExpr...)
if err := s.SolveTrue(); err != nil {
return err
}
f.processResults(s, &ast.BlockStmt{List: stmt.Body})
return nil
}
func (f *FileMap) boolOr(list []ast.Expr) ast.Expr {
if len(list) == 0 {
return nil
}
if len(list) == 1 {
return list[0]
}
current := list[0]
for i := 1; i < len(list); i++ {
current = &ast.BinaryExpr{X: current, Y: list[i], Op: token.LOR}
}
return current
}
func (f *FileMap) inspectIf(stmt *ast.IfStmt, falseExpr ...ast.Expr) error {
// main if block
s := brenda.NewSolver(f.fset, f.info.Info.Uses, f.info.Info.Defs, stmt.Cond, falseExpr...)
if err := s.SolveTrue(); err != nil {
return err
}
f.processResults(s, stmt.Body)
switch e := stmt.Else.(type) {
case *ast.BlockStmt:
// else block
s := brenda.NewSolver(f.fset, f.info.Info.Uses, f.info.Info.Defs, stmt.Cond, falseExpr...)
if err := s.SolveFalse(); err != nil {
return err
}
f.processResults(s, e)
case *ast.IfStmt:
// else if block
falseExpr = append(falseExpr, stmt.Cond)
if err := f.inspectIf(e, falseExpr...); err != nil {
return err
}
}
return nil
}
func (f *FileMap) processResults(s *brenda.Solver, block *ast.BlockStmt) {
for expr, match := range s.Components {
if !match.Match && !match.Inverse {
continue
}
found, op, expr := f.isErrorComparison(expr)
if !found {
continue
}
if op == token.NEQ && match.Match || op == token.EQL && match.Inverse {
ast.Inspect(block, f.inspectNodeForReturn(expr))
ast.Inspect(block, f.inspectNodeForWrap(block, expr))
}
}
}
func (f *FileMap) isErrorComparison(e ast.Expr) (found bool, sign token.Token, expr ast.Expr) {
if b, ok := e.(*ast.BinaryExpr); ok {
if b.Op != token.NEQ && b.Op != token.EQL {
return
}
xErr := f.isError(b.X)
yNil := f.isNil(b.Y)
if xErr && yNil {
return true, b.Op, b.X
}
yErr := f.isError(b.Y)
xNil := f.isNil(b.X)
if yErr && xNil {
return true, b.Op, b.Y
}
}
return
}
func (f *FileMap) inspectNodeForReturn(search ast.Expr) func(node ast.Node) bool {
return func(node ast.Node) bool {
if node == nil {
return true
}
switch n := node.(type) {
case *ast.ReturnStmt:
if f.isErrorReturn(n, search) {
pos := f.fset.Position(n.Pos())
f.addExclude(pos.Filename, pos.Line)
}
}
return true
}
}
func (f *FileMap) inspectNodeForWrap(block *ast.BlockStmt, search ast.Expr) func(node ast.Node) bool {
return func(node ast.Node) bool {
if node == nil {
return true
}
switch n := node.(type) {
case *ast.DeclStmt:
// covers the case:
// var e = foo()
// and
// var e error = foo()
gd, ok := n.Decl.(*ast.GenDecl)
if !ok {
// notest
return true
}
if gd.Tok != token.VAR {
// notest
return true
}
if len(gd.Specs) != 1 {
// notest
return true
}
spec, ok := gd.Specs[0].(*ast.ValueSpec)
if !ok {
// notest
return true
}
if len(spec.Names) != 1 || len(spec.Values) != 1 {
// notest
return true
}
newSearch := spec.Names[0]
if f.isErrorCall(spec.Values[0], search) {
ast.Inspect(block, f.inspectNodeForReturn(newSearch))
}
case *ast.AssignStmt:
if len(n.Lhs) != 1 || len(n.Rhs) != 1 {
// notest
return true
}
newSearch := n.Lhs[0]
if f.isErrorCall(n.Rhs[0], search) {
ast.Inspect(block, f.inspectNodeForReturn(newSearch))
}
}
return true
}
}
func (f *FileMap) isErrorCall(expr, search ast.Expr) bool {
n, ok := expr.(*ast.CallExpr)
if !ok {
return false
}
if !f.isError(n) {
// never gets here, but leave it in for completeness
// notest
return false
}
for _, arg := range n.Args {
if f.matcher.Match(arg, search) {
return true
}
}
return false
}
func (f *FileMap) isErrorReturnNamedResultParameters(r *ast.ReturnStmt, search ast.Expr) bool {
// covers the syntax:
// func a() (err error) {
// if err != nil {
// return
// }
// }
scope := f.findScope(r, func(n ast.Node) bool {
switch n.(type) {
case *ast.FuncDecl, *ast.FuncLit:
return true
}
return false
})
var t *ast.FuncType
switch s := scope.(type) {
case *ast.FuncDecl:
t = s.Type
case *ast.FuncLit:
t = s.Type
}
if t.Results == nil {
return false
}
last := t.Results.List[len(t.Results.List)-1]
if last.Names == nil {
// anonymous returns - shouldn't be able to get here because a bare
// return statement with either have zero results or named results.
// notest
return false
}
id := last.Names[len(last.Names)-1]
return f.matcher.Match(id, search)
}
func (f *FileMap) isErrorReturn(r *ast.ReturnStmt, search ast.Expr) bool {
if len(r.Results) == 0 {
return f.isErrorReturnNamedResultParameters(r, search)
}
last := r.Results[len(r.Results)-1]
// check the last result is an error
if !f.isError(last) {
return false
}
// check all the other results are nil or zero
for i, v := range r.Results {
if i == len(r.Results)-1 {
// ignore the last item
break
}
if !f.isZero(v) {
return false
}
}
return f.matcher.Match(last, search) || f.isErrorCall(last, search)
}
func (f *FileMap) isError(v ast.Expr) bool {
if n, ok := f.info.TypeOf(v).(*types.Named); ok {
o := n.Obj()
return o != nil && o.Pkg() == nil && o.Name() == "error"
}
return false
}
func (f *FileMap) isNil(v ast.Expr) bool {
t := f.info.Types[v]
return t.IsNil()
}
func (f *FileMap) isZero(v ast.Expr) bool {
t := f.info.Types[v]
if t.IsNil() {
return true
}
if t.Value != nil {
// constant
switch t.Value.Kind() {
case constant.Bool:
return constant.BoolVal(t.Value) == false
case constant.String:
return constant.StringVal(t.Value) == ""
case constant.Int, constant.Float, constant.Complex:
return constant.Sign(t.Value) == 0
default:
// notest
return false
}
}
if t.IsValue() {
if cl, ok := v.(*ast.CompositeLit); ok {
for _, e := range cl.Elts {
if kve, ok := e.(*ast.KeyValueExpr); ok {
e = kve.Value
}
if !f.isZero(e) {
return false
}
}
}
}
return true
}