cmd/compile: add a script to measure ssa/gen's coverage

Since rulegen is only tested by inspecting and running its output code,
we have no good way to see if any chunks of its source are actually
being unused.

Code coverage only works as part of 'go test', since it needs to
instrument our code. Add a script that sets up a tiny test for that
purpose, with a quick example on how to use it.

We need to use a script, because there's no other way to make this work
without breaking 'go run *.go'. It's far more common to run the
generator than to obtain a coverage profile, so this solution seems like
the right tradeoff, and we don't break existing users.

The script isn't terribly portable, but that's okay for now.

At the time of wriging, coverage sits at 89.7%. I've manually skimmed
main.go and rulegen.go, and practically all unused code is either error
handling, or optional code like *genLog and "if false". A couple of
small exceptions stand out, though I'm not paying attention to them in
this CL.

While at it, inline a couple of tiny unusedInspector methods that were
only needed once or twice.

Change-Id: I78c5fb47c8536d70e546a437637d4428ec7adfaa
Reviewed-on: https://go-review.googlesource.com/c/go/+/212760
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Daniel Martí 2020-01-04 19:45:38 +09:00
parent 4f020a52c5
commit 7855d6835d
2 changed files with 35 additions and 15 deletions

View File

@ -0,0 +1,26 @@
#!/usr/bin/env bash
# Copyright 2020 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# A quick and dirty way to obtain code coverage from rulegen's main func. For
# example:
#
# ./cover.bash && go tool cover -html=cover.out
#
# This script is needed to set up a temporary test file, so that we don't break
# regular 'go run *.go' usage to run the generator.
cat >main_test.go <<-EOF
// +build ignore
package main
import "testing"
func TestCoverage(t *testing.T) { main() }
EOF
go test -run='^TestCoverage$' -coverprofile=cover.out "$@" *.go
rm -f main_test.go

View File

@ -386,23 +386,13 @@ func (u *unusedInspector) exprs(list []ast.Expr) {
}
}
func (u *unusedInspector) stmts(list []ast.Stmt) {
for _, x := range list {
u.node(x)
}
}
func (u *unusedInspector) decls(list []ast.Decl) {
for _, x := range list {
u.node(x)
}
}
func (u *unusedInspector) node(node ast.Node) {
switch node := node.(type) {
case *ast.File:
defer u.scoped()()
u.decls(node.Decls)
for _, decl := range node.Decls {
u.node(decl)
}
case *ast.GenDecl:
for _, spec := range node.Specs {
u.node(spec)
@ -437,7 +427,9 @@ func (u *unusedInspector) node(node ast.Node) {
case *ast.BlockStmt:
defer u.scoped()()
u.stmts(node.List)
for _, stmt := range node.List {
u.node(stmt)
}
case *ast.IfStmt:
if node.Init != nil {
u.node(node.Init)
@ -469,7 +461,9 @@ func (u *unusedInspector) node(node ast.Node) {
case *ast.CaseClause:
u.exprs(node.List)
defer u.scoped()()
u.stmts(node.Body)
for _, stmt := range node.Body {
u.node(stmt)
}
case *ast.BranchStmt:
case *ast.ExprStmt:
u.node(node.X)