Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating ClangSharp to support UTF-8 literals in preview mode #342

Merged
merged 1 commit into from
Apr 28, 2022
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ The available configuration options (visible with `-c help`) are:
Options:
?, h, help Show help and usage information for -c, --config
compatible-codegen Bindings should be generated with .NET Standard 2.0 compatibility. Setting this disables preview code generation.
latest-codegen Bindings should be generated for the latest stable version of .NET/C#. This is currently .NET 5/C# 9.
preview-codegen Bindings should be generated for the latest preview version of .NET/C#. This is currently .NET 6/C# 10.
latest-codegen Bindings should be generated for the latest stable version of .NET/C#. This is currently .NET 6/C# 10.
preview-codegen Bindings should be generated for the latest preview version of .NET/C#. This is currently .NET 7/C# 11.
single-file Bindings should be generated to a single output file. This is the default.
multi-file Bindings should be generated so there is approximately one type per file.
unix-types Bindings should be generated assuming Unix defaults. This is the default on Unix platforms.
Expand Down
27 changes: 18 additions & 9 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2292,18 +2292,27 @@ private void VisitStringLiteral(StringLiteral stringLiteral)
case CX_CharacterKind.CX_CLK_Ascii:
case CX_CharacterKind.CX_CLK_UTF8:
{
outputBuilder.Write("new byte[] { ");

var bytes = Encoding.UTF8.GetBytes(stringLiteral.String);

foreach (var b in bytes)
if (Config.GeneratePreviewCode)
{
outputBuilder.Write("0x");
outputBuilder.Write(b.ToString("X2"));
outputBuilder.Write(", ");
outputBuilder.Write('"');
outputBuilder.Write(EscapeString(stringLiteral.String));
outputBuilder.Write("\\0\"u8");
}
else
{
outputBuilder.Write("new byte[] { ");

outputBuilder.Write("0x00 }");
var bytes = Encoding.UTF8.GetBytes(stringLiteral.String);

foreach (var b in bytes)
{
outputBuilder.Write("0x");
outputBuilder.Write(b.ToString("X2"));
outputBuilder.Write(", ");
}

outputBuilder.Write("0x00 }");
}
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ public IReadOnlyCollection<string> ExcludedNames

public bool GenerateNativeInheritanceAttribute => _options.HasFlag(PInvokeGeneratorConfigurationOptions.GenerateNativeInheritanceAttribute);

public bool GeneratePreviewCode => _options.HasFlag(PInvokeGeneratorConfigurationOptions.GeneratePreviewCode);

public bool GenerateSetsLastSystemErrorAttribute => _options.HasFlag(PInvokeGeneratorConfigurationOptions.GenerateSetsLastSystemErrorAttribute);

public bool GenerateSourceLocationAttribute => _options.HasFlag(PInvokeGeneratorConfigurationOptions.GenerateSourceLocationAttribute);
Expand Down
4 changes: 2 additions & 2 deletions sources/ClangSharpPInvokeGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class Program
// Codegen Options

new HelpItem("compatible-codegen", "Bindings should be generated with .NET Standard 2.0 compatibility. Setting this disables preview code generation."),
new HelpItem("latest-codegen", "Bindings should be generated for the latest stable version of .NET/C#. This is currently .NET 5/C# 9."),
new HelpItem("preview-codegen", "Bindings should be generated for the latest preview version of .NET/C#. This is currently .NET 6/C# 10."),
new HelpItem("latest-codegen", "Bindings should be generated for the latest stable version of .NET/C#. This is currently .NET 6/C# 10."),
new HelpItem("preview-codegen", "Bindings should be generated for the latest preview version of .NET/C#. This is currently .NET 7/C# 11."),

// File Options

Expand Down