summaryrefslogtreecommitdiffstats
path: root/src/internal/coverage/cformat/format.go
blob: 7e7a2778c725f15faec197d57794726897cddf63 (plain)
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
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package cformat

// This package provides apis for producing human-readable summaries
// of coverage data (e.g. a coverage percentage for a given package or
// set of packages) and for writing data in the legacy test format
// emitted by "go test -coverprofile=<outfile>".
//
// The model for using these apis is to create a Formatter object,
// then make a series of calls to SetPackage and AddUnit passing in
// data read from coverage meta-data and counter-data files. E.g.
//
//		myformatter := cformat.NewFormatter()
//		...
//		for each package P in meta-data file: {
//			myformatter.SetPackage(P)
//			for each function F in P: {
//				for each coverable unit U in F: {
//					myformatter.AddUnit(U)
//				}
//			}
//		}
//		myformatter.EmitPercent(os.Stdout, "", true, true)
//		myformatter.EmitTextual(somefile)
//
// These apis are linked into tests that are built with "-cover", and
// called at the end of test execution to produce text output or
// emit coverage percentages.

import (
	"fmt"
	"internal/coverage"
	"internal/coverage/cmerge"
	"io"
	"sort"
	"text/tabwriter"
)

type Formatter struct {
	// Maps import path to package state.
	pm map[string]*pstate
	// Records current package being visited.
	pkg string
	// Pointer to current package state.
	p *pstate
	// Counter mode.
	cm coverage.CounterMode
}

// pstate records package-level coverage data state:
// - a table of functions (file/fname/literal)
// - a map recording the index/ID of each func encountered so far
// - a table storing execution count for the coverable units in each func
type pstate struct {
	// slice of unique functions
	funcs []fnfile
	// maps function to index in slice above (index acts as function ID)
	funcTable map[fnfile]uint32

	// A table storing coverage counts for each coverable unit.
	unitTable map[extcu]uint32
}

// extcu encapsulates a coverable unit within some function.
type extcu struct {
	fnfid uint32 // index into p.funcs slice
	coverage.CoverableUnit
}

// fnfile is a function-name/file-name tuple.
type fnfile struct {
	file  string
	fname string
	lit   bool
}

func NewFormatter(cm coverage.CounterMode) *Formatter {
	return &Formatter{
		pm: make(map[string]*pstate),
		cm: cm,
	}
}

// SetPackage tells the formatter that we're about to visit the
// coverage data for the package with the specified import path.
// Note that it's OK to call SetPackage more than once with the
// same import path; counter data values will be accumulated.
func (fm *Formatter) SetPackage(importpath string) {
	if importpath == fm.pkg {
		return
	}
	fm.pkg = importpath
	ps, ok := fm.pm[importpath]
	if !ok {
		ps = new(pstate)
		fm.pm[importpath] = ps
		ps.unitTable = make(map[extcu]uint32)
		ps.funcTable = make(map[fnfile]uint32)
	}
	fm.p = ps
}

// AddUnit passes info on a single coverable unit (file, funcname,
// literal flag, range of lines, and counter value) to the formatter.
// Counter values will be accumulated where appropriate.
func (fm *Formatter) AddUnit(file string, fname string, isfnlit bool, unit coverage.CoverableUnit, count uint32) {
	if fm.p == nil {
		panic("AddUnit invoked before SetPackage")
	}
	fkey := fnfile{file: file, fname: fname, lit: isfnlit}
	idx, ok := fm.p.funcTable[fkey]
	if !ok {
		idx = uint32(len(fm.p.funcs))
		fm.p.funcs = append(fm.p.funcs, fkey)
		fm.p.funcTable[fkey] = idx
	}
	ukey := extcu{fnfid: idx, CoverableUnit: unit}
	pcount := fm.p.unitTable[ukey]
	var result uint32
	if fm.cm == coverage.CtrModeSet {
		if count != 0 || pcount != 0 {
			result = 1
		}
	} else {
		// Use saturating arithmetic.
		result, _ = cmerge.SaturatingAdd(pcount, count)
	}
	fm.p.unitTable[ukey] = result
}

// sortUnits sorts a slice of extcu objects in a package according to
// source position information (e.g. file and line). Note that we don't
// include function name as part of the sorting criteria, the thinking
// being that is better to provide things in the original source order.
func (p *pstate) sortUnits(units []extcu) {
	sort.Slice(units, func(i, j int) bool {
		ui := units[i]
		uj := units[j]
		ifile := p.funcs[ui.fnfid].file
		jfile := p.funcs[uj.fnfid].file
		if ifile != jfile {
			return ifile < jfile
		}
		// NB: not taking function literal flag into account here (no
		// need, since other fields are guaranteed to be distinct).
		if units[i].StLine != units[j].StLine {
			return units[i].StLine < units[j].StLine
		}
		if units[i].EnLine != units[j].EnLine {
			return units[i].EnLine < units[j].EnLine
		}
		if units[i].StCol != units[j].StCol {
			return units[i].StCol < units[j].StCol
		}
		if units[i].EnCol != units[j].EnCol {
			return units[i].EnCol < units[j].EnCol
		}
		return units[i].NxStmts < units[j].NxStmts
	})
}

// EmitTextual writes the accumulated coverage data in the legacy
// cmd/cover text format to the writer 'w'. We sort the data items by
// importpath, source file, and line number before emitting (this sorting
// is not explicitly mandated by the format, but seems like a good idea
// for repeatable/deterministic dumps).
func (fm *Formatter) EmitTextual(w io.Writer) error {
	if fm.cm == coverage.CtrModeInvalid {
		panic("internal error, counter mode unset")
	}
	if _, err := fmt.Fprintf(w, "mode: %s\n", fm.cm.String()); err != nil {
		return err
	}
	pkgs := make([]string, 0, len(fm.pm))
	for importpath := range fm.pm {
		pkgs = append(pkgs, importpath)
	}
	sort.Strings(pkgs)
	for _, importpath := range pkgs {
		p := fm.pm[importpath]
		units := make([]extcu, 0, len(p.unitTable))
		for u := range p.unitTable {
			units = append(units, u)
		}
		p.sortUnits(units)
		for _, u := range units {
			count := p.unitTable[u]
			file := p.funcs[u.fnfid].file
			if _, err := fmt.Fprintf(w, "%s:%d.%d,%d.%d %d %d\n",
				file, u.StLine, u.StCol,
				u.EnLine, u.EnCol, u.NxStmts, count); err != nil {
				return err
			}
		}
	}
	return nil
}

// EmitPercent writes out a "percentage covered" string to the writer 'w'.
func (fm *Formatter) EmitPercent(w io.Writer, covpkgs string, noteEmpty bool, aggregate bool) error {
	pkgs := make([]string, 0, len(fm.pm))
	for importpath := range fm.pm {
		pkgs = append(pkgs, importpath)
	}

	rep := func(cov, tot uint64) error {
		if tot != 0 {
			if _, err := fmt.Fprintf(w, "coverage: %.1f%% of statements%s\n",
				100.0*float64(cov)/float64(tot), covpkgs); err != nil {
				return err
			}
		} else if noteEmpty {
			if _, err := fmt.Fprintf(w, "coverage: [no statements]\n"); err != nil {
				return err
			}
		}
		return nil
	}

	sort.Strings(pkgs)
	var totalStmts, coveredStmts uint64
	for _, importpath := range pkgs {
		p := fm.pm[importpath]
		if !aggregate {
			totalStmts, coveredStmts = 0, 0
		}
		for unit, count := range p.unitTable {
			nx := uint64(unit.NxStmts)
			totalStmts += nx
			if count != 0 {
				coveredStmts += nx
			}
		}
		if !aggregate {
			if _, err := fmt.Fprintf(w, "\t%s\t\t", importpath); err != nil {
				return err
			}
			if err := rep(coveredStmts, totalStmts); err != nil {
				return err
			}
		}
	}
	if aggregate {
		if err := rep(coveredStmts, totalStmts); err != nil {
			return err
		}
	}

	return nil
}

// EmitFuncs writes out a function-level summary to the writer 'w'. A
// note on handling function literals: although we collect coverage
// data for unnamed literals, it probably does not make sense to
// include them in the function summary since there isn't any good way
// to name them (this is also consistent with the legacy cmd/cover
// implementation). We do want to include their counts in the overall
// summary however.
func (fm *Formatter) EmitFuncs(w io.Writer) error {
	if fm.cm == coverage.CtrModeInvalid {
		panic("internal error, counter mode unset")
	}
	perc := func(covered, total uint64) float64 {
		if total == 0 {
			total = 1
		}
		return 100.0 * float64(covered) / float64(total)
	}
	tabber := tabwriter.NewWriter(w, 1, 8, 1, '\t', 0)
	defer tabber.Flush()
	allStmts := uint64(0)
	covStmts := uint64(0)

	pkgs := make([]string, 0, len(fm.pm))
	for importpath := range fm.pm {
		pkgs = append(pkgs, importpath)
	}
	sort.Strings(pkgs)

	// Emit functions for each package, sorted by import path.
	for _, importpath := range pkgs {
		p := fm.pm[importpath]
		if len(p.unitTable) == 0 {
			continue
		}
		units := make([]extcu, 0, len(p.unitTable))
		for u := range p.unitTable {
			units = append(units, u)
		}

		// Within a package, sort the units, then walk through the
		// sorted array. Each time we hit a new function, emit the
		// summary entry for the previous function, then make one last
		// emit call at the end of the loop.
		p.sortUnits(units)
		fname := ""
		ffile := ""
		flit := false
		var fline uint32
		var cstmts, tstmts uint64
		captureFuncStart := func(u extcu) {
			fname = p.funcs[u.fnfid].fname
			ffile = p.funcs[u.fnfid].file
			flit = p.funcs[u.fnfid].lit
			fline = u.StLine
		}
		emitFunc := func(u extcu) error {
			// Don't emit entries for function literals (see discussion
			// in function header comment above).
			if !flit {
				if _, err := fmt.Fprintf(tabber, "%s:%d:\t%s\t%.1f%%\n",
					ffile, fline, fname, perc(cstmts, tstmts)); err != nil {
					return err
				}
			}
			captureFuncStart(u)
			allStmts += tstmts
			covStmts += cstmts
			tstmts = 0
			cstmts = 0
			return nil
		}
		for k, u := range units {
			if k == 0 {
				captureFuncStart(u)
			} else {
				if fname != p.funcs[u.fnfid].fname {
					// New function; emit entry for previous one.
					if err := emitFunc(u); err != nil {
						return err
					}
				}
			}
			tstmts += uint64(u.NxStmts)
			count := p.unitTable[u]
			if count != 0 {
				cstmts += uint64(u.NxStmts)
			}
		}
		if err := emitFunc(extcu{}); err != nil {
			return err
		}
	}
	if _, err := fmt.Fprintf(tabber, "%s\t%s\t%.1f%%\n",
		"total", "(statements)", perc(covStmts, allStmts)); err != nil {
		return err
	}
	return nil
}