Skip to content

Commit 573272e

Browse files
committed
Generate valid C# when a renamed override causes conflicts
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent d9a33ba commit 573272e

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

Diff for: src/Generator/Passes/RenamePass.cs

+16-3
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ where typedefDecl.Type.Desugar() is FunctionType
199199
declarations.AddRange(@class.TemplateParameters);
200200
}
201201

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

206206
if (decl is Method && decl.IsGenerated)
207207
return @class.GetPropertyByName(newName) != null;
@@ -226,6 +226,19 @@ private static IEnumerable<Function> GetFunctionsWithTheSameParams(Function func
226226
f => !f.Ignore && f.Parameters.SequenceEqual(function.Parameters, new ParameterComparer()));
227227
}
228228

229+
private static bool CheckExisting(Declaration decl, Declaration existing)
230+
{
231+
var method = decl as Method;
232+
var property = decl as Property;
233+
if (method?.IsOverride != true && property?.IsOverride != true)
234+
return true;
235+
236+
existing.Name = existing.Name == existing.OriginalName ||
237+
string.IsNullOrEmpty(existing.OriginalName) ?
238+
existing.Name + "_" : existing.OriginalName;
239+
return false;
240+
}
241+
229242
public override bool VisitClassDecl(Class @class)
230243
{
231244
if (!base.VisitClassDecl(@class))

Diff for: tests/CSharp/CSharp.Tests.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ public void TestUncompilableCode()
6161
hasOverride.CauseRenamingError();
6262
using (var qux = new Qux())
6363
{
64-
new Bar(qux).Dispose();
64+
qux.Type.GetHashCode();
65+
using (Bar bar = new Bar(qux))
66+
{
67+
bar.Type.GetHashCode();
68+
}
6569
}
6670
using (var quux = new Quux())
6771
{

Diff for: tests/CSharp/CSharp.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ int Qux::takeReferenceToPointer(Foo*& ret)
230230
return ret->A;
231231
}
232232

233+
int Qux::type() const
234+
{
235+
return 0;
236+
}
237+
233238
Bar::Bar(Qux qux)
234239
{
235240
}
@@ -286,6 +291,11 @@ void Bar::setIndex(int value)
286291
index = value;
287292
}
288293

294+
int Bar::type() const
295+
{
296+
return 1;
297+
}
298+
289299
ForceCreationOfInterface::ForceCreationOfInterface()
290300
{
291301
}

Diff for: tests/CSharp/CSharp.h

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class DLL_API Qux
8585
void setInterface(Qux* qux);
8686
virtual void makeClassDynamic();
8787
virtual int takeReferenceToPointer(Foo*& ret);
88+
virtual int type() const;
8889
};
8990

9091
class DLL_API Bar : public Qux
@@ -115,6 +116,8 @@ class DLL_API Bar : public Qux
115116
int publicInt;
116117
double publicDouble;
117118
};
119+
static const int Type = 4;
120+
int type() const override;
118121

119122
protected:
120123
enum class ProtectedNestedEnum

0 commit comments

Comments
 (0)