Skip to content

Commit 984be3b

Browse files
committed
go/internal/gccgoimporter: enhance for new export data, fix test issues
This patch merges in support for reading indexed type export data, from the gofrontend CL https://golang.org/cl/143022 (which includes a change in the export data version number from V2 to V3). Also fixes the key tests to insure that they run both in gccgo builds and main Go repo builds if "gccgo" is present (prior to this the tests were not running in either scenario); this required fixing up some of the expected results. Fixes #28961. Change-Id: I644d171f2a46be9160f89dada06ab3c20468bab7 Reviewed-on: https://go-review.googlesource.com/c/149957 Run-TryBot: Than McIntosh <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent ca37492 commit 984be3b

File tree

5 files changed

+214
-87
lines changed

5 files changed

+214
-87
lines changed

src/go/internal/gccgoimporter/gccgoinstallation_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package gccgoimporter
66

77
import (
88
"go/types"
9-
"runtime"
109
"testing"
1110
)
1211

@@ -144,14 +143,14 @@ var importablePackages = [...]string{
144143
}
145144

146145
func TestInstallationImporter(t *testing.T) {
147-
// This test relies on gccgo being around, which it most likely will be if we
148-
// were compiled with gccgo.
149-
if runtime.Compiler != "gccgo" {
146+
// This test relies on gccgo being around.
147+
gpath := gccgoPath()
148+
if gpath == "" {
150149
t.Skip("This test needs gccgo")
151150
}
152151

153152
var inst GccgoInstallation
154-
err := inst.InitFromDriver("gccgo")
153+
err := inst.InitFromDriver(gpath)
155154
if err != nil {
156155
t.Fatal(err)
157156
}
@@ -176,12 +175,12 @@ func TestInstallationImporter(t *testing.T) {
176175

177176
// Test for certain specific entities in the imported data.
178177
for _, test := range [...]importerTest{
179-
{pkgpath: "io", name: "Reader", want: "type Reader interface{Read(p []uint8) (n int, err error)}"},
178+
{pkgpath: "io", name: "Reader", want: "type Reader interface{Read(p []byte) (n int, err error)}"},
180179
{pkgpath: "io", name: "ReadWriter", want: "type ReadWriter interface{Reader; Writer}"},
181180
{pkgpath: "math", name: "Pi", want: "const Pi untyped float"},
182181
{pkgpath: "math", name: "Sin", want: "func Sin(x float64) float64"},
183182
{pkgpath: "sort", name: "Ints", want: "func Ints(a []int)"},
184-
{pkgpath: "unsafe", name: "Pointer", want: "type Pointer unsafe.Pointer"},
183+
{pkgpath: "unsafe", name: "Pointer", want: "type Pointer"},
185184
} {
186185
runImporterTest(t, imp, nil, &test)
187186
}

src/go/internal/gccgoimporter/importer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func findExportFile(searchpaths []string, pkgpath string) (string, error) {
6262
const (
6363
gccgov1Magic = "v1;\n"
6464
gccgov2Magic = "v2;\n"
65+
gccgov3Magic = "v3;\n"
6566
goimporterMagic = "\n$$ "
6667
archiveMagic = "!<ar"
6768
)
@@ -90,7 +91,7 @@ func openExportFile(fpath string) (reader io.ReadSeeker, closer io.Closer, err e
9091

9192
var elfreader io.ReaderAt
9293
switch string(magic[:]) {
93-
case gccgov1Magic, gccgov2Magic, goimporterMagic:
94+
case gccgov1Magic, gccgov2Magic, gccgov3Magic, goimporterMagic:
9495
// Raw export data.
9596
reader = f
9697
return
@@ -195,7 +196,7 @@ func GetImporter(searchpaths []string, initmap map[*types.Package]InitData) Impo
195196
}
196197

197198
switch magics {
198-
case gccgov1Magic, gccgov2Magic:
199+
case gccgov1Magic, gccgov2Magic, gccgov3Magic:
199200
var p parser
200201
p.init(fpath, reader, imports)
201202
pkg = p.parsePackage()

src/go/internal/gccgoimporter/importer_test.go

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"os"
1212
"os/exec"
1313
"path/filepath"
14-
"runtime"
1514
"testing"
1615
)
1716

@@ -53,9 +52,6 @@ func runImporterTest(t *testing.T, imp Importer, initmap map[*types.Package]Init
5352
// Check that the package's own init function has the package's priority
5453
for _, pkginit := range initdata.Inits {
5554
if pkginit.InitFunc == test.wantinits[0] {
56-
if initdata.Priority != pkginit.Priority {
57-
t.Errorf("%s: got self priority %d; want %d", test.pkgpath, pkginit.Priority, initdata.Priority)
58-
}
5955
found = true
6056
break
6157
}
@@ -65,27 +61,11 @@ func runImporterTest(t *testing.T, imp Importer, initmap map[*types.Package]Init
6561
t.Errorf("%s: could not find expected function %q", test.pkgpath, test.wantinits[0])
6662
}
6763

68-
// Each init function in the list other than the first one is a
69-
// dependency of the function immediately before it. Check that
70-
// the init functions appear in descending priority order.
71-
priority := initdata.Priority
72-
for _, wantdepinit := range test.wantinits[1:] {
73-
found = false
74-
for _, pkginit := range initdata.Inits {
75-
if pkginit.InitFunc == wantdepinit {
76-
if priority <= pkginit.Priority {
77-
t.Errorf("%s: got dep priority %d; want less than %d", test.pkgpath, pkginit.Priority, priority)
78-
}
79-
found = true
80-
priority = pkginit.Priority
81-
break
82-
}
83-
}
84-
85-
if !found {
86-
t.Errorf("%s: could not find expected function %q", test.pkgpath, wantdepinit)
87-
}
88-
}
64+
// FIXME: the original version of this test was written against
65+
// the v1 export data scheme for capturing init functions, so it
66+
// verified the priority values. We moved away from the priority
67+
// scheme some time ago; it is not clear how much work it would be
68+
// to validate the new init export data.
8969
}
9070
}
9171

@@ -100,7 +80,7 @@ var importerTests = [...]importerTest{
10080
{pkgpath: "time", name: "Nanosecond", want: "const Nanosecond Duration", wantval: "1"},
10181
{pkgpath: "unicode", name: "IsUpper", want: "func IsUpper(r rune) bool"},
10282
{pkgpath: "unicode", name: "MaxRune", want: "const MaxRune untyped rune", wantval: "1114111"},
103-
{pkgpath: "imports", wantinits: []string{"imports..import", "fmt..import", "math..import"}},
83+
{pkgpath: "imports", wantinits: []string{"imports..import", "fmt..import"}},
10484
{pkgpath: "importsar", name: "Hello", want: "var Hello string"},
10585
{pkgpath: "aliases", name: "A14", want: "type A14 = func(int, T0) chan T2"},
10686
{pkgpath: "aliases", name: "C0", want: "type C0 struct{f1 C1; f2 C1}"},
@@ -109,8 +89,7 @@ var importerTests = [...]importerTest{
10989
}
11090

11191
func TestGoxImporter(t *testing.T) {
112-
testenv.MustHaveGoBuild(t)
113-
92+
testenv.MustHaveExec(t) // this is to skip nacl, js
11493
initmap := make(map[*types.Package]InitData)
11594
imp := GetImporter([]string{"testdata"}, initmap)
11695

@@ -119,12 +98,24 @@ func TestGoxImporter(t *testing.T) {
11998
}
12099
}
121100

122-
func TestObjImporter(t *testing.T) {
123-
testenv.MustHaveGoBuild(t)
101+
// gccgoPath returns a path to gccgo if it is present (either in
102+
// path or specified via GCCGO environment variable), or an
103+
// empty string if no gccgo is available.
104+
func gccgoPath() string {
105+
gccgoname := os.Getenv("GCCGO")
106+
if gccgoname == "" {
107+
gccgoname = "gccgo"
108+
}
109+
if gpath, gerr := exec.LookPath(gccgoname); gerr == nil {
110+
return gpath
111+
}
112+
return ""
113+
}
124114

125-
// This test relies on gccgo being around, which it most likely will be if we
126-
// were compiled with gccgo.
127-
if runtime.Compiler != "gccgo" {
115+
func TestObjImporter(t *testing.T) {
116+
// This test relies on gccgo being around.
117+
gpath := gccgoPath()
118+
if gpath == "" {
128119
t.Skip("This test needs gccgo")
129120
}
130121

@@ -144,10 +135,13 @@ func TestObjImporter(t *testing.T) {
144135

145136
for _, test := range importerTests {
146137
gofile := filepath.Join("testdata", test.pkgpath+".go")
138+
if _, err := os.Stat(gofile); os.IsNotExist(err) {
139+
continue
140+
}
147141
ofile := filepath.Join(tmpdir, test.pkgpath+".o")
148142
afile := filepath.Join(artmpdir, "lib"+test.pkgpath+".a")
149143

150-
cmd := exec.Command("gccgo", "-fgo-pkgpath="+test.pkgpath, "-c", "-o", ofile, gofile)
144+
cmd := exec.Command(gpath, "-fgo-pkgpath="+test.pkgpath, "-c", "-o", ofile, gofile)
151145
out, err := cmd.CombinedOutput()
152146
if err != nil {
153147
t.Logf("%s", out)

0 commit comments

Comments
 (0)