]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
diagnostics: handle fatal_error in SARIF output [PR120063]
authorDavid Malcolm <dmalcolm@redhat.com>
Fri, 12 Sep 2025 14:24:36 +0000 (10:24 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Fri, 12 Sep 2025 14:24:36 +0000 (10:24 -0400)
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 <dmalcolm@redhat.com>
gcc/diagnostics/context.cc
gcc/diagnostics/sarif-sink.cc
gcc/testsuite/gcc.dg/fatal-error-html.py [new file with mode: 0644]
gcc/testsuite/gcc.dg/fatal-error-sarif.py [new file with mode: 0644]
gcc/testsuite/gcc.dg/fatal-error.c [new file with mode: 0644]

index 0f8670ba806e24056cb7b0e7ddd8b208e639ff7e..139c022a6027ad7fc2c0d2abc5587a2edd3739d7 100644 (file)
@@ -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));
 }
index c85a35e4f631ee86dad68d1c81248cac42881e80..bc121388bb87d2810838c7556b67239603152d3a 100644 (file)
@@ -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 (file)
index 0000000..b7e7a68
--- /dev/null
@@ -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 (file)
index 0000000..4c434ed
--- /dev/null
@@ -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 (file)
index 0000000..54f9007
--- /dev/null
@@ -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" } } */