Skip to content

Commit f4cb6ac

Browse files
authored
Add analyzer support to validate the stateless marshaller shapes (dotnet#72643)
1 parent dcf40dd commit f4cb6ac

26 files changed

+3596
-691
lines changed

docs/project/list-of-diagnostics.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ The diagnostic id values reserved for .NET Libraries analyzer warnings are `SYSL
163163
| __`SYSLIB1052`__ | Specified configuration is not supported by source-generated P/Invokes |
164164
| __`SYSLIB1053`__ | Specified LibraryImportAttribute arguments cannot be forwarded to DllImportAttribute |
165165
| __`SYSLIB1054`__ | Use 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time |
166-
| __`SYSLIB1055`__ | Invalid CustomTypeMarshallerAttribute usage |
166+
| __`SYSLIB1055`__ | Invalid CustomMarshallerAttribute usage |
167167
| __`SYSLIB1056`__ | Specified native type is invalid |
168168
| __`SYSLIB1057`__ | Marshaller type does not have the required shape |
169-
| __`SYSLIB1058`__ | Marshaller type defines a well-known method without specifying support for the corresponding feature |
169+
| __`SYSLIB1058`__ | Invalid NativeMarshallingAttribute usage |
170170
| __`SYSLIB1059`__ | Marshaller type does not support allocating constructor |
171-
| __`SYSLIB1060`__ | BufferSize should be set on CustomTypeMarshallerAttribute |
171+
| __`SYSLIB1060`__ | Specified marshaller type is invalid |
172172
| __`SYSLIB1061`__ | Marshaller type has incompatible method signatures |
173173
| __`SYSLIB1062`__ | Project must be updated with '<AllowUnsafeBlocks>true</AllowUnsafeBlocks>' |
174174
| __`SYSLIB1063`__ | *_`SYSLIB1063`-`SYSLIB1069` reserved for Microsoft.Interop.LibraryImportGenerator._* |

src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/AnalyzerDiagnostics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static class Ids
2424
public const string CustomMarshallerTypeMustHaveRequiredShape = Prefix + "1057";
2525
public const string InvalidNativeMarshallingAttributeUsage = Prefix + "1058";
2626
public const string MissingAllocatingMarshallingFallback = Prefix + "1059";
27-
public const string CallerAllocConstructorMustHaveBufferSize = Prefix + "1060";
27+
public const string InvalidMarshallerType = Prefix + "1060";
2828
public const string InvalidSignaturesInMarshallerShape = Prefix + "1061";
2929
}
3030

src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/CustomMarshallerAttributeAnalyzer.cs

Lines changed: 568 additions & 41 deletions
Large diffs are not rendered by default.

src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/CustomMarshallerAttributeFixer.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ private static void AddMissingMembers(DocumentEditor editor, IEnumerable<Diagnos
7070

7171
public override ImmutableArray<string> FixableDiagnosticIds { get; } =
7272
ImmutableArray.Create(
73-
AnalyzerDiagnostics.Ids.CustomMarshallerTypeMustHaveRequiredShape,
74-
AnalyzerDiagnostics.Ids.MissingAllocatingMarshallingFallback,
75-
AnalyzerDiagnostics.Ids.InvalidNativeMarshallingAttributeUsage);
73+
AnalyzerDiagnostics.Ids.CustomMarshallerTypeMustHaveRequiredShape);
7674

7775
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
7876
{

src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/DiagnosticReporter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ public DiagnosticReporter(Action<DiagnosticDescriptor, ImmutableDictionary<strin
1919
public static DiagnosticReporter CreateForLocation(Location location, Action<Diagnostic> reportDiagnostic) => new((descriptor, properties, args) => reportDiagnostic(location.CreateDiagnostic(descriptor, properties, args)));
2020

2121
public void CreateAndReportDiagnostic(DiagnosticDescriptor descriptor, params object[] messageArgs) => _diagnosticFactory(descriptor, ImmutableDictionary<string, string>.Empty, messageArgs);
22+
23+
public void CreateAndReportDiagnostic(DiagnosticDescriptor descriptor, ImmutableDictionary<string, string> properties, params object[] messageArgs) => _diagnosticFactory(descriptor, properties, messageArgs);
2224
}
2325
}

src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/NativeMarshallingAttributeAnalyzer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class NativeMarshallingAttributeAnalyzer : DiagnosticAnalyzer
2222
public static readonly DiagnosticDescriptor MarshallerEntryPointTypeMustHaveCustomMarshallerAttributeWithMatchingManagedTypeRule =
2323
new DiagnosticDescriptor(
2424
Ids.InvalidNativeMarshallingAttributeUsage,
25-
GetResourceString(nameof(SR.InvalidMarshallerTypeTitle)),
25+
GetResourceString(nameof(SR.InvalidNativeMarshallingAttributeUsageTitle)),
2626
GetResourceString(nameof(SR.EntryPointTypeMustHaveCustomMarshallerAttributeWithMatchingManagedTypeMessage)),
2727
Category,
2828
DiagnosticSeverity.Error,
@@ -32,7 +32,7 @@ public class NativeMarshallingAttributeAnalyzer : DiagnosticAnalyzer
3232
public static readonly DiagnosticDescriptor MarshallerEntryPointTypeMustBeNonNullRule =
3333
new DiagnosticDescriptor(
3434
Ids.InvalidNativeMarshallingAttributeUsage,
35-
GetResourceString(nameof(SR.InvalidMarshallerTypeTitle)),
35+
GetResourceString(nameof(SR.InvalidNativeMarshallingAttributeUsageTitle)),
3636
GetResourceString(nameof(SR.EntryPointTypeMustBeNonNullMessage)),
3737
Category,
3838
DiagnosticSeverity.Error,
@@ -42,7 +42,7 @@ public class NativeMarshallingAttributeAnalyzer : DiagnosticAnalyzer
4242
public static readonly DiagnosticDescriptor GenericEntryPointMarshallerTypeMustBeClosedOrMatchArityRule =
4343
new DiagnosticDescriptor(
4444
Ids.InvalidNativeMarshallingAttributeUsage,
45-
GetResourceString(nameof(SR.InvalidMarshallerTypeTitle)),
45+
GetResourceString(nameof(SR.InvalidNativeMarshallingAttributeUsageTitle)),
4646
GetResourceString(nameof(SR.GenericEntryPointMarshallerTypeMustBeClosedOrMatchArityMessage)),
4747
Category,
4848
DiagnosticSeverity.Error,

0 commit comments

Comments
 (0)