Skip to content

Commit a1eed29

Browse files
committed
Add strip-enum-member-type-name config option.
1 parent 305eff5 commit a1eed29

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ Options:
256256
generate-unmanaged-constants Unmanaged constants should be generated using static ref readonly properties. This is currently experimental.
257257
generate-vtbl-index-attribute [VtblIndex(#)] attribute should be generated to document the underlying VTBL index for a helper method.
258258
259+
# Stripping Options
260+
261+
strip-enum-member-type-name Strips the enum type name from the beginning of its member names.
262+
259263
# Logging Options
260264
261265
log-exclusions A list of excluded declaration types should be generated. This will also log if the exclusion was due to an exact or partial match.

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using static ClangSharp.Interop.CXUnaryOperatorKind;
2020
using static ClangSharp.Interop.CXEvalResultKind;
2121
using static ClangSharp.Interop.CXTypeKind;
22+
using System.Text.RegularExpressions;
2223

2324
namespace ClangSharp;
2425

@@ -294,6 +295,13 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl)
294295
parentName = _outputBuilder.Name;
295296
}
296297

298+
var strippedName = escapedName;
299+
if (Config.StripEnumMemberTypeName)
300+
{
301+
Regex stripParentNameRegex = new($"^{Regex.Escape(parentName)}_*");
302+
strippedName = stripParentNameRegex.Replace(escapedName, string.Empty);
303+
}
304+
297305
var kind = isAnonymousEnum ? ValueKind.Primitive : ValueKind.Enumerator;
298306
var flags = ValueFlags.Constant;
299307

@@ -305,7 +313,7 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl)
305313
var desc = new ValueDesc {
306314
AccessSpecifier = accessSpecifier,
307315
TypeName = typeName,
308-
EscapedName = escapedName,
316+
EscapedName = Config.StripEnumMemberTypeName ? strippedName : escapedName,
309317
NativeTypeName = null,
310318
ParentName = parentName,
311319
Kind = kind,

sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ public IReadOnlyCollection<string> ExcludedNames
265265

266266
public bool GenerateVtblIndexAttribute => _options.HasFlag(PInvokeGeneratorConfigurationOptions.GenerateVtblIndexAttribute);
267267

268+
public bool StripEnumMemberTypeName => _options.HasFlag(PInvokeGeneratorConfigurationOptions.StripEnumMemberTypeName);
269+
268270
public string HeaderText => _headerText;
269271

270272
[AllowNull]

sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfigurationOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,6 @@ public enum PInvokeGeneratorConfigurationOptions : long
8686
GenerateCallConvMemberFunction = 1L << 37,
8787

8888
GenerateGenericPointerWrapper = 1L << 38,
89+
90+
StripEnumMemberTypeName = 1L << 39,
8991
}

sources/ClangSharpPInvokeGenerator/Program.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ public static class Program
177177
new TwoColumnHelpRow("generate-unmanaged-constants", "Unmanaged constants should be generated using static ref readonly properties. This is currently experimental."),
178178
new TwoColumnHelpRow("generate-vtbl-index-attribute", "[VtblIndex(#)] attribute should be generated to document the underlying VTBL index for a helper method."),
179179

180+
new TwoColumnHelpRow("", ""),
181+
new TwoColumnHelpRow("# Stripping Options", ""),
182+
new TwoColumnHelpRow("", ""),
183+
184+
new TwoColumnHelpRow("strip-enum-member-type-name", "Strips the enum type name from the beginning of its member names."),
185+
180186
new TwoColumnHelpRow("", ""),
181187
new TwoColumnHelpRow("# Logging Options", ""),
182188
new TwoColumnHelpRow("", ""),
@@ -617,6 +623,14 @@ public static void Run(InvocationContext context)
617623
break;
618624
}
619625

626+
// Strip Options
627+
628+
case "strip-enum-member-type-name":
629+
{
630+
configOptions |= PInvokeGeneratorConfigurationOptions.StripEnumMemberTypeName;
631+
break;
632+
}
633+
620634
// Legacy Options
621635

622636
case "default-remappings":

0 commit comments

Comments
 (0)