-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Report Requires diagnostics from dataflow analyzer (#92724)
This moves the logic for generating diagnostics about RequiresUnreferencedCode, RequiresAssemblyFiles, and RequiresDynamicCode attributes from the separate Requires* analyzers into the DynamicallyAccessedMembersAnalyzer. This includes logic for warning on access to attributed members, not the warnings about mismatching attributes on base/override methods. The override validation logic is still handled by the respective analyzers. The DynamicallyAccessedMembersAnalyzer is now turned on by any of the settings EnableTrimAnalyzer, EnableAotAnalyzer, or EnableSingleFileAnalyzer. A new type DataFlowAnalyzerContext is used to cache per-compilation state of the analyzers, to avoid recomputing it for each trim analysis pattern that might potentially produce a warning. The context only stores state for enabled analyzers, based on the MSBuild property values. There are a few minor differences in the warning behavior with this change: - Implicit calls to annotated base ctors warn because they are visible in the CFG - Unused local functions aren't discovered by the analysis, so don't warn (matching linker/AOT behavior) Also fixes a few asserts that were being hit with the live analyzer (changes from de011df): - Ref property passed as out parameter - Delegate creation of an invocation operation * Add test for annotated value assigned to event * Use DiagnosticContext for consistency * Move field access handling into dataflow analyzer * Remove unnecessary merge logic for reflection access pattern * PR feedback - Clean up a few unused usings - Remove unused Instance field * Fix ExpectedWarnings for ILLink * Fix assert for ref property as out param And related cases involving implicit indexer references. * Add test for deconstruction assignment to ref property * Fix delegate creation over invocation * Fix ExpectedWarnings for illink * Silence dataflow warnings if EnableTrimAnalyer isn't set Extra warnings were being produced on some projects where the single-file analyzer was enabled, causing the DAM analyzer to light up. The dataflow warnings were missing a check of EnableTrimAnalzer to prevent these warnings. This also lifts the checking of MSBuild property values out into the compilation start action, so it only needs to be done per compilation. The context type has been renamed to reflect that it holds state relevant to all of the analyses handled by the dataflow logic now. * Add test coverage for EnableTrimAnalyzer checks And add missing checks in DAM analyzer
- Loading branch information
Showing
33 changed files
with
782 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/tools/illink/src/ILLink.RoslynAnalyzer/DataflowAnalyzerContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace ILLink.RoslynAnalyzer | ||
{ | ||
public readonly struct DataFlowAnalyzerContext | ||
{ | ||
private readonly Dictionary<RequiresAnalyzerBase, ImmutableArray<ISymbol>> _enabledAnalyzers; | ||
|
||
public IEnumerable<RequiresAnalyzerBase> EnabledRequiresAnalyzers => _enabledAnalyzers.Keys; | ||
|
||
public ImmutableArray<ISymbol> GetSpecialIncompatibleMembers (RequiresAnalyzerBase analyzer) | ||
{ | ||
if (!_enabledAnalyzers.TryGetValue (analyzer, out var members)) | ||
throw new System.ArgumentException ($"Analyzer {analyzer.GetType ().Name} is not in the cache"); | ||
return members; | ||
} | ||
|
||
public readonly bool EnableTrimAnalyzer { get; } | ||
|
||
public readonly bool AnyAnalyzersEnabled => EnableTrimAnalyzer || _enabledAnalyzers.Count > 0; | ||
|
||
DataFlowAnalyzerContext (Dictionary<RequiresAnalyzerBase, ImmutableArray<ISymbol>> enabledAnalyzers, bool enableTrimAnalyzer) | ||
{ | ||
_enabledAnalyzers = enabledAnalyzers; | ||
EnableTrimAnalyzer = enableTrimAnalyzer; | ||
} | ||
|
||
public static DataFlowAnalyzerContext Create (AnalyzerOptions options, Compilation compilation, ImmutableArray<RequiresAnalyzerBase> requiresAnalyzers) | ||
{ | ||
var enabledAnalyzers = new Dictionary<RequiresAnalyzerBase, ImmutableArray<ISymbol>> (); | ||
foreach (var analyzer in requiresAnalyzers) { | ||
if (analyzer.IsAnalyzerEnabled (options)) { | ||
var incompatibleMembers = analyzer.GetSpecialIncompatibleMembers (compilation); | ||
enabledAnalyzers.Add (analyzer, incompatibleMembers); | ||
} | ||
} | ||
return new DataFlowAnalyzerContext ( | ||
enabledAnalyzers, | ||
options.IsMSBuildPropertyValueTrue (MSBuildPropertyOptionNames.EnableTrimAnalyzer)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.