Enable assembly defaults, update analyzer

このコミットが含まれているのは:
Sergio Pedri committed 2023-09-27 15:03:06 +02:00
1 親 dd8f221905
コミット e5daf558ec
10ファイル変更
+217 -71

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

+3
ファイルの表示
@@ -67,3 +67,6 @@ CMPSD2D0057 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github
CMPSD2D0058 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0059 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0060 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0061 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0062 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
CMPSD2D0063 | ComputeSharp.D2D1.Shaders | Error | [Documentation](https://github.com/Sergio0694/ComputeSharp)
@@ -1,62 +0,0 @@
using System.Collections.Immutable;
using ComputeSharp.D2D1.SourceGenerators.Helpers;
using ComputeSharp.SourceGeneration.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.D2D1.SourceGenerators;
/// <summary>
/// A diagnostic analyzer that generates an error whenever [D2DEffectName] is used with an invalid value.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class InvalidEffectDisplayNameValueAnalyzer : DiagnosticAnalyzer
{
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(InvalidD2DEffectDisplayNameAttributeValue);
/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(static context =>
{
// Get the [D2DEffectDisplayName] symbol
if (context.Compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DEffectDisplayNameAttribute") is not { } d2DEffectIdAttributeSymbol)
{
return;
}
context.RegisterSymbolAction(context =>
{
// Only struct types are possible targets
if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Struct } typeSymbol)
{
return;
}
foreach (AttributeData attributeData in typeSymbol.GetAttributes())
{
// Look for the [D2DEffectDisplayName] use
if (!SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, d2DEffectIdAttributeSymbol))
{
continue;
}
// Validate the effect display name
if (!D2D1EffectMetadataParser.IsValidEffectMetadataName(attributeData))
{
context.ReportDiagnostic(Diagnostic.Create(
InvalidD2DEffectDisplayNameAttributeValue,
attributeData.GetLocation()));
}
return;
}
}, SymbolKind.NamedType);
});
}
}
@@ -0,0 +1,88 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using ComputeSharp.D2D1.SourceGenerators.Helpers;
using ComputeSharp.SourceGeneration.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using static ComputeSharp.SourceGeneration.Diagnostics.DiagnosticDescriptors;
namespace ComputeSharp.D2D1.SourceGenerators;
/// <summary>
/// A diagnostic analyzer that generates an error whenever any shader metadata value attributes are used with an invalid value.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class InvalidEffectMetadataValueAnalyzer : DiagnosticAnalyzer
{
/// <summary>
/// The set of type names for all metadata attributes.
/// </summary>
private static readonly ImmutableArray<string> MetadataAttributeTypeNames = ImmutableArray.Create(
"ComputeSharp.D2D1.D2DEffectDisplayNameAttribute",
"ComputeSharp.D2D1.D2DEffectDescriptionAttribute",
"ComputeSharp.D2D1.D2DEffectCategoryAttribute",
"ComputeSharp.D2D1.D2DEffectAuthorAttribute");
/// <summary>
/// The mapping of diagnostics to their attribute type names.
/// </summary>
private static readonly ImmutableDictionary<string, DiagnosticDescriptor> AttributeTypeNameToDiagnosticMapping = ImmutableDictionary.CreateRange(new[]
{
new KeyValuePair<string, DiagnosticDescriptor>("D2DEffectDisplayNameAttribute", InvalidD2DEffectDisplayNameAttributeValue),
new KeyValuePair<string, DiagnosticDescriptor>("D2DEffectDescriptionAttribute", InvalidD2DEffectDescriptionAttributeValue),
new KeyValuePair<string, DiagnosticDescriptor>("D2DEffectCategoryAttribute", InvalidD2DEffectCategoryAttributeValue),
new KeyValuePair<string, DiagnosticDescriptor>("D2DEffectAuthorAttribute", InvalidD2DEffectAuthorAttributeValue)
});
/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
InvalidD2DEffectDisplayNameAttributeValue,
InvalidD2DEffectDescriptionAttributeValue,
InvalidD2DEffectCategoryAttributeValue,
InvalidD2DEffectAuthorAttributeValue);
/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(static context =>
{
// Get the metadata attribute symbols
if (!context.Compilation.TryBuildNamedTypeSymbolSet(MetadataAttributeTypeNames, out ImmutableHashSet<INamedTypeSymbol>? typeSymbols))
{
return;
}
context.RegisterSymbolAction(context =>
{
// Only struct types and assemblies are possible targets
if (context.Symbol is not (INamedTypeSymbol { TypeKind: TypeKind.Struct } or IAssemblySymbol))
{
return;
}
foreach (AttributeData attributeData in context.Symbol.GetAttributes())
{
// Only check attributes we care about
if (attributeData.AttributeClass is not INamedTypeSymbol attributeSymbol ||
!typeSymbols.Contains(attributeSymbol))
{
continue;
}
// Validate the effect display name
if (!D2D1EffectMetadataParser.IsValidEffectMetadataName(attributeData))
{
context.ReportDiagnostic(Diagnostic.Create(
AttributeTypeNameToDiagnosticMapping[attributeSymbol.MetadataName],
attributeData.GetLocation()));
}
return;
}
}, SymbolKind.NamedType | SymbolKind.Assembly);
});
}
}
+49 -1
ファイルの表示
@@ -877,7 +877,7 @@ partial class DiagnosticDescriptors
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>[D2DEffectDisplayName]</c> attribute is being used with an invalid value.
/// <para>
/// Format: <c>"The [D2DEffectDisplayName] attribute is being used with an invalid value (the input text must be a valid GUID)"</c>.
/// Format: <c>"The [D2DEffectDisplayName] attribute is being used with an invalid value"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidD2DEffectDisplayNameAttributeValue = new DiagnosticDescriptor(
@@ -889,4 +889,52 @@ partial class DiagnosticDescriptors
isEnabledByDefault: true,
description: "The [D2DEffectDisplayName] attribute must contain valid text.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>[D2DEffectDescription]</c> attribute is being used with an invalid value.
/// <para>
/// Format: <c>"The [D2DEffectDescription] attribute is being used with an invalid value"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidD2DEffectDescriptionAttributeValue = new DiagnosticDescriptor(
id: "CMPSD2D0061",
title: "Invalid [D2DEffectDescription] attribute value",
messageFormat: "The [D2DEffectDescription] attribute is being used with an invalid value",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The [D2DEffectDescription] attribute must contain valid text.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>[D2DEffectCategory]</c> attribute is being used with an invalid value.
/// <para>
/// Format: <c>"The [D2DEffectDescription] attribute is being used with an invalid value"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidD2DEffectCategoryAttributeValue = new DiagnosticDescriptor(
id: "CMPSD2D0062",
title: "Invalid [D2DEffectCategory] attribute value",
messageFormat: "The [D2DEffectCategory] attribute is being used with an invalid value",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The [D2DEffectCategory] attribute must contain valid text.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
/// <summary>
/// Gets a <see cref="DiagnosticDescriptor"/> for when the <c>[D2DEffectAuthor]</c> attribute is being used with an invalid value.
/// <para>
/// Format: <c>"The [D2DEffectAuthor] attribute is being used with an invalid value"</c>.
/// </para>
/// </summary>
public static readonly DiagnosticDescriptor InvalidD2DEffectAuthorAttributeValue = new DiagnosticDescriptor(
id: "CMPSD2D0063",
title: "Invalid [D2DEffectAuthor] attribute value",
messageFormat: "The [D2DEffectAuthor] attribute is being used with an invalid value",
category: "ComputeSharp.D2D1.Shaders",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The [D2DEffectAuthor] attribute must contain valid text.",
helpLinkUri: "https://github.com/Sergio0694/ComputeSharp");
}
+13 -6
ファイルの表示
@@ -2,6 +2,7 @@ using System;
using System.Diagnostics.CodeAnalysis;
using System.Security;
using System.Text.RegularExpressions;
using ComputeSharp.SourceGeneration.Extensions;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.D2D1.SourceGenerators.Helpers;
@@ -41,23 +42,29 @@ internal static class D2D1EffectMetadataParser
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
/// <param name="typeSymbol">The input <see cref="INamedTypeSymbol"/> instance.</param>
/// <param name="attributeFullyQualifiedMetadataName">The fully qualified metadata name of the target attribute.</param>
/// <param name="isAssemblyLevelAttributeSupported">Whether the attribute can also be on the assembly.</param>
/// <param name="effectMetadataName">The resulting defined effect metadata name, if found.</param>
/// <returns>Whether or not a defined effect metadata name could be found.</returns>
public static bool TryGetEffectMetadataName(
Compilation compilation,
INamedTypeSymbol typeSymbol,
string attributeFullyQualifiedMetadataName,
bool isAssemblyLevelAttributeSupported,
[NotNullWhen(true)] out string? effectMetadataName)
{
INamedTypeSymbol metadataAttributeSymbol = compilation.GetTypeByMetadataName(attributeFullyQualifiedMetadataName)!;
foreach (AttributeData attributeData in typeSymbol.GetAttributes())
// Always check the attributes on the type first
if (typeSymbol.TryGetAttributeWithType(metadataAttributeSymbol, out AttributeData? attributeData))
{
// Check that the attribute is the one we're looking for
if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, metadataAttributeSymbol))
{
return TryGetEffectMetadataName(attributeData, out effectMetadataName);
}
return TryGetEffectMetadataName(attributeData, out effectMetadataName);
}
// Check assembly-level attributes next, if needed
if (isAssemblyLevelAttributeSupported &&
typeSymbol.ContainingAssembly!.TryGetAttributeWithType(metadataAttributeSymbol, out attributeData))
{
return TryGetEffectMetadataName(attributeData, out effectMetadataName);
}
effectMetadataName = default;
@@ -23,6 +23,7 @@ partial class ID2D1ShaderGenerator
compilation,
structDeclarationSymbol,
"ComputeSharp.D2D1.D2DEffectDisplayNameAttribute",
isAssemblyLevelAttributeSupported: false,
out string? effectDisplayName))
{
return effectDisplayName;
@@ -43,6 +44,7 @@ partial class ID2D1ShaderGenerator
compilation,
structDeclarationSymbol,
"ComputeSharp.D2D1.D2DEffectDescriptionAttribute",
isAssemblyLevelAttributeSupported: false,
out string? effectDescription))
{
return effectDescription;
@@ -63,6 +65,7 @@ partial class ID2D1ShaderGenerator
compilation,
structDeclarationSymbol,
"ComputeSharp.D2D1.D2DEffectCategoryAttribute",
isAssemblyLevelAttributeSupported: true,
out string? effectCategory))
{
return effectCategory;
@@ -83,6 +86,7 @@ partial class ID2D1ShaderGenerator
compilation,
structDeclarationSymbol,
"ComputeSharp.D2D1.D2DEffectAuthorAttribute",
isAssemblyLevelAttributeSupported: true,
out string? effectAuthor))
{
return effectAuthor;
+1 -1
ファイルの表示
@@ -8,7 +8,7 @@ namespace ComputeSharp.D2D1;
/// <remarks>
/// This only applies to effects created from <see cref="Interop.D2D1PixelShaderEffect"/>.
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)]
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = false)]
public sealed class D2DEffectAuthorAttribute : Attribute
{
/// <summary>
+1 -1
ファイルの表示
@@ -8,7 +8,7 @@ namespace ComputeSharp.D2D1;
/// <remarks>
/// This only applies to effects created from <see cref="Interop.D2D1PixelShaderEffect"/>.
/// </remarks>
[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)]
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Assembly, AllowMultiple = false)]
public sealed class D2DEffectCategoryAttribute : Attribute
{
/// <summary>
+34
ファイルの表示
@@ -2,6 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
namespace ComputeSharp.SourceGeneration.Extensions;
@@ -11,6 +14,37 @@ namespace ComputeSharp.SourceGeneration.Extensions;
/// </summary>
internal static class CompilationExtensions
{
/// <summary>
/// Tries to build a set of <see cref="INamedTypeSymbol"/> instances form the input mapping of names.
/// </summary>
/// <param name="compilation">The <see cref="Compilation"/> to consider for analysis.</param>
/// <param name="fullyQualifiedTypeNames">The input fully qualified type names.</param>
/// <param name="typeSymbols">The resulting mapping of resolved <see cref="INamedTypeSymbol"/> instances.</param>
/// <returns>Whether all requested <see cref="INamedTypeSymbol"/> instances could be resolved.</returns>
public static bool TryBuildNamedTypeSymbolSet(
this Compilation compilation,
IEnumerable<string> fullyQualifiedTypeNames,
[NotNullWhen(true)] out ImmutableHashSet<INamedTypeSymbol>? typeSymbols)
{
ImmutableHashSet<INamedTypeSymbol>.Builder builder = ImmutableHashSet.CreateBuilder<INamedTypeSymbol>(SymbolEqualityComparer.Default);
foreach (string fullyQualifiedTypeName in fullyQualifiedTypeNames)
{
if (compilation.GetTypeByMetadataName(fullyQualifiedTypeName) is not INamedTypeSymbol typeSymbol)
{
typeSymbols = null;
return false;
}
_ = builder.Add(typeSymbol);
}
typeSymbols = builder.ToImmutable();
return true;
}
/// <summary>
/// <para>
/// Checks whether or not a type with a specified metadata name is accessible from a given <see cref="Compilation"/> instance.
+24
ファイルの表示
@@ -27,6 +27,30 @@ internal static class ISymbolExtensions
return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat);
}
/// <summary>
/// Tries to get an attribute with the specified type.
/// </summary>
/// <param name="symbol">The input <see cref="ISymbol"/> instance to check.</param>
/// <param name="typeSymbol">The <see cref="ITypeSymbol"/> instance for the attribute type to look for.</param>
/// <param name="attributeData">The resulting attribute, if it was found.</param>
/// <returns>Whether or not <paramref name="symbol"/> has an attribute with the specified name.</returns>
public static bool TryGetAttributeWithType(this ISymbol symbol, ITypeSymbol typeSymbol, [NotNullWhen(true)] out AttributeData? attributeData)
{
foreach (AttributeData attribute in symbol.GetAttributes())
{
if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, typeSymbol))
{
attributeData = attribute;
return true;
}
}
attributeData = null;
return false;
}
/// <summary>
/// Tries to get an attribute with the specified fully qualified metadata name.
/// </summary>