/
base-annotation.go
109 lines (87 loc) · 2.09 KB
/
base-annotation.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
package testutil
import (
"fmt"
"strings"
"github.com/cs-au-dk/goat/analysis/cfg"
"golang.org/x/tools/go/expect"
)
type basicAnnotation struct {
note *expect.Note
mgr NotesManager
}
func (a basicAnnotation) Note() *expect.Note {
return a.note
}
func (a basicAnnotation) Name() string {
return a.note.Name
}
func (a basicAnnotation) Manager() NotesManager {
return a.mgr
}
type annList []Annotation
// Returns all annotations found on the same line as the given annotation.
func (a1 basicAnnotation) Related() annList {
// Arbitrary capacity.
as := make([]Annotation, 0, 5)
for n2 := range a1.mgr.related[a1.note] {
as = append(as, a1.mgr.AnnotationOf(n2))
}
return as
}
func (a basicAnnotation) Nodes() map[cfg.Node]struct{} {
return a.mgr.NodesForNote(a.note)
}
func (a basicAnnotation) String() string {
fset := a.mgr.loadRes.Prog.Fset
npos := fset.Position(a.note.Pos)
args := make([]string, 0, len(a.note.Args))
for _, arg := range a.note.Args {
args = append(args, fmt.Sprintf("%v", arg))
}
return "//@ Basic annotation: " + a.note.Name + "(" +
strings.Join(args, ", ") + ") at " + npos.String()
}
func (a basicAnnotation) Panicked() bool {
return a.Related().Exists(func(a Annotation) bool {
_, ok := a.(AnnPanicked)
return ok
})
}
func (a basicAnnotation) FalseNegative() bool {
return a.Related().Exists(func(a Annotation) bool {
_, ok := a.(AnnFalseNegative)
return ok
})
}
func (a basicAnnotation) FalsePositive() bool {
return a.Related().Exists(func(a Annotation) bool {
_, ok := a.(AnnFalsePositive)
return ok
})
}
func (la annList) Filter(pred func(Annotation) bool) annList {
res := make([]Annotation, 0, len(la))
for _, ann := range la {
if pred(ann) {
res = append(res, ann)
}
}
return res
}
func (la annList) Find(pred func(Annotation) bool) (Annotation, bool) {
for _, ann := range la {
if pred(ann) {
return ann, true
}
}
return nil, false
}
func (la annList) Exists(pred func(Annotation) bool) bool {
_, found := la.Find(pred)
return found
}
func (la annList) ForEach(do func(Annotation)) {
for _, ann := range la {
do(ann)
}
}