public override void Initialize(AnalysisContext context) { if (!System.Diagnostics.Debugger.IsAttached) { context.EnableConcurrentExecution(); } context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.ReportDiagnostics); context.RegisterCompilationStartAction(context => { if (!context.Options.IsMSBuildPropertyValueTrue(MSBuildPropertyOptionNames.EnableTrimAnalyzer, context.Compilation)) { return; } context.RegisterOperationBlockAction(context => { if (context.OwningSymbol.IsInRequiresUnreferencedCodeAttributeScope()) { return; } // See https://github.com/dotnet/linker/issues/2587 // Need to punt on handling compiler generated methods until the linker is fixed // async is handled here and the rest are handled just below // iterators could be handled here once https://github.com/dotnet/roslyn/issues/20179 is fixed if (context.OwningSymbol is IMethodSymbol methodSymbol && methodSymbol.IsAsync) { return; } // Sub optimal way to handle analyzer not to generate warnings until the linker is fixed // Iterators, local functions and lambdas are handled foreach (IOperation blockOperation in context.OperationBlocks) { if (blockOperation is IBlockOperation blocks) { foreach (IOperation operation in blocks.Operations) { if (operation.Kind == OperationKind.AnonymousFunction || operation.Kind == OperationKind.LocalFunction || operation.Kind == OperationKind.YieldBreak || operation.Kind == OperationKind.YieldReturn) { return; } } } } foreach (var operationBlock in context.OperationBlocks) { ControlFlowGraph cfg = context.GetControlFlowGraph(operationBlock); TrimDataFlowAnalysis trimDataFlowAnalysis = new (context, cfg); foreach (var diagnostic in trimDataFlowAnalysis.ComputeTrimAnalysisPatterns().CollectDiagnostics()) { context.ReportDiagnostic(diagnostic); } } }); context.RegisterSyntaxNodeAction(context => { ProcessGenericParameters(context); }, SyntaxKind.GenericName); context.RegisterSymbolAction(context => { VerifyMemberOnlyApplyToTypesOrStrings(context, context.Symbol); }, SymbolKind.Method); context.RegisterSymbolAction(context => { VerifyMemberOnlyApplyToTypesOrStrings(context, context.Symbol); }, SymbolKind.Property); context.RegisterSymbolAction(context => { VerifyMemberOnlyApplyToTypesOrStrings(context, context.Symbol); }, SymbolKind.Field); }); }