| File: DataFlowAnalyzerContext.cs | Web Access |
| Project: src\runtime\src\tools\illink\src\ILLink.RoslynAnalyzer\ILLink.RoslynAnalyzer.csproj (ILLink.RoslynAnalyzer) |
// 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 { internal 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 Compilation Compilation { get; } public readonly RequiresUnreferencedCodeAnalyzer? TrimAnalyzer { get; } public readonly bool AnyAnalyzersEnabled => TrimAnalyzer is not null || _enabledAnalyzers.Count > 0; private DataFlowAnalyzerContext( Dictionary<RequiresAnalyzerBase, ImmutableArray<ISymbol>> enabledAnalyzers, RequiresUnreferencedCodeAnalyzer? trimAnalyzer, Compilation compilation) { _enabledAnalyzers = enabledAnalyzers; TrimAnalyzer = trimAnalyzer; Compilation = compilation; } public static DataFlowAnalyzerContext Create(AnalyzerOptions options, Compilation compilation, ImmutableArray<RequiresAnalyzerBase> requiresAnalyzers) { var enabledAnalyzers = new Dictionary<RequiresAnalyzerBase, ImmutableArray<ISymbol>>(); RequiresUnreferencedCodeAnalyzer? trimAnalyzer = null; foreach (var analyzer in requiresAnalyzers) { if (analyzer.IsAnalyzerEnabled(options)) { var incompatibleMembers = analyzer.GetSpecialIncompatibleMembers(compilation); enabledAnalyzers.Add(analyzer, incompatibleMembers); if (analyzer is RequiresUnreferencedCodeAnalyzer rucAnalyzer) { trimAnalyzer = rucAnalyzer; } } } return new DataFlowAnalyzerContext( enabledAnalyzers, trimAnalyzer, compilation); } } }