public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/53391] New: Slightly misleading warning on printf format mismatch
@ 2012-05-18 2:41 Keith.S.Thompson at gmail dot com
2012-05-18 9:27 ` [Bug c/53391] " jakub at gcc dot gnu.org
2012-05-18 18:27 ` Keith.S.Thompson at gmail dot com
0 siblings, 2 replies; 3+ messages in thread
From: Keith.S.Thompson at gmail dot com @ 2012-05-18 2:41 UTC (permalink / raw)
To: gcc-bugs
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: unassigned@gcc.gnu.org
ReportedBy: Keith.S.Thompson@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
^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug c/53391] Slightly misleading warning on printf format mismatch
2012-05-18 2:41 [Bug c/53391] New: Slightly misleading warning on printf format mismatch Keith.S.Thompson at gmail dot com
@ 2012-05-18 9:27 ` jakub at gcc dot gnu.org
2012-05-18 18:27 ` Keith.S.Thompson at gmail dot com
1 sibling, 0 replies; 3+ messages in thread
From: jakub at gcc dot gnu.org @ 2012-05-18 9:27 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53391
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jakub at gcc dot gnu.org
--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> 2012-05-18 08:31:59 UTC ---
This is not about trying to be clever, the useless conversion is long gone
before the warning is emitted.
^ permalink raw reply [flat|nested] 3+ messages in thread
* [Bug c/53391] Slightly misleading warning on printf format mismatch
2012-05-18 2:41 [Bug c/53391] New: Slightly misleading warning on printf format mismatch Keith.S.Thompson at gmail dot com
2012-05-18 9:27 ` [Bug c/53391] " jakub at gcc dot gnu.org
@ 2012-05-18 18:27 ` Keith.S.Thompson at gmail dot com
1 sibling, 0 replies; 3+ messages in thread
From: Keith.S.Thompson at gmail dot com @ 2012-05-18 18:27 UTC (permalink / raw)
To: gcc-bugs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53391
--- Comment #2 from Keith Thompson <Keith.S.Thompson at gmail dot com> 2012-05-18 18:26:31 UTC ---
The cleverness I was referring to was having diagnostics refer to an
appropriate typedef rather than to the original type (e.g., "size_t" rather
than "unsigned int"). I find this clever in a *good* way.
The conversion is a no-op if my_long happens to be the same type as long, as it
is in this example -- but it's very useful from the programmer's point of view
if my_long (or something with a more meaningful name) *could* be either long or
int.
I suppose solving this would require saving some information about the cast so
that the name of the type of the expression ("my_long" rather than "long") can
be referred to in diagnostic messages.
I should note that this is not production code. The original program was
intended to use "%ld" and convert the argument to long; I ran into the
misleading warning due to a logic error.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2012-05-18 18:27 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-18 2:41 [Bug c/53391] New: Slightly misleading warning on printf format mismatch Keith.S.Thompson at gmail dot com
2012-05-18 9:27 ` [Bug c/53391] " jakub at gcc dot gnu.org
2012-05-18 18:27 ` Keith.S.Thompson at gmail dot com
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).