From a1eed293a9af700a6998dc55867e7ef14f105069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B8is=C3=A6ther=20Rasch?= Date: Wed, 7 Feb 2024 08:44:27 +0100 Subject: [PATCH 1/6] Add strip-enum-member-type-name config option. --- README.md | 4 ++++ .../PInvokeGenerator.VisitDecl.cs | 10 +++++++++- .../PInvokeGeneratorConfiguration.cs | 2 ++ .../PInvokeGeneratorConfigurationOptions.cs | 2 ++ sources/ClangSharpPInvokeGenerator/Program.cs | 14 ++++++++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8d8df0c9..3a36108e 100644 --- a/README.md +++ b/README.md @@ -256,6 +256,10 @@ Options: generate-unmanaged-constants Unmanaged constants should be generated using static ref readonly properties. This is currently experimental. generate-vtbl-index-attribute [VtblIndex(#)] attribute should be generated to document the underlying VTBL index for a helper method. + # Stripping Options + + strip-enum-member-type-name Strips the enum type name from the beginning of its member names. + # Logging Options 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. diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index 10a6b486..a8b4b4cc 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -19,6 +19,7 @@ using static ClangSharp.Interop.CXUnaryOperatorKind; using static ClangSharp.Interop.CXEvalResultKind; using static ClangSharp.Interop.CXTypeKind; +using System.Text.RegularExpressions; namespace ClangSharp; @@ -294,6 +295,13 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl) parentName = _outputBuilder.Name; } + var strippedName = escapedName; + if (Config.StripEnumMemberTypeName) + { + Regex stripParentNameRegex = new($"^{Regex.Escape(parentName)}_*"); + strippedName = stripParentNameRegex.Replace(escapedName, string.Empty); + } + var kind = isAnonymousEnum ? ValueKind.Primitive : ValueKind.Enumerator; var flags = ValueFlags.Constant; @@ -305,7 +313,7 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl) var desc = new ValueDesc { AccessSpecifier = accessSpecifier, TypeName = typeName, - EscapedName = escapedName, + EscapedName = Config.StripEnumMemberTypeName ? strippedName : escapedName, NativeTypeName = null, ParentName = parentName, Kind = kind, diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs index 3dfb786b..e36133e0 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs @@ -265,6 +265,8 @@ public IReadOnlyCollection ExcludedNames public bool GenerateVtblIndexAttribute => _options.HasFlag(PInvokeGeneratorConfigurationOptions.GenerateVtblIndexAttribute); + public bool StripEnumMemberTypeName => _options.HasFlag(PInvokeGeneratorConfigurationOptions.StripEnumMemberTypeName); + public string HeaderText => _headerText; [AllowNull] diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfigurationOptions.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfigurationOptions.cs index db91ccd4..c2a34293 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfigurationOptions.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfigurationOptions.cs @@ -86,4 +86,6 @@ public enum PInvokeGeneratorConfigurationOptions : long GenerateCallConvMemberFunction = 1L << 37, GenerateGenericPointerWrapper = 1L << 38, + + StripEnumMemberTypeName = 1L << 39, } diff --git a/sources/ClangSharpPInvokeGenerator/Program.cs b/sources/ClangSharpPInvokeGenerator/Program.cs index 6dd06fee..69eb17ee 100644 --- a/sources/ClangSharpPInvokeGenerator/Program.cs +++ b/sources/ClangSharpPInvokeGenerator/Program.cs @@ -177,6 +177,12 @@ public static class Program new TwoColumnHelpRow("generate-unmanaged-constants", "Unmanaged constants should be generated using static ref readonly properties. This is currently experimental."), new TwoColumnHelpRow("generate-vtbl-index-attribute", "[VtblIndex(#)] attribute should be generated to document the underlying VTBL index for a helper method."), + new TwoColumnHelpRow("", ""), + new TwoColumnHelpRow("# Stripping Options", ""), + new TwoColumnHelpRow("", ""), + + new TwoColumnHelpRow("strip-enum-member-type-name", "Strips the enum type name from the beginning of its member names."), + new TwoColumnHelpRow("", ""), new TwoColumnHelpRow("# Logging Options", ""), new TwoColumnHelpRow("", ""), @@ -617,6 +623,14 @@ public static void Run(InvocationContext context) break; } + // Strip Options + + case "strip-enum-member-type-name": + { + configOptions |= PInvokeGeneratorConfigurationOptions.StripEnumMemberTypeName; + break; + } + // Legacy Options case "default-remappings": From da4255fc77d3c079736837d411ba65a4284bbeb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B8is=C3=A6ther=20Rasch?= Date: Wed, 7 Feb 2024 09:04:08 +0100 Subject: [PATCH 2/6] Match enum type name case insensitively --- .../ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index a8b4b4cc..ed4c93b4 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -298,7 +298,7 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl) var strippedName = escapedName; if (Config.StripEnumMemberTypeName) { - Regex stripParentNameRegex = new($"^{Regex.Escape(parentName)}_*"); + Regex stripParentNameRegex = new($"^{Regex.Escape(parentName)}_*", RegexOptions.IgnoreCase); strippedName = stripParentNameRegex.Replace(escapedName, string.Empty); } From 44af1626a997d9ab24765b93ae732857d680da42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B8is=C3=A6ther=20Rasch?= Date: Wed, 7 Feb 2024 09:04:59 +0100 Subject: [PATCH 3/6] Contain StripEnumMemberTypeName config option logic in a single location --- .../PInvokeGenerator.VisitDecl.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index ed4c93b4..6ddf4af4 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -295,11 +295,11 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl) parentName = _outputBuilder.Name; } - var strippedName = escapedName; if (Config.StripEnumMemberTypeName) { Regex stripParentNameRegex = new($"^{Regex.Escape(parentName)}_*", RegexOptions.IgnoreCase); - strippedName = stripParentNameRegex.Replace(escapedName, string.Empty); + var strippedName = stripParentNameRegex.Replace(escapedName, string.Empty); + escapedName = strippedName; } var kind = isAnonymousEnum ? ValueKind.Primitive : ValueKind.Enumerator; @@ -313,7 +313,7 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl) var desc = new ValueDesc { AccessSpecifier = accessSpecifier, TypeName = typeName, - EscapedName = Config.StripEnumMemberTypeName ? strippedName : escapedName, + EscapedName = escapedName, NativeTypeName = null, ParentName = parentName, Kind = kind, From fa6653d54abe4206fb8231755fda408e67b068bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B8is=C3=A6ther=20Rasch?= Date: Wed, 7 Feb 2024 09:06:34 +0100 Subject: [PATCH 4/6] Move Regex using next to related usings --- .../ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index 6ddf4af4..393d23f3 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Runtime.InteropServices; using System.Text; +using System.Text.RegularExpressions; using ClangSharp.Abstractions; using ClangSharp.CSharp; using static ClangSharp.Interop.CX_CastKind; @@ -19,7 +20,6 @@ using static ClangSharp.Interop.CXUnaryOperatorKind; using static ClangSharp.Interop.CXEvalResultKind; using static ClangSharp.Interop.CXTypeKind; -using System.Text.RegularExpressions; namespace ClangSharp; From 6703d6a4cbbcd82557057c6443724bd929b0ba05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B8is=C3=A6ther=20Rasch?= Date: Sun, 19 May 2024 15:20:22 +0200 Subject: [PATCH 5/6] Simplify prefix stripping logic and consolidate --- .../PInvokeGenerator.VisitDecl.cs | 18 +++++----- .../PInvokeGenerator.VisitStmt.cs | 2 +- .../PInvokeGenerator.cs | 33 ++++++++++++++----- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index 393d23f3..974b98da 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -297,9 +297,7 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl) if (Config.StripEnumMemberTypeName) { - Regex stripParentNameRegex = new($"^{Regex.Escape(parentName)}_*", RegexOptions.IgnoreCase); - var strippedName = stripParentNameRegex.Replace(escapedName, string.Empty); - escapedName = strippedName; + escapedName = PrefixAndStrip(escapedName, parentName, trimChar: '_'); } var kind = isAnonymousEnum ? ValueKind.Primitive : ValueKind.Enumerator; @@ -543,12 +541,12 @@ private void VisitFunctionDecl(FunctionDecl functionDecl) if ((cxxMethodDecl is not null) && cxxMethodDecl.IsVirtual) { isVirtual = true; - escapedName = PrefixAndStripName(name, GetOverloadIndex(cxxMethodDecl)); + escapedName = PrefixAndStripMethodName(name, GetOverloadIndex(cxxMethodDecl)); } else { isVirtual = false; - escapedName = EscapeAndStripName(name); + escapedName = EscapeAndStripMethodName(name); } var returnType = functionDecl.ReturnType; @@ -2032,7 +2030,7 @@ void OutputMarkerInterface(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethodD var desc = new FunctionOrDelegateDesc { AccessSpecifier = AccessSpecifier.Public, - EscapedName = EscapeAndStripName(name), + EscapedName = EscapeAndStripMethodName(name), IsMemberFunction = true, NativeTypeName = nativeTypeName, HasFnPtrCodeGen = !_config.ExcludeFnptrCodegen, @@ -2120,7 +2118,7 @@ void OutputVtblEntry(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethodDecl) } var remappedName = FixupNameForMultipleHits(cxxMethodDecl); - var escapedName = EscapeAndStripName(remappedName); + var escapedName = EscapeAndStripMethodName(remappedName); var desc = new FieldDesc { @@ -2189,7 +2187,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod var desc = new FunctionOrDelegateDesc { AccessSpecifier = AccessSpecifier.Public, IsAggressivelyInlined = _config.GenerateAggressiveInlining, - EscapedName = EscapeAndStripName(name), + EscapedName = EscapeAndStripMethodName(name), ParentName = parentName, IsMemberFunction = true, IsInherited = isInherited, @@ -2269,7 +2267,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod { body.Write("Marshal.GetDelegateForFunctionPointer<"); body.BeginMarker("delegate"); - body.Write(PrefixAndStripName(name, GetOverloadIndex(cxxMethodDecl))); + body.Write(PrefixAndStripMethodName(name, GetOverloadIndex(cxxMethodDecl))); body.EndMarker("delegate"); body.Write(">("); } @@ -2278,7 +2276,7 @@ void OutputVtblHelperMethod(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethod { body.Write("lpVtbl->"); body.BeginMarker("vtbl", new KeyValuePair("explicit", true)); - body.Write(EscapeAndStripName(remappedName)); + body.Write(EscapeAndStripMethodName(remappedName)); body.EndMarker("vtbl"); } else diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs index e216d889..3fd39a09 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs @@ -970,7 +970,7 @@ private void VisitDeclRefExpr(DeclRefExpr declRefExpr) if (declRefExpr.Decl is FunctionDecl) { - escapedName = EscapeAndStripName(name); + escapedName = EscapeAndStripMethodName(name); } } diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs index 6edb8098..76ee2b96 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -2326,13 +2326,9 @@ private static string EscapeName(string name) } } - private string EscapeAndStripName(string name) + private string EscapeAndStripMethodName(string name) { - if (name.StartsWith(_config.MethodPrefixToStrip, StringComparison.Ordinal)) - { - name = name[_config.MethodPrefixToStrip.Length..]; - } - + name = PrefixAndStrip(name, _config.MethodPrefixToStrip); return EscapeName(name); } @@ -6198,13 +6194,32 @@ private void ParenthesizeStmt(Stmt stmt) } } - private string PrefixAndStripName(string name, uint overloadIndex) + /// + /// Checks whether the specified name starts with a prefix, optionally trims a separator character following the prefix and returns the remainder. + /// + /// The name to strip. + /// The prefix to strip from . + /// Additional separator that may follow which should also be trimmed away. + /// + /// if it does not start with ; + /// otherwise, + /// the remainder of after stripping and all immediate following occurences of from it beginning. + /// + private static string PrefixAndStrip(string name, string prefix, char trimChar = '\0') { - if (name.StartsWith(_config.MethodPrefixToStrip, StringComparison.Ordinal)) + var nameSpan = name.AsSpan(); + if (nameSpan.StartsWith(prefix, StringComparison.Ordinal)) { - name = name[_config.MethodPrefixToStrip.Length..]; + nameSpan = nameSpan[prefix.Length..]; + nameSpan = nameSpan.TrimStart(trimChar); + return nameSpan.ToString(); + } + return name; } + private string PrefixAndStripMethodName(string name, uint overloadIndex) + { + name = PrefixAndStrip(name, _config.MethodPrefixToStrip); return $"_{name}{((overloadIndex != 0) ? overloadIndex.ToString(CultureInfo.InvariantCulture) : "")}"; } From c246b4161de63a4dbb49b948391700245c8768b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20H=C3=B8is=C3=A6ther=20Rasch?= Date: Sun, 19 May 2024 15:21:05 +0200 Subject: [PATCH 6/6] Automatic Whitespace fixup made by Visual Studio --- .../PInvokeGenerator.cs | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs index 76ee2b96..c6cbcc7c 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -1454,18 +1454,18 @@ static void GenerateTransparentStructs(PInvokeGenerator generator, Stream? strea return type.Contains('*', StringComparison.Ordinal) ? (8, 4, +1) : type switch { - "sbyte" => (1, 1, -1), - "byte" => (1, 1, +1), - "short" => (2, 2, -1), - "ushort" => (2, 2, +1), - "int" => (4, 4, -1), - "uint" => (4, 4, +1), - "nint" => (8, 4, -1), - "nuint" => (8, 4, +1), - "long" => (8, 8, -1), - "ulong" => (8, 8, +1), - _ => (0, 0, 0), - }; + "sbyte" => (1, 1, -1), + "byte" => (1, 1, +1), + "short" => (2, 2, -1), + "ushort" => (2, 2, +1), + "int" => (4, 4, -1), + "uint" => (4, 4, +1), + "nint" => (8, 4, -1), + "nuint" => (8, 4, +1), + "long" => (8, 8, -1), + "ulong" => (8, 8, +1), + _ => (0, 0, 0), + }; } static void OutputConversions(StreamWriter sw, string indentString, string name, string type, PInvokeGeneratorTransparentStructKind kind, string target) @@ -5403,7 +5403,7 @@ private static bool IsTransparentStructBoolean(PInvokeGeneratorTransparentStruct => kind is PInvokeGeneratorTransparentStructKind.Boolean; private static bool IsTransparentStructHandle(PInvokeGeneratorTransparentStructKind kind) - => kind is PInvokeGeneratorTransparentStructKind.Handle + => kind is PInvokeGeneratorTransparentStructKind.Handle or PInvokeGeneratorTransparentStructKind.HandleWin32; private static bool IsTransparentStructHexBased(PInvokeGeneratorTransparentStructKind kind) @@ -6149,7 +6149,7 @@ private bool NeedsReturnFixup(CXXMethodDecl cxxMethodDecl) private static bool NeedsNewKeyword(string name) { - return name.Equals("Equals",StringComparison.Ordinal) + return name.Equals("Equals", StringComparison.Ordinal) || name.Equals("GetHashCode", StringComparison.Ordinal) || name.Equals("GetType", StringComparison.Ordinal) || name.Equals("MemberwiseClone", StringComparison.Ordinal) @@ -6215,7 +6215,7 @@ private static string PrefixAndStrip(string name, string prefix, char trimChar = return nameSpan.ToString(); } return name; - } + } private string PrefixAndStripMethodName(string name, uint overloadIndex) {