@@ -47,29 +47,11 @@ protected static async Task<ImmutableArray<Diagnostic>> GetSortedDiagnosticsFrom
4747 projects . Add ( document . Project ) ;
4848 }
4949
50- var supportedDiagnosticsSpecificOptions = new Dictionary < string , ReportDiagnostic > ( ) ;
51- foreach ( var analyzer in analyzers )
52- {
53- foreach ( var diagnostic in analyzer . SupportedDiagnostics )
54- {
55- // make sure the analyzers we are testing are enabled
56- supportedDiagnosticsSpecificOptions [ diagnostic . Id ] = ReportDiagnostic . Default ;
57- }
58- }
59-
60- // Report exceptions during the analysis process as errors
61- supportedDiagnosticsSpecificOptions . Add ( "AD0001" , ReportDiagnostic . Error ) ;
62-
6350 var diagnostics = ImmutableArray . CreateBuilder < Diagnostic > ( ) ;
6451 foreach ( var project in projects )
6552 {
66- // update the project compilation options
67- var modifiedSpecificDiagnosticOptions = supportedDiagnosticsSpecificOptions . ToImmutableDictionary ( ) . SetItems ( project . CompilationOptions . SpecificDiagnosticOptions ) ;
68- var modifiedCompilationOptions = project . CompilationOptions . WithSpecificDiagnosticOptions ( modifiedSpecificDiagnosticOptions ) ;
69- var processedProject = project . WithCompilationOptions ( modifiedCompilationOptions ) ;
70-
71- var compilation = await processedProject . GetCompilationAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
72- var compilationWithAnalyzers = compilation . WithAnalyzers ( analyzers , processedProject . AnalyzerOptions , cancellationToken ) ;
53+ var compilation = await project . GetCompilationAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
54+ var compilationWithAnalyzers = compilation . WithAnalyzers ( analyzers , project . AnalyzerOptions , cancellationToken ) ;
7355 var compilerDiagnostics = compilation . GetDiagnostics ( cancellationToken ) ;
7456 var compilerErrors = compilerDiagnostics . Where ( i => i . Severity == DiagnosticSeverity . Error ) ;
7557 var diags = await compilationWithAnalyzers . GetAnalyzerDiagnosticsAsync ( ) . ConfigureAwait ( false ) ;
@@ -130,10 +112,6 @@ protected virtual Solution CreateSolution(ProjectId projectId, string language)
130112 {
131113 var compilationOptions = new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary , allowUnsafe : true ) ;
132114
133- var additionalDiagnosticOptions = this . GetDisabledDiagnostics ( ) . Select ( id => new KeyValuePair < string , ReportDiagnostic > ( id , ReportDiagnostic . Suppress ) ) ;
134- var newSpecificOptions = compilationOptions . SpecificDiagnosticOptions . AddRange ( additionalDiagnosticOptions ) ;
135- compilationOptions = compilationOptions . WithSpecificDiagnosticOptions ( newSpecificOptions ) ;
136-
137115 Solution solution = new AdhocWorkspace ( )
138116 . CurrentSolution
139117 . AddProject ( projectId , TestProjectName , TestProjectName , language )
@@ -195,13 +173,32 @@ protected DiagnosticResult CSharpDiagnostic(DiagnosticDescriptor descriptor)
195173 /// <summary>
196174 /// Create a project using the input strings as sources.
197175 /// </summary>
176+ /// <remarks>
177+ /// <para>This method first creates a <see cref="Project"/> by calling <see cref="CreateProjectImpl"/>, and then
178+ /// applies compilation options to the project by calling <see cref="ApplyCompilationOptions"/>.</para>
179+ /// </remarks>
198180 /// <param name="sources">Classes in the form of strings.</param>
199181 /// <param name="language">The language the source classes are in. Values may be taken from the
200182 /// <see cref="LanguageNames"/> class.</param>
201183 /// <param name="filenames">The filenames or null if the default filename should be used</param>
202184 /// <returns>A <see cref="Project"/> created out of the <see cref="Document"/>s created from the source
203185 /// strings.</returns>
204- protected virtual Project CreateProject ( string [ ] sources , string language = LanguageNames . CSharp , string [ ] filenames = null )
186+ protected Project CreateProject ( string [ ] sources , string language = LanguageNames . CSharp , string [ ] filenames = null )
187+ {
188+ Project project = this . CreateProjectImpl ( sources , language , filenames ) ;
189+ return this . ApplyCompilationOptions ( project ) ;
190+ }
191+
192+ /// <summary>
193+ /// Create a project using the input strings as sources.
194+ /// </summary>
195+ /// <param name="sources">Classes in the form of strings.</param>
196+ /// <param name="language">The language the source classes are in. Values may be taken from the
197+ /// <see cref="LanguageNames"/> class.</param>
198+ /// <param name="filenames">The filenames or null if the default filename should be used</param>
199+ /// <returns>A <see cref="Project"/> created out of the <see cref="Document"/>s created from the source
200+ /// strings.</returns>
201+ protected virtual Project CreateProjectImpl ( string [ ] sources , string language , string [ ] filenames )
205202 {
206203 string fileNamePrefix = DefaultFilePathPrefix ;
207204 string fileExt = language == LanguageNames . CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt ;
@@ -222,6 +219,47 @@ protected virtual Project CreateProject(string[] sources, string language = Lang
222219 return solution . GetProject ( projectId ) ;
223220 }
224221
222+ /// <summary>
223+ /// Applies compilation options to a project.
224+ /// </summary>
225+ /// <remarks>
226+ /// <para>The default implementation configures the project by enabling all supported diagnostics of analyzers
227+ /// included in <see cref="GetCSharpDiagnosticAnalyzers"/> as well as <c>AD0001</c>. After configuring these
228+ /// diagnostics, any diagnostic IDs indicated in <see cref="GetDisabledDiagnostics"/> are explictly supressed
229+ /// using <see cref="ReportDiagnostic.Suppress"/>.</para>
230+ /// </remarks>
231+ /// <param name="project">The project.</param>
232+ /// <returns>The modified project.</returns>
233+ protected virtual Project ApplyCompilationOptions ( Project project )
234+ {
235+ var analyzers = this . GetCSharpDiagnosticAnalyzers ( ) ;
236+
237+ var supportedDiagnosticsSpecificOptions = new Dictionary < string , ReportDiagnostic > ( ) ;
238+ foreach ( var analyzer in analyzers )
239+ {
240+ foreach ( var diagnostic in analyzer . SupportedDiagnostics )
241+ {
242+ // make sure the analyzers we are testing are enabled
243+ supportedDiagnosticsSpecificOptions [ diagnostic . Id ] = ReportDiagnostic . Default ;
244+ }
245+ }
246+
247+ // Report exceptions during the analysis process as errors
248+ supportedDiagnosticsSpecificOptions . Add ( "AD0001" , ReportDiagnostic . Error ) ;
249+
250+ foreach ( var id in this . GetDisabledDiagnostics ( ) )
251+ {
252+ supportedDiagnosticsSpecificOptions [ id ] = ReportDiagnostic . Suppress ;
253+ }
254+
255+ // update the project compilation options
256+ var modifiedSpecificDiagnosticOptions = supportedDiagnosticsSpecificOptions . ToImmutableDictionary ( ) . SetItems ( project . CompilationOptions . SpecificDiagnosticOptions ) ;
257+ var modifiedCompilationOptions = project . CompilationOptions . WithSpecificDiagnosticOptions ( modifiedSpecificDiagnosticOptions ) ;
258+
259+ Solution solution = project . Solution . WithProjectCompilationOptions ( project . Id , modifiedCompilationOptions ) ;
260+ return solution . GetProject ( project . Id ) ;
261+ }
262+
225263 /// <summary>
226264 /// Sort <see cref="Diagnostic"/>s by location in source document.
227265 /// </summary>
0 commit comments