Bug 5310

Summary: Weird warnings about using (int)NULL
Product: gcc Reporter: Ralph Loader <suckfish>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: bonzini, gcc-bugs, gdr, manu, nathan
Priority: P3 Keywords: diagnostic
Version: 3.0.3   
Target Milestone: 4.3.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2006-03-01 02:15:18
Attachments: temp.cc

Description Ralph Loader 2002-01-07 22:46:00 UTC
The attached file, compiled with g++ -Wall generates:

temp.cc: In function `void bar()':
temp.cc:8: warning: passing NULL used for non-pointer argument passing 1 of 
   `void foo(int)'
temp.cc:8: warning: argument to non-pointer type `int' from NULL

I think the warning message is bogus.  The argument expression has been cast to an int, so the compiler should not warn about using it as an int argument.

If it is decided that the warning message from line 8 is in fact correct, then line 9 should also give a warning (using NULL as a long is just as silly as using NULL as an int).

Release:
3.0.3

Environment:
Debian GNU Linux, i686.
Debian gcc package gcc-3.0-3.0.3-1

How-To-Repeat:
Compile attached file with g++ -Wall
Comment 1 Nathan Sidwell 2002-09-15 12:25:11 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: confirmed.
Comment 2 Andrew Pinski 2003-05-28 13:36:07 UTC
On the mainline (20030526), I only get one warning:

pr5310.cc: In function `void bar()':
pr5310.cc:9: warning: passing NULL used for non-pointer argument 1 of `void 
   foo(int)'

Here is the proprocessed form of the testcase:
void foo (int);
void foo (long);

void bar()
{
   foo ((int)__null);
   foo ((long)__null);
}
Comment 3 Gabriel Dos Reis 2005-11-30 19:15:08 UTC
(In reply to comment #2)
> On the mainline (20030526), I only get one warning:
> 
> pr5310.cc: In function `void bar()':
> pr5310.cc:9: warning: passing NULL used for non-pointer argument 1 of `void 
>    foo(int)'
> 
> Here is the proprocessed form of the testcase:
> void foo (int);
> void foo (long);
> 
> void bar()
> {
>    foo ((int)__null);
>    foo ((long)__null);
> }


The issue here has several roots:
   (1) cp/call.c:convert_like_real() should warn only if !c_cast_p;
   (2) convert_like_real() was called (as convert_like_with_context)
       with c_cast_p set to false; which is one source of the bug
   (3) since __null is of type int, the cast to int was a no-op, and
       since the C++ front-end currently does not have a high level
       representation of the program (e.g. lowering is done as part of
       parsing), it does not have ways to make the difference.

Patches to correct any point above will be a progress.
 
Comment 4 Manuel López-Ibáñez 2007-01-09 15:30:06 UTC
(convert_like_real gives me the creeps.) 

I must check whether this warning has been taken by the new Wconversion, which is not currently enabled by Wall.
Comment 5 Manuel López-Ibáñez 2007-05-23 01:49:32 UTC
(In reply to comment #3)

>    (3) since __null is of type int, the cast to int was a no-op, and
>        since the C++ front-end currently does not have a high level
>        representation of the program (e.g. lowering is done as part of
>        parsing), it does not have ways to make the difference.
> 

Where is lowering performed? It actually seems that the cast (long)__null is performed somewhere (where?) while parsing, while (int)__null is just accepted as __null. I tried to follow up the code with just long i = (long) __null; but the C++ front-end is impossible to debug for me. It just jumps again and again to the same places. Is there any trick to debug this kind of thing?
Comment 6 Manuel López-Ibáñez 2007-05-23 01:50:49 UTC
(In reply to comment #4)
> (convert_like_real gives me the creeps.) 
> 
> I must check whether this warning has been taken by the new Wconversion, which
> is not currently enabled by Wall.
> 

By the way, this is not enabled by Wall anymore. You need Wconversion for this.
Comment 7 Manuel López-Ibáñez 2007-11-12 01:14:47 UTC
*** Bug 33160 has been marked as a duplicate of this bug. ***
Comment 8 patchapp@dberlin.org 2007-11-15 20:05:39 UTC
Subject: Bug number PR c++/5310

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-11/msg00865.html
Comment 9 Manuel López-Ibáñez 2007-11-23 19:15:20 UTC
Subject: Bug 5310

Author: manu
Date: Fri Nov 23 19:15:09 2007
New Revision: 130381

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130381
Log:
2007-11-23  Mark Mitchell  <mark@codesourcery.com>
	    Manuel Lopez-Ibanez  <manu@gcc.gnu.org>
	
	PR c++/5310
cp/
	* call.c (convert_like_real): Build a zero constant when __null is
	converted to an integer type.
testsuite/
	* g++.dg/warn/pr5310.C: New.
	* g++.dg/warn/pr33160.C: New

Added:
    trunk/gcc/testsuite/g++.dg/warn/pr33160.C
    trunk/gcc/testsuite/g++.dg/warn/pr5310.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/call.c
    trunk/gcc/testsuite/ChangeLog

Comment 10 Manuel López-Ibáñez 2007-11-23 19:16:17 UTC
Fixed in GCC 4.3