]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cover: do not report coverage for assembly functions
authorRuss Cox <rsc@golang.org>
Fri, 10 Nov 2017 04:58:42 +0000 (23:58 -0500)
committerRuss Cox <rsc@golang.org>
Thu, 16 Nov 2017 01:41:51 +0000 (01:41 +0000)
cover -func mode was reporting a coverage for function
declarations without bodies - assembly functions.
Since we are not annotating their code, we have no data
for those functions and should not report them at all.

Fixes #6880.

Change-Id: I4b8cd90805accf61f54e3ee167f54f4dc10c7c59
Reviewed-on: https://go-review.googlesource.com/77152
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/cover/func.go
src/cmd/go/go_test.go
src/cmd/go/testdata/src/coverasm/p.go [new file with mode: 0644]
src/cmd/go/testdata/src/coverasm/p.s [new file with mode: 0644]
src/cmd/go/testdata/src/coverasm/p_test.go [new file with mode: 0644]

index 05c7c12c994636f4c00371c65a06ccea1c2abf79..1673fbf31507d9bac57ca7a98e24d2725d8e6aa0 100644 (file)
@@ -113,6 +113,10 @@ type FuncVisitor struct {
 func (v *FuncVisitor) Visit(node ast.Node) ast.Visitor {
        switch n := node.(type) {
        case *ast.FuncDecl:
+               if n.Body == nil {
+                       // Do not count declarations of assembly functions.
+                       break
+               }
                start := v.fset.Position(n.Pos())
                end := v.fset.Position(n.End())
                fe := &FuncExtent{
index d2933bc3cbca0b7e11da98e124a1b5f513e1d690..9dd5f8347c0d52532a66c8a7ceb350cccb5f30a3 100644 (file)
@@ -2457,6 +2457,19 @@ func TestCoverageErrorLine(t *testing.T) {
        }
 }
 
+func TestCoverageFunc(t *testing.T) {
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.parallel()
+       tg.makeTempdir()
+       tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
+
+       tg.run("test", "-coverprofile="+filepath.Join(tg.tempdir, "cover.out"), "coverasm")
+       tg.run("tool", "cover", "-func="+filepath.Join(tg.tempdir, "cover.out"))
+       tg.grepStdout(`\tg\t*100.0%`, "did not find g 100% covered")
+       tg.grepStdoutNot(`\tf\t*[0-9]`, "reported coverage for assembly function f")
+}
+
 func TestPluginNonMain(t *testing.T) {
        wd, err := os.Getwd()
        if err != nil {
diff --git a/src/cmd/go/testdata/src/coverasm/p.go b/src/cmd/go/testdata/src/coverasm/p.go
new file mode 100644 (file)
index 0000000..ab0c300
--- /dev/null
@@ -0,0 +1,7 @@
+package p
+
+func f()
+
+func g() {
+       println("g")
+}
diff --git a/src/cmd/go/testdata/src/coverasm/p.s b/src/cmd/go/testdata/src/coverasm/p.s
new file mode 100644 (file)
index 0000000..5e728f9
--- /dev/null
@@ -0,0 +1,2 @@
+// empty asm file,
+// so go test doesn't complain about declaration of f in p.go.
diff --git a/src/cmd/go/testdata/src/coverasm/p_test.go b/src/cmd/go/testdata/src/coverasm/p_test.go
new file mode 100644 (file)
index 0000000..3cb3bd5
--- /dev/null
@@ -0,0 +1,7 @@
+package p
+
+import "testing"
+
+func Test(t *testing.T) {
+       g()
+}