forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Report diagnostics in generator (dotnet/runtimelab#158)
Commit migrated from dotnet/runtimelab@98f55be
- Loading branch information
1 parent
09e1a9d
commit 653aeb3
Showing
16 changed files
with
866 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
217 changes: 217 additions & 0 deletions
217
src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/GeneratorDiagnostics.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
|
||
using Microsoft.CodeAnalysis; | ||
|
||
#nullable enable | ||
|
||
namespace Microsoft.Interop | ||
{ | ||
internal static class DiagnosticExtensions | ||
{ | ||
public static Diagnostic CreateDiagnostic( | ||
this ISymbol symbol, | ||
DiagnosticDescriptor descriptor, | ||
params object[] args) | ||
{ | ||
IEnumerable<Location> locationsInSource = symbol.Locations.Where(l => l.IsInSource); | ||
if (!locationsInSource.Any()) | ||
return Diagnostic.Create(descriptor, Location.None, args); | ||
|
||
return Diagnostic.Create( | ||
descriptor, | ||
location: locationsInSource.First(), | ||
additionalLocations: locationsInSource.Skip(1), | ||
messageArgs: args); | ||
} | ||
|
||
public static Diagnostic CreateDiagnostic( | ||
this AttributeData attributeData, | ||
DiagnosticDescriptor descriptor, | ||
params object[] args) | ||
{ | ||
SyntaxReference? syntaxReference = attributeData.ApplicationSyntaxReference; | ||
Location location = syntaxReference is not null | ||
? syntaxReference.GetSyntax().GetLocation() | ||
: Location.None; | ||
|
||
return Diagnostic.Create( | ||
descriptor, | ||
location: location.IsInSource ? location : Location.None, | ||
messageArgs: args); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Class for reporting diagnostics in the DLL import generator | ||
/// </summary> | ||
public class GeneratorDiagnostics | ||
{ | ||
public class Ids | ||
{ | ||
public const string Prefix = "DLLIMPORTGEN"; | ||
public const string TypeNotSupported = Prefix + "001"; | ||
public const string ConfigurationNotSupported = Prefix + "002"; | ||
} | ||
|
||
private const string Category = "DllImportGenerator"; | ||
|
||
public readonly static DiagnosticDescriptor ParameterTypeNotSupported = | ||
new DiagnosticDescriptor( | ||
Ids.TypeNotSupported, | ||
GetResourceString(nameof(Resources.TypeNotSupportedTitle)), | ||
GetResourceString(nameof(Resources.TypeNotSupportedMessageParameter)), | ||
Category, | ||
DiagnosticSeverity.Error, | ||
isEnabledByDefault: true, | ||
description: GetResourceString(Resources.TypeNotSupportedDescription)); | ||
|
||
public readonly static DiagnosticDescriptor ReturnTypeNotSupported = | ||
new DiagnosticDescriptor( | ||
Ids.TypeNotSupported, | ||
GetResourceString(nameof(Resources.TypeNotSupportedTitle)), | ||
GetResourceString(nameof(Resources.TypeNotSupportedMessageReturn)), | ||
Category, | ||
DiagnosticSeverity.Error, | ||
isEnabledByDefault: true, | ||
description: GetResourceString(Resources.TypeNotSupportedDescription)); | ||
|
||
public readonly static DiagnosticDescriptor ParameterConfigurationNotSupported = | ||
new DiagnosticDescriptor( | ||
Ids.ConfigurationNotSupported, | ||
GetResourceString(nameof(Resources.ConfigurationNotSupportedTitle)), | ||
GetResourceString(nameof(Resources.ConfigurationNotSupportedMessageParameter)), | ||
Category, | ||
DiagnosticSeverity.Error, | ||
isEnabledByDefault: true, | ||
description: GetResourceString(Resources.ConfigurationNotSupportedDescription)); | ||
|
||
public readonly static DiagnosticDescriptor ReturnConfigurationNotSupported = | ||
new DiagnosticDescriptor( | ||
Ids.ConfigurationNotSupported, | ||
GetResourceString(nameof(Resources.ConfigurationNotSupportedTitle)), | ||
GetResourceString(nameof(Resources.ConfigurationNotSupportedMessageReturn)), | ||
Category, | ||
DiagnosticSeverity.Error, | ||
isEnabledByDefault: true, | ||
description: GetResourceString(Resources.ConfigurationNotSupportedDescription)); | ||
|
||
public readonly static DiagnosticDescriptor ConfigurationNotSupported = | ||
new DiagnosticDescriptor( | ||
Ids.ConfigurationNotSupported, | ||
GetResourceString(nameof(Resources.ConfigurationNotSupportedTitle)), | ||
GetResourceString(nameof(Resources.ConfigurationNotSupportedMessage)), | ||
Category, | ||
DiagnosticSeverity.Error, | ||
isEnabledByDefault: true, | ||
description: GetResourceString(Resources.ConfigurationNotSupportedDescription)); | ||
|
||
public readonly static DiagnosticDescriptor ConfigurationValueNotSupported = | ||
new DiagnosticDescriptor( | ||
Ids.ConfigurationNotSupported, | ||
GetResourceString(nameof(Resources.ConfigurationNotSupportedTitle)), | ||
GetResourceString(nameof(Resources.ConfigurationNotSupportedMessageValue)), | ||
Category, | ||
DiagnosticSeverity.Error, | ||
isEnabledByDefault: true, | ||
description: GetResourceString(Resources.ConfigurationNotSupportedDescription)); | ||
|
||
private readonly GeneratorExecutionContext context; | ||
|
||
public GeneratorDiagnostics(GeneratorExecutionContext context) | ||
{ | ||
this.context = context; | ||
} | ||
|
||
/// <summary> | ||
/// Report diagnostic for configuration that is not supported by the DLL import source generator | ||
/// </summary> | ||
/// <param name="attributeData">Attribute specifying the unsupported configuration</param> | ||
/// <param name="configurationName">Name of the configuration</param> | ||
/// <param name="unsupportedValue">[Optiona] Unsupported configuration value</param> | ||
public void ReportConfigurationNotSupported( | ||
AttributeData attributeData, | ||
string configurationName, | ||
string? unsupportedValue = null) | ||
{ | ||
if (unsupportedValue == null) | ||
{ | ||
this.context.ReportDiagnostic( | ||
attributeData.CreateDiagnostic( | ||
GeneratorDiagnostics.ConfigurationNotSupported, | ||
configurationName)); | ||
} | ||
else | ||
{ | ||
this.context.ReportDiagnostic( | ||
attributeData.CreateDiagnostic( | ||
GeneratorDiagnostics.ConfigurationValueNotSupported, | ||
unsupportedValue, | ||
configurationName)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Report diagnostic for marshalling of a parameter/return that is not supported | ||
/// </summary> | ||
/// <param name="method">Method with the parameter/return</param> | ||
/// <param name="info">Type info for the parameter/return</param> | ||
internal void ReportMarshallingNotSupported( | ||
IMethodSymbol method, | ||
TypePositionInfo info) | ||
{ | ||
if (info.MarshallingAttributeInfo != null && info.MarshallingAttributeInfo is MarshalAsInfo) | ||
{ | ||
// Report that the specified marshalling configuration is not supported. | ||
// We don't forward marshalling attributes, so this is reported differently | ||
// than when there is no attribute and the type itself is not supported. | ||
if (info.IsManagedReturnPosition) | ||
{ | ||
this.context.ReportDiagnostic( | ||
method.CreateDiagnostic( | ||
GeneratorDiagnostics.ReturnConfigurationNotSupported, | ||
nameof(System.Runtime.InteropServices.MarshalAsAttribute), | ||
method.Name)); | ||
} | ||
else | ||
{ | ||
Debug.Assert(info.ManagedIndex <= method.Parameters.Length); | ||
IParameterSymbol paramSymbol = method.Parameters[info.ManagedIndex]; | ||
this.context.ReportDiagnostic( | ||
paramSymbol.CreateDiagnostic( | ||
GeneratorDiagnostics.ParameterConfigurationNotSupported, | ||
nameof(System.Runtime.InteropServices.MarshalAsAttribute), | ||
paramSymbol.Name)); | ||
} | ||
} | ||
else | ||
{ | ||
// Report that the type is not supported | ||
if (info.IsManagedReturnPosition) | ||
{ | ||
this.context.ReportDiagnostic( | ||
method.CreateDiagnostic( | ||
GeneratorDiagnostics.ReturnTypeNotSupported, | ||
method.ReturnType.ToDisplayString(), | ||
method.Name)); | ||
} | ||
else | ||
{ | ||
Debug.Assert(info.ManagedIndex <= method.Parameters.Length); | ||
IParameterSymbol paramSymbol = method.Parameters[info.ManagedIndex]; | ||
this.context.ReportDiagnostic( | ||
paramSymbol.CreateDiagnostic( | ||
GeneratorDiagnostics.ParameterTypeNotSupported, | ||
paramSymbol.Type.ToDisplayString(), | ||
paramSymbol.Name)); | ||
} | ||
} | ||
} | ||
|
||
private static LocalizableResourceString GetResourceString(string resourceName) | ||
{ | ||
return new LocalizableResourceString(resourceName, Resources.ResourceManager, typeof(Resources)); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...es/System.Runtime.InteropServices/gen/DllImportGenerator/ManualTypeMarshallingAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.