Skip to content

Commit e43eebd

Browse files
committed
cmd/cover: fix buglets in counter insertion
This patch has a couple of minor fixes to new-style counter insertion (noticed these problems while working on the fix for issue 56370). First, make sure that the function registration sequence (writing of nctrs, pkgid, funcid to counter var prolog) comes prior to the first counter update (they were reversed up to this point, due to an artifact of the way cmd/internal/edit operates). Second, fix up "per function" counter insertion mode (an experimental feature disabled by default that adds just a single counter to each function as opposed to one per basic block), which was failing to insert the single counter in the right place. Change-Id: Icfb613ca385647f35c0e52da2da8edeb2a506ab7 Reviewed-on: https://go-review.googlesource.com/c/go/+/444835 Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: David Chase <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Than McIntosh <[email protected]>
1 parent 67403a3 commit e43eebd

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/cmd/cover/cfg_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ func TestCoverWithCfg(t *testing.T) {
129129
},
130130
}
131131

132-
tag := "first"
133132
var incfg string
134133
for _, scenario := range scenarios {
135134
// Instrument package "a", producing a set of instrumented output
@@ -138,6 +137,7 @@ func TestCoverWithCfg(t *testing.T) {
138137
pname := "a"
139138
mode := scenario.mode
140139
gran := scenario.gran
140+
tag := mode + "_" + gran
141141
incfg = writePkgConfig(t, instdira, tag, ppath, pname, gran)
142142
ofs, outcfg, _ := runPkgCover(t, instdira, tag, incfg, mode,
143143
pfiles("a"), false)
@@ -158,6 +158,7 @@ func TestCoverWithCfg(t *testing.T) {
158158
// Expect error if config file inaccessible/unreadable.
159159
mode := "atomic"
160160
errExpected := true
161+
tag := "errors"
161162
_, _, errmsg := runPkgCover(t, instdira, tag, "/not/a/file", mode,
162163
pfiles("a"), errExpected)
163164
want := "error reading pkgconfig file"

src/cmd/cover/cover.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,9 @@ func (f *File) Visit(node ast.Node) ast.Visitor {
426426
if *pkgcfg != "" {
427427
f.preFunc(n, fname)
428428
}
429-
ast.Walk(f, n.Body)
429+
if pkgconfig.Granularity != "perfunc" {
430+
ast.Walk(f, n.Body)
431+
}
430432
if *pkgcfg != "" {
431433
flit := true
432434
f.postFunc(n, fname, flit, n.Body)
@@ -465,6 +467,13 @@ func (f *File) preFunc(fn ast.Node, fname string) {
465467
}
466468

467469
func (f *File) postFunc(fn ast.Node, funcname string, flit bool, body *ast.BlockStmt) {
470+
471+
// Tack on single counter write if we are in "perfunc" mode.
472+
singleCtr := ""
473+
if pkgconfig.Granularity == "perfunc" {
474+
singleCtr = "; " + f.newCounter(fn.Pos(), fn.Pos(), 1)
475+
}
476+
468477
// record the length of the counter var required.
469478
nc := len(f.fn.units) + coverage.FirstCtrOffset
470479
f.pkg.counterLengths = append(f.pkg.counterLengths, nc)
@@ -504,12 +513,16 @@ func (f *File) postFunc(fn ast.Node, funcname string, flit bool, body *ast.Block
504513
cv := f.fn.counterVar
505514
regHook := hookWrite(cv, 0, strconv.Itoa(len(f.fn.units))) + " ; " +
506515
hookWrite(cv, 1, mkPackageIdExpression()) + " ; " +
507-
hookWrite(cv, 2, strconv.Itoa(int(funcId)))
516+
hookWrite(cv, 2, strconv.Itoa(int(funcId))) + singleCtr
517+
518+
// Insert the registration sequence into the function. We want this sequence to
519+
// appear before any counter updates, so use a hack to ensure that this edit
520+
// applies before the edit corresponding to the prolog counter update.
508521

509-
// Insert the registration sequence into the function.
510522
boff := f.offset(body.Pos())
511-
ipos := f.fset.File(body.Pos()).Pos(boff + 1)
512-
f.edit.Insert(f.offset(ipos), regHook+" ; ")
523+
ipos := f.fset.File(body.Pos()).Pos(boff)
524+
ip := f.offset(ipos)
525+
f.edit.Replace(ip, ip+1, string(f.content[ipos-1])+regHook+" ; ")
513526

514527
f.fn.counterVar = ""
515528
}
@@ -661,7 +674,6 @@ func (f *File) newCounter(start, end token.Pos, numStmt int) string {
661674
NxStmts: uint32(numStmt),
662675
}
663676
f.fn.units = append(f.fn.units, unit)
664-
665677
} else {
666678
stmt = counterStmt(f, fmt.Sprintf("%s.Count[%d]", *varVar,
667679
len(f.blocks)))

0 commit comments

Comments
 (0)