aboutsummaryrefslogtreecommitdiffhomepage
path: root/main.go
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2024-07-12 18:28:53 +0200
committerRon Evans <[email protected]>2024-07-20 14:30:34 +0200
commit80269b98ba1163485edf0972d1b9e93353f67e5b (patch)
tree31130a2b2e6215655772272c7461a254248eaee7 /main.go
parent3788b31ba52a79a1a0f832c0096e7805df6b8ee1 (diff)
downloadtinygo-80269b98ba1163485edf0972d1b9e93353f67e5b.tar.gz
tinygo-80269b98ba1163485edf0972d1b9e93353f67e5b.zip
diagnostics: move diagnostic printing to a new package
This is a refactor, which should (in theory) not change the behavior of the compiler. But since this is a pretty large change, there is a chance there will be some regressions. For that reason, the previous commits added a bunch of tests to make sure most error messages will not be changed due to this refactor.
Diffstat (limited to 'main.go')
-rw-r--r--main.go96
1 files changed, 3 insertions, 93 deletions
diff --git a/main.go b/main.go
index 9af0b0fc5..d0e1145ce 100644
--- a/main.go
+++ b/main.go
@@ -8,8 +8,6 @@ import (
"errors"
"flag"
"fmt"
- "go/scanner"
- "go/types"
"io"
"os"
"os/exec"
@@ -30,8 +28,8 @@ import (
"github.com/mattn/go-colorable"
"github.com/tinygo-org/tinygo/builder"
"github.com/tinygo-org/tinygo/compileopts"
+ "github.com/tinygo-org/tinygo/diagnostics"
"github.com/tinygo-org/tinygo/goenv"
- "github.com/tinygo-org/tinygo/interp"
"github.com/tinygo-org/tinygo/loader"
"golang.org/x/tools/go/buildutil"
"tinygo.org/x/go-llvm"
@@ -1292,99 +1290,13 @@ func usage(command string) {
}
}
-// try to make the path relative to the current working directory. If any error
-// occurs, this error is ignored and the absolute path is returned instead.
-func tryToMakePathRelative(dir, wd string) string {
- if wd == "" {
- return dir // working directory not found
- }
- relpath, err := filepath.Rel(wd, dir)
- if err != nil {
- return dir
- }
- return relpath
-}
-
-// printCompilerError prints compiler errors using the provided logger function
-// (similar to fmt.Println).
-func printCompilerError(err error, logln func(...interface{}), wd string) {
- switch err := err.(type) {
- case types.Error:
- printCompilerError(scanner.Error{
- Pos: err.Fset.Position(err.Pos),
- Msg: err.Msg,
- }, logln, wd)
- case scanner.Error:
- if !strings.HasPrefix(err.Pos.Filename, filepath.Join(goenv.Get("GOROOT"), "src")) && !strings.HasPrefix(err.Pos.Filename, filepath.Join(goenv.Get("TINYGOROOT"), "src")) {
- // This file is not from the standard library (either the GOROOT or
- // the TINYGOROOT). Make the path relative, for easier reading.
- // Ignore any errors in the process (falling back to the absolute
- // path).
- err.Pos.Filename = tryToMakePathRelative(err.Pos.Filename, wd)
- }
- logln(err)
- case scanner.ErrorList:
- for _, scannerErr := range err {
- printCompilerError(*scannerErr, logln, wd)
- }
- case *interp.Error:
- logln("#", err.ImportPath)
- logln(err.Error())
- if len(err.Inst) != 0 {
- logln(err.Inst)
- }
- if len(err.Traceback) > 0 {
- logln("\ntraceback:")
- for _, line := range err.Traceback {
- logln(line.Pos.String() + ":")
- logln(line.Inst)
- }
- }
- case loader.Errors:
- // Parser errors, typechecking errors, or `go list` errors.
- // err.Pkg is nil for `go list` errors.
- if err.Pkg != nil {
- logln("#", err.Pkg.ImportPath)
- }
- for _, err := range err.Errs {
- printCompilerError(err, logln, wd)
- }
- case loader.Error:
- if err.Err.Pos.Filename != "" {
- // Probably a syntax error in a dependency.
- printCompilerError(err.Err, logln, wd)
- } else {
- // Probably an "import cycle not allowed" error.
- logln("package", err.ImportStack[0])
- for i := 1; i < len(err.ImportStack); i++ {
- pkgPath := err.ImportStack[i]
- if i == len(err.ImportStack)-1 {
- // last package
- logln("\timports", pkgPath+": "+err.Err.Error())
- } else {
- // not the last pacakge
- logln("\timports", pkgPath)
- }
- }
- }
- case *builder.MultiError:
- for _, err := range err.Errs {
- printCompilerError(err, logln, wd)
- }
- default:
- logln("error:", err)
- }
-}
-
func handleCompilerError(err error) {
if err != nil {
wd, getwdErr := os.Getwd()
if getwdErr != nil {
wd = ""
}
- printCompilerError(err, func(args ...interface{}) {
- fmt.Fprintln(os.Stderr, args...)
- }, wd)
+ diagnostics.CreateDiagnostics(err).WriteTo(os.Stderr, wd)
os.Exit(1)
}
}
@@ -1790,9 +1702,7 @@ func main() {
if getwdErr != nil {
wd = ""
}
- printCompilerError(err, func(args ...interface{}) {
- fmt.Fprintln(stderr, args...)
- }, wd)
+ diagnostics.CreateDiagnostics(err).WriteTo(os.Stderr, wd)
}
if !passed {
select {