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{
93 for _
, name
:= range attrTests
{
94 testAttribute(t
, name
)
98 func TestDebugLoc(t
*testing
.T
) {
104 b
:= ctx
.NewBuilder()
107 d
:= NewDIBuilder(mod
)
111 file
:= d
.CreateFile("dummy_file", "dummy_dir")
112 voidInfo
:= d
.CreateBasicType(DIBasicType
{Name
: "void"})
113 typeInfo
:= d
.CreateSubroutineType(DISubroutineType
{
115 Parameters
: []Metadata
{voidInfo
},
118 scope
:= d
.CreateFunction(file
, DIFunction
{
128 b
.SetCurrentDebugLocation(10, 20, scope
, Metadata
{})
129 loc
:= b
.GetCurrentDebugLocation()
131 t
.Errorf("Got line %d, though wanted 10", loc
.Line
)
134 t
.Errorf("Got column %d, though wanted 20", loc
.Col
)
136 if loc
.Scope
.C
!= scope
.C
{
137 t
.Errorf("Got metadata %v as scope, though wanted %v", loc
.Scope
.C
, scope
.C
)
141 func TestSubtypes(t
*testing
.T
) {
145 int_pointer
:= PointerType(cont
.Int32Type(), 0)
146 int_inner
:= int_pointer
.Subtypes()
147 if len(int_inner
) != 1 {
148 t
.Errorf("Got size %d, though wanted 1", len(int_inner
))
150 if int_inner
[0] != cont
.Int32Type() {
151 t
.Errorf("Expected int32 type")
154 st_pointer
:= cont
.StructType([]Type
{cont
.Int32Type(), cont
.Int8Type()}, false)
155 st_inner
:= st_pointer
.Subtypes()
156 if len(st_inner
) != 2 {
157 t
.Errorf("Got size %d, though wanted 2", len(int_inner
))
159 if st_inner
[0] != cont
.Int32Type() {
160 t
.Errorf("Expected first struct field to be int32")
162 if st_inner
[1] != cont
.Int8Type() {
163 t
.Errorf("Expected second struct field to be int8")