From fc5f06f5bfd10ddc25ef62b9975d9dbd7ca320a9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 23 Nov 2023 23:11:52 +0100 Subject: [PATCH] Use primary constructors in base analyzer types --- ...safeBlocksCompilationOptionAnalyzerBase.cs | 23 ++---------- ...erDescriptorAttributeTargetAnalyzerBase.cs | 37 ++++--------------- ...erDescriptorAttributeTargetAnalyzerBase.cs | 37 ++++--------------- 3 files changed, 20 insertions(+), 77 deletions(-) diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs index 1b719501..8f620e8b 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase.cs @@ -8,26 +8,11 @@ namespace ComputeSharp.SourceGeneration.Diagnostics; /// /// A diagnostic analyzer that generates an error if the AllowUnsafeBlocks compilation option is not set. /// -public abstract class MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase : DiagnosticAnalyzer +/// The instance to use. +public abstract class MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase(DiagnosticDescriptor diagnosticDescriptor) : DiagnosticAnalyzer { - /// - /// The instance to use. - /// - private readonly DiagnosticDescriptor diagnosticDescriptor; - - /// - /// Creates a new instance with the specified arguments. - /// - /// The instance to use. - private protected MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase(DiagnosticDescriptor diagnosticDescriptor) - { - this.diagnosticDescriptor = diagnosticDescriptor; - - SupportedDiagnostics = ImmutableArray.Create(diagnosticDescriptor); - } - /// - public sealed override ImmutableArray SupportedDiagnostics { get; } + public sealed override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(diagnosticDescriptor); /// public sealed override void Initialize(AnalysisContext context) @@ -40,7 +25,7 @@ public abstract class MissingAllowUnsafeBlocksCompilationOptionAnalyzerBase : Di // Check whether unsafe blocks are available, and emit an error if they are not if (!context.Compilation.IsAllowUnsafeBlocksEnabled()) { - context.ReportDiagnostic(Diagnostic.Create(this.diagnosticDescriptor, location: null)); + context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, location: null)); } }); } diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs index b0165b0f..cd91417a 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs @@ -8,35 +8,14 @@ namespace ComputeSharp.SourceGeneration.Diagnostics; /// /// A diagnostic analyzer that generates an error if a field in a target shader type is of a type that is not accessible from its containing assembly. /// -public abstract class NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase : DiagnosticAnalyzer +/// The instance to use. +/// The fully qualified type name of the target attribute. +public abstract class NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase( + DiagnosticDescriptor diagnosticDescriptor, + string generatedShaderDescriptorFullyQualifiedTypeName) : DiagnosticAnalyzer { - /// - /// The instance to use. - /// - private readonly DiagnosticDescriptor diagnosticDescriptor; - - /// - /// The fully qualified type name of the target attribute. - /// - private readonly string generatedShaderDescriptorFullyQualifiedTypeName; - - /// - /// Creates a new instance with the specified arguments. - /// - /// The instance to use. - /// The fully qualified type name of the target attribute. - private protected NotAccessibleFieldTypeInGeneratedShaderDescriptorAttributeTargetAnalyzerBase( - DiagnosticDescriptor diagnosticDescriptor, - string generatedShaderDescriptorFullyQualifiedTypeName) - { - this.diagnosticDescriptor = diagnosticDescriptor; - this.generatedShaderDescriptorFullyQualifiedTypeName = generatedShaderDescriptorFullyQualifiedTypeName; - - SupportedDiagnostics = ImmutableArray.Create(diagnosticDescriptor); - } - /// - public sealed override ImmutableArray SupportedDiagnostics { get; } + public sealed override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(diagnosticDescriptor); /// public sealed override void Initialize(AnalysisContext context) @@ -47,7 +26,7 @@ public abstract class NotAccessibleFieldTypeInGeneratedShaderDescriptorAttribute context.RegisterCompilationStartAction(context => { // Get the symbol for the target attribute type - if (context.Compilation.GetTypeByMetadataName(this.generatedShaderDescriptorFullyQualifiedTypeName) is not { } generatedShaderDescriptorAttributeSymbol) + if (context.Compilation.GetTypeByMetadataName(generatedShaderDescriptorFullyQualifiedTypeName) is not { } generatedShaderDescriptorAttributeSymbol) { return; } @@ -78,7 +57,7 @@ public abstract class NotAccessibleFieldTypeInGeneratedShaderDescriptorAttribute if (!fieldTypeSymbol.IsAccessibleFromCompilationAssembly(context.Compilation)) { context.ReportDiagnostic(Diagnostic.Create( - this.diagnosticDescriptor, + diagnosticDescriptor, attribute.GetLocation(), typeSymbol, memberSymbol.Name, diff --git a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs index 9e050c6c..8c5ed57c 100644 --- a/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs +++ b/src/ComputeSharp.SourceGeneration.Hlsl/Diagnostics/Analyzers/NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase.cs @@ -8,35 +8,14 @@ namespace ComputeSharp.SourceGeneration.Diagnostics; /// /// A diagnostic analyzer that generates an error if a target shader type is not accessible from its containing assembly. /// -public abstract class NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase : DiagnosticAnalyzer +/// The instance to use. +/// The fully qualified type name of the target attribute. +public abstract class NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase( + DiagnosticDescriptor diagnosticDescriptor, + string generatedShaderDescriptorFullyQualifiedTypeName) : DiagnosticAnalyzer { - /// - /// The instance to use. - /// - private readonly DiagnosticDescriptor diagnosticDescriptor; - - /// - /// The fully qualified type name of the target attribute. - /// - private readonly string generatedShaderDescriptorFullyQualifiedTypeName; - - /// - /// Creates a new instance with the specified arguments. - /// - /// The instance to use. - /// The fully qualified type name of the target attribute. - private protected NotAccessibleGeneratedShaderDescriptorAttributeTargetAnalyzerBase( - DiagnosticDescriptor diagnosticDescriptor, - string generatedShaderDescriptorFullyQualifiedTypeName) - { - this.diagnosticDescriptor = diagnosticDescriptor; - this.generatedShaderDescriptorFullyQualifiedTypeName = generatedShaderDescriptorFullyQualifiedTypeName; - - SupportedDiagnostics = ImmutableArray.Create(diagnosticDescriptor); - } - /// - public sealed override ImmutableArray SupportedDiagnostics { get; } + public sealed override ImmutableArray SupportedDiagnostics { get; } = ImmutableArray.Create(diagnosticDescriptor); /// public sealed override void Initialize(AnalysisContext context) @@ -47,7 +26,7 @@ public abstract class NotAccessibleGeneratedShaderDescriptorAttributeTargetAnaly context.RegisterCompilationStartAction(context => { // Get the symbol for the target attribute type - if (context.Compilation.GetTypeByMetadataName(this.generatedShaderDescriptorFullyQualifiedTypeName) is not { } generatedShaderDescriptorAttributeSymbol) + if (context.Compilation.GetTypeByMetadataName(generatedShaderDescriptorFullyQualifiedTypeName) is not { } generatedShaderDescriptorAttributeSymbol) { return; } @@ -65,7 +44,7 @@ public abstract class NotAccessibleGeneratedShaderDescriptorAttributeTargetAnaly !typeSymbol.IsAccessibleFromContainingAssembly(context.Compilation)) { context.ReportDiagnostic(Diagnostic.Create( - this.diagnosticDescriptor, + diagnosticDescriptor, attribute.GetLocation(), typeSymbol)); }