[PATCH v3] c-format: Add -Wformat-int-precision option [PR80060]
This option is enabled by default when -Wformat option is enabled. A user can specify -Wno-format-int-precision to disable emitting warnings when passing an argument of an incompatible integer type to a 'd', 'i', 'o', 'u', 'x', or 'X' conversion specifier when it has the same precision as the expected type. Signed-off-by: Daniil Stas gcc/c-family/ChangeLog: * c-format.c (check_format_types): Don't emit warnings when passing an argument of an incompatible integer type to a 'd', 'i', 'o', 'u', 'x', or 'X' conversion specifier when it has the same precision as the expected type if -Wno-format-int-precision option is specified. * c.opt: Add -Wformat-int-precision option. gcc/ChangeLog: * doc/invoke.texi: Add -Wformat-int-precision option description. gcc/testsuite/ChangeLog: * c-c++-common/Wformat-int-precision-1.c: New test. * c-c++-common/Wformat-int-precision-2.c: New test. --- Changes for v3: - Added additional @code{} derictives to the documentation where needed. - Changed tests to run on "! long_neq_int" target instead of "lp64". - Added a test case to check that gcc still emits warnings for arguments with different precision even with -Wno-format-int-precision option enabled. Changes for v2: - Changed the option name to -Wformat-int-precision. - Changed the option description as was suggested by Martin. - Changed Wformat-int-precision-2.c to use dg-bogus instead of previous invalid syntax. gcc/c-family/c-format.c | 2 +- gcc/c-family/c.opt | 6 ++ gcc/doc/invoke.texi | 17 - .../c-c++-common/Wformat-int-precision-1.c | 7 +++ .../c-c++-common/Wformat-int-precision-2.c | 8 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/Wformat-int-precision-1.c create mode 100644 gcc/testsuite/c-c++-common/Wformat-int-precision-2.c diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c index e735e092043..c66787f931f 100644 --- a/gcc/c-family/c-format.c +++ b/gcc/c-family/c-format.c @@ -4248,7 +4248,7 @@ check_format_types (const substring_loc &fmt_loc, && (!pedantic || i < 2) && char_type_flag) continue; - if (types->scalar_identity_flag + if ((types->scalar_identity_flag || !warn_format_int_precision) && (TREE_CODE (cur_type) == TREE_CODE (wanted_type) || (INTEGRAL_TYPE_P (cur_type) && INTEGRAL_TYPE_P (wanted_type))) diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 3976fc368db..0621585a4f9 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -684,6 +684,12 @@ C ObjC C++ LTO ObjC++ Warning Alias(Wformat-overflow=, 1, 0) IntegerRange(0, 2) Warn about function calls with format strings that write past the end of the destination region. Same as -Wformat-overflow=1. +Wformat-int-precision +C ObjC C++ ObjC++ Var(warn_format_int_precision) Warning LangEnabledBy(C ObjC C++ ObjC++,Wformat=,warn_format >= 1, 0) +Warn when passing an argument of an incompatible integer type to a 'd', 'i', +'o', 'u', 'x', or 'X' conversion specifier even when it has the same precision +as the expected type. + 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. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 4b1b58318f0..da69d804598 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -351,7 +351,7 @@ Objective-C and Objective-C++ Dialects}. -Werror -Werror=* -Wexpansion-to-defined -Wfatal-errors @gol -Wfloat-conversion -Wfloat-equal -Wformat -Wformat=2 @gol -Wno-format-contains-nul -Wno-format-extra-args @gol --Wformat-nonliteral -Wformat-overflow=@var{n} @gol +-Wformat-nonliteral -Wformat-overflow=@var{n} -Wformat-int-precision @gol -Wformat-security -Wformat-signedness -Wformat-truncation=@var{n} @gol -Wformat-y2k -Wframe-address @gol -Wframe-larger-than=@var{byte-size} -Wno-free-nonheap-object @gol @@ -6113,6 +6113,21 @@ If @option{-Wformat} is specified, also warn if the format string is not a string literal and so cannot be checked, unless the format function takes its format arguments as a @code{va_list}. +@item -Wformat-int-precision +@opindex Wformat-int-precision +@opindex Wno-format-int-precision +Warn when passing an argument of an incompatible integer type to +a @samp{d}, @samp{i}, @samp{o}, @samp{u}, @samp{x}, or @samp{X} conversion +specifier even when it has the same precision as the expected type. +For example, on targets where @code{int64_t} is a typedef for @code{long}, +the warning is issued for the @code{printf} call below even when both +@code{long} and @code{long long} have the same size and precision. + +@smallexample + extern int64_t n; + printf ("%l
Re: [PATCH v2] c-format: Add -Wformat-int-precision option [PR80060]
On Thu, 4 Nov 2021 18:25:14 -0600 Martin Sebor wrote: > On 10/31/21 8:13 AM, Daniil Stas wrote: > > On Sun, 10 Oct 2021 23:10:20 + > > Daniil Stas wrote: > > > >> This option is enabled by default when -Wformat option is enabled. > >> A user can specify -Wno-format-int-precision to disable emitting > >> warnings when passing an argument of an incompatible integer type > >> to a 'd', 'i', 'o', 'u', 'x', or 'X' conversion specifier when it > >> has the same precision as the expected type. > >> > >> Signed-off-by: Daniil Stas > >> > >> gcc/c-family/ChangeLog: > >> > >>* c-format.c (check_format_types): Don't emit warnings when > >>passing an argument of an incompatible integer type to > >>a 'd', 'i', 'o', 'u', 'x', or 'X' conversion specifier when > >> it has the same precision as the expected type if > >>-Wno-format-int-precision option is specified. > >>* c.opt: Add -Wformat-int-precision option. > >> > >> gcc/ChangeLog: > >> > >>* doc/invoke.texi: Add -Wformat-int-precision option > >> description. > >> > >> gcc/testsuite/ChangeLog: > >> > >>* c-c++-common/Wformat-int-precision-1.c: New test. > >>* c-c++-common/Wformat-int-precision-2.c: New test. > >> --- > >> This is an update of patch "c-format: Add -Wformat-same-precision > >> option [PR80060]". The changes comparing to the first patch > >> version: > >> > >> - changed the option name to -Wformat-int-precision > >> - changed the option description as was suggested by Martin > >> - changed Wformat-int-precision-2.c to used dg-bogus instead of > >> previous invalid syntax > >> > >> I also tried to combine the tests into one file with #pragma GCC > >> diagnostic, but looks like it's not possible. I want to test that > >> when passing just -Wformat option everything works as before my > >> patch by default. And then in another test case to check that > >> passing -Wno-format-int-precision disables the warning. But looks > >> like in GCC you can't toggle the warnings such as > >> -Wno-format-int-precision individually but only can disable the > >> general -Wformat option that will disable all the formatting > >> warnings together, which is not the proper test. > > > > Hi, > > Can anyone review this patch? > > Thank you > > I can't approve the change but it looks pretty good to me. > > The documentation should wrap code symbols like int64_t, long, > or printf in @code{} directives. > > I don't think the first test needs to be restricted to just > lp64, although I'd expect it to already be covered by the test > suite. The lp64 selector only tells us that int is 32 bits > and long (and pointer) are 64, but nothing about long long so > I suspect the test might fail on other targets. There's llp64 > that's true for 4 byte ints and longs (but few targets match), > and long_neq_int that's true when long is not the same size as > int. So I think the inverse of the latter might be best, with > int and long as arguments. testsuite/lib/target-supports.exp > defines these and others. > > It might also be a good idea to add another case to the second > test to exercise arguments with different precision to make > sure -Wformat still triggers for those even with > -Wno-format-int-precision. > > The -Wformat warnings are Joseph's domain (CC'd) so either he > or some other C or global reviewer needs to sign off on changes > in this area. (Please ping the patch weekly until you get > a response.) > > Thanks > Martin Hi, Martin Thanks for your response. I've sent an updated patch. Best regards, Daniil
Re: [PATCH v3] c-format: Add -Wformat-int-precision option [PR80060]
On Mon, 22 Nov 2021 20:35:03 + Joseph Myers wrote: > On Sun, 21 Nov 2021, Daniil Stas via Gcc-patches wrote: > > > This option is enabled by default when -Wformat option is enabled. A > > user can specify -Wno-format-int-precision to disable emitting > > warnings when passing an argument of an incompatible integer type to > > a 'd', 'i', 'o', 'u', 'x', or 'X' conversion specifier when it has > > the same precision as the expected type. > > I'd expect this to apply to 'b' and 'B' as well (affects commit > message, ChangeLog entry, option help string, documentation). > Hi Joseph, I can't find any description of these specifiers anywhere. And looks like gcc doesn't recognize them when I try to compile a sample program with them (I just get %B printed when I run the program). Do these specifiers actually exist? Can you point me to the documentation? Thanks
[PATCH] c-format: Add -Wformat-same-precision option [PR80060]
This option is enabled by default when -Wformat option is enabled. A user can specify -Wno-format-same-precision to disable emitting warnings about an argument passed to printf-like function having a different type from the one specified in the format string if the types precisions are the same. Signed-off-by: Daniil Stas gcc/c-family/ChangeLog: * c-format.c (check_format_types): Don't emit warnings about type differences with the format string if -Wno-format-same-precision is specified and the types have the same precision. * c.opt: Add -Wformat-same-precision option. gcc/ChangeLog: * doc/invoke.texi: Add -Wformat-same-precision option description. gcc/testsuite/ChangeLog: * c-c++-common/Wformat-same-precision-1.c: New test. * c-c++-common/Wformat-same-precision-2.c: New test. --- gcc/c-family/c-format.c | 2 +- gcc/c-family/c.opt| 5 + gcc/doc/invoke.texi | 8 +++- gcc/testsuite/c-c++-common/Wformat-same-precision-1.c | 7 +++ gcc/testsuite/c-c++-common/Wformat-same-precision-2.c | 7 +++ 5 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/Wformat-same-precision-1.c create mode 100644 gcc/testsuite/c-c++-common/Wformat-same-precision-2.c diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c index b4cb765a9d3..07cdcefbef8 100644 --- a/gcc/c-family/c-format.c +++ b/gcc/c-family/c-format.c @@ -4243,7 +4243,7 @@ check_format_types (const substring_loc &fmt_loc, && (!pedantic || i < 2) && char_type_flag) continue; - if (types->scalar_identity_flag + if ((types->scalar_identity_flag || !warn_format_same_precision) && (TREE_CODE (cur_type) == TREE_CODE (wanted_type) || (INTEGRAL_TYPE_P (cur_type) && INTEGRAL_TYPE_P (wanted_type))) diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 9c151d19870..e7af7365c91 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -656,6 +656,11 @@ C ObjC C++ LTO ObjC++ Warning Alias(Wformat-overflow=, 1, 0) IntegerRange(0, 2) Warn about function calls with format strings that write past the end of the destination region. Same as -Wformat-overflow=1. +Wformat-same-precision +C ObjC C++ ObjC++ Var(warn_format_same_precision) Warning LangEnabledBy(C ObjC C++ ObjC++,Wformat=,warn_format >= 1, 0) +Warn about type differences with the format string even if the types +precision is the same. + 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. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index ba98eab68a5..8833f257d75 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -347,7 +347,7 @@ Objective-C and Objective-C++ Dialects}. -Werror -Werror=* -Wexpansion-to-defined -Wfatal-errors @gol -Wfloat-conversion -Wfloat-equal -Wformat -Wformat=2 @gol -Wno-format-contains-nul -Wno-format-extra-args @gol --Wformat-nonliteral -Wformat-overflow=@var{n} @gol +-Wformat-nonliteral -Wformat-overflow=@var{n} -Wformat-same-precision @gol -Wformat-security -Wformat-signedness -Wformat-truncation=@var{n} @gol -Wformat-y2k -Wframe-address @gol -Wframe-larger-than=@var{byte-size} -Wno-free-nonheap-object @gol @@ -6054,6 +6054,12 @@ If @option{-Wformat} is specified, also warn if the format string is not a string literal and so cannot be checked, unless the format function takes its format arguments as a @code{va_list}. +@item -Wformat-same-precision +@opindex Wformat-same-precision +@opindex Wno-format-same-precision +If @option{-Wformat} is specified, warn about type differences with the format +string even if the types precision is the same. + @item -Wformat-security @opindex Wformat-security @opindex Wno-format-security diff --git a/gcc/testsuite/c-c++-common/Wformat-same-precision-1.c b/gcc/testsuite/c-c++-common/Wformat-same-precision-1.c new file mode 100644 index 000..fbc11e4200a --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wformat-same-precision-1.c @@ -0,0 +1,7 @@ +/* { dg-do compile { target lp64 } } */ +/* { dg-options "-Wformat" } */ + +void test () +{ + __builtin_printf ("%lu\n", (long long) 1); /* { dg-warning "expects argument of type" } */ +} diff --git a/gcc/testsuite/c-c++-common/Wformat-same-precision-2.c b/gcc/testsuite/c-c++-common/Wformat-same-precision-2.c new file mode 100644 index 000..17e643e0441 --- /dev/null +++ b/gcc/testsuite/c-c++-common/Wformat-same-precision-2.c @@ -0,0 +1,7 @@ +/* { dg-do compile { target lp64 } } */ +/* { dg-options "-Wformat -Wno-format-same-precision" } */ + +void test () +{ + __builtin_printf ("%lu\n", (long long) 1); /* { ! dg-warning "expects argument of type" } */ +} -- 2.33.0
Re: [PATCH] c-format: Add -Wformat-same-precision option [PR80060]
Hi, Martin On Thu, 30 Sep 2021 09:02:28 -0600 Martin Sebor wrote: > On 9/26/21 3:52 PM, Daniil Stas via Gcc-patches wrote: > > This option is enabled by default when -Wformat option is enabled. A > > user can specify -Wno-format-same-precision to disable emitting > > warnings about an argument passed to printf-like function having a > > different type from the one specified in the format string if the > > types precisions are the same. > > Having an option to control this -Wformat aspect seems useful so > just a few comments mostly on the wording/naming choices. > > Coming up with good names is tricky but I wonder if we can find > one that's clearer than "-Wformat-same-precision". Precision can > mean a few different things in this context: in the representation > of integers it refers to the number of value bits. In that of > floating types, it refers to the number of significand bits. And > in printf directives, it refers to what comes after the optional > period and what controls the minimum number of digits to format > (or maximum number of characters in a string). So "same precision" > seems rather vague (and the proposed manual entry doesn't make it > clear). > > IIUC, the option is specifically for directives that take integer > arguments and controls whether using an argument of an incompatible > integer type to a conversion specifier like i or x is diagnosed when > the argument has the same precision as the expected type. > > With that in mind, would mentioning the word integer (or just int > for short) be an improvement? E.g., -Wformat-int-precision? > Yes, I like -Wformat-int-precision name too. > Some more comments on the documentation text are below. > > > > > Signed-off-by: Daniil Stas > > > > gcc/c-family/ChangeLog: > > > > * c-format.c (check_format_types): Don't emit warnings about > > type differences with the format string if > > -Wno-format-same-precision is specified and the types have > > the same precision. > > * c.opt: Add -Wformat-same-precision option. > > > > gcc/ChangeLog: > > > > * doc/invoke.texi: Add -Wformat-same-precision option > > description. > > > > gcc/testsuite/ChangeLog: > > > > * c-c++-common/Wformat-same-precision-1.c: New test. > > * c-c++-common/Wformat-same-precision-2.c: New test. > > --- > > gcc/c-family/c-format.c | 2 +- > > gcc/c-family/c.opt| 5 + > > gcc/doc/invoke.texi | 8 +++- > > gcc/testsuite/c-c++-common/Wformat-same-precision-1.c | 7 +++ > > gcc/testsuite/c-c++-common/Wformat-same-precision-2.c | 7 +++ > > 5 files changed, 27 insertions(+), 2 deletions(-) > > create mode 100644 > > gcc/testsuite/c-c++-common/Wformat-same-precision-1.c create mode > > 100644 gcc/testsuite/c-c++-common/Wformat-same-precision-2.c > > > > diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c > > index b4cb765a9d3..07cdcefbef8 100644 > > --- a/gcc/c-family/c-format.c > > +++ b/gcc/c-family/c-format.c > > @@ -4243,7 +4243,7 @@ check_format_types (const substring_loc > > &fmt_loc, && (!pedantic || i < 2) > > && char_type_flag) > > continue; > > - if (types->scalar_identity_flag > > + if ((types->scalar_identity_flag || > > !warn_format_same_precision) && (TREE_CODE (cur_type) == TREE_CODE > > (wanted_type) || (INTEGRAL_TYPE_P (cur_type) > > && INTEGRAL_TYPE_P (wanted_type))) > > diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt > > index 9c151d19870..e7af7365c91 100644 > > --- a/gcc/c-family/c.opt > > +++ b/gcc/c-family/c.opt > > @@ -656,6 +656,11 @@ C ObjC C++ LTO ObjC++ Warning > > Alias(Wformat-overflow=, 1, 0) IntegerRange(0, 2) Warn about > > function calls with format strings that write past the end of the > > destination region. Same as -Wformat-overflow=1. > > +Wformat-same-precision > > +C ObjC C++ ObjC++ Var(warn_format_same_precision) Warning > > LangEnabledBy(C ObjC C++ ObjC++,Wformat=,warn_format >= 1, 0) +Warn > > about type differences with the format string even if the types > > +precision is the same. > > The grammar doesn't seem quite right here (I recommend to adjust > the text as well along similar lines as the manual, except more > brief as is customary here). > > > > + > > Wformat-security > > C ObjC C++ ObjC++ Var(warn_format_security) Warning &g
[PATCH v2] c-format: Add -Wformat-int-precision option [PR80060]
This option is enabled by default when -Wformat option is enabled. A user can specify -Wno-format-int-precision to disable emitting warnings when passing an argument of an incompatible integer type to a 'd', 'i', 'o', 'u', 'x', or 'X' conversion specifier when it has the same precision as the expected type. Signed-off-by: Daniil Stas gcc/c-family/ChangeLog: * c-format.c (check_format_types): Don't emit warnings when passing an argument of an incompatible integer type to a 'd', 'i', 'o', 'u', 'x', or 'X' conversion specifier when it has the same precision as the expected type if -Wno-format-int-precision option is specified. * c.opt: Add -Wformat-int-precision option. gcc/ChangeLog: * doc/invoke.texi: Add -Wformat-int-precision option description. gcc/testsuite/ChangeLog: * c-c++-common/Wformat-int-precision-1.c: New test. * c-c++-common/Wformat-int-precision-2.c: New test. --- This is an update of patch "c-format: Add -Wformat-same-precision option [PR80060]". The changes comparing to the first patch version: - changed the option name to -Wformat-int-precision - changed the option description as was suggested by Martin - changed Wformat-int-precision-2.c to used dg-bogus instead of previous invalid syntax I also tried to combine the tests into one file with #pragma GCC diagnostic, but looks like it's not possible. I want to test that when passing just -Wformat option everything works as before my patch by default. And then in another test case to check that passing -Wno-format-int-precision disables the warning. But looks like in GCC you can't toggle the warnings such as -Wno-format-int-precision individually but only can disable the general -Wformat option that will disable all the formatting warnings together, which is not the proper test. gcc/c-family/c-format.c | 2 +- gcc/c-family/c.opt | 6 ++ gcc/doc/invoke.texi | 17 - .../c-c++-common/Wformat-int-precision-1.c | 7 +++ .../c-c++-common/Wformat-int-precision-2.c | 7 +++ 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/Wformat-int-precision-1.c create mode 100644 gcc/testsuite/c-c++-common/Wformat-int-precision-2.c diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c index ca66c81f716..dd4436929f8 100644 --- a/gcc/c-family/c-format.c +++ b/gcc/c-family/c-format.c @@ -4243,7 +4243,7 @@ check_format_types (const substring_loc &fmt_loc, && (!pedantic || i < 2) && char_type_flag) continue; - if (types->scalar_identity_flag + if ((types->scalar_identity_flag || !warn_format_int_precision) && (TREE_CODE (cur_type) == TREE_CODE (wanted_type) || (INTEGRAL_TYPE_P (cur_type) && INTEGRAL_TYPE_P (wanted_type))) diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 06457ac739e..f5b4af3f3f6 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -660,6 +660,12 @@ C ObjC C++ LTO ObjC++ Warning Alias(Wformat-overflow=, 1, 0) IntegerRange(0, 2) Warn about function calls with format strings that write past the end of the destination region. Same as -Wformat-overflow=1. +Wformat-int-precision +C ObjC C++ ObjC++ Var(warn_format_int_precision) Warning LangEnabledBy(C ObjC C++ ObjC++,Wformat=,warn_format >= 1, 0) +Warn when passing an argument of an incompatible integer type to a 'd', 'i', +'o', 'u', 'x', or 'X' conversion specifier even when it has the same precision +as the expected type. + 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. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 8b3ebcfbc4f..05dec6ba832 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -348,7 +348,7 @@ Objective-C and Objective-C++ Dialects}. -Werror -Werror=* -Wexpansion-to-defined -Wfatal-errors @gol -Wfloat-conversion -Wfloat-equal -Wformat -Wformat=2 @gol -Wno-format-contains-nul -Wno-format-extra-args @gol --Wformat-nonliteral -Wformat-overflow=@var{n} @gol +-Wformat-nonliteral -Wformat-overflow=@var{n} -Wformat-int-precision @gol -Wformat-security -Wformat-signedness -Wformat-truncation=@var{n} @gol -Wformat-y2k -Wframe-address @gol -Wframe-larger-than=@var{byte-size} -Wno-free-nonheap-object @gol @@ -6056,6 +6056,21 @@ If @option{-Wformat} is specified, also warn if the format string is not a string literal and so cannot be checked, unless the format function takes its format arguments as a @code{va_list}. +@item -Wformat-int-precision +@opindex Wformat-int-precision +@opindex Wno-format-int-precision +Warn when passing an argument of an incompatible integer type to +a @samp{d}, @samp{i}, @samp{o}, @samp{u}, @samp{x}, or @samp{X} conver
Re: [PATCH v2] c-format: Add -Wformat-int-precision option [PR80060]
On Sun, 10 Oct 2021 23:10:20 + Daniil Stas wrote: > This option is enabled by default when -Wformat option is enabled. A > user can specify -Wno-format-int-precision to disable emitting > warnings when passing an argument of an incompatible integer type to > a 'd', 'i', 'o', 'u', 'x', or 'X' conversion specifier when it has > the same precision as the expected type. > > Signed-off-by: Daniil Stas > > gcc/c-family/ChangeLog: > > * c-format.c (check_format_types): Don't emit warnings when > passing an argument of an incompatible integer type to > a 'd', 'i', 'o', 'u', 'x', or 'X' conversion specifier when > it has the same precision as the expected type if > -Wno-format-int-precision option is specified. > * c.opt: Add -Wformat-int-precision option. > > gcc/ChangeLog: > > * doc/invoke.texi: Add -Wformat-int-precision option > description. > > gcc/testsuite/ChangeLog: > > * c-c++-common/Wformat-int-precision-1.c: New test. > * c-c++-common/Wformat-int-precision-2.c: New test. > --- > This is an update of patch "c-format: Add -Wformat-same-precision > option [PR80060]". The changes comparing to the first patch version: > > - changed the option name to -Wformat-int-precision > - changed the option description as was suggested by Martin > - changed Wformat-int-precision-2.c to used dg-bogus instead of > previous invalid syntax > > I also tried to combine the tests into one file with #pragma GCC > diagnostic, but looks like it's not possible. I want to test that > when passing just -Wformat option everything works as before my patch > by default. And then in another test case to check that passing > -Wno-format-int-precision disables the warning. But looks like in GCC > you can't toggle the warnings such as -Wno-format-int-precision > individually but only can disable the general -Wformat option that > will disable all the formatting warnings together, which is not the > proper test. Hi, Can anyone review this patch? Thank you -- Daniil
[PATCH v4] c-format: Add -Wformat-int-precision option [PR80060]
This option is enabled by default when -Wformat option is enabled. A user can specify -Wno-format-int-precision to disable emitting warnings when passing an argument of an incompatible integer type to a 'd', 'i', 'b', 'B', 'o', 'u', 'x', or 'X' conversion specifier when it has the same precision as the expected type. Signed-off-by: Daniil Stas gcc/c-family/ChangeLog: * c-format.c (check_format_types): Don't emit warnings when passing an argument of an incompatible integer type to a 'd', 'i', 'b', 'B', 'o', 'u', 'x', or 'X' conversion specifier when it has the same precision as the expected type if -Wno-format-int-precision option is specified. * c.opt: Add -Wformat-int-precision option. gcc/ChangeLog: * doc/invoke.texi: Add -Wformat-int-precision option description. gcc/testsuite/ChangeLog: * c-c++-common/Wformat-int-precision-1.c: New test. * c-c++-common/Wformat-int-precision-2.c: New test. --- Changes for v4: - Added 'b' and 'B' format specifiers to the option descriptions. Changes for v3: - Added additional @code{} derictives to the documentation where needed. - Changed tests to run on "! long_neq_int" target instead of "lp64". - Added a test case to check that gcc still emits warnings for arguments with different precision even with -Wno-format-int-precision option enabled. Changes for v2: - Changed the option name to -Wformat-int-precision. - Changed the option description as was suggested by Martin. - Changed Wformat-int-precision-2.c to use dg-bogus instead of previous invalid syntax. gcc/c-family/c-format.c | 2 +- gcc/c-family/c.opt | 6 ++ gcc/doc/invoke.texi | 17 - .../c-c++-common/Wformat-int-precision-1.c | 7 +++ .../c-c++-common/Wformat-int-precision-2.c | 8 5 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/Wformat-int-precision-1.c create mode 100644 gcc/testsuite/c-c++-common/Wformat-int-precision-2.c diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c index e735e092043..c66787f931f 100644 --- a/gcc/c-family/c-format.c +++ b/gcc/c-family/c-format.c @@ -4248,7 +4248,7 @@ check_format_types (const substring_loc &fmt_loc, && (!pedantic || i < 2) && char_type_flag) continue; - if (types->scalar_identity_flag + if ((types->scalar_identity_flag || !warn_format_int_precision) && (TREE_CODE (cur_type) == TREE_CODE (wanted_type) || (INTEGRAL_TYPE_P (cur_type) && INTEGRAL_TYPE_P (wanted_type))) diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 4b8a094b206..d7d952765c6 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -684,6 +684,12 @@ C ObjC C++ LTO ObjC++ Warning Alias(Wformat-overflow=, 1, 0) IntegerRange(0, 2) Warn about function calls with format strings that write past the end of the destination region. Same as -Wformat-overflow=1. +Wformat-int-precision +C ObjC C++ ObjC++ Var(warn_format_int_precision) Warning LangEnabledBy(C ObjC C++ ObjC++,Wformat=,warn_format >= 1, 0) +Warn when passing an argument of an incompatible integer type to a 'd', 'i', +'b', 'B', 'o', 'u', 'x', or 'X' conversion specifier even when it has the same +precision as the expected type. + 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. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 3bddfbaae6a..94a7ad96c50 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -351,7 +351,7 @@ Objective-C and Objective-C++ Dialects}. -Werror -Werror=* -Wexpansion-to-defined -Wfatal-errors @gol -Wfloat-conversion -Wfloat-equal -Wformat -Wformat=2 @gol -Wno-format-contains-nul -Wno-format-extra-args @gol --Wformat-nonliteral -Wformat-overflow=@var{n} @gol +-Wformat-nonliteral -Wformat-overflow=@var{n} -Wformat-int-precision @gol -Wformat-security -Wformat-signedness -Wformat-truncation=@var{n} @gol -Wformat-y2k -Wframe-address @gol -Wframe-larger-than=@var{byte-size} -Wno-free-nonheap-object @gol @@ -6122,6 +6122,21 @@ If @option{-Wformat} is specified, also warn if the format string is not a string literal and so cannot be checked, unless the format function takes its format arguments as a @code{va_list}. +@item -Wformat-int-precision +@opindex Wformat-int-precision +@opindex Wno-format-int-precision +Warn when passing an argument of an incompatible integer type to +a @samp{d}, @samp{i}, @samp{b}, @samp{B}, @samp{o}, @samp{u}, @samp{x}, +or @samp{X} conversion specifier even when it has the same precision as +the expected type. For example, on targets where @code{int64_t} is a typedef +for @code{long}, the warning is issued for the @code{printf} call below
Re: [PATCH v3] c-format: Add -Wformat-int-precision option [PR80060]
On Tue, 23 Nov 2021 22:48:24 + Joseph Myers wrote: > On Tue, 23 Nov 2021, Daniil Stas via Gcc-patches wrote: > > > On Mon, 22 Nov 2021 20:35:03 + > > Joseph Myers wrote: > > > > > On Sun, 21 Nov 2021, Daniil Stas via Gcc-patches wrote: > > > > [...] > > > > > > I'd expect this to apply to 'b' and 'B' as well (affects commit > > > message, ChangeLog entry, option help string, documentation). > > > > > > > Hi Joseph, > > > > I can't find any description of these specifiers anywhere. And > > looks > > They're new specifiers in C23. See the most recent working draft > <http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2731.pdf>. > Ah, thank you. I've sent an updated patch.
Re: [PATCH v4] c-format: Add -Wformat-int-precision option [PR80060]
On Sat, 27 Nov 2021 22:18:23 + Daniil Stas wrote: > This option is enabled by default when -Wformat option is enabled. A > user can specify -Wno-format-int-precision to disable emitting > warnings when passing an argument of an incompatible integer type to > a 'd', 'i', 'b', 'B', 'o', 'u', 'x', or 'X' conversion specifier when > it has the same precision as the expected type. > > Signed-off-by: Daniil Stas > > gcc/c-family/ChangeLog: > > * c-format.c (check_format_types): Don't emit warnings when > passing an argument of an incompatible integer type to > a 'd', 'i', 'b', 'B', 'o', 'u', 'x', or 'X' conversion > specifier when it has the same precision as the expected type > if -Wno-format-int-precision option is specified. > * c.opt: Add -Wformat-int-precision option. > > gcc/ChangeLog: > > * doc/invoke.texi: Add -Wformat-int-precision option > description. > > gcc/testsuite/ChangeLog: > > * c-c++-common/Wformat-int-precision-1.c: New test. > * c-c++-common/Wformat-int-precision-2.c: New test. > --- > Changes for v4: > - Added 'b' and 'B' format specifiers to the option descriptions. > > Changes for v3: > - Added additional @code{} derictives to the documentation where > needed. > - Changed tests to run on "! long_neq_int" target instead of "lp64". > - Added a test case to check that gcc still emits warnings for > arguments with different precision even with > -Wno-format-int-precision option enabled. > > Changes for v2: > - Changed the option name to -Wformat-int-precision. > - Changed the option description as was suggested by Martin. > - Changed Wformat-int-precision-2.c to use dg-bogus instead of > previous invalid syntax. > > gcc/c-family/c-format.c | 2 +- > gcc/c-family/c.opt | 6 ++ > gcc/doc/invoke.texi | 17 > - .../c-c++-common/Wformat-int-precision-1.c | > 7 +++ .../c-c++-common/Wformat-int-precision-2.c | 8 > 5 files changed, 38 insertions(+), 2 deletions(-) > create mode 100644 > gcc/testsuite/c-c++-common/Wformat-int-precision-1.c create mode > 100644 gcc/testsuite/c-c++-common/Wformat-int-precision-2.c > > diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c > index e735e092043..c66787f931f 100644 > --- a/gcc/c-family/c-format.c > +++ b/gcc/c-family/c-format.c > @@ -4248,7 +4248,7 @@ check_format_types (const substring_loc > &fmt_loc, && (!pedantic || i < 2) > && char_type_flag) > continue; > - if (types->scalar_identity_flag > + if ((types->scalar_identity_flag || !warn_format_int_precision) > && (TREE_CODE (cur_type) == TREE_CODE (wanted_type) > || (INTEGRAL_TYPE_P (cur_type) > && INTEGRAL_TYPE_P (wanted_type))) > diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt > index 4b8a094b206..d7d952765c6 100644 > --- a/gcc/c-family/c.opt > +++ b/gcc/c-family/c.opt > @@ -684,6 +684,12 @@ C ObjC C++ LTO ObjC++ Warning > Alias(Wformat-overflow=, 1, 0) IntegerRange(0, 2) Warn about function > calls with format strings that write past the end of the destination > region. Same as -Wformat-overflow=1. > +Wformat-int-precision > +C ObjC C++ ObjC++ Var(warn_format_int_precision) Warning > LangEnabledBy(C ObjC C++ ObjC++,Wformat=,warn_format >= 1, 0) +Warn > when passing an argument of an incompatible integer type to a 'd', > 'i', +'b', 'B', 'o', 'u', 'x', or 'X' conversion specifier even when > it has the same +precision as the expected type. + > 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. diff --git > a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index > 3bddfbaae6a..94a7ad96c50 100644 --- a/gcc/doc/invoke.texi > +++ b/gcc/doc/invoke.texi > @@ -351,7 +351,7 @@ Objective-C and Objective-C++ Dialects}. > -Werror -Werror=* -Wexpansion-to-defined -Wfatal-errors @gol > -Wfloat-conversion -Wfloat-equal -Wformat -Wformat=2 @gol > -Wno-format-contains-nul -Wno-format-extra-args @gol > --Wformat-nonliteral -Wformat-overflow=@var{n} @gol > +-Wformat-nonliteral -Wformat-overflow=@var{n} > -Wformat-int-precision @gol -Wformat-security -Wformat-signedness > -Wformat-truncation=@var{n} @gol -Wformat-y2k -Wframe-address @gol > -Wframe-larger-than=@var{byte-size} -Wno-free-nonheap-object @gol > @@ -6122,6 +6122,21 @@ If @option{-Wformat} is specified, also warn > if the format string is not a string literal and so cannot be > checked, unless the format function takes its format arguments as a > @code{va_list}. > +@item -Wformat-int-precision > +@opindex Wformat-int-precision > +@opindex Wno-format-int-precision > +Warn when passing an argument of an incompatible integer type to > +a @samp{d}, @samp{i}, @samp{b}, @samp{B}, @samp{o}, @samp{u}, > @samp{x}, +or @samp{X} co
Re: [PATCH v4] c-format: Add -Wformat-int-precision option [PR80060]
On Tue, 21 Dec 2021 00:43:24 +0200 Daniil Stas wrote: > On Sat, 27 Nov 2021 22:18:23 + > Daniil Stas wrote: > > > This option is enabled by default when -Wformat option is enabled. A > > user can specify -Wno-format-int-precision to disable emitting > > warnings when passing an argument of an incompatible integer type to > > a 'd', 'i', 'b', 'B', 'o', 'u', 'x', or 'X' conversion specifier > > when it has the same precision as the expected type. > > > > Signed-off-by: Daniil Stas > > > > gcc/c-family/ChangeLog: > > > > * c-format.c (check_format_types): Don't emit warnings when > > passing an argument of an incompatible integer type to > > a 'd', 'i', 'b', 'B', 'o', 'u', 'x', or 'X' conversion > > specifier when it has the same precision as the expected > > type if -Wno-format-int-precision option is specified. > > * c.opt: Add -Wformat-int-precision option. > > > > gcc/ChangeLog: > > > > * doc/invoke.texi: Add -Wformat-int-precision option > > description. > > > > gcc/testsuite/ChangeLog: > > > > * c-c++-common/Wformat-int-precision-1.c: New test. > > * c-c++-common/Wformat-int-precision-2.c: New test. > > --- > > Changes for v4: > > - Added 'b' and 'B' format specifiers to the option descriptions. > > > > Changes for v3: > > - Added additional @code{} derictives to the documentation where > > needed. > > - Changed tests to run on "! long_neq_int" target instead of > > "lp64". > > - Added a test case to check that gcc still emits warnings for > > arguments with different precision even with > > -Wno-format-int-precision option enabled. > > > > Changes for v2: > > - Changed the option name to -Wformat-int-precision. > > - Changed the option description as was suggested by Martin. > > - Changed Wformat-int-precision-2.c to use dg-bogus instead of > > previous invalid syntax. > > > > gcc/c-family/c-format.c | 2 +- > > gcc/c-family/c.opt | 6 ++ > > gcc/doc/invoke.texi | 17 > > - .../c-c++-common/Wformat-int-precision-1.c | > > 7 +++ .../c-c++-common/Wformat-int-precision-2.c | 8 > > 5 files changed, 38 insertions(+), 2 deletions(-) > > create mode 100644 > > gcc/testsuite/c-c++-common/Wformat-int-precision-1.c create mode > > 100644 gcc/testsuite/c-c++-common/Wformat-int-precision-2.c > > > > diff --git a/gcc/c-family/c-format.c b/gcc/c-family/c-format.c > > index e735e092043..c66787f931f 100644 > > --- a/gcc/c-family/c-format.c > > +++ b/gcc/c-family/c-format.c > > @@ -4248,7 +4248,7 @@ check_format_types (const substring_loc > > &fmt_loc, && (!pedantic || i < 2) > > && char_type_flag) > > continue; > > - if (types->scalar_identity_flag > > + if ((types->scalar_identity_flag || > > !warn_format_int_precision) && (TREE_CODE (cur_type) == TREE_CODE > > (wanted_type) || (INTEGRAL_TYPE_P (cur_type) > > && INTEGRAL_TYPE_P (wanted_type))) > > diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt > > index 4b8a094b206..d7d952765c6 100644 > > --- a/gcc/c-family/c.opt > > +++ b/gcc/c-family/c.opt > > @@ -684,6 +684,12 @@ C ObjC C++ LTO ObjC++ Warning > > Alias(Wformat-overflow=, 1, 0) IntegerRange(0, 2) Warn about > > function calls with format strings that write past the end of the > > destination region. Same as -Wformat-overflow=1. > > +Wformat-int-precision > > +C ObjC C++ ObjC++ Var(warn_format_int_precision) Warning > > LangEnabledBy(C ObjC C++ ObjC++,Wformat=,warn_format >= 1, 0) +Warn > > when passing an argument of an incompatible integer type to a 'd', > > 'i', +'b', 'B', 'o', 'u', 'x', or 'X' conversion specifier even when > > it has the same +precision as the expected type. + > > 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. diff --git > > a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index > > 3bddfbaae6a..94a7ad96c50 100644 --- a/gcc/doc/invoke.texi > > +++ b/gcc/doc/invoke.texi > > @@ -351,7 +351,7 @@ Objective-C and Objective-C++ Dialects}. > > -Werror -Werror=* -Wexpansion-to-defined -Wfatal-errors @gol > > -Wfloat-conversion -Wfloat-equal -Wformat -Wformat=2 @gol > > -Wno-format-contains-nul -Wno-format-extra-args @gol > > --Wformat-nonliteral -Wformat-overflow=@var{n} @gol > > +-Wformat-nonliteral -Wformat-overflow=@var{n} > > -Wformat-int-precision @gol -Wformat-security -Wformat-signedness > > -Wformat-truncation=@var{n} @gol -Wformat-y2k -Wframe-address @gol > > -Wframe-larger-than=@var{byte-size} -Wno-free-nonheap-object @gol > > @@ -6122,6 +6122,21 @@ If @option{-Wformat} is specified, also warn > > if the format string is not a string literal and so cannot be > > checked, unless the format function takes its format arguments as a > > @code{va_list}. > > +@item -Wformat-int-precision > > +@opi