https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78183
Bug ID: 78183
Summary: Silence warnings about alternate forms of custom
format specifiers
Product: gcc
Version: 5.4.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: drazen.kacar at tereo dot hr
Target Milestone: ---
Glibc allows writing custom printf format conversions, so I've implemented a
conversion which takes char * and prints the string with all non-printable
characters in \xxx notation. This is very useful for printing debugging
output (I'm currently writing a lot of unit tests) and logging.
However, if I register it with unused conversion specifier (eg. %W) then gcc
produces a warning. This has already been reported in bug 47781, but doesn't
look like it will be resolved soon.
Another option is to use %s and implement my version as an alternate form,
so it would be used with the %#s specifier. Then the compiler would now how to
check arguments without any code modifications. However, if I do this gcc
warns about '#' flag being used with %s.
For example (function which implements conversion not shown):
prompt> tail -17 myprintf.c
int
main()
{
char *str = "burek\r\n";
printf("Before registering: %#s\n", str);
if(register_printf_specifier ('s', str_log, str_log_arginfo))
{
fputs("Cannot register %s specifier", stderr);
exit(1);
}
printf("%s\n", str);
printf("%#s\n", str);
return 0;
}
prompt> gcc -o myprintf myprintf.c
myprintf.c: In function 'main':
myprintf.c:94:12: warning: '#' flag used with '%s' gnu_printf format
[-Wformat=]
printf("Before registering: %#s\n", str);
^
myprintf.c:102:12: warning: '#' flag used with '%s' gnu_printf format
[-Wformat=]
printf("%#s\n", str);
^
prompt> ./myprintf
Before registering: burek
burek
burek\r\n
=== End example ===
While we're waiting for the full implementation of the problem from bug 47781
would it be possible to implement something that would silence warnings for
alternate forms which aren't specified by a standard?
I can see two approaches:
a) Don't produce warning about this by default and introduce a compiler flag to
turn it on.
b) Introduce another compiler flag (eg. -Wno-format-alt-flag) which would
silence this specific warning.
I tried to find command line option which would disable specific warning
identified by a number or a name, but couldn't find it.