diff mbox series

issue a correct fix-it hint for bad argument in GCC diagnostics (PR 80619)

Message ID 89c63048-ce6a-ad21-b671-5a79f380a26b@gmail.com
State New
Headers show
Series issue a correct fix-it hint for bad argument in GCC diagnostics (PR 80619) | expand

Commit Message

Martin Sebor Aug. 5, 2019, 5:51 p.m. UTC
When the argument to a directive with a length modifier like %lu
in a call to a GCC diagnostic function such as warning()) is not
of the right integer type, GCC suggests to replace the length
modifier in the directive with 'w'.  For instance:

   warning: format ‘%lu’ expects argument of type ‘long unsigned int’, 
but argument 2 has type ‘int’ [-Wformat=]
    f ("%lu", 0);
        ~~^
        %wu

To issue the right hint (i.e., %u in the message above) the attached
patch introduces a new format_lengths enumerator to represent the 'w'
modifier.

Tested on x86_64-linux.

Martin

Comments

Jeff Law Aug. 8, 2019, 4:45 p.m. UTC | #1
On 8/5/19 11:51 AM, Martin Sebor wrote:
> When the argument to a directive with a length modifier like %lu
> in a call to a GCC diagnostic function such as warning()) is not
> of the right integer type, GCC suggests to replace the length
> modifier in the directive with 'w'.  For instance:
> 
>   warning: format ‘%lu’ expects argument of type ‘long unsigned int’,
> but argument 2 has type ‘int’ [-Wformat=]
>    f ("%lu", 0);
>        ~~^
>        %wu
> 
> To issue the right hint (i.e., %u in the message above) the attached
> patch introduces a new format_lengths enumerator to represent the 'w'
> modifier.
> 
> Tested on x86_64-linux.
> 
> Martin
> 
> gcc-80619.diff
> 
> PR c/80619 - bad fix-it hint for GCC %lu directive with int argument: %wu
> 
> gcc/c-family/ChangeLog:
> 
> 	PR c/80619
> 	* c-format.c (printf_length_specs): Set FMT_LEN_w for "w".
> 	(asm_fprintf_length_spec): Same.
> 	* c-format.h (format_lengths): Add FMT_LEN_w.
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR c/80619
> 	* gcc.dg/format/pr80619.c: New test.
OK
jeff
diff mbox series

Patch

PR c/80619 - bad fix-it hint for GCC %lu directive with int argument: %wu

gcc/c-family/ChangeLog:

	PR c/80619
	* c-format.c (printf_length_specs): Set FMT_LEN_w for "w".
	(asm_fprintf_length_spec): Same.
	* c-format.h (format_lengths): Add FMT_LEN_w.

gcc/testsuite/ChangeLog:

	PR c/80619
	* gcc.dg/format/pr80619.c: New test.


diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c
index 6363fa4f686..1fa551957d9 100644
--- a/gcc/c-family/c-format.c
+++ b/gcc/c-family/c-format.c
@@ -497,7 +497,7 @@  static const format_length_info printf_length_specs[] =
 static const format_length_info asm_fprintf_length_specs[] =
 {
   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
-  { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
+  { "w", FMT_LEN_w, STD_C89, NO_FMT, 0 },
   { NO_FMT, NO_FMT, 0 }
 };
 
@@ -505,7 +505,7 @@  static const format_length_info asm_fprintf_length_specs[] =
 static const format_length_info gcc_diag_length_specs[] =
 {
   { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C89, 0 },
-  { "w", FMT_LEN_none, STD_C89, NO_FMT, 0 },
+  { "w", FMT_LEN_w, STD_C89, NO_FMT, 0 },
   { NO_FMT, NO_FMT, 0 }
 };
 
diff --git a/gcc/c-family/c-format.h b/gcc/c-family/c-format.h
index 972ba46f109..6aa68dfe883 100644
--- a/gcc/c-family/c-format.h
+++ b/gcc/c-family/c-format.h
@@ -36,6 +36,7 @@  enum format_lengths
   FMT_LEN_H,
   FMT_LEN_D,
   FMT_LEN_DD,
+  FMT_LEN_w,   /* GCC's HOST_WIDE_INT.  */
   FMT_LEN_MAX
 };
 
diff --git a/gcc/testsuite/gcc.dg/format/pr80619.c b/gcc/testsuite/gcc.dg/format/pr80619.c
new file mode 100644
index 00000000000..c9f0496a1c2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/format/pr80619.c
@@ -0,0 +1,89 @@ 
+/* PR c/80619 - bad fix-it hint for GCC %lu directive with int argument: %wu
+   { dg-do compile }
+   { dg-options "-Wall -fdiagnostics-show-caret" } */
+
+void T (const char*, ...) __attribute__ ((format (__gcc_diag__, 1, 2)));
+
+void test_suggested_modifier (void)
+{
+  T ("%ld", 0);     // { dg-warning "format '%ld' expects argument of type 'long int', but argument 2 has type 'int'" }
+  /* { dg-begin-multiline-output "" }
+   T ("%ld", 0);
+       ~~^   ~
+         |   |
+         |   int
+         long int
+       %d
+   { dg-end-multiline-output "" } */
+
+  T ("%li", 0);     // { dg-warning "format '%li' expects argument of type 'long int', but argument 2 has type 'int'" }
+  /* { dg-begin-multiline-output "" }
+   T ("%li", 0);
+       ~~^   ~
+         |   |
+         |   int
+         long int
+       %i
+       { dg-end-multiline-output "" } */
+
+  T ("%lu", 0);     // { dg-warning "format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'int'" }
+  /* { dg-begin-multiline-output "" }
+   T ("%lu", 0);
+       ~~^   ~
+         |   |
+         |   int
+         long unsigned int
+       %u
+       { dg-end-multiline-output "" } */
+
+  T ("%lx", 0);     // { dg-warning "format '%lx' expects argument of type 'long unsigned int', but argument 2 has type 'int'" }
+  /* { dg-begin-multiline-output "" }
+   T ("%lx", 0);
+       ~~^   ~
+         |   |
+         |   int
+         long unsigned int
+       %x
+       { dg-end-multiline-output "" } */
+
+  T ("%lli", 0);    // { dg-warning "format '%lli' expects argument of type 'long long int', but argument 2 has type 'int'" }
+  /* { dg-begin-multiline-output "" }
+   T ("%lli", 0);
+       ~~~^   ~
+          |   |
+          |   int
+          long long int
+       %i
+       { dg-end-multiline-output "" } */
+
+  T ("%llo", 0);    // { dg-warning "format '%llo' expects argument of type 'long long unsigned int', but argument 2 has type 'int'" }
+  /* { dg-begin-multiline-output "" }
+   T ("%llo", 0);
+       ~~~^   ~
+          |   |
+          |   int
+          long long unsigned int
+       %o
+       { dg-end-multiline-output "" } */
+
+  T ("%llu", 0);    // { dg-warning "format '%llu' expects argument of type 'long long unsigned int', but argument 2 has type 'int'" }
+  /* { dg-begin-multiline-output "" }
+   T ("%llu", 0);
+       ~~~^   ~
+          |   |
+          |   int
+          long long unsigned int
+       %u
+       { dg-end-multiline-output "" } */
+
+  T ("%llx", 0);    // { dg-warning "format '%llx' expects argument of type 'long long unsigned int', but argument 2 has type 'int'" }
+  /* { dg-begin-multiline-output "" }
+   T ("%llx", 0);
+       ~~~^   ~
+          |   |
+          |   int
+          long long unsigned int
+       %x
+       { dg-end-multiline-output "" } */
+}
+