https://gcc.gnu.org/g:d5050287acd28cbe23df527605449f514a659bba
commit r16-1269-gd5050287acd28cbe23df527605449f514a659bba Author: David Malcolm <dmalc...@redhat.com> Date: Fri Jun 6 13:41:28 2025 -0400 diagnostics: introduce xml::doctypedecl to avoid hardcoding html As further generalization of XML support during prototyping of new features, don't hardcode the HTML DTD. gcc/ChangeLog: * diagnostic-format-html.cc (struct html_doctypedecl): New. (html_builder::html_builder): Use it to populate the document's m_doctypedecl. * xml.cc (xml::document::write_as_xml): Replace hardcoded HTML DTD with use of m_doctypedecl field. (selftest::test_no_dtd): New. (selftest::xml_cc_tests): New. * xml.h (struct doctypedecl): New decl. Signed-off-by: David Malcolm <dmalc...@redhat.com> Diff: --- gcc/diagnostic-format-html.cc | 19 +++++++++++++++++++ gcc/xml.cc | 19 ++++++++++++++----- gcc/xml.h | 8 ++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/gcc/diagnostic-format-html.cc b/gcc/diagnostic-format-html.cc index 076790bfff7a..ea2dbbba9293 100644 --- a/gcc/diagnostic-format-html.cc +++ b/gcc/diagnostic-format-html.cc @@ -319,6 +319,24 @@ const char * const HTML_SCRIPT " });\n" " highlight_current_focus_idx ();\n"); +struct html_doctypedecl : public xml::doctypedecl +{ + void write_as_xml (pretty_printer *pp, + int depth, bool indent) const final override + { + if (indent) + { + for (int i = 0; i < depth; ++i) + pp_string (pp, " "); + } + pp_string (pp, "<!DOCTYPE html\n" + " PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" + " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"); + if (indent) + pp_newline (pp); + } +}; + /* html_builder's ctor. */ html_builder::html_builder (diagnostic_context &context, @@ -336,6 +354,7 @@ html_builder::html_builder (diagnostic_context &context, gcc_assert (m_line_maps); m_document = std::make_unique<xml::document> (); + m_document->m_doctypedecl = std::make_unique<html_doctypedecl> (); { auto html_element = std::make_unique<xml::element> ("html", false); html_element->set_attr ("xmlns", diff --git a/gcc/xml.cc b/gcc/xml.cc index e75884066f30..6c95288607de 100644 --- a/gcc/xml.cc +++ b/gcc/xml.cc @@ -128,11 +128,8 @@ void document::write_as_xml (pretty_printer *pp, int depth, bool indent) const { pp_string (pp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); - pp_string (pp, "<!DOCTYPE html\n" - " PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" - " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"); - if (indent) - pp_newline (pp); + if (m_doctypedecl) + m_doctypedecl->write_as_xml (pp, depth, indent); for (auto &iter : m_children) iter->write_as_xml (pp, depth, indent); } @@ -283,6 +280,17 @@ printer::get_insertion_point () const namespace selftest { +static void +test_no_dtd () +{ + xml::document doc; + pretty_printer pp; + doc.write_as_xml (&pp, 0, true); + ASSERT_STREQ + (pp_formatted_text (&pp), + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); +} + static void test_printer () { @@ -349,6 +357,7 @@ test_attribute_ordering () void xml_cc_tests () { + test_no_dtd (); test_printer (); test_attribute_ordering (); } diff --git a/gcc/xml.h b/gcc/xml.h index 523a44dd146f..3c5813a22862 100644 --- a/gcc/xml.h +++ b/gcc/xml.h @@ -29,6 +29,7 @@ struct node; struct node_with_children; struct document; struct element; + struct doctypedecl; struct node { @@ -72,6 +73,13 @@ struct document : public node_with_children { void write_as_xml (pretty_printer *pp, int depth, bool indent) const final override; + + std::unique_ptr<doctypedecl> m_doctypedecl; +}; + +struct doctypedecl : public node +{ + // still abstract }; struct element : public node_with_children