Goroutine without waitgroup or channel; risks resource leaks or race conditions
`Usage of 'go tool cover':
1// Copyright 2013 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.45package main67import (8 "bytes"9 "cmd/internal/cov/covcmd"10 "cmp"11 "encoding/json"12 "flag"13 "fmt"14 "go/ast"15 "go/parser"16 "go/scanner"17 "go/token"18 "internal/coverage"19 "internal/coverage/encodemeta"20 "internal/coverage/slicewriter"21 "io"22 "log"23 "os"24 "path/filepath"25 "slices"26 "strconv"27 "strings"2829 "cmd/internal/edit"30 "cmd/internal/objabi"31 "cmd/internal/telemetry/counter"32)3334const usageMessage = "" +35 `Usage of 'go tool cover':36Given a coverage profile produced by 'go test':37 go test -coverprofile=c.out3839Open a web browser displaying annotated source code:40 go tool cover -html=c.out4142Write out an HTML file instead of launching a web browser:43 go tool cover -html=c.out -o coverage.html4445Display coverage percentages to stdout for each function:46 go tool cover -func=c.out4748Finally, to generate modified source code with coverage annotations49for a package (what go test -cover does):50 go tool cover -mode=set -var=CoverageVariableName \51 -pkgcfg=<config> -outfilelist=<file> file1.go ... fileN.go5253where -pkgcfg points to a file containing the package path,54package name, module path, and related info from "go build",55and -outfilelist points to a file containing the filenames56of the instrumented output files (one per input file).57See https://pkg.go.dev/cmd/internal/cov/covcmd#CoverPkgConfig for58more on the package config.59`6061func usage() {62 fmt.Fprint(os.Stderr, usageMessage)63 fmt.Fprintln(os.Stderr, "\nFlags:")64 flag.PrintDefaults()65 fmt.Fprintln(os.Stderr, "\n Only one of -html, -func, or -mode may be set.")66 os.Exit(2)67}6869var (70 mode = flag.String("mode", "", "coverage mode: set, count, atomic")71 varVar = flag.String("var", "GoCover", "name of coverage variable to generate")72 output = flag.String("o", "", "file for output")73 outfilelist = flag.String("outfilelist", "", "file containing list of output files (one per line) if -pkgcfg is in use")74 htmlOut = flag.String("html", "", "generate HTML representation of coverage profile")75 funcOut = flag.String("func", "", "output coverage profile information for each function")76 pkgcfg = flag.String("pkgcfg", "", "enable full-package instrumentation mode using params from specified config file")77 pkgconfig covcmd.CoverPkgConfig78 outputfiles []string // list of *.cover.go instrumented outputs to write, one per input (set when -pkgcfg is in use)79 profile string // The profile to read; the value of -html or -func80 counterStmt func(*File, string) string81 covervarsoutfile string // an additional Go source file into which we'll write definitions of coverage counter variables + meta data variables (set when -pkgcfg is in use).82 cmode coverage.CounterMode83 cgran coverage.CounterGranularity84)8586const (87 atomicPackagePath = "sync/atomic"88 atomicPackageName = "_cover_atomic_"89)9091func main() {92 counter.Open()9394 objabi.AddVersionFlag()95 flag.Usage = usage96 objabi.Flagparse(usage)97 counter.Inc("cover/invocations")98 counter.CountFlags("cover/flag:", *flag.CommandLine)99100 // Usage information when no arguments.101 if flag.NFlag() == 0 && flag.NArg() == 0 {102 flag.Usage()103 }104105 err := parseFlags()106 if err != nil {107 fmt.Fprintln(os.Stderr, err)108 fmt.Fprintln(os.Stderr, `For usage information, run "go tool cover -help"`)109 os.Exit(2)110 }111112 // Generate coverage-annotated source.113 if *mode != "" {114 annotate(flag.Args())115 return116 }117118 // Output HTML or function coverage information.119 if *htmlOut != "" {120 err = htmlOutput(profile, *output)121 } else {122 err = funcOutput(profile, *output)123 }124125 if err != nil {126 fmt.Fprintf(os.Stderr, "cover: %v\n", err)127 os.Exit(2)128 }129}130131// parseFlags sets the profile and counterStmt globals and performs validations.132func parseFlags() error {133 profile = *htmlOut134 if *funcOut != "" {135 if profile != "" {136 return fmt.Errorf("too many options")137 }138 profile = *funcOut139 }140141 // Must either display a profile or rewrite Go source.142 if (profile == "") == (*mode == "") {143 return fmt.Errorf("too many options")144 }145146 if *varVar != "" && !token.IsIdentifier(*varVar) {147 return fmt.Errorf("-var: %q is not a valid identifier", *varVar)148 }149150 if *mode != "" {151 switch *mode {152 case "set":153 counterStmt = setCounterStmt154 cmode = coverage.CtrModeSet155 case "count":156 counterStmt = incCounterStmt157 cmode = coverage.CtrModeCount158 case "atomic":159 counterStmt = atomicCounterStmt160 cmode = coverage.CtrModeAtomic161 case "regonly":162 counterStmt = nil163 cmode = coverage.CtrModeRegOnly164 case "testmain":165 counterStmt = nil166 cmode = coverage.CtrModeTestMain167 default:168 return fmt.Errorf("unknown -mode %v", *mode)169 }170171 if flag.NArg() == 0 {172 return fmt.Errorf("missing source file(s)")173 } else {174 if *pkgcfg != "" {175 if *output != "" {176 return fmt.Errorf("please use '-outfilelist' flag instead of '-o'")177 }178 var err error179 if outputfiles, err = readOutFileList(*outfilelist); err != nil {180 return err181 }182 covervarsoutfile = outputfiles[0]183 outputfiles = outputfiles[1:]184 numInputs := len(flag.Args())185 numOutputs := len(outputfiles)186 if numOutputs != numInputs {187 return fmt.Errorf("number of output files (%d) not equal to number of input files (%d)", numOutputs, numInputs)188 }189 if err := readPackageConfig(*pkgcfg); err != nil {190 return err191 }192 return nil193 } else {194 if *outfilelist != "" {195 return fmt.Errorf("'-outfilelist' flag applicable only when -pkgcfg used")196 }197 }198 if flag.NArg() == 1 {199 return nil200 }201 }202 } else if flag.NArg() == 0 {203 return nil204 }205 return fmt.Errorf("too many arguments")206}207208func readOutFileList(path string) ([]string, error) {209 data, err := os.ReadFile(path)210 if err != nil {211 return nil, fmt.Errorf("error reading -outfilelist file %q: %v", path, err)212 }213 return strings.Split(strings.TrimSpace(string(data)), "\n"), nil214}215216func readPackageConfig(path string) error {217 data, err := os.ReadFile(path)218 if err != nil {219 return fmt.Errorf("error reading pkgconfig file %q: %v", path, err)220 }221 if err := json.Unmarshal(data, &pkgconfig); err != nil {222 return fmt.Errorf("error reading pkgconfig file %q: %v", path, err)223 }224 switch pkgconfig.Granularity {225 case "perblock":226 cgran = coverage.CtrGranularityPerBlock227 case "perfunc":228 cgran = coverage.CtrGranularityPerFunc229 default:230 return fmt.Errorf(`%s: pkgconfig requires perblock/perfunc value`, path)231 }232 return nil233}234235// Block represents the information about a basic block to be recorded in the analysis.236// Note: Our definition of basic block is based on control structures; we don't break237// apart && and ||. We could but it doesn't seem important enough to bother.238type Block struct {239 startByte token.Pos240 endByte token.Pos241 numStmt int242}243244// Package holds package-specific state.245type Package struct {246 mdb *encodemeta.CoverageMetaDataBuilder247 counterLengths []int248}249250// Function holds func-specific state.251type Func struct {252 units []coverage.CoverableUnit253 counterVar string254}255256// File is a wrapper for the state of a file used in the parser.257// The basic parse tree walker is a method of this type.258type File struct {259 fset *token.FileSet260 name string // Name of file.261 astFile *ast.File262 blocks []Block263 content []byte264 edit *edit.Buffer265 mdb *encodemeta.CoverageMetaDataBuilder266 fn Func267 pkg *Package268}269270// Range represents a contiguous range of executable code within a basic block.271type Range struct {272 pos token.Pos273 end token.Pos274}275276// codeRanges analyzes a block range and returns the sub-ranges that contain277// executable code, excluding comment-only and blank lines.278// If no executable code is found, it returns a single zero-width range at279// start, so that callers always get at least one range (required by pkgcfg280// mode, which needs a counter unit for every function body).281func (f *File) codeRanges(start, end token.Pos) []Range {282 var (283 startOffset = f.offset(start)284 endOffset = f.offset(end)285 src = f.content[startOffset:endOffset]286 origFile = f.fset.File(start)287 )288289 // Create a temporary File for scanning this block.290 // We use a separate file because we're scanning a slice of the291 // original source, so positions in scanFile are relative to the292 // block start, not the original file.293 scanFile := token.NewFileSet().AddFile("", -1, len(src))294295 var s scanner.Scanner296 s.Init(scanFile, src, nil, 0)297298 // Build ranges in a single pass through the token stream.299 // We track the last line known to contain code (prevEndLine).300 // When the next token appears on a line beyond prevEndLine+1,301 // a gap (comment or blank lines) has been detected: close the302 // current range and start a new one. Using the token's position303 // directly (rather than the line start) ensures counter insertion304 // lands after any closing "*/" on that line.305 var ranges []Range306 var codeStart token.Pos // start of current code range (in origFile)307 prevEndLine := 0 // last line with code; 0 means no code yet308309 for {310 pos, tok, lit := s.Scan()311 if tok == token.EOF {312 break313 }314315 // Skip braces and automatic semicolons: braces are block316 // delimiters, not executable code. The Go spec317 // (https://go.dev/ref/spec#Semicolons) requires the scanner318 // to insert semicolons (with lit == "\n") after }, ), ], etc.319 // These are always on lines already marked by real tokens,320 // except for lone "}" lines. Skipping both prevents a lone321 // "}" from being treated as a separate code range, which322 // would cause counter insertion after return statements.323 if tok == token.LBRACE || tok == token.RBRACE {324 continue325 }326 if tok == token.SEMICOLON && lit == "\n" {327 continue328 }329330 // Use PositionFor with adjusted=false to ignore //line directives.331 startLine := scanFile.PositionFor(pos, false).Line332 endLine := startLine333 if tok == token.STRING {334 // Only string literals can span multiple lines.335 // TODO(adonovan): simplify when https://go.dev/issue/74958 is resolved.336 endLine = scanFile.PositionFor(pos+token.Pos(len(lit)), false).Line337 }338339 if prevEndLine == 0 {340 // First code token — start the first range.341 codeStart = origFile.Pos(startOffset + scanFile.Offset(pos))342 } else if startLine > prevEndLine+1 {343 // Gap detected — close previous range, start new one.344 codeEnd := origFile.Pos(startOffset + scanFile.Offset(scanFile.LineStart(prevEndLine+1)))345 ranges = append(ranges, Range{pos: codeStart, end: codeEnd})346 codeStart = origFile.Pos(startOffset + scanFile.Offset(pos))347 }348349 if endLine > prevEndLine {350 prevEndLine = endLine351 }352 }353354 // Close any open code range at the end.355 if prevEndLine > 0 {356 if prevEndLine < scanFile.LineCount() {357 // There are non-code lines after the last code line358 // (e.g., a lone "}"). Close at the next line's start.359 codeEnd := origFile.Pos(startOffset + scanFile.Offset(scanFile.LineStart(prevEndLine+1)))360 ranges = append(ranges, Range{pos: codeStart, end: codeEnd})361 } else {362 ranges = append(ranges, Range{pos: codeStart, end: end})363 }364 }365366 // If no code was found, return a zero-width range so that callers367 // still get a counter (needed for pkgcfg function registration)368 // but the range doesn't visually cover any source lines.369 if len(ranges) == 0 {370 return []Range{{pos: start, end: start}}371 }372373 return ranges374}375376// insideStatement reports whether pos falls strictly inside377// (not at the start of) any statement in stmts.378func insideStatement(pos token.Pos, stmts []ast.Stmt) bool {379 // Binary search for the first statement starting at or after pos.380 i, _ := slices.BinarySearchFunc(stmts, pos, func(s ast.Stmt, p token.Pos) int {381 return cmp.Compare(s.Pos(), p)382 })383 // Check if pos falls inside the preceding statement.384 return i > 0 && pos < stmts[i-1].End()385}386387// mergeRangesWithinStatements merges consecutive ranges when a later range's388// start position falls strictly inside a statement. This prevents counter389// insertion inside multi-line statements such as const (...) blocks.390func mergeRangesWithinStatements(ranges []Range, stmts []ast.Stmt) []Range {391 if len(ranges) <= 1 {392 return ranges393 }394 merged := []Range{ranges[0]}395 for _, r := range ranges[1:] {396 if insideStatement(r.pos, stmts) {397 // Extend previous range to cover this one.398 merged[len(merged)-1].end = r.end399 } else {400 merged = append(merged, r)401 }402 }403 return merged404}405406// findText finds text in the original source, starting at pos.407// It correctly skips over comments and assumes it need not408// handle quoted strings.409// It returns a byte offset within f.src.410func (f *File) findText(pos token.Pos, text string) int {411 b := []byte(text)412 start := f.offset(pos)413 i := start414 s := f.content415 for i < len(s) {416 if bytes.HasPrefix(s[i:], b) {417 return i418 }419 if i+2 <= len(s) && s[i] == '/' && s[i+1] == '/' {420 for i < len(s) && s[i] != '\n' {421 i++422 }423 continue424 }425 if i+2 <= len(s) && s[i] == '/' && s[i+1] == '*' {426 for i += 2; ; i++ {427 if i+2 > len(s) {428 return 0429 }430 if s[i] == '*' && s[i+1] == '/' {431 i += 2432 break433 }434 }435 continue436 }437 i++438 }439 return -1440}441442// Visit implements the ast.Visitor interface.443func (f *File) Visit(node ast.Node) ast.Visitor {444 switch n := node.(type) {445 case *ast.BlockStmt:446 // If it's a switch or select, the body is a list of case clauses; don't tag the block itself.447 if len(n.List) > 0 {448 switch n.List[0].(type) {449 case *ast.CaseClause: // switch450 for _, n := range n.List {451 clause := n.(*ast.CaseClause)452 f.addCounters(clause.Colon+1, clause.Colon+1, clause.End(), clause.Body, false)453 }454 return f455 case *ast.CommClause: // select456 for _, n := range n.List {457 clause := n.(*ast.CommClause)458 f.addCounters(clause.Colon+1, clause.Colon+1, clause.End(), clause.Body, false)459 }460 return f461 }462 }463 f.addCounters(n.Lbrace, n.Lbrace+1, n.Rbrace+1, n.List, true) // +1 to step past closing brace.464 case *ast.IfStmt:465 if n.Init != nil {466 ast.Walk(f, n.Init)467 }468 ast.Walk(f, n.Cond)469 ast.Walk(f, n.Body)470 if n.Else == nil {471 return nil472 }473 // The elses are special, because if we have474 // if x {475 // } else if y {476 // }477 // we want to cover the "if y". To do this, we need a place to drop the counter,478 // so we add a hidden block:479 // if x {480 // } else {481 // if y {482 // }483 // }484 elseOffset := f.findText(n.Body.End(), "else")485 if elseOffset < 0 {486 panic("lost else")487 }488 f.edit.Insert(elseOffset+4, "{")489 f.edit.Insert(f.offset(n.Else.End()), "}")490491 // We just created a block, now walk it.492 // Adjust the position of the new block to start after493 // the "else". That will cause it to follow the "{"494 // we inserted above.495 pos := f.fset.File(n.Body.End()).Pos(elseOffset + 4)496 switch stmt := n.Else.(type) {497 case *ast.IfStmt:498 block := &ast.BlockStmt{499 Lbrace: pos,500 List: []ast.Stmt{stmt},501 Rbrace: stmt.End(),502 }503 n.Else = block504 case *ast.BlockStmt:505 stmt.Lbrace = pos506 default:507 panic("unexpected node type in if")508 }509 ast.Walk(f, n.Else)510 return nil511 case *ast.SelectStmt:512 // Don't annotate an empty select - creates a syntax error.513 if n.Body == nil || len(n.Body.List) == 0 {514 return nil515 }516 case *ast.SwitchStmt:517 // Don't annotate an empty switch - creates a syntax error.518 if n.Body == nil || len(n.Body.List) == 0 {519 if n.Init != nil {520 ast.Walk(f, n.Init)521 }522 if n.Tag != nil {523 ast.Walk(f, n.Tag)524 }525 return nil526 }527 case *ast.TypeSwitchStmt:528 // Don't annotate an empty type switch - creates a syntax error.529 if n.Body == nil || len(n.Body.List) == 0 {530 if n.Init != nil {531 ast.Walk(f, n.Init)532 }533 ast.Walk(f, n.Assign)534 return nil535 }536 case *ast.FuncDecl:537 // Don't annotate functions with blank names - they cannot be executed.538 // Similarly for bodyless funcs.539 if n.Name.Name == "_" || n.Body == nil {540 return nil541 }542 fname := n.Name.Name543 // Skip AddUint32 and StoreUint32 if we're instrumenting544 // sync/atomic itself in atomic mode (out of an abundance of545 // caution), since as part of the instrumentation process we546 // add calls to AddUint32/StoreUint32, and we don't want to547 // somehow create an infinite loop.548 //549 // Note that in the current implementation (Go 1.20) both550 // routines are assembly stubs that forward calls to the551 // internal/runtime/atomic equivalents, hence the infinite552 // loop scenario is purely theoretical (maybe if in some553 // future implementation one of these functions might be554 // written in Go). See #57445 for more details.555 if atomicOnAtomic() && (fname == "AddUint32" || fname == "StoreUint32") {556 return nil557 }558 // Determine proper function or method name.559 if r := n.Recv; r != nil && len(r.List) == 1 {560 t := r.List[0].Type561 star := ""562 if p, _ := t.(*ast.StarExpr); p != nil {563 t = p.X564 star = "*"565 }566 if p, _ := t.(*ast.Ident); p != nil {567 fname = star + p.Name + "." + fname568 }569 }570 walkBody := true571 if *pkgcfg != "" {572 f.preFunc(n, fname)573 if pkgconfig.Granularity == "perfunc" {574 walkBody = false575 }576 }577 if walkBody {578 ast.Walk(f, n.Body)579 }580 if *pkgcfg != "" {581 flit := false582 f.postFunc(n, fname, flit, n.Body)583 }584 return nil585 case *ast.FuncLit:586 // For function literals enclosed in functions, just glom the587 // code for the literal in with the enclosing function (for now).588 if f.fn.counterVar != "" {589 return f590 }591592 // Hack: function literals aren't named in the go/ast representation,593 // and we don't know what name the compiler will choose. For now,594 // just make up a descriptive name.595 pos := n.Pos()596 p := f.fset.File(pos).Position(pos)597 fname := fmt.Sprintf("func.L%d.C%d", p.Line, p.Column)598 if *pkgcfg != "" {599 f.preFunc(n, fname)600 }601 if pkgconfig.Granularity != "perfunc" {602 ast.Walk(f, n.Body)603 }604 if *pkgcfg != "" {605 flit := true606 f.postFunc(n, fname, flit, n.Body)607 }608 return nil609 }610 return f611}612613func mkCounterVarName(idx int) string {614 return fmt.Sprintf("%s_%d", *varVar, idx)615}616617func mkPackageIdVar() string {618 return *varVar + "P"619}620621func mkMetaVar() string {622 return *varVar + "M"623}624625func mkPackageIdExpression() string {626 ppath := pkgconfig.PkgPath627 if hcid := coverage.HardCodedPkgID(ppath); hcid != -1 {628 return fmt.Sprintf("uint32(%d)", uint32(hcid))629 }630 return mkPackageIdVar()631}632633func (f *File) preFunc(fn ast.Node, fname string) {634 f.fn.units = f.fn.units[:0]635636 // create a new counter variable for this function.637 cv := mkCounterVarName(len(f.pkg.counterLengths))638 f.fn.counterVar = cv639}640641func (f *File) postFunc(fn ast.Node, funcname string, flit bool, body *ast.BlockStmt) {642643 // Tack on single counter write if we are in "perfunc" mode.644 singleCtr := ""645 if pkgconfig.Granularity == "perfunc" {646 singleCtr = "; " + f.newCounter(fn.Pos(), fn.Pos(), 1)647 }648649 // record the length of the counter var required.650 nc := len(f.fn.units) + coverage.FirstCtrOffset651 f.pkg.counterLengths = append(f.pkg.counterLengths, nc)652653 // FIXME: for windows, do we want "\" and not "/"? Need to test here.654 // Currently filename is formed as packagepath + "/" + basename.655 fnpos := f.fset.Position(fn.Pos())656 ppath := pkgconfig.PkgPath657 filename := ppath + "/" + filepath.Base(fnpos.Filename)658659 // The convention for cmd/cover is that if the go command that660 // kicks off coverage specifies a local import path (e.g. "go test661 // -cover ./thispackage"), the tool will capture full pathnames662 // for source files instead of relative paths, which tend to work663 // more smoothly for "go tool cover -html". See also issue #56433664 // for more details.665 if pkgconfig.Local {666 filename = f.name667 }668669 // Hand off function to meta-data builder.670 fd := coverage.FuncDesc{671 Funcname: funcname,672 Srcfile: filename,673 Units: f.fn.units,674 Lit: flit,675 }676 funcId := f.mdb.AddFunc(fd)677678 hookWrite := func(cv string, which int, val string) string {679 return fmt.Sprintf("%s[%d] = %s", cv, which, val)680 }681 if *mode == "atomic" {682 hookWrite = func(cv string, which int, val string) string {683 return fmt.Sprintf("%sStoreUint32(&%s[%d], %s)",684 atomicPackagePrefix(), cv, which, val)685 }686 }687688 // Generate the registration hook sequence for the function. This689 // sequence looks like690 //691 // counterVar[0] = <num_units>692 // counterVar[1] = pkgId693 // counterVar[2] = fnId694 //695 cv := f.fn.counterVar696 regHook := hookWrite(cv, 0, strconv.Itoa(len(f.fn.units))) + " ; " +697 hookWrite(cv, 1, mkPackageIdExpression()) + " ; " +698 hookWrite(cv, 2, strconv.Itoa(int(funcId))) + singleCtr699700 // Insert the registration sequence into the function. We want this sequence to701 // appear before any counter updates, so use a hack to ensure that this edit702 // applies before the edit corresponding to the prolog counter update.703704 boff := f.offset(body.Pos())705 ipos := f.fset.File(body.Pos()).Pos(boff)706 ip := f.offset(ipos)707 f.edit.Replace(ip, ip+1, string(f.content[ipos-1])+regHook+" ; ")708709 f.fn.counterVar = ""710}711712func annotate(names []string) {713 var p *Package714 if *pkgcfg != "" {715 pp := pkgconfig.PkgPath716 pn := pkgconfig.PkgName717 mp := pkgconfig.ModulePath718 mdb, err := encodemeta.NewCoverageMetaDataBuilder(pp, pn, mp)719 if err != nil {720 log.Fatalf("creating coverage meta-data builder: %v\n", err)721 }722 p = &Package{723 mdb: mdb,724 }725 }726 // TODO: process files in parallel here if it matters.727 for k, name := range names {728 if strings.ContainsAny(name, "\r\n") {729 // annotateFile uses '//line' directives, which don't permit newlines.730 log.Fatalf("cover: input path contains newline character: %q", name)731 }732733 fd := os.Stdout734 isStdout := true735 if *pkgcfg != "" {736 var err error737 fd, err = os.Create(outputfiles[k])738 if err != nil {739 log.Fatalf("cover: %s", err)740 }741 isStdout = false742 } else if *output != "" {743 var err error744 fd, err = os.Create(*output)745 if err != nil {746 log.Fatalf("cover: %s", err)747 }748 isStdout = false749 }750 p.annotateFile(name, fd)751 if !isStdout {752 if err := fd.Close(); err != nil {753 log.Fatalf("cover: %s", err)754 }755 }756 }757758 if *pkgcfg != "" {759 fd, err := os.Create(covervarsoutfile)760 if err != nil {761 log.Fatalf("cover: %s", err)762 }763 p.emitMetaData(fd)764 if err := fd.Close(); err != nil {765 log.Fatalf("cover: %s", err)766 }767 }768}769770func (p *Package) annotateFile(name string, fd io.Writer) {771 fset := token.NewFileSet()772 content, err := os.ReadFile(name)773 if err != nil {774 log.Fatalf("cover: %s: %s", name, err)775 }776 parsedFile, err := parser.ParseFile(fset, name, content, parser.ParseComments|parser.SkipObjectResolution)777 if err != nil {778 log.Fatalf("cover: %s: %s", name, err)779 }780781 file := &File{782 fset: fset,783 name: name,784 content: content,785 edit: edit.NewBuffer(content),786 astFile: parsedFile,787 }788 if p != nil {789 file.mdb = p.mdb790 file.pkg = p791 }792793 if *mode == "atomic" {794 // Add import of sync/atomic immediately after package clause.795 // We do this even if there is an existing import, because the796 // existing import may be shadowed at any given place we want797 // to refer to it, and our name (_cover_atomic_) is less likely to798 // be shadowed. The one exception is if we're visiting the799 // sync/atomic package itself, in which case we can refer to800 // functions directly without an import prefix. See also #57445.801 if pkgconfig.PkgPath != "sync/atomic" {802 file.edit.Insert(file.offset(file.astFile.Name.End()),803 fmt.Sprintf("; import %s %q", atomicPackageName, atomicPackagePath))804 }805 }806 if pkgconfig.PkgName == "main" {807 file.edit.Insert(file.offset(file.astFile.Name.End()),808 "; import _ \"runtime/coverage\"")809 }810811 if counterStmt != nil {812 ast.Walk(file, file.astFile)813 }814 newContent := file.edit.Bytes()815816 if strings.ContainsAny(name, "\r\n") {817 // This should have been checked by the caller already, but we double check818 // here just to be sure we haven't missed a caller somewhere.819 panic(fmt.Sprintf("annotateFile: name contains unexpected newline character: %q", name))820 }821 fmt.Fprintf(fd, "//line %s:1:1\n", name)822 fd.Write(newContent)823824 // After printing the source tree, add some declarations for the825 // counters etc. We could do this by adding to the tree, but it's826 // easier just to print the text.827 file.addVariables(fd)828829 // Emit a reference to the atomic package to avoid830 // import and not used error when there's no code in a file.831 if *mode == "atomic" {832 fmt.Fprintf(fd, "\nvar _ = %sLoadUint32\n", atomicPackagePrefix())833 }834}835836// setCounterStmt returns the expression: __count[23] = 1.837func setCounterStmt(f *File, counter string) string {838 return fmt.Sprintf("%s = 1", counter)839}840841// incCounterStmt returns the expression: __count[23]++.842func incCounterStmt(f *File, counter string) string {843 return fmt.Sprintf("%s++", counter)844}845846// atomicCounterStmt returns the expression: atomic.AddUint32(&__count[23], 1)847func atomicCounterStmt(f *File, counter string) string {848 return fmt.Sprintf("%sAddUint32(&%s, 1)", atomicPackagePrefix(), counter)849}850851// newCounter creates a new counter expression of the appropriate form.852func (f *File) newCounter(start, end token.Pos, numStmt int) string {853 var stmt string854 if *pkgcfg != "" {855 slot := len(f.fn.units) + coverage.FirstCtrOffset856 if f.fn.counterVar == "" {857 panic("internal error: counter var unset")858 }859 stmt = counterStmt(f, fmt.Sprintf("%s[%d]", f.fn.counterVar, slot))860 // Physical positions, ignoring //line directives.861 stpos := f.position(start)862 enpos := f.position(end)863 stpos, enpos = dedup(stpos, enpos)864 unit := coverage.CoverableUnit{865 StLine: uint32(stpos.Line),866 StCol: uint32(stpos.Column),867 EnLine: uint32(enpos.Line),868 EnCol: uint32(enpos.Column),869 NxStmts: uint32(numStmt),870 }871 f.fn.units = append(f.fn.units, unit)872 } else {873 stmt = counterStmt(f, fmt.Sprintf("%s.Count[%d]", *varVar,874 len(f.blocks)))875 f.blocks = append(f.blocks, Block{start, end, numStmt})876 }877 return stmt878}879880// addCounters takes a list of statements and adds counters to the beginning of881// each basic block at the top level of that list. For instance, given882//883// S1884// if cond {885// S2886// }887// S3888//889// counters will be added before S1 and before S3. The block containing S2890// will be visited in a separate call.891// TODO: Nested simple blocks get unnecessary (but correct) counters892func (f *File) addCounters(pos, insertPos, blockEnd token.Pos, list []ast.Stmt, extendToClosingBrace bool) {893 // Special case: make sure we add a counter to an empty block. Can't do this below894 // or we will add a counter to an empty statement list after, say, a return statement.895 if len(list) == 0 {896 r := f.codeRanges(insertPos, blockEnd)[0]897 f.edit.Insert(f.offset(r.pos), f.newCounter(r.pos, r.end, 0)+";")898 return899 }900 // Make a copy of the list, as we may mutate it and should leave the901 // existing list intact.902 list = append([]ast.Stmt(nil), list...)903 // We have a block (statement list), but it may have several basic blocks due to the904 // appearance of statements that affect the flow of control.905 for {906 // Find first statement that affects flow of control (break, continue, if, etc.).907 // It will be the last statement of this basic block.908 var last int909 end := blockEnd910 for last = 0; last < len(list); last++ {911 stmt := list[last]912 end = f.statementBoundary(stmt)913 if f.endsBasicSourceBlock(stmt) {914 // If it is a labeled statement, we need to place a counter between915 // the label and its statement because it may be the target of a goto916 // and thus start a basic block. That is, given917 // foo: stmt918 // we need to create919 // foo: ; stmt920 // and mark the label as a block-terminating statement.921 // The result will then be922 // foo: COUNTER[n]++; stmt923 // However, we can't do this if the labeled statement is already924 // a control statement, such as a labeled for.925 if label, isLabel := stmt.(*ast.LabeledStmt); isLabel && !f.isControl(label.Stmt) {926 newLabel := *label927 newLabel.Stmt = &ast.EmptyStmt{928 Semicolon: label.Stmt.Pos(),929 Implicit: true,930 }931 end = label.Pos() // Previous block ends before the label.932 list[last] = &newLabel933 // Open a gap and drop in the old statement, now without a label.934 list = append(list, nil)935 copy(list[last+1:], list[last:])936 list[last+1] = label.Stmt937 }938 last++939 extendToClosingBrace = false // Block is broken up now.940 break941 }942 }943 if extendToClosingBrace {944 end = blockEnd945 }946 if pos != end { // Can have no source to cover if e.g. blocks abut.947 // Create counters only for executable code ranges.948 // Merge back ranges that fall inside a statement to avoid949 // inserting counters inside multi-line constructs (e.g. const blocks).950 for i, r := range mergeRangesWithinStatements(f.codeRanges(pos, end), list[:last]) {951 insertOffset := f.offset(r.pos)952 if i == 0 {953 insertOffset = f.offset(insertPos)954 }955 f.edit.Insert(insertOffset, f.newCounter(r.pos, r.end, last)+";")956 }957 }958 list = list[last:]959 if len(list) == 0 {960 break961 }962 pos = list[0].Pos()963 insertPos = pos964 }965}966967// hasFuncLiteral reports the existence and position of the first func literal968// in the node, if any. If a func literal appears, it usually marks the termination969// of a basic block because the function body is itself a block.970// Therefore we draw a line at the start of the body of the first function literal we find.971// TODO: what if there's more than one? Probably doesn't matter much.972func hasFuncLiteral(n ast.Node) (bool, token.Pos) {973 if n == nil {974 return false, 0975 }976 var literal funcLitFinder977 ast.Walk(&literal, n)978 return literal.found(), token.Pos(literal)979}980981// statementBoundary finds the location in s that terminates the current basic982// block in the source.983func (f *File) statementBoundary(s ast.Stmt) token.Pos {984 // Control flow statements are easy.985 switch s := s.(type) {986 case *ast.BlockStmt:987 // Treat blocks like basic blocks to avoid overlapping counters.988 return s.Lbrace989 case *ast.IfStmt:990 found, pos := hasFuncLiteral(s.Init)991 if found {992 return pos993 }994 found, pos = hasFuncLiteral(s.Cond)995 if found {996 return pos997 }998 return s.Body.Lbrace999 case *ast.ForStmt:1000 found, pos := hasFuncLiteral(s.Init)1001 if found {1002 return pos1003 }1004 found, pos = hasFuncLiteral(s.Cond)1005 if found {1006 return pos1007 }1008 found, pos = hasFuncLiteral(s.Post)1009 if found {1010 return pos1011 }1012 return s.Body.Lbrace1013 case *ast.LabeledStmt:1014 return f.statementBoundary(s.Stmt)1015 case *ast.RangeStmt:1016 found, pos := hasFuncLiteral(s.X)1017 if found {1018 return pos1019 }1020 return s.Body.Lbrace1021 case *ast.SwitchStmt:1022 found, pos := hasFuncLiteral(s.Init)1023 if found {1024 return pos1025 }1026 found, pos = hasFuncLiteral(s.Tag)1027 if found {1028 return pos1029 }1030 return s.Body.Lbrace1031 case *ast.SelectStmt:1032 return s.Body.Lbrace1033 case *ast.TypeSwitchStmt:1034 found, pos := hasFuncLiteral(s.Init)1035 if found {1036 return pos1037 }1038 return s.Body.Lbrace1039 }1040 // If not a control flow statement, it is a declaration, expression, call, etc. and it may have a function literal.1041 // If it does, that's tricky because we want to exclude the body of the function from this block.1042 // Draw a line at the start of the body of the first function literal we find.1043 // TODO: what if there's more than one? Probably doesn't matter much.1044 found, pos := hasFuncLiteral(s)1045 if found {1046 return pos1047 }1048 return s.End()1049}10501051// endsBasicSourceBlock reports whether s changes the flow of control: break, if, etc.,1052// or if it's just problematic, for instance contains a function literal, which will complicate1053// accounting due to the block-within-an expression.1054func (f *File) endsBasicSourceBlock(s ast.Stmt) bool {1055 switch s := s.(type) {1056 case *ast.BlockStmt:1057 // Treat blocks like basic blocks to avoid overlapping counters.1058 return true1059 case *ast.BranchStmt:1060 return true1061 case *ast.ForStmt:1062 return true1063 case *ast.IfStmt:1064 return true1065 case *ast.LabeledStmt:1066 return true // A goto may branch here, starting a new basic block.1067 case *ast.RangeStmt:1068 return true1069 case *ast.SwitchStmt:1070 return true1071 case *ast.SelectStmt:1072 return true1073 case *ast.TypeSwitchStmt:1074 return true1075 case *ast.ExprStmt:1076 // Calls to panic change the flow.1077 // We really should verify that "panic" is the predefined function,1078 // but without type checking we can't and the likelihood of it being1079 // an actual problem is vanishingly small.1080 if call, ok := s.X.(*ast.CallExpr); ok {1081 if ident, ok := call.Fun.(*ast.Ident); ok && ident.Name == "panic" && len(call.Args) == 1 {1082 return true1083 }1084 }1085 }1086 found, _ := hasFuncLiteral(s)1087 return found1088}10891090// isControl reports whether s is a control statement that, if labeled, cannot be1091// separated from its label.1092func (f *File) isControl(s ast.Stmt) bool {1093 switch s.(type) {1094 case *ast.ForStmt, *ast.RangeStmt, *ast.SwitchStmt, *ast.SelectStmt, *ast.TypeSwitchStmt:1095 return true1096 }1097 return false1098}10991100// funcLitFinder implements the ast.Visitor pattern to find the location of any1101// function literal in a subtree.1102type funcLitFinder token.Pos11031104func (f *funcLitFinder) Visit(node ast.Node) (w ast.Visitor) {1105 if f.found() {1106 return nil // Prune search.1107 }1108 switch n := node.(type) {1109 case *ast.FuncLit:1110 *f = funcLitFinder(n.Body.Lbrace)1111 return nil // Prune search.1112 }1113 return f1114}11151116func (f *funcLitFinder) found() bool {1117 return token.Pos(*f) != token.NoPos1118}11191120// Sort interface for []block1; used for self-check in addVariables.11211122type block1 struct {1123 Block1124 index int1125}11261127// position returns the Position for pos, ignoring //line directives.1128func (f *File) position(pos token.Pos) token.Position {1129 return f.fset.PositionFor(pos, false)1130}11311132// offset translates a token position into a 0-indexed byte offset.1133func (f *File) offset(pos token.Pos) int {1134 return f.position(pos).Offset1135}11361137// addVariables adds to the end of the file the declarations to set up the counter and position variables.1138func (f *File) addVariables(w io.Writer) {1139 if *pkgcfg != "" {1140 return1141 }1142 // Self-check: Verify that the instrumented basic blocks are disjoint.1143 t := make([]block1, len(f.blocks))1144 for i := range f.blocks {1145 t[i].Block = f.blocks[i]1146 t[i].index = i1147 }1148 slices.SortFunc(t, func(a, b block1) int {1149 return cmp.Compare(a.startByte, b.startByte)1150 })1151 for i := 1; i < len(t); i++ {1152 if t[i-1].endByte > t[i].startByte {1153 fmt.Fprintf(os.Stderr, "cover: internal error: block %d overlaps block %d\n", t[i-1].index, t[i].index)1154 // Note: error message is in byte positions, not token positions.1155 fmt.Fprintf(os.Stderr, "\t%s:#%d,#%d %s:#%d,#%d\n",1156 f.name, f.offset(t[i-1].startByte), f.offset(t[i-1].endByte),1157 f.name, f.offset(t[i].startByte), f.offset(t[i].endByte))1158 }1159 }11601161 // Declare the coverage struct as a package-level variable.1162 fmt.Fprintf(w, "\nvar %s = struct {\n", *varVar)1163 fmt.Fprintf(w, "\tCount [%d]uint32\n", len(f.blocks))1164 fmt.Fprintf(w, "\tPos [3 * %d]uint32\n", len(f.blocks))1165 fmt.Fprintf(w, "\tNumStmt [%d]uint16\n", len(f.blocks))1166 fmt.Fprintf(w, "} {\n")11671168 // Initialize the position array field.1169 fmt.Fprintf(w, "\tPos: [3 * %d]uint32{\n", len(f.blocks))11701171 // A nice long list of positions. Each position is encoded as follows to reduce size:1172 // - 32-bit starting line number1173 // - 32-bit ending line number1174 // - (16 bit ending column number << 16) | (16-bit starting column number).1175 for i, block := range f.blocks {1176 // Physical positions, ignoring //line directives.1177 start := f.position(block.startByte)1178 end := f.position(block.endByte)11791180 start, end = dedup(start, end)11811182 fmt.Fprintf(w, "\t\t%d, %d, %#x, // [%d]\n", start.Line, end.Line, (end.Column&0xFFFF)<<16|(start.Column&0xFFFF), i)1183 }11841185 // Close the position array.1186 fmt.Fprintf(w, "\t},\n")11871188 // Initialize the position array field.1189 fmt.Fprintf(w, "\tNumStmt: [%d]uint16{\n", len(f.blocks))11901191 // A nice long list of statements-per-block, so we can give a conventional1192 // valuation of "percent covered". To save space, it's a 16-bit number, so we1193 // clamp it if it overflows - won't matter in practice.1194 for i, block := range f.blocks {1195 n := block.numStmt1196 if n > 1<<16-1 {1197 n = 1<<16 - 11198 }1199 fmt.Fprintf(w, "\t\t%d, // %d\n", n, i)1200 }12011202 // Close the statements-per-block array.1203 fmt.Fprintf(w, "\t},\n")12041205 // Close the struct initialization.1206 fmt.Fprintf(w, "}\n")1207}12081209// It is possible for positions to repeat when there is a line1210// directive that does not specify column information and the input1211// has not been passed through gofmt.1212// See issues #27530 and #30746.1213// Tests are TestHtmlUnformatted and TestLineDup.1214// We use a map to avoid duplicates.12151216// pos2 is a pair of token.Position values, used as a map key type.1217type pos2 struct {1218 p1, p2 token.Position1219}12201221// seenPos2 tracks whether we have seen a token.Position pair.1222var seenPos2 = make(map[pos2]bool)12231224// dedup takes a token.Position pair and returns a pair that does not1225// duplicate any existing pair. The returned pair will have the Offset1226// fields cleared.1227func dedup(p1, p2 token.Position) (r1, r2 token.Position) {1228 key := pos2{1229 p1: p1,1230 p2: p2,1231 }12321233 // We want to ignore the Offset fields in the map,1234 // since cover uses only file/line/column.1235 key.p1.Offset = 01236 key.p2.Offset = 012371238 for seenPos2[key] {1239 key.p2.Column++1240 }1241 seenPos2[key] = true12421243 return key.p1, key.p21244}12451246func (p *Package) emitMetaData(w io.Writer) {1247 if *pkgcfg == "" {1248 return1249 }12501251 // If the "EmitMetaFile" path has been set, invoke a helper1252 // that will write out a pre-cooked meta-data file for this package1253 // to the specified location, in effect simulating the execution1254 // of a test binary that doesn't do any testing to speak of.1255 if pkgconfig.EmitMetaFile != "" {1256 p.emitMetaFile(pkgconfig.EmitMetaFile)1257 }12581259 // Something went wrong if regonly/testmain mode is in effect and1260 // we have instrumented functions.1261 if counterStmt == nil && len(p.counterLengths) != 0 {1262 panic("internal error: seen functions with regonly/testmain")1263 }12641265 // Emit package name.1266 fmt.Fprintf(w, "\npackage %s\n\n", pkgconfig.PkgName)12671268 // Emit package ID var.1269 fmt.Fprintf(w, "\nvar %sP uint32\n", *varVar)12701271 // Emit all of the counter variables.1272 for k := range p.counterLengths {1273 cvn := mkCounterVarName(k)1274 fmt.Fprintf(w, "var %s [%d]uint32\n", cvn, p.counterLengths[k])1275 }12761277 // Emit encoded meta-data.1278 var sws slicewriter.WriteSeeker1279 digest, err := p.mdb.Emit(&sws)1280 if err != nil {1281 log.Fatalf("encoding meta-data: %v", err)1282 }1283 p.mdb = nil1284 fmt.Fprintf(w, "var %s = [...]byte{\n", mkMetaVar())1285 payload := sws.BytesWritten()1286 for k, b := range payload {1287 fmt.Fprintf(w, " 0x%x,", b)1288 if k != 0 && k%8 == 0 {1289 fmt.Fprintf(w, "\n")1290 }1291 }1292 fmt.Fprintf(w, "}\n")12931294 fixcfg := covcmd.CoverFixupConfig{1295 Strategy: "normal",1296 MetaVar: mkMetaVar(),1297 MetaLen: len(payload),1298 MetaHash: fmt.Sprintf("%x", digest),1299 PkgIdVar: mkPackageIdVar(),1300 CounterPrefix: *varVar,1301 CounterGranularity: pkgconfig.Granularity,1302 CounterMode: *mode,1303 }1304 fixdata, err := json.Marshal(fixcfg)1305 if err != nil {1306 log.Fatalf("marshal fixupcfg: %v", err)1307 }1308 if err := os.WriteFile(pkgconfig.OutConfig, fixdata, 0666); err != nil {1309 log.Fatalf("error writing %s: %v", pkgconfig.OutConfig, err)1310 }1311}13121313// atomicOnAtomic returns true if we're instrumenting1314// the sync/atomic package AND using atomic mode.1315func atomicOnAtomic() bool {1316 return *mode == "atomic" && pkgconfig.PkgPath == "sync/atomic"1317}13181319// atomicPackagePrefix returns the import path prefix used to refer to1320// our special import of sync/atomic; this is either set to the1321// constant atomicPackageName plus a dot or the empty string if we're1322// instrumenting the sync/atomic package itself.1323func atomicPackagePrefix() string {1324 if atomicOnAtomic() {1325 return ""1326 }1327 return atomicPackageName + "."1328}13291330func (p *Package) emitMetaFile(outpath string) {1331 // Open output file.1332 of, err := os.OpenFile(outpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)1333 if err != nil {1334 log.Fatalf("opening covmeta %s: %v", outpath, err)1335 }13361337 if len(p.counterLengths) == 0 {1338 // This corresponds to the case where we have no functions1339 // in the package to instrument. Leave the file empty file if1340 // this happens.1341 if err = of.Close(); err != nil {1342 log.Fatalf("closing meta-data file: %v", err)1343 }1344 return1345 }13461347 // Encode meta-data.1348 var sws slicewriter.WriteSeeker1349 digest, err := p.mdb.Emit(&sws)1350 if err != nil {1351 log.Fatalf("encoding meta-data: %v", err)1352 }1353 payload := sws.BytesWritten()1354 blobs := [][]byte{payload}13551356 // Write meta-data file directly.1357 mfw := encodemeta.NewCoverageMetaFileWriter(outpath, of)1358 err = mfw.Write(digest, blobs, cmode, cgran)1359 if err != nil {1360 log.Fatalf("writing meta-data file: %v", err)1361 }1362 if err = of.Close(); err != nil {1363 log.Fatalf("closing meta-data file: %v", err)1364 }1365}
Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.