|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Collections.Immutable; |
| 8 | +using ILLink.Shared; |
| 9 | +using Microsoft.CodeAnalysis; |
| 10 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 11 | +using Microsoft.CodeAnalysis.Operations; |
| 12 | + |
| 13 | +namespace ILLink.RoslynAnalyzer |
| 14 | +{ |
| 15 | + [DiagnosticAnalyzer (LanguageNames.CSharp)] |
| 16 | + public class DynamicallyAccessedMembersAnalyzer : DiagnosticAnalyzer |
| 17 | + { |
| 18 | + internal const string DynamicallyAccessedMembers = nameof (DynamicallyAccessedMembers); |
| 19 | + internal const string DynamicallyAccessedMembersAttribute = nameof (DynamicallyAccessedMembersAttribute); |
| 20 | + |
| 21 | + static ImmutableArray<DiagnosticDescriptor> GetSupportedDiagnostics () |
| 22 | + { |
| 23 | + var diagDescriptorsArrayBuilder = ImmutableArray.CreateBuilder<DiagnosticDescriptor> (23); |
| 24 | + for (int i = (int) DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsParameter; |
| 25 | + i <= (int) DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsGenericParameter; i++) { |
| 26 | + diagDescriptorsArrayBuilder.Add (DiagnosticDescriptors.GetDiagnosticDescriptor ((DiagnosticId) i)); |
| 27 | + } |
| 28 | + |
| 29 | + return diagDescriptorsArrayBuilder.ToImmutable (); |
| 30 | + } |
| 31 | + |
| 32 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => GetSupportedDiagnostics (); |
| 33 | + |
| 34 | + public override void Initialize (AnalysisContext context) |
| 35 | + { |
| 36 | + context.EnableConcurrentExecution (); |
| 37 | + context.ConfigureGeneratedCodeAnalysis (GeneratedCodeAnalysisFlags.ReportDiagnostics); |
| 38 | + context.RegisterOperationAction (operationAnalysisContext => { |
| 39 | + var assignmentOperation = (IAssignmentOperation) operationAnalysisContext.Operation; |
| 40 | + ProcessAssignmentOperation (operationAnalysisContext, assignmentOperation); |
| 41 | + }, OperationKind.SimpleAssignment); |
| 42 | + |
| 43 | + context.RegisterOperationAction (operationAnalysisContext => { |
| 44 | + var invocationOperation = (IInvocationOperation) operationAnalysisContext.Operation; |
| 45 | + ProcessInvocationOperation (operationAnalysisContext, invocationOperation); |
| 46 | + }, OperationKind.Invocation); |
| 47 | + |
| 48 | + context.RegisterOperationAction (operationAnalysisContext => { |
| 49 | + var returnOperation = (IReturnOperation) operationAnalysisContext.Operation; |
| 50 | + ProcessReturnOperation (operationAnalysisContext, returnOperation); |
| 51 | + }, OperationKind.Return); |
| 52 | + } |
| 53 | + |
| 54 | + static void ProcessAssignmentOperation (OperationAnalysisContext context, IAssignmentOperation assignmentOperation) |
| 55 | + { |
| 56 | + if (context.ContainingSymbol.HasAttribute (RequiresUnreferencedCodeAnalyzer.FullyQualifiedRequiresUnreferencedCodeAttribute)) |
| 57 | + return; |
| 58 | + |
| 59 | + if (TryGetSymbolFromOperation (assignmentOperation.Target) is not ISymbol target || |
| 60 | + TryGetSymbolFromOperation (assignmentOperation.Value) is not ISymbol source) |
| 61 | + return; |
| 62 | + |
| 63 | + if (!target.TryGetDynamicallyAccessedMemberTypes (out var damtOnTarget)) |
| 64 | + return; |
| 65 | + |
| 66 | + source.TryGetDynamicallyAccessedMemberTypes (out var damtOnSource); |
| 67 | + if (Annotations.SourceHasRequiredAnnotations (damtOnSource, damtOnTarget, out var missingAnnotations)) |
| 68 | + return; |
| 69 | + |
| 70 | + var diag = GetDiagnosticId (source.Kind, target.Kind); |
| 71 | + var diagArgs = GetDiagnosticArguments (source.Kind == SymbolKind.NamedType ? context.ContainingSymbol : source, target, missingAnnotations); |
| 72 | + context.ReportDiagnostic (Diagnostic.Create (DiagnosticDescriptors.GetDiagnosticDescriptor (diag), assignmentOperation.Syntax.GetLocation (), diagArgs)); |
| 73 | + } |
| 74 | + |
| 75 | + static void ProcessInvocationOperation (OperationAnalysisContext context, IInvocationOperation invocationOperation) |
| 76 | + { |
| 77 | + if (context.ContainingSymbol.HasAttribute (RequiresUnreferencedCodeAnalyzer.FullyQualifiedRequiresUnreferencedCodeAttribute)) |
| 78 | + return; |
| 79 | + |
| 80 | + ProcessTypeArguments (context, invocationOperation); |
| 81 | + ProcessArguments (context, invocationOperation); |
| 82 | + if (!invocationOperation.TargetMethod.TryGetDynamicallyAccessedMemberTypes (out var damtOnCalledMethod)) |
| 83 | + return; |
| 84 | + |
| 85 | + if (TryGetSymbolFromOperation (invocationOperation.Instance) is ISymbol instance && |
| 86 | + !instance.HasAttribute (RequiresUnreferencedCodeAnalyzer.FullyQualifiedRequiresUnreferencedCodeAttribute)) { |
| 87 | + instance!.TryGetDynamicallyAccessedMemberTypes (out var damtOnCaller); |
| 88 | + if (Annotations.SourceHasRequiredAnnotations (damtOnCaller, damtOnCalledMethod, out var missingAnnotations)) |
| 89 | + return; |
| 90 | + |
| 91 | + var diag = GetDiagnosticId (instance!.Kind, invocationOperation.TargetMethod.Kind); |
| 92 | + var diagArgs = GetDiagnosticArguments (instance.Kind == SymbolKind.NamedType ? context.ContainingSymbol : instance, invocationOperation.TargetMethod, missingAnnotations); |
| 93 | + context.ReportDiagnostic (Diagnostic.Create (DiagnosticDescriptors.GetDiagnosticDescriptor (diag), invocationOperation.Syntax.GetLocation (), diagArgs)); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + static void ProcessReturnOperation (OperationAnalysisContext context, IReturnOperation returnOperation) |
| 98 | + { |
| 99 | + if (!context.ContainingSymbol.TryGetDynamicallyAccessedMemberTypesOnReturnType (out var damtOnReturnType) || |
| 100 | + context.ContainingSymbol.HasAttribute (RequiresUnreferencedCodeAnalyzer.FullyQualifiedRequiresUnreferencedCodeAttribute)) |
| 101 | + return; |
| 102 | + |
| 103 | + if (TryGetSymbolFromOperation (returnOperation) is not ISymbol returnedSymbol) |
| 104 | + return; |
| 105 | + |
| 106 | + returnedSymbol.TryGetDynamicallyAccessedMemberTypes (out var damtOnReturnedValue); |
| 107 | + if (Annotations.SourceHasRequiredAnnotations (damtOnReturnedValue, damtOnReturnType, out var missingAnnotations)) |
| 108 | + return; |
| 109 | + |
| 110 | + var diag = GetDiagnosticId (returnedSymbol.Kind, context.ContainingSymbol.Kind, true); |
| 111 | + var diagArgs = GetDiagnosticArguments (returnedSymbol.Kind == SymbolKind.NamedType ? context.ContainingSymbol : returnedSymbol, context.ContainingSymbol, missingAnnotations); |
| 112 | + context.ReportDiagnostic (Diagnostic.Create (DiagnosticDescriptors.GetDiagnosticDescriptor (diag), returnOperation.Syntax.GetLocation (), diagArgs)); |
| 113 | + } |
| 114 | + |
| 115 | + static void ProcessArguments (OperationAnalysisContext context, IInvocationOperation invocationOperation) |
| 116 | + { |
| 117 | + if (context.ContainingSymbol.HasAttribute (RequiresUnreferencedCodeAnalyzer.FullyQualifiedRequiresUnreferencedCodeAttribute)) |
| 118 | + return; |
| 119 | + |
| 120 | + foreach (var argument in invocationOperation.Arguments) { |
| 121 | + var targetParameter = argument.Parameter; |
| 122 | + if (targetParameter is null || !targetParameter.TryGetDynamicallyAccessedMemberTypes (out var damtOnParameter)) |
| 123 | + return; |
| 124 | + |
| 125 | + bool sourceIsMethodReturnType = argument.Value.Kind != OperationKind.Conversion; |
| 126 | + ISymbol sourceArgument = sourceIsMethodReturnType ? TryGetSymbolFromOperation (argument.Value)! : context.ContainingSymbol; |
| 127 | + if (sourceArgument is null) |
| 128 | + return; |
| 129 | + |
| 130 | + sourceArgument.TryGetDynamicallyAccessedMemberTypes (out var damtOnArgument); |
| 131 | + if (Annotations.SourceHasRequiredAnnotations (damtOnArgument, damtOnParameter, out var missingAnnotations)) |
| 132 | + return; |
| 133 | + |
| 134 | + IMethodSymbol? callerMethod = null; |
| 135 | + // This can only happen within methods of System.Type and derived types. |
| 136 | + if (sourceArgument.Kind == SymbolKind.Method && !sourceIsMethodReturnType) { |
| 137 | + callerMethod = (IMethodSymbol?) sourceArgument; |
| 138 | + sourceArgument = sourceArgument.ContainingType; |
| 139 | + } |
| 140 | + |
| 141 | + var diag = GetDiagnosticId (sourceArgument.Kind, targetParameter.Kind); |
| 142 | + var diagArgs = GetDiagnosticArguments (callerMethod ?? sourceArgument, targetParameter, missingAnnotations); |
| 143 | + context.ReportDiagnostic (Diagnostic.Create (DiagnosticDescriptors.GetDiagnosticDescriptor (diag), argument.Syntax.GetLocation (), diagArgs)); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + static void ProcessTypeArguments (OperationAnalysisContext context, IInvocationOperation invocationOperation) |
| 148 | + { |
| 149 | + var targetMethod = invocationOperation.TargetMethod; |
| 150 | + if (targetMethod.HasAttribute (RequiresUnreferencedCodeAnalyzer.FullyQualifiedRequiresUnreferencedCodeAttribute)) |
| 151 | + return; |
| 152 | + |
| 153 | + for (int i = 0; i < targetMethod.TypeParameters.Length; i++) { |
| 154 | + var arg = targetMethod.TypeArguments[i]; |
| 155 | + var param = targetMethod.TypeParameters[i]; |
| 156 | + if (!param.TryGetDynamicallyAccessedMemberTypes (out var damtOnTypeParameter)) |
| 157 | + continue; |
| 158 | + |
| 159 | + arg.TryGetDynamicallyAccessedMemberTypes (out var damtOnTypeArgument); |
| 160 | + if (Annotations.SourceHasRequiredAnnotations (damtOnTypeArgument, damtOnTypeParameter, out var missingAnnotations)) |
| 161 | + continue; |
| 162 | + |
| 163 | + var diag = GetDiagnosticId (arg.Kind, param.Kind); |
| 164 | + var diagArgs = GetDiagnosticArguments (arg, param, missingAnnotations); |
| 165 | + context.ReportDiagnostic (Diagnostic.Create (DiagnosticDescriptors.GetDiagnosticDescriptor (diag), invocationOperation.Syntax.GetLocation (), diagArgs)); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + static DiagnosticId GetDiagnosticId (SymbolKind source, SymbolKind target, bool targetIsMethodReturnType = false) |
| 170 | + => (source, target) switch { |
| 171 | + (SymbolKind.Parameter, SymbolKind.Field) => DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsField, |
| 172 | + (SymbolKind.Parameter, SymbolKind.Method) => targetIsMethodReturnType ? |
| 173 | + DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsMethodReturnType : |
| 174 | + DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsThisParameter, |
| 175 | + (SymbolKind.Parameter, SymbolKind.Parameter) => DiagnosticId.DynamicallyAccessedMembersMismatchParameterTargetsParameter, |
| 176 | + (SymbolKind.Field, SymbolKind.Parameter) => DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsParameter, |
| 177 | + (SymbolKind.Field, SymbolKind.Field) => DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsField, |
| 178 | + (SymbolKind.Field, SymbolKind.Method) => targetIsMethodReturnType ? |
| 179 | + DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsMethodReturnType : |
| 180 | + DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsThisParameter, |
| 181 | + (SymbolKind.Field, SymbolKind.TypeParameter) => DiagnosticId.DynamicallyAccessedMembersMismatchFieldTargetsGenericParameter, |
| 182 | + (SymbolKind.NamedType, SymbolKind.Method) => targetIsMethodReturnType ? |
| 183 | + DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsMethodReturnType : |
| 184 | + DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsThisParameter, |
| 185 | + (SymbolKind.Method, SymbolKind.Field) => DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsField, |
| 186 | + (SymbolKind.Method, SymbolKind.Method) => targetIsMethodReturnType ? |
| 187 | + DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsMethodReturnType : |
| 188 | + DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsThisParameter, |
| 189 | + // Source here will always be a method's return type. |
| 190 | + (SymbolKind.Method, SymbolKind.Parameter) => DiagnosticId.DynamicallyAccessedMembersMismatchMethodReturnTypeTargetsParameter, |
| 191 | + (SymbolKind.NamedType, SymbolKind.Field) => DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsField, |
| 192 | + (SymbolKind.NamedType, SymbolKind.Parameter) => DiagnosticId.DynamicallyAccessedMembersMismatchThisParameterTargetsParameter, |
| 193 | + (SymbolKind.TypeParameter, SymbolKind.Field) => DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsField, |
| 194 | + (SymbolKind.TypeParameter, SymbolKind.Method) => DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsMethodReturnType, |
| 195 | + (SymbolKind.TypeParameter, SymbolKind.Parameter) => DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsParameter, |
| 196 | + (SymbolKind.TypeParameter, SymbolKind.TypeParameter) => DiagnosticId.DynamicallyAccessedMembersMismatchTypeArgumentTargetsGenericParameter, |
| 197 | + _ => throw new NotImplementedException () |
| 198 | + }; |
| 199 | + |
| 200 | + static string[] GetDiagnosticArguments (ISymbol source, ISymbol target, string missingAnnotations) |
| 201 | + { |
| 202 | + var args = new List<string> (); |
| 203 | + args.AddRange (GetDiagnosticArguments (target)); |
| 204 | + args.AddRange (GetDiagnosticArguments (source)); |
| 205 | + args.Add (missingAnnotations); |
| 206 | + return args.ToArray (); |
| 207 | + } |
| 208 | + |
| 209 | + static IEnumerable<string> GetDiagnosticArguments (ISymbol symbol) |
| 210 | + { |
| 211 | + var args = new List<string> (); |
| 212 | + args.AddRange (symbol.Kind switch { |
| 213 | + SymbolKind.Parameter => new string[] { symbol.GetDisplayName (), symbol.ContainingSymbol.GetDisplayName () }, |
| 214 | + SymbolKind.NamedType => new string[] { symbol.GetDisplayName () }, |
| 215 | + SymbolKind.Field => new string[] { symbol.GetDisplayName () }, |
| 216 | + SymbolKind.Method => new string[] { symbol.GetDisplayName () }, |
| 217 | + SymbolKind.TypeParameter => new string[] { symbol.GetDisplayName (), symbol.ContainingSymbol.GetDisplayName () }, |
| 218 | + _ => throw new NotImplementedException ($"Unsupported source or target symbol {symbol}.") |
| 219 | + }); |
| 220 | + |
| 221 | + return args; |
| 222 | + } |
| 223 | + |
| 224 | + static ISymbol? TryGetSymbolFromOperation (IOperation? operation) => |
| 225 | + operation switch { |
| 226 | + IArgumentOperation argument => TryGetSymbolFromOperation (argument.Value), |
| 227 | + IAssignmentOperation assignment => TryGetSymbolFromOperation (assignment.Value), |
| 228 | + IConversionOperation conversion => TryGetSymbolFromOperation (conversion.Operand), |
| 229 | + // Represents an implicit/explicit reference to an instance. We grab the result type of this operation. |
| 230 | + // In cases where this is an implicit reference, this will result in returning the actual type of the |
| 231 | + // instance that 'this' represents. |
| 232 | + IInstanceReferenceOperation instanceReference => instanceReference.Type, |
| 233 | + // The target method of an invocation represents the called method. If this invocation is found within |
| 234 | + // a return operation, this representes the returned value, which might be annotated. |
| 235 | + IInvocationOperation invocation => invocation.TargetMethod, |
| 236 | + IMemberReferenceOperation memberReference => memberReference.Member, |
| 237 | + IParameterReferenceOperation parameterReference => parameterReference.Parameter, |
| 238 | + IReturnOperation returnOp => TryGetSymbolFromOperation (returnOp.ReturnedValue), |
| 239 | + ITypeOfOperation typeOf => typeOf.TypeOperand, |
| 240 | + _ => null |
| 241 | + }; |
| 242 | + } |
| 243 | +} |
0 commit comments