|
| 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.Collections.Generic; |
| 6 | +using System.Collections.Immutable; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.Linq; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Microsoft.CodeAnalysis.PooledObjects; |
| 12 | + |
| 13 | +namespace Microsoft.CodeAnalysis.Diagnostics; |
| 14 | + |
| 15 | +internal sealed partial class DiagnosticAnalyzerService |
| 16 | +{ |
| 17 | + private sealed class HostAnalyzerInfo |
| 18 | + { |
| 19 | + private const int BuiltInCompilerPriority = -2; |
| 20 | + private const int RegularDiagnosticAnalyzerPriority = -1; |
| 21 | + |
| 22 | + private readonly ImmutableHashSet<DiagnosticAnalyzer> _hostAnalyzers; |
| 23 | + private readonly ImmutableHashSet<DiagnosticAnalyzer> _allAnalyzers; |
| 24 | + public readonly ImmutableArray<DiagnosticAnalyzer> OrderedAllAnalyzers; |
| 25 | + |
| 26 | + public HostAnalyzerInfo( |
| 27 | + ImmutableHashSet<DiagnosticAnalyzer> hostAnalyzers, |
| 28 | + ImmutableHashSet<DiagnosticAnalyzer> allAnalyzers) |
| 29 | + { |
| 30 | + _hostAnalyzers = hostAnalyzers; |
| 31 | + _allAnalyzers = allAnalyzers; |
| 32 | + |
| 33 | + // order analyzers. |
| 34 | + // order will be in this order |
| 35 | + // BuiltIn Compiler Analyzer (C#/VB) < Regular DiagnosticAnalyzers < Document/ProjectDiagnosticAnalyzers |
| 36 | + OrderedAllAnalyzers = [.. _allAnalyzers.OrderBy(PriorityComparison)]; |
| 37 | + } |
| 38 | + |
| 39 | + public bool IsHostAnalyzer(DiagnosticAnalyzer analyzer) |
| 40 | + => _hostAnalyzers.Contains(analyzer); |
| 41 | + |
| 42 | + public HostAnalyzerInfo WithExcludedAnalyzers(ImmutableHashSet<DiagnosticAnalyzer> excludedAnalyzers) |
| 43 | + { |
| 44 | + if (excludedAnalyzers.IsEmpty) |
| 45 | + { |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + return new(_hostAnalyzers, _allAnalyzers.Except(excludedAnalyzers)); |
| 50 | + } |
| 51 | + |
| 52 | + private int PriorityComparison(DiagnosticAnalyzer state1, DiagnosticAnalyzer state2) |
| 53 | + => GetPriority(state1) - GetPriority(state2); |
| 54 | + |
| 55 | + private static int GetPriority(DiagnosticAnalyzer state) |
| 56 | + { |
| 57 | + // compiler gets highest priority |
| 58 | + if (state.IsCompilerAnalyzer()) |
| 59 | + { |
| 60 | + return BuiltInCompilerPriority; |
| 61 | + } |
| 62 | + |
| 63 | + return state switch |
| 64 | + { |
| 65 | + DocumentDiagnosticAnalyzer analyzer => analyzer.Priority, |
| 66 | + _ => RegularDiagnosticAnalyzerPriority, |
| 67 | + }; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Return <see cref="DiagnosticAnalyzer"/>s for the given <see cref="Project"/>. |
| 73 | + /// </summary> |
| 74 | + internal ImmutableArray<DiagnosticAnalyzer> GetProjectAnalyzers(Project project) |
| 75 | + { |
| 76 | + var hostAnalyzerInfo = GetOrCreateHostAnalyzerInfo(project); |
| 77 | + var projectAnalyzerInfo = GetOrCreateProjectAnalyzerInfo(project); |
| 78 | + return hostAnalyzerInfo.OrderedAllAnalyzers.AddRange(projectAnalyzerInfo.Analyzers); |
| 79 | + } |
| 80 | + |
| 81 | + private HostAnalyzerInfo GetOrCreateHostAnalyzerInfo(Project project) |
| 82 | + { |
| 83 | + var projectAnalyzerInfo = GetOrCreateProjectAnalyzerInfo(project); |
| 84 | + |
| 85 | + var solution = project.Solution; |
| 86 | + var key = new HostAnalyzerInfoKey(project.Language, project.State.HasSdkCodeStyleAnalyzers, solution.SolutionState.Analyzers.HostAnalyzerReferences); |
| 87 | + // Some Host Analyzers may need to be treated as Project Analyzers so that they do not have access to the |
| 88 | + // Host fallback options. These ids will be used when building up the Host and Project analyzer collections. |
| 89 | + var referenceIdsToRedirect = GetReferenceIdsToRedirectAsProjectAnalyzers(project); |
| 90 | + var hostAnalyzerInfo = ImmutableInterlocked.GetOrAdd(ref _hostAnalyzerStateMap, key, CreateLanguageSpecificAnalyzerMap, (solution.SolutionState.Analyzers, referenceIdsToRedirect)); |
| 91 | + return hostAnalyzerInfo.WithExcludedAnalyzers(projectAnalyzerInfo.SkippedAnalyzersInfo.SkippedAnalyzers); |
| 92 | + |
| 93 | + static HostAnalyzerInfo CreateLanguageSpecificAnalyzerMap(HostAnalyzerInfoKey arg, (HostDiagnosticAnalyzers HostAnalyzers, ImmutableHashSet<object> ReferenceIdsToRedirect) state) |
| 94 | + { |
| 95 | + var language = arg.Language; |
| 96 | + var analyzersPerReference = state.HostAnalyzers.GetOrCreateHostDiagnosticAnalyzersPerReference(language); |
| 97 | + |
| 98 | + var (hostAnalyzerCollection, projectAnalyzerCollection) = GetAnalyzerCollections(analyzersPerReference, state.ReferenceIdsToRedirect); |
| 99 | + var (hostAnalyzers, allAnalyzers) = PartitionAnalyzers(projectAnalyzerCollection, hostAnalyzerCollection, includeWorkspacePlaceholderAnalyzers: true); |
| 100 | + |
| 101 | + return new HostAnalyzerInfo(hostAnalyzers, allAnalyzers); |
| 102 | + } |
| 103 | + |
| 104 | + static (ImmutableArray<ImmutableArray<DiagnosticAnalyzer>> HostAnalyzerCollection, ImmutableArray<ImmutableArray<DiagnosticAnalyzer>> ProjectAnalyzerCollection) GetAnalyzerCollections( |
| 105 | + ImmutableDictionary<object, ImmutableArray<DiagnosticAnalyzer>> analyzersPerReference, |
| 106 | + ImmutableHashSet<object> referenceIdsToRedirectAsProjectAnalyzers) |
| 107 | + { |
| 108 | + if (referenceIdsToRedirectAsProjectAnalyzers.IsEmpty) |
| 109 | + { |
| 110 | + return ([.. analyzersPerReference.Values], []); |
| 111 | + } |
| 112 | + |
| 113 | + using var _1 = ArrayBuilder<ImmutableArray<DiagnosticAnalyzer>>.GetInstance(out var hostAnalyzerCollection); |
| 114 | + using var _2 = ArrayBuilder<ImmutableArray<DiagnosticAnalyzer>>.GetInstance(out var projectAnalyzerCollection); |
| 115 | + |
| 116 | + foreach (var (referenceId, analyzers) in analyzersPerReference) |
| 117 | + { |
| 118 | + if (referenceIdsToRedirectAsProjectAnalyzers.Contains(referenceId)) |
| 119 | + { |
| 120 | + projectAnalyzerCollection.Add(analyzers); |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + hostAnalyzerCollection.Add(analyzers); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return (hostAnalyzerCollection.ToImmutableAndClear(), projectAnalyzerCollection.ToImmutableAndClear()); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private static (ImmutableHashSet<DiagnosticAnalyzer> hostAnalyzers, ImmutableHashSet<DiagnosticAnalyzer> allAnalyzers) PartitionAnalyzers( |
| 133 | + ImmutableArray<ImmutableArray<DiagnosticAnalyzer>> projectAnalyzerCollection, |
| 134 | + ImmutableArray<ImmutableArray<DiagnosticAnalyzer>> hostAnalyzerCollection, |
| 135 | + bool includeWorkspacePlaceholderAnalyzers) |
| 136 | + { |
| 137 | + using var _1 = PooledHashSet<DiagnosticAnalyzer>.GetInstance(out var hostAnalyzers); |
| 138 | + using var _2 = PooledHashSet<DiagnosticAnalyzer>.GetInstance(out var allAnalyzers); |
| 139 | + |
| 140 | + if (includeWorkspacePlaceholderAnalyzers) |
| 141 | + { |
| 142 | + hostAnalyzers.Add(FileContentLoadAnalyzer.Instance); |
| 143 | + hostAnalyzers.Add(GeneratorDiagnosticsPlaceholderAnalyzer.Instance); |
| 144 | + allAnalyzers.Add(FileContentLoadAnalyzer.Instance); |
| 145 | + allAnalyzers.Add(GeneratorDiagnosticsPlaceholderAnalyzer.Instance); |
| 146 | + } |
| 147 | + |
| 148 | + foreach (var analyzers in projectAnalyzerCollection) |
| 149 | + { |
| 150 | + foreach (var analyzer in analyzers) |
| 151 | + { |
| 152 | + Debug.Assert(analyzer != FileContentLoadAnalyzer.Instance && analyzer != GeneratorDiagnosticsPlaceholderAnalyzer.Instance); |
| 153 | + allAnalyzers.Add(analyzer); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + foreach (var analyzers in hostAnalyzerCollection) |
| 158 | + { |
| 159 | + foreach (var analyzer in analyzers) |
| 160 | + { |
| 161 | + Debug.Assert(analyzer != FileContentLoadAnalyzer.Instance && analyzer != GeneratorDiagnosticsPlaceholderAnalyzer.Instance); |
| 162 | + allAnalyzers.Add(analyzer); |
| 163 | + hostAnalyzers.Add(analyzer); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return (hostAnalyzers.ToImmutableHashSet(), allAnalyzers.ToImmutableHashSet()); |
| 168 | + } |
| 169 | + |
| 170 | + private static ImmutableHashSet<object> GetReferenceIdsToRedirectAsProjectAnalyzers(Project project) |
| 171 | + { |
| 172 | + if (project.State.HasSdkCodeStyleAnalyzers) |
| 173 | + { |
| 174 | + // When a project uses CodeStyle analyzers added by the SDK, we remove them in favor of the |
| 175 | + // Features analyzers. We need to then treat the Features analyzers as Project analyzers so |
| 176 | + // they do not get access to the Host fallback options. |
| 177 | + return GetFeaturesAnalyzerReferenceIds(project.Solution.SolutionState.Analyzers); |
| 178 | + } |
| 179 | + |
| 180 | + return []; |
| 181 | + |
| 182 | + static ImmutableHashSet<object> GetFeaturesAnalyzerReferenceIds(HostDiagnosticAnalyzers hostAnalyzers) |
| 183 | + { |
| 184 | + var builder = ImmutableHashSet.CreateBuilder<object>(); |
| 185 | + |
| 186 | + foreach (var analyzerReference in hostAnalyzers.HostAnalyzerReferences) |
| 187 | + { |
| 188 | + if (analyzerReference.IsFeaturesAnalyzer()) |
| 189 | + builder.Add(analyzerReference.Id); |
| 190 | + } |
| 191 | + |
| 192 | + return builder.ToImmutable(); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private readonly record struct HostAnalyzerInfoKey( |
| 197 | + string Language, bool HasSdkCodeStyleAnalyzers, IReadOnlyList<AnalyzerReference> AnalyzerReferences); |
| 198 | +} |
0 commit comments