Commit 94197135 authored by Matthew Dempsky's avatar Matthew Dempsky

cmd/compile: cleanup import logic slightly

Use bio.Reader. Include newline character in the expected string value
instead of truncating it. Get rid of weird "empty archive" check.

Passes toolstash-check.

Change-Id: I16e42542db4827e6ee3644b9a5540a4a30b9bc41
Reviewed-on: https://go-review.googlesource.com/107620
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 86827fd7
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"bytes" "bytes"
"cmd/compile/internal/ssa" "cmd/compile/internal/ssa"
"cmd/compile/internal/types" "cmd/compile/internal/types"
"cmd/internal/bio"
"cmd/internal/dwarf" "cmd/internal/dwarf"
"cmd/internal/obj" "cmd/internal/obj"
"cmd/internal/objabi" "cmd/internal/objabi"
...@@ -1008,13 +1009,12 @@ func importfile(f *Val) *types.Pkg { ...@@ -1008,13 +1009,12 @@ func importfile(f *Val) *types.Pkg {
importpkg.Imported = true importpkg.Imported = true
impf, err := os.Open(file) imp, err := bio.Open(file)
if err != nil { if err != nil {
yyerror("can't open import: %q: %v", path_, err) yyerror("can't open import: %q: %v", path_, err)
errorexit() errorexit()
} }
defer impf.Close() defer imp.Close()
imp := bufio.NewReader(impf)
// check object header // check object header
p, err := imp.ReadString('\n') p, err := imp.ReadString('\n')
...@@ -1022,13 +1022,10 @@ func importfile(f *Val) *types.Pkg { ...@@ -1022,13 +1022,10 @@ func importfile(f *Val) *types.Pkg {
yyerror("import %s: reading input: %v", file, err) yyerror("import %s: reading input: %v", file, err)
errorexit() errorexit()
} }
if len(p) > 0 {
p = p[:len(p)-1]
}
if p == "!<arch>" { // package archive if p == "!<arch>\n" { // package archive
// package export block should be first // package export block should be first
sz := arsize(imp, "__.PKGDEF") sz := arsize(imp.Reader, "__.PKGDEF")
if sz <= 0 { if sz <= 0 {
yyerror("import %s: not a package file", file) yyerror("import %s: not a package file", file)
errorexit() errorexit()
...@@ -1038,22 +1035,16 @@ func importfile(f *Val) *types.Pkg { ...@@ -1038,22 +1035,16 @@ func importfile(f *Val) *types.Pkg {
yyerror("import %s: reading input: %v", file, err) yyerror("import %s: reading input: %v", file, err)
errorexit() errorexit()
} }
if len(p) > 0 {
p = p[:len(p)-1]
}
} }
if p != "empty archive" { if !strings.HasPrefix(p, "go object ") {
if !strings.HasPrefix(p, "go object ") { yyerror("import %s: not a go object file: %s", file, p)
yyerror("import %s: not a go object file: %s", file, p) errorexit()
errorexit() }
} q := fmt.Sprintf("%s %s %s %s\n", objabi.GOOS, objabi.GOARCH, objabi.Version, objabi.Expstring())
if p[10:] != q {
q := fmt.Sprintf("%s %s %s %s", objabi.GOOS, objabi.GOARCH, objabi.Version, objabi.Expstring()) yyerror("import %s: object is [%s] expected [%s]", file, p[10:], q)
if p[10:] != q { errorexit()
yyerror("import %s: object is [%s] expected [%s]", file, p[10:], q)
errorexit()
}
} }
// process header lines // process header lines
...@@ -1119,7 +1110,7 @@ func importfile(f *Val) *types.Pkg { ...@@ -1119,7 +1110,7 @@ func importfile(f *Val) *types.Pkg {
fmt.Printf("importing %s (%s)\n", path_, file) fmt.Printf("importing %s (%s)\n", path_, file)
} }
imp.ReadByte() // skip \n after $$B imp.ReadByte() // skip \n after $$B
Import(importpkg, imp) Import(importpkg, imp.Reader)
default: default:
yyerror("no import in %q", path_) yyerror("no import in %q", path_)
......
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