diff --git a/Tests/Bindings.cs b/Tests/Bindings.cs new file mode 100644 index 00000000..65739168 --- /dev/null +++ b/Tests/Bindings.cs @@ -0,0 +1,52 @@ +namespace LLVMSharp.Tests +{ + using NUnit.Framework; + using System; + + public class Bindings + { + [Test] + public void StringMarshaling() + { + var mod = LLVM.ModuleCreateWithName("string_marshaling_test"); + + var param_types = new LLVMTypeRef[0]; + var func_type = LLVM.FunctionType(LLVM.Int32Type(), param_types, false); + var main = LLVM.AddFunction(mod, "main", func_type); + + const string BasicBlockName = "entry"; + var entry = LLVM.AppendBasicBlock(main, BasicBlockName); + + var builder = LLVM.CreateBuilder(); + LLVM.PositionBuilderAtEnd(builder, entry); + var ret = LLVM.BuildRet(builder, LLVM.ConstInt(LLVM.Int32Type(), 0ul, new LLVMBool(false))); + + if (LLVM.VerifyModule(mod, LLVMVerifierFailureAction.LLVMPrintMessageAction, out var error) != new LLVMBool(true)) + { + Console.WriteLine($"Error: {error}"); + } + + var basicBlockName = LLVM.GetBasicBlockName(entry); + Assert.AreEqual(BasicBlockName, basicBlockName); + + var valueName = LLVM.GetValueName(ret); + Assert.IsEmpty(valueName); + + var layout = LLVM.GetDataLayout(mod); + Assert.IsEmpty(layout); + + var moduleTarget = LLVM.GetTarget(mod); + Assert.IsEmpty(moduleTarget); + + LLVM.InitializeX86TargetInfo(); + + var targetName = LLVM.GetTargetName(LLVM.GetFirstTarget()); + Assert.IsTrue(targetName.Contains("x86")); + + var targetDescription = LLVM.GetTargetDescription(LLVM.GetFirstTarget()); + Assert.IsNotEmpty(targetDescription); + + LLVM.DisposeBuilder(builder); + } + } +} diff --git a/src/Generated.cs b/src/Generated.cs index 13d215b6..9f1d383c 100644 --- a/src/Generated.cs +++ b/src/Generated.cs @@ -1150,15 +1150,9 @@ public static partial class LLVM [DllImport(libraryPath, EntryPoint = "LLVMGetDataLayoutStr", CallingConvention = CallingConvention.Cdecl)] public static extern string GetDataLayoutStr(LLVMModuleRef @M); - [DllImport(libraryPath, EntryPoint = "LLVMGetDataLayout", CallingConvention = CallingConvention.Cdecl)] - public static extern string GetDataLayout(LLVMModuleRef @M); - [DllImport(libraryPath, EntryPoint = "LLVMSetDataLayout", CallingConvention = CallingConvention.Cdecl)] public static extern void SetDataLayout(LLVMModuleRef @M, [MarshalAs(UnmanagedType.LPStr)] string @DataLayoutStr); - [DllImport(libraryPath, EntryPoint = "LLVMGetTarget", CallingConvention = CallingConvention.Cdecl)] - public static extern string GetTarget(LLVMModuleRef @M); - [DllImport(libraryPath, EntryPoint = "LLVMSetTarget", CallingConvention = CallingConvention.Cdecl)] public static extern void SetTarget(LLVMModuleRef @M, [MarshalAs(UnmanagedType.LPStr)] string @Triple); @@ -1405,9 +1399,6 @@ public static partial class LLVM [DllImport(libraryPath, EntryPoint = "LLVMGetValueKind", CallingConvention = CallingConvention.Cdecl)] public static extern LLVMValueKind GetValueKind(LLVMValueRef @Val); - [DllImport(libraryPath, EntryPoint = "LLVMGetValueName", CallingConvention = CallingConvention.Cdecl)] - public static extern string GetValueName(LLVMValueRef @Val); - [DllImport(libraryPath, EntryPoint = "LLVMSetValueName", CallingConvention = CallingConvention.Cdecl)] public static extern void SetValueName(LLVMValueRef @Val, [MarshalAs(UnmanagedType.LPStr)] string @Name); @@ -2173,9 +2164,6 @@ public static partial class LLVM [DllImport(libraryPath, EntryPoint = "LLVMValueAsBasicBlock", CallingConvention = CallingConvention.Cdecl)] public static extern LLVMBasicBlockRef ValueAsBasicBlock(LLVMValueRef @Val); - [DllImport(libraryPath, EntryPoint = "LLVMGetBasicBlockName", CallingConvention = CallingConvention.Cdecl)] - public static extern string GetBasicBlockName(LLVMBasicBlockRef @BB); - [DllImport(libraryPath, EntryPoint = "LLVMGetBasicBlockParent", CallingConvention = CallingConvention.Cdecl)] public static extern LLVMValueRef GetBasicBlockParent(LLVMBasicBlockRef @BB); @@ -3163,12 +3151,6 @@ public static partial class LLVM [DllImport(libraryPath, EntryPoint = "LLVMGetTargetFromTriple", CallingConvention = CallingConvention.Cdecl)] internal static extern LLVMBool GetTargetFromTriple([MarshalAs(UnmanagedType.LPStr)] string @Triple, out LLVMTargetRef @T, out IntPtr @ErrorMessage); - [DllImport(libraryPath, EntryPoint = "LLVMGetTargetName", CallingConvention = CallingConvention.Cdecl)] - public static extern string GetTargetName(LLVMTargetRef @T); - - [DllImport(libraryPath, EntryPoint = "LLVMGetTargetDescription", CallingConvention = CallingConvention.Cdecl)] - public static extern string GetTargetDescription(LLVMTargetRef @T); - [DllImport(libraryPath, EntryPoint = "LLVMTargetHasJIT", CallingConvention = CallingConvention.Cdecl)] public static extern LLVMBool TargetHasJIT(LLVMTargetRef @T); diff --git a/src/Overloads.Interop.cs b/src/Overloads.Interop.cs index 4eb7c515..212e4cbe 100644 --- a/src/Overloads.Interop.cs +++ b/src/Overloads.Interop.cs @@ -7,17 +7,26 @@ partial class LLVM { [DllImport(libraryPath, EntryPoint = "LLVMGetValueName", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr GetValueNameAsPtr(LLVMValueRef @Val); - - [DllImport(libraryPath, EntryPoint = "LLVMGetTarget", CallingConvention = CallingConvention.Cdecl)] - public static extern IntPtr GetTargetAsPtr(LLVMModuleRef M); + public static string GetValueName(LLVMValueRef @Val) => Marshal.PtrToStringAnsi(GetValueNameAsPtr(@Val)); [DllImport(libraryPath, EntryPoint = "LLVMGetDataLayout", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr GetDataLayoutAsPtr(LLVMModuleRef M); + public static string GetDataLayout(LLVMModuleRef M) => Marshal.PtrToStringAnsi(GetDataLayoutAsPtr(M)); + + [DllImport(libraryPath, EntryPoint = "LLVMGetTarget", CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr GetTargetAsPtr(LLVMModuleRef M); + public static string GetTarget(LLVMModuleRef M) => Marshal.PtrToStringAnsi(GetTargetAsPtr(M)); [DllImport(libraryPath, EntryPoint = "LLVMGetTargetName", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr GetTargetNameAsPtr(LLVMTargetRef @T); + public static string GetTargetName(LLVMTargetRef @T) => Marshal.PtrToStringAnsi(GetTargetNameAsPtr(@T)); [DllImport(libraryPath, EntryPoint = "LLVMGetTargetDescription", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr GetTargetDescriptionAsPtr(LLVMTargetRef @T); + public static string GetTargetDescription(LLVMTargetRef @T) => Marshal.PtrToStringAnsi(GetTargetDescriptionAsPtr(@T)); + + [DllImport(libraryPath, EntryPoint = "LLVMGetBasicBlockName", CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr GetBasicBlockNameAsPtr(LLVMBasicBlockRef @BB); + public static string GetBasicBlockName(LLVMBasicBlockRef @BB) => Marshal.PtrToStringAnsi(GetBasicBlockNameAsPtr(@BB)); } }