Skip to content

Commit

Permalink
Add support for SupportedOSPlatformAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
tannergooding committed Nov 7, 2021
1 parent 04e3df8 commit 1dae106
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public void WriteCustomAttribute(string attribute, Action callback = null)
{
AddUsingDirective("System.Runtime.InteropServices");
}
else if (attribute.StartsWith("SupportedOSPlatform("))
{
AddUsingDirective("System.Runtime.Versioning");
}

if (!_customAttrIsForParameter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,6 @@ public void BeginStruct<TCustomAttrGeneratorData>(in StructDesc<TCustomAttrGener

public void BeginMarkerInterface(string[] baseTypeNames)
{
NeedsNewline = true;
WriteIndented("public interface Interface");

if (baseTypeNames is not null)
Expand All @@ -672,7 +671,6 @@ public void BeginMarkerInterface(string[] baseTypeNames)

public void BeginExplicitVtbl()
{
NeedsNewline = true;
WriteIndentedLine("public partial struct Vtbl");
WriteBlockStart();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,10 @@ private void VisitRecordDecl(RecordDecl recordDecl)
_testOutputBuilder.WriteIndented("/// <summary>Provides validation of the <see cref=\"");
_testOutputBuilder.Write(escapedName);
_testOutputBuilder.WriteLine("\" /> struct.</summary>");

WithAttributes("*", onlySupportedOSPlatform: true, isTestOutput: true);
WithAttributes(name, onlySupportedOSPlatform: true, isTestOutput: true);

_testOutputBuilder.WriteIndented("public static unsafe partial class ");
_testOutputBuilder.Write(escapedName);
_testOutputBuilder.WriteLine("Tests");
Expand Down Expand Up @@ -1434,13 +1438,23 @@ private void VisitRecordDecl(RecordDecl recordDecl)

if (_config.GenerateMarkerInterfaces)
{
if (_outputBuilder is CSharp.CSharpOutputBuilder csharpOutputBuilder)
{
csharpOutputBuilder.NeedsNewline = true;
}

_outputBuilder.BeginMarkerInterface(baseTypeNames);
OutputMarkerInterfaces(cxxRecordDecl, cxxRecordDecl);
_outputBuilder.EndMarkerInterface();
}

if (_config.GenerateExplicitVtbls || _config.GenerateTrimmableVtbls)
{
if (_outputBuilder is CSharp.CSharpOutputBuilder csharpOutputBuilder)
{
csharpOutputBuilder.NeedsNewline = true;
}

_outputBuilder.BeginExplicitVtbl();
OutputVtblEntries(cxxRecordDecl, cxxRecordDecl);
_outputBuilder.EndExplicitVtbl();
Expand Down Expand Up @@ -1581,7 +1595,7 @@ void OutputMarkerInterface(CXXRecordDecl cxxRecordDecl, CXXMethodDecl cxxMethodD
return;
}

if (_config.GenerateTrimmableVtbls && cxxMethodDecl.Parameters.Any((parmVarDecl) => parmVarDecl.Type.CanonicalType.Kind == CXTypeKind.CXType_FunctionProto))
if (_config.GenerateTrimmableVtbls && cxxMethodDecl.Parameters.Any((parmVarDecl) => (parmVarDecl.Type.CanonicalType is PointerType pointerType) && (pointerType.PointeeType is FunctionType)))
{
// This breaks trimming right now
return;
Expand Down
8 changes: 5 additions & 3 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5196,13 +5196,15 @@ private void Visit(IEnumerable<Cursor> cursors)

private void Visit(IEnumerable<Cursor> cursors, IEnumerable<Cursor> excludedCursors) => Visit(cursors.Except(excludedCursors));

private void WithAttributes(string remappedName)
private void WithAttributes(string remappedName, bool onlySupportedOSPlatform = false, bool isTestOutput = false)
{
var outputBuilder = isTestOutput ? _testOutputBuilder : _outputBuilder;

if (_config.WithAttributes.TryGetValue(remappedName, out var attributes))
{
foreach (var attribute in attributes)
foreach (var attribute in attributes.Where((a) => !onlySupportedOSPlatform || a.StartsWith("SupportedOSPlatform(")))
{
_outputBuilder.WriteCustomAttribute(attribute);
outputBuilder.WriteCustomAttribute(attribute);
}
}
}
Expand Down

0 comments on commit 1dae106

Please sign in to comment.