Skip to content

Commit

Permalink
Switch all diagnostics to errors
Browse files Browse the repository at this point in the history
Make Compiles test check that all reported diagnostics are DLLIMPORTGEN

Add doc listing diagnostics reported by the P/Invoke source generator
  • Loading branch information
elinor-fung committed Oct 9, 2020
1 parent 216e717 commit 9d04790
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions DllImportGenerator/DllImportGenerator/GeneratorDiagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static Diagnostic CreateDiagnostic(
/// </summary>
public class GeneratorDiagnostics
{
private class Ids
public class Ids
{
public const string Prefix = "DLLIMPORTGEN";
public const string TypeNotSupported = Prefix + "001";
Expand All @@ -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));

Expand All @@ -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));

Expand Down
39 changes: 39 additions & 0 deletions DllImportGenerator/designs/Diagnostics.md
Original file line number Diff line number Diff line change
@@ -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);
```

0 comments on commit 9d04790

Please sign in to comment.