[Bug c/106039] Inconsistent error reporting for printf() when format string is a macro

jonathan.leffler at gmail dot com gcc-bugzilla@gcc.gnu.org
Tue Jun 21 02:52:17 GMT 2022


--- Comment #5 from Jonathan Leffler <jonathan.leffler at gmail dot com> ---
I got an email from pinskia — thank you.  I responded from an iPhone and got a
message back about 'HTML attachments are not allowed'.  I'm not sure if that
got through.  Anyway, what I said (tried to say) was:

Thank you for looking at this.

In the second and subsequent errors, the line number of the macro is used in
all three lines of the error report, whereas in the first, the second line of
the messages is the `printf()` line and not the macro line again.

Amplifying on that somewhat terse response — for example:

gcc-bug.c:5:23: error: format ‘%x’ expects argument of type ‘unsigned int’, but
argument 3 has type ‘long int’ [-Werror=format=]
    5 | #define PRT_BR_LN1    "%-21s %-16x %-16x %-18s %-18s\n"
gcc-bug.c:5:23: note: in definition of macro ‘PRT_BR_LN1’
    5 | #define PRT_BR_LN1    "%-21s %-16x %-16x %-18s %-18s\n"
gcc-bug.c:5:34: note: format string is defined here
    5 | #define PRT_BR_LN1    "%-21s %-16x %-16x %-18s %-18s\n"

All three lines reference the macro on line 5, whereas in the first error
messate, the middle part of the message refers to line 18 where the print
statement is found:

gcc-bug.c:4:23: error: format ‘%x’ expects argument of type ‘unsigned int’, but
argument 2 has type ‘long int’ [-Werror=format=]
    4 | #define PRT_BR_HDR    "%-16x\n"
gcc-bug.c:18:12: note: in expansion of macro ‘PRT_BR_HDR’
   18 |     printf(PRT_BR_HDR, (intptr_t) val1);
gcc-bug.c:4:28: note: format string is defined here
    4 | #define PRT_BR_HDR    "%-16x\n"

This is a lot more helpful than reporting the macro line 3 times.

More information about the Gcc-bugs mailing list