1
2
3
4
5
6
7
8
9 package gccgoimporter
10
11 import (
12 "go/types"
13 "os"
14 "os/exec"
15 "path/filepath"
16 "regexp"
17 "runtime"
18 "strconv"
19 "testing"
20 )
21
22 type importerTest struct {
23 pkgpath, name, want, wantval string
24 wantinits []string
25 gccgoVersion int
26 }
27
28 func runImporterTest(t *testing.T, imp Importer, initmap map[*types.Package]InitData, test *importerTest) {
29 pkg, err := imp(make(map[string]*types.Package), test.pkgpath, ".", nil)
30 if err != nil {
31 t.Error(err)
32 return
33 }
34
35 if test.name != "" {
36 obj := pkg.Scope().Lookup(test.name)
37 if obj == nil {
38 t.Errorf("%s: object not found", test.name)
39 return
40 }
41
42 got := types.ObjectString(obj, types.RelativeTo(pkg))
43 if got != test.want {
44 t.Errorf("%s: got %q; want %q", test.name, got, test.want)
45 }
46
47 if test.wantval != "" {
48 gotval := obj.(*types.Const).Val().String()
49 if gotval != test.wantval {
50 t.Errorf("%s: got val %q; want val %q", test.name, gotval, test.wantval)
51 }
52 }
53 }
54
55 if len(test.wantinits) > 0 {
56 initdata := initmap[pkg]
57 found := false
58
59 for _, pkginit := range initdata.Inits {
60 if pkginit.InitFunc == test.wantinits[0] {
61 found = true
62 break
63 }
64 }
65
66 if !found {
67 t.Errorf("%s: could not find expected function %q", test.pkgpath, test.wantinits[0])
68 }
69
70
71
72
73
74
75 }
76 }
77
78
79
80 var importerTests = [...]importerTest{
81 {pkgpath: "pointer", name: "Int8Ptr", want: "type Int8Ptr *int8"},
82 {pkgpath: "complexnums", name: "NN", want: "const NN untyped complex", wantval: "(-1 + -1i)"},
83 {pkgpath: "complexnums", name: "NP", want: "const NP untyped complex", wantval: "(-1 + 1i)"},
84 {pkgpath: "complexnums", name: "PN", want: "const PN untyped complex", wantval: "(1 + -1i)"},
85 {pkgpath: "complexnums", name: "PP", want: "const PP untyped complex", wantval: "(1 + 1i)"},
86 {pkgpath: "conversions", name: "Bits", want: "const Bits Units", wantval: `"bits"`},
87 {pkgpath: "time", name: "Duration", want: "type Duration int64"},
88 {pkgpath: "time", name: "Nanosecond", want: "const Nanosecond Duration", wantval: "1"},
89 {pkgpath: "unicode", name: "IsUpper", want: "func IsUpper(r rune) bool"},
90 {pkgpath: "unicode", name: "MaxRune", want: "const MaxRune untyped rune", wantval: "1114111"},
91 {pkgpath: "imports", wantinits: []string{"imports..import", "fmt..import"}},
92 {pkgpath: "importsar", name: "Hello", want: "var Hello string"},
93 {pkgpath: "aliases", name: "A14", gccgoVersion: 7, want: "type A14 = func(int, T0) chan T2"},
94 {pkgpath: "aliases", name: "C0", gccgoVersion: 7, want: "type C0 struct{f1 C1; f2 C1}"},
95 {pkgpath: "escapeinfo", name: "NewT", want: "func NewT(data []byte) *T"},
96 {pkgpath: "issue27856", name: "M", gccgoVersion: 7, want: "type M struct{E F}"},
97 {pkgpath: "v1reflect", name: "Type", want: "type Type interface{Align() int; AssignableTo(u Type) bool; Bits() int; ChanDir() ChanDir; Elem() Type; Field(i int) StructField; FieldAlign() int; FieldByIndex(index []int) StructField; FieldByName(name string) (StructField, bool); FieldByNameFunc(match func(string) bool) (StructField, bool); Implements(u Type) bool; In(i int) Type; IsVariadic() bool; Key() Type; Kind() Kind; Len() int; Method(int) Method; MethodByName(string) (Method, bool); Name() string; NumField() int; NumIn() int; NumMethod() int; NumOut() int; Out(i int) Type; PkgPath() string; Size() uintptr; String() string; common() *commonType; rawString() string; runtimeType() *runtimeType; uncommon() *uncommonType}"},
98 {pkgpath: "nointerface", name: "I", want: "type I int"},
99 {pkgpath: "issue29198", name: "FooServer", gccgoVersion: 7, want: "type FooServer struct{FooServer *FooServer; user string; ctx context.Context}"},
100 {pkgpath: "issue30628", name: "Apple", want: "type Apple struct{hey sync.RWMutex; x int; RQ [517]struct{Count uintptr; NumBytes uintptr; Last uintptr}}"},
101 {pkgpath: "issue31540", name: "S", gccgoVersion: 7, want: "type S struct{b int; map[Y]Z}"},
102 {pkgpath: "issue34182", name: "T1", want: "type T1 struct{f *T2}"},
103 {pkgpath: "notinheap", name: "S", want: "type S struct{}"},
104 }
105
106 func TestGoxImporter(t *testing.T) {
107 testenv.MustHaveExec(t)
108 initmap := make(map[*types.Package]InitData)
109 imp := GetImporter([]string{"testdata"}, initmap)
110
111 for _, test := range importerTests {
112 runImporterTest(t, imp, initmap, &test)
113 }
114 }
115
116
117
118
119 func gccgoPath() string {
120 gccgoname := os.Getenv("GCCGO")
121 if gccgoname == "" {
122 gccgoname = "gccgo"
123 }
124 if gpath, gerr := exec.LookPath(gccgoname); gerr == nil {
125 return gpath
126 }
127 return ""
128 }
129
130 func TestObjImporter(t *testing.T) {
131
132 gpath := gccgoPath()
133 if gpath == "" {
134 t.Skip("This test needs gccgo")
135 }
136 if runtime.GOOS == "aix" {
137
138
139
140 t.Skip("no support yet for debug/xcoff")
141 }
142
143 verout, err := exec.Command(gpath, "--version").Output()
144 if err != nil {
145 t.Logf("%s", verout)
146 if exit, ok := err.(*exec.ExitError); ok && len(exit.Stderr) > 0 {
147 t.Logf("stderr:\n%s", exit.Stderr)
148 }
149 t.Fatal(err)
150 }
151 vers := regexp.MustCompile(`([0-9]+)\.([0-9]+)`).FindSubmatch(verout)
152 if len(vers) == 0 {
153 t.Fatalf("could not find version number in %s", verout)
154 }
155 major, err := strconv.Atoi(string(vers[1]))
156 if err != nil {
157 t.Fatal(err)
158 }
159 minor, err := strconv.Atoi(string(vers[2]))
160 if err != nil {
161 t.Fatal(err)
162 }
163 t.Logf("gccgo version %d.%d", major, minor)
164
165 tmpdir := t.TempDir()
166 initmap := make(map[*types.Package]InitData)
167 imp := GetImporter([]string{tmpdir}, initmap)
168
169 artmpdir := t.TempDir()
170 arinitmap := make(map[*types.Package]InitData)
171 arimp := GetImporter([]string{artmpdir}, arinitmap)
172
173 for _, test := range importerTests {
174 if major < test.gccgoVersion {
175
176 t.Logf("skipping %q: not supported before gccgo version %d", test.pkgpath, test.gccgoVersion)
177 continue
178 }
179
180 gofile := filepath.Join("testdata", test.pkgpath+".go")
181 if _, err := os.Stat(gofile); os.IsNotExist(err) {
182 continue
183 }
184 ofile := filepath.Join(tmpdir, test.pkgpath+".o")
185 afile := filepath.Join(artmpdir, "lib"+test.pkgpath+".a")
186
187 cmd := exec.Command(gpath, "-fgo-pkgpath="+test.pkgpath, "-c", "-o", ofile, gofile)
188 if out, err := cmd.CombinedOutput(); err != nil {
189 t.Logf("%s", out)
190 t.Fatalf("gccgo %s failed: %s", gofile, err)
191 }
192
193 runImporterTest(t, imp, initmap, &test)
194
195 cmd = exec.Command("ar", "cr", afile, ofile)
196 if out, err := cmd.CombinedOutput(); err != nil {
197 t.Logf("%s", out)
198 t.Fatalf("ar cr %s %s failed: %s", afile, ofile, err)
199 }
200
201 runImporterTest(t, arimp, arinitmap, &test)
202
203 if err = os.Remove(ofile); err != nil {
204 t.Fatal(err)
205 }
206 if err = os.Remove(afile); err != nil {
207 t.Fatal(err)
208 }
209 }
210 }
211
View as plain text