Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,16 @@ private void VisitDeclRefExpr(DeclRefExpr declRefExpr)

if (declRefExpr.Decl is EnumConstantDecl enumConstantDecl)
{
if (enumConstantDecl.DeclContext is NamedDecl enumTypeDecl)
{
var enumTypeName = GetRemappedCursorName(enumTypeDecl, out _, skipUsing: true);

if (!IsAnonymousEnum(enumTypeName))
{
escapedName = EscapeAndStripEnumMemberName(name, enumTypeName);
}
}

if ((declRefExpr.DeclContext != enumConstantDecl.DeclContext) && (enumConstantDecl.DeclContext is NamedDecl namedDecl))
{
var enumName = GetRemappedCursorName(namedDecl, out _, skipUsing: true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.

using System.Threading.Tasks;
using NUnit.Framework;

namespace ClangSharp.UnitTests;

/// <summary>
/// Regression test for https://github.com/dotnet/ClangSharp/issues/576.
/// When <c>strip-enum-member-type-name</c> is used, references to sibling enum members inside
/// an initializer expression must be stripped the same way as the member declarations, otherwise
/// the emitted code references undefined names.
/// </summary>
[Platform("win")]
public sealed class StripEnumMemberReferenceTest : PInvokeGeneratorTest
{
[Test]
public Task SiblingReferencesAreStripped()
{
var inputContents = @"typedef enum WGPUInstanceBackend
{
WGPUInstanceBackend_Vulkan = 1 << 0,
WGPUInstanceBackend_GL = 1 << 1,
WGPUInstanceBackend_Metal = 1 << 2,
WGPUInstanceBackend_Primary = WGPUInstanceBackend_Vulkan | WGPUInstanceBackend_Metal,
WGPUInstanceBackend_Secondary = WGPUInstanceBackend_GL
} WGPUInstanceBackend;
";

var expectedOutputContents = @"namespace ClangSharp.Test
{
public enum WGPUInstanceBackend
{
Vulkan = 1 << 0,
GL = 1 << 1,
Metal = 1 << 2,
Primary = Vulkan | Metal,
Secondary = GL,
}
}
";

return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, PInvokeGeneratorConfigurationOptions.StripEnumMemberTypeName);
}
}
Loading