-
Notifications
You must be signed in to change notification settings - Fork 128
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
Warn on RUC annotated ctors invoked through new()
constraint in analyzer
#2254
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,11 @@ | |
|
||
using System; | ||
using System.Collections.Immutable; | ||
using System.Diagnostics.CodeAnalysis; | ||
using ILLink.Shared; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Operations; | ||
|
||
namespace ILLink.RoslynAnalyzer | ||
{ | ||
|
@@ -31,6 +33,49 @@ public sealed class RequiresUnreferencedCodeAnalyzer : RequiresAnalyzerBase | |
operationContext.Operation.Syntax.GetLocation ())); | ||
}; | ||
|
||
[SuppressMessage ("MicrosoftCodeAnalysisPerformance", "RS1008", | ||
Justification = "This action is registered through a compilation start action, so that the instances which " + | ||
"can register this operation action will not outlive a compilation's lifetime, avoiding the possibility of " + | ||
"this data causing stale compilations to remain in memory.")] | ||
static readonly Action<OperationAnalysisContext> s_constructorConstraint = operationContext => { | ||
if (FindContainingSymbol (operationContext, DiagnosticTargets.All) is not ISymbol containingSymbol || | ||
containingSymbol.HasAttribute (RequiresUnreferencedCodeAttribute)) | ||
return; | ||
|
||
var typeParams = ImmutableArray<ITypeParameterSymbol>.Empty; | ||
var typeArgs = ImmutableArray<ITypeSymbol>.Empty; | ||
if (operationContext.Operation is IObjectCreationOperation objectCreationOperation && | ||
objectCreationOperation.Type is INamedTypeSymbol objectType && objectType.IsGenericType) { | ||
typeParams = objectType.TypeParameters; | ||
typeArgs = objectType.TypeArguments; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This only reacts to calling a constructor on a generic type. static class MyTest<T> where T : new ()
{
static void DoNothing() {}
}
void Test()
{
// IL2026
MyTest.DoNothing<ClassWithRUCOnCtor>();
} I must admit that I don't know enough about analyzers to tell what type of solution would be the best here. Linker will warn anywhere the generic is instantiated. Even cases like This is obviously not that common, so it's OK to solve this later... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved the analysis context used here to a |
||
} else if (operationContext.Operation is IInvocationOperation invocationOperation && | ||
invocationOperation.TargetMethod is IMethodSymbol targetMethod && targetMethod.IsGenericMethod) { | ||
typeParams = targetMethod.TypeParameters; | ||
typeArgs = targetMethod.TypeArguments; | ||
} | ||
mateoatr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for (int i = 0; i < typeParams.Length; i++) { | ||
var typeParameter = typeParams[i]; | ||
var typeArgument = typeArgs[i]; | ||
if (!typeParameter.HasConstructorConstraint) | ||
continue; | ||
|
||
var typeArgCtors = ((INamedTypeSymbol) typeArgument).InstanceConstructors; | ||
foreach (var instanceCtor in typeArgCtors) { | ||
if (instanceCtor.Arity > 0) | ||
continue; | ||
|
||
if (instanceCtor.TryGetAttribute (RequiresUnreferencedCodeAttribute, out var requiresUnreferencedCodeAttribute)) { | ||
operationContext.ReportDiagnostic (Diagnostic.Create (s_requiresUnreferencedCodeRule, | ||
operationContext.Operation.Syntax.GetLocation (), | ||
containingSymbol.GetDisplayName (), | ||
(string) requiresUnreferencedCodeAttribute.ConstructorArguments[0].Value!, | ||
GetUrlFromAttribute (requiresUnreferencedCodeAttribute))); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => | ||
ImmutableArray.Create (s_dynamicTypeInvocationRule, s_requiresUnreferencedCodeRule, s_requiresUnreferencedCodeAttributeMismatch); | ||
|
||
|
@@ -47,8 +92,15 @@ public sealed class RequiresUnreferencedCodeAnalyzer : RequiresAnalyzerBase | |
protected override bool IsAnalyzerEnabled (AnalyzerOptions options, Compilation compilation) => | ||
options.IsMSBuildPropertyValueTrue (MSBuildPropertyOptionNames.EnableTrimAnalyzer, compilation); | ||
|
||
private protected override ImmutableArray<(Action<OperationAnalysisContext> Action, OperationKind[] OperationKind)> ExtraOperationActions => | ||
ImmutableArray.Create ((s_dynamicTypeInvocation, new OperationKind[] { OperationKind.DynamicInvocation })); | ||
private protected override ImmutableArray<(Action<OperationAnalysisContext> Action, OperationKind[] OperationKind)> ExtraOperationActions { | ||
get { | ||
var diagsBuilder = ImmutableArray.CreateBuilder<(Action<OperationAnalysisContext>, OperationKind[])> (); | ||
diagsBuilder.Add ((s_dynamicTypeInvocation, new OperationKind[] { OperationKind.DynamicInvocation })); | ||
diagsBuilder.Add ((s_constructorConstraint, new OperationKind[] { OperationKind.Invocation, OperationKind.ObjectCreation })); | ||
|
||
return diagsBuilder.ToImmutable (); | ||
} | ||
} | ||
|
||
protected override bool VerifyAttributeArguments (AttributeData attribute) => | ||
attribute.ConstructorArguments.Length >= 1 && attribute.ConstructorArguments[0] is { Type: { SpecialType: SpecialType.System_String } } ctorArg; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please explain why this action causes the warning and the one above doesn't? I don't see how they're really different...