xref: /aosp_15_r20/external/bazelbuild-rules_go/go/tools/coverdata/coverdata.go (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1/* Copyright 2018 The Bazel Authors. All rights reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7   http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14*/
15
16// Package coverdata provides a registration function for files with
17// coverage instrumentation.
18//
19// This package is part of the Bazel Go rules, and its interface
20// should not be considered public. It may change without notice.
21package coverdata
22
23import (
24	"fmt"
25	"testing"
26)
27
28// Contains all coverage data for the program.
29var (
30	Counters = make(map[string][]uint32)
31	Blocks = make(map[string][]testing.CoverBlock)
32)
33
34// RegisterFile causes the coverage data recorded for a file to be included
35// in program-wide coverage reports. This should be called from init functions
36// in packages with coverage instrumentation.
37func RegisterFile(fileName string, counter []uint32, pos []uint32, numStmts []uint16) {
38	if 3*len(counter) != len(pos) || len(counter) != len(numStmts) {
39		panic("coverage: mismatched sizes")
40	}
41	if Counters[fileName] != nil {
42		// Already registered.
43		fmt.Printf("Already covered %s\n", fileName)
44		return
45	}
46	Counters[fileName] = counter
47	block := make([]testing.CoverBlock, len(counter))
48	for i := range counter {
49		block[i] = testing.CoverBlock{
50			Line0: pos[3*i+0],
51			Col0:  uint16(pos[3*i+2]),
52			Line1: pos[3*i+1],
53			Col1:  uint16(pos[3*i+2] >> 16),
54			Stmts: numStmts[i],
55		}
56	}
57	Blocks[fileName] = block
58}
59