Skip to content

Commit 8f17d74

Browse files
Migrate logic over
1 parent 58f80cd commit 8f17d74

File tree

3 files changed

+35
-130
lines changed

3 files changed

+35
-130
lines changed

src/Features/Core/Portable/Diagnostics/Service/DiagnosticAnalyzerService.HostAnalyzerInfo.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,16 @@ private static int GetPriority(DiagnosticAnalyzer state)
7272
/// Return <see cref="DiagnosticAnalyzer"/>s for the given <see cref="Project"/>.
7373
/// </summary>
7474
internal ImmutableArray<DiagnosticAnalyzer> GetProjectAnalyzers(Project project)
75+
{
76+
var (hostAnalyzers, projectAnalyzers) = GetProjectAnalyzersPair(project);
77+
return [.. hostAnalyzers, .. projectAnalyzers];
78+
}
79+
80+
private (ImmutableArray<DiagnosticAnalyzer> hostAnalyzers, ImmutableHashSet<DiagnosticAnalyzer> projectAnalyzers) GetProjectAnalyzersPair(Project project)
7581
{
7682
var hostAnalyzerInfo = GetOrCreateHostAnalyzerInfo(project);
7783
var projectAnalyzerInfo = GetOrCreateProjectAnalyzerInfo(project);
78-
return hostAnalyzerInfo.OrderedAllAnalyzers.AddRange(projectAnalyzerInfo.Analyzers);
84+
return (hostAnalyzerInfo.OrderedAllAnalyzers, projectAnalyzerInfo.Analyzers);
7985
}
8086

8187
private HostAnalyzerInfo GetOrCreateHostAnalyzerInfo(Project project)

src/Features/Core/Portable/Diagnostics/Service/DiagnosticAnalyzerService.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
using Microsoft.CodeAnalysis.Host;
1414
using Microsoft.CodeAnalysis.Host.Mef;
1515
using Microsoft.CodeAnalysis.Options;
16+
using Microsoft.CodeAnalysis.Shared.Extensions;
1617
using Microsoft.CodeAnalysis.Shared.TestHooks;
1718
using Microsoft.CodeAnalysis.SolutionCrawler;
1819
using Microsoft.CodeAnalysis.Workspaces.Diagnostics;
20+
using Roslyn.Utilities;
1921

2022
namespace Microsoft.CodeAnalysis.Diagnostics;
2123

@@ -124,20 +126,40 @@ private ImmutableArray<DiagnosticAnalyzer> GetDiagnosticAnalyzers(
124126
ImmutableHashSet<string>? diagnosticIds,
125127
Func<DiagnosticAnalyzer, bool>? shouldIncludeAnalyzer)
126128
{
127-
var analyzersForProject = GetProjectAnalyzers(project);
128-
var analyzers = analyzersForProject.WhereAsArray(a => ShouldIncludeAnalyzer(project, a));
129+
var (hostAnalyzers, projectAnalyzers) = GetProjectAnalyzersPair(project);
130+
return [
131+
.. hostAnalyzers.WhereAsArray(a => ShouldIncludeAnalyzer(project, a, isHostAnalyzer: true)),
132+
.. projectAnalyzers.WhereAsArray(a => ShouldIncludeAnalyzer(project, a, isHostAnalyzer: false))];
129133

130-
return analyzers;
131-
132-
bool ShouldIncludeAnalyzer(Project project, DiagnosticAnalyzer analyzer)
134+
bool ShouldIncludeAnalyzer(Project project, DiagnosticAnalyzer analyzer, bool isHostAnalyzer)
133135
{
134136
if (!DocumentAnalysisExecutor.IsAnalyzerEnabledForProject(analyzer, project, this._globalOptions))
135137
return false;
136138

137139
if (shouldIncludeAnalyzer != null && !shouldIncludeAnalyzer(analyzer))
138140
return false;
139141

140-
if (diagnosticIds != null && _analyzerInfoCache.GetDiagnosticDescriptors(analyzer).All(d => !diagnosticIds.Contains(d.Id)))
142+
var descriptors = _analyzerInfoCache.GetDiagnosticDescriptors(analyzer);
143+
144+
if (diagnosticIds != null && descriptors.All(d => !diagnosticIds.Contains(d.Id)))
145+
return false;
146+
147+
var analyzerConfigOptions = project.GetAnalyzerConfigOptions();
148+
149+
var allDescriptorsAreHidden = project.CompilationOptions != null && descriptors.All(static (d, arg) =>
150+
{
151+
var effectiveSeverity = d.GetEffectiveSeverity(
152+
arg.CompilationOptions,
153+
arg.isHostAnalyzer
154+
? arg.analyzerConfigOptions?.ConfigOptionsWithFallback
155+
: arg.analyzerConfigOptions?.ConfigOptionsWithoutFallback,
156+
arg.analyzerConfigOptions?.TreeOptions);
157+
158+
return effectiveSeverity == ReportDiagnostic.Hidden;
159+
},
160+
(project.CompilationOptions, isHostAnalyzer, analyzerConfigOptions));
161+
162+
if (allDescriptorsAreHidden)
141163
return false;
142164

143165
return true;

src/Features/Core/Portable/Diagnostics/Service/DiagnosticAnalyzerService_ForceAnalyzeProject.cs

Lines changed: 0 additions & 123 deletions
This file was deleted.

0 commit comments

Comments
 (0)