Skip to content

Commit 8d81b80

Browse files
committed
Fix null assignments
-fixed some broken using from TerraFX files -Allowed BaseType to be specified for ComTypes -Added several files for override due to issue in generation
1 parent d28b57b commit 8d81b80

File tree

3 files changed

+74
-11
lines changed

3 files changed

+74
-11
lines changed

generator.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,21 @@
1616
"!eng/submodules/terrafx.interop.windows/generation/DirectX/d3dx12/d3dx12_state_object/generate.rsp"
1717
],
1818
"ManualOverrides": {
19+
"sources/Windows/um/httpserv/IHttpResponse.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Windows/um/httpserv/IHttpResponse.cs",
20+
"sources/Windows/um/httpserv/IHttpResponse2.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Windows/um/httpserv/IHttpResponse2.cs",
21+
"sources/Windows/um/httpserv/IHttpResponse3.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Windows/um/httpserv/IHttpResponse3.cs",
22+
"sources/Windows/um/httpserv/IHttpResponse4.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Windows/um/httpserv/IHttpResponse4.cs",
23+
"sources/Windows/um/httpserv/IHttpTraceContext.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Windows/um/httpserv/IHttpTraceContext.cs",
24+
"sources/Windows/um/httpserv/CHttpModule.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Windows/um/httpserv/CHttpModule.cs",
1925
"sources/Windows/um/icm/Windows.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Windows/um/icm/Windows.cs",
2026
"sources/Windows/um/WinUser/Windows.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Windows/um/WinUser/Windows.cs",
2127
"sources/Windows/shared/strsafe/Windows.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Windows/shared/strsafe/Windows.cs",
2228
"sources/Windows/shared/hidpi/Windows.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Windows/shared/hidpi/Windows.cs",
2329
"sources/Gdiplus/um/gdiplustypes/GpRect.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Gdiplus/um/gdiplustypes/GpRect.cs",
2430
"sources/Gdiplus/um/gdiplustypes/GpRectF.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Gdiplus/um/gdiplustypes/GpRectF.cs",
25-
"sources/Gdiplus/um/gdiplustypes/GpPathData.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Gdiplus/um/gdiplustypes/GpPathData.cs"
31+
"sources/Gdiplus/um/gdiplustypes/GpPathData.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Gdiplus/um/gdiplustypes/GpPathData.cs",
32+
"sources/Winrt/winrt/windows.security.authentication.web.core/IID.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Winrt/winrt/windows.security.authentication.web.core/IID.Manual.cs",
33+
"sources/Winrt/winrt/windows.ui.input/IID.gen.cs": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows/Winrt/winrt/windows.ui.input/IID.Manual.cs"
2634
},
2735
"InputSourceRoot": "eng/submodules/terrafx.interop.windows/sources/Interop/Windows",
2836
"InputTestRoot": "eng/submodules/terrafx.interop.windows/tests/Interop/Windows",
@@ -33,8 +41,11 @@
3341
},
3442
"ChangeNamespace": {
3543
"Mappings": {
36-
"TerraFX.Interop.(.*)": "Silk.NET.$1"
44+
"TerraFX.Interop(.*)": "Silk.NET$1"
3745
}
46+
},
47+
"TransformCOM": {
48+
"BaseType": "IUnknown.Interface"
3849
}
3950
},
4051
"SDL": {

sources/SilkTouch/SilkTouch/Mods/ChangeNamespace.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
@@ -199,5 +199,21 @@ FileScopedNamespaceDeclarationSyntax syntax
199199
null => null
200200
};
201201
}
202+
203+
public override SyntaxNode? VisitUsingDirective(UsingDirectiveSyntax node)
204+
{
205+
var oldNs = node.Name?.ToString() ?? string.Empty;
206+
var newNs = ModUtils.GroupedRegexReplace(_regexes, oldNs);
207+
if (oldNs != newNs && _allNamespaces.Contains(oldNs))
208+
{
209+
_usingsToAdd.Add(oldNs);
210+
}
211+
return base.VisitUsingDirective(node) switch {
212+
UsingDirectiveSyntax syntax
213+
=> syntax.WithName(ModUtils.NamespaceIntoIdentifierName(newNs)),
214+
{ } ret => ret,
215+
null => null
216+
};
217+
}
202218
}
203219
}

sources/SilkTouch/SilkTouch/Mods/TransformCOM.cs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,38 @@
1717
using System.Xml.Linq;
1818
using Microsoft.Extensions.Logging;
1919
using Silk.NET.SilkTouch.Clang;
20+
using Microsoft.Extensions.Options;
2021

2122
namespace Silk.NET.SilkTouch.Mods
2223
{
2324
/// <summary>
2425
/// A mod to modify COM interface structs into opaque structs that act like ComPtr objects
2526
/// </summary>
2627
/// <param name="logger">The logger to use.</param>
27-
[ModConfiguration<Config>]
28-
public class TransformCOM(ILogger<TransformCOM> logger) : Mod
28+
/// <param name="config">The configuration to use.</param>
29+
[ModConfiguration<Configuration>]
30+
public class TransformCOM(
31+
ILogger<TransformCOM> logger,
32+
IOptionsSnapshot<TransformCOM.Configuration> config) : Mod
2933
{
3034
/// <summary>
3135
/// The configuration for the <see cref="TransformCOM"/> mod.
3236
/// </summary>
33-
public class Config
37+
public class Configuration
3438
{
35-
39+
/// <summary>
40+
/// The base type to consider the base of the com tree
41+
/// Usually this is IUknown.Interface
42+
/// </summary>
43+
public string? BaseType { get; init; }
3644
}
3745

3846
/// <inheritdoc />
3947
public override async Task ExecuteAsync(IModContext ctx, CancellationToken ct = default)
4048
{
4149
await base.ExecuteAsync(ctx, ct);
4250

43-
var firstPass = new TypeDiscoverer();
51+
var firstPass = new TypeDiscoverer(config.Value.BaseType ?? "IUnknown.Interface");
4452
var proj = ctx.SourceProject;
4553
if (proj is null)
4654
{
@@ -101,7 +109,11 @@ public override async Task ExecuteAsync(IModContext ctx, CancellationToken ct =
101109
.OfType<MemberAccessExpressionSyntax>()
102110
.Where(m => m.Expression is PrefixUnaryExpressionSyntax pues && pues.IsKind(SyntaxKind.PointerMemberAccessExpression));
103111

104-
if (memberAccesses.Count() == 0)
112+
var nullAssignments = root.DescendantNodes()
113+
.OfType<AssignmentExpressionSyntax>()
114+
.Where(aes => aes.Right.IsKind(SyntaxKind.NullLiteralExpression));
115+
116+
if (memberAccesses.Count() == 0 && nullAssignments.Count() == 0)
105117
{
106118
continue;
107119
}
@@ -120,6 +132,16 @@ public override async Task ExecuteAsync(IModContext ctx, CancellationToken ct =
120132
}
121133
}
122134

135+
foreach (var nullAssignment in nullAssignments)
136+
{
137+
var typeInfo = semanticModel.GetTypeInfo(nullAssignment.Left);
138+
// Check if the type is a ComType
139+
if (typeInfo.Type != null && firstPass.FoundCOMTypes.Any(type => type.Item1 == typeInfo.Type.ToDisplayString()))
140+
{
141+
var newNullAssignment = SyntaxFactory.AssignmentExpression(nullAssignment.Kind(), nullAssignment.Left, LiteralExpression(SyntaxKind.DefaultExpression));
142+
editor.ReplaceNode(nullAssignment, newNullAssignment);
143+
}
144+
}
123145
proj = doc.Project;
124146

125147
logger.LogInformation("COM Object Usage Update for {0} Complete ({1}/{2})", doc.Name, index, count);
@@ -128,7 +150,7 @@ public override async Task ExecuteAsync(IModContext ctx, CancellationToken ct =
128150
ctx.SourceProject = proj;
129151
}
130152

131-
class TypeDiscoverer : CSharpSyntaxWalker
153+
class TypeDiscoverer(string BaseType) : CSharpSyntaxWalker
132154
{
133155
private Dictionary<string, List<(string, bool)>> _interfaceParenting = new Dictionary<string, List<(string, bool)>>();
134156

@@ -179,7 +201,7 @@ public override void VisitInterfaceDeclaration(InterfaceDeclarationSyntax node)
179201

180202
private void CheckBases((string, bool) className, BaseListSyntax bases)
181203
{
182-
if (bases.Types.Any(baseType => baseType.Type.ToString() == "IUnknown.Interface" || FoundCOMTypes.Any(val => val.Item1 == $"{baseType.Type}")))
204+
if (bases.Types.Any(baseType => baseType.Type.ToString() == BaseType || FoundCOMTypes.Any(val => val.Item1 == $"{baseType.Type}")))
183205
{
184206
COMTypeValidated(className);
185207
return;
@@ -303,6 +325,20 @@ class Rewriter(List<(string, bool)> ComTypes)
303325

304326
return base.VisitCastExpression(node);
305327
}
328+
329+
public override SyntaxNode? VisitParameter(ParameterSyntax node)
330+
{
331+
var visited = base.VisitParameter(node);
332+
var visitedParameter = visited as ParameterSyntax;
333+
if (visitedParameter is null || visitedParameter.Default is null || visitedParameter.Type is null ||
334+
visitedParameter.Default.Value.IsKind(SyntaxKind.NullLiteralExpression) ||
335+
!ComTypes.Any(com => visitedParameter.Type?.ToString() == com.Item1))
336+
{
337+
return visited;
338+
}
339+
340+
return Parameter(visitedParameter.AttributeLists, visitedParameter.Modifiers, visitedParameter.Type, visitedParameter.Identifier, EqualsValueClause(LiteralExpression(SyntaxKind.DefaultExpression)));
341+
}
306342
}
307343
}
308344
}

0 commit comments

Comments
 (0)