gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Can not get gcc to warn about implicit un/signed casting

#122722 - sgeos - Wed Mar 21, 2007 3:17 pm

Split from "Is this font readable?"

HyperHacker wrote:
sgeos wrote:
How do I get it to complain? It should complain, but this didn't for me:
Maybe it only does that with hex numbers then.

This produced no warnings:
Code:
int main(void)
{
        signed int   sh  = 0xffffffff;
        signed int   sn  = -1;
        unsigned int uh  = 0xffffffff;
        unsigned int un  = -1;
        return sh + sn + uh + un ;
}

Code:
USER@Stardust ~/c/test
$ gcc -Wall -Wextra -pedantic -Werror -o test test.c

USER@Stardust ~/c/test
$ gcc -Wall -Wextra -ansi -pedantic -Werror -o test test.c

USER@Stardust ~/c/test
$

Other compilers may complain about these implicit casts and gcc should complain about them.
If it does, I don't know how to get gcc to complain. Does this behavior conform to the standard?

-Brendan

#122759 - Quirky - Wed Mar 21, 2007 9:11 pm

-Wconversion should do it.

#122765 - HyperHacker - Wed Mar 21, 2007 10:44 pm

Try -0x00000001, I've seen that before. (Not in my code, I think in GBA_NDS_FAT.)
_________________
I'm a PSP hacker now, but I still <3 DS.

#122768 - tepples - Wed Mar 21, 2007 11:27 pm

The code for an unsigned all-bits-true value is ~0U.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#122790 - sgeos - Thu Mar 22, 2007 2:48 am

-Wconversion works. Funny that this isn't covered in -Wall or -Wextra. I wonder if there are any other warning that really should be turned on. Admittedly, that is easy enough to figure out. (man gcc)
Code:
USER@Stardust ~/c/test
$ gcc -Wall -Wextra -ansi -pedantic -Werror -o test test.c

USER@Stardust ~/c/test
$ gcc -Wall -Wextra -ansi -pedantic -Werror -Wconversion -o test test.c
test.c: In function `main':
test.c:9: warning: negative integer implicitly converted to unsigned type
test.c:12: warning: passing arg 1 of `func' as unsigned due to prototype

USER@Stardust ~/c/test
$ gcc -Wconversion -o test test.c
test.c: In function `main':
test.c:9: warning: negative integer implicitly converted to unsigned type
test.c:12: warning: passing arg 1 of `func' as unsigned due to prototype

USER@Stardust ~/c/test
$

Code:
unsigned int func(unsigned int pU, signed int pS)
{
        return pU + pS;
}

int main(void)
{
        signed int   a = ~0L;
        unsigned int b = -1;                    /* ling 9 warning */
        unsigned int c = (unsigned int) -1;     /* fixes the warning */
        return
                func(a, a) +                    /* ling 12 warning */
                func((unsigned)a, a) +          /* fixes the warning */
                b + c;
}

-Brendan

#122815 - kusma - Thu Mar 22, 2007 4:26 pm

yes, but also keep in mind that gcc < 4.3 is kind of broken on float-handling in -Wconversion:

test.c:
Code:

float func(float f);

float test(float f)
{
   return func(f);
}


Code:

$ arm-eabi-gcc -c -Wconversion test.c -o test.o
test.c: In function 'test':
test.c:5: warning: passing argument 1 of 'func' as 'float' rather than 'double' due to prototype


There's some kind of reasoning for this here, http://gcc.gnu.org/wiki/NewWconversion but I can't really see that I get why this was done to begin with.

If anyone knows a workaround, please let me know ;)