-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotate.go
206 lines (184 loc) · 4.68 KB
/
annotate.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
package main
import (
"bufio"
"fmt"
"html"
"io"
"os"
"strings"
"sort"
)
const style string = `
<style type="text/css">
table.listing td {
padding: 0px;
font-size: 12px;
vertical-align: top;
padding-left: 10px;
}
table.listing td:first-child {
text-align: right;
font-weight: bold;
vertical-align: center;
}
table.listing tr.miss td {
background-color: #FFC8C8;
}
table.listing tr.hit td {
background-color: #E8FFE8;
}
</style>
`
// As for the actual functions, we want to sort them roughly by the
// usefulness of the information they contain. That would mean PL/PgSQL
// functions first, then SQL functions, and the rest at the very end.
// However, we currently don't have that information so just sort by
// whether prosrc is nil or not.
type FunctionSlice []*Function
func (fs FunctionSlice) Len() int {
return len(fs)
}
func (fs FunctionSlice) Swap(i, j int) {
fs[i], fs[j] = fs[j], fs[i]
}
func (fs FunctionSlice) Less(i, j int) bool {
if (fs[i].prosrc == nil) != (fs[j].prosrc == nil) {
return fs[i].prosrc != nil
}
return fs[i].Signature < fs[j].Signature
}
func Annotate(w io.Writer, functions FunctionSlice, hideSourceListFile string) error {
var hideSourceList []string
if hideSourceListFile != "" {
fh, err := os.Open(hideSourceListFile)
if err != nil {
return fmt.Errorf("could not open file %#v for reading: %s", hideSourceListFile, err)
}
defer fh.Close()
r := bufio.NewReader(fh)
for {
line, err := r.ReadString('\n')
if err != nil && err != io.EOF {
return fmt.Errorf("could not read from file %#v: %s", hideSourceListFile, err)
}
line = strings.TrimRight(line, "\r\n")
if len(line) > 0 {
hideSourceList = append(hideSourceList, line)
}
if err == io.EOF {
break
}
}
sort.Strings(hideSourceList)
}
fmt.Fprintf(w, "<html>%s<head><title>Coverage Report</title></head><body>\n", style)
// see above
sort.Sort(functions)
for _, fn := range(functions) {
fmt.Fprintf(w, "function %s:\n<br /><br />\n", html.EscapeString(fn.Signature))
err := printSource(w, fn, hideSourceList)
if err != nil {
return err
}
}
fmt.Fprint(w, "</body></html>")
return nil
}
/* try and strip single-line comments */
func stripComments(lines []string) []string {
stripped := make([]string, len(lines))
for i, line := range(lines) {
idx := strings.Index(line, "--")
if idx > -1 {
stripped[i] = line[:idx]
} else {
stripped[i] = line
}
}
return stripped
}
func findNextLineno(lineno int32, lines []sourceLine) int32 {
for _, line := range(lines) {
if line.lineno > lineno {
return line.lineno
}
}
return 0
}
type sourceLineInfo struct {
firstLineno int32
lastLineno int32
ncalls int32
}
func stripSomeStuff(lines []string) []string {
for i, l := range(lines) {
// PL/PgSQL doesn't tell us where these statements are
if strings.ToLower(strings.TrimSpace(l)) == "end if;" ||
strings.ToLower(strings.TrimSpace(l)) == "end loop;" {
lines[i] = ""
}
}
return lines
}
func getLineInfo(split []string, lines []sourceLine) (info []sourceLineInfo, err error) {
stripped := stripComments(split)
stripped = stripSomeStuff(stripped)
info = make([]sourceLineInfo, 0, len(lines))
for idx, line := range(lines) {
li := sourceLineInfo{
firstLineno: line.lineno,
ncalls: line.ncalls,
}
nextLineNo := findNextLineno(line.lineno, lines[idx:])
if nextLineNo == 0 {
li.lastLineno = int32(len(split))
} else {
li.lastLineno = nextLineNo - 1
}
for li.lastLineno > li.firstLineno &&
strings.TrimSpace(stripped[li.lastLineno - 1]) == "" {
li.lastLineno--
}
info = append(info, li)
}
return info, nil
}
func printSource(w io.Writer, fn *Function, hideSourceList []string) error {
// see if we need to hide this source
idx := sort.SearchStrings(hideSourceList, fn.Signature)
if idx < len(hideSourceList) && hideSourceList[idx] == fn.Signature {
fmt.Fprintf(w, "<p>(source code hidden)</p><br />\n")
return nil
}
if fn.prosrc == nil {
fmt.Fprintf(w, "<p>(no source code information)</p><br />\n")
return nil
}
lines := strings.Split(*fn.prosrc, "\n")
fmt.Fprintf(w, `<table class="listing">`)
lineInfo, err := getLineInfo(lines, fn.sourceLines)
if err != nil {
return err
}
for lineno, line := range(lines) {
lineno += 1
class := "whitespace"
for _, li := range(lineInfo) {
if int32(lineno) >= li.firstLineno && int32(lineno) <= li.lastLineno {
if li.ncalls == 0 {
class = "miss"
} else {
class = "hit"
}
break
}
}
fmt.Fprintf(w, `<tr class="%s"><td>%d<td><code><pre>%s</pre></code></td></tr>`,
class, lineno,
html.EscapeString(strings.Replace(line, "\t", " ", -1)))
fmt.Fprintln(w)
_ = lineno
}
fmt.Fprintf(w, "</table><br />\n")
return nil
}