Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / bindings / go / llvm / ir_test.go
blob5dd0598c012689286194f61b090feb807eccd721
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 "byval",
55 "convergent",
56 "inalloca",
57 "inlinehint",
58 "inreg",
59 "jumptable",
60 "minsize",
61 "naked",
62 "nest",
63 "noalias",
64 "nobuiltin",
65 "nocapture",
66 "noduplicate",
67 "noimplicitfloat",
68 "noinline",
69 "nonlazybind",
70 "nonnull",
71 "noredzone",
72 "noreturn",
73 "nounwind",
74 "optnone",
75 "optsize",
76 "readnone",
77 "readonly",
78 "returned",
79 "returns_twice",
80 "signext",
81 "safestack",
82 "ssp",
83 "sspreq",
84 "sspstrong",
85 "sret",
86 "sanitize_thread",
87 "sanitize_memory",
88 "uwtable",
89 "zeroext",
90 "cold",
91 "nocf_check",
94 for _, name := range attrTests {
95 testAttribute(t, name)
99 func TestDebugLoc(t *testing.T) {
100 mod := NewModule("")
101 defer mod.Dispose()
103 ctx := mod.Context()
105 b := ctx.NewBuilder()
106 defer b.Dispose()
108 d := NewDIBuilder(mod)
109 defer func() {
110 d.Destroy()
112 file := d.CreateFile("dummy_file", "dummy_dir")
113 voidInfo := d.CreateBasicType(DIBasicType{Name: "void"})
114 typeInfo := d.CreateSubroutineType(DISubroutineType{
115 File: file,
116 Parameters: []Metadata{voidInfo},
117 Flags: 0,
119 scope := d.CreateFunction(file, DIFunction{
120 Name: "foo",
121 LinkageName: "foo",
122 Line: 10,
123 ScopeLine: 10,
124 Type: typeInfo,
125 File: file,
126 IsDefinition: true,
129 b.SetCurrentDebugLocation(10, 20, scope, Metadata{})
130 loc := b.GetCurrentDebugLocation()
131 if loc.Line != 10 {
132 t.Errorf("Got line %d, though wanted 10", loc.Line)
134 if loc.Col != 20 {
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) {
143 cont := NewContext()
144 defer cont.Dispose()
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")