aboutsummaryrefslogtreecommitdiffhomepage
path: root/cgo/cgo_test.go
blob: f852c7f5f92f28ac0969c253b2bdecb8dbf8452b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package cgo

import (
	"bytes"
	"flag"
	"fmt"
	"go/ast"
	"go/format"
	"go/parser"
	"go/scanner"
	"go/token"
	"go/types"
	"os"
	"path/filepath"
	"regexp"
	"runtime"
	"strings"
	"testing"
)

// Pass -update to go test to update the output of the test files.
var flagUpdate = flag.Bool("update", false, "Update images based on test output.")

// normalizeResult normalizes Go source code that comes out of tests across
// platforms and Go versions.
func normalizeResult(t *testing.T, result string) string {
	result = strings.ReplaceAll(result, "\r\n", "\n")

	// This changed to 'undefined:', in Go 1.20.
	result = strings.ReplaceAll(result, ": undeclared name:", ": undefined:")
	// Go 1.20 added a bit more detail
	result = regexp.MustCompile(`(unknown field z in struct literal).*`).ReplaceAllString(result, "$1")

	return result
}

func TestCGo(t *testing.T) {
	var cflags = []string{"--target=armv6m-unknown-unknown-eabi"}

	for _, name := range []string{
		"basic",
		"errors",
		"types",
		"symbols",
		"flags",
		"const",
	} {
		name := name // avoid a race condition
		t.Run(name, func(t *testing.T) {
			// Read the AST in memory.
			path := filepath.Join("testdata", name+".go")
			fset := token.NewFileSet()
			f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
			if err != nil {
				t.Fatal("could not parse Go source file:", err)
			}

			// Process the AST with CGo.
			cgoFiles, _, _, _, _, cgoErrors := Process([]*ast.File{f}, "testdata", "main", fset, cflags, "linux")

			// Check the AST for type errors.
			var typecheckErrors []error
			config := types.Config{
				Error: func(err error) {
					typecheckErrors = append(typecheckErrors, err)
				},
				Importer: newSimpleImporter(),
				Sizes:    types.SizesFor("gccgo", "arm"),
			}
			_, err = config.Check("", fset, append([]*ast.File{f}, cgoFiles...), nil)
			if err != nil && len(typecheckErrors) == 0 {
				// Only report errors when no type errors are found (an
				// unexpected condition).
				t.Error(err)
			}

			// Store the (formatted) output in a buffer. Format it, so it
			// becomes easier to read (and will hopefully change less with CGo
			// changes).
			buf := &bytes.Buffer{}
			if len(cgoErrors) != 0 {
				buf.WriteString("// CGo errors:\n")
				for _, err := range cgoErrors {
					buf.WriteString(formatDiagnostic(err))
				}
				buf.WriteString("\n")
			}
			if len(typecheckErrors) != 0 {
				buf.WriteString("// Type checking errors after CGo processing:\n")
				for _, err := range typecheckErrors {
					buf.WriteString(formatDiagnostic(err))
				}
				buf.WriteString("\n")
			}
			err = format.Node(buf, fset, cgoFiles[0])
			if err != nil {
				t.Errorf("could not write out CGo AST: %v", err)
			}
			actual := normalizeResult(t, buf.String())

			// Read the file with the expected output, to compare against.
			outfile := filepath.Join("testdata", name+".out.go")
			expectedBytes, err := os.ReadFile(outfile)
			if err != nil {
				t.Fatalf("could not read expected output: %v", err)
			}
			expected := strings.ReplaceAll(string(expectedBytes), "\r\n", "\n")

			// Check whether the output is as expected.
			if expected != actual {
				// It is not. Test failed.
				if *flagUpdate {
					// Update the file with the expected data.
					err := os.WriteFile(outfile, []byte(actual), 0666)
					if err != nil {
						t.Error("could not write updated output file:", err)
					}
					return
				}
				t.Errorf("output did not match:\n%s", string(actual))
			}
		})
	}
}

func Test_cgoPackage_isEquivalentAST(t *testing.T) {
	fieldA := &ast.Field{Type: &ast.BasicLit{Kind: token.STRING, Value: "a"}}
	fieldB := &ast.Field{Type: &ast.BasicLit{Kind: token.STRING, Value: "b"}}
	listOfFieldA := &ast.FieldList{List: []*ast.Field{fieldA}}
	listOfFieldB := &ast.FieldList{List: []*ast.Field{fieldB}}
	funcDeclA := &ast.FuncDecl{Name: &ast.Ident{Name: "a"}, Type: &ast.FuncType{Params: &ast.FieldList{}, Results: listOfFieldA}}
	funcDeclB := &ast.FuncDecl{Name: &ast.Ident{Name: "b"}, Type: &ast.FuncType{Params: &ast.FieldList{}, Results: listOfFieldB}}
	funcDeclNoResults := &ast.FuncDecl{Name: &ast.Ident{Name: "C"}, Type: &ast.FuncType{Params: &ast.FieldList{}}}

	testCases := []struct {
		name     string
		a, b     ast.Node
		expected bool
	}{
		{
			name:     "both nil",
			expected: true,
		},
		{
			name:     "not same type",
			a:        fieldA,
			b:        &ast.FuncDecl{},
			expected: false,
		},
		{
			name:     "Field same",
			a:        fieldA,
			b:        fieldA,
			expected: true,
		},
		{
			name:     "Field different",
			a:        fieldA,
			b:        fieldB,
			expected: false,
		},
		{
			name:     "FuncDecl Type Results nil",
			a:        funcDeclNoResults,
			b:        funcDeclNoResults,
			expected: true,
		},
		{
			name:     "FuncDecl Type Results same",
			a:        funcDeclA,
			b:        funcDeclA,
			expected: true,
		},
		{
			name:     "FuncDecl Type Results different",
			a:        funcDeclA,
			b:        funcDeclB,
			expected: false,
		},
		{
			name:     "FuncDecl Type Results a nil",
			a:        funcDeclNoResults,
			b:        funcDeclB,
			expected: false,
		},
		{
			name:     "FuncDecl Type Results b nil",
			a:        funcDeclA,
			b:        funcDeclNoResults,
			expected: false,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			p := &cgoPackage{}
			if got := p.isEquivalentAST(tc.a, tc.b); tc.expected != got {
				t.Errorf("expected %v, got %v", tc.expected, got)
			}
		})
	}
}

// simpleImporter implements the types.Importer interface, but only allows
// importing the syscall and unsafe packages.
type simpleImporter struct {
	syscallPkg *types.Package
}

func newSimpleImporter() *simpleImporter {
	i := &simpleImporter{}

	// Implement a dummy syscall package with the Errno type.
	i.syscallPkg = types.NewPackage("syscall", "syscall")
	obj := types.NewTypeName(token.NoPos, i.syscallPkg, "Errno", nil)
	named := types.NewNamed(obj, nil, nil)
	i.syscallPkg.Scope().Insert(obj)
	named.SetUnderlying(types.Typ[types.Uintptr])
	sig := types.NewSignatureType(nil, nil, nil, types.NewTuple(), types.NewTuple(types.NewParam(token.NoPos, i.syscallPkg, "", types.Typ[types.String])), false)
	named.AddMethod(types.NewFunc(token.NoPos, i.syscallPkg, "Error", sig))
	i.syscallPkg.MarkComplete()

	return i
}

// Import implements the Importer interface. For testing usage only: it only
// supports importing the unsafe package.
func (i *simpleImporter) Import(path string) (*types.Package, error) {
	switch path {
	case "syscall":
		return i.syscallPkg, nil
	case "unsafe":
		return types.Unsafe, nil
	default:
		return nil, fmt.Errorf("importer not implemented for package %s", path)
	}
}

// formatDiagnostic formats the error message to be an indented comment. It
// also fixes Windows path name issues (backward slashes).
func formatDiagnostic(err error) string {
	var msg string
	switch err := err.(type) {
	case scanner.Error:
		msg = err.Pos.String() + ": " + err.Msg
	default:
		msg = err.Error()
	}
	if runtime.GOOS == "windows" {
		// Fix Windows path slashes.
		msg = strings.ReplaceAll(msg, "testdata\\", "testdata/")
	}
	return "//     " + msg + "\n"
}