diff --git a/Directory.Build.props b/Directory.Build.props index 7b102047..148ea050 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -47,7 +47,7 @@ 17.0.0 ClangSharp ClangSharp - 18.1.0 + 18.1.0.1 rc1 pr diff --git a/Directory.Packages.props b/Directory.Packages.props index 7430ca20..6704befd 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -17,7 +17,7 @@ - + diff --git a/README.md b/README.md index 8d8df0c9..795e325a 100644 --- a/README.md +++ b/README.md @@ -155,32 +155,33 @@ Options: -e, --exclude A declaration name to exclude from binding generation. [] -f, --file A file to parse and generate bindings for. [] -F, --file-directory The base path for files to parse. [] - -h, --headerFile A file which contains the header to prefix every generated file with. [] + -hf, --headerFile A file which contains the header to prefix every generated file with. [] -i, --include A declaration name to include in binding generation. [] -I, --include-directory Add directory to include search path. [] - -x, --language Treat subsequent input files as having type . [default: c++] + -x, --language Treat subsequent input files as having type . [default: c++] -l, --libraryPath The string to use in the DllImport attribute used when generating bindings. [] -m, --methodClassName The name of the static class that will contain the generated method bindings. [default: Methods] -n, --namespace The namespace in which to place the generated bindings. [] - --nativeTypeNamesToStrip The contents to strip from the generated NativeTypeName attributes. -om, --output-mode The mode describing how the information collected from the headers are presented in the resultant bindings. [default: CSharp] - -o, --output The output location to write the generated bindings to. [] + -o, --output The output location to write the generated bindings to. [] -p, --prefixStrip The prefix to strip from the generated method bindings. [] + --nativeTypeNamesToStrip The contents to strip from the generated NativeTypeName attributes. [] -r, --remap A declaration name to be remapped to another name during binding generation. [] - -std, --std Language standard to compile for. [] + -std Language standard to compile for. [] -to, --test-output The output location to write the generated tests to. [] -t, --traverse A file name included either directly or indirectly by -f that should be traversed during binding generation. [] - -v, --version Prints the current version information for the tool and its native dependencies. + -v, --version Prints the current version information for the tool and its native dependencies. -was, --with-access-specifier An access specifier to be used with the given qualified or remapped declaration name during binding generation. Supports wildcards. [] -wa, --with-attribute An attribute to be added to the given remapped declaration name during binding generation. Supports wildcards. [] -wcc, --with-callconv A calling convention to be used for the given declaration during binding generation. Supports wildcards. [] -wc, --with-class A class to be used for the given remapped constant or function declaration name during binding generation. Supports wildcards. [] -wg, --with-guid A GUID to be used for the given declaration during binding generation. Supports wildcards. [] + -wl, --with-length A length to be used for the given declaration during binding generation. Supports wildcards. [] -wlb, --with-librarypath A library path to be used for the given declaration during binding generation. Supports wildcards. [] -wmi, --with-manual-import A remapped function name to be treated as a manual import during binding generation. Supports wildcards. [] -wn, --with-namespace A namespace to be used for the given remapped declaration name during binding generation. Supports wildcards. [] -wp, --with-packing Overrides the StructLayoutAttribute.Pack property for the given type. Supports wildcards. [] - -wsle, --with-setlasterror Add the SetLastError=true modifier to a given DllImport or UnmanagedFunctionPointer. Supports wildcards. [] + -wsle, --with-setlasterror Add the SetLastError=true modifier or SetsSystemLastError attribute to a given DllImport or UnmanagedFunctionPointer. Supports wildcards. [] -wsgct, --with-suppressgctransition Add the SuppressGCTransition calling convention to a given DllImport or UnmanagedFunctionPointer. Supports wildcards. [] -wts, --with-transparent-struct A remapped type name to be treated as a transparent wrapper during binding generation. Supports wildcards. [] -wt, --with-type A type to be used for the given enum declaration during binding generation. Supports wildcards. [] @@ -218,7 +219,7 @@ Options: # Exclusion Options exclude-anonymous-field-helpers The helper ref properties generated for fields in nested anonymous structs and unions should not be generated. - exclude-com-proxies Types recognized as COM proxies should not have bindings generated. Thes are currently function declarations ending with _UserFree, _UserMarshal, _UserSize, _UserUnmarshal, _Proxy, or _Stub. + exclude-com-proxies Types recognized as COM proxies should not have bindings generated. These are currently function declarations ending with _UserFree, _UserMarshal, _UserSize, _UserUnmarshal, _Proxy, or _Stub. exclude-default-remappings Default remappings for well known types should not be added. This currently includes intptr_t, ptrdiff_t, size_t, and uintptr_t exclude-empty-records Bindings for records that contain no members should not be generated. These are commonly encountered for opaque handle like types such as HWND. exclude-enum-operators Bindings for operators over enum types should not be generated. These are largely unnecessary in C# as the operators are available by default. @@ -243,19 +244,24 @@ Options: generate-callconv-member-function Instance function pointers should use [CallConvMemberFunction] where applicable. generate-cpp-attributes [CppAttributeList("")] should be generated to document the encountered C++ attributes. generate-disable-runtime-marshalling [assembly: DisableRuntimeMarshalling] should be generated. - generate-doc-includes <include> xml documentation tags should be generated for declarations. + generate-doc-includes xml documentation tags should be generated for declarations. generate-file-scoped-namespaces Namespaces should be scoped to the file to reduce nesting. generate-guid-member Types with an associated GUID should have a corresponding member generated. generate-helper-types Code files should be generated for various helper attributes and declared transparent structs. generate-macro-bindings Bindings for macro-definitions should be generated. This currently only works with value like macros and not function-like ones. generate-marker-interfaces Bindings for marker interfaces representing native inheritance hierarchies should be generated. - generate-native-bitfield-attribute [NativeBitfield(\"\", offset: #, length: #)] attribute should be generated to document the encountered bitfield layout. + generate-native-bitfield-attribute [NativeBitfield("", offset: #, length: #)] attribute should be generated to document the encountered bitfield layout. generate-native-inheritance-attribute [NativeInheritance("")] attribute should be generated to document the encountered C++ base type. + generate-generic-pointer-wrapper Pointer should be used for limited generic type support. generate-setslastsystemerror-attribute [SetsLastSystemError] attribute should be generated rather than using SetLastError = true. generate-template-bindings Bindings for template-definitions should be generated. This is currently experimental. 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..c538e421 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; @@ -272,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 = ""; @@ -294,6 +294,8 @@ private void VisitEnumConstantDecl(EnumConstantDecl enumConstantDecl) parentName = _outputBuilder.Name; } + var escapedName = EscapeAndStripEnumMemberName(name, parentName); + var kind = isAnonymousEnum ? ValueKind.Primitive : ValueKind.Enumerator; var flags = ValueFlags.Constant; @@ -535,12 +537,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; @@ -2024,7 +2026,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, @@ -2112,7 +2114,7 @@ void OutputVtblEntry(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethodDecl) } var remappedName = FixupNameForMultipleHits(cxxMethodDecl); - var escapedName = EscapeAndStripName(remappedName); + var escapedName = EscapeAndStripMethodName(remappedName); var desc = new FieldDesc { @@ -2181,7 +2183,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, @@ -2261,7 +2263,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(">("); } @@ -2270,7 +2272,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..d3b9bb84 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -435,6 +435,7 @@ public void Close() GenerateNativeBitfieldAttribute(this, stream, leaveStreamOpen); GenerateNativeInheritanceAttribute(this, stream, leaveStreamOpen); GenerateNativeTypeNameAttribute(this, stream, leaveStreamOpen); + GenerateNativeAnnotationAttribute(this, stream, leaveStreamOpen); GenerateSetsLastSystemErrorAttribute(this, stream, leaveStreamOpen); GenerateVtblIndexAttribute(this, stream, leaveStreamOpen); GenerateTransparentStructs(this, stream, leaveStreamOpen); @@ -795,6 +796,96 @@ static void GenerateNativeTypeNameAttribute(PInvokeGenerator generator, Stream? } } + static void GenerateNativeAnnotationAttribute(PInvokeGenerator generator, Stream? stream, bool leaveStreamOpen) + { + const string AttributeName = "NativeAnnotationAttribute"; + var config = generator.Config; + + var ns = generator.GetNamespace(AttributeName); + if (config.ExcludedNames.Contains(AttributeName) || config.ExcludedNames.Contains($"{ns}.{AttributeName}")) + { + return; + } + + if (stream is null) + { + var outputPath = Path.Combine(config.OutputLocation, $"{AttributeName}.cs"); + stream = generator._outputStreamFactory(outputPath); + } + + using var sw = new StreamWriter(stream, s_defaultStreamWriterEncoding, DefaultStreamWriterBufferSize, leaveStreamOpen); + sw.NewLine = "\n"; + + if (!string.IsNullOrEmpty(config.HeaderText)) + { + sw.WriteLine(config.HeaderText); + } + + var indentString = " "; + + sw.WriteLine("using System;"); + sw.WriteLine("using System.Diagnostics;"); + sw.WriteLine(); + + sw.Write("namespace "); + sw.Write(ns); + + if (generator.Config.GenerateFileScopedNamespaces) + { + sw.WriteLine(';'); + sw.WriteLine(); + indentString = ""; + } + else + { + sw.WriteLine(); + sw.WriteLine('{'); + } + + sw.Write(indentString); + sw.WriteLine("/// Defines the annotation found in a native declaration."); + sw.Write(indentString); + sw.WriteLine("[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]"); + sw.Write(indentString); + sw.WriteLine("[Conditional(\"DEBUG\")]"); + sw.Write(indentString); + sw.WriteLine($"internal sealed partial class {AttributeName} : Attribute"); + sw.Write(indentString); + sw.WriteLine('{'); + sw.Write(indentString); + sw.WriteLine(" private readonly string _annotation;"); + sw.WriteLine(); + sw.Write(indentString); + sw.WriteLine($" /// Initializes a new instance of the class."); + sw.Write(indentString); + sw.WriteLine(" /// The annotation that was used in the native declaration."); + sw.Write(indentString); + sw.WriteLine($" public {AttributeName}(string annotation)"); + sw.Write(indentString); + sw.WriteLine(" {"); + sw.Write(indentString); + sw.WriteLine(" _annotation = annotation;"); + sw.Write(indentString); + sw.WriteLine(" }"); + sw.WriteLine(); + sw.Write(indentString); + sw.WriteLine(" /// Gets the annotation that was used in the native declaration."); + sw.Write(indentString); + sw.WriteLine(" public string Annotation => _annotation;"); + sw.Write(indentString); + sw.WriteLine('}'); + + if (!generator.Config.GenerateFileScopedNamespaces) + { + sw.WriteLine('}'); + } + + if (!leaveStreamOpen) + { + stream = null; + } + } + static void GenerateSetsLastSystemErrorAttribute(PInvokeGenerator generator, Stream? stream, bool leaveStreamOpen) { var config = generator.Config; @@ -1454,18 +1545,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) @@ -2326,13 +2417,23 @@ private static string EscapeName(string name) } } - private string EscapeAndStripName(string name) + private string EscapeAndStripMethodName(string name) + { + name = PrefixAndStrip(name, _config.MethodPrefixToStrip); + return EscapeName(name); + } + + private string EscapeAndStripEnumMemberName(string name, string enumTypeName) { - if (name.StartsWith(_config.MethodPrefixToStrip, StringComparison.Ordinal)) + if (Config.StripEnumMemberTypeName) { - name = name[_config.MethodPrefixToStrip.Length..]; + var escapedName = PrefixAndStrip(name, enumTypeName, trimChar: '_'); + if (escapedName.Length > 0 && char.IsAsciiDigit(escapedName[0])) + { + escapedName = '_' + escapedName; + } + return escapedName; } - return EscapeName(name); } @@ -5407,7 +5508,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) @@ -6153,7 +6254,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) @@ -6198,13 +6299,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) : "")}"; } @@ -6695,6 +6815,13 @@ private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform = break; } + case CX_AttrKind_Annotate: + { + var annotationText = attr.Spelling; + outputBuilder.WriteCustomAttribute($"""NativeAnnotation("{annotationText}")"""); + break; + } + case CX_AttrKind_Format: case CX_AttrKind_FormatArg: case CX_AttrKind_MSNoVTable: 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..322ef1e4 100644 --- a/sources/ClangSharpPInvokeGenerator/Program.cs +++ b/sources/ClangSharpPInvokeGenerator/Program.cs @@ -28,7 +28,7 @@ public static class Program private static readonly string[] s_excludeOptionAliases = ["--exclude", "-e"]; private static readonly string[] s_fileOptionAliases = ["--file", "-f"]; private static readonly string[] s_fileDirectionOptionAliases = ["--file-directory", "-F"]; - private static readonly string[] s_headerOptionAliases = ["--headerFile", "-h"]; + private static readonly string[] s_headerOptionAliases = ["--headerFile", "-hf"]; private static readonly string[] s_includeOptionAliases = ["--include", "-i"]; private static readonly string[] s_includeDirectoryOptionAliases = ["--include-directory", "-I"]; private static readonly string[] s_languageOptionAliases = ["--language", "-x"]; @@ -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":