Skip to content

Commit 5e4d38b

Browse files
committed
Add support for compilation end diagnostics even though we don't currently use them
1 parent 92c4dfb commit 5e4d38b

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/FixAllContextHelper.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ internal static class FixAllContextHelper
2222

2323
private static readonly Func<CompilationWithAnalyzers, SyntaxTree, CancellationToken, Task<ImmutableArray<Diagnostic>>> GetAnalyzerSyntaxDiagnosticsAsync;
2424
private static readonly Func<CompilationWithAnalyzers, SemanticModel, TextSpan?, CancellationToken, Task<ImmutableArray<Diagnostic>>> GetAnalyzerSemanticDiagnosticsAsync;
25+
private static readonly Func<CompilationWithAnalyzers, ImmutableArray<DiagnosticAnalyzer>, CancellationToken, Task<ImmutableArray<Diagnostic>>> GetAnalyzerCompilationDiagnosticsAsync;
2526

2627
static FixAllContextHelper()
2728
{
@@ -34,6 +35,9 @@ static FixAllContextHelper()
3435

3536
methodInfo = typeof(CompilationWithAnalyzers).GetRuntimeMethod(nameof(GetAnalyzerSemanticDiagnosticsAsync), new[] { typeof(SemanticModel), typeof(TextSpan?), typeof(CancellationToken) });
3637
GetAnalyzerSemanticDiagnosticsAsync = (Func<CompilationWithAnalyzers, SemanticModel, TextSpan?, CancellationToken, Task<ImmutableArray<Diagnostic>>>)methodInfo?.CreateDelegate(typeof(Func<CompilationWithAnalyzers, SemanticModel, TextSpan?, CancellationToken, Task<ImmutableArray<Diagnostic>>>));
38+
39+
methodInfo = typeof(CompilationWithAnalyzers).GetRuntimeMethod(nameof(GetAnalyzerCompilationDiagnosticsAsync), new[] { typeof(ImmutableArray<DiagnosticAnalyzer>), typeof(CancellationToken) });
40+
GetAnalyzerCompilationDiagnosticsAsync = (Func<CompilationWithAnalyzers, ImmutableArray<DiagnosticAnalyzer>, CancellationToken, Task<ImmutableArray<Diagnostic>>>)methodInfo?.CreateDelegate(typeof(Func<CompilationWithAnalyzers, ImmutableArray<DiagnosticAnalyzer>, CancellationToken, Task<ImmutableArray<Diagnostic>>>));
3741
}
3842
}
3943

@@ -126,7 +130,7 @@ public static async Task<ImmutableDictionary<Project, ImmutableArray<Diagnostic>
126130
return ImmutableDictionary<Project, ImmutableArray<Diagnostic>>.Empty;
127131
}
128132

129-
public static async Task<ImmutableArray<Diagnostic>> GetAllDiagnosticsAsync(Compilation compilation, CompilationWithAnalyzers compilationWithAnalyzers, IEnumerable<Document> documents, bool includeCompilerDiagnostics, CancellationToken cancellationToken)
133+
public static async Task<ImmutableArray<Diagnostic>> GetAllDiagnosticsAsync(Compilation compilation, CompilationWithAnalyzers compilationWithAnalyzers, ImmutableArray<DiagnosticAnalyzer> analyzers, IEnumerable<Document> documents, bool includeCompilerDiagnostics, CancellationToken cancellationToken)
130134
{
131135
if (GetAnalyzerSyntaxDiagnosticsAsync == null || GetAnalyzerSemanticDiagnosticsAsync == null)
132136
{
@@ -138,9 +142,7 @@ public static async Task<ImmutableArray<Diagnostic>> GetAllDiagnosticsAsync(Comp
138142
compilationWithAnalyzers.Compilation.GetDeclarationDiagnostics(cancellationToken);
139143

140144
// Note that the following loop to obtain syntax and semantic diagnostics for each document cannot operate
141-
// on parallel due to our use of a single CompilationWithAnalyzers instance. Also note that the following
142-
// code is not sufficient for cases where analyzers register compilation end actions. However, this project
143-
// does not currently contain any such analyzers.
145+
// on parallel due to our use of a single CompilationWithAnalyzers instance.
144146
var diagnostics = ImmutableArray<Diagnostic>.Empty;
145147
foreach (var document in documents)
146148
{
@@ -153,6 +155,11 @@ public static async Task<ImmutableArray<Diagnostic>> GetAllDiagnosticsAsync(Comp
153155
diagnostics = diagnostics.AddRange(semanticDiagnostics);
154156
}
155157

158+
foreach (var analyzer in analyzers)
159+
{
160+
diagnostics = diagnostics.AddRange(await GetAnalyzerCompilationDiagnosticsAsync(compilationWithAnalyzers, ImmutableArray.Create(analyzer), cancellationToken).ConfigureAwait(false));
161+
}
162+
156163
if (includeCompilerDiagnostics)
157164
{
158165
// This is the special handling for cases where code fixes operate on warnings produced by the C#
@@ -238,7 +245,7 @@ private static async Task<ImmutableArray<Diagnostic>> GetAllDiagnosticsAsync(Fix
238245
// GetDeclarationDiagnostics workaround for dotnet/roslyn#7446 a single time, rather than once per document.
239246
var compilation = await project.GetCompilationAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
240247
var compilationWithAnalyzers = compilation.WithAnalyzers(analyzers, project.AnalyzerOptions, fixAllContext.CancellationToken);
241-
ImmutableArray<Diagnostic> diagnostics = await GetAllDiagnosticsAsync(compilation, compilationWithAnalyzers, project.Documents, includeCompilerDiagnostics, fixAllContext.CancellationToken).ConfigureAwait(false);
248+
ImmutableArray<Diagnostic> diagnostics = await GetAllDiagnosticsAsync(compilation, compilationWithAnalyzers, analyzers, project.Documents, includeCompilerDiagnostics, fixAllContext.CancellationToken).ConfigureAwait(false);
242249

243250
// Make sure to filter the results to the set requested for the Fix All operation, since analyzers can
244251
// report diagnostics with different IDs.

StyleCop.Analyzers/StyleCopTester/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ private static async Task<ImmutableArray<Diagnostic>> GetProjectAnalyzerDiagnost
438438
Compilation compilation = await processedProject.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
439439
CompilationWithAnalyzers compilationWithAnalyzers = compilation.WithAnalyzers(analyzers, cancellationToken: cancellationToken);
440440

441-
var diagnostics = await FixAllContextHelper.GetAllDiagnosticsAsync(compilation, compilationWithAnalyzers, project.Documents, true, cancellationToken).ConfigureAwait(false);
441+
var diagnostics = await FixAllContextHelper.GetAllDiagnosticsAsync(compilation, compilationWithAnalyzers, analyzers, project.Documents, true, cancellationToken).ConfigureAwait(false);
442442
return diagnostics;
443443
}
444444

0 commit comments

Comments
 (0)