OSDN Git Service

fb39955ec10f86cb87182eb2f28feb746cbb3845
[android-x86/external-llvm.git] / bindings / go / llvm / ir_test.go
1 //===- ir_test.go - Tests for ir ------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file tests bindings for the ir component.
11 //
12 //===----------------------------------------------------------------------===//
13
14 package llvm
15
16 import (
17         "strings"
18         "testing"
19 )
20
21 func testAttribute(t *testing.T, name string) {
22         mod := NewModule("")
23         defer mod.Dispose()
24
25         ftyp := FunctionType(VoidType(), nil, false)
26         fn := AddFunction(mod, "foo", ftyp)
27
28         kind := AttributeKindID(name)
29         attr := mod.Context().CreateEnumAttribute(kind, 0)
30
31         fn.AddFunctionAttr(attr)
32         newattr := fn.GetEnumFunctionAttribute(kind)
33         if attr != newattr {
34                 t.Errorf("got attribute mask %d, want %d", newattr, attr)
35         }
36
37         text := mod.String()
38         if !strings.Contains(text, " "+name+" ") {
39                 t.Errorf("expected attribute '%s', got:\n%s", name, text)
40         }
41
42         fn.RemoveEnumFunctionAttribute(kind)
43         newattr = fn.GetEnumFunctionAttribute(kind)
44         if !newattr.IsNil() {
45                 t.Errorf("got attribute mask %d, want 0", newattr)
46         }
47 }
48
49 func TestAttributes(t *testing.T) {
50         // Tests that our attribute constants haven't drifted from LLVM's.
51         attrTests := []string{
52                 "sanitize_address",
53                 "alwaysinline",
54                 "builtin",
55                 "byval",
56                 "convergent",
57                 "inalloca",
58                 "inlinehint",
59                 "inreg",
60                 "jumptable",
61                 "minsize",
62                 "naked",
63                 "nest",
64                 "noalias",
65                 "nobuiltin",
66                 "nocapture",
67                 "noduplicate",
68                 "noimplicitfloat",
69                 "noinline",
70                 "nonlazybind",
71                 "nonnull",
72                 "noredzone",
73                 "noreturn",
74                 "nounwind",
75                 "optnone",
76                 "optsize",
77                 "readnone",
78                 "readonly",
79                 "returned",
80                 "returns_twice",
81                 "signext",
82                 "safestack",
83                 "ssp",
84                 "sspreq",
85                 "sspstrong",
86                 "sret",
87                 "sanitize_thread",
88                 "sanitize_memory",
89                 "uwtable",
90                 "zeroext",
91                 "cold",
92         }
93
94         for _, name := range attrTests {
95                 testAttribute(t, name)
96         }
97 }
98
99 func TestDebugLoc(t *testing.T) {
100         mod := NewModule("")
101         defer mod.Dispose()
102
103         ctx := mod.Context()
104
105         b := ctx.NewBuilder()
106         defer b.Dispose()
107
108         d := NewDIBuilder(mod)
109         defer func() {
110                 d.Destroy()
111         }()
112         file := d.CreateFile("dummy_file", "dummy_dir")
113         voidInfo := d.CreateBasicType(DIBasicType{Name: "void"})
114         typeInfo := d.CreateSubroutineType(DISubroutineType{file, []Metadata{voidInfo}})
115         scope := d.CreateFunction(file, DIFunction{
116                 Name:         "foo",
117                 LinkageName:  "foo",
118                 Line:         10,
119                 ScopeLine:    10,
120                 Type:         typeInfo,
121                 File:         file,
122                 IsDefinition: true,
123         })
124
125         b.SetCurrentDebugLocation(10, 20, scope, Metadata{})
126         loc := b.GetCurrentDebugLocation()
127         if loc.Line != 10 {
128                 t.Errorf("Got line %d, though wanted 10", loc.Line)
129         }
130         if loc.Col != 20 {
131                 t.Errorf("Got column %d, though wanted 20", loc.Col)
132         }
133         if loc.Scope.C != scope.C {
134                 t.Errorf("Got metadata %v as scope, though wanted %v", loc.Scope.C, scope.C)
135         }
136 }
137
138 func TestSubtypes(t *testing.T) {
139         cont := NewContext()
140         defer cont.Dispose()
141
142         int_pointer := PointerType(cont.Int32Type(), 0)
143         int_inner := int_pointer.Subtypes()
144         if len(int_inner) != 1 {
145                 t.Errorf("Got size %d, though wanted 1", len(int_inner))
146         }
147         if int_inner[0] != cont.Int32Type() {
148                 t.Errorf("Expected int32 type")
149         }
150
151         st_pointer := cont.StructType([]Type{cont.Int32Type(), cont.Int8Type()}, false)
152         st_inner := st_pointer.Subtypes()
153         if len(st_inner) != 2 {
154                 t.Errorf("Got size %d, though wanted 2", len(int_inner))
155         }
156         if st_inner[0] != cont.Int32Type() {
157                 t.Errorf("Expected first struct field to be int32")
158         }
159         if st_inner[1] != cont.Int8Type() {
160                 t.Errorf("Expected second struct field to be int8")
161         }
162 }