Skip to content

Commit

Permalink
Actually fix instrumentation of nested switch statements
Browse files Browse the repository at this point in the history
  • Loading branch information
rillig committed Oct 7, 2023
1 parent 60bb624 commit 1017c6a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
9 changes: 9 additions & 0 deletions instrumenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ func (i *instrumenter) prepareSwitchStmt(n *ast.SwitchStmt) {
newBody = append(newBody, gen.use(gen.ident(tagExprName)))
}
newBody = append(newBody, gen.switchStmt(nil, n.Body))
i.fixStmtRefs(newBody)

// The initialization statements are executed in a new scope.
// Use the same scope for storing the tag expression in a variable
Expand Down Expand Up @@ -375,6 +376,7 @@ func (i *instrumenter) prepareTypeSwitchStmt(ts *ast.TypeSwitchStmt) {
newBody = append(newBody, gen.use(gen.ident(tagExprName)))
}
newBody = append(newBody, clause.Body...)
i.fixStmtRefs(newBody)

for range clause.List {
test := tests[0]
Expand All @@ -401,10 +403,17 @@ func (i *instrumenter) prepareTypeSwitchStmt(ts *ast.TypeSwitchStmt) {
newBody = append(newBody, gen.define(tag, tagExpr.X))
newBody = append(newBody, assignments...)
newBody = append(newBody, gen.switchStmt(nil, gen.block(newClauses)))
i.fixStmtRefs(newBody)

i.stmtSubst[ts] = gen.block(newBody)
}

func (i *instrumenter) fixStmtRefs(stmts []ast.Stmt) {
for si, stmt := range stmts {
i.stmtRef[stmt] = &stmts[si]
}
}

// replace replaces each prepared node with the instrumentation code,
// in declaration order.
func (i *instrumenter) replace(n ast.Node) bool {
Expand Down
9 changes: 6 additions & 3 deletions testdata/instrumenter/TypeSwitchStmt.branch
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,12 @@ func typeSwitchStmtNested() {
switch {
case GobcoCover(22, gobco1):
default:
switch 1 + 1 {
case GobcoCover(23, gobco2 == 2):
break
{
gobco2 := 1 + 1
switch {
case GobcoCover(23, gobco2 == 2):
break
}
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions testdata/instrumenter/TypeSwitchStmt.cond
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,12 @@ func typeSwitchStmtNested() {
switch {
case GobcoCover(22, gobco1):
default:
switch 1 + 1 {
case GobcoCover(28, gobco2 == 2):
break
{
gobco2 := 1 + 1
switch {
case GobcoCover(28, gobco2 == 2):
break
}
}
}
}
Expand Down

0 comments on commit 1017c6a

Please sign in to comment.