http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53391

             Bug #: 53391
           Summary: Slightly misleading warning on printf format mismatch
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: keith.s.thomp...@gmail.com


Given the following program (c.c):

#include <stdio.h>
int main(void) {
    typedef long my_long;
    my_long x = 42;
    printf("x = %d\n", (long)x);
    return 0;
}

"gcc -c c.c" produces the following warning:

c.c: In function ‘main’:
c.c:5:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2
has type ‘my_long’ [-Wformat]

The argument is actually of type long, not my_long; the warning should say
"..., but argument 2 has type ‘long’".

Now because of the way typedefs work, long and my_long are really different
names for the same type. gcc is obviously trying to be clever about which name
makes for a clearer warning message. In this case, since x is explicitly
converted to long, referring to "long" would be less confusing. *Some* warning
is certainly appropriate, since "%d" requires an int argument.

(Speculation: gcc sees that the cast is a no-op, so it ignores it, which is
fine except that it results in this misleading warning message.)

Ubuntu x86 12.04
gcc (Ubuntu/Linaro 4.7.0-7ubuntu3) 4.7.0

Reply via email to