From: David Symonds Date: Thu, 7 Jun 2018 10:44:17 +0000 (+1000) Subject: cmd/cover: fix sorting of profile segment boundaries, again X-Git-Tag: go1.11beta1~178 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=aadaec5045d1b37f9ca7111866a26b5149fec78f;p=gostls13.git cmd/cover: fix sorting of profile segment boundaries, again 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 TryBot-Result: Gobot Gobot Reviewed-by: Rob Pike --- diff --git a/src/cmd/cover/profile.go b/src/cmd/cover/profile.go index 0da42ebfd3..656c862740 100644 --- a/src/cmd/cover/profile.go +++ b/src/cmd/cover/profile.go @@ -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 } diff --git a/src/cmd/cover/testdata/html/html.go b/src/cmd/cover/testdata/html/html.go index 5c7b81f063..20578259a5 100644 --- a/src/cmd/cover/testdata/html/html.go +++ b/src/cmd/cover/testdata/html/html.go @@ -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 diff --git a/src/cmd/cover/testdata/html/html.golden b/src/cmd/cover/testdata/html/html.golden index 2a2abd65a5..84377d1e20 100644 --- a/src/cmd/cover/testdata/html/html.golden +++ b/src/cmd/cover/testdata/html/html.golden @@ -8,3 +8,11 @@ func f() { } // END f +// START g +func g() { + if false { + fmt.Printf("Hello") + } +} + +// END g diff --git a/src/cmd/cover/testdata/html/html_test.go b/src/cmd/cover/testdata/html/html_test.go index d52cf51149..c15561fe4a 100644 --- a/src/cmd/cover/testdata/html/html_test.go +++ b/src/cmd/cover/testdata/html/html_test.go @@ -4,4 +4,5 @@ import "testing" func TestAll(t *testing.T) { f() + g() }