summaryrefslogtreecommitdiffstats
path: root/misc/cgo/testgodefs
diff options
context:
space:
mode:
Diffstat (limited to 'misc/cgo/testgodefs')
-rw-r--r--misc/cgo/testgodefs/testdata/anonunion.go26
-rw-r--r--misc/cgo/testgodefs/testdata/bitfields.go31
-rw-r--r--misc/cgo/testgodefs/testdata/fieldtypedef.go18
-rw-r--r--misc/cgo/testgodefs/testdata/issue37479.go33
-rw-r--r--misc/cgo/testgodefs/testdata/issue37621.go23
-rw-r--r--misc/cgo/testgodefs/testdata/issue38649.go15
-rw-r--r--misc/cgo/testgodefs/testdata/issue39534.go12
-rw-r--r--misc/cgo/testgodefs/testdata/issue48396.go18
-rw-r--r--misc/cgo/testgodefs/testdata/issue8478.go20
-rw-r--r--misc/cgo/testgodefs/testdata/main.go57
-rw-r--r--misc/cgo/testgodefs/testgodefs_test.go112
11 files changed, 365 insertions, 0 deletions
diff --git a/misc/cgo/testgodefs/testdata/anonunion.go b/misc/cgo/testgodefs/testdata/anonunion.go
new file mode 100644
index 0000000..18840f2
--- /dev/null
+++ b/misc/cgo/testgodefs/testdata/anonunion.go
@@ -0,0 +1,26 @@
+// Copyright 2014 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+//
+// +build ignore
+
+package main
+
+// This file tests that when cgo -godefs sees a struct with a field
+// that is an anonymous union, the first field in the union is
+// promoted to become a field of the struct. See issue 6677 for
+// background.
+
+/*
+typedef struct {
+ union {
+ long l;
+ int c;
+ };
+} t;
+*/
+import "C"
+
+// Input for cgo -godefs.
+
+type T C.t
diff --git a/misc/cgo/testgodefs/testdata/bitfields.go b/misc/cgo/testgodefs/testdata/bitfields.go
new file mode 100644
index 0000000..6a9724d
--- /dev/null
+++ b/misc/cgo/testgodefs/testdata/bitfields.go
@@ -0,0 +1,31 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+//
+// +build ignore
+
+package main
+
+// This file tests that we don't generate an incorrect field location
+// for a bitfield that appears aligned.
+
+/*
+struct bitfields {
+ unsigned int B1 : 5;
+ unsigned int B2 : 1;
+ unsigned int B3 : 1;
+ unsigned int B4 : 1;
+ unsigned int Short1 : 16; // misaligned on 8 bit boundary
+ unsigned int B5 : 1;
+ unsigned int B6 : 1;
+ unsigned int B7 : 1;
+ unsigned int B8 : 1;
+ unsigned int B9 : 1;
+ unsigned int B10 : 3;
+ unsigned int Short2 : 16; // alignment is OK
+ unsigned int Short3 : 16; // alignment is OK
+};
+*/
+import "C"
+
+type bitfields C.struct_bitfields
diff --git a/misc/cgo/testgodefs/testdata/fieldtypedef.go b/misc/cgo/testgodefs/testdata/fieldtypedef.go
new file mode 100644
index 0000000..45c0bf8
--- /dev/null
+++ b/misc/cgo/testgodefs/testdata/fieldtypedef.go
@@ -0,0 +1,18 @@
+// Copyright 2018 The Go Authors. All rights reserve d.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+//
+// +build ignore
+
+package main
+
+/*
+struct S1 { int f1; };
+struct S2 { struct S1 s1; };
+typedef struct S1 S1Type;
+typedef struct S2 S2Type;
+*/
+import "C"
+
+type S1 C.S1Type
+type S2 C.S2Type
diff --git a/misc/cgo/testgodefs/testdata/issue37479.go b/misc/cgo/testgodefs/testdata/issue37479.go
new file mode 100644
index 0000000..a210eb5
--- /dev/null
+++ b/misc/cgo/testgodefs/testdata/issue37479.go
@@ -0,0 +1,33 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+//
+// +build ignore
+
+package main
+
+/*
+typedef struct A A;
+
+typedef struct {
+ struct A *next;
+ struct A **prev;
+} N;
+
+struct A
+{
+ N n;
+};
+
+typedef struct B
+{
+ A* a;
+} B;
+*/
+import "C"
+
+type N C.N
+
+type A C.A
+
+type B C.B
diff --git a/misc/cgo/testgodefs/testdata/issue37621.go b/misc/cgo/testgodefs/testdata/issue37621.go
new file mode 100644
index 0000000..d5ace3f
--- /dev/null
+++ b/misc/cgo/testgodefs/testdata/issue37621.go
@@ -0,0 +1,23 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+//
+// +build ignore
+
+package main
+
+/*
+struct tt {
+ long long a;
+ long long b;
+};
+
+struct s {
+ struct tt ts[3];
+};
+*/
+import "C"
+
+type TT C.struct_tt
+
+type S C.struct_s
diff --git a/misc/cgo/testgodefs/testdata/issue38649.go b/misc/cgo/testgodefs/testdata/issue38649.go
new file mode 100644
index 0000000..6af74d6
--- /dev/null
+++ b/misc/cgo/testgodefs/testdata/issue38649.go
@@ -0,0 +1,15 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+//
+// +build ignore
+
+package main
+
+/*
+struct Issue38649 { int x; };
+#define issue38649 struct Issue38649
+*/
+import "C"
+
+type issue38649 C.issue38649
diff --git a/misc/cgo/testgodefs/testdata/issue39534.go b/misc/cgo/testgodefs/testdata/issue39534.go
new file mode 100644
index 0000000..9899ba1
--- /dev/null
+++ b/misc/cgo/testgodefs/testdata/issue39534.go
@@ -0,0 +1,12 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+//
+// +build ignore
+
+package main
+
+// enum { ENUMVAL = 0x1 };
+import "C"
+
+const ENUMVAL = C.ENUMVAL
diff --git a/misc/cgo/testgodefs/testdata/issue48396.go b/misc/cgo/testgodefs/testdata/issue48396.go
new file mode 100644
index 0000000..d4c1924
--- /dev/null
+++ b/misc/cgo/testgodefs/testdata/issue48396.go
@@ -0,0 +1,18 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+//
+// +build ignore
+
+package main
+
+/*
+// from <linux/kcm.h>
+struct issue48396 {
+ int fd;
+ int bpf_fd;
+};
+*/
+import "C"
+
+type Issue48396 C.struct_issue48396
diff --git a/misc/cgo/testgodefs/testdata/issue8478.go b/misc/cgo/testgodefs/testdata/issue8478.go
new file mode 100644
index 0000000..2321446
--- /dev/null
+++ b/misc/cgo/testgodefs/testdata/issue8478.go
@@ -0,0 +1,20 @@
+// Copyright 2014 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+//
+// +build ignore
+
+package main
+
+// Issue 8478. Test that void* is consistently mapped to *byte.
+
+/*
+typedef struct {
+ void *p;
+ void **q;
+ void ***r;
+} s;
+*/
+import "C"
+
+type Issue8478 C.s
diff --git a/misc/cgo/testgodefs/testdata/main.go b/misc/cgo/testgodefs/testdata/main.go
new file mode 100644
index 0000000..5c670f3
--- /dev/null
+++ b/misc/cgo/testgodefs/testdata/main.go
@@ -0,0 +1,57 @@
+// Copyright 2014 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+ "fmt"
+ "os"
+ "reflect"
+)
+
+// Test that the struct field in anonunion.go was promoted.
+var v1 T
+var v2 = v1.L
+
+// Test that P, Q, and R all point to byte.
+var v3 = Issue8478{P: (*byte)(nil), Q: (**byte)(nil), R: (***byte)(nil)}
+
+// Test that N, A and B are fully defined
+var v4 = N{}
+var v5 = A{}
+var v6 = B{}
+
+// Test that S is fully defined
+var v7 = S{}
+
+// Test that #define'd type is fully defined
+var _ = issue38649{X: 0}
+
+// Test that prefixes do not cause duplicate field names.
+var _ = Issue48396{Fd: 1, Bpf_fd: 2}
+
+func main() {
+ pass := true
+
+ // The Go translation of bitfields should not have any of the
+ // bitfield types. The order in which bitfields are laid out
+ // in memory is implementation defined, so we can't easily
+ // know how a bitfield should correspond to a Go type, even if
+ // it appears to be aligned correctly.
+ bitfieldType := reflect.TypeOf(bitfields{})
+ check := func(name string) {
+ _, ok := bitfieldType.FieldByName(name)
+ if ok {
+ fmt.Fprintf(os.Stderr, "found unexpected bitfields field %s\n", name)
+ pass = false
+ }
+ }
+ check("Short1")
+ check("Short2")
+ check("Short3")
+
+ if !pass {
+ os.Exit(1)
+ }
+}
diff --git a/misc/cgo/testgodefs/testgodefs_test.go b/misc/cgo/testgodefs/testgodefs_test.go
new file mode 100644
index 0000000..d03769e
--- /dev/null
+++ b/misc/cgo/testgodefs/testgodefs_test.go
@@ -0,0 +1,112 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package testgodefs
+
+import (
+ "bytes"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "runtime"
+ "strings"
+ "testing"
+)
+
+// We are testing cgo -godefs, which translates Go files that use
+// import "C" into Go files with Go definitions of types defined in the
+// import "C" block. Add more tests here.
+var filePrefixes = []string{
+ "anonunion",
+ "bitfields",
+ "issue8478",
+ "fieldtypedef",
+ "issue37479",
+ "issue37621",
+ "issue38649",
+ "issue39534",
+ "issue48396",
+}
+
+func TestGoDefs(t *testing.T) {
+ testdata, err := filepath.Abs("testdata")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ gopath, err := os.MkdirTemp("", "testgodefs-gopath")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(gopath)
+
+ dir := filepath.Join(gopath, "src", "testgodefs")
+ if err := os.MkdirAll(dir, 0755); err != nil {
+ t.Fatal(err)
+ }
+
+ for _, fp := range filePrefixes {
+ cmd := exec.Command("go", "tool", "cgo",
+ "-godefs",
+ "-srcdir", testdata,
+ "-objdir", dir,
+ fp+".go")
+ cmd.Stderr = new(bytes.Buffer)
+
+ out, err := cmd.Output()
+ if err != nil {
+ t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, cmd.Stderr)
+ }
+
+ fn := fp + "_defs.go"
+ if err := os.WriteFile(filepath.Join(dir, fn), out, 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ // Verify that command line arguments are not rewritten in the generated comment,
+ // see go.dev/issue/52063
+ hasGeneratedByComment := false
+ for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
+ cgoExe := "cgo"
+ if runtime.GOOS == "windows" {
+ cgoExe = "cgo.exe"
+ }
+ if !strings.HasPrefix(line, "// "+cgoExe+" -godefs") {
+ continue
+ }
+ if want := "// " + cgoExe + " " + strings.Join(cmd.Args[3:], " "); line != want {
+ t.Errorf("%s: got generated comment %q, want %q", fn, line, want)
+ }
+ hasGeneratedByComment = true
+ break
+ }
+
+ if !hasGeneratedByComment {
+ t.Errorf("%s: comment with generating cgo -godefs command not found", fn)
+ }
+ }
+
+ main, err := os.ReadFile(filepath.Join("testdata", "main.go"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(dir, "main.go"), main, 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module testgodefs\ngo 1.14\n"), 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ // Use 'go run' to build and run the resulting binary in a single step,
+ // instead of invoking 'go build' and the resulting binary separately, so that
+ // this test can pass on mobile builders, which do not copy artifacts back
+ // from remote invocations.
+ cmd := exec.Command("go", "run", ".")
+ cmd.Env = append(os.Environ(), "GOPATH="+gopath)
+ cmd.Dir = dir
+ if out, err := cmd.CombinedOutput(); err != nil {
+ t.Fatalf("%s [%s]: %v\n%s", strings.Join(cmd.Args, " "), dir, err, out)
+ }
+}