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 14 package llvm 15 16 import ( 17 "strings" 18 "testing" 19 ) 20 21 func 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 46 func 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 {ConvergentAttribute, "convergent"}, 57 {InAllocaAttribute, "inalloca"}, 58 {InlineHintAttribute, "inlinehint"}, 59 {InRegAttribute, "inreg"}, 60 {JumpTableAttribute, "jumptable"}, 61 {MinSizeAttribute, "minsize"}, 62 {NakedAttribute, "naked"}, 63 {NestAttribute, "nest"}, 64 {NoAliasAttribute, "noalias"}, 65 {NoBuiltinAttribute, "nobuiltin"}, 66 {NoCaptureAttribute, "nocapture"}, 67 {NoDuplicateAttribute, "noduplicate"}, 68 {NoImplicitFloatAttribute, "noimplicitfloat"}, 69 {NoInlineAttribute, "noinline"}, 70 {NonLazyBindAttribute, "nonlazybind"}, 71 {NonNullAttribute, "nonnull"}, 72 {NoRedZoneAttribute, "noredzone"}, 73 {NoReturnAttribute, "noreturn"}, 74 {NoUnwindAttribute, "nounwind"}, 75 {OptimizeNoneAttribute, "optnone"}, 76 {OptimizeForSizeAttribute, "optsize"}, 77 {ReadNoneAttribute, "readnone"}, 78 {ReadOnlyAttribute, "readonly"}, 79 {ReturnedAttribute, "returned"}, 80 {ReturnsTwiceAttribute, "returns_twice"}, 81 {SExtAttribute, "signext"}, 82 {SafeStackAttribute, "safestack"}, 83 {StackProtectAttribute, "ssp"}, 84 {StackProtectReqAttribute, "sspreq"}, 85 {StackProtectStrongAttribute, "sspstrong"}, 86 {StructRetAttribute, "sret"}, 87 {SanitizeThreadAttribute, "sanitize_thread"}, 88 {SanitizeMemoryAttribute, "sanitize_memory"}, 89 {UWTableAttribute, "uwtable"}, 90 {ZExtAttribute, "zeroext"}, 91 {ColdAttribute, "cold"}, 92 } 93 94 for _, a := range attrTests { 95 testAttribute(t, a.attr, a.name) 96 } 97 } 98