Skip to content

Update analyzer to use FeatureGuardAttribute #99340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public override FeatureChecksValue VisitPropertyReference (IPropertyReferenceOpe
// A single property may serve as a feature check for multiple features.
FeatureChecksValue featureChecks = FeatureChecksValue.None;
foreach (var analyzer in _dataFlowAnalyzerContext.EnabledRequiresAnalyzers) {
if (analyzer.IsFeatureCheck (operation.Property, _dataFlowAnalyzerContext.Compilation)) {
if (analyzer.IsFeatureGuard (operation.Property, _dataFlowAnalyzerContext.Compilation)) {
var featureCheck = new FeatureChecksValue (analyzer.RequiresAttributeFullyQualifiedName);
featureChecks = featureChecks.And (featureCheck);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public LocalDataFlowVisitor (
var current = state.Current;
HandleReturnValue (branchValue, branchValueOperation, in current.Context);
// Must be called for every return value even if it did not return an understood condition,
// because the non-understood conditions will produce warnings for FeatureCheck properties.
// because the non-understood conditions will produce warnings for FeatureGuard properties.
HandleReturnConditionValue (conditionValue, branchValueOperation);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public class DynamicallyAccessedMembersAnalyzer : DiagnosticAnalyzer
internal const string DynamicallyAccessedMembersAttribute = nameof (DynamicallyAccessedMembersAttribute);
public const string attributeArgument = "attributeArgument";
public const string FullyQualifiedDynamicallyAccessedMembersAttribute = "System.Diagnostics.CodeAnalysis." + DynamicallyAccessedMembersAttribute;
public const string FullyQualifiedFeatureCheckAttribute = "System.Diagnostics.CodeAnalysis.FeatureCheckAttribute";
public const string FullyQualifiedFeatureDependsOnAttribute = "System.Diagnostics.CodeAnalysis.FeatureDependsOnAttribute";
public const string FullyQualifiedFeatureGuardAttribute = "System.Diagnostics.CodeAnalysis.FeatureGuardAttribute";
public static Lazy<ImmutableArray<RequiresAnalyzerBase>> RequiresAnalyzers { get; } = new Lazy<ImmutableArray<RequiresAnalyzerBase>> (GetRequiresAnalyzers);
static ImmutableArray<RequiresAnalyzerBase> GetRequiresAnalyzers () =>
ImmutableArray.Create<RequiresAnalyzerBase> (
Expand Down Expand Up @@ -53,8 +52,8 @@ public static ImmutableArray<DiagnosticDescriptor> GetSupportedDiagnostics ()
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.UnrecognizedTypeNameInTypeGetType));
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.UnrecognizedParameterInMethodCreateInstance));
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.ParametersOfAssemblyCreateInstanceCannotBeAnalyzed));
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.ReturnValueDoesNotMatchFeatureChecks));
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.InvalidFeatureCheck));
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.ReturnValueDoesNotMatchFeatureGuards));
diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.InvalidFeatureGuard));

foreach (var requiresAnalyzer in RequiresAnalyzers.Value) {
foreach (var diagnosticDescriptor in requiresAnalyzer.SupportedDiagnostics)
Expand Down
18 changes: 3 additions & 15 deletions src/tools/illink/src/ILLink.RoslynAnalyzer/ISymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,14 @@ internal static DynamicallyAccessedMemberTypes GetDynamicallyAccessedMemberTypes
return (DynamicallyAccessedMemberTypes) dynamicallyAccessedMembers.ConstructorArguments[0].Value!;
}

internal static ValueSet<string> GetFeatureCheckAnnotations (this IPropertySymbol propertySymbol)
internal static ValueSet<string> GetFeatureGuardAnnotations (this IPropertySymbol propertySymbol)
{
HashSet<string> featureSet = new ();
foreach (var attributeData in propertySymbol.GetAttributes (DynamicallyAccessedMembersAnalyzer.FullyQualifiedFeatureCheckAttribute)) {
foreach (var attributeData in propertySymbol.GetAttributes (DynamicallyAccessedMembersAnalyzer.FullyQualifiedFeatureGuardAttribute)) {
if (attributeData.ConstructorArguments is [TypedConstant { Value: INamedTypeSymbol featureType }])
AddFeatures (featureType);
featureSet.Add (featureType.GetDisplayName ());
}
return featureSet.Count == 0 ? ValueSet<string>.Empty : new ValueSet<string> (featureSet);

void AddFeatures (INamedTypeSymbol featureType) {
var featureName = featureType.GetDisplayName ();
if (!featureSet.Add (featureName))
return;

// Look at FeatureDependsOn attributes on the feature type.
foreach (var featureTypeAttributeData in featureType.GetAttributes (DynamicallyAccessedMembersAnalyzer.FullyQualifiedFeatureDependsOnAttribute)) {
if (featureTypeAttributeData.ConstructorArguments is [TypedConstant { Value: INamedTypeSymbol featureTypeSymbol }])
AddFeatures (featureTypeSymbol);
}
}
}

internal static bool TryGetReturnAttribute (this IMethodSymbol member, string attributeName, [NotNullWhen (returnValue: true)] out AttributeData? attribute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,19 +302,19 @@ protected virtual bool CreateSpecialIncompatibleMembersDiagnostic (
// - custom feature checks defined in library code
private protected virtual bool IsRequiresCheck (IPropertySymbol propertySymbol, Compilation compilation) => false;

internal static bool IsAnnotatedFeatureCheck (IPropertySymbol propertySymbol, string featureName)
internal static bool IsAnnotatedFeatureGuard (IPropertySymbol propertySymbol, string featureName)
{
// Only respect FeatureCheckAttribute on static boolean properties.
// Only respect FeatureGuardAttribute on static boolean properties.
if (!propertySymbol.IsStatic || propertySymbol.Type.SpecialType != SpecialType.System_Boolean)
return false;

ValueSet<string> featureCheckAnnotations = propertySymbol.GetFeatureCheckAnnotations ();
ValueSet<string> featureCheckAnnotations = propertySymbol.GetFeatureGuardAnnotations ();
return featureCheckAnnotations.Contains (featureName);
}

internal bool IsFeatureCheck (IPropertySymbol propertySymbol, Compilation compilation)
internal bool IsFeatureGuard (IPropertySymbol propertySymbol, Compilation compilation)
{
return IsAnnotatedFeatureCheck (propertySymbol, RequiresAttributeFullyQualifiedName)
return IsAnnotatedFeatureGuard (propertySymbol, RequiresAttributeFullyQualifiedName)
|| IsRequiresCheck (propertySymbol, compilation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public IEnumerable<Diagnostic> CollectDiagnostics (DataFlowAnalyzerContext conte
if (!OwningSymbol.IsStatic || OwningSymbol.Type.SpecialType != SpecialType.System_Boolean) {
// Warn about invalid feature checks (non-static or non-bool properties)
diagnosticContext.AddDiagnostic (
DiagnosticId.InvalidFeatureCheck);
DiagnosticId.InvalidFeatureGuard);
return diagnosticContext.Diagnostics;
}

Expand All @@ -57,7 +57,7 @@ public IEnumerable<Diagnostic> CollectDiagnostics (DataFlowAnalyzerContext conte

if (!returnValueFeatures.Contains (feature)) {
diagnosticContext.AddDiagnostic (
DiagnosticId.ReturnValueDoesNotMatchFeatureChecks,
DiagnosticId.ReturnValueDoesNotMatchFeatureGuards,
OwningSymbol.GetDisplayName (),
feature);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,12 @@ public override void HandleReturnConditionValue (FeatureChecksValue returnCondit
if (OwningSymbol is not IMethodSymbol method)
return;

// FeatureCheck validation needs to happen only for properties.
// FeatureGuard validation needs to happen only for properties.
if (method.MethodKind != MethodKind.PropertyGet)
return;

IPropertySymbol propertySymbol = (IPropertySymbol) method.AssociatedSymbol!;
var featureCheckAnnotations = propertySymbol.GetFeatureCheckAnnotations ();
var featureCheckAnnotations = propertySymbol.GetFeatureGuardAnnotations ();

// If there are no feature checks, there is nothing to validate.
if (featureCheckAnnotations.IsEmpty())
Expand Down
4 changes: 2 additions & 2 deletions src/tools/illink/src/ILLink.Shared/DiagnosticId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ public enum DiagnosticId
RequiresDynamicCodeOnStaticConstructor = 3056,

// Feature guard diagnostic ids.
ReturnValueDoesNotMatchFeatureChecks = 4000,
InvalidFeatureCheck = 4001
ReturnValueDoesNotMatchFeatureGuards = 4000,
InvalidFeatureGuard = 4001
}

public static class DiagnosticIdExtensions
Expand Down
16 changes: 8 additions & 8 deletions src/tools/illink/src/ILLink.Shared/SharedStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1197,16 +1197,16 @@
<data name="RedundantSuppressionTitle" xml:space="preserve">
<value>Unused 'UnconditionalSuppressMessageAttribute' found. Consider removing the unused warning suppression.</value>
</data>
<data name="ReturnValueDoesNotMatchFeatureChecksMessage" xml:space="preserve">
<value>Return value does not match FeatureCheckAttribute '{1}'.</value>
<data name="ReturnValueDoesNotMatchFeatureGuardsMessage" xml:space="preserve">
<value>Return value does not match FeatureGuardAttribute '{1}'.</value>
</data>
<data name="ReturnValueDoesNotMatchFeatureChecksTitle" xml:space="preserve">
<value>Return value does not match FeatureCheck annotations of the property. The check should return false whenever any of the features referenced in the FeatureCheck annotations is disabled.</value>
<data name="ReturnValueDoesNotMatchFeatureGuardsTitle" xml:space="preserve">
<value>Return value does not match FeatureGuard annotations of the property. The check should return false whenever any of the features referenced in the FeatureGuard annotations is disabled.</value>
</data>
<data name="InvalidFeatureCheckMessage" xml:space="preserve">
<value>Invalid FeatureCheckAttribute. The attribute must be placed on a static boolean property with only a 'get' accessor.</value>
<data name="InvalidFeatureGuardMessage" xml:space="preserve">
<value>Invalid FeatureGuardAttribute. The attribute must be placed on a static boolean property with only a 'get' accessor.</value>
</data>
<data name="InvalidFeatureCheckTitle" xml:space="preserve">
<value>Invalid FeatureCheckAttribute.</value>
<data name="InvalidFeatureGuardTitle" xml:space="preserve">
<value>Invalid FeatureGuardAttribute.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public Task FeatureCheckDataFlow ()
}

[Fact]
public Task FeatureCheckAttributeDataFlow ()
public Task FeatureGuardAttributeDataFlow ()
{
return RunTest ();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

namespace System.Diagnostics.CodeAnalysis
{
// Allow AttributeTargets.Method for testing invalid usages of a custom FeatureCheckAttribute
[AttributeUsage (AttributeTargets.Property | AttributeTargets.Method, Inherited=false)]
public sealed class FeatureCheckAttribute : Attribute
// Allow AttributeTargets.Method for testing invalid usages of a custom FeatureGuardAttribute
[AttributeUsage (AttributeTargets.Property | AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
public sealed class FeatureGuardAttribute : Attribute
{
public Type FeatureType { get; }

public FeatureCheckAttribute (Type featureType)
public FeatureGuardAttribute (Type featureType)
{
FeatureType = featureType;
}
Expand Down
Loading