Skip to content

Commit

Permalink
Generate valid C# when a renamed override causes conflicts
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
  • Loading branch information
ddobrev committed Jul 13, 2019
1 parent d9a33ba commit 046f29c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/Generator/Passes/RenamePass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ where typedefDecl.Type.Desugar() is FunctionType
declarations.AddRange(@class.TemplateParameters);
}

var result = declarations.Any(d => d != decl && d.Name == newName);
if (result)
return true;
var existing = declarations.Find(d => d != decl && d.Name == newName);
if (existing != null)
return CheckExisting(decl, existing);

if (decl is Method && decl.IsGenerated)
return @class.GetPropertyByName(newName) != null;
Expand All @@ -226,6 +226,18 @@ private static IEnumerable<Function> GetFunctionsWithTheSameParams(Function func
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 ?
existing.Name + "_" : existing.OriginalName;
return false;
}

public override bool VisitClassDecl(Class @class)
{
if (!base.VisitClassDecl(@class))
Expand Down
6 changes: 5 additions & 1 deletion tests/CSharp/CSharp.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ public void TestUncompilableCode()
hasOverride.CauseRenamingError();
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())
{
Expand Down
10 changes: 10 additions & 0 deletions tests/CSharp/CSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ int Qux::takeReferenceToPointer(Foo*& ret)
return ret->A;
}

int Qux::type() const
{
return 0;
}

Bar::Bar(Qux qux)
{
}
Expand Down Expand Up @@ -286,6 +291,11 @@ void Bar::setIndex(int value)
index = value;
}

int Bar::type() const
{
return 1;
}

ForceCreationOfInterface::ForceCreationOfInterface()
{
}
Expand Down
3 changes: 3 additions & 0 deletions tests/CSharp/CSharp.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class DLL_API Qux
void setInterface(Qux* qux);
virtual void makeClassDynamic();
virtual int takeReferenceToPointer(Foo*& ret);
virtual int type() const;
};

class DLL_API Bar : public Qux
Expand Down Expand Up @@ -115,6 +116,8 @@ class DLL_API Bar : public Qux
int publicInt;
double publicDouble;
};
static const int Type = 4;
int type() const override;

protected:
enum class ProtectedNestedEnum
Expand Down

0 comments on commit 046f29c

Please sign in to comment.