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