public static ImmutableArray <Diagnostic> GetAnalyzerDiagnostics <TCompilation>( this TCompilation c, DiagnosticAnalyzer[] analyzers, TestValidationMode validationMode, AnalyzerOptions options = null) where TCompilation : Compilation { var compilationWithAnalyzers = c.WithAnalyzers(analyzers.ToImmutableArray(), options, CancellationToken.None); var diagnostics = c.GetDiagnostics(); if (validationMode != TestValidationMode.AllowCompileErrors) { CompilationUtils.ValidateNoCompileErrors(diagnostics); } var diagnosticDescriptors = analyzers.SelectMany(analyzer => analyzer.SupportedDiagnostics); var analyzerDiagnosticIds = diagnosticDescriptors.Select(diagnosticDescriptor => diagnosticDescriptor.Id); var allDiagnosticIds = new HashSet <string>(analyzerDiagnosticIds, StringComparer.Ordinal) { "AD0001" // Failures caught by the Analyzer Driver. }; var allDiagnostics = compilationWithAnalyzers.GetAllDiagnosticsAsync().Result; var resultDiagnostics = allDiagnostics.Where(diagnostic => allDiagnosticIds.Contains(diagnostic.Id)); return(resultDiagnostics.ToImmutableArray()); }
public static ImmutableArray <Diagnostic> GetAnalyzerDiagnostics <TCompilation>( this TCompilation c, DiagnosticAnalyzer[] analyzers, TestValidationMode validationMode, AnalyzerOptions options = null, Action <Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException = null, bool logAnalyzerExceptionAsDiagnostics = true) where TCompilation : Compilation { ImmutableArray <DiagnosticAnalyzer> analyzersArray = analyzers.ToImmutableArray(); var exceptionDiagnostics = new ConcurrentSet <Diagnostic>(); if (onAnalyzerException == null) { if (logAnalyzerExceptionAsDiagnostics) { onAnalyzerException = (ex, analyzer, diagnostic) => { exceptionDiagnostics.Add(diagnostic); }; } else { // We want unit tests to throw if any analyzer OR the driver throws, unless the test explicitly provides a delegate. onAnalyzerException = FailFastOnAnalyzerException; } } using (var driver = AnalyzerDriver.CreateAndAttachToCompilation(c, analyzersArray, options, new AnalyzerManager(analyzersArray), onAnalyzerException, null, false, out Compilation newCompilation, CancellationToken.None)) { ImmutableArray <Diagnostic> diagnostics = newCompilation.GetDiagnostics(); if (validationMode != TestValidationMode.AllowCompileErrors) { CompilationUtils.ValidateNoCompileErrors(diagnostics); } return(driver.GetDiagnosticsAsync(newCompilation).Result.AddRange(exceptionDiagnostics)); } }
public static ImmutableArray <Diagnostic> GetAnalyzerDiagnostics <TCompilation>( this TCompilation c, DiagnosticAnalyzer[] analyzers, TestValidationMode validationMode, AnalyzerOptions options = null) where TCompilation : Compilation { var compilationWithAnalyzers = c.WithAnalyzers(analyzers.ToImmutableArray(), options, CancellationToken.None); var diagnostics = c.GetDiagnostics(); if (validationMode != TestValidationMode.AllowCompileErrors) { CompilationUtils.ValidateNoCompileErrors(diagnostics); } var analyzerDiagnostics = compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync().Result; var allDiagnostics = compilationWithAnalyzers.GetAllDiagnosticsAsync().Result; var failureDiagnostics = allDiagnostics.Where(diagnostic => diagnostic.Id == "AD0001"); var resultDiagnostics = analyzerDiagnostics.Concat(failureDiagnostics); return(resultDiagnostics.ToImmutableArray()); }