diff --git a/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.cs b/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.cs index b888478c..886dac19 100644 --- a/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.cs +++ b/sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Text; +using System.Xml.Linq; namespace ClangSharp.CSharp; @@ -313,6 +314,11 @@ private void AddVtblIndexAttribute(long vtblIndex, string? prefix = null, string private void AddNativeTypeNameAttribute(string nativeTypeName, string? prefix = null, string? postfix = null, string? attributePrefix = null) { + foreach (var entry in _config.NativeTypeNamesToStrip) + { + nativeTypeName = nativeTypeName.Replace(entry, ""); + } + if (string.IsNullOrWhiteSpace(nativeTypeName)) { return; diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index 8fa6608b..e2c75b5c 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -1525,10 +1525,20 @@ private void VisitRecordDecl(RecordDecl recordDecl) _ = withUsings.Add("System.Runtime.InteropServices"); } - if (desc.NativeType is not null) + var nativeTypeName = desc.NativeType; + + if (nativeTypeName is not null) { - withAttributes.Add($"NativeTypeName(\"{EscapeString(desc.NativeType)}\")"); - _ = withUsings.Add(GetNamespace("NativeTypeNameAttribute")); + foreach (var entry in _config.NativeTypeNamesToStrip) + { + nativeTypeName = nativeTypeName.Replace(entry, ""); + } + + if (!string.IsNullOrWhiteSpace(nativeTypeName)) + { + withAttributes.Add($"NativeTypeName(\"{EscapeString(nativeTypeName)}\")"); + _ = withUsings.Add(GetNamespace("NativeTypeNameAttribute")); + } } if (_config.GenerateNativeInheritanceAttribute && (desc.NativeInheritance is not null)) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs index bf937dcc..6d7fc4c7 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs @@ -28,6 +28,7 @@ public sealed class PInvokeGeneratorConfiguration private readonly SortedSet _excludedNames; private readonly SortedSet _forceRemappedNames; private readonly SortedSet _includedNames; + private readonly SortedSet _nativeTypeNamesToStrip; private readonly SortedSet _withManualImports; private readonly SortedSet _traversalNames; private readonly SortedSet _withSetLastErrors; @@ -76,6 +77,7 @@ public PInvokeGeneratorConfiguration(string defaultNamespace, string outputLocat _excludedNames = new SortedSet(); _forceRemappedNames = new SortedSet(); _includedNames = new SortedSet(); + _nativeTypeNamesToStrip = new SortedSet(); _withManualImports = new SortedSet(); _traversalNames = new SortedSet(); _withSetLastErrors = new SortedSet(); @@ -279,6 +281,20 @@ public string MethodPrefixToStrip } } + [AllowNull] + public IReadOnlyCollection NativeTypeNamesToStrip + { + get + { + return _nativeTypeNamesToStrip; + } + + init + { + AddRange(_nativeTypeNamesToStrip, value, StringExtensions.NormalizePath); + } + } + public string DefaultNamespace => _defaultNamespace; public PInvokeGeneratorOutputMode OutputMode => _outputMode; diff --git a/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs index b37ff1bb..2c321370 100644 --- a/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; +using System.Xml.Linq; using ClangSharp.Abstractions; using ClangSharp.CSharp; @@ -110,12 +111,7 @@ public void BeginField(in FieldDesc desc) desc.WriteCustomAttrs?.Invoke(desc.CustomAttrGeneratorData); _ = _sb.Append("'); _ = _sb.Append(EscapeText(desc.ReturnType)); @@ -265,12 +257,8 @@ public void BeginStruct(in StructDesc info) _ = _sb.Append("\" access=\""); _ = _sb.Append(info.AccessSpecifier.AsString()); _ = _sb.Append('"'); - if (info.NativeType is not null) - { - _ = _sb.Append(" native=\""); - _ = _sb.Append(info.NativeType); - _ = _sb.Append('"'); - } + + AddNativeTypeNameAttribute(info.NativeType); if (info.NativeInheritance is not null) { @@ -418,4 +406,26 @@ public void EndIndexerParameters() public void BeginDereference() => _sb.Append(""); public void EndDereference() => _sb.Append(""); + + private void AddNativeTypeNameAttribute(string? nativeTypeName) + { + if (nativeTypeName is null) + { + return; + } + + foreach (var entry in _config.NativeTypeNamesToStrip) + { + nativeTypeName = nativeTypeName.Replace(entry, ""); + } + + if (string.IsNullOrWhiteSpace(nativeTypeName)) + { + return; + } + + _ = _sb.Append(" native=\""); + _ = _sb.Append(EscapeText(nativeTypeName)); + _ = _sb.Append('"'); + } } diff --git a/sources/ClangSharpPInvokeGenerator/Program.cs b/sources/ClangSharpPInvokeGenerator/Program.cs index 87e500d3..f708bf16 100644 --- a/sources/ClangSharpPInvokeGenerator/Program.cs +++ b/sources/ClangSharpPInvokeGenerator/Program.cs @@ -36,6 +36,7 @@ public class Program private static readonly Option s_libraryPath; private static readonly Option s_methodClassName; private static readonly Option s_methodPrefixToStrip; + private static readonly Option s_nativeTypeNamesToStrip; private static readonly Option s_namespaceName; private static readonly Option s_outputLocation; private static readonly Option s_outputMode; @@ -141,6 +142,7 @@ static Program() s_outputMode = GetOutputModeOption(); s_outputLocation = GetOutputOption(); s_methodPrefixToStrip = GetPrefixStripOption(); + s_nativeTypeNamesToStrip = GetNativeTypeNamesStripOption(); s_remappedNameValuePairs = GetRemapOption(); s_std = GetStdOption(); s_testOutputLocation = GetTestOutputOption(); @@ -178,6 +180,7 @@ static Program() s_outputMode, s_outputLocation, s_methodPrefixToStrip, + s_nativeTypeNamesToStrip, s_remappedNameValuePairs, s_std, s_testOutputLocation, @@ -231,6 +234,7 @@ public static void Run(InvocationContext context) var libraryPath = context.ParseResult.GetValueForOption(s_libraryPath) ?? ""; var methodClassName = context.ParseResult.GetValueForOption(s_methodClassName) ?? ""; var methodPrefixToStrip = context.ParseResult.GetValueForOption(s_methodPrefixToStrip) ?? ""; + var nativeTypeNamesToStrip = context.ParseResult.GetValueForOption(s_nativeTypeNamesToStrip) ?? Array.Empty(); var namespaceName = context.ParseResult.GetValueForOption(s_namespaceName) ?? ""; var outputLocation = context.ParseResult.GetValueForOption(s_outputLocation) ?? ""; var outputMode = context.ParseResult.GetValueForOption(s_outputMode); @@ -660,6 +664,7 @@ public static void Run(InvocationContext context) IncludedNames = includedNames, LibraryPath = libraryPath, MethodPrefixToStrip = methodPrefixToStrip, + NativeTypeNamesToStrip = nativeTypeNamesToStrip, RemappedNames = remappedNames, TraversalNames = traversalNames, TestOutputLocation = testOutputLocation, @@ -1053,6 +1058,17 @@ private static Option GetNamespaceOption() ); } + private static Option GetNativeTypeNamesStripOption() + { + return new Option( + aliases: new string[] { "--nativeTypeNamesToStrip" }, + description: "The contents to strip from the generated NativeTypeName attributes.", + getDefaultValue: Array.Empty + ) { + AllowMultipleArgumentsPerToken = true + }; + } + private static Option GetOutputModeOption() { return new Option(