Bug 1027 - slightly misleading printf format warning
Summary: slightly misleading printf format warning
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 minor
Target Milestone: 4.0.0
Assignee: Joseph S. Myers
URL:
Keywords:
: 12867 15657 (view as bug list)
Depends on:
Blocks:
 
Reported: 2000-12-09 17:46 UTC by cwitty
Modified: 2004-07-02 00:24 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2004-04-27 02:25:25


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description cwitty 2000-12-09 17:46:01 UTC
When you compile the following code with -c -O -Wall -g,
the warning messages include:
7: warning: char format, double arg (arg 2)
for the line
printf("%s", &d);
I find this warning misleading; I would prefer:
warning: char* format, double* arg (arg 2)

Release:
gcc version 2.7.2.3

Environment:
Debian GNU/Linux (slink); x86

How-To-Repeat:
#include <stdio.h>

void foo() {
  double d;

  printf("%c", d);
  printf("%s", &d);
  printf("%c", &d);
  printf("%s", d);
}
Comment 1 Joseph S. Myers 2000-12-10 04:59:32 UTC
Responsible-Changed-From-To: unassigned->jsm28
Responsible-Changed-Why: I'll fix this when the necessary diagnostics support
    (see messages sent to this PR) is available in GCC.
Comment 2 Joseph S. Myers 2000-12-10 04:59:32 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Confirmed as a bug.
Comment 3 Joseph S. Myers 2000-12-10 11:40:57 UTC
From: "Joseph S. Myers" <jsm28@cam.ac.uk>
To: <cwitty@newtonlabs.com>
Cc: <gcc-gnats@gcc.gnu.org>,  <gcc-bugs@gcc.gnu.org>
Subject: Re: c/1027: slightly misleading printf format warning
Date: Sun, 10 Dec 2000 11:40:57 +0000 (GMT)

 On 10 Dec 2000 cwitty@newtonlabs.com wrote:
 
 > When you compile the following code with -c -O -Wall -g,
 > the warning messages include:
 > 7: warning: char format, double arg (arg 2)
 > for the line
 > printf("%s", &d);
 > I find this warning misleading; I would prefer:
 > warning: char* format, double* arg (arg 2)
 
 The problem is that the code for printing type names when format checking
 tries to be too clever about working out the names to use and deciding
 that types are the same from their names when it had already decided that
 they were different types.  This leads to various other oddities with
 messages referring to "different type arg" when they could be more
 precise.
 
 The C++ front end includes general support for producing a name for an
 arbitrary type in diagnostics with the %T format specifier in cp_error,
 cp_pedwarn etc..  What's needed to fix this problem properly is for the C
 front end to get something along the lines of what the C++ front end has
 for formatting types; then the actual argument type could be formatted
 with %T.  (Adding these capabilities to the C front end could probably
 also lead to better diagnostics in various other places.)
 
 -- 
 Joseph S. Myers
 jsm28@cam.ac.uk
 

Comment 4 Joseph S. Myers 2000-12-10 12:09:45 UTC
From: "Joseph S. Myers" <jsm28@cam.ac.uk>
To: Gabriel Dos Reis <gdr@codesourcery.com>
Cc: <cwitty@newtonlabs.com>,  <gcc-gnats@gcc.gnu.org>, 
     <gcc-bugs@gcc.gnu.org>
Subject: Re: c/1027: slightly misleading printf format warning
Date: Sun, 10 Dec 2000 12:09:45 +0000 (GMT)

 On 10 Dec 2000, Gabriel Dos Reis wrote:
 
 > I've already put in the necessary machinery for so doing in
 > diagnostics.*.  What remains to be done is to replace error_with_decl()
 > and warning_with_decl() to error() and warning() (maybe error_at())
 > with use of those specifiers.  That was planed but I didn't get the
 > chance to finish the work.  Already on my TODO list but I would love
 > to assist any taker.
 
 What's also needed for the present PR is for the code in cp/error.c for
 formatting types to move to common code where C can use it as well.  (I'd
 guess that most of it can probably be shared, though stylistic differences
 such as "int*" for C++ versus "int *" for C should be allowed for.  Or, a
 version covering only C types might be a lot simpler.)  The existing code
 in c-lang.c
 
     case 'D':
     case 'F':
     case 'T':
       {
         const char *n = DECL_NAME (t)
           ? (*decl_printable_name) (t, 2)
           : "({anonymous})";
         output_add_string (buffer, n);
       }
       return 1;
 
 along with the default decl_printable_name / decl_name doesn't seem
 adequate for even the simple case of this PR (where a name for a pointer
 type should be constructed).
 
 -- 
 Joseph S. Myers
 jsm28@cam.ac.uk
 

Comment 5 Joseph S. Myers 2000-12-10 12:55:17 UTC
From: "Joseph S. Myers" <jsm28@cam.ac.uk>
To: Gabriel Dos_Reis <gdosreis@sophia.inria.fr>
Cc: Gabriel Dos Reis <gdr@codesourcery.com>,  <cwitty@newtonlabs.com>, 
     <gcc-gnats@gcc.gnu.org>,  <gcc-bugs@gcc.gnu.org>
Subject: Re: c/1027: slightly misleading printf format warning
Date: Sun, 10 Dec 2000 12:55:17 +0000 (GMT)

 On Sun, 10 Dec 2000, Gabriel Dos_Reis wrote:
 
 > Do you see a better short term solution?
 
 No.  I don't think this PR is release-critical, so fixing it can wait
 until the diagnostics support for type names is shared between C and C++.
 
 -- 
 Joseph S. Myers
 jsm28@cam.ac.uk
 
Comment 6 Gabriel Dos Reis 2000-12-10 12:57:16 UTC
From: Gabriel Dos Reis <gdr@codesourcery.com>
To: "Joseph S. Myers" <jsm28@cam.ac.uk>
Cc: <cwitty@newtonlabs.com>, <gcc-gnats@gcc.gnu.org>, <gcc-bugs@gcc.gnu.org>
Subject: Re: c/1027: slightly misleading printf format warning
Date: 10 Dec 2000 12:57:16 +0100

 "Joseph S. Myers" <jsm28@cam.ac.uk> writes:
 
 | On 10 Dec 2000 cwitty@newtonlabs.com wrote:
 | 
 | > When you compile the following code with -c -O -Wall -g,
 | > the warning messages include:
 | > 7: warning: char format, double arg (arg 2)
 | > for the line
 | > printf("%s", &d);
 | > I find this warning misleading; I would prefer:
 | > warning: char* format, double* arg (arg 2)
 | 
 | The problem is that the code for printing type names when format checking
 | tries to be too clever about working out the names to use and deciding
 | that types are the same from their names when it had already decided that
 | they were different types.  This leads to various other oddities with
 | messages referring to "different type arg" when they could be more
 | precise.
 | 
 | The C++ front end includes general support for producing a name for an
 | arbitrary type in diagnostics with the %T format specifier in cp_error,
 | cp_pedwarn etc..  What's needed to fix this problem properly is for the C
 | front end to get something along the lines of what the C++ front end has
 | for formatting types; then the actual argument type could be formatted
 | with %T.  (Adding these capabilities to the C front end could probably
 | also lead to better diagnostics in various other places.)
 
 I've already put in the necessary machinery for so doing in
 diagnostics.*.  What remains to be done is to replace error_with_decl()
 and warning_with_decl() to error() and warning() (maybe error_at())
 with use of those specifiers.  That was planed but I didn't get the
 chance to finish the work.  Already on my TODO list but I would love
 to assist any taker. 
 
 -- Gaby

Comment 7 Joseph S. Myers 2000-12-10 12:59:32 UTC
From: jsm28@gcc.gnu.org
To: cwitty@newtonlabs.com, gcc-gnats@gcc.gnu.org, jsm28@cam.ac.uk,
  jsm28@gcc.gnu.org, nobody@gcc.gnu.org
Cc:  
Subject: Re: c/1027
Date: 10 Dec 2000 12:59:32 -0000

 Synopsis: slightly misleading printf format warning
 
 Responsible-Changed-From-To: unassigned->jsm28
 Responsible-Changed-By: jsm28
 Responsible-Changed-When: Sun Dec 10 04:59:32 2000
 Responsible-Changed-Why:
     I'll fix this when the necessary diagnostics support
     (see messages sent to this PR) is available in GCC.
 State-Changed-From-To: open->analyzed
 State-Changed-By: jsm28
 State-Changed-When: Sun Dec 10 04:59:32 2000
 State-Changed-Why:
     Confirmed as a bug.
 
 http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=1027&database=gcc
Comment 8 gdosreis 2000-12-10 13:45:40 UTC
From: Gabriel Dos_Reis <gdosreis@sophia.inria.fr>
To: "Joseph S. Myers" <jsm28@cam.ac.uk>
Cc: Gabriel Dos Reis <gdr@codesourcery.com>, <cwitty@newtonlabs.com>,
   <gcc-gnats@gcc.gnu.org>, <gcc-bugs@gcc.gnu.org>
Subject: Re: c/1027: slightly misleading printf format warning
Date: Sun, 10 Dec 2000 13:45:40 +0100 (MET)

 | On 10 Dec 2000, Gabriel Dos Reis wrote:
 | 
 | > I've already put in the necessary machinery for so doing in
 | > diagnostics.*.  What remains to be done is to replace error_with_decl()
 | > and warning_with_decl() to error() and warning() (maybe error_at())
 | > with use of those specifiers.  That was planed but I didn't get the
 | > chance to finish the work.  Already on my TODO list but I would love
 | > to assist any taker.
 | 
 | What's also needed for the present PR is for the code in cp/error.c for
 | formatting types to move to common code where C can use it as well.  (I'd
 | guess that most of it can probably be shared, though stylistic differences
 | such as "int*" for C++ versus "int *" for C should be allowed for.  
 
 Certainly.  In C++ we tend to systematically print scopes and such.
 But it is true that all those differences can be parameterized through
 call-backs.  The codes in cp/error.c are still under development
 (although there has been no visible activity about them).
 In the long run cp_error() and cp_warning() are supposed to killed in
 favor of error() and warning().
 
 | ... Or, a
 | version covering only C types might be a lot simpler.)  The existing code
 | in c-lang.c
 | 
 |     case 'D':
 |     case 'F':
 |     case 'T':
 |       {
 |         const char *n = DECL_NAME (t)
 |           ? (*decl_printable_name) (t, 2)
 |           : "({anonymous})";
 |         output_add_string (buffer, n);
 |       }
 |       return 1;
 | 
 | along with the default decl_printable_name / decl_name doesn't seem
 | adequate for even the simple case of this PR (where a name for a pointer
 | type should be constructed).
 
 Do you see a better short term solution?
 
 -- Gaby
 CodeSourcery, LLC		http://www.codesourcery.com

Comment 9 Andrew Pinski 2003-05-24 23:26:34 UTC
still happens on the mainline (20030524):

pr1027.c:7: warning: int format, double arg (arg 2)
pr1027.c:8: warning: char format, double arg (arg 2)
pr1027.c:9: warning: int format, pointer arg (arg 2)
pr1027.c:10: warning: format argument is not a pointer (arg 2)
Comment 10 Joseph S. Myers 2003-11-01 10:21:30 UTC
*** Bug 12867 has been marked as a duplicate of this bug. ***
Comment 11 Andrew Pinski 2004-05-26 23:04:24 UTC
*** Bug 15657 has been marked as a duplicate of this bug. ***
Comment 12 Wolfgang Bangerth 2004-06-01 14:37:27 UTC
I also find the message overly terse. The abbreviated form "arg" for 
argument sounds too much like an unquoted reference to a variable. 
Why can't we speak English as in most other messages, for example 
  "The format contains a char*, but argument 2 is a float." 
? 
 
W. 
Comment 13 Gabriel Dos Reis 2004-06-01 15:04:08 UTC
Subject: Re:  slightly misleading printf format warning

"bangerth at dealii dot org" <gcc-bugzilla@gcc.gnu.org> writes:

| I also find the message overly terse. The abbreviated form "arg" for 

Agreed.

-- Gaby
Comment 14 Joseph S. Myers 2004-06-01 15:37:27 UTC
Subject: Re:  slightly misleading printf format warning

On Tue, 1 Jun 2004, bangerth at dealii dot org wrote:

> I also find the message overly terse. The abbreviated form "arg" for 
> argument sounds too much like an unquoted reference to a variable. 
> Why can't we speak English as in most other messages, for example 
>   "The format contains a char*, but argument 2 is a float." 
> ? 

How about something like

warning: format '%s' expects a char *, but argument 2 has type float *

(i.e., giving the particular format with bad argument type)?

The %T format isn't yet hooked up to the pretty-printer code for printing
types, once it is then improving these warnings will be straighforward.

Comment 15 Wolfgang Bangerth 2004-06-01 15:52:10 UTC
Sounds pretty good to me :-) 
W. 
Comment 16 Gabriel Dos Reis 2004-06-01 16:07:51 UTC
Subject: Re:  slightly misleading printf format warning

"jsm at polyomino dot org dot uk" <gcc-bugzilla@gcc.gnu.org> writes:

| Subject: Re:  slightly misleading printf format warning
| 
| On Tue, 1 Jun 2004, bangerth at dealii dot org wrote:
| 
| > I also find the message overly terse. The abbreviated form "arg" for 
| > argument sounds too much like an unquoted reference to a variable. 
| > Why can't we speak English as in most other messages, for example 
| >   "The format contains a char*, but argument 2 is a float." 
| > ? 
| 
| How about something like
| 
| warning: format '%s' expects a char *, but argument 2 has type float *
| 
| (i.e., giving the particular format with bad argument type)?
| 
| The %T format isn't yet hooked up to the pretty-printer code for printing
| types, once it is then improving these warnings will be straighforward.

Absolutely.  Apart from not using the GNU coding style, the
pretty-printer in gcc/c-pretty-printer.[hc] is ready for used in the
C front-end.  If you plan to use it before I complete the new C++
pretty-printer, please don't lose the style :-)

[Note: The current pretty-printer will print "char*" (C++ style)
       as opposed to "char *" (GNU style). ]

-- Gaby
 
Comment 17 CVS Commits 2004-07-01 08:52:38 UTC
Subject: Bug 1027

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	jsm28@gcc.gnu.org	2004-07-01 08:52:33

Modified files:
	gcc            : ChangeLog c-format.c c-lang.c c-objc-common.c 
	                 c-pretty-print.c c-tree.h 
	gcc/objc       : objc-lang.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/gcc.dg: Wswitch-enum.c Wswitch.c 
	gcc/testsuite/gcc.dg/format: branch-1.c diag-1.c multattr-3.c 
	                             xopen-1.c 
Added files:
	gcc/testsuite/gcc.dg/format: diag-2.c 

Log message:
	PR c/1027
	* c-lang.c (c_initialize_diagnostics): Move from here ...
	* c-objc-common.c: ... to here.  Include "c-pretty-print.h".
	(c_tree_printer): Use pretty-printer to format %T.
	* c-pretty-print.c (pp_c_specifier_qualifier_list): Include space
	before '*' if not C++.
	(pp_c_direct_abstract_declarator): Don't try to print array upper
	bound for flexible array members.
	* c-tree.h: Include "diagnostic.h".
	(c_initialize_diagnostics): Declare.
	* objc/objc-lang.c (LANG_HOOKS_INITIALIZE_DIAGNOSTICS): Define.
	* c-format.c (format_type_warning): New function.  Improve
	diagnostics for incorrect format argument types.
	(check_format_types): Use it.  Add two parameters.  Use the
	TYPE_MAIN_VARIANT of wanted_type.
	(check_format_info_main): Pass new parameters to
	check_format_types.
	(struct format_wanted_type): Update comment.
	
	testsuite:
	* gcc.dg/Wswitch-enum.c, gcc.dg/Wswitch.c,
	gcc.dg/format/branch-1.c, gcc.dg/format/diag-1.c,
	gcc.dg/format/multattr-3.c, gcc.dg/format/xopen-1.c: Update
	expected warning text.
	* gcc.dg/format/diag-2.c: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.4240&r2=2.4241
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/c-format.c.diff?cvsroot=gcc&r1=1.58&r2=1.59
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/c-lang.c.diff?cvsroot=gcc&r1=1.127&r2=1.128
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/c-objc-common.c.diff?cvsroot=gcc&r1=1.49&r2=1.50
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/c-pretty-print.c.diff?cvsroot=gcc&r1=1.50&r2=1.51
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/c-tree.h.diff?cvsroot=gcc&r1=1.157&r2=1.158
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/objc/objc-lang.c.diff?cvsroot=gcc&r1=1.45&r2=1.46
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.3930&r2=1.3931
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/Wswitch-enum.c.diff?cvsroot=gcc&r1=1.3&r2=1.4
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/Wswitch.c.diff?cvsroot=gcc&r1=1.4&r2=1.5
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/format/diag-2.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/format/branch-1.c.diff?cvsroot=gcc&r1=1.2&r2=1.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/format/diag-1.c.diff?cvsroot=gcc&r1=1.2&r2=1.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/format/multattr-3.c.diff?cvsroot=gcc&r1=1.1&r2=1.2
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/format/xopen-1.c.diff?cvsroot=gcc&r1=1.4&r2=1.5

Comment 18 Andrew Pinski 2004-07-02 00:24:23 UTC
Fixed.