Skip to content

Commit 92c4dfb

Browse files
committed
Refactor FixAllContextHelper and update StyleCopTester to use it
1 parent 0fbbca3 commit 92c4dfb

2 files changed

Lines changed: 43 additions & 27 deletions

File tree

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

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,44 @@ public static async Task<ImmutableDictionary<Project, ImmutableArray<Diagnostic>
126126
return ImmutableDictionary<Project, ImmutableArray<Diagnostic>>.Empty;
127127
}
128128

129+
public static async Task<ImmutableArray<Diagnostic>> GetAllDiagnosticsAsync(Compilation compilation, CompilationWithAnalyzers compilationWithAnalyzers, IEnumerable<Document> documents, bool includeCompilerDiagnostics, CancellationToken cancellationToken)
130+
{
131+
if (GetAnalyzerSyntaxDiagnosticsAsync == null || GetAnalyzerSemanticDiagnosticsAsync == null)
132+
{
133+
// In everything except Roslyn 1.1, we use GetAllDiagnosticsAsync and return the result.
134+
var allDiagnostics = await compilationWithAnalyzers.GetAllDiagnosticsAsync().ConfigureAwait(false);
135+
return allDiagnostics;
136+
}
137+
138+
compilationWithAnalyzers.Compilation.GetDeclarationDiagnostics(cancellationToken);
139+
140+
// 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.
144+
var diagnostics = ImmutableArray<Diagnostic>.Empty;
145+
foreach (var document in documents)
146+
{
147+
var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
148+
var syntaxDiagnostics = await GetAnalyzerSyntaxDiagnosticsAsync(compilationWithAnalyzers, syntaxTree, cancellationToken).ConfigureAwait(false);
149+
diagnostics = diagnostics.AddRange(syntaxDiagnostics);
150+
151+
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
152+
var semanticDiagnostics = await GetAnalyzerSemanticDiagnosticsAsync(compilationWithAnalyzers, semanticModel, default(TextSpan?), cancellationToken).ConfigureAwait(false);
153+
diagnostics = diagnostics.AddRange(semanticDiagnostics);
154+
}
155+
156+
if (includeCompilerDiagnostics)
157+
{
158+
// This is the special handling for cases where code fixes operate on warnings produced by the C#
159+
// compiler, as opposed to being created by specific analyzers.
160+
var compilerDiagnostics = compilation.GetDiagnostics(cancellationToken);
161+
diagnostics = diagnostics.AddRange(compilerDiagnostics);
162+
}
163+
164+
return diagnostics;
165+
}
166+
129167
private static ImmutableDictionary<string, ImmutableArray<Type>> GetAllAnalyzers()
130168
{
131169
Assembly assembly = typeof(NoCodeFixAttribute).GetTypeInfo().Assembly;
@@ -200,31 +238,7 @@ private static async Task<ImmutableArray<Diagnostic>> GetAllDiagnosticsAsync(Fix
200238
// GetDeclarationDiagnostics workaround for dotnet/roslyn#7446 a single time, rather than once per document.
201239
var compilation = await project.GetCompilationAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
202240
var compilationWithAnalyzers = compilation.WithAnalyzers(analyzers, project.AnalyzerOptions, fixAllContext.CancellationToken);
203-
compilationWithAnalyzers.Compilation.GetDeclarationDiagnostics(fixAllContext.CancellationToken);
204-
205-
// Note that the following loop to obtain syntax and semantic diagnostics for each document cannot operate
206-
// on parallel due to our use of a single CompilationWithAnalyzers instance. Also note that the following
207-
// code is not sufficient for cases where analyzers register compilation end actions. However, this project
208-
// does not currently contain any such analyzers.
209-
var diagnostics = ImmutableArray<Diagnostic>.Empty;
210-
foreach (var document in project.Documents)
211-
{
212-
var syntaxTree = await document.GetSyntaxTreeAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
213-
var syntaxDiagnostics = await GetAnalyzerSyntaxDiagnosticsAsync(compilationWithAnalyzers, syntaxTree, fixAllContext.CancellationToken).ConfigureAwait(false);
214-
diagnostics = diagnostics.AddRange(syntaxDiagnostics);
215-
216-
var semanticModel = await document.GetSemanticModelAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
217-
var semanticDiagnostics = await GetAnalyzerSemanticDiagnosticsAsync(compilationWithAnalyzers, semanticModel, default(TextSpan?), fixAllContext.CancellationToken).ConfigureAwait(false);
218-
diagnostics = diagnostics.AddRange(semanticDiagnostics);
219-
}
220-
221-
if (includeCompilerDiagnostics)
222-
{
223-
// This is the special handling for cases where code fixes operate on warnings produced by the C#
224-
// compiler, as opposed to being created by specific analyzers.
225-
var compilerDiagnostics = compilation.GetDiagnostics(fixAllContext.CancellationToken);
226-
diagnostics = diagnostics.AddRange(compilerDiagnostics);
227-
}
241+
ImmutableArray<Diagnostic> diagnostics = await GetAllDiagnosticsAsync(compilation, compilationWithAnalyzers, project.Documents, includeCompilerDiagnostics, fixAllContext.CancellationToken).ConfigureAwait(false);
228242

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

StyleCop.Analyzers/StyleCopTester/Program.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace StyleCopTester
1919
using Microsoft.CodeAnalysis.CodeFixes;
2020
using Microsoft.CodeAnalysis.Diagnostics;
2121
using Microsoft.CodeAnalysis.MSBuild;
22+
using Microsoft.CodeAnalysis.Text;
23+
using StyleCop.Analyzers.Helpers;
2224
using File = System.IO.File;
2325
using Path = System.IO.Path;
2426

@@ -436,8 +438,8 @@ private static async Task<ImmutableArray<Diagnostic>> GetProjectAnalyzerDiagnost
436438
Compilation compilation = await processedProject.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
437439
CompilationWithAnalyzers compilationWithAnalyzers = compilation.WithAnalyzers(analyzers, cancellationToken: cancellationToken);
438440

439-
var allDiagnostics = await compilationWithAnalyzers.GetAllDiagnosticsAsync().ConfigureAwait(false);
440-
return allDiagnostics;
441+
var diagnostics = await FixAllContextHelper.GetAllDiagnosticsAsync(compilation, compilationWithAnalyzers, project.Documents, true, cancellationToken).ConfigureAwait(false);
442+
return diagnostics;
441443
}
442444

443445
private static void PrintHelp()

0 commit comments

Comments
 (0)