https://gcc.gnu.org/g:0fb30003867635965e2305a4e6f4ac19c2b54225
commit r16-2765-g0fb30003867635965e2305a4e6f4ac19c2b54225 Author: David Malcolm <dmalc...@redhat.com> Date: Mon Aug 4 10:45:31 2025 -0400 diagnostics: avoid stray trailing space in html sink in sarif-replay [PR116792] For the common case where a diagnostic has no metadata, sarif-replay's html output was adding a stray space followed by an empty <div> for the metadata. Fixed thusly. gcc/ChangeLog: PR diagnostics/116792 * diagnostics/html-sink.cc (html_builder::make_element_for_diagnostic): Don't add the metadata element if it's empty. (html_builder::make_element_for_metadata): Return null rather than an empty element. gcc/testsuite/ChangeLog: PR diagnostics/116792 * gcc.dg/plugin/diagnostic-test-graphs-html.py: Remove trailing space from expected text of message. * sarif-replay.dg/2.1.0-valid/embedded-links-check-html.py: Likewise. * sarif-replay.dg/2.1.0-valid/graphs-check-html.py: Likewise. Signed-off-by: David Malcolm <dmalc...@redhat.com> Diff: --- gcc/diagnostics/html-sink.cc | 12 ++++++++---- gcc/testsuite/gcc.dg/plugin/diagnostic-test-graphs-html.py | 2 +- .../sarif-replay.dg/2.1.0-valid/embedded-links-check-html.py | 2 +- .../sarif-replay.dg/2.1.0-valid/graphs-check-html.py | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/gcc/diagnostics/html-sink.cc b/gcc/diagnostics/html-sink.cc index 13d6309b7a45..934d8e2cb341 100644 --- a/gcc/diagnostics/html-sink.cc +++ b/gcc/diagnostics/html-sink.cc @@ -1018,10 +1018,11 @@ html_builder::make_element_for_diagnostic (const diagnostic_info &diagnostic, // Add any metadata as a suffix to the message if (diagnostic.m_metadata) - { - xp.add_text (" "); - xp.append (make_element_for_metadata (*diagnostic.m_metadata)); - } + if (auto e = make_element_for_metadata (*diagnostic.m_metadata)) + { + xp.add_text (" "); + xp.append (std::move (e)); + } // Add any option as a suffix to the message @@ -1234,6 +1235,9 @@ html_builder::make_element_for_metadata (const metadata &m) (make_metadata_element (std::move (label), std::move (url))); } + if (span_metadata->m_children.empty ()) + return nullptr; + return span_metadata; } diff --git a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-graphs-html.py b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-graphs-html.py index 11e5fd1a3859..9ff4645754af 100644 --- a/gcc/testsuite/gcc.dg/plugin/diagnostic-test-graphs-html.py +++ b/gcc/testsuite/gcc.dg/plugin/diagnostic-test-graphs-html.py @@ -26,7 +26,7 @@ def test_result_graph(html_tree): assert message.attrib['id'] == 'gcc-diag-0-message' assert message[0].tag == make_tag('strong') - assert message[0].tail == ' this is a placeholder error, with graphs ' + assert message[0].tail == ' this is a placeholder error, with graphs' graph = diag.find("./xhtml:div[@class='gcc-directed-graph']", ns) assert graph is not None diff --git a/gcc/testsuite/sarif-replay.dg/2.1.0-valid/embedded-links-check-html.py b/gcc/testsuite/sarif-replay.dg/2.1.0-valid/embedded-links-check-html.py index ff1c2f261853..845a7afed9f4 100644 --- a/gcc/testsuite/sarif-replay.dg/2.1.0-valid/embedded-links-check-html.py +++ b/gcc/testsuite/sarif-replay.dg/2.1.0-valid/embedded-links-check-html.py @@ -25,4 +25,4 @@ def test_generated_html(html_tree): assert_tag(msg[1], 'a') assert msg[1].text == 'link' assert msg[1].get('href') == 'http://www.example.com' - assert msg[1].tail == '. ' + assert msg[1].tail == '.' diff --git a/gcc/testsuite/sarif-replay.dg/2.1.0-valid/graphs-check-html.py b/gcc/testsuite/sarif-replay.dg/2.1.0-valid/graphs-check-html.py index 63b80c98738f..355c84baa9c1 100644 --- a/gcc/testsuite/sarif-replay.dg/2.1.0-valid/graphs-check-html.py +++ b/gcc/testsuite/sarif-replay.dg/2.1.0-valid/graphs-check-html.py @@ -24,7 +24,7 @@ def test_result_graph(html_tree): assert message.attrib['id'] == 'gcc-diag-0-message' assert message[0].tag == make_tag('strong') - assert message[0].tail == ' this is a placeholder error, with graphs ' + assert message[0].tail == ' this is a placeholder error, with graphs' graph = diag.find("./xhtml:div[@class='gcc-directed-graph']", ns) assert graph is not None