aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2024-09-03 15:11:01 -0400
committerDavid Malcolm <dmalcolm@redhat.com>2024-09-03 15:11:01 -0400
commitd0891f3aa75d31744de728905f2f454e9d07ce54 (patch)
tree5bdad2d4b131ae54248c1523c657c1719160ff53
parent34f01475611b422668a70744c79273c7019625f2 (diff)
downloadgcc-d0891f3aa75d31744de728905f2f454e9d07ce54.zip
gcc-d0891f3aa75d31744de728905f2f454e9d07ce54.tar.gz
gcc-d0891f3aa75d31744de728905f2f454e9d07ce54.tar.bz2
pretty-print: add selftest of pp_format's stack
gcc/ChangeLog: * pretty-print-format-impl.h (pp_formatted_chunks::get_prev): New accessor. * pretty-print.cc (selftest::push_pp_format): New. (ASSERT_TEXT_TOKEN): New macro. (selftest::test_pp_format_stack): New test. (selftest::pretty_print_cc_tests): New. Signed-off-by: David Malcolm <dmalcolm@redhat.com>
-rw-r--r--gcc/pretty-print-format-impl.h3
-rw-r--r--gcc/pretty-print.cc78
2 files changed, 81 insertions, 0 deletions
diff --git a/gcc/pretty-print-format-impl.h b/gcc/pretty-print-format-impl.h
index c70f61c..ec4425c 100644
--- a/gcc/pretty-print-format-impl.h
+++ b/gcc/pretty-print-format-impl.h
@@ -376,6 +376,9 @@ public:
void dump (FILE *out) const;
void DEBUG_FUNCTION dump () const { dump (stderr); }
+ // For use in selftests
+ pp_formatted_chunks *get_prev () const { return m_prev; }
+
private:
/* Pointer to previous level on the stack. */
pp_formatted_chunks *m_prev;
diff --git a/gcc/pretty-print.cc b/gcc/pretty-print.cc
index 50aea69..115f376 100644
--- a/gcc/pretty-print.cc
+++ b/gcc/pretty-print.cc
@@ -3547,6 +3547,83 @@ test_custom_tokens_2 ()
"print_tokens was called");
}
+/* Helper subroutine for test_pp_format_stack.
+ Call pp_format (phases 1 and 2), without calling phase 3. */
+
+static void
+push_pp_format (pretty_printer *pp, const char *msg, ...)
+{
+ va_list ap;
+
+ va_start (ap, msg);
+ rich_location rich_loc (line_table, UNKNOWN_LOCATION);
+ text_info ti (msg, &ap, 0, nullptr, &rich_loc);
+ pp_format (pp, &ti);
+ va_end (ap);
+}
+
+#define ASSERT_TEXT_TOKEN(TOKEN, EXPECTED_TEXT) \
+ SELFTEST_BEGIN_STMT \
+ ASSERT_NE ((TOKEN), nullptr); \
+ ASSERT_EQ ((TOKEN)->m_kind, pp_token::kind::text); \
+ ASSERT_STREQ \
+ (as_a <const pp_token_text *> (TOKEN)->m_value.get (), \
+ (EXPECTED_TEXT)); \
+ SELFTEST_END_STMT
+
+
+/* Verify that the stack of pp_formatted_chunks works as expected. */
+
+static void
+test_pp_format_stack ()
+{
+ auto_fix_quotes fix_quotes;
+
+ pretty_printer pp;
+ push_pp_format (&pp, "unexpected foo: %i bar: %qs", 42, "test");
+ push_pp_format (&pp, "In function: %qs", "test_fn");
+
+ /* Expect the top of the stack to have:
+ (gdb) call top->dump()
+ 0: [TEXT("In function: ")]
+ 1: [BEGIN_QUOTE, TEXT("test_fn"), END_QUOTE]. */
+
+ pp_formatted_chunks *top = pp_buffer (&pp)->m_cur_formatted_chunks;
+ ASSERT_NE (top, nullptr);
+ ASSERT_TEXT_TOKEN (top->get_token_lists ()[0]->m_first, "In function: ");
+ ASSERT_EQ (top->get_token_lists ()[1]->m_first->m_kind,
+ pp_token::kind::begin_quote);
+ ASSERT_EQ (top->get_token_lists ()[2], nullptr);
+
+ /* Expect an entry in the stack below it with:
+ 0: [TEXT("unexpected foo: ")]
+ 1: [TEXT("42")]
+ 2: [TEXT(" bar: ")]
+ 3: [BEGIN_QUOTE, TEXT("test"), END_QUOTE]. */
+ pp_formatted_chunks *prev = top->get_prev ();
+ ASSERT_NE (prev, nullptr);
+ ASSERT_TEXT_TOKEN (prev->get_token_lists ()[0]->m_first, "unexpected foo: ");
+ ASSERT_TEXT_TOKEN (prev->get_token_lists ()[1]->m_first, "42");
+ ASSERT_TEXT_TOKEN (prev->get_token_lists ()[2]->m_first, " bar: ");
+ ASSERT_EQ (prev->get_token_lists ()[3]->m_first->m_kind,
+ pp_token::kind::begin_quote);
+ ASSERT_EQ (prev->get_token_lists ()[4], nullptr);
+
+ ASSERT_EQ (prev->get_prev (), nullptr);
+
+ /* Pop the top of the stack. */
+ pp_output_formatted_text (&pp);
+ ASSERT_EQ (pp_buffer (&pp)->m_cur_formatted_chunks, prev);
+ pp_newline (&pp);
+
+ /* Pop the remaining entry from the stack. */
+ pp_output_formatted_text (&pp);
+ ASSERT_EQ (pp_buffer (&pp)->m_cur_formatted_chunks, nullptr);
+
+ ASSERT_STREQ (pp_formatted_text (&pp),
+ "In function: `test_fn'\nunexpected foo: 42 bar: `test'");
+}
+
/* A subclass of pretty_printer for use by test_prefixes_and_wrapping. */
class test_pretty_printer : public pretty_printer
@@ -3976,6 +4053,7 @@ pretty_print_cc_tests ()
test_merge_consecutive_text_tokens ();
test_custom_tokens_1 ();
test_custom_tokens_2 ();
+ test_pp_format_stack ();
test_prefixes_and_wrapping ();
test_urls ();
test_urls_from_braces ();