Browse Source

Fix naming conflicts with nested types and members

Fixes https://github.com/mono/CppSharp/issues/1353.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1355/head
Dimitar Dobrev 5 years ago
parent
commit
54c6159f35
  1. 19
      src/Generator/Passes/RenamePass.cs
  2. 4
      tests/Common/Common.Tests.cs
  3. 16
      tests/Common/Common.cpp
  4. 10
      tests/Common/Common.h

19
src/Generator/Passes/RenamePass.cs

@ -47,12 +47,10 @@ namespace CppSharp.Passes @@ -47,12 +47,10 @@ namespace CppSharp.Passes
public virtual bool Rename(Declaration decl, out string newName)
{
var method = decl as Method;
if (method != null && !method.IsStatic)
if (decl is Method method && !method.IsStatic)
{
Method rootBaseMethod;
var @class = method.OriginalNamespace as Class;
if (@class != null && @class.IsInterface)
if (method.OriginalNamespace is Class @class && @class.IsInterface)
rootBaseMethod = (Method) method.OriginalFunction;
else
rootBaseMethod = method.GetRootBaseMethod();
@ -63,8 +61,7 @@ namespace CppSharp.Passes @@ -63,8 +61,7 @@ namespace CppSharp.Passes
}
}
var property = decl as Property;
if (property != null && !property.IsStatic)
if (decl is Property property && !property.IsStatic)
{
var rootBaseProperty = ((Class) property.Namespace).GetBasePropertyByName(property);
if (rootBaseProperty != null && rootBaseProperty != property)
@ -74,6 +71,16 @@ namespace CppSharp.Passes @@ -74,6 +71,16 @@ namespace CppSharp.Passes
}
}
if (!(decl is ClassTemplateSpecialization) &&
!string.IsNullOrEmpty(decl.Name) && AreThereConflicts(decl, decl.Name))
{
char initialLetter = char.IsUpper(decl.Name[0]) ?
char.ToLowerInvariant(decl.Name[0]) :
char.ToUpperInvariant(decl.Name[0]);
newName = initialLetter + decl.Name.Substring(1);
return true;
}
newName = decl.Name;
return false;
}

4
tests/Common/Common.Tests.cs

@ -567,6 +567,10 @@ public class CommonTests : GeneratorTestFixture @@ -567,6 +567,10 @@ public class CommonTests : GeneratorTestFixture
prop.StartWithVerb = 5;
Assert.That(prop.Contains('a'), Is.EqualTo(prop.Contains("a")));
Assert.That(prop.conflict, Is.EqualTo(CommonTest.TestProperties.Conflict.Value1));
prop.conflict = CommonTest.TestProperties.Conflict.Value2;
Assert.That(prop.conflict, Is.EqualTo(CommonTest.TestProperties.Conflict.Value2));
}
using (var prop = new HasOverridenSetter())
{

16
tests/Common/Common.cpp

@ -541,7 +541,8 @@ SomeNamespace::AbstractClass::~AbstractClass() @@ -541,7 +541,8 @@ SomeNamespace::AbstractClass::~AbstractClass()
}
TestProperties::TestProperties() : Field(0), _refToPrimitiveInSetter(0),
_getterAndSetterWithTheSameName(0), _setterReturnsBoolean(0), _virtualSetterReturnsBoolean(0)
_getterAndSetterWithTheSameName(0), _setterReturnsBoolean(0),
_virtualSetterReturnsBoolean(0), _conflict(Conflict::Value1)
{
}
@ -550,7 +551,8 @@ TestProperties::TestProperties(const TestProperties& other) : Field(other.Field) @@ -550,7 +551,8 @@ TestProperties::TestProperties(const TestProperties& other) : Field(other.Field)
_refToPrimitiveInSetter(other._refToPrimitiveInSetter),
_getterAndSetterWithTheSameName(other._getterAndSetterWithTheSameName),
_setterReturnsBoolean(other._setterReturnsBoolean),
_virtualSetterReturnsBoolean(other._virtualSetterReturnsBoolean)
_virtualSetterReturnsBoolean(other._virtualSetterReturnsBoolean),
_conflict(other._conflict)
{
}
@ -693,6 +695,16 @@ bool TestProperties::contains(const char* str) @@ -693,6 +695,16 @@ bool TestProperties::contains(const char* str)
return true;
}
TestProperties::Conflict TestProperties::GetConflict()
{
return _conflict;
}
void TestProperties::SetConflict(Conflict conflict)
{
_conflict = conflict;
}
HasOverridenSetter::HasOverridenSetter()
{
}

10
tests/Common/Common.h

@ -590,6 +590,12 @@ public: @@ -590,6 +590,12 @@ public:
Value2
};
enum class Conflict
{
Value1,
Value2
};
TestProperties();
TestProperties(const TestProperties& other);
int Field;
@ -636,12 +642,16 @@ public: @@ -636,12 +642,16 @@ public:
bool contains(char c);
bool contains(const char* str);
Conflict GetConflict();
void SetConflict(Conflict _conflict);
private:
int FieldValue;
double _refToPrimitiveInSetter;
int _getterAndSetterWithTheSameName;
int _setterReturnsBoolean;
int _virtualSetterReturnsBoolean;
Conflict _conflict;
};
class DLL_API HasOverridenSetter : public TestProperties

Loading…
Cancel
Save