Skip to content

Adding basic support for stripping text out of NativeTypeName attributes #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Xml.Linq;

namespace ClangSharp.CSharp;

Expand Down Expand Up @@ -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;
Expand Down
16 changes: 13 additions & 3 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public sealed class PInvokeGeneratorConfiguration
private readonly SortedSet<string> _excludedNames;
private readonly SortedSet<string> _forceRemappedNames;
private readonly SortedSet<string> _includedNames;
private readonly SortedSet<string> _nativeTypeNamesToStrip;
private readonly SortedSet<string> _withManualImports;
private readonly SortedSet<string> _traversalNames;
private readonly SortedSet<string> _withSetLastErrors;
Expand Down Expand Up @@ -76,6 +77,7 @@ public PInvokeGeneratorConfiguration(string defaultNamespace, string outputLocat
_excludedNames = new SortedSet<string>();
_forceRemappedNames = new SortedSet<string>();
_includedNames = new SortedSet<string>();
_nativeTypeNamesToStrip = new SortedSet<string>();
_withManualImports = new SortedSet<string>();
_traversalNames = new SortedSet<string>();
_withSetLastErrors = new SortedSet<string>();
Expand Down Expand Up @@ -279,6 +281,20 @@ public string MethodPrefixToStrip
}
}

[AllowNull]
public IReadOnlyCollection<string> NativeTypeNamesToStrip
{
get
{
return _nativeTypeNamesToStrip;
}

init
{
AddRange(_nativeTypeNamesToStrip, value, StringExtensions.NormalizePath);
}
}

public string DefaultNamespace => _defaultNamespace;

public PInvokeGeneratorOutputMode OutputMode => _outputMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml.Linq;
using ClangSharp.Abstractions;
using ClangSharp.CSharp;

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

if (!string.IsNullOrWhiteSpace(desc.NativeTypeName))
{
_ = _sb.Append(" native=\"");
_ = _sb.Append(EscapeText(desc.NativeTypeName));
_ = _sb.Append('"');
}
AddNativeTypeNameAttribute(desc.NativeTypeName);
}

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

_ = _sb.Append("<type");
if (!string.IsNullOrWhiteSpace(desc.NativeTypeName))
{
_ = _sb.Append(" native=\"");
_ = _sb.Append(EscapeText(desc.NativeTypeName));
_ = _sb.Append('"');
}

AddNativeTypeNameAttribute(desc.NativeTypeName);

_ = _sb.Append('>');
_ = _sb.Append(EscapeText(desc.ReturnType));
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -418,4 +406,26 @@ public void EndIndexerParameters()
public void BeginDereference() => _sb.Append("<deref>");

public void EndDereference() => _sb.Append("</deref>");

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('"');
}
}
16 changes: 16 additions & 0 deletions sources/ClangSharpPInvokeGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class Program
private static readonly Option<string> s_libraryPath;
private static readonly Option<string> s_methodClassName;
private static readonly Option<string> s_methodPrefixToStrip;
private static readonly Option<string[]> s_nativeTypeNamesToStrip;
private static readonly Option<string> s_namespaceName;
private static readonly Option<string> s_outputLocation;
private static readonly Option<PInvokeGeneratorOutputMode> s_outputMode;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -178,6 +180,7 @@ static Program()
s_outputMode,
s_outputLocation,
s_methodPrefixToStrip,
s_nativeTypeNamesToStrip,
s_remappedNameValuePairs,
s_std,
s_testOutputLocation,
Expand Down Expand Up @@ -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<string>();
var namespaceName = context.ParseResult.GetValueForOption(s_namespaceName) ?? "";
var outputLocation = context.ParseResult.GetValueForOption(s_outputLocation) ?? "";
var outputMode = context.ParseResult.GetValueForOption(s_outputMode);
Expand Down Expand Up @@ -660,6 +664,7 @@ public static void Run(InvocationContext context)
IncludedNames = includedNames,
LibraryPath = libraryPath,
MethodPrefixToStrip = methodPrefixToStrip,
NativeTypeNamesToStrip = nativeTypeNamesToStrip,
RemappedNames = remappedNames,
TraversalNames = traversalNames,
TestOutputLocation = testOutputLocation,
Expand Down Expand Up @@ -1053,6 +1058,17 @@ private static Option<string> GetNamespaceOption()
);
}

private static Option<string[]> GetNativeTypeNamesStripOption()
{
return new Option<string[]>(
aliases: new string[] { "--nativeTypeNamesToStrip" },
description: "The contents to strip from the generated NativeTypeName attributes.",
getDefaultValue: Array.Empty<string>
) {
AllowMultipleArgumentsPerToken = true
};
}

private static Option<PInvokeGeneratorOutputMode> GetOutputModeOption()
{
return new Option<PInvokeGeneratorOutputMode>(
Expand Down