Skip to content

Commit ef339d9

Browse files
authored
Merge pull request #9 from monkey0506/remove-diagnostic-ngd1002
Remove diagnostic NGD1002
2 parents 787191d + b80e503 commit ef339d9

File tree

4 files changed

+59
-49
lines changed

4 files changed

+59
-49
lines changed

Constants.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ internal static class Constants
4242
public const string SourceFileName = RootNamespace + ".g.cs";
4343

4444
public const string MarshalAsArgumentMustUseObjectCreationSyntaxID = "NGD1001";
45-
public const string InvalidMarshalParamsAsArrayLengthID = "NGD1002";
4645
public static readonly DiagnosticDescriptor MarshalAsArgumentMustUseObjectCreationSyntaxDescriptor =
4746
new(MarshalAsArgumentMustUseObjectCreationSyntaxID, "Invalid MarshalAs argument", "MarshalAs argument must be null or use object creation syntax", "Usage", DiagnosticSeverity.Error, true);
48-
public static readonly DiagnosticDescriptor InvalidMarshalParamsAsArrayLengthDescriptor =
49-
new(InvalidMarshalParamsAsArrayLengthID, $"Invalid marshalParamsAs argument", $"marshalParamsAs argument must be array of correct length", "Usage", DiagnosticSeverity.Error, true);
5047

5148
public static readonly string[] GenericActionTypeParameters = new[]
5249
{

MarshalInfo.cs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,23 @@ internal static class MarshalInfo
2525
List<string?> marshalAsParamsStrings = new();
2626
if (collection is IArrayCreationOperation arrayCreation)
2727
{
28-
var arrayLength = arrayCreation.DimensionSizes[0].ConstantValue;
29-
if (!arrayLength.HasValue || ((int)arrayLength.Value!) != argumentCount)
30-
{
31-
diagnostics.Add(Diagnostic.Create(Constants.InvalidMarshalParamsAsArrayLengthDescriptor, location));
32-
}
33-
else if (arrayCreation.Initializer is not null)
28+
if (arrayCreation.Initializer is not null && (argumentCount > 0))
3429
{
3530
foreach (var elementValue in arrayCreation.Initializer.ElementValues)
3631
{
3732
cancellationToken.ThrowIfCancellationRequested();
3833
GetMarshalAsFromOperation(elementValue, cancellationToken, argumentCount, diagnostics, location, marshalAsParamsStrings);
34+
if (marshalAsParamsStrings.Count == argumentCount)
35+
{
36+
break;
37+
}
38+
}
39+
for (int count = marshalAsParamsStrings.Count; count < argumentCount; ++count)
40+
{
41+
marshalAsParamsStrings.Add(null);
3942
}
4043
}
41-
// else (no initializer), default to no marshaling
44+
// else (no initializer or no arguments), default to no marshaling
4245
}
4346
else if (!collection.ConstantValue.HasValue) // argument is not null
4447
{
@@ -54,7 +57,7 @@ internal static class MarshalInfo
5457
return marshalAsParamsStrings.Count > 0 ? marshalAsParamsStrings.ToImmutableArray() : null;
5558
}
5659

57-
private static void GetMarshalAsFromField(IFieldReferenceOperation fieldReference, CancellationToken cancellationToken, int argumentCount, List<Diagnostic> diagnostics, Location location, List<string?> marshalAsStrings)
60+
private static void GetMarshalAsFromField(IFieldReferenceOperation fieldReference, CancellationToken cancellationToken, int argumentCount, List<string?> marshalAsStrings)
5861
{
5962
// `GetOperation` is only returning `null` for the relevant `SyntaxNode`s here, so we have to manually parse the field initializer
6063
// see <https://stackoverflow.com/q/75916082/1136311>
@@ -64,22 +67,20 @@ private static void GetMarshalAsFromField(IFieldReferenceOperation fieldReferenc
6467
bool isInsideArrayInitializer = false;
6568
bool isInsideNewExpression = false;
6669
bool isInsideObjectInitializer = false;
67-
bool addedArrayLengthDiagnostic = false;
6870
var addMarshalAsString = () =>
6971
{
7072
if (sb.Length != 0)
7173
{
7274
marshalAsStrings.Add(sb.ToString());
73-
if (isArray && !addedArrayLengthDiagnostic && marshalAsStrings.Count > argumentCount)
74-
{
75-
addedArrayLengthDiagnostic = true;
76-
diagnostics.Add(Diagnostic.Create(Constants.InvalidMarshalParamsAsArrayLengthDescriptor, location));
77-
}
7875
_ = sb.Clear();
7976
}
8077
};
8178
foreach (var syntaxToken in fieldDeclaration.DescendantTokens())
8279
{
80+
if (marshalAsStrings.Count == argumentCount)
81+
{
82+
return;
83+
}
8384
var token = syntaxToken.ToString();
8485
switch (token)
8586
{
@@ -112,7 +113,15 @@ private static void GetMarshalAsFromField(IFieldReferenceOperation fieldReferenc
112113
addMarshalAsString();
113114
continue;
114115
case "null":
116+
if (isArray && !isInsideArrayInitializer)
117+
{
118+
return;
119+
}
115120
marshalAsStrings.Add(null);
121+
if (!isArray && !isInsideObjectInitializer)
122+
{
123+
return;
124+
}
116125
continue;
117126
case ",":
118127
if (isInsideObjectInitializer)
@@ -158,7 +167,14 @@ private static void GetMarshalAsFromOperation(IOperation value, CancellationToke
158167
}
159168
if (value is IFieldReferenceOperation fieldReference && fieldReference.Field.IsReadOnly)
160169
{
161-
GetMarshalAsFromField(fieldReference, cancellationToken, argumentCount, diagnostics, location, marshalAsStrings);
170+
GetMarshalAsFromField(fieldReference, cancellationToken, argumentCount, marshalAsStrings);
171+
if (fieldReference.Field.Type is IArrayTypeSymbol && (marshalAsStrings.Count > 0))
172+
{
173+
for (int count = marshalAsStrings.Count; count < argumentCount; ++count)
174+
{
175+
marshalAsStrings.Add(null);
176+
}
177+
}
162178
return;
163179
}
164180
IObjectCreationOperation? objectCreation = value as IObjectCreationOperation;
@@ -183,7 +199,7 @@ private static void GetMarshalAsFromOperation(IOperation value, CancellationToke
183199
public static void GetMarshalAsFromOperation(IOperation value, CancellationToken cancellationToken, List<Diagnostic> diagnostics, Location location, out string? marshalAsString)
184200
{
185201
List<string?> marshalAsStrings = new(1);
186-
GetMarshalAsFromOperation(value, cancellationToken, 0, diagnostics, location, marshalAsStrings);
202+
GetMarshalAsFromOperation(value, cancellationToken, 1, diagnostics, location, marshalAsStrings);
187203
marshalAsString = marshalAsStrings.FirstOrDefault();
188204
}
189205
}

NativeGenericDelegateInfo.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ public NativeGenericDelegateInfo(MethodSymbolWithMarshalInfo methodSymbolWithMar
132132
FunctionPointerTypeArgumentsWithReturnType += ", void";
133133
}
134134
cancellationToken.ThrowIfCancellationRequested();
135-
_ = sb.Append(andNewLine).Append($"MarshalInfo.Equals({(isAction ? "null" : "marshalReturnAs")}, marshalParamsAs, {marshalReturnAsAttribute}, {marshalParamsAsAttributes})");
135+
_ = sb.Append(andNewLine).Append($"MarshalInfo.Equals({(isAction ? "null" : "marshalReturnAs")}, {marshalReturnAsAttribute})")
136+
.Append(andNewLine).Append($"MarshalInfo.PartiallyEquals(marshalParamsAs, {marshalParamsAsAttributes})");
136137
TypeArgumentCheckWithMarshalInfoCondition = sb.ToString();
137138
}
138139
}

PartialImplementations.cs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -188,35 +188,7 @@ namespace {Constants.RootNamespace}
188188
{{
189189
file static class MarshalInfo
190190
{{
191-
internal static bool Equals(MarshalAsAttribute? marshalReturnAsLeft, MarshalAsAttribute?[]? marshalParamsAsLeft, MarshalAsAttribute? marshalReturnAsRight, MarshalAsAttribute?[]? marshalParamsAsRight)
192-
{{
193-
if (!Equals(marshalReturnAsLeft, marshalReturnAsRight))
194-
{{
195-
return false;
196-
}}
197-
if (marshalParamsAsLeft is null)
198-
{{
199-
return marshalParamsAsRight is null;
200-
}}
201-
else if (marshalParamsAsRight is null)
202-
{{
203-
return false;
204-
}}
205-
if (marshalParamsAsLeft.Length != marshalParamsAsRight.Length)
206-
{{
207-
return false;
208-
}}
209-
for (int i = 0; i < marshalParamsAsLeft.Length; ++i)
210-
{{
211-
if (!Equals(marshalParamsAsLeft[i], marshalParamsAsRight[i]))
212-
{{
213-
return false;
214-
}}
215-
}}
216-
return true;
217-
}}
218-
219-
private static bool Equals(MarshalAsAttribute? left, MarshalAsAttribute? right)
191+
internal static bool Equals(MarshalAsAttribute? left, MarshalAsAttribute? right)
220192
{{
221193
if (left is null)
222194
{{
@@ -238,6 +210,30 @@ private static bool Equals(MarshalAsAttribute? left, MarshalAsAttribute? right)
238210
left.MarshalTypeRef == right.MarshalTypeRef &&
239211
left.MarshalCookie == right.MarshalCookie;
240212
}}
213+
214+
internal static bool PartiallyEquals(MarshalAsAttribute?[]? left, MarshalAsAttribute?[]? right)
215+
{{
216+
if (left is null)
217+
{{
218+
return right is null;
219+
}}
220+
int i = 0;
221+
for (int len = Math.Min(left.Length, right?.Length ?? 0); i < len; ++i)
222+
{{
223+
if (!Equals(left[i], right![i]))
224+
{{
225+
return false;
226+
}}
227+
}}
228+
for ( ; i < left.Length; ++i)
229+
{{
230+
if (!Equals(left[i], null))
231+
{{
232+
return false;
233+
}}
234+
}}
235+
return true;
236+
}}
241237
}}
242238
{ConcreteClassDefinitions}{InterfaceImplementations}}}
243239

0 commit comments

Comments
 (0)