[3/4] support %f in pp_format

Message ID 1541610522-56777-4-git-send-email-dmalcolm@redhat.com
State New
Headers show
Series
  • Fix -fopt-info-inline (PR ipa/86395)
Related show

Commit Message

David Malcolm Nov. 7, 2018, 5:08 p.m.
Numerous formatted messages from the inliner use %f, mostly as %f, but
occasionally with length modifiers.

This patch implements the simplest case of "%f" for pp_format (with no
modifier support) to make it easier to port these messages from fprintf
to dump_printf_loc.

The selftest has an assertion that %f on 1.0 is printed as "1.000000".
This comes from the host's sprintf, and I believe this is guaranteed by
POSIX: "If the precision is missing, it shall be taken as 6".  If this is
an issue I can drop the selftest.

Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu, in
conjunction with the rest of the patch kit.

OK for trunk?

gcc/c-family/ChangeLog:
	* c-format.c (gcc_dump_printf_char_table): Add entry for %f.

gcc/ChangeLog:
	* pretty-print.c (pp_format): Handle %f.
	(selftest::test_pp_format): Add test of %f.
	* pretty-print.h (pp_double): New macro.

gcc/testsuite/ChangeLog:
	* gcc.dg/format/gcc_diag-10.c: Add coverage for %f.
---
 gcc/c-family/c-format.c                   | 3 +++
 gcc/pretty-print.c                        | 6 ++++++
 gcc/pretty-print.h                        | 1 +
 gcc/testsuite/gcc.dg/format/gcc_diag-10.c | 2 ++
 4 files changed, 12 insertions(+)

Comments

Richard Biener Nov. 8, 2018, 11:30 a.m. | #1
On Wed, Nov 7, 2018 at 5:22 PM David Malcolm <dmalcolm@redhat.com> wrote:
>
> Numerous formatted messages from the inliner use %f, mostly as %f, but
> occasionally with length modifiers.
>
> This patch implements the simplest case of "%f" for pp_format (with no
> modifier support) to make it easier to port these messages from fprintf
> to dump_printf_loc.
>
> The selftest has an assertion that %f on 1.0 is printed as "1.000000".
> This comes from the host's sprintf, and I believe this is guaranteed by
> POSIX: "If the precision is missing, it shall be taken as 6".  If this is
> an issue I can drop the selftest.
>
> Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu, in
> conjunction with the rest of the patch kit.
>
> OK for trunk?

OK.

> gcc/c-family/ChangeLog:
>         * c-format.c (gcc_dump_printf_char_table): Add entry for %f.
>
> gcc/ChangeLog:
>         * pretty-print.c (pp_format): Handle %f.
>         (selftest::test_pp_format): Add test of %f.
>         * pretty-print.h (pp_double): New macro.
>
> gcc/testsuite/ChangeLog:
>         * gcc.dg/format/gcc_diag-10.c: Add coverage for %f.
> ---
>  gcc/c-family/c-format.c                   | 3 +++
>  gcc/pretty-print.c                        | 6 ++++++
>  gcc/pretty-print.h                        | 1 +
>  gcc/testsuite/gcc.dg/format/gcc_diag-10.c | 2 ++
>  4 files changed, 12 insertions(+)
>
> diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c
> index 385ee1a..8d91a77 100644
> --- a/gcc/c-family/c-format.c
> +++ b/gcc/c-family/c-format.c
> @@ -810,6 +810,9 @@ static const format_char_info gcc_dump_printf_char_table[] =
>    /* T requires a "tree" at runtime.  */
>    { "T",   1, STD_C89, { T89_T,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "", "\"",   NULL },
>
> +  /* %f requires a "double"; it doesn't support modifiers.  */
> +  { "f",   0, STD_C89, { T89_D,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "", "\"",   NULL },
> +
>    { NULL,  0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
>  };
>
> diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c
> index 7dd900b..19ef75b 100644
> --- a/gcc/pretty-print.c
> +++ b/gcc/pretty-print.c
> @@ -977,6 +977,7 @@ pp_indent (pretty_printer *pp)
>     %ld, %li, %lo, %lu, %lx: long versions of the above.
>     %lld, %lli, %llo, %llu, %llx: long long versions.
>     %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
> +   %f: double
>     %c: character.
>     %s: string.
>     %p: pointer (printed in a host-dependent manner).
> @@ -1307,6 +1308,10 @@ pp_format (pretty_printer *pp, text_info *text)
>               (pp, *text->args_ptr, precision, unsigned, "u");
>           break;
>
> +       case 'f':
> +         pp_double (pp, va_arg (*text->args_ptr, double));
> +         break;
> +
>         case 'Z':
>           {
>             int *v = va_arg (*text->args_ptr, int *);
> @@ -2160,6 +2165,7 @@ test_pp_format ()
>    ASSERT_PP_FORMAT_2 ("17 12345678", "%wo %x", (HOST_WIDE_INT)15, 0x12345678);
>    ASSERT_PP_FORMAT_2 ("0xcafebabe 12345678", "%wx %x", (HOST_WIDE_INT)0xcafebabe,
>                       0x12345678);
> +  ASSERT_PP_FORMAT_2 ("1.000000 12345678", "%f %x", 1.0, 0x12345678);
>    ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
>    ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world",
>                       0x12345678);
> diff --git a/gcc/pretty-print.h b/gcc/pretty-print.h
> index 2decc51..a6e60f1 100644
> --- a/gcc/pretty-print.h
> +++ b/gcc/pretty-print.h
> @@ -330,6 +330,7 @@ pp_get_prefix (const pretty_printer *pp) { return pp->prefix; }
>        pp_string (PP, pp_buffer (PP)->digit_buffer);            \
>      }                                                          \
>    while (0)
> +#define pp_double(PP, F)       pp_scalar (PP, "%f", F)
>  #define pp_pointer(PP, P)      pp_scalar (PP, "%p", P)
>
>  #define pp_identifier(PP, ID)  pp_string (PP, (pp_translate_identifiers (PP) \
> diff --git a/gcc/testsuite/gcc.dg/format/gcc_diag-10.c b/gcc/testsuite/gcc.dg/format/gcc_diag-10.c
> index 97a1993..ba2629b 100644
> --- a/gcc/testsuite/gcc.dg/format/gcc_diag-10.c
> +++ b/gcc/testsuite/gcc.dg/format/gcc_diag-10.c
> @@ -183,4 +183,6 @@ void test_dump (tree t, gimple *stmt, cgraph_node *node)
>    dump ("%T", t);
>    dump ("%G", stmt);
>    dump ("%C", node);
> +  dump ("%f", 1.0);
> +  dump ("%4.2f", 1.0); /* { dg-warning "format" } */
>  }
> --
> 1.8.5.3
>

Patch

diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c
index 385ee1a..8d91a77 100644
--- a/gcc/c-family/c-format.c
+++ b/gcc/c-family/c-format.c
@@ -810,6 +810,9 @@  static const format_char_info gcc_dump_printf_char_table[] =
   /* T requires a "tree" at runtime.  */
   { "T",   1, STD_C89, { T89_T,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "", "\"",   NULL },
 
+  /* %f requires a "double"; it doesn't support modifiers.  */
+  { "f",   0, STD_C89, { T89_D,   BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN,  BADLEN  }, "", "\"",   NULL },
+
   { NULL,  0, STD_C89, NOLENGTHS, NULL, NULL, NULL }
 };
 
diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c
index 7dd900b..19ef75b 100644
--- a/gcc/pretty-print.c
+++ b/gcc/pretty-print.c
@@ -977,6 +977,7 @@  pp_indent (pretty_printer *pp)
    %ld, %li, %lo, %lu, %lx: long versions of the above.
    %lld, %lli, %llo, %llu, %llx: long long versions.
    %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
+   %f: double
    %c: character.
    %s: string.
    %p: pointer (printed in a host-dependent manner).
@@ -1307,6 +1308,10 @@  pp_format (pretty_printer *pp, text_info *text)
 	      (pp, *text->args_ptr, precision, unsigned, "u");
 	  break;
 
+	case 'f':
+	  pp_double (pp, va_arg (*text->args_ptr, double));
+	  break;
+
 	case 'Z':
 	  {
 	    int *v = va_arg (*text->args_ptr, int *);
@@ -2160,6 +2165,7 @@  test_pp_format ()
   ASSERT_PP_FORMAT_2 ("17 12345678", "%wo %x", (HOST_WIDE_INT)15, 0x12345678);
   ASSERT_PP_FORMAT_2 ("0xcafebabe 12345678", "%wx %x", (HOST_WIDE_INT)0xcafebabe,
 		      0x12345678);
+  ASSERT_PP_FORMAT_2 ("1.000000 12345678", "%f %x", 1.0, 0x12345678);
   ASSERT_PP_FORMAT_2 ("A 12345678", "%c %x", 'A', 0x12345678);
   ASSERT_PP_FORMAT_2 ("hello world 12345678", "%s %x", "hello world",
 		      0x12345678);
diff --git a/gcc/pretty-print.h b/gcc/pretty-print.h
index 2decc51..a6e60f1 100644
--- a/gcc/pretty-print.h
+++ b/gcc/pretty-print.h
@@ -330,6 +330,7 @@  pp_get_prefix (const pretty_printer *pp) { return pp->prefix; }
       pp_string (PP, pp_buffer (PP)->digit_buffer);		\
     }								\
   while (0)
+#define pp_double(PP, F)       pp_scalar (PP, "%f", F)
 #define pp_pointer(PP, P)      pp_scalar (PP, "%p", P)
 
 #define pp_identifier(PP, ID)  pp_string (PP, (pp_translate_identifiers (PP) \
diff --git a/gcc/testsuite/gcc.dg/format/gcc_diag-10.c b/gcc/testsuite/gcc.dg/format/gcc_diag-10.c
index 97a1993..ba2629b 100644
--- a/gcc/testsuite/gcc.dg/format/gcc_diag-10.c
+++ b/gcc/testsuite/gcc.dg/format/gcc_diag-10.c
@@ -183,4 +183,6 @@  void test_dump (tree t, gimple *stmt, cgraph_node *node)
   dump ("%T", t);
   dump ("%G", stmt);
   dump ("%C", node);
+  dump ("%f", 1.0);
+  dump ("%4.2f", 1.0); /* { dg-warning "format" } */
 }