void TestDisabledDiagnostics() { var disabledDiagDescriptor = new DiagnosticDescriptor("XX001", "DummyDescription", "DummyMessage", "DummyCategory", DiagnosticSeverity.Warning, isEnabledByDefault: false); var enabledDiagDescriptor = new DiagnosticDescriptor("XX002", "DummyDescription", "DummyMessage", "DummyCategory", DiagnosticSeverity.Warning, isEnabledByDefault: true); var disabledDiag = CodeAnalysis.Diagnostic.Create(disabledDiagDescriptor, Location.None); var enabledDiag = CodeAnalysis.Diagnostic.Create(enabledDiagDescriptor, Location.None); var diags = new[] { disabledDiag, enabledDiag }; // Verify that only the enabled diag shows up after filtering. var options = TestOptions.Dll; var comp = CreateCompilationWithMscorlib45("", compOptions: options); var effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray(); Assert.Equal(1, effectiveDiags.Length); Assert.Contains(enabledDiag, effectiveDiags); // If the disabled diag was enabled through options, then it should show up. var specificDiagOptions = new Dictionary <string, ReportDiagnostic>(); specificDiagOptions.Add(disabledDiagDescriptor.Id, ReportDiagnostic.Warn); specificDiagOptions.Add(enabledDiagDescriptor.Id, ReportDiagnostic.Suppress); options = TestOptions.Dll.WithSpecificDiagnosticOptions(specificDiagOptions); comp = CreateCompilationWithMscorlib45("", compOptions: options); effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray(); Assert.Equal(1, effectiveDiags.Length); Assert.Contains(disabledDiag, effectiveDiags); }
void TestGetEffectiveDiagnosticsGlobal() { var noneDiagDesciptor = new DiagnosticDescriptor("XX0001", "DummyDescription", "DummyMessage", "DummyCategory", DiagnosticSeverity.Hidden, isEnabledByDefault: true); var infoDiagDesciptor = new DiagnosticDescriptor("XX0002", "DummyDescription", "DummyMessage", "DummyCategory", DiagnosticSeverity.Info, isEnabledByDefault: true); var warningDiagDesciptor = new DiagnosticDescriptor("XX0003", "DummyDescription", "DummyMessage", "DummyCategory", DiagnosticSeverity.Warning, isEnabledByDefault: true); var errorDiagDesciptor = new DiagnosticDescriptor("XX0004", "DummyDescription", "DummyMessage", "DummyCategory", DiagnosticSeverity.Error, isEnabledByDefault: true); var noneDiag = Microsoft.CodeAnalysis.Diagnostic.Create(noneDiagDesciptor, Location.None); var infoDiag = Microsoft.CodeAnalysis.Diagnostic.Create(infoDiagDesciptor, Location.None); var warningDiag = Microsoft.CodeAnalysis.Diagnostic.Create(warningDiagDesciptor, Location.None); var errorDiag = Microsoft.CodeAnalysis.Diagnostic.Create(errorDiagDesciptor, Location.None); var diags = new [] { noneDiag, infoDiag, warningDiag, errorDiag }; var options = OptionsDll.WithGeneralDiagnosticOption(ReportDiagnostic.Default); var comp = CreateCompilationWithMscorlib45("", compOptions: options); var effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray(); Assert.Equal(4, effectiveDiags.Length); options = OptionsDll.WithGeneralDiagnosticOption(ReportDiagnostic.Error); comp = CreateCompilationWithMscorlib45("", compOptions: options); effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray(); Assert.Equal(4, effectiveDiags.Length); Assert.Equal(1, effectiveDiags.Count(d => d.IsWarningAsError)); options = OptionsDll.WithGeneralDiagnosticOption(ReportDiagnostic.Warn); comp = CreateCompilationWithMscorlib45("", compOptions: options); effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray(); Assert.Equal(4, effectiveDiags.Length); Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Error)); Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Warning)); options = OptionsDll.WithGeneralDiagnosticOption(ReportDiagnostic.Info); comp = CreateCompilationWithMscorlib45("", compOptions: options); effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray(); Assert.Equal(4, effectiveDiags.Length); Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Error)); Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Info)); options = OptionsDll.WithGeneralDiagnosticOption(ReportDiagnostic.Hidden); comp = CreateCompilationWithMscorlib45("", compOptions: options); effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray(); Assert.Equal(4, effectiveDiags.Length); Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Error)); Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Hidden)); options = OptionsDll.WithGeneralDiagnosticOption(ReportDiagnostic.Suppress); comp = CreateCompilationWithMscorlib45("", compOptions: options); effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray(); Assert.Equal(2, effectiveDiags.Length); Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Error)); Assert.Equal(1, effectiveDiags.Count(d => d.Severity == DiagnosticSeverity.Hidden)); }
/// <summary> /// Given a set of compiler or <see cref="IDiagnosticAnalyzer"/> 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(this Compilation compilation, IEnumerable <Diagnostic> diagnostics) { return(AnalyzerDriver.GetEffectiveDiagnostics(diagnostics, compilation)); }
void TestGetEffectiveDiagnostics() { var noneDiagDesciptor = new DiagnosticDescriptor("XX0001", "DummyDescription", "DummyMessage", "DummyCategory", DiagnosticSeverity.None); var infoDiagDesciptor = new DiagnosticDescriptor("XX0002", "DummyDescription", "DummyMessage", "DummyCategory", DiagnosticSeverity.Info); var warningDiagDesciptor = new DiagnosticDescriptor("XX0003", "DummyDescription", "DummyMessage", "DummyCategory", DiagnosticSeverity.Warning); var errorDiagDesciptor = new DiagnosticDescriptor("XX0004", "DummyDescription", "DummyMessage", "DummyCategory", DiagnosticSeverity.Error); var noneDiag = Microsoft.CodeAnalysis.Diagnostic.Create(noneDiagDesciptor, Location.None); var infoDiag = Microsoft.CodeAnalysis.Diagnostic.Create(infoDiagDesciptor, Location.None); var warningDiag = Microsoft.CodeAnalysis.Diagnostic.Create(warningDiagDesciptor, Location.None); var errorDiag = Microsoft.CodeAnalysis.Diagnostic.Create(errorDiagDesciptor, Location.None); var diags = new[] { noneDiag, infoDiag, warningDiag, errorDiag }; // Escalate all diagnostics to error. var specificDiagOptions = new Dictionary <string, ReportDiagnostic>(); specificDiagOptions.Add(noneDiagDesciptor.Id, ReportDiagnostic.Error); specificDiagOptions.Add(infoDiagDesciptor.Id, ReportDiagnostic.Error); specificDiagOptions.Add(warningDiagDesciptor.Id, ReportDiagnostic.Error); var options = TestOptions.Dll.WithSpecificDiagnosticOptions(specificDiagOptions); var comp = CreateCompilationWithMscorlib45("", compOptions: options); var effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray(); Assert.Equal(diags.Length, effectiveDiags.Length); foreach (var effectiveDiag in effectiveDiags) { Assert.True(effectiveDiag.Severity == DiagnosticSeverity.Error || (effectiveDiag.Severity == DiagnosticSeverity.Warning && effectiveDiag.IsWarningAsError)); } // Suppress all diagnostics. // NOTE: Diagnostics with default severity error cannot be suppressed and its severity cannot be lowered. specificDiagOptions = new Dictionary <string, ReportDiagnostic>(); specificDiagOptions.Add(noneDiagDesciptor.Id, ReportDiagnostic.Suppress); specificDiagOptions.Add(infoDiagDesciptor.Id, ReportDiagnostic.Suppress); specificDiagOptions.Add(warningDiagDesciptor.Id, ReportDiagnostic.Suppress); specificDiagOptions.Add(errorDiagDesciptor.Id, ReportDiagnostic.Suppress); options = TestOptions.Dll.WithSpecificDiagnosticOptions(specificDiagOptions); comp = CreateCompilationWithMscorlib45("", compOptions: options); effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray(); Assert.Equal(1, effectiveDiags.Length); Assert.Equal(errorDiagDesciptor.Id, effectiveDiags[0].Id); // Shuffle diagnostic severity. specificDiagOptions = new Dictionary <string, ReportDiagnostic>(); specificDiagOptions.Add(noneDiagDesciptor.Id, ReportDiagnostic.Info); specificDiagOptions.Add(infoDiagDesciptor.Id, ReportDiagnostic.Warn); specificDiagOptions.Add(warningDiagDesciptor.Id, ReportDiagnostic.Error); specificDiagOptions.Add(errorDiagDesciptor.Id, ReportDiagnostic.Warn); options = TestOptions.Dll.WithSpecificDiagnosticOptions(specificDiagOptions); comp = CreateCompilationWithMscorlib45("", compOptions: options); effectiveDiags = AnalyzerDriver.GetEffectiveDiagnostics(diags, comp).ToArray(); Assert.Equal(diags.Length, effectiveDiags.Length); var diagIds = new HashSet <string>(diags.Select(d => d.Id)); foreach (var effectiveDiag in effectiveDiags) { Assert.True(diagIds.Remove(effectiveDiag.Id)); switch (effectiveDiag.Severity) { case DiagnosticSeverity.Info: Assert.Equal(noneDiagDesciptor.Id, effectiveDiag.Id); break; case DiagnosticSeverity.Warning: if (!effectiveDiag.IsWarningAsError) { Assert.Equal(infoDiagDesciptor.Id, effectiveDiag.Id); } else { Assert.Equal(warningDiagDesciptor.Id, effectiveDiag.Id); } break; case DiagnosticSeverity.Error: Assert.Equal(errorDiagDesciptor.Id, effectiveDiag.Id); break; default: throw ExceptionUtilities.Unreachable; } } Assert.Empty(diagIds); }