Print relative filenames in errors

Error messages could get quite lengthy as the code was written previously,
because we always used the complete absolute path for the file in question.
This change "prettifies" this to be relative to whatever contextual path
the user has chosen during compilation.  This shortens messages considerably.
This commit is contained in:
joeduffy 2016-11-15 18:00:43 -08:00
parent c527cedb03
commit ff0059cd7b
3 changed files with 28 additions and 10 deletions

View file

@ -12,8 +12,8 @@ type Options struct {
} }
// DefaultOpts returns the default set of compiler options. // DefaultOpts returns the default set of compiler options.
func DefaultOpts() Options { func DefaultOpts(pwd string) Options {
return Options{ return Options{
Diag: diag.DefaultSink(), Diag: diag.DefaultSink(pwd),
} }
} }

View file

@ -4,13 +4,18 @@ package diag
// Pos represents a position in a file. // Pos represents a position in a file.
type Pos struct { type Pos struct {
Row int Ln int
Col int Col int
} }
// EmptyPos may be used when no position is needed. // EmptyPos may be used when no position is needed.
var EmptyPos = Pos{0, 0} var EmptyPos = Pos{0, 0}
// IsEmpty returns true if the Pos information is missing.
func (pos Pos) IsEmpty() bool {
return pos.Ln == 0 && pos.Col == 0
}
// Location represents a region spanning two positions in a file. // Location represents a region spanning two positions in a file.
type Location struct { type Location struct {
Start Pos Start Pos
@ -19,3 +24,8 @@ type Location struct {
// EmptyLocation may be used when no position information is available. // EmptyLocation may be used when no position information is available.
var EmptyLocation = Location{EmptyPos, EmptyPos} var EmptyLocation = Location{EmptyPos, EmptyPos}
// IsEmpty returns true if the Location information is missing.
func (loc Location) IsEmpty() bool {
return loc.Start.IsEmpty() && loc.End.IsEmpty()
}

View file

@ -6,6 +6,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"os" "os"
"path/filepath"
"strconv" "strconv"
"github.com/golang/glog" "github.com/golang/glog"
@ -27,14 +28,15 @@ type Sink interface {
} }
// DefaultDiags returns a default sink that simply logs output to stderr/stdout. // DefaultDiags returns a default sink that simply logs output to stderr/stdout.
func DefaultSink() Sink { func DefaultSink(pwd string) Sink {
return &defaultSink{} return &defaultSink{pwd: pwd}
} }
// defaultSink is the default sink which logs output to stderr/stdout. // defaultSink is the default sink which logs output to stderr/stdout.
type defaultSink struct { type defaultSink struct {
errors int pwd string // an optional present working directory to which output paths will be relative to.
warnings int errors int // the number of errors that have been issued.
warnings int // the number of warnings that have been issued.
} }
func (d *defaultSink) Count() int { func (d *defaultSink) Count() int {
@ -79,10 +81,16 @@ func (d *defaultSink) stringify(diag *Diag, prefix string, args ...interface{})
} }
if diag.Doc != nil { if diag.Doc != nil {
buffer.WriteString(diag.Doc.File) rel := diag.Doc.File
if diag.Loc != nil { if d.pwd != "" {
// If a PWD is available, convert the file to be relative to it.
rel, _ = filepath.Rel(d.pwd, rel)
}
buffer.WriteString(rel)
if diag.Loc != nil && !diag.Loc.IsEmpty() {
buffer.WriteRune(':') buffer.WriteRune(':')
buffer.WriteString(strconv.Itoa(diag.Loc.Start.Row)) buffer.WriteString(strconv.Itoa(diag.Loc.Start.Ln))
buffer.WriteRune(':') buffer.WriteRune(':')
buffer.WriteString(strconv.Itoa(diag.Loc.Start.Col)) buffer.WriteString(strconv.Itoa(diag.Loc.Start.Col))
} }