diff --git a/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs index 5060f29d..56daf313 100644 --- a/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.VisitDecl.cs @@ -224,6 +224,10 @@ public void EndValue(in ValueDesc desc) WriteLine(';'); } } + else + { + WriteLine(';'); + } break; } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/GlobalStructInitializerTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/GlobalStructInitializerTest.cs new file mode 100644 index 00000000..4415f669 --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/GlobalStructInitializerTest.cs @@ -0,0 +1,43 @@ +// 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; + +public sealed class GlobalStructInitializerTest : PInvokeGeneratorTest +{ + // Regression test for https://github.com/dotnet/clangsharp/issues/503 + // A non-const global variable initialized with a struct initializer list was + // missing its terminating semicolon, which corrupted the rest of the file. + [Test] + public Task NonConstGlobalTest() + { + var inputContents = @"typedef struct Point { int x; int y; } Point; + +Point MyGlobalPoint = { .x = 10, .y = 20 }; +"; + + var expectedOutputContents = @"namespace ClangSharp.Test +{ + public partial struct Point + { + public int x; + + public int y; + } + + public static partial class Methods + { + public static Point MyGlobalPoint = new Point + { + x = 10, + y = 20, + }; + } +} +"; + + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } +}