Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Generator.Tests/Passes/TestPasses.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CodingSeb.ExpressionEvaluator;
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Generators.CSharp;
using CppSharp.Passes;
using NUnit.Framework;
Expand Down Expand Up @@ -511,6 +512,38 @@ public void TestFlattenAnonymousTypesToFields()
Assert.AreEqual(AccessSpecifier.Protected, @protected.Access);
}

[Test]
public void TestDelegatesPassCLI()
{
Driver.Options.GeneratorKind = GeneratorKind.CLI;
passBuilder.AddPass(new DelegatesPass());
passBuilder.RunPasses(pass => pass.VisitASTContext(AstContext));

// Delegates for virtual methods are only used by the C# generator to hook vtables
var @class = AstContext.FindDecl<Class>("TestDelegatesPassVirtualMethod").First();
var method = @class.Methods.First(m => m.Name == "VirtualMethod");
Assert.That(method.FunctionType.Type, Is.Not.InstanceOf<TypedefType>());
Assert.That(@class.Declarations.OfType<TypedefDecl>().Where(t => t.IsSynthetized), Is.Empty);

// Function pointers still get a delegate
var function = AstContext.FindFunction("TestDelegatesPassFunctionPointer").First();
Assert.That(function.Parameters[0].Type, Is.InstanceOf<TypedefType>());
}

[Test]
public void TestDelegatesPassCSharp()
{
passBuilder.AddPass(new DelegatesPass());
passBuilder.RunPasses(pass => pass.VisitASTContext(AstContext));

var @class = AstContext.FindDecl<Class>("TestDelegatesPassVirtualMethod").First();
var method = @class.Methods.First(m => m.Name == "VirtualMethod");
Assert.That(method.FunctionType.Type, Is.InstanceOf<TypedefType>());

var function = AstContext.FindFunction("TestDelegatesPassFunctionPointer").First();
Assert.That(function.Parameters[0].Type, Is.InstanceOf<TypedefType>());
}

private PassBuilder<TranslationUnitPass> passBuilder;
}
}
2 changes: 2 additions & 0 deletions src/Generator/Passes/DelegatesPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public override bool VisitMethodDecl(Method method)
{
if (!base.VisitMethodDecl(method) || !method.IsVirtual || method.Ignore)
return false;
if (Options.IsCLIGenerator)
return true;

var functionType = new FunctionType
{
Expand Down
8 changes: 8 additions & 0 deletions tests/dotnet/Native/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,11 @@ class TestExtractInterfacePass
public:
void DoSomething();
};

class TestDelegatesPassVirtualMethod
{
public:
virtual int VirtualMethod(int i);
};

void TestDelegatesPassFunctionPointer(void (*callback)(int));
Loading