From 73670debbc4f4bcee1ce0414685e5b46163ac6be Mon Sep 17 00:00:00 2001 From: josetr <37419832+josetr@users.noreply.github.com> Date: Fri, 16 Oct 2020 20:18:32 +0100 Subject: [PATCH] Fix anonymous member name colliding with enclosing type name (#1425) * Fix anonymous member name colliding with enclosing type name --- src/Generator/Passes/CleanInvalidDeclNamesPass.cs | 9 ++++++++- tests/CSharp/CSharp.Tests.cs | 6 ++++++ tests/CSharp/CSharp.h | 4 ++++ tests/CSharp/ExcludedUnit.hpp | 11 +++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/CSharp/ExcludedUnit.hpp diff --git a/src/Generator/Passes/CleanInvalidDeclNamesPass.cs b/src/Generator/Passes/CleanInvalidDeclNamesPass.cs index 00d71959..56113d60 100644 --- a/src/Generator/Passes/CleanInvalidDeclNamesPass.cs +++ b/src/Generator/Passes/CleanInvalidDeclNamesPass.cs @@ -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}"; + } } } } diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index d7d7662d..2dd953ef 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -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"); + } } diff --git a/tests/CSharp/CSharp.h b/tests/CSharp/CSharp.h index 277f5afa..e181dde5 100644 --- a/tests/CSharp/CSharp.h +++ b/tests/CSharp/CSharp.h @@ -6,6 +6,7 @@ #include #include #include "AnotherUnit.h" +#include "ExcludedUnit.hpp" #include "CSharpTemplates.h" class DLL_API Foo @@ -1401,3 +1402,6 @@ struct { typedef int boolean_t; DLL_API boolean_t takeTypemapTypedefParam(boolean_t b); +class DLL_API TestAnonymousMemberNameCollision : public ClassUsingUnion { + +}; diff --git a/tests/CSharp/ExcludedUnit.hpp b/tests/CSharp/ExcludedUnit.hpp new file mode 100644 index 00000000..4a7b38d8 --- /dev/null +++ b/tests/CSharp/ExcludedUnit.hpp @@ -0,0 +1,11 @@ +#pragma once + +class ClassUsingUnion { +public: + union { + float arr[2]; + struct { + float a, b; + }; + }; +};