1//===- ir_test.go - Tests for ir ------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file tests bindings for the ir component.
11//
12//===----------------------------------------------------------------------===//
13
14package llvm
15
16import (
17	"strings"
18	"testing"
19)
20
21func testAttribute(t *testing.T, attr Attribute, name string) {
22	mod := NewModule("")
23	defer mod.Dispose()
24
25	ftyp := FunctionType(VoidType(), nil, false)
26	fn := AddFunction(mod, "foo", ftyp)
27
28	fn.AddFunctionAttr(attr)
29	newattr := fn.FunctionAttr()
30	if attr != newattr {
31		t.Errorf("got attribute mask %d, want %d", newattr, attr)
32	}
33
34	text := mod.String()
35	if !strings.Contains(text, " "+name+" ") {
36		t.Errorf("expected attribute '%s', got:\n%s", name, text)
37	}
38
39	fn.RemoveFunctionAttr(attr)
40	newattr = fn.FunctionAttr()
41	if newattr != 0 {
42		t.Errorf("got attribute mask %d, want 0", newattr)
43	}
44}
45
46func TestAttributes(t *testing.T) {
47	// Tests that our attribute constants haven't drifted from LLVM's.
48	attrTests := []struct {
49		attr Attribute
50		name string
51	}{
52		{SanitizeAddressAttribute, "sanitize_address"},
53		{AlwaysInlineAttribute, "alwaysinline"},
54		{BuiltinAttribute, "builtin"},
55		{ByValAttribute, "byval"},
56		{InAllocaAttribute, "inalloca"},
57		{InlineHintAttribute, "inlinehint"},
58		{InRegAttribute, "inreg"},
59		{JumpTableAttribute, "jumptable"},
60		{MinSizeAttribute, "minsize"},
61		{NakedAttribute, "naked"},
62		{NestAttribute, "nest"},
63		{NoAliasAttribute, "noalias"},
64		{NoBuiltinAttribute, "nobuiltin"},
65		{NoCaptureAttribute, "nocapture"},
66		{NoDuplicateAttribute, "noduplicate"},
67		{NoImplicitFloatAttribute, "noimplicitfloat"},
68		{NoInlineAttribute, "noinline"},
69		{NonLazyBindAttribute, "nonlazybind"},
70		{NonNullAttribute, "nonnull"},
71		{NoRedZoneAttribute, "noredzone"},
72		{NoReturnAttribute, "noreturn"},
73		{NoUnwindAttribute, "nounwind"},
74		{OptimizeNoneAttribute, "optnone"},
75		{OptimizeForSizeAttribute, "optsize"},
76		{ReadNoneAttribute, "readnone"},
77		{ReadOnlyAttribute, "readonly"},
78		{ReturnedAttribute, "returned"},
79		{ReturnsTwiceAttribute, "returns_twice"},
80		{SExtAttribute, "signext"},
81		{StackProtectAttribute, "ssp"},
82		{StackProtectReqAttribute, "sspreq"},
83		{StackProtectStrongAttribute, "sspstrong"},
84		{StructRetAttribute, "sret"},
85		{SanitizeThreadAttribute, "sanitize_thread"},
86		{SanitizeMemoryAttribute, "sanitize_memory"},
87		{UWTableAttribute, "uwtable"},
88		{ZExtAttribute, "zeroext"},
89		{ColdAttribute, "cold"},
90	}
91
92	for _, a := range attrTests {
93		testAttribute(t, a.attr, a.name)
94	}
95}
96