From 46b40bbe054e488af62186265b0408d2b4370bd3 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Fri, 4 Aug 2017 00:52:48 +0300 Subject: [PATCH] Fixed the generated C# for a case of a typedef of a function pointer. Typedefs of function pointers can be written in two ways: typedef void (*typedefedFuncPtr)(); int f(typedefedFuncPtr fptr); typedef void (typedefedFuncPtr)(); int f(typedefedFuncPtr* fptr); Up until now we only supported the former. Signed-off-by: Dimitar Dobrev --- src/Generator/Generators/CSharp/CSharpSources.cs | 4 ++-- src/Generator/Generators/CSharp/CSharpTypePrinter.cs | 4 ++-- src/Generator/Passes/DelegatesPass.cs | 3 ++- tests/CSharp/CSharp.Tests.cs | 4 ++-- tests/CSharp/CSharp.cpp | 4 ++-- tests/CSharp/CSharp.h | 4 ++-- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 3a424371..066cb50a 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -2959,9 +2959,9 @@ namespace CppSharp.Generators.CSharp GenerateDeclarationCommon(typedef); - FunctionType functionType; + var functionType = typedef.Type as FunctionType; - if (typedef.Type.IsPointerTo(out functionType)) + if (functionType != null || typedef.Type.IsPointerTo(out functionType)) { PushBlock(BlockKind.Typedef); var attributedType = typedef.Type.GetPointee() as AttributedType; diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index 8ab8c6ee..8b8b7dd8 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -320,8 +320,8 @@ namespace CppSharp.Generators.CSharp } } - FunctionType func; - if (decl.Type.IsPointerTo(out func)) + FunctionType func = decl.Type as FunctionType; + if (func != null || decl.Type.IsPointerTo(out func)) { if (ContextKind == TypePrinterContextKind.Native) return IntPtrType; diff --git a/src/Generator/Passes/DelegatesPass.cs b/src/Generator/Passes/DelegatesPass.cs index 958c09ce..cbf24ef1 100644 --- a/src/Generator/Passes/DelegatesPass.cs +++ b/src/Generator/Passes/DelegatesPass.cs @@ -143,9 +143,10 @@ namespace CppSharp.Passes private void VisitFunctionTypeParameters(Function function) { foreach (var param in from param in function.Parameters + where !(param.Type is TypedefType) let paramType = param.Type.Desugar() where paramType.IsAddress() && - paramType.GetFinalPointee().Desugar() is FunctionType + paramType.GetPointee() is FunctionType select param) { var module = function.TranslationUnit.Module; diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index e1ebc05a..5ad94355 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -856,10 +856,10 @@ public unsafe class CSharpTests : GeneratorTestFixture } } - [Test, Platform(Exclude = "Win", Reason = "This test crashes our Windows build, possibly a problem with the NUnit runner there.")] + [Test] public void TestFuncWithTypedefedFuncPtrAsParam() { - Func_int_IntPtr_CSharp_Bar___Internal function = (a, b) => 5; + TypedefedFuncPtr function = (a, b) => 5; Assert.That(CSharp.CSharp.FuncWithTypedefedFuncPtrAsParam(function), Is.EqualTo(5)); } } diff --git a/tests/CSharp/CSharp.cpp b/tests/CSharp/CSharp.cpp index d93ebb33..0f97f529 100644 --- a/tests/CSharp/CSharp.cpp +++ b/tests/CSharp/CSharp.cpp @@ -1355,9 +1355,9 @@ void useStdStringJustAsParameter(std::string s) { } -int funcWithTypedefedFuncPtrAsParam(typedefedFuncPtr *func) +int funcWithTypedefedFuncPtrAsParam(typedefedFuncPtr* func) { Foo* a = 0; Bar b; return func(a, b); -} \ No newline at end of file +} diff --git a/tests/CSharp/CSharp.h b/tests/CSharp/CSharp.h index 9bd7b41e..5eb32c59 100644 --- a/tests/CSharp/CSharp.h +++ b/tests/CSharp/CSharp.h @@ -1218,5 +1218,5 @@ extern const ComplexArrayElement ArrayOfVariableSize[]; void useStdStringJustAsParameter(std::string s); -typedef int (typedefedFuncPtr)(Foo *a, Bar b); -int DLL_API funcWithTypedefedFuncPtrAsParam(typedefedFuncPtr *func); +typedef int (typedefedFuncPtr)(Foo* a, Bar b); +int DLL_API funcWithTypedefedFuncPtrAsParam(typedefedFuncPtr* func);