Skip to content

Commit

Permalink
Create DiagnosticInfo as an incremental Diagnostic replacement type. (d…
Browse files Browse the repository at this point in the history
…otnet#86908)

Creates DiagnosticInfo record to replace Diagnostic in the incremental pipeline to work around dotnet/roslyn#68291
  • Loading branch information
jtschuster authored Jun 2, 2023
1 parent ffa12cc commit c3859ad
Show file tree
Hide file tree
Showing 29 changed files with 404 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ internal static class Comparers
/// <summary>
/// Comparer for an individual generated stub source as a syntax tree and the generated diagnostics for the stub.
/// </summary>
public static readonly IEqualityComparer<(MemberDeclarationSyntax Syntax, ImmutableArray<Diagnostic> Diagnostics)> GeneratedSyntax = new CustomValueTupleElementComparer<MemberDeclarationSyntax, ImmutableArray<Diagnostic>>(SyntaxEquivalentComparer.Instance, new ImmutableArraySequenceEqualComparer<Diagnostic>(EqualityComparer<Diagnostic>.Default));
public static readonly IEqualityComparer<(MemberDeclarationSyntax, StatementSyntax, AttributeListSyntax, ImmutableArray<Diagnostic>)> GeneratedSyntax4 =
new CustomValueTupleElementComparer<MemberDeclarationSyntax, StatementSyntax, AttributeListSyntax, ImmutableArray<Diagnostic>>(
public static readonly IEqualityComparer<(MemberDeclarationSyntax Syntax, ImmutableArray<DiagnosticInfo> Diagnostics)> GeneratedSyntax = new CustomValueTupleElementComparer<MemberDeclarationSyntax, ImmutableArray<DiagnosticInfo>>(SyntaxEquivalentComparer.Instance, new ImmutableArraySequenceEqualComparer<DiagnosticInfo>(EqualityComparer<DiagnosticInfo>.Default));
public static readonly IEqualityComparer<(MemberDeclarationSyntax, StatementSyntax, AttributeListSyntax, ImmutableArray<DiagnosticInfo>)> GeneratedSyntax4 =
new CustomValueTupleElementComparer<MemberDeclarationSyntax, StatementSyntax, AttributeListSyntax, ImmutableArray<DiagnosticInfo>>(
SyntaxEquivalentComparer.Instance, SyntaxEquivalentComparer.Instance, SyntaxEquivalentComparer.Instance,
new ImmutableArraySequenceEqualComparer<Diagnostic>(EqualityComparer<Diagnostic>.Default));
new ImmutableArraySequenceEqualComparer<DiagnosticInfo>(EqualityComparer<DiagnosticInfo>.Default));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ public class Ids
}

private const string Category = "JSImportGenerator";
private readonly List<Diagnostic> _diagnostics = new List<Diagnostic>();
private readonly List<DiagnosticInfo> _diagnostics = new List<DiagnosticInfo>();

public IEnumerable<Diagnostic> Diagnostics => _diagnostics;
public IEnumerable<DiagnosticInfo> Diagnostics => _diagnostics;

public void ReportInvalidMarshallingAttributeInfo(
AttributeData attributeData,
string reasonResourceName,
params string[] reasonArgs)
{
_diagnostics.Add(
attributeData.CreateDiagnostic(
attributeData.CreateDiagnosticInfo(
GeneratorDiagnostics.MarshallingAttributeConfigurationNotSupported,
new LocalizableResourceString(reasonResourceName, SR.ResourceManager, typeof(FxResources.Microsoft.Interop.JavaScript.JSImportGenerator.SR), reasonArgs)));
}
Expand All @@ -57,14 +57,14 @@ public void ReportConfigurationNotSupported(
if (unsupportedValue == null)
{
_diagnostics.Add(
attributeData.CreateDiagnostic(
attributeData.CreateDiagnosticInfo(
GeneratorDiagnostics.ConfigurationNotSupported,
configurationName));
}
else
{
_diagnostics.Add(
attributeData.CreateDiagnostic(
attributeData.CreateDiagnosticInfo(
GeneratorDiagnostics.ConfigurationValueNotSupported,
unsupportedValue,
configurationName));
Expand Down Expand Up @@ -104,7 +104,7 @@ public void ReportMarshallingNotSupported(
if (info.IsManagedReturnPosition)
{
_diagnostics.Add(
diagnosticLocation.CreateDiagnostic(
diagnosticLocation.CreateDiagnosticInfo(
GeneratorDiagnostics.ReturnTypeNotSupportedWithDetails,
diagnosticProperties,
notSupportedDetails!,
Expand All @@ -113,7 +113,7 @@ public void ReportMarshallingNotSupported(
else
{
_diagnostics.Add(
diagnosticLocation.CreateDiagnostic(
diagnosticLocation.CreateDiagnosticInfo(
GeneratorDiagnostics.ParameterTypeNotSupportedWithDetails,
diagnosticProperties,
notSupportedDetails!,
Expand All @@ -128,7 +128,7 @@ public void ReportMarshallingNotSupported(
if (info.IsManagedReturnPosition)
{
_diagnostics.Add(
diagnosticLocation.CreateDiagnostic(
diagnosticLocation.CreateDiagnosticInfo(
GeneratorDiagnostics.ReturnConfigurationNotSupported,
diagnosticProperties,
nameof(System.Runtime.InteropServices.MarshalAsAttribute),
Expand All @@ -137,7 +137,7 @@ public void ReportMarshallingNotSupported(
else
{
_diagnostics.Add(
diagnosticLocation.CreateDiagnostic(
diagnosticLocation.CreateDiagnosticInfo(
GeneratorDiagnostics.ParameterConfigurationNotSupported,
diagnosticProperties,
nameof(System.Runtime.InteropServices.MarshalAsAttribute),
Expand All @@ -150,7 +150,7 @@ public void ReportMarshallingNotSupported(
if (info.IsManagedReturnPosition)
{
_diagnostics.Add(
diagnosticLocation.CreateDiagnostic(
diagnosticLocation.CreateDiagnosticInfo(
GeneratorDiagnostics.ReturnTypeNotSupported,
diagnosticProperties,
info.ManagedType.DiagnosticFormattedName,
Expand All @@ -159,7 +159,7 @@ public void ReportMarshallingNotSupported(
else
{
_diagnostics.Add(
diagnosticLocation.CreateDiagnostic(
diagnosticLocation.CreateDiagnosticInfo(
GeneratorDiagnostics.ParameterTypeNotSupported,
diagnosticProperties,
info.ManagedType.DiagnosticFormattedName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal sealed record IncrementalStubGenerationContext(
MethodSignatureDiagnosticLocations DiagnosticLocation,
JSExportData JSExportData,
MarshallingGeneratorFactoryKey<(TargetFramework TargetFramework, Version TargetFrameworkVersion, JSGeneratorOptions)> GeneratorFactoryKey,
SequenceEqualImmutableArray<Diagnostic> Diagnostics);
SequenceEqualImmutableArray<DiagnosticInfo> Diagnostics);

public static class StepNames
{
Expand Down Expand Up @@ -69,13 +69,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
if (data.Right.IsEmpty // no attributed methods
|| data.Left.Compilation.Options is CSharpCompilationOptions { AllowUnsafe: true }) // Unsafe code enabled
{
return ImmutableArray<Diagnostic>.Empty;
return ImmutableArray<DiagnosticInfo>.Empty;
}

return ImmutableArray.Create(Diagnostic.Create(GeneratorDiagnostics.JSExportRequiresAllowUnsafeBlocks, null));
return ImmutableArray.Create(DiagnosticInfo.Create(GeneratorDiagnostics.JSExportRequiresAllowUnsafeBlocks, null));
}));

IncrementalValuesProvider<(MemberDeclarationSyntax, StatementSyntax, AttributeListSyntax, ImmutableArray<Diagnostic>)> generateSingleStub = methodsToGenerate
IncrementalValuesProvider<(MemberDeclarationSyntax, StatementSyntax, AttributeListSyntax, ImmutableArray<DiagnosticInfo>)> generateSingleStub = methodsToGenerate
.Combine(stubEnvironment)
.Combine(stubOptions)
.Select(static (data, ct) => new
Expand Down Expand Up @@ -214,7 +214,7 @@ private static IncrementalStubGenerationContext CalculateStubInformation(
new MethodSignatureDiagnosticLocations(originalSyntax),
jsExportData,
CreateGeneratorFactory(environment, options),
new SequenceEqualImmutableArray<Diagnostic>(generatorDiagnostics.Diagnostics.ToImmutableArray()));
new SequenceEqualImmutableArray<DiagnosticInfo>(generatorDiagnostics.Diagnostics.ToImmutableArray()));
}

private static MarshallingGeneratorFactoryKey<(TargetFramework, Version, JSGeneratorOptions)> CreateGeneratorFactory(StubEnvironment env, JSGeneratorOptions options)
Expand Down Expand Up @@ -286,7 +286,7 @@ private static NamespaceDeclarationSyntax GenerateRegSource(
return ns;
}

private static (MemberDeclarationSyntax, StatementSyntax, AttributeListSyntax, ImmutableArray<Diagnostic>) GenerateSource(
private static (MemberDeclarationSyntax, StatementSyntax, AttributeListSyntax, ImmutableArray<DiagnosticInfo>) GenerateSource(
IncrementalStubGenerationContext incrementalContext)
{
var diagnostics = new GeneratorDiagnostics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal sealed record IncrementalStubGenerationContext(
MethodSignatureDiagnosticLocations DiagnosticLocation,
JSImportData JSImportData,
MarshallingGeneratorFactoryKey<(TargetFramework TargetFramework, Version TargetFrameworkVersion, JSGeneratorOptions)> GeneratorFactoryKey,
SequenceEqualImmutableArray<Diagnostic> Diagnostics);
SequenceEqualImmutableArray<DiagnosticInfo> Diagnostics);

public static class StepNames
{
Expand Down Expand Up @@ -69,13 +69,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
if (data.Right.IsEmpty // no attributed methods
|| data.Left.Compilation.Options is CSharpCompilationOptions { AllowUnsafe: true }) // Unsafe code enabled
{
return ImmutableArray<Diagnostic>.Empty;
return ImmutableArray<DiagnosticInfo>.Empty;
}

return ImmutableArray.Create(Diagnostic.Create(GeneratorDiagnostics.JSImportRequiresAllowUnsafeBlocks, null));
return ImmutableArray.Create(DiagnosticInfo.Create(GeneratorDiagnostics.JSImportRequiresAllowUnsafeBlocks, null));
}));

IncrementalValuesProvider<(MemberDeclarationSyntax, ImmutableArray<Diagnostic>)> generateSingleStub = methodsToGenerate
IncrementalValuesProvider<(MemberDeclarationSyntax, ImmutableArray<DiagnosticInfo>)> generateSingleStub = methodsToGenerate
.Combine(stubEnvironment)
.Combine(stubOptions)
.Select(static (data, ct) => new
Expand Down Expand Up @@ -201,7 +201,7 @@ private static IncrementalStubGenerationContext CalculateStubInformation(
new MethodSignatureDiagnosticLocations(originalSyntax),
jsImportData,
CreateGeneratorFactory(environment, options),
new SequenceEqualImmutableArray<Diagnostic>(generatorDiagnostics.Diagnostics.ToImmutableArray()));
new SequenceEqualImmutableArray<DiagnosticInfo>(generatorDiagnostics.Diagnostics.ToImmutableArray()));
}

private static MarshallingGeneratorFactoryKey<(TargetFramework, Version, JSGeneratorOptions)> CreateGeneratorFactory(StubEnvironment env, JSGeneratorOptions options)
Expand All @@ -211,7 +211,7 @@ private static IncrementalStubGenerationContext CalculateStubInformation(
return MarshallingGeneratorFactoryKey.Create((env.TargetFramework, env.TargetFrameworkVersion, options), jsGeneratorFactory);
}

private static (MemberDeclarationSyntax, ImmutableArray<Diagnostic>) GenerateSource(
private static (MemberDeclarationSyntax, ImmutableArray<DiagnosticInfo>) GenerateSource(
IncrementalStubGenerationContext incrementalContext)
{
var diagnostics = new GeneratorDiagnostics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override void Initialize(AnalysisContext context)
&& GetAttribute(typeSymbol, TypeNames.InterfaceTypeAttribute, out AttributeData? comInterfaceAttribute)
&& !InterfaceTypeAttributeIsSupported(comInterfaceAttribute, out string unsupportedValue))
{
context.ReportDiagnostic(comInterfaceAttribute.CreateDiagnostic(AnalyzerDiagnostics.InterfaceTypeNotSupported, unsupportedValue));
context.ReportDiagnostic(comInterfaceAttribute.CreateDiagnosticInfo(AnalyzerDiagnostics.InterfaceTypeNotSupported, unsupportedValue).ToDiagnostic());
}
}, SymbolKind.NamedType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ DiagnosticOr<ComInterfaceContext> AddContext(ComInterfaceInfo iface)
{
// The base has failed generation at some point, so this interface cannot be generated
var diagnostic = DiagnosticOr<ComInterfaceContext>.From(
Diagnostic.Create(
DiagnosticInfo.Create(
GeneratorDiagnostics.BaseInterfaceIsNotGenerated,
iface.DiagnosticLocation.AsLocation(), iface.ThisInterfaceKey, iface.BaseInterfaceKey));
iface.DiagnosticLocation, iface.ThisInterfaceKey, iface.BaseInterfaceKey));
nameToContextCache[iface.ThisInterfaceKey] = diagnostic;
return diagnostic;
}
Expand Down
Loading

0 comments on commit c3859ad

Please # to comment.