Skip to content

Commit 7a7aea2

Browse files
Merge pull request #388 from tannergooding/main
Adding basic support for stripping text out of NativeTypeName attributes
2 parents 84f07ed + fdde93c commit 7a7aea2

File tree

5 files changed

+79
-21
lines changed

5 files changed

+79
-21
lines changed

sources/ClangSharp.PInvokeGenerator/CSharp/CSharpOutputBuilder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Diagnostics;
55
using System.Text;
6+
using System.Xml.Linq;
67

78
namespace ClangSharp.CSharp;
89

@@ -313,6 +314,11 @@ private void AddVtblIndexAttribute(long vtblIndex, string? prefix = null, string
313314

314315
private void AddNativeTypeNameAttribute(string nativeTypeName, string? prefix = null, string? postfix = null, string? attributePrefix = null)
315316
{
317+
foreach (var entry in _config.NativeTypeNamesToStrip)
318+
{
319+
nativeTypeName = nativeTypeName.Replace(entry, "");
320+
}
321+
316322
if (string.IsNullOrWhiteSpace(nativeTypeName))
317323
{
318324
return;

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,10 +1525,20 @@ private void VisitRecordDecl(RecordDecl recordDecl)
15251525
_ = withUsings.Add("System.Runtime.InteropServices");
15261526
}
15271527

1528-
if (desc.NativeType is not null)
1528+
var nativeTypeName = desc.NativeType;
1529+
1530+
if (nativeTypeName is not null)
15291531
{
1530-
withAttributes.Add($"NativeTypeName(\"{EscapeString(desc.NativeType)}\")");
1531-
_ = withUsings.Add(GetNamespace("NativeTypeNameAttribute"));
1532+
foreach (var entry in _config.NativeTypeNamesToStrip)
1533+
{
1534+
nativeTypeName = nativeTypeName.Replace(entry, "");
1535+
}
1536+
1537+
if (!string.IsNullOrWhiteSpace(nativeTypeName))
1538+
{
1539+
withAttributes.Add($"NativeTypeName(\"{EscapeString(nativeTypeName)}\")");
1540+
_ = withUsings.Add(GetNamespace("NativeTypeNameAttribute"));
1541+
}
15321542
}
15331543

15341544
if (_config.GenerateNativeInheritanceAttribute && (desc.NativeInheritance is not null))

sources/ClangSharp.PInvokeGenerator/PInvokeGeneratorConfiguration.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public sealed class PInvokeGeneratorConfiguration
2828
private readonly SortedSet<string> _excludedNames;
2929
private readonly SortedSet<string> _forceRemappedNames;
3030
private readonly SortedSet<string> _includedNames;
31+
private readonly SortedSet<string> _nativeTypeNamesToStrip;
3132
private readonly SortedSet<string> _withManualImports;
3233
private readonly SortedSet<string> _traversalNames;
3334
private readonly SortedSet<string> _withSetLastErrors;
@@ -76,6 +77,7 @@ public PInvokeGeneratorConfiguration(string defaultNamespace, string outputLocat
7677
_excludedNames = new SortedSet<string>();
7778
_forceRemappedNames = new SortedSet<string>();
7879
_includedNames = new SortedSet<string>();
80+
_nativeTypeNamesToStrip = new SortedSet<string>();
7981
_withManualImports = new SortedSet<string>();
8082
_traversalNames = new SortedSet<string>();
8183
_withSetLastErrors = new SortedSet<string>();
@@ -279,6 +281,20 @@ public string MethodPrefixToStrip
279281
}
280282
}
281283

284+
[AllowNull]
285+
public IReadOnlyCollection<string> NativeTypeNamesToStrip
286+
{
287+
get
288+
{
289+
return _nativeTypeNamesToStrip;
290+
}
291+
292+
init
293+
{
294+
AddRange(_nativeTypeNamesToStrip, value, StringExtensions.NormalizePath);
295+
}
296+
}
297+
282298
public string DefaultNamespace => _defaultNamespace;
283299

284300
public PInvokeGeneratorOutputMode OutputMode => _outputMode;

sources/ClangSharp.PInvokeGenerator/XML/XmlOutputBuilder.VisitDecl.cs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.Runtime.InteropServices;
55
using System.Text;
6+
using System.Xml.Linq;
67
using ClangSharp.Abstractions;
78
using ClangSharp.CSharp;
89

@@ -110,12 +111,7 @@ public void BeginField(in FieldDesc desc)
110111
desc.WriteCustomAttrs?.Invoke(desc.CustomAttrGeneratorData);
111112
_ = _sb.Append("<type");
112113

113-
if (!string.IsNullOrWhiteSpace(desc.NativeTypeName))
114-
{
115-
_ = _sb.Append(" native=\"");
116-
_ = _sb.Append(EscapeText(desc.NativeTypeName));
117-
_ = _sb.Append('"');
118-
}
114+
AddNativeTypeNameAttribute(desc.NativeTypeName);
119115
}
120116

121117
public void WriteFixedCountField(string typeName, string escapedName, string fixedName, string count)
@@ -182,12 +178,8 @@ public void BeginFunctionOrDelegate(in FunctionOrDelegateDesc desc, ref bool isM
182178
desc.WriteCustomAttrs?.Invoke(desc.CustomAttrGeneratorData);
183179

184180
_ = _sb.Append("<type");
185-
if (!string.IsNullOrWhiteSpace(desc.NativeTypeName))
186-
{
187-
_ = _sb.Append(" native=\"");
188-
_ = _sb.Append(EscapeText(desc.NativeTypeName));
189-
_ = _sb.Append('"');
190-
}
181+
182+
AddNativeTypeNameAttribute(desc.NativeTypeName);
191183

192184
_ = _sb.Append('>');
193185
_ = _sb.Append(EscapeText(desc.ReturnType));
@@ -265,12 +257,8 @@ public void BeginStruct(in StructDesc info)
265257
_ = _sb.Append("\" access=\"");
266258
_ = _sb.Append(info.AccessSpecifier.AsString());
267259
_ = _sb.Append('"');
268-
if (info.NativeType is not null)
269-
{
270-
_ = _sb.Append(" native=\"");
271-
_ = _sb.Append(info.NativeType);
272-
_ = _sb.Append('"');
273-
}
260+
261+
AddNativeTypeNameAttribute(info.NativeType);
274262

275263
if (info.NativeInheritance is not null)
276264
{
@@ -418,4 +406,26 @@ public void EndIndexerParameters()
418406
public void BeginDereference() => _sb.Append("<deref>");
419407

420408
public void EndDereference() => _sb.Append("</deref>");
409+
410+
private void AddNativeTypeNameAttribute(string? nativeTypeName)
411+
{
412+
if (nativeTypeName is null)
413+
{
414+
return;
415+
}
416+
417+
foreach (var entry in _config.NativeTypeNamesToStrip)
418+
{
419+
nativeTypeName = nativeTypeName.Replace(entry, "");
420+
}
421+
422+
if (string.IsNullOrWhiteSpace(nativeTypeName))
423+
{
424+
return;
425+
}
426+
427+
_ = _sb.Append(" native=\"");
428+
_ = _sb.Append(EscapeText(nativeTypeName));
429+
_ = _sb.Append('"');
430+
}
421431
}

sources/ClangSharpPInvokeGenerator/Program.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class Program
3636
private static readonly Option<string> s_libraryPath;
3737
private static readonly Option<string> s_methodClassName;
3838
private static readonly Option<string> s_methodPrefixToStrip;
39+
private static readonly Option<string[]> s_nativeTypeNamesToStrip;
3940
private static readonly Option<string> s_namespaceName;
4041
private static readonly Option<string> s_outputLocation;
4142
private static readonly Option<PInvokeGeneratorOutputMode> s_outputMode;
@@ -141,6 +142,7 @@ static Program()
141142
s_outputMode = GetOutputModeOption();
142143
s_outputLocation = GetOutputOption();
143144
s_methodPrefixToStrip = GetPrefixStripOption();
145+
s_nativeTypeNamesToStrip = GetNativeTypeNamesStripOption();
144146
s_remappedNameValuePairs = GetRemapOption();
145147
s_std = GetStdOption();
146148
s_testOutputLocation = GetTestOutputOption();
@@ -178,6 +180,7 @@ static Program()
178180
s_outputMode,
179181
s_outputLocation,
180182
s_methodPrefixToStrip,
183+
s_nativeTypeNamesToStrip,
181184
s_remappedNameValuePairs,
182185
s_std,
183186
s_testOutputLocation,
@@ -231,6 +234,7 @@ public static void Run(InvocationContext context)
231234
var libraryPath = context.ParseResult.GetValueForOption(s_libraryPath) ?? "";
232235
var methodClassName = context.ParseResult.GetValueForOption(s_methodClassName) ?? "";
233236
var methodPrefixToStrip = context.ParseResult.GetValueForOption(s_methodPrefixToStrip) ?? "";
237+
var nativeTypeNamesToStrip = context.ParseResult.GetValueForOption(s_nativeTypeNamesToStrip) ?? Array.Empty<string>();
234238
var namespaceName = context.ParseResult.GetValueForOption(s_namespaceName) ?? "";
235239
var outputLocation = context.ParseResult.GetValueForOption(s_outputLocation) ?? "";
236240
var outputMode = context.ParseResult.GetValueForOption(s_outputMode);
@@ -660,6 +664,7 @@ public static void Run(InvocationContext context)
660664
IncludedNames = includedNames,
661665
LibraryPath = libraryPath,
662666
MethodPrefixToStrip = methodPrefixToStrip,
667+
NativeTypeNamesToStrip = nativeTypeNamesToStrip,
663668
RemappedNames = remappedNames,
664669
TraversalNames = traversalNames,
665670
TestOutputLocation = testOutputLocation,
@@ -1053,6 +1058,17 @@ private static Option<string> GetNamespaceOption()
10531058
);
10541059
}
10551060

1061+
private static Option<string[]> GetNativeTypeNamesStripOption()
1062+
{
1063+
return new Option<string[]>(
1064+
aliases: new string[] { "--nativeTypeNamesToStrip" },
1065+
description: "The contents to strip from the generated NativeTypeName attributes.",
1066+
getDefaultValue: Array.Empty<string>
1067+
) {
1068+
AllowMultipleArgumentsPerToken = true
1069+
};
1070+
}
1071+
10561072
private static Option<PInvokeGeneratorOutputMode> GetOutputModeOption()
10571073
{
10581074
return new Option<PInvokeGeneratorOutputMode>(

0 commit comments

Comments
 (0)