[Codegen] Adjust saturation test. NFC.
[llvm-core.git] / bindings / go / llvm / ir_test.go
blob601a22589d70b86219dfc97290b6c107198049cf
1 //===- ir_test.go - Tests for ir ------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file tests bindings for the ir component.
11 //===----------------------------------------------------------------------===//
13 package llvm
15 import (
16 "strings"
17 "testing"
20 func testAttribute(t *testing.T, name string) {
21 mod := NewModule("")
22 defer mod.Dispose()
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)
32 if attr != newattr {
33 t.Errorf("got attribute %p, want %p", newattr.C, attr.C)
36 text := mod.String()
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)
43 if !newattr.IsNil() {
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{
51 "sanitize_address",
52 "alwaysinline",
53 "builtin",
54 "convergent",
55 "inalloca",
56 "inlinehint",
57 "inreg",
58 "jumptable",
59 "minsize",
60 "naked",
61 "nest",
62 "noalias",
63 "nobuiltin",
64 "nocapture",
65 "noduplicate",
66 "noimplicitfloat",
67 "noinline",
68 "nonlazybind",
69 "nonnull",
70 "noredzone",
71 "noreturn",
72 "nounwind",
73 "optnone",
74 "optsize",
75 "readnone",
76 "readonly",
77 "returned",
78 "returns_twice",
79 "signext",
80 "safestack",
81 "ssp",
82 "sspreq",
83 "sspstrong",
84 "sret",
85 "sanitize_thread",
86 "sanitize_memory",
87 "uwtable",
88 "zeroext",
89 "cold",
90 "nocf_check",
93 for _, name := range attrTests {
94 testAttribute(t, name)
98 func TestDebugLoc(t *testing.T) {
99 mod := NewModule("")
100 defer mod.Dispose()
102 ctx := mod.Context()
104 b := ctx.NewBuilder()
105 defer b.Dispose()
107 d := NewDIBuilder(mod)
108 defer func() {
109 d.Destroy()
111 file := d.CreateFile("dummy_file", "dummy_dir")
112 voidInfo := d.CreateBasicType(DIBasicType{Name: "void"})
113 typeInfo := d.CreateSubroutineType(DISubroutineType{
114 File: file,
115 Parameters: []Metadata{voidInfo},
116 Flags: 0,
118 scope := d.CreateFunction(file, DIFunction{
119 Name: "foo",
120 LinkageName: "foo",
121 Line: 10,
122 ScopeLine: 10,
123 Type: typeInfo,
124 File: file,
125 IsDefinition: true,
128 b.SetCurrentDebugLocation(10, 20, scope, Metadata{})
129 loc := b.GetCurrentDebugLocation()
130 if loc.Line != 10 {
131 t.Errorf("Got line %d, though wanted 10", loc.Line)
133 if loc.Col != 20 {
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) {
142 cont := NewContext()
143 defer cont.Dispose()
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")