5cd782b7d96f7f9533fc3b921a45a65a7c0935cf
[lldb.git] / llvm / 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                 "nocf_check",
93         }
94
95         for _, name := range attrTests {
96                 testAttribute(t, name)
97         }
98 }
99
100 func TestDebugLoc(t *testing.T) {
101         mod := NewModule("")
102         defer mod.Dispose()
103
104         ctx := mod.Context()
105
106         b := ctx.NewBuilder()
107         defer b.Dispose()
108
109         d := NewDIBuilder(mod)
110         defer func() {
111                 d.Destroy()
112         }()
113         file := d.CreateFile("dummy_file", "dummy_dir")
114         voidInfo := d.CreateBasicType(DIBasicType{Name: "void"})
115         typeInfo := d.CreateSubroutineType(DISubroutineType{file, []Metadata{voidInfo}})
116         scope := d.CreateFunction(file, DIFunction{
117                 Name:         "foo",
118                 LinkageName:  "foo",
119                 Line:         10,
120                 ScopeLine:    10,
121                 Type:         typeInfo,
122                 File:         file,
123                 IsDefinition: true,
124         })
125
126         b.SetCurrentDebugLocation(10, 20, scope, Metadata{})
127         loc := b.GetCurrentDebugLocation()
128         if loc.Line != 10 {
129                 t.Errorf("Got line %d, though wanted 10", loc.Line)
130         }
131         if loc.Col != 20 {
132                 t.Errorf("Got column %d, though wanted 20", loc.Col)
133         }
134         if loc.Scope.C != scope.C {
135                 t.Errorf("Got metadata %v as scope, though wanted %v", loc.Scope.C, scope.C)
136         }
137 }
138
139 func TestSubtypes(t *testing.T) {
140         cont := NewContext()
141         defer cont.Dispose()
142
143         int_pointer := PointerType(cont.Int32Type(), 0)
144         int_inner := int_pointer.Subtypes()
145         if len(int_inner) != 1 {
146                 t.Errorf("Got size %d, though wanted 1", len(int_inner))
147         }
148         if int_inner[0] != cont.Int32Type() {
149                 t.Errorf("Expected int32 type")
150         }
151
152         st_pointer := cont.StructType([]Type{cont.Int32Type(), cont.Int8Type()}, false)
153         st_inner := st_pointer.Subtypes()
154         if len(st_inner) != 2 {
155                 t.Errorf("Got size %d, though wanted 2", len(int_inner))
156         }
157         if st_inner[0] != cont.Int32Type() {
158                 t.Errorf("Expected first struct field to be int32")
159         }
160         if st_inner[1] != cont.Int8Type() {
161                 t.Errorf("Expected second struct field to be int8")
162         }
163 }