Browse Source

Prioritize public non-field properties when resolving naming conflicts

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

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1389/head
Dimitar Dobrev 5 years ago
parent
commit
28a5c5d76c
  1. 2
      src/Generator/Passes/CheckDuplicatedNamesPass.cs
  2. 17
      src/Generator/Passes/GetterSetterToPropertyPass.cs
  3. 26
      src/Generator/Passes/RenamePass.cs
  4. 2
      tests/Common/Common.Tests.cs
  5. 11
      tests/Common/Common.cpp
  6. 5
      tests/Common/Common.h

2
src/Generator/Passes/CheckDuplicatedNamesPass.cs

@ -36,7 +36,7 @@ namespace CppSharp.Passes @@ -36,7 +36,7 @@ namespace CppSharp.Passes
}
var count = Count++;
if (count == 0)
if (count <= 1)
return false;
decl.Name += count.ToString(CultureInfo.InvariantCulture);

17
src/Generator/Passes/GetterSetterToPropertyPass.cs

@ -311,18 +311,14 @@ namespace CppSharp.Passes @@ -311,18 +311,14 @@ namespace CppSharp.Passes
(string.Compare(name, firstWord, StringComparison.InvariantCultureIgnoreCase) != 0) &&
!char.IsNumber(name[3]))
{
if (char.IsLower(name[0]))
if (name.Length == 4)
{
if (name.Length == 4)
{
return char.ToLowerInvariant(
name[3]).ToString(CultureInfo.InvariantCulture);
}
return char.ToLowerInvariant(
name[3]).ToString(CultureInfo.InvariantCulture) +
name.Substring(4);
name[3]).ToString(CultureInfo.InvariantCulture);
}
return name.Substring(3);
return char.ToLowerInvariant(
name[3]).ToString(CultureInfo.InvariantCulture) +
name.Substring(4);
}
return name;
}
@ -337,8 +333,7 @@ namespace CppSharp.Passes @@ -337,8 +333,7 @@ namespace CppSharp.Passes
return nameBuilder.ToString();
nameBuilder.TrimUnderscores();
if (char.IsLower(name[0]) && !char.IsLower(nameBuilder[0]))
nameBuilder[0] = char.ToLowerInvariant(nameBuilder[0]);
nameBuilder[0] = char.ToLowerInvariant(nameBuilder[0]);
return nameBuilder.ToString();
}

26
src/Generator/Passes/RenamePass.cs

@ -71,16 +71,6 @@ namespace CppSharp.Passes @@ -71,16 +71,6 @@ namespace CppSharp.Passes
}
}
if (!(decl is Class) &&
!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;
}
@ -214,9 +204,19 @@ namespace CppSharp.Passes @@ -214,9 +204,19 @@ namespace CppSharp.Passes
return @class.GetPropertyByName(newName) != null;
var property = decl as Property;
if (property != null && property.Field != null)
return ((Class) decl.Namespace).Properties.Find(
p => p != decl && p.Name == newName) != null;
if (property != null)
{
Property existingProperty = @class.Properties.Find(
p => p != decl && p.Name == newName);
if (existingProperty != null)
{
if (property.Access <= existingProperty.Access &&
(property.Field != null || existingProperty.Field == null))
return true;
existingProperty.Name = property.Name;
}
}
var enumItem = decl as Enumeration.Item;
if (enumItem != null)

2
tests/Common/Common.Tests.cs

@ -580,6 +580,8 @@ public class CommonTests : GeneratorTestFixture @@ -580,6 +580,8 @@ public class CommonTests : GeneratorTestFixture
prop.Callback = x => 4 * x;
Assert.That(prop.Callback(5), Is.EqualTo(20));
Assert.That(prop.ArchiveName, Is.EqualTo(20));
}
using (var prop = new HasOverridenSetter())
{

11
tests/Common/Common.cpp

@ -545,14 +545,16 @@ SomeNamespace::AbstractClass::~AbstractClass() @@ -545,14 +545,16 @@ SomeNamespace::AbstractClass::~AbstractClass()
{
}
TestProperties::TestProperties() : Field(0), _refToPrimitiveInSetter(0),
TestProperties::TestProperties() : Field(0), ArchiveName(0),
FieldValue(0), _refToPrimitiveInSetter(0),
_getterAndSetterWithTheSameName(0), _setterReturnsBoolean(0),
_virtualSetterReturnsBoolean(0), _conflict(Conflict::Value1),
ConstRefField(Field)
{
}
TestProperties::TestProperties(const TestProperties& other) : Field(other.Field),
TestProperties::TestProperties(const TestProperties& other) :
Field(other.Field), ArchiveName(other.ArchiveName),
FieldValue(other.FieldValue),
_refToPrimitiveInSetter(other._refToPrimitiveInSetter),
_getterAndSetterWithTheSameName(other._getterAndSetterWithTheSameName),
@ -733,6 +735,11 @@ void TestProperties::setCallback(int(*value)(int)) @@ -733,6 +735,11 @@ void TestProperties::setCallback(int(*value)(int))
_callback = value;
}
int TestProperties::GetArchiveName() const
{
return 20;
}
HasOverridenSetter::HasOverridenSetter()
{
}

5
tests/Common/Common.h

@ -653,6 +653,11 @@ public: @@ -653,6 +653,11 @@ public:
virtual int(*getCallback())(int);
virtual void setCallback(int(*value)(int));
int GetArchiveName() const;
protected:
const int ArchiveName;
private:
int FieldValue;
double _refToPrimitiveInSetter;

Loading…
Cancel
Save