Browse Source

Generate valid C# when a renamed override causes conflicts

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1237/head
Dimitar Dobrev 6 years ago
parent
commit
573272e855
  1. 19
      src/Generator/Passes/RenamePass.cs
  2. 6
      tests/CSharp/CSharp.Tests.cs
  3. 10
      tests/CSharp/CSharp.cpp
  4. 3
      tests/CSharp/CSharp.h

19
src/Generator/Passes/RenamePass.cs

@ -199,9 +199,9 @@ namespace CppSharp.Passes
declarations.AddRange(@class.TemplateParameters); declarations.AddRange(@class.TemplateParameters);
} }
var result = declarations.Any(d => d != decl && d.Name == newName); var existing = declarations.Find(d => d != decl && d.Name == newName);
if (result) if (existing != null)
return true; return CheckExisting(decl, existing);
if (decl is Method && decl.IsGenerated) if (decl is Method && decl.IsGenerated)
return @class.GetPropertyByName(newName) != null; return @class.GetPropertyByName(newName) != null;
@ -226,6 +226,19 @@ namespace CppSharp.Passes
f => !f.Ignore && f.Parameters.SequenceEqual(function.Parameters, new ParameterComparer())); f => !f.Ignore && f.Parameters.SequenceEqual(function.Parameters, new ParameterComparer()));
} }
private static bool CheckExisting(Declaration decl, Declaration existing)
{
var method = decl as Method;
var property = decl as Property;
if (method?.IsOverride != true && property?.IsOverride != true)
return true;
existing.Name = existing.Name == existing.OriginalName ||
string.IsNullOrEmpty(existing.OriginalName) ?
existing.Name + "_" : existing.OriginalName;
return false;
}
public override bool VisitClassDecl(Class @class) public override bool VisitClassDecl(Class @class)
{ {
if (!base.VisitClassDecl(@class)) if (!base.VisitClassDecl(@class))

6
tests/CSharp/CSharp.Tests.cs

@ -61,7 +61,11 @@ public unsafe class CSharpTests : GeneratorTestFixture
hasOverride.CauseRenamingError(); hasOverride.CauseRenamingError();
using (var qux = new Qux()) using (var qux = new Qux())
{ {
new Bar(qux).Dispose(); qux.Type.GetHashCode();
using (Bar bar = new Bar(qux))
{
bar.Type.GetHashCode();
}
} }
using (var quux = new Quux()) using (var quux = new Quux())
{ {

10
tests/CSharp/CSharp.cpp

@ -230,6 +230,11 @@ int Qux::takeReferenceToPointer(Foo*& ret)
return ret->A; return ret->A;
} }
int Qux::type() const
{
return 0;
}
Bar::Bar(Qux qux) Bar::Bar(Qux qux)
{ {
} }
@ -286,6 +291,11 @@ void Bar::setIndex(int value)
index = value; index = value;
} }
int Bar::type() const
{
return 1;
}
ForceCreationOfInterface::ForceCreationOfInterface() ForceCreationOfInterface::ForceCreationOfInterface()
{ {
} }

3
tests/CSharp/CSharp.h

@ -85,6 +85,7 @@ public:
void setInterface(Qux* qux); void setInterface(Qux* qux);
virtual void makeClassDynamic(); virtual void makeClassDynamic();
virtual int takeReferenceToPointer(Foo*& ret); virtual int takeReferenceToPointer(Foo*& ret);
virtual int type() const;
}; };
class DLL_API Bar : public Qux class DLL_API Bar : public Qux
@ -115,6 +116,8 @@ public:
int publicInt; int publicInt;
double publicDouble; double publicDouble;
}; };
static const int Type = 4;
int type() const override;
protected: protected:
enum class ProtectedNestedEnum enum class ProtectedNestedEnum

Loading…
Cancel
Save