Source file src/cmd/cover/profile.go
1
2
3
4
5
6
7
8
9 package main
10
11 import (
12 "bufio"
13 "fmt"
14 "math"
15 "os"
16 "regexp"
17 "sort"
18 "strconv"
19 "strings"
20 )
21
22
23 type Profile struct {
24 FileName string
25 Mode string
26 Blocks []ProfileBlock
27 }
28
29
30 type ProfileBlock struct {
31 StartLine, StartCol int
32 EndLine, EndCol int
33 NumStmt, Count int
34 }
35
36 type byFileName []*Profile
37
38 func (p byFileName) Len() int { return len(p) }
39 func (p byFileName) Less(i, j int) bool { return p[i].FileName < p[j].FileName }
40 func (p byFileName) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
41
42
43
44 func ParseProfiles(fileName string) ([]*Profile, error) {
45 pf, err := os.Open(fileName)
46 if err != nil {
47 return nil, err
48 }
49 defer pf.Close()
50
51 files := make(map[string]*Profile)
52 buf := bufio.NewReader(pf)
53
54
55
56
57 s := bufio.NewScanner(buf)
58 mode := ""
59 for s.Scan() {
60 line := s.Text()
61 if mode == "" {
62 const p = "mode: "
63 if !strings.HasPrefix(line, p) || line == p {
64 return nil, fmt.Errorf("bad mode line: %v", line)
65 }
66 mode = line[len(p):]
67 continue
68 }
69 m := lineRe.FindStringSubmatch(line)
70 if m == nil {
71 return nil, fmt.Errorf("line %q doesn't match expected format: %v", m, lineRe)
72 }
73 fn := m[1]
74 p := files[fn]
75 if p == nil {
76 p = &Profile{
77 FileName: fn,
78 Mode: mode,
79 }
80 files[fn] = p
81 }
82 p.Blocks = append(p.Blocks, ProfileBlock{
83 StartLine: toInt(m[2]),
84 StartCol: toInt(m[3]),
85 EndLine: toInt(m[4]),
86 EndCol: toInt(m[5]),
87 NumStmt: toInt(m[6]),
88 Count: toInt(m[7]),
89 })
90 }
91 if err := s.Err(); err != nil {
92 return nil, err
93 }
94 for _, p := range files {
95 sort.Sort(blocksByStart(p.Blocks))
96
97 j := 1
98 for i := 1; i < len(p.Blocks); i++ {
99 b := p.Blocks[i]
100 last := p.Blocks[j-1]
101 if b.StartLine == last.StartLine &&
102 b.StartCol == last.StartCol &&
103 b.EndLine == last.EndLine &&
104 b.EndCol == last.EndCol {
105 if b.NumStmt != last.NumStmt {
106 return nil, fmt.Errorf("inconsistent NumStmt: changed from %d to %d", last.NumStmt, b.NumStmt)
107 }
108 if mode == "set" {
109 p.Blocks[j-1].Count |= b.Count
110 } else {
111 p.Blocks[j-1].Count += b.Count
112 }
113 continue
114 }
115 p.Blocks[j] = b
116 j++
117 }
118 p.Blocks = p.Blocks[:j]
119 }
120
121 profiles := make([]*Profile, 0, len(files))
122 for _, profile := range files {
123 profiles = append(profiles, profile)
124 }
125 sort.Sort(byFileName(profiles))
126 return profiles, nil
127 }
128
129 type blocksByStart []ProfileBlock
130
131 func (b blocksByStart) Len() int { return len(b) }
132 func (b blocksByStart) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
133 func (b blocksByStart) Less(i, j int) bool {
134 bi, bj := b[i], b[j]
135 return bi.StartLine < bj.StartLine || bi.StartLine == bj.StartLine && bi.StartCol < bj.StartCol
136 }
137
138 var lineRe = regexp.MustCompile(`^(.+):([0-9]+).([0-9]+),([0-9]+).([0-9]+) ([0-9]+) ([0-9]+)$`)
139
140 func toInt(s string) int {
141 i, err := strconv.Atoi(s)
142 if err != nil {
143 panic(err)
144 }
145 return i
146 }
147
148
149
150
151 type Boundary struct {
152 Offset int
153 Start bool
154 Count int
155 Norm float64
156 Index int
157 }
158
159
160 func (p *Profile) Boundaries(src []byte) (boundaries []Boundary) {
161
162 max := 0
163 for _, b := range p.Blocks {
164 if b.Count > max {
165 max = b.Count
166 }
167 }
168
169 divisor := math.Log(float64(max))
170
171
172 index := 0
173 boundary := func(offset int, start bool, count int) Boundary {
174 b := Boundary{Offset: offset, Start: start, Count: count, Index: index}
175 index++
176 if !start || count == 0 {
177 return b
178 }
179 if max <= 1 {
180 b.Norm = 0.8
181 } else if count > 0 {
182 b.Norm = math.Log(float64(count)) / divisor
183 }
184 return b
185 }
186
187 line, col := 1, 2
188 for si, bi := 0, 0; si < len(src) && bi < len(p.Blocks); {
189 b := p.Blocks[bi]
190 if b.StartLine == line && b.StartCol == col {
191 boundaries = append(boundaries, boundary(si, true, b.Count))
192 }
193 if b.EndLine == line && b.EndCol == col || line > b.EndLine {
194 boundaries = append(boundaries, boundary(si, false, 0))
195 bi++
196 continue
197 }
198 if src[si] == '\n' {
199 line++
200 col = 0
201 }
202 col++
203 si++
204 }
205 sort.Sort(boundariesByPos(boundaries))
206 return
207 }
208
209 type boundariesByPos []Boundary
210
211 func (b boundariesByPos) Len() int { return len(b) }
212 func (b boundariesByPos) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
213 func (b boundariesByPos) Less(i, j int) bool {
214 if b[i].Offset == b[j].Offset {
215
216
217 return b[i].Index < b[j].Index
218 }
219 return b[i].Offset < b[j].Offset
220 }
221
View as plain text