]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/debug/dwarf/type_test.go
Add Go frontend, libgo library, and Go testsuite.
[thirdparty/gcc.git] / libgo / go / debug / dwarf / type_test.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package dwarf_test
6
7 import (
8 . "debug/dwarf"
9 "debug/elf"
10 "debug/macho"
11 "testing"
12 )
13
14 var typedefTests = map[string]string{
15 "t_ptr_volatile_int": "*volatile int",
16 "t_ptr_const_char": "*const char",
17 "t_long": "long int",
18 "t_ushort": "short unsigned int",
19 "t_func_int_of_float_double": "func(float, double) int",
20 "t_ptr_func_int_of_float_double": "*func(float, double) int",
21 "t_func_ptr_int_of_char_schar_uchar": "func(char, signed char, unsigned char) *int",
22 "t_func_void_of_char": "func(char) void",
23 "t_func_void_of_void": "func() void",
24 "t_func_void_of_ptr_char_dots": "func(*char, ...) void",
25 "t_my_struct": "struct my_struct {vi volatile int@0; x char@4 : 1@7; y int@4 : 4@27; array [40]long long int@8}",
26 "t_my_union": "union my_union {vi volatile int@0; x char@0 : 1@7; y int@0 : 4@28; array [40]long long int@0}",
27 "t_my_enum": "enum my_enum {e1=1; e2=2; e3=-5; e4=1000000000000000}",
28 "t_my_list": "struct list {val short int@0; next *t_my_list@8}",
29 "t_my_tree": "struct tree {left *struct tree@0; right *struct tree@8; val long long unsigned int@16}",
30 }
31
32 func elfData(t *testing.T, name string) *Data {
33 f, err := elf.Open(name)
34 if err != nil {
35 t.Fatal(err)
36 }
37
38 d, err := f.DWARF()
39 if err != nil {
40 t.Fatal(err)
41 }
42 return d
43 }
44
45 func machoData(t *testing.T, name string) *Data {
46 f, err := macho.Open(name)
47 if err != nil {
48 t.Fatal(err)
49 }
50
51 d, err := f.DWARF()
52 if err != nil {
53 t.Fatal(err)
54 }
55 return d
56 }
57
58
59 func TestTypedefsELF(t *testing.T) { testTypedefs(t, elfData(t, "testdata/typedef.elf")) }
60
61 func TestTypedefsMachO(t *testing.T) {
62 testTypedefs(t, machoData(t, "testdata/typedef.macho"))
63 }
64
65 func testTypedefs(t *testing.T, d *Data) {
66 r := d.Reader()
67 seen := make(map[string]bool)
68 for {
69 e, err := r.Next()
70 if err != nil {
71 t.Fatal("r.Next:", err)
72 }
73 if e == nil {
74 break
75 }
76 if e.Tag == TagTypedef {
77 typ, err := d.Type(e.Offset)
78 if err != nil {
79 t.Fatal("d.Type:", err)
80 }
81 t1 := typ.(*TypedefType)
82 var typstr string
83 if ts, ok := t1.Type.(*StructType); ok {
84 typstr = ts.Defn()
85 } else {
86 typstr = t1.Type.String()
87 }
88
89 if want, ok := typedefTests[t1.Name]; ok {
90 if seen[t1.Name] {
91 t.Errorf("multiple definitions for %s", t1.Name)
92 }
93 seen[t1.Name] = true
94 if typstr != want {
95 t.Errorf("%s:\n\thave %s\n\twant %s", t1.Name, typstr, want)
96 }
97 }
98 }
99 if e.Tag != TagCompileUnit {
100 r.SkipChildren()
101 }
102 }
103
104 for k := range typedefTests {
105 if !seen[k] {
106 t.Errorf("missing %s", k)
107 }
108 }
109 }