aboutsummaryrefslogtreecommitdiffstats
path: root/bindings/go/llvm/ir_test.go
blob: 981c94aa63ecd6be7cf08f378db9a093b150555a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//===- ir_test.go - Tests for ir ------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file tests bindings for the ir component.
//
//===----------------------------------------------------------------------===//

package llvm

import (
	"strings"
	"testing"
)

func testAttribute(t *testing.T, attr Attribute, name string) {
	mod := NewModule("")
	defer mod.Dispose()

	ftyp := FunctionType(VoidType(), nil, false)
	fn := AddFunction(mod, "foo", ftyp)

	fn.AddFunctionAttr(attr)
	newattr := fn.FunctionAttr()
	if attr != newattr {
		t.Errorf("got attribute mask %d, want %d", newattr, attr)
	}

	text := mod.String()
	if !strings.Contains(text, " "+name+" ") {
		t.Errorf("expected attribute '%s', got:\n%s", name, text)
	}

	fn.RemoveFunctionAttr(attr)
	newattr = fn.FunctionAttr()
	if newattr != 0 {
		t.Errorf("got attribute mask %d, want 0", newattr)
	}
}

func TestAttributes(t *testing.T) {
	// Tests that our attribute constants haven't drifted from LLVM's.
	attrTests := []struct {
		attr Attribute
		name string
	}{
		{SanitizeAddressAttribute, "sanitize_address"},
		{AlwaysInlineAttribute, "alwaysinline"},
		{BuiltinAttribute, "builtin"},
		{ByValAttribute, "byval"},
		{InAllocaAttribute, "inalloca"},
		{InlineHintAttribute, "inlinehint"},
		{InRegAttribute, "inreg"},
		{JumpTableAttribute, "jumptable"},
		{MinSizeAttribute, "minsize"},
		{NakedAttribute, "naked"},
		{NestAttribute, "nest"},
		{NoAliasAttribute, "noalias"},
		{NoBuiltinAttribute, "nobuiltin"},
		{NoCaptureAttribute, "nocapture"},
		{NoDuplicateAttribute, "noduplicate"},
		{NoImplicitFloatAttribute, "noimplicitfloat"},
		{NoInlineAttribute, "noinline"},
		{NonLazyBindAttribute, "nonlazybind"},
		{NonNullAttribute, "nonnull"},
		{NoRedZoneAttribute, "noredzone"},
		{NoReturnAttribute, "noreturn"},
		{NoUnwindAttribute, "nounwind"},
		{OptimizeNoneAttribute, "optnone"},
		{OptimizeForSizeAttribute, "optsize"},
		{ReadNoneAttribute, "readnone"},
		{ReadOnlyAttribute, "readonly"},
		{ReturnedAttribute, "returned"},
		{ReturnsTwiceAttribute, "returns_twice"},
		{SExtAttribute, "signext"},
		{StackProtectAttribute, "ssp"},
		{StackProtectReqAttribute, "sspreq"},
		{StackProtectStrongAttribute, "sspstrong"},
		{StructRetAttribute, "sret"},
		{SanitizeThreadAttribute, "sanitize_thread"},
		{SanitizeMemoryAttribute, "sanitize_memory"},
		{UWTableAttribute, "uwtable"},
		{ZExtAttribute, "zeroext"},
		{ColdAttribute, "cold"},
	}

	for _, a := range attrTests {
		testAttribute(t, a.attr, a.name)
	}
}