public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Bizarre warning about width of argument
@ 2002-12-27  3:31 Trevor Jenkins
  2002-12-27  3:37 ` Russ Allbery
  0 siblings, 1 reply; 4+ messages in thread
From: Trevor Jenkins @ 2002-12-27  3:31 UTC (permalink / raw)
  To: Gnu Compiler Collection Hackers

When the following example is compiled we get a warning:

foo.c: In function `foo':
foo.c:8: warning: passing arg 1 of `bar' with different width due to prototype

This test case is extracted from a large system where these "errors" are
being reported all over the place. The gcc comand being used is

gcc -c -ansi -fno-nonansi-builtins -Wshadow -Wconversion foo.c

Here's the real gcc command taken from the system make file:

gcc -c -Wall -Wpointer-arith -Wshadow -Wcast-qual -Wcast-align \
-Wconversion -Wid-clash-32 -Winline   -Woverloaded-virtual \
-Wmissing-prototypes -Wstrict-prototypes -Waggregate-return -Wno-implicit \
foo.c

and here's the simplest test case we can find as foo.c:

#include <string.h>

void foo(unsigned short);
void bar(unsigned short);

void foo(unsigned short zindex)
{
bar(zindex);
}

void bar(unsigned short a)
{
}

Regards, Trevor

British Sign Language is not inarticulate handwaving; it's a living language.
Support the campaign for formal recognition by the British government now!
Details at http://www.fdp.org.uk/

-- 

<>< Re: deemed!

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Bizarre warning about width of argument
  2002-12-27  3:31 Bizarre warning about width of argument Trevor Jenkins
@ 2002-12-27  3:37 ` Russ Allbery
  2002-12-28 19:50   ` [GCC] " Trevor Jenkins
  0 siblings, 1 reply; 4+ messages in thread
From: Russ Allbery @ 2002-12-27  3:37 UTC (permalink / raw)
  To: Gnu Compiler Collection Hackers

Trevor Jenkins <trevor.jenkins@suneidesis.com> writes:

> When the following example is compiled we get a warning:

> foo.c: In function `foo':
> foo.c:8: warning: passing arg 1 of `bar' with different width due to prototype

> This test case is extracted from a large system where these "errors" are
> being reported all over the place. The gcc comand being used is

> gcc -c -ansi -fno-nonansi-builtins -Wshadow -Wconversion foo.c

> and here's the simplest test case we can find as foo.c:

> #include <string.h>

> void foo(unsigned short);
> void bar(unsigned short);

> void foo(unsigned short zindex)
> {
> bar(zindex);
> }

> void bar(unsigned short a)
> {
> }

The warning is correct for what -Wconversion is for.  You probably don't
want to use this flag with regular programs.

`-Wconversion'
     Warn if a prototype causes a type conversion that is different
     from what would happen to the same argument in the absence of a
     prototype.  This includes conversions of fixed point to floating
     and vice versa, and conversions changing the width or signedness
     of a fixed point argument except when the same as the default
     promotion.

In other words, -Wconversion produces a warning if the presence of a
prototype causes an argument to be converted to a different type than if
there hadn't been a prototype.  It's intended for helping in converting
K&R C to ANSI C.  In your test case above, without a prototype the
unsigned short variable would be promoted to an int.

-Wconversion produces warnings about perfectly valid and stylistically
correct ANSI C.

-- 
Russ Allbery (rra@stanford.edu)             <http://www.eyrie.org/~eagle/>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [GCC] Re: Bizarre warning about width of argument
  2002-12-27  3:37 ` Russ Allbery
@ 2002-12-28 19:50   ` Trevor Jenkins
  2002-12-28 20:30     ` Russ Allbery
  0 siblings, 1 reply; 4+ messages in thread
From: Trevor Jenkins @ 2002-12-28 19:50 UTC (permalink / raw)
  To: Gnu Compiler Collection Hackers

On Thu, 26 Dec 2002, Russ Allbery <rra@stanford.edu> wrote:

> Trevor Jenkins <trevor.jenkins@suneidesis.com> writes:
>
> > When the following example is compiled we get a warning:
>
> > foo.c: In function `foo':
> > foo.c:8: warning: passing arg 1 of `bar' with different width due to prototype
>
> > gcc -c -ansi -fno-nonansi-builtins -Wshadow -Wconversion foo.c
>

> The warning is correct for what -Wconversion is for.  You probably don't
> want to use this flag with regular programs.

Thank you; yes that fixed that issue with the test case. However, there is
another issue which can also be demonstrated with a near identical test
case:

#include <string.h>

void foo(unsigned short);
void bar(unsigned short);

void foo(unsigned short index)
{
bar(index);
}

void bar(unsigned short a)
{
}

the gcc options are slightly different but closer to those used in the
actual system:

gcc -c -fno-builtin -Wshadow foo.c

producing

riley.c:6: warning: declaration of `index' shadows global declaration
riley.c: In function `foo':
riley.c:7: warning: declaration of `index' shadows global declaration

The function "index" is not defined in the C standard. However, it is
mentioned in the string.h include file. So the question is how do we
suppress the warning for non-standard functions whilst retaining the
warning for functions defined in the standard?

Regards, Trevor

British Sign Language is not inarticulate handwaving; it's a living language.
Support the campaign for formal recognition by the British government now!
Details at http://www.fdp.org.uk/

-- 

<>< Re: deemed!

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [GCC] Re: Bizarre warning about width of argument
  2002-12-28 19:50   ` [GCC] " Trevor Jenkins
@ 2002-12-28 20:30     ` Russ Allbery
  0 siblings, 0 replies; 4+ messages in thread
From: Russ Allbery @ 2002-12-28 20:30 UTC (permalink / raw)
  To: Gnu Compiler Collection Hackers

Trevor Jenkins <trevor.jenkins@suneidesis.com> writes:

> the gcc options are slightly different but closer to those used in the
> actual system:

> gcc -c -fno-builtin -Wshadow foo.c

> producing

> riley.c:6: warning: declaration of `index' shadows global declaration
> riley.c: In function `foo':
> riley.c:7: warning: declaration of `index' shadows global declaration

> The function "index" is not defined in the C standard. However, it is
> mentioned in the string.h include file. So the question is how do we
> suppress the warning for non-standard functions whilst retaining the
> warning for functions defined in the standard?

Compile with gcc -ansi (or gcc -std=c99 or something similar, or define
_POSIX_SOURCE or _XOPEN_SOURCE, or several other ways of accomplishing the
same thing).  Then the system headers shouldn't expose those symbols.  Of
course, then you won't be able to call non-standard functions at all.

This is the really annoying part of using -Wshadow, and is the reason why
I've considered stopping using it as a regular compile option when
building things in "all warnings" mode.  It's not really clear how useful
-Wshadow is overall; most of the things it uncovers are completely
harmless, like using the names of standard functions as the names of
arguments in prototypes.  Or things like using:

    struct sockaddr_in sin;

in a file that for one reason or another includes math.h.

-- 
Russ Allbery (rra@stanford.edu)             <http://www.eyrie.org/~eagle/>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-12-29  0:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-27  3:31 Bizarre warning about width of argument Trevor Jenkins
2002-12-27  3:37 ` Russ Allbery
2002-12-28 19:50   ` [GCC] " Trevor Jenkins
2002-12-28 20:30     ` Russ Allbery

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).