]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cover: fix sorting of profile segment boundaries, again
authorDavid Symonds <dsymonds@golang.org>
Thu, 7 Jun 2018 10:44:17 +0000 (20:44 +1000)
committerDavid Symonds <dsymonds@golang.org>
Thu, 7 Jun 2018 22:47:53 +0000 (22:47 +0000)
This is a refinement of CL 114855, which fixed the empty clause case,
but broke some other cases where segment boundaries can coincide
for other reasons.

Fixes #25767.

Change-Id: I2a387c83f9d651c8358f3e11b03f6167af0eb8bf
Reviewed-on: https://go-review.googlesource.com/116976
Run-TryBot: David Symonds <dsymonds@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
src/cmd/cover/profile.go
src/cmd/cover/testdata/html/html.go
src/cmd/cover/testdata/html/html.golden
src/cmd/cover/testdata/html/html_test.go

index 0da42ebfd357067b63861186f5b5022555206f4b..656c8627402749be814387d20a755b0a57a15f0f 100644 (file)
@@ -153,6 +153,7 @@ type Boundary struct {
        Start  bool    // Is this the start of a block?
        Count  int     // Event count from the cover profile.
        Norm   float64 // Count normalized to [0..1].
+       Index  int     // Order in input file.
 }
 
 // Boundaries returns a Profile as a set of Boundary objects within the provided src.
@@ -168,8 +169,10 @@ func (p *Profile) Boundaries(src []byte) (boundaries []Boundary) {
        divisor := math.Log(float64(max))
 
        // boundary returns a Boundary, populating the Norm field with a normalized Count.
+       index := 0
        boundary := func(offset int, start bool, count int) Boundary {
-               b := Boundary{Offset: offset, Start: start, Count: count}
+               b := Boundary{Offset: offset, Start: start, Count: count, Index: index}
+               index++
                if !start || count == 0 {
                        return b
                }
@@ -209,10 +212,9 @@ func (b boundariesByPos) Len() int      { return len(b) }
 func (b boundariesByPos) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
 func (b boundariesByPos) Less(i, j int) bool {
        if b[i].Offset == b[j].Offset {
-               // Boundaries at the same offset should be ordered Start < !Start.
-               // They represent empty sections of code (e.g. a switch/select clause
-               // without a body).
-               return b[i].Start && !b[j].Start
+               // Boundaries at the same offset should be ordered according to
+               // their original position.
+               return b[i].Index < b[j].Index
        }
        return b[i].Offset < b[j].Offset
 }
index 5c7b81f0631bc374c36afd0f9d9867b8d27d8234..20578259a5c826859bdcdda7bdef5e89fbc8b78c 100644 (file)
@@ -1,5 +1,7 @@
 package html
 
+import "fmt"
+
 // This file is tested by html_test.go.
 // The comments below are markers for extracting the annotated source
 // from the HTML output.
@@ -16,3 +18,13 @@ func f() {
 }
 
 // END f
+
+// https://golang.org/issue/25767
+// START g
+func g() {
+       if false {
+               fmt.Printf("Hello")
+       }
+}
+
+// END g
index 2a2abd65a5e5b005298a47e870dcc5c17228fac4..84377d1e2035a93485605e123a2d742613d7c625 100644 (file)
@@ -8,3 +8,11 @@ func f() <span class="cov8" title="1">{
 }
 
 // END f
+// START g
+func g() <span class="cov8" title="1">{
+       if false </span><span class="cov0" title="0">{
+               fmt.Printf("Hello")
+       }</span>
+}
+
+// END g
index d52cf5114990e41d182073bc6bf91f77c1ea1247..c15561fe4a92fd9e743b1b81dd08292157ae3d45 100644 (file)
@@ -4,4 +4,5 @@ import "testing"
 
 func TestAll(t *testing.T) {
        f()
+       g()
 }