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

Refactor Enum member type escaping #556

Merged
merged 1 commit into from
May 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl)

var accessSpecifier = AccessSpecifier.None;
var name = GetRemappedCursorName(enumConstantDecl);
var escapedName = EscapeName(name);
var typeName = GetTargetTypeName(enumConstantDecl, out _);
var isAnonymousEnum = false;
var parentName = "";
Expand All @@ -295,10 +294,7 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl)
parentName = _outputBuilder.Name;
}

if (Config.StripEnumMemberTypeName)
{
escapedName = PrefixAndStrip(escapedName, parentName, trimChar: '_');
}
var escapedName = EscapeAndStripEnumMemberName(name, parentName);

var kind = isAnonymousEnum ? ValueKind.Primitive : ValueKind.Enumerator;
var flags = ValueFlags.Constant;
Expand Down
14 changes: 14 additions & 0 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2332,6 +2332,20 @@ private string EscapeAndStripMethodName(string name)
return EscapeName(name);
}

private string EscapeAndStripEnumMemberName(string name, string enumTypeName)
{
if (Config.StripEnumMemberTypeName)
{
var escapedName = PrefixAndStrip(name, enumTypeName, trimChar: '_');
if (escapedName.Length > 0 && char.IsAsciiDigit(escapedName[0]))
{
escapedName = '_' + escapedName;
}
return escapedName;
}
return EscapeName(name);
}

internal static string EscapeCharacter(char value) => value switch {
'\0' => @"\0",
'\\' => @"\\",
Expand Down