aboutsummaryrefslogtreecommitdiff
path: root/gcc/xml.cc
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/xml.cc')
-rw-r--r--gcc/xml.cc87
1 files changed, 64 insertions, 23 deletions
diff --git a/gcc/xml.cc b/gcc/xml.cc
index 6c95288..a23298b 100644
--- a/gcc/xml.cc
+++ b/gcc/xml.cc
@@ -28,6 +28,7 @@ along with GCC; see the file COPYING3. If not see
#include "xml-printer.h"
#include "pretty-print.h"
#include "selftest.h"
+#include "selftest-xml.h"
namespace xml {
@@ -121,6 +122,11 @@ node_with_children::add_text (std::string str)
add_child (std::make_unique <text> (std::move (str)));
}
+void
+node_with_children::add_text_from_pp (pretty_printer &pp)
+{
+ add_text (pp_formatted_text (&pp));
+}
/* struct document : public node_with_children. */
@@ -196,13 +202,11 @@ raw::write_as_xml (pretty_printer *pp,
pp_string (pp, m_xml_src.c_str ());
}
-#if __GNUC__ >= 10
-# pragma GCC diagnostic pop
-#endif
-
// class printer
-printer::printer (element &insertion_point)
+printer::printer (element &insertion_point,
+ bool check_popped_tags)
+: m_check_popped_tags (check_popped_tags)
{
m_open_tags.push_back (&insertion_point);
}
@@ -227,9 +231,16 @@ printer::push_tag_with_class (std::string name, std::string class_,
push_element (std::move (new_element));
}
+/* Pop the current topmost tag.
+ If m_check_popped_tags, assert that the tag we're popping is
+ EXPECTED_NAME. */
+
void
-printer::pop_tag ()
+printer::pop_tag (const char *expected_name ATTRIBUTE_UNUSED)
{
+ gcc_assert (!m_open_tags.empty ());
+ if (m_check_popped_tags)
+ gcc_assert (expected_name == get_insertion_point ()->m_kind);
m_open_tags.pop_back ();
}
@@ -247,6 +258,13 @@ printer::add_text (std::string text)
}
void
+printer::add_text_from_pp (pretty_printer &pp)
+{
+ element *parent = m_open_tags.back ();
+ parent->add_text_from_pp (pp);
+}
+
+void
printer::add_raw (std::string text)
{
element *parent = m_open_tags.back ();
@@ -274,20 +292,47 @@ printer::get_insertion_point () const
return m_open_tags.back ();
}
+void
+printer::dump () const
+{
+ pretty_printer pp;
+ pp.set_output_stream (stderr);
+ pp_printf (&pp, "open tags: %i:", (int)m_open_tags.size ());
+ for (auto iter : m_open_tags)
+ pp_printf (&pp, " <%s>", iter->m_kind.c_str ());
+ pp_newline (&pp);
+ pp_printf (&pp, "xml:");
+ pp_newline (&pp);
+ m_open_tags[0]->write_as_xml (&pp, 1, true);
+ pp_flush (&pp);
+}
+
+#if __GNUC__ >= 10
+# pragma GCC diagnostic pop
+#endif
+
} // namespace xml
#if CHECKING_P
namespace selftest {
+void
+assert_xml_print_eq (const location &loc,
+ const xml::node &node,
+ const char *expected_value)
+{
+ pretty_printer pp;
+ node.write_as_xml (&pp, 0, true);
+ ASSERT_STREQ_AT (loc, pp_formatted_text (&pp), expected_value);
+}
+
static void
test_no_dtd ()
{
xml::document doc;
- pretty_printer pp;
- doc.write_as_xml (&pp, 0, true);
- ASSERT_STREQ
- (pp_formatted_text (&pp),
+ ASSERT_XML_PRINT_EQ
+ (doc,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
}
@@ -303,14 +348,12 @@ test_printer ()
xp.set_attr ("color", "red");
xp.add_text ("world");
xp.push_tag ("baz");
- xp.pop_tag ();
- xp.pop_tag ();
- xp.pop_tag ();
+ xp.pop_tag ("baz");
+ xp.pop_tag ("bar");
+ xp.pop_tag ("foo");
- pretty_printer pp;
- top.write_as_xml (&pp, 0, true);
- ASSERT_STREQ
- (pp_formatted_text (&pp),
+ ASSERT_XML_PRINT_EQ
+ (top,
"<top>\n"
" <foo>\n"
" hello\n"
@@ -334,18 +377,16 @@ test_attribute_ordering ()
xp.set_attr ("hastings", "1066");
xp.set_attr ("edgehill", "1642");
xp.set_attr ("naseby", "1645");
- xp.pop_tag ();
+ xp.pop_tag ("chronological");
xp.push_tag ("alphabetical");
xp.set_attr ("edgehill", "1642");
xp.set_attr ("hastings", "1066");
xp.set_attr ("maldon", "991");
xp.set_attr ("naseby", "1645");
- xp.pop_tag ();
+ xp.pop_tag ("alphabetical");
- pretty_printer pp;
- top.write_as_xml (&pp, 0, true);
- ASSERT_STREQ
- (pp_formatted_text (&pp),
+ ASSERT_XML_PRINT_EQ
+ (top,
"<top>\n"
" <chronological maldon=\"991\" hastings=\"1066\" edgehill=\"1642\" naseby=\"1645\"/>\n"
" <alphabetical edgehill=\"1642\" hastings=\"1066\" maldon=\"991\" naseby=\"1645\"/>\n"