Browse Source

Fix anonymous member name colliding with enclosing type name (#1425)

* Fix anonymous member name colliding with enclosing type name
pull/1426/head
josetr 5 years ago committed by GitHub
parent
commit
73670debbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      src/Generator/Passes/CleanInvalidDeclNamesPass.cs
  2. 6
      tests/CSharp/CSharp.Tests.cs
  3. 4
      tests/CSharp/CSharp.h
  4. 11
      tests/CSharp/ExcludedUnit.hpp

9
src/Generator/Passes/CleanInvalidDeclNamesPass.cs

@ -146,7 +146,14 @@ namespace CppSharp.Passes @@ -146,7 +146,14 @@ namespace CppSharp.Passes
{
var anonymousDecls = decls.Where(p => string.IsNullOrEmpty(p.Name)).ToList();
for (int i = 0; i < anonymousDecls.Count; i++)
anonymousDecls[i].Name = $"_{i}";
{
var anonymousDecl = anonymousDecls[i];
if (anonymousDecl.Namespace != null && anonymousDecl.Namespace.Name == anonymousDecl.Name)
anonymousDecl.Name = $"__{i}";
else
anonymousDecl.Name = $"_{i}";
}
}
}
}

6
tests/CSharp/CSharp.Tests.cs

@ -1429,4 +1429,10 @@ public unsafe class CSharpTests : GeneratorTestFixture @@ -1429,4 +1429,10 @@ public unsafe class CSharpTests : GeneratorTestFixture
{
Assert.That(CSharp.CSharp.TakeTypemapTypedefParam(false), Is.False);
}
[Test]
public void TestAnonymousMemberNameCollision()
{
StringAssert.EndsWith(nameof(CSharp.TestAnonymousMemberNameCollision._0.__0), "__0");
}
}

4
tests/CSharp/CSharp.h

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
#include <limits>
#include <string>
#include "AnotherUnit.h"
#include "ExcludedUnit.hpp"
#include "CSharpTemplates.h"
class DLL_API Foo
@ -1401,3 +1402,6 @@ struct { @@ -1401,3 +1402,6 @@ struct {
typedef int boolean_t;
DLL_API boolean_t takeTypemapTypedefParam(boolean_t b);
class DLL_API TestAnonymousMemberNameCollision : public ClassUsingUnion {
};

11
tests/CSharp/ExcludedUnit.hpp

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
#pragma once
class ClassUsingUnion {
public:
union {
float arr[2];
struct {
float a, b;
};
};
};
Loading…
Cancel
Save