github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/internal/coverage/defs.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package coverage
     6  
     7  // CovMetaMagic holds the magic string for a meta-data file.
     8  var CovMetaMagic = [4]byte{'\x00', '\x63', '\x76', '\x6d'}
     9  
    10  // MetaFilePref is a prefix used when emitting meta-data files; these
    11  // files are of the form "covmeta.<hash>", where hash is a hash
    12  // computed from the hashes of all the package meta-data symbols in
    13  // the program.
    14  const MetaFilePref = "covmeta"
    15  
    16  // MetaFileVersion contains the current (most recent) meta-data file version.
    17  const MetaFileVersion = 1
    18  
    19  // MetaFileHeader stores file header information for a meta-data file.
    20  type MetaFileHeader struct {
    21  	Magic        [4]byte
    22  	Version      uint32
    23  	TotalLength  uint64
    24  	Entries      uint64
    25  	MetaFileHash [16]byte
    26  	StrTabOffset uint32
    27  	StrTabLength uint32
    28  	CMode        CounterMode
    29  	CGranularity CounterGranularity
    30  	_            [6]byte
    31  }
    32  
    33  // MetaSymbolHeader stores header information for a single
    34  // meta-data blob, e.g. the coverage meta-data payload
    35  // computed for a given Go package.
    36  type MetaSymbolHeader struct {
    37  	Length     uint32
    38  	PkgName    uint32
    39  	PkgPath    uint32
    40  	ModulePath uint32
    41  	MetaHash   [16]byte
    42  	_          byte
    43  	_          [3]byte
    44  	NumFiles   uint32
    45  	NumFuncs   uint32
    46  }
    47  
    48  const CovMetaHeaderSize = 16 + 4 + 4 + 4 + 4 + 4 + 4 + 4
    49  
    50  // FuncDesc encapsulates the meta-data definitions for a single Go function.
    51  // This version assumes that we're looking at a function before inlining;
    52  // if we want to capture a post-inlining view of the world, the
    53  // representations of source positions would need to be a good deal more
    54  // complicated.
    55  type FuncDesc struct {
    56  	Funcname string
    57  	Srcfile  string
    58  	Units    []CoverableUnit
    59  	Lit      bool
    60  }
    61  
    62  // CoverableUnit describes the source characteristics of a single
    63  // program unit for which we want to gather coverage info. Coverable
    64  // units are either "simple" or "intraline"; a "simple" coverable unit
    65  // corresponds to a basic block (region of straight-line code with no
    66  // jumps or control transfers). An "intraline" unit corresponds to a
    67  // logical clause nested within some other simple unit. A simple unit
    68  // will have a zero Parent value; for an intraline unit NxStmts will
    69  // be zero and Parent will be set to 1 plus the index of the
    70  // containing simple statement. Example:
    71  //
    72  //	L7:   q := 1
    73  //	L8:   x := (y == 101 || launch() == false)
    74  //	L9:   r := x * 2
    75  //
    76  // For the code above we would have three simple units (one for each
    77  // line), then an intraline unit describing the "launch() == false"
    78  // clause in line 8, with Parent pointing to the index of the line 8
    79  // unit in the units array.
    80  //
    81  // Note: in the initial version of the coverage revamp, only simple
    82  // units will be in use.
    83  type CoverableUnit struct {
    84  	StLine, StCol uint32
    85  	EnLine, EnCol uint32
    86  	NxStmts       uint32
    87  	Parent        uint32
    88  }
    89  
    90  // CounterMode tracks the "flavor" of the coverage counters being
    91  // used in a given coverage-instrumented program.
    92  type CounterMode uint8
    93  
    94  const (
    95  	CtrModeInvalid  CounterMode = iota
    96  	CtrModeSet
    97  	CtrModeCount
    98  	CtrModeAtomic
    99  	CtrModeRegOnly
   100  	CtrModeTestMain
   101  )
   102  
   103  func (cm CounterMode) String() string
   104  
   105  func ParseCounterMode(mode string) CounterMode
   106  
   107  // CounterGranularity tracks the granularity of the coverage counters being
   108  // used in a given coverage-instrumented program.
   109  type CounterGranularity uint8
   110  
   111  const (
   112  	CtrGranularityInvalid CounterGranularity = iota
   113  	CtrGranularityPerBlock
   114  	CtrGranularityPerFunc
   115  )
   116  
   117  func (cm CounterGranularity) String() string
   118  
   119  // Name of file within the "go test -cover" temp coverdir directory
   120  // containing a list of meta-data files for packages being tested
   121  // in a "go test -coverpkg=... ..." run. This constant is shared
   122  // by the Go command and by the coverage runtime.
   123  const MetaFilesFileName = "metafiles.txt"
   124  
   125  // MetaFilePaths contains information generated by the Go command and
   126  // the read in by coverage test support functions within an executing
   127  // "go test -cover" binary.
   128  type MetaFileCollection struct {
   129  	ImportPaths       []string
   130  	MetaFileFragments []string
   131  }
   132  
   133  // CovCounterMagic holds the magic string for a coverage counter-data file.
   134  var CovCounterMagic = [4]byte{'\x00', '\x63', '\x77', '\x6d'}
   135  
   136  // CounterFileVersion stores the most recent counter data file version.
   137  const CounterFileVersion = 1
   138  
   139  // CounterFileHeader stores files header information for a counter-data file.
   140  type CounterFileHeader struct {
   141  	Magic     [4]byte
   142  	Version   uint32
   143  	MetaHash  [16]byte
   144  	CFlavor   CounterFlavor
   145  	BigEndian bool
   146  	_         [6]byte
   147  }
   148  
   149  // CounterSegmentHeader encapsulates information about a specific
   150  // segment in a counter data file, which at the moment contains
   151  // counters data from a single execution of a coverage-instrumented
   152  // program. Following the segment header will be the string table and
   153  // args table, and then (possibly) padding bytes to bring the byte
   154  // size of the preamble up to a multiple of 4. Immediately following
   155  // that will be the counter payloads.
   156  //
   157  // The "args" section of a segment is used to store annotations
   158  // describing where the counter data came from; this section is
   159  // basically a series of key-value pairs (can be thought of as an
   160  // encoded 'map[string]string'). At the moment we only write os.Args()
   161  // data to this section, using pairs of the form "argc=<integer>",
   162  // "argv0=<os.Args[0]>", "argv1=<os.Args[1]>", and so on. In the
   163  // future the args table may also include things like GOOS/GOARCH
   164  // values, and/or tags indicating which tests were run to generate the
   165  // counter data.
   166  type CounterSegmentHeader struct {
   167  	FcnEntries uint64
   168  	StrTabLen  uint32
   169  	ArgsLen    uint32
   170  }
   171  
   172  // CounterFileFooter appears at the tail end of a counter data file,
   173  // and stores the number of segments it contains.
   174  type CounterFileFooter struct {
   175  	Magic       [4]byte
   176  	_           [4]byte
   177  	NumSegments uint32
   178  	_           [4]byte
   179  }
   180  
   181  // CounterFilePref is the file prefix used when emitting coverage data
   182  // output files. CounterFileTemplate describes the format of the file
   183  // name: prefix followed by meta-file hash followed by process ID
   184  // followed by emit UnixNanoTime.
   185  const CounterFilePref = "covcounters"
   186  const CounterFileTempl = "%s.%x.%d.%d"
   187  const CounterFileRegexp = `^%s\.(\S+)\.(\d+)\.(\d+)+$`
   188  
   189  // CounterFlavor describes how function and counters are
   190  // stored/represented in the counter section of the file.
   191  type CounterFlavor uint8
   192  
   193  const (
   194  	// "Raw" representation: all values (pkg ID, func ID, num counters,
   195  	// and counters themselves) are stored as uint32's.
   196  	CtrRaw CounterFlavor = iota + 1
   197  
   198  	// "ULeb" representation: all values (pkg ID, func ID, num counters,
   199  	// and counters themselves) are stored with ULEB128 encoding.
   200  	CtrULeb128
   201  )
   202  
   203  func Round4(x int) int
   204  
   205  const NumCtrsOffset = 0
   206  const PkgIdOffset = 1
   207  const FuncIdOffset = 2
   208  const FirstCtrOffset = 3