cmd/compile: simplify error sorting

Errors have unique seq values (their index within the errors slice),
so errcmp never needs to fallback to sorting by message text.
Moreover, comparing by original index is exactly the purpose of using
a stable sort algorithm (and sort.Stable was added in Go 1.2), so we
really only need to compare by lineno.

Change-Id: I7f534b72a05d899ae9788dc7ef0541dd92a8b578
Reviewed-on: https://go-review.googlesource.com/19929
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Matthew Dempsky 2016-02-25 18:17:31 -08:00
parent e0fa809f4c
commit a5b7a8d6dd

View file

@ -19,7 +19,6 @@ import (
type Error struct {
lineno int
seq int
msg string
}
@ -49,35 +48,24 @@ func adderrorname(n *Node) {
func adderr(line int, format string, args ...interface{}) {
errors = append(errors, Error{
seq: len(errors),
lineno: line,
msg: fmt.Sprintf("%v: %s\n", Ctxt.Line(line), fmt.Sprintf(format, args...)),
})
}
// errcmp sorts errors by line, then seq, then message.
type errcmp []Error
// byLineno sorts errors by lineno.
type byLineno []Error
func (x errcmp) Len() int { return len(x) }
func (x errcmp) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x errcmp) Less(i, j int) bool {
a := &x[i]
b := &x[j]
if a.lineno != b.lineno {
return a.lineno < b.lineno
}
if a.seq != b.seq {
return a.seq < b.seq
}
return a.msg < b.msg
}
func (x byLineno) Len() int { return len(x) }
func (x byLineno) Less(i, j int) bool { return x[i].lineno < x[j].lineno }
func (x byLineno) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func Flusherrors() {
bstdout.Flush()
if len(errors) == 0 {
return
}
sort.Sort(errcmp(errors))
sort.Stable(byLineno(errors))
for i := 0; i < len(errors); i++ {
if i == 0 || errors[i].msg != errors[i-1].msg {
fmt.Printf("%s", errors[i].msg)