private static IEnumerable<Diagnostic> GetEffectiveDiagnosticsImpl(ImmutableArray<Diagnostic> diagnostics, Compilation compilation) { var suppressMessageState = new SuppressMessageAttributeState(compilation); foreach (var diagnostic in diagnostics) { if (diagnostic != null) { var effectiveDiagnostic = compilation.Options.FilterDiagnostic(diagnostic); if (effectiveDiagnostic != null) { yield return suppressMessageState.ApplySourceSuppressions(effectiveDiagnostic); } } } }
/// <summary> /// Given a set of compiler or <see cref="DiagnosticAnalyzer"/> generated <paramref name="diagnostics"/>, returns the effective diagnostics after applying the below filters: /// 1) <see cref="CompilationOptions.SpecificDiagnosticOptions"/> specified for the given <paramref name="compilation"/>. /// 2) <see cref="CompilationOptions.GeneralDiagnosticOption"/> specified for the given <paramref name="compilation"/>. /// 3) Diagnostic suppression through applied <see cref="System.Diagnostics.CodeAnalysis.SuppressMessageAttribute"/>. /// 4) Pragma directives for the given <paramref name="compilation"/>. /// </summary> public static IEnumerable<Diagnostic> GetEffectiveDiagnostics(IEnumerable<Diagnostic> diagnostics, Compilation compilation) { if (diagnostics == null) { throw new ArgumentNullException(nameof(diagnostics)); } if (compilation == null) { throw new ArgumentNullException(nameof(compilation)); } var suppressMessageState = new SuppressMessageAttributeState(compilation); foreach (var diagnostic in diagnostics.ToImmutableArray()) { if (diagnostic != null) { var effectiveDiagnostic = compilation.Options.FilterDiagnostic(diagnostic); if (effectiveDiagnostic != null) { effectiveDiagnostic = suppressMessageState.ApplySourceSuppressions(effectiveDiagnostic); yield return effectiveDiagnostic; } } } }
private ImmutableArray<Diagnostic> FilterDiagnosticsSuppressedInSource(ImmutableArray<Diagnostic> diagnostics, Compilation compilation, SuppressMessageAttributeState suppressMessageState) { if (diagnostics.IsEmpty) { return diagnostics; } var reportSuppressedDiagnostics = compilation.Options.ReportSuppressedDiagnostics; var builder = ImmutableArray.CreateBuilder<Diagnostic>(); for (var i = 0; i < diagnostics.Length; i++) { #if DEBUG // We should have ignored diagnostics with invalid locations and reported analyzer exception diagnostic for the same. DiagnosticAnalysisContextHelpers.VerifyDiagnosticLocationsInCompilation(diagnostics[i], compilation); #endif var diagnostic = suppressMessageState.ApplySourceSuppressions(diagnostics[i]); if (!reportSuppressedDiagnostics && diagnostic.IsSuppressed) { // Diagnostic suppressed in source. continue; } builder.Add(diagnostic); } return builder.ToImmutable(); }