From cbbd2b1c82942411f70b41f0aca6f480a01baef8 Mon Sep 17 00:00:00 2001 From: Tobias Burnus Date: Sat, 12 Apr 2014 00:43:54 +0200 Subject: [PATCH] re PR c/60194 (-Wformat should also warn when using %d (instead of %u) for unsigned arguments) 2014-04-11 Tobias Burnus PR c/60194 gcc/ * doc/invoke.texi (-Wformat-signedness): Document it. (Wformat=2): Mention that this enables -Wformat-signedness. gcc/c-family/ * c.opt (Wformat-signedness): Add * c-format.c(check_format_types): Use it. gcc/testsuite/ * * g++.dg/warn/warn_format_signedness.C: New. * gcc.dg/format/warn-signedness.c: New. From-SVN: r209328 --- gcc/ChangeLog | 6 ++++++ gcc/c-family/ChangeLog | 6 ++++++ gcc/c-family/c-format.c | 4 +++- gcc/c-family/c.opt | 4 ++++ gcc/doc/invoke.texi | 10 ++++++++-- gcc/testsuite/ChangeLog | 6 ++++++ gcc/testsuite/g++.dg/warn/warn_format_signedness.C | 11 +++++++++++ gcc/testsuite/gcc.dg/format/warn-signedness.c | 11 +++++++++++ 8 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/warn_format_signedness.C create mode 100644 gcc/testsuite/gcc.dg/format/warn-signedness.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 6b5e623..fd68163 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2014-04-11 Tobias Burnus + + PR c/60194 + * doc/invoke.texi (-Wformat-signedness): Document it. + (Wformat=2): Mention that this enables -Wformat-signedness. + 2014-04-11 Joern Rennecke * common/config/epiphany/epiphany-common.c diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index c780dfd..34354a3 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,9 @@ +2014-04-11 Tobias Burnus + + PR c/60194 + * c.opt (Wformat-signedness): Add + * c-format.c(check_format_types): Use it. + 2014-04-11 Jason Merrill PR c++/57926 diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c index cdc09c4..4c0313d 100644 --- a/gcc/c-family/c-format.c +++ b/gcc/c-family/c-format.c @@ -2418,7 +2418,9 @@ check_format_types (format_wanted_type *types) a second level of indirection. */ if (TREE_CODE (wanted_type) == INTEGER_TYPE && TREE_CODE (cur_type) == INTEGER_TYPE - && (!pedantic || i == 0 || (i == 1 && char_type_flag)) + && ((!pedantic && !warn_format_signedness) + || (i == 0 && !warn_format_signedness) + || (i == 1 && char_type_flag)) && (TYPE_UNSIGNED (wanted_type) ? wanted_type == c_common_unsigned_type (cur_type) : wanted_type == c_common_signed_type (cur_type))) diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 2abf66c..390c056 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -415,6 +415,10 @@ Wformat-security C ObjC C++ ObjC++ Var(warn_format_security) Warning LangEnabledBy(C ObjC C++ ObjC++,Wformat=, warn_format >= 2, 0) Warn about possible security problems with format functions +Wformat-signedness +C ObjC C++ ObjC++ Var(warn_format_signedness) Warning LangEnabledBy(C ObjC C++ ObjC++,Wformat=, warn_format >= 2, 0) +Warn about sign differences with format functions + Wformat-y2k C ObjC C++ ObjC++ Var(warn_format_y2k) Warning LangEnabledBy(C ObjC C++ ObjC++,Wformat=,warn_format >= 2, 0) Warn about strftime formats yielding 2-digit years diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index e2fd4c6..860a545 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -246,7 +246,7 @@ Objective-C and Objective-C++ Dialects}. -Wno-endif-labels -Werror -Werror=* @gol -Wfatal-errors -Wfloat-equal -Wformat -Wformat=2 @gol -Wno-format-contains-nul -Wno-format-extra-args -Wformat-nonliteral @gol --Wformat-security -Wformat-y2k @gol +-Wformat-security -Wformat-signedness -Wformat-y2k @gol -Wframe-larger-than=@var{len} -Wno-free-nonheap-object -Wjump-misses-init @gol -Wignored-qualifiers @gol -Wimplicit -Wimplicit-function-declaration -Wimplicit-int @gol @@ -3539,7 +3539,7 @@ The C standard specifies that zero-length formats are allowed. @opindex Wformat=2 Enable @option{-Wformat} plus additional format checks. Currently equivalent to @option{-Wformat -Wformat-nonliteral -Wformat-security --Wformat-y2k}. +-Wformat-signedness -Wformat-y2k}. @item -Wformat-nonliteral @opindex Wformat-nonliteral @@ -3561,6 +3561,12 @@ currently a subset of what @option{-Wformat-nonliteral} warns about, but in future warnings may be added to @option{-Wformat-security} that are not included in @option{-Wformat-nonliteral}.) +@item -Wformat-signedness +@opindex Wformat-signedness +@opindex Wno-format-signedness +If @option{-Wformat} is specified, also warn if the format string +requires an unsigned argument and the argument is signed and vice versa. + @item -Wformat-y2k @opindex Wformat-y2k @opindex Wno-format-y2k diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index ad54ae8..97a80a3 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,11 @@ 2014-04-11 Tobias Burnus + PR c/60194 + * * g++.dg/warn/warn_format_signedness.C: New. + * gcc.dg/format/warn-signedness.c: New. + +2014-04-11 Tobias Burnus + PR fortran/58880 PR fortran/60495 * gfortran.dg/finalize_25.f90: New. diff --git a/gcc/testsuite/g++.dg/warn/warn_format_signedness.C b/gcc/testsuite/g++.dg/warn/warn_format_signedness.C new file mode 100644 index 0000000..473d522 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/warn_format_signedness.C @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-Wformat -Wformat-signedness" } */ + +/* PR c/60194 */ + +void foo(unsigned u, int i, unsigned char uc, signed char sc) { + __builtin_printf("%d\n", u); /* { dg-warning "expects argument of type 'int', but argument 2 has type 'unsigned int'" } */ + __builtin_printf("%u\n", i); /* { dg-warning "expects argument of type 'unsigned int', but argument 2 has type 'int'" } */ + __builtin_printf("%c\n", sc); + __builtin_printf("%c\n", uc); +} diff --git a/gcc/testsuite/gcc.dg/format/warn-signedness.c b/gcc/testsuite/gcc.dg/format/warn-signedness.c new file mode 100644 index 0000000..473d522 --- /dev/null +++ b/gcc/testsuite/gcc.dg/format/warn-signedness.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-Wformat -Wformat-signedness" } */ + +/* PR c/60194 */ + +void foo(unsigned u, int i, unsigned char uc, signed char sc) { + __builtin_printf("%d\n", u); /* { dg-warning "expects argument of type 'int', but argument 2 has type 'unsigned int'" } */ + __builtin_printf("%u\n", i); /* { dg-warning "expects argument of type 'unsigned int', but argument 2 has type 'int'" } */ + __builtin_printf("%c\n", sc); + __builtin_printf("%c\n", uc); +} -- 2.7.4