]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
diagnostics: introduce xml::doctypedecl to avoid hardcoding html
authorDavid Malcolm <dmalcolm@redhat.com>
Fri, 6 Jun 2025 17:41:28 +0000 (13:41 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Fri, 6 Jun 2025 17:41:28 +0000 (13:41 -0400)
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 <dmalcolm@redhat.com>
gcc/diagnostic-format-html.cc
gcc/xml.cc
gcc/xml.h

index 076790bfff7a28823e03b3bcaf60c1407c30b0cd..ea2dbbba9293d64bfa1113bbbc3e8b65c05015d0 100644 (file)
@@ -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",
index e75884066f30292a6cda063154bda32016ce268f..6c95288607de491e38d16559fa34d6ed5770460b 100644 (file)
@@ -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 ();
 }
index 523a44dd146f9664fade937708a0adb54d5bc151..3c5813a22862fa43247cfe22cc0ec7b6d9ba5864 100644 (file)
--- 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