This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: PATCH RFA: C++ frontend warning: comparing function pointer to NULL


> 
> Ian
> 
> 
> cp/ChangeLog:
> 2007-01-04  Ian Lance Taylor  <iant@google.com>
> 
> 	* typeck.c (build_binary_op): Warn about comparing a non-weak
> 	address to NULL.
> 
> testsuite/ChangeLog:
> 2007-01-04  Ian Lance Taylor  <iant@google.com>
> 
> 	* g++.dg/warn/Walways-true-1.C: New test.
> 	* g++.dg/warn/Walways-true-2.C: New test.
> 
> 
> Index: gcc/cp/typeck.c
> ===================================================================
> --- gcc/cp/typeck.c	(revision 120376)
> +++ gcc/cp/typeck.c	(working copy)
> @@ -3334,10 +3334,24 @@ build_binary_op (enum tree_code code, tr
>  					      "comparison");
>        else if ((code0 == POINTER_TYPE || TYPE_PTRMEM_P (type0))
>  	       && null_ptr_cst_p (op1))
> -	result_type = type0;
> +	{
> +	  if (TREE_CODE (op0) == ADDR_EXPR
> +	      && DECL_P (TREE_OPERAND (op0, 0)) 
> +	      && !DECL_WEAK (TREE_OPERAND (op0, 0)))
> +	    warning (OPT_Walways_true, "the address of %qD will never be NULL",
> +		     TREE_OPERAND (op0, 0));

I remember when the C code was added for this warning (which looked almost
the same), we would ICE for code like:

int f(int a)
{
  return (&a) == 0;
}

Looking at the above code looks like we would ICE in the C++ code with the
above testcase.
The C front-end checks for PARM and LABEL decls too:
          if (TREE_CODE (op0) == ADDR_EXPR 
              && DECL_P (TREE_OPERAND (op0, 0))
              && (TREE_CODE (TREE_OPERAND (op0, 0)) == PARM_DECL
                  || TREE_CODE (TREE_OPERAND (op0, 0)) == LABEL_DECL
                  || !DECL_WEAK (TREE_OPERAND (op0, 0))))

Could you add a testcase for the argument and label cases and make sure
we don't ICE on them?

Thanks,
Andrew Pinski


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]