From 016f71d08dbc2896f6de1721612cade25892b47b Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Fri, 9 Oct 2020 14:10:19 -0700 Subject: [PATCH] Switch all diagnostics to errors Make Compiles test check that all reported diagnostics are DLLIMPORTGEN Add doc listing diagnostics reported by the P/Invoke source generator --- .../DllImportGenerator.UnitTests/Compiles.cs | 1 + .../GeneratorDiagnostics.cs | 6 +-- DllImportGenerator/designs/Diagnostics.md | 39 +++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 DllImportGenerator/designs/Diagnostics.md diff --git a/DllImportGenerator/DllImportGenerator.UnitTests/Compiles.cs b/DllImportGenerator/DllImportGenerator.UnitTests/Compiles.cs index ab1b7aff1032..815db781816b 100644 --- a/DllImportGenerator/DllImportGenerator.UnitTests/Compiles.cs +++ b/DllImportGenerator/DllImportGenerator.UnitTests/Compiles.cs @@ -161,6 +161,7 @@ public async Task ValidateSnippets_WithDiagnostics(string source) var newComp = TestUtils.RunGenerators(comp, out var generatorDiags, new Microsoft.Interop.DllImportGenerator()); Assert.NotEmpty(generatorDiags); + Assert.All(generatorDiags, d => Assert.StartsWith(Microsoft.Interop.GeneratorDiagnostics.Ids.Prefix, d.Id)); var newCompDiags = newComp.GetDiagnostics(); Assert.Empty(newCompDiags); diff --git a/DllImportGenerator/DllImportGenerator/GeneratorDiagnostics.cs b/DllImportGenerator/DllImportGenerator/GeneratorDiagnostics.cs index e3f7a39a9543..17657fe4eaf5 100644 --- a/DllImportGenerator/DllImportGenerator/GeneratorDiagnostics.cs +++ b/DllImportGenerator/DllImportGenerator/GeneratorDiagnostics.cs @@ -48,7 +48,7 @@ public static Diagnostic CreateDiagnostic( /// public class GeneratorDiagnostics { - private class Ids + public class Ids { public const string Prefix = "DLLIMPORTGEN"; public const string TypeNotSupported = Prefix + "001"; @@ -63,7 +63,7 @@ private class Ids GetResourceString(nameof(Resources.TypeNotSupportedTitle)), GetResourceString(nameof(Resources.TypeNotSupportedMessageParameter)), Category, - DiagnosticSeverity.Warning, + DiagnosticSeverity.Error, isEnabledByDefault: true, description: GetResourceString(Resources.TypeNotSupportedDescription)); @@ -73,7 +73,7 @@ private class Ids GetResourceString(nameof(Resources.TypeNotSupportedTitle)), GetResourceString(nameof(Resources.TypeNotSupportedMessageReturn)), Category, - DiagnosticSeverity.Warning, + DiagnosticSeverity.Error, isEnabledByDefault: true, description: GetResourceString(Resources.TypeNotSupportedDescription)); diff --git a/DllImportGenerator/designs/Diagnostics.md b/DllImportGenerator/designs/Diagnostics.md new file mode 100644 index 000000000000..d69d633022b0 --- /dev/null +++ b/DllImportGenerator/designs/Diagnostics.md @@ -0,0 +1,39 @@ +# Generator Diagnostics + +For all [Roslyn diagnostics](https://docs.microsoft.com/dotnet/api/microsoft.codeanalysis.diagnostic) reported by the P/Invoke source generator: + +| Setting | Value | +| -------- | ------------------ | +| Category | DllImportGenerator | +| Severity | Error | +| Enabled | True | + +The P/Invoke source generator emits the following diagnostics. + +## `DLLIMPORTGEN001`: Specified type is not supported by source-generated P/Invokes + +A method marked `GeneratedDllImport` has a parameter or return type that is not supported by source-generated P/Invokes. + +```C# +// 'object' without any specific marshalling configuration +[GeneratedDllImport("NativeLib")] +public static partial void Method(object o); +``` + +## `DLLIMPORTGEN002`: Specified configuration is not supported by source-generated P/Invokes + +A method marked `GeneratedDllImport` has configuration that is not supported by source-generated P/Invokes. This may be configuration of the method itself or its parameter or return types. + +```C# +// MarshalAs value that does not map to an UnmanagedType +[GeneratedDllImport("NativeLib")] +public static partial void Method([MarshalAs(1)] int i); + +// Unsupported field on MarshalAs (SafeArraySubType, SafeArrayUserDefinedSubType, IidParameterIndex) +[GeneratedDllImport("NativeLib")] +public static partial void Method([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BOOL)] bool[] bArr); + +// Unsupported combination of MarshalAs and type being marshalled +[GeneratedDllImport("NativeLib")] +public static partial void Method([MarshalAs(UnmanagedType.LPStr)] bool b); +``` \ No newline at end of file