1 //===- ir_test.go - Tests for ir ------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file tests bindings for the ir component.
11 //===----------------------------------------------------------------------===//
20 func testAttribute(t
*testing
.T
, name
string) {
24 ftyp
:= FunctionType(VoidType(), nil, false)
25 fn
:= AddFunction(mod
, "foo", ftyp
)
27 kind
:= AttributeKindID(name
)
28 attr
:= mod
.Context().CreateEnumAttribute(kind
, 0)
30 fn
.AddFunctionAttr(attr
)
31 newattr
:= fn
.GetEnumFunctionAttribute(kind
)
33 t
.Errorf("got attribute %p, want %p", newattr
.C
, attr
.C
)
37 if !strings
.Contains(text
, " "+name
+" ") {
38 t
.Errorf("expected attribute '%s', got:\n%s", name
, text
)
41 fn
.RemoveEnumFunctionAttribute(kind
)
42 newattr
= fn
.GetEnumFunctionAttribute(kind
)
44 t
.Errorf("got attribute %p, want 0", newattr
.C
)
48 func TestAttributes(t
*testing
.T
) {
49 // Tests that our attribute constants haven't drifted from LLVM's.
50 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
{
116 Parameters
: []Metadata
{voidInfo
},
119 scope
:= d
.CreateFunction(file
, DIFunction
{
129 b
.SetCurrentDebugLocation(10, 20, scope
, Metadata
{})
130 loc
:= b
.GetCurrentDebugLocation()
132 t
.Errorf("Got line %d, though wanted 10", loc
.Line
)
135 t
.Errorf("Got column %d, though wanted 20", loc
.Col
)
137 if loc
.Scope
.C
!= scope
.C
{
138 t
.Errorf("Got metadata %v as scope, though wanted %v", loc
.Scope
.C
, scope
.C
)
142 func TestSubtypes(t
*testing
.T
) {
146 int_pointer
:= PointerType(cont
.Int32Type(), 0)
147 int_inner
:= int_pointer
.Subtypes()
148 if len(int_inner
) != 1 {
149 t
.Errorf("Got size %d, though wanted 1", len(int_inner
))
151 if int_inner
[0] != cont
.Int32Type() {
152 t
.Errorf("Expected int32 type")
155 st_pointer
:= cont
.StructType([]Type
{cont
.Int32Type(), cont
.Int8Type()}, false)
156 st_inner
:= st_pointer
.Subtypes()
157 if len(st_inner
) != 2 {
158 t
.Errorf("Got size %d, though wanted 2", len(int_inner
))
160 if st_inner
[0] != cont
.Int32Type() {
161 t
.Errorf("Expected first struct field to be int32")
163 if st_inner
[1] != cont
.Int8Type() {
164 t
.Errorf("Expected second struct field to be int8")