]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: add a script to measure ssa/gen's coverage
authorDaniel Martí <mvdan@mvdan.cc>
Sat, 4 Jan 2020 10:45:38 +0000 (19:45 +0900)
committerDaniel Martí <mvdan@mvdan.cc>
Fri, 21 Feb 2020 18:30:22 +0000 (18:30 +0000)
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>
src/cmd/compile/internal/ssa/gen/cover.bash [new file with mode: 0755]
src/cmd/compile/internal/ssa/gen/rulegen.go

diff --git a/src/cmd/compile/internal/ssa/gen/cover.bash b/src/cmd/compile/internal/ssa/gen/cover.bash
new file mode 100755 (executable)
index 0000000..6c860fc
--- /dev/null
@@ -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
index 0d51458f60bfd5e5cf433a076d35ba0b8c44d5e7..2530a61c762b7b0bf7dfc449f489fadb219285b0 100644 (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)