diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index abf6e627..10d2761c 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -2607,6 +2607,7 @@ private void VisitVarDecl(VarDecl varDecl) _outputBuilder.Write(' '); var isProperty = false; + var isStringLiteral = false; if (IsStmtAsWritten(varDecl.Init, out var stringLiteral, removeParens: true)) { @@ -2620,6 +2621,7 @@ private void VisitVarDecl(VarDecl varDecl) typeName = "ReadOnlySpan"; isProperty = true; + isStringLiteral = true; break; } @@ -2638,6 +2640,7 @@ private void VisitVarDecl(VarDecl varDecl) _outputBuilder.Write("const "); typeName = "string"; + isStringLiteral = true; break; } @@ -2664,7 +2667,7 @@ private void VisitVarDecl(VarDecl varDecl) _outputBuilder.Write(typeName); - if (type is ArrayType) + if (!isStringLiteral && type is ArrayType) { _outputBuilder.Write("[]"); } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/VarDeclarationTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/VarDeclarationTest.cs index 1edcd13e..1cb83127 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/VarDeclarationTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/VarDeclarationTest.cs @@ -180,6 +180,44 @@ public static partial class Methods await ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents); } + [Fact] + public async Task WideStringLiteralConstTest() + { + var inputContents = $@"const wchar_t MyConst1[] = L""Test"";"; + + var expectedOutputContents = $@"namespace ClangSharp.Test +{{ + public static partial class Methods + {{ + [NativeTypeName(""const wchar_t [5]"")] + public const string MyConst1 = ""Test""; + }} +}} +"; + + await ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents); + } + + [Fact] + public async Task StringLiteralConstTest() + { + var inputContents = $@"const char MyConst1[] = ""Test"";"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public static partial class Methods + {{ + [NativeTypeName(""const char [5]"")] + public static ReadOnlySpan MyConst1 => new byte[] {{ 0x54, 0x65, 0x73, 0x74, 0x00 }}; + }} +}} +"; + + await ValidateGeneratedBindingsAsync(inputContents, expectedOutputContents); + } + [Fact] public async Task UncheckedConversionMacroTest() {