1 //===- ir_test.go - Tests for ir ------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file tests bindings for the ir component.
12 //===----------------------------------------------------------------------===//
21 func testAttribute(t *testing.T, name string) {
25 ftyp := FunctionType(VoidType(), nil, false)
26 fn := AddFunction(mod, "foo", ftyp)
28 kind := AttributeKindID(name)
29 attr := mod.Context().CreateEnumAttribute(kind, 0)
31 fn.AddFunctionAttr(attr)
32 newattr := fn.GetEnumFunctionAttribute(kind)
34 t.Errorf("got attribute mask %d, want %d", newattr, attr)
38 if !strings.Contains(text, " "+name+" ") {
39 t.Errorf("expected attribute '%s', got:\n%s", name, text)
42 fn.RemoveEnumFunctionAttribute(kind)
43 newattr = fn.GetEnumFunctionAttribute(kind)
45 t.Errorf("got attribute mask %d, want 0", newattr)
49 func TestAttributes(t *testing.T) {
50 // Tests that our attribute constants haven't drifted from LLVM's.
51 attrTests := []string{
94 for _, name := range attrTests {
95 testAttribute(t, name)
99 func TestDebugLoc(t *testing.T) {
105 b := ctx.NewBuilder()
108 d := NewDIBuilder(mod)
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{
125 b.SetCurrentDebugLocation(10, 20, scope, Metadata{})
126 loc := b.GetCurrentDebugLocation()
128 t.Errorf("Got line %d, though wanted 10", loc.Line)
131 t.Errorf("Got column %d, though wanted 20", loc.Col)
133 if loc.Scope.C != scope.C {
134 t.Errorf("Got metadata %v as scope, though wanted %v", loc.Scope.C, scope.C)
138 func TestSubtypes(t *testing.T) {
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))
147 if int_inner[0] != cont.Int32Type() {
148 t.Errorf("Expected int32 type")
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))
156 if st_inner[0] != cont.Int32Type() {
157 t.Errorf("Expected first struct field to be int32")
159 if st_inner[1] != cont.Int8Type() {
160 t.Errorf("Expected second struct field to be int8")