Added error for captured fields of invalid types

このコミットが含まれているのは:
Sergio Pedri committed 2021-02-17 19:22:42 +01:00
1 親 eb5cf26d8f
コミット 4b280bf2b6
9ファイル変更
+76 -17

検索条件に一致するファイルはありません

+2
ファイルの表示
@@ -0,0 +1,2 @@
; Shipped analyzer releases
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
+8
ファイルの表示
@@ -0,0 +1,8 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
### New Rules
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
CMPS0001 | ComputeSharp.IComputeShader | Error | IComputeShaderSourceGenerator, [Documentation](https://github.com/Sergio0694/ComputeSharp)
+2 -2
ファイルの表示
@@ -47,8 +47,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" PrivateAssets="all" Pack="false" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3-beta1.21109.6" PrivateAssets="all" Pack="false" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0-4.final" PrivateAssets="all" Pack="false" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3-beta1.21116.13" PrivateAssets="all" Pack="false" />
</ItemGroup>
</Project>
+26
ファイルの表示
@@ -0,0 +1,26 @@
using Microsoft.CodeAnalysis;
namespace ComputeSharp.SourceGenerators.Diagnostics
{
/// <summary>
/// A container for all <see cref="DiagnosticDescriptor"/> instances for errors reported by analyzers in this project.
/// </summary>
internal static class DiagnosticDescriptors
{
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for an invalid shader field.
/// <para>
/// Format: <c>"The compute shader of type {0} contains a field \"{1}\" of an invalid type {2}"</c>.
/// </para>
/// </summary>
public static DiagnosticDescriptor InvalidShaderField { get; } = new(
id: "CMPS0001",
title: "Invalid shader field",
messageFormat: "The compute shader of type {0} contains a field \"{1}\" of an invalid type {2}",
category: typeof(IComputeShader).FullName,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "A type representing a compute shader contains a field of a type that is not supported in HLSL.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
}
+3 -3
ファイルの表示
@@ -31,12 +31,12 @@ namespace ComputeSharp.SourceGenerators.Extensions
.WithParameterOptions(SymbolDisplayParameterOptions.None);
/// <summary>
/// Gets the full metadata name for a given <see cref="INamedTypeSymbol"/> instance.
/// Gets the full metadata name for a given <see cref="ITypeSymbol"/> instance.
/// </summary>
/// <param name="symbol">The input <see cref="INamedTypeSymbol"/> instance.</param>
/// <param name="symbol">The input <see cref="ITypeSymbol"/> instance.</param>
/// <returns>The full metadata name for <paramref name="symbol"/>.</returns>
[Pure]
public static string GetFullMetadataName(this INamedTypeSymbol symbol)
public static string GetFullMetadataName(this ITypeSymbol symbol)
{
static StringBuilder BuildFrom(ISymbol? symbol, StringBuilder builder)
{
+4 -2
ファイルの表示
@@ -141,9 +141,11 @@ namespace ComputeSharp.SourceGenerators
foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>())
{
if (fieldSymbol.IsStatic) continue;
if (fieldSymbol.Type is not INamedTypeSymbol { IsStatic: false } typeSymbol)
{
continue;
}
INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type;
string typeName = typeSymbol.GetFullMetadataName();
if (HlslKnownTypes.IsTypedResourceType(typeName))
+4 -5
ファイルの表示
@@ -120,11 +120,10 @@ namespace ComputeSharp.SourceGenerators
{
foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>())
{
if (fieldSymbol.IsStatic) continue;
INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type;
if (typeSymbol.TypeKind != TypeKind.Delegate) continue;
if (fieldSymbol.Type is not INamedTypeSymbol { TypeKind: TypeKind.Delegate, IsStatic: false } typeSymbol)
{
continue;
}
// hash += hash << 5;
yield return ExpressionStatement(ParseExpression($"hash += hash << 5"));
+26 -4
ファイルの表示
@@ -4,6 +4,7 @@ using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using ComputeSharp.__Internals;
using ComputeSharp.SourceGenerators.Diagnostics;
using ComputeSharp.SourceGenerators.Extensions;
using ComputeSharp.SourceGenerators.Mappings;
using ComputeSharp.SourceGenerators.SyntaxRewriters;
@@ -49,7 +50,7 @@ namespace ComputeSharp.SourceGenerators
Dictionary<IFieldSymbol, string> constantDefinitions = new(SymbolEqualityComparer.Default);
// Explore the syntax tree and extract the processed info
var processedMembers = GetProcessedMembers(structDeclarationSymbol, discoveredTypes).ToArray();
var processedMembers = GetProcessedMembers(context, structDeclarationSymbol, discoveredTypes).ToArray();
var sharedBuffers = GetGroupSharedMembers(structDeclarationSymbol, discoveredTypes).ToArray();
var (entryPoint, localFunctions) = GetProcessedMethods(structDeclaration, structDeclarationSymbol, semanticModel, discoveredTypes, staticMethods, constantDefinitions);
var processedTypes = GetProcessedTypes(discoveredTypes).ToArray();
@@ -82,17 +83,30 @@ namespace ComputeSharp.SourceGenerators
/// <summary>
/// Gets a sequence of captured members and their mapped names.
/// </summary>
/// <param name="context">The current generator context in use.</param>
/// <param name="structDeclarationSymbol">The input <see cref="INamedTypeSymbol"/> instance to process.</param>
/// <param name="types">The collection of currently discovered types.</param>
/// <returns>A sequence of captured members in <paramref name="structDeclarationSymbol"/>.</returns>
[Pure]
private static IEnumerable<IEnumerable<string>> GetProcessedMembers(INamedTypeSymbol structDeclarationSymbol, ICollection<INamedTypeSymbol> types)
private static IEnumerable<IEnumerable<string>> GetProcessedMembers(
GeneratorExecutionContext context,
INamedTypeSymbol structDeclarationSymbol,
ICollection<INamedTypeSymbol> types)
{
foreach (var fieldSymbol in structDeclarationSymbol.GetMembers().OfType<IFieldSymbol>())
{
if (fieldSymbol.IsStatic) continue;
// Captured fields must be named type symbols
if (fieldSymbol.Type is not INamedTypeSymbol typeSymbol)
{
context.ReportDiagnostic(Diagnostic.Create(
DiagnosticDescriptors.InvalidShaderField,
fieldSymbol.Locations.FirstOrDefault(),
structDeclarationSymbol, fieldSymbol.Name, fieldSymbol.Type));
INamedTypeSymbol typeSymbol = (INamedTypeSymbol)fieldSymbol.Type;
continue;
}
if (fieldSymbol.IsStatic) continue;
string typeName = HlslKnownTypes.GetMappedName(typeSymbol);
@@ -107,6 +121,14 @@ namespace ComputeSharp.SourceGenerators
{
types.Add((INamedTypeSymbol)typeSymbol.TypeArguments[0]);
}
else if (!typeSymbol.IsUnmanagedType)
{
// Shaders can only capture valid HLSL resource types or unmanaged types
context.ReportDiagnostic(Diagnostic.Create(
DiagnosticDescriptors.InvalidShaderField,
fieldSymbol.Locations.FirstOrDefault(),
structDeclarationSymbol, fieldSymbol.Name, typeSymbol));
}
}
}
+1 -1
ファイルの表示
@@ -478,7 +478,7 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters
// Track and rewrite the discarded declaration
if (this.semanticModel.GetOperation(node.Expression) is IDiscardOperation operation)
{
TypeSyntax typeSyntax = operation.Type.TrackType(this.discoveredTypes);
TypeSyntax typeSyntax = operation.Type!.TrackType(this.discoveredTypes);
string identifier = $"__implicit{this.implicitVariables.Count}";
// Add the variable to the list of implicit declarations