llvm.org GIT mirror llvm / d1894a1
Fix go binding to adapt the new attribute API https://reviews.llvm.org/D26339 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@287328 91177308-0d34-0410-b5e6-96231b3b80d8 Amaury Sechet 3 years ago
2 changed file(s) with 193 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
5757 Metadata struct {
5858 C C.LLVMMetadataRef
5959 }
60 Attribute struct {
61 C C.LLVMAttributeRef
62 }
6063 Opcode C.LLVMOpcode
6164 TypeKind C.LLVMTypeKind
6265 Linkage C.LLVMLinkage
7780 func (c MemoryBuffer) IsNil() bool { return c.C == nil }
7881 func (c PassManager) IsNil() bool { return c.C == nil }
7982 func (c Use) IsNil() bool { return c.C == nil }
83 func (c Attribute) IsNil() bool { return c.C == nil }
8084
8185 // helpers
8286 func llvmTypeRefPtr(t *Type) *C.LLVMTypeRef { return (*C.LLVMTypeRef)(unsafe.Pointer(t)) }
312316 defer C.free(unsafe.Pointer(cname))
313317 id = int(C.LLVMGetMDKindID(cname, C.unsigned(len(name))))
314318 return
319 }
320
321 //-------------------------------------------------------------------------
322 // llvm.Attribute
323 //-------------------------------------------------------------------------
324
325 func AttributeKindID(name string) (id uint) {
326 cname := C.CString(name)
327 defer C.free(unsafe.Pointer(cname))
328 id = uint(C.LLVMGetEnumAttributeKindForName(cname, C.size_t(len(name))))
329 return
330 }
331
332 func (c Context) CreateEnumAttribute(kind uint, val uint64) (a Attribute) {
333 a.C = C.LLVMCreateEnumAttribute(c.C, C.unsigned(kind), C.uint64_t(val))
334 return
335 }
336
337 func (a Attribute) GetEnumKind() (id int) {
338 id = int(C.LLVMGetEnumAttributeKind(a.C))
339 return
340 }
341
342 func (a Attribute) GetEnumValue() (val uint64) {
343 val = uint64(C.LLVMGetEnumAttributeValue(a.C))
344 return
345 }
346
347 func (c Context) CreateStringAttribute(kind string, val string) (a Attribute) {
348 ckind := C.CString(kind)
349 defer C.free(unsafe.Pointer(ckind))
350 cval := C.CString(val)
351 defer C.free(unsafe.Pointer(cval))
352 a.C = C.LLVMCreateStringAttribute(c.C,
353 ckind, C.unsigned(len(kind)),
354 cval, C.unsigned(len(val)))
355 return
356 }
357
358 func (a Attribute) GetStringKind() string {
359 length := C.unsigned(0)
360 ckind := C.LLVMGetStringAttributeKind(a.C, &length)
361 return C.GoStringN(ckind, C.int(length))
362 }
363
364 func (a Attribute) GetStringValue() string {
365 length := C.unsigned(0)
366 ckind := C.LLVMGetStringAttributeValue(a.C, &length)
367 return C.GoStringN(ckind, C.int(length))
368 }
369
370 func (a Attribute) IsEnum() bool {
371 return C.LLVMIsEnumAttribute(a.C) != 0;
372 }
373
374 func (a Attribute) IsString() bool {
375 return C.LLVMIsStringAttribute(a.C) != 0;
315376 }
316377
317378 //-------------------------------------------------------------------------
9921053 defer C.free(unsafe.Pointer(cname))
9931054 C.LLVMSetGC(v.C, cname)
9941055 }
1056 func (v Value) AddAttributeAtIndex(i int, a Attribute) {
1057 C.LLVMAddAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), a.C)
1058 }
1059 func (v Value) AddFunctionAttr(a Attribute) {
1060 v.AddAttributeAtIndex(C.LLVMAttributeFunctionIndex, a);
1061 }
1062 func (v Value) GetEnumAttributeAtIndex(i int, kind uint) (a Attribute) {
1063 a.C = C.LLVMGetEnumAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), C.unsigned(kind))
1064 return
1065 }
1066 func (v Value) GetEnumFunctionAttribute(kind uint) Attribute {
1067 return v.GetEnumAttributeAtIndex(C.LLVMAttributeFunctionIndex, kind)
1068 }
1069 func (v Value) GetStringAttributeAtIndex(i int, kind string) (a Attribute) {
1070 ckind := C.CString(kind)
1071 defer C.free(unsafe.Pointer(ckind))
1072 a.C = C.LLVMGetStringAttributeAtIndex(v.C, C.LLVMAttributeIndex(i),
1073 ckind, C.unsigned(len(kind)))
1074 return
1075 }
1076 func (v Value) RemoveEnumAttributeAtIndex(i int, kind uint) {
1077 C.LLVMRemoveEnumAttributeAtIndex(v.C, C.LLVMAttributeIndex(i), C.unsigned(kind))
1078 }
1079 func (v Value) RemoveEnumFunctionAttribute(kind uint) {
1080 v.RemoveEnumAttributeAtIndex(C.LLVMAttributeFunctionIndex, kind);
1081 }
1082 func (v Value) RemoveStringAttributeAtIndex(i int, kind string) {
1083 ckind := C.CString(kind)
1084 defer C.free(unsafe.Pointer(ckind))
1085 C.LLVMRemoveStringAttributeAtIndex(v.C, C.LLVMAttributeIndex(i),
1086 ckind, C.unsigned(len(kind)))
1087 }
9951088 func (v Value) AddTargetDependentFunctionAttr(attr, value string) {
9961089 cattr := C.CString(attr)
9971090 defer C.free(unsafe.Pointer(cattr))
10811174 func (v Value) InstructionCallConv() CallConv {
10821175 return CallConv(C.LLVMCallConv(C.LLVMGetInstructionCallConv(v.C)))
10831176 }
1177 func (v Value) AddCallSiteAttribute(i int, a Attribute) {
1178 C.LLVMAddCallSiteAttribute(v.C, C.LLVMAttributeIndex(i), a.C)
1179 }
10841180 func (v Value) SetInstrParamAlignment(i int, align int) {
10851181 C.LLVMSetInstrParamAlignment(v.C, C.unsigned(i), C.unsigned(align))
10861182 }
0 //===- ir_test.go - Tests for ir ------------------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file tests bindings for the ir component.
10 //
11 //===----------------------------------------------------------------------===//
12
13 package llvm
14
15 import (
16 "strings"
17 "testing"
18 )
19
20 func testAttribute(t *testing.T, name string) {
21 mod := NewModule("")
22 defer mod.Dispose()
23
24 ftyp := FunctionType(VoidType(), nil, false)
25 fn := AddFunction(mod, "foo", ftyp)
26
27 kind := AttributeKindID(name)
28 attr := mod.Context().CreateEnumAttribute(kind, 0)
29
30 fn.AddFunctionAttr(attr)
31 newattr := fn.GetEnumFunctionAttribute(kind)
32 if attr != newattr {
33 t.Errorf("got attribute mask %d, want %d", newattr, attr)
34 }
35
36 text := mod.String()
37 if !strings.Contains(text, " "+name+" ") {
38 t.Errorf("expected attribute '%s', got:\n%s", name, text)
39 }
40
41 fn.RemoveEnumFunctionAttribute(kind)
42 newattr = fn.GetEnumFunctionAttribute(kind)
43 if !newattr.IsNil() {
44 t.Errorf("got attribute mask %d, want 0", newattr)
45 }
46 }
47
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 }
92
93 for _, name := range attrTests {
94 testAttribute(t, name)
95 }
96 }