@@ -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.
0 commit comments