From: David Malcolm Date: Fri, 12 Sep 2025 14:24:36 +0000 (-0400) Subject: diagnostics: handle fatal_error in SARIF output [PR120063] X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=259347de43e9650659a6e7e624b5dc037c0180a0;p=thirdparty%2Fgcc.git diagnostics: handle fatal_error in SARIF output [PR120063] gcc/ChangeLog: PR diagnostics/120063 * diagnostics/context.cc (context::execution_failed_p): Also treat any kind::fatal errors as leading to failed execution. * diagnostics/sarif-sink.cc (maybe_get_sarif_level): Handle kind::fatal as SARIF level "error". gcc/testsuite/ChangeLog: PR diagnostics/120063 * gcc.dg/fatal-error.c: New test. * gcc.dg/fatal-error-html.py: New test. * gcc.dg/fatal-error-sarif.py: New test. Signed-off-by: David Malcolm --- diff --git a/gcc/diagnostics/context.cc b/gcc/diagnostics/context.cc index 0f8670ba806..139c022a602 100644 --- a/gcc/diagnostics/context.cc +++ b/gcc/diagnostics/context.cc @@ -463,9 +463,8 @@ context::dump (FILE *outfile, int indent) const bool context::execution_failed_p () const { - /* Equivalent to (seen_error () || werrorcount), but on - this context, rather than global_dc. */ - return (diagnostic_count (kind::error) + return (diagnostic_count (kind::fatal) + || diagnostic_count (kind::error) || diagnostic_count (kind::sorry) || diagnostic_count (kind::werror)); } diff --git a/gcc/diagnostics/sarif-sink.cc b/gcc/diagnostics/sarif-sink.cc index c85a35e4f63..bc121388bb8 100644 --- a/gcc/diagnostics/sarif-sink.cc +++ b/gcc/diagnostics/sarif-sink.cc @@ -1980,6 +1980,7 @@ maybe_get_sarif_level (enum kind diag_kind) { case kind::warning: return "warning"; + case kind::fatal: case kind::error: return "error"; case kind::note: diff --git a/gcc/testsuite/gcc.dg/fatal-error-html.py b/gcc/testsuite/gcc.dg/fatal-error-html.py new file mode 100644 index 00000000000..b7e7a680ce8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/fatal-error-html.py @@ -0,0 +1,28 @@ +from htmltest import * + +import pytest + +@pytest.fixture(scope='function', autouse=True) +def html_tree(): + return html_tree_from_env() + +def test_results(html_tree): + root = html_tree.getroot () + assert root.tag == make_tag('html') + + head = root.find('xhtml:head', ns) + assert head is not None + + body = root.find('xhtml:body', ns) + assert body is not None + + diag_list = body.find("./xhtml:div[@class='gcc-diagnostic-list']", ns) + assert len(diag_list) + + diag = diag_list.find('xhtml:div[@id="gcc-diag-0"]', ns) + assert diag is not None + message = diag.find("./xhtml:div[@class='gcc-message']", ns) + assert message is not None + assert message[0].tag == make_tag('strong') + assert message[0].text == 'fatal error: ' + assert message[0].tail.startswith(' this-does-not-exist.h:') diff --git a/gcc/testsuite/gcc.dg/fatal-error-sarif.py b/gcc/testsuite/gcc.dg/fatal-error-sarif.py new file mode 100644 index 00000000000..4c434ed8dcf --- /dev/null +++ b/gcc/testsuite/gcc.dg/fatal-error-sarif.py @@ -0,0 +1,29 @@ +from sarif import * + +import pytest + +@pytest.fixture(scope='function', autouse=True) +def sarif(): + return sarif_from_env() + +def test_execution_unsuccessful(sarif): + runs = sarif['runs'] + run = runs[0] + + invocations = run['invocations'] + assert len(invocations) == 1 + invocation = invocations[0] + + # We expect the fatal error to make executionSuccessful be false + assert invocation['executionSuccessful'] == False + +def test_fatal_error(sarif): + runs = sarif['runs'] + run = runs[0] + results = run['results'] + + assert len(results) == 1 + + result = results[0] + assert result['level'] == 'error' + assert result['message']['text'] == "this-does-not-exist.h: No such file or directory" diff --git a/gcc/testsuite/gcc.dg/fatal-error.c b/gcc/testsuite/gcc.dg/fatal-error.c new file mode 100644 index 00000000000..54f90079d9f --- /dev/null +++ b/gcc/testsuite/gcc.dg/fatal-error.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-additional-options "-fdiagnostics-add-output=sarif" } */ +/* { dg-additional-options "-fdiagnostics-add-output=experimental-html:javascript=no" } */ + +#include "this-does-not-exist.h" + +/* { dg-prune-output "fatal error:" } + { dg-prune-output "compilation terminated" } + { dg-final { verify-sarif-file } } + { dg-final { run-sarif-pytest fatal-error.c "fatal-error-sarif.py" } } + { dg-final { run-html-pytest fatal-error.c "fatal-error-html.py" } } */