From: Daniel Martí Date: Sat, 4 Jan 2020 10:45:38 +0000 (+0900) Subject: cmd/compile: add a script to measure ssa/gen's coverage X-Git-Tag: go1.15beta1~1156 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7855d6835d3a27e967a0c2d748f9f39305e7ba47;p=gostls13.git 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í TryBot-Result: Gobot Gobot Reviewed-by: Keith Randall --- diff --git a/src/cmd/compile/internal/ssa/gen/cover.bash b/src/cmd/compile/internal/ssa/gen/cover.bash new file mode 100755 index 0000000000..6c860fc864 --- /dev/null +++ b/src/cmd/compile/internal/ssa/gen/cover.bash @@ -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 diff --git a/src/cmd/compile/internal/ssa/gen/rulegen.go b/src/cmd/compile/internal/ssa/gen/rulegen.go index 0d51458f60..2530a61c76 100644 --- a/src/cmd/compile/internal/ssa/gen/rulegen.go +++ b/src/cmd/compile/internal/ssa/gen/rulegen.go @@ -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)