|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | | -using System; |
6 | 5 | using System.Collections.Generic; |
7 | 6 | using System.Collections.Immutable; |
8 | 7 | using System.Linq; |
9 | 8 | using Microsoft.CodeAnalysis.PooledObjects; |
10 | | -using Roslyn.Utilities; |
11 | 9 |
|
12 | 10 | namespace Microsoft.CodeAnalysis.Diagnostics; |
13 | 11 |
|
14 | 12 | internal sealed partial class DiagnosticAnalyzerService |
15 | 13 | { |
16 | | - private sealed partial class DiagnosticIncrementalAnalyzer |
| 14 | + private sealed partial class StateManager |
17 | 15 | { |
18 | | - private sealed partial class StateManager |
| 16 | + private HostAnalyzerInfo GetOrCreateHostAnalyzerInfo( |
| 17 | + SolutionState solution, ProjectState project, ProjectAnalyzerInfo projectAnalyzerInfo) |
19 | 18 | { |
20 | | - private HostAnalyzerInfo GetOrCreateHostAnalyzerInfo( |
21 | | - SolutionState solution, ProjectState project, ProjectAnalyzerInfo projectAnalyzerInfo) |
| 19 | + var key = new HostAnalyzerInfoKey(project.Language, project.HasSdkCodeStyleAnalyzers, solution.Analyzers.HostAnalyzerReferences); |
| 20 | + // Some Host Analyzers may need to be treated as Project Analyzers so that they do not have access to the |
| 21 | + // Host fallback options. These ids will be used when building up the Host and Project analyzer collections. |
| 22 | + var referenceIdsToRedirect = GetReferenceIdsToRedirectAsProjectAnalyzers(solution, project); |
| 23 | + var hostAnalyzerInfo = ImmutableInterlocked.GetOrAdd(ref _hostAnalyzerStateMap, key, CreateLanguageSpecificAnalyzerMap, (solution.Analyzers, referenceIdsToRedirect)); |
| 24 | + return hostAnalyzerInfo.WithExcludedAnalyzers(projectAnalyzerInfo.SkippedAnalyzersInfo.SkippedAnalyzers); |
| 25 | + |
| 26 | + static HostAnalyzerInfo CreateLanguageSpecificAnalyzerMap(HostAnalyzerInfoKey arg, (HostDiagnosticAnalyzers HostAnalyzers, ImmutableHashSet<object> ReferenceIdsToRedirect) state) |
22 | 27 | { |
23 | | - var key = new HostAnalyzerInfoKey(project.Language, project.HasSdkCodeStyleAnalyzers, solution.Analyzers.HostAnalyzerReferences); |
24 | | - // Some Host Analyzers may need to be treated as Project Analyzers so that they do not have access to the |
25 | | - // Host fallback options. These ids will be used when building up the Host and Project analyzer collections. |
26 | | - var referenceIdsToRedirect = GetReferenceIdsToRedirectAsProjectAnalyzers(solution, project); |
27 | | - var hostAnalyzerInfo = ImmutableInterlocked.GetOrAdd(ref _hostAnalyzerStateMap, key, CreateLanguageSpecificAnalyzerMap, (solution.Analyzers, referenceIdsToRedirect)); |
28 | | - return hostAnalyzerInfo.WithExcludedAnalyzers(projectAnalyzerInfo.SkippedAnalyzersInfo.SkippedAnalyzers); |
29 | | - |
30 | | - static HostAnalyzerInfo CreateLanguageSpecificAnalyzerMap(HostAnalyzerInfoKey arg, (HostDiagnosticAnalyzers HostAnalyzers, ImmutableHashSet<object> ReferenceIdsToRedirect) state) |
31 | | - { |
32 | | - var language = arg.Language; |
33 | | - var analyzersPerReference = state.HostAnalyzers.GetOrCreateHostDiagnosticAnalyzersPerReference(language); |
| 28 | + var language = arg.Language; |
| 29 | + var analyzersPerReference = state.HostAnalyzers.GetOrCreateHostDiagnosticAnalyzersPerReference(language); |
| 30 | + |
| 31 | + var (hostAnalyzerCollection, projectAnalyzerCollection) = GetAnalyzerCollections(analyzersPerReference, state.ReferenceIdsToRedirect); |
| 32 | + var (hostAnalyzers, allAnalyzers) = PartitionAnalyzers(projectAnalyzerCollection, hostAnalyzerCollection, includeWorkspacePlaceholderAnalyzers: true); |
34 | 33 |
|
35 | | - var (hostAnalyzerCollection, projectAnalyzerCollection) = GetAnalyzerCollections(analyzersPerReference, state.ReferenceIdsToRedirect); |
36 | | - var (hostAnalyzers, allAnalyzers) = PartitionAnalyzers(projectAnalyzerCollection, hostAnalyzerCollection, includeWorkspacePlaceholderAnalyzers: true); |
| 34 | + return new HostAnalyzerInfo(hostAnalyzers, allAnalyzers); |
| 35 | + } |
37 | 36 |
|
38 | | - return new HostAnalyzerInfo(hostAnalyzers, allAnalyzers); |
| 37 | + static (IEnumerable<ImmutableArray<DiagnosticAnalyzer>> HostAnalyzerCollection, IEnumerable<ImmutableArray<DiagnosticAnalyzer>> ProjectAnalyzerCollection) GetAnalyzerCollections( |
| 38 | + ImmutableDictionary<object, ImmutableArray<DiagnosticAnalyzer>> analyzersPerReference, |
| 39 | + ImmutableHashSet<object> referenceIdsToRedirectAsProjectAnalyzers) |
| 40 | + { |
| 41 | + if (referenceIdsToRedirectAsProjectAnalyzers.IsEmpty) |
| 42 | + { |
| 43 | + return (analyzersPerReference.Values, []); |
39 | 44 | } |
40 | 45 |
|
41 | | - static (IEnumerable<ImmutableArray<DiagnosticAnalyzer>> HostAnalyzerCollection, IEnumerable<ImmutableArray<DiagnosticAnalyzer>> ProjectAnalyzerCollection) GetAnalyzerCollections( |
42 | | - ImmutableDictionary<object, ImmutableArray<DiagnosticAnalyzer>> analyzersPerReference, |
43 | | - ImmutableHashSet<object> referenceIdsToRedirectAsProjectAnalyzers) |
| 46 | + var hostAnalyzerCollection = ArrayBuilder<ImmutableArray<DiagnosticAnalyzer>>.GetInstance(); |
| 47 | + var projectAnalyzerCollection = ArrayBuilder<ImmutableArray<DiagnosticAnalyzer>>.GetInstance(); |
| 48 | + |
| 49 | + foreach (var (referenceId, analyzers) in analyzersPerReference) |
44 | 50 | { |
45 | | - if (referenceIdsToRedirectAsProjectAnalyzers.IsEmpty) |
| 51 | + if (referenceIdsToRedirectAsProjectAnalyzers.Contains(referenceId)) |
46 | 52 | { |
47 | | - return (analyzersPerReference.Values, []); |
| 53 | + projectAnalyzerCollection.Add(analyzers); |
48 | 54 | } |
49 | | - |
50 | | - var hostAnalyzerCollection = ArrayBuilder<ImmutableArray<DiagnosticAnalyzer>>.GetInstance(); |
51 | | - var projectAnalyzerCollection = ArrayBuilder<ImmutableArray<DiagnosticAnalyzer>>.GetInstance(); |
52 | | - |
53 | | - foreach (var (referenceId, analyzers) in analyzersPerReference) |
| 55 | + else |
54 | 56 | { |
55 | | - if (referenceIdsToRedirectAsProjectAnalyzers.Contains(referenceId)) |
56 | | - { |
57 | | - projectAnalyzerCollection.Add(analyzers); |
58 | | - } |
59 | | - else |
60 | | - { |
61 | | - hostAnalyzerCollection.Add(analyzers); |
62 | | - } |
| 57 | + hostAnalyzerCollection.Add(analyzers); |
63 | 58 | } |
64 | | - |
65 | | - return (hostAnalyzerCollection.ToImmutableAndFree(), projectAnalyzerCollection.ToImmutableAndFree()); |
66 | 59 | } |
| 60 | + |
| 61 | + return (hostAnalyzerCollection.ToImmutableAndFree(), projectAnalyzerCollection.ToImmutableAndFree()); |
67 | 62 | } |
| 63 | + } |
68 | 64 |
|
69 | | - private static ImmutableHashSet<object> GetReferenceIdsToRedirectAsProjectAnalyzers( |
70 | | - SolutionState solution, ProjectState project) |
| 65 | + private static ImmutableHashSet<object> GetReferenceIdsToRedirectAsProjectAnalyzers( |
| 66 | + SolutionState solution, ProjectState project) |
| 67 | + { |
| 68 | + if (project.HasSdkCodeStyleAnalyzers) |
71 | 69 | { |
72 | | - if (project.HasSdkCodeStyleAnalyzers) |
73 | | - { |
74 | | - // When a project uses CodeStyle analyzers added by the SDK, we remove them in favor of the |
75 | | - // Features analyzers. We need to then treat the Features analyzers as Project analyzers so |
76 | | - // they do not get access to the Host fallback options. |
77 | | - return GetFeaturesAnalyzerReferenceIds(solution.Analyzers); |
78 | | - } |
79 | | - |
80 | | - return []; |
| 70 | + // When a project uses CodeStyle analyzers added by the SDK, we remove them in favor of the |
| 71 | + // Features analyzers. We need to then treat the Features analyzers as Project analyzers so |
| 72 | + // they do not get access to the Host fallback options. |
| 73 | + return GetFeaturesAnalyzerReferenceIds(solution.Analyzers); |
| 74 | + } |
81 | 75 |
|
82 | | - static ImmutableHashSet<object> GetFeaturesAnalyzerReferenceIds(HostDiagnosticAnalyzers hostAnalyzers) |
83 | | - { |
84 | | - var builder = ImmutableHashSet.CreateBuilder<object>(); |
| 76 | + return []; |
85 | 77 |
|
86 | | - foreach (var analyzerReference in hostAnalyzers.HostAnalyzerReferences) |
87 | | - { |
88 | | - if (analyzerReference.IsFeaturesAnalyzer()) |
89 | | - builder.Add(analyzerReference.Id); |
90 | | - } |
| 78 | + static ImmutableHashSet<object> GetFeaturesAnalyzerReferenceIds(HostDiagnosticAnalyzers hostAnalyzers) |
| 79 | + { |
| 80 | + var builder = ImmutableHashSet.CreateBuilder<object>(); |
91 | 81 |
|
92 | | - return builder.ToImmutable(); |
| 82 | + foreach (var analyzerReference in hostAnalyzers.HostAnalyzerReferences) |
| 83 | + { |
| 84 | + if (analyzerReference.IsFeaturesAnalyzer()) |
| 85 | + builder.Add(analyzerReference.Id); |
93 | 86 | } |
| 87 | + |
| 88 | + return builder.ToImmutable(); |
94 | 89 | } |
95 | 90 | } |
96 | 91 | } |
|
0 commit comments