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 573272e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
19 changes: 16 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,19 @@ 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 ||
string.IsNullOrEmpty(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 573272e

Please sign in to comment.