Commit a5b7a8d6 authored by Matthew Dempsky's avatar Matthew Dempsky

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: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent e0fa809f
...@@ -19,7 +19,6 @@ import ( ...@@ -19,7 +19,6 @@ import (
type Error struct { type Error struct {
lineno int lineno int
seq int
msg string msg string
} }
...@@ -49,35 +48,24 @@ func adderrorname(n *Node) { ...@@ -49,35 +48,24 @@ func adderrorname(n *Node) {
func adderr(line int, format string, args ...interface{}) { func adderr(line int, format string, args ...interface{}) {
errors = append(errors, Error{ errors = append(errors, Error{
seq: len(errors),
lineno: line, lineno: line,
msg: fmt.Sprintf("%v: %s\n", Ctxt.Line(line), fmt.Sprintf(format, args...)), msg: fmt.Sprintf("%v: %s\n", Ctxt.Line(line), fmt.Sprintf(format, args...)),
}) })
} }
// errcmp sorts errors by line, then seq, then message. // byLineno sorts errors by lineno.
type errcmp []Error type byLineno []Error
func (x errcmp) Len() int { return len(x) } func (x byLineno) Len() int { return len(x) }
func (x errcmp) Swap(i, j int) { x[i], x[j] = x[j], x[i] } func (x byLineno) Less(i, j int) bool { return x[i].lineno < x[j].lineno }
func (x errcmp) Less(i, j int) bool { func (x byLineno) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
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 Flusherrors() { func Flusherrors() {
bstdout.Flush() bstdout.Flush()
if len(errors) == 0 { if len(errors) == 0 {
return return
} }
sort.Sort(errcmp(errors)) sort.Stable(byLineno(errors))
for i := 0; i < len(errors); i++ { for i := 0; i < len(errors); i++ {
if i == 0 || errors[i].msg != errors[i-1].msg { if i == 0 || errors[i].msg != errors[i-1].msg {
fmt.Printf("%s", errors[i].msg) fmt.Printf("%s", errors[i].msg)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment