diff options
author | Sven Boemer <sbomer@gmail.com> | 2022-02-15 18:07:40 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-15 18:07:40 +0300 |
commit | dece0cdfc9d5f37dd0ade3b5820ef031b4cafa63 (patch) | |
tree | 8856b268ee7a2a824d34592f46fa664ebe1f6194 | |
parent | 829bc56a934d156fdfcde6b062062448637091bc (diff) |
[DAM analyzer] Respect EnableTrimAnalyzer property (#2602)
-rw-r--r-- | src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs | 45 | ||||
-rw-r--r-- | test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs | 31 |
2 files changed, 56 insertions, 20 deletions
diff --git a/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs b/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs index 653ff4f8d..518e855b2 100644 --- a/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs +++ b/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs @@ -57,31 +57,36 @@ namespace ILLink.RoslynAnalyzer if (!System.Diagnostics.Debugger.IsAttached) context.EnableConcurrentExecution (); context.ConfigureGeneratedCodeAnalysis (GeneratedCodeAnalysisFlags.ReportDiagnostics); - context.RegisterOperationBlockAction (context => { - if (context.OwningSymbol.IsInRequiresUnreferencedCodeAttributeScope ()) + context.RegisterCompilationStartAction (context => { + if (!context.Options.IsMSBuildPropertyValueTrue (MSBuildPropertyOptionNames.EnableTrimAnalyzer, context.Compilation)) return; - foreach (var operationBlock in context.OperationBlocks) { - ControlFlowGraph cfg = context.GetControlFlowGraph (operationBlock); - TrimDataFlowAnalysis trimDataFlowAnalysis = new (context, cfg); + context.RegisterOperationBlockAction (context => { + if (context.OwningSymbol.IsInRequiresUnreferencedCodeAttributeScope ()) + return; - foreach (var diagnostic in trimDataFlowAnalysis.ComputeTrimAnalysisPatterns ().CollectDiagnostics ()) { - context.ReportDiagnostic (diagnostic); + 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); }); - 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); } static void ProcessGenericParameters (SyntaxNodeAnalysisContext context) diff --git a/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs b/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs index 24f0f1d3e..d24ad8aa7 100644 --- a/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs +++ b/test/ILLink.RoslynAnalyzer.Tests/DynamicallyAccessedMembersAnalyzerTests.cs @@ -20,6 +20,37 @@ namespace ILLink.RoslynAnalyzer.Tests expected: expected); } + [Fact] + public Task NoWarningsIfAnalyzerIsNotEnabled () + { + var TargetParameterWithAnnotations = @" +using System; +using System.Diagnostics.CodeAnalysis; + +public class Foo +{ +} + +class C +{ + public static void Main() + { + M(typeof(Foo)); + } + + private static void NeedsPublicMethodsOnParameter( + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type parameter) + { + } + + private static void M(Type type) + { + NeedsPublicMethodsOnParameter(type); + } +}"; + return VerifyCS.VerifyAnalyzerAsync (TargetParameterWithAnnotations); + } + #region SourceParameter [Fact] public Task SourceParameterDoesNotMatchTargetParameterAnnotations () |