https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65168
--- Comment #3 from Manuel López-Ibáñez <manu at gcc dot gnu.org> ---
I got confused by Clang's message. The problem is not the pointer or NULL. The
problem is converting the address of a reference to bool:
int fii(int *p) {
int &r=*p;
return !&r;
}
This is trivial to implement in GCC:
manuel@gcc14:~$ ~/test1/221557M/build/gcc/cc1plus -Waddress ~/pr65168.cc
/home/manuel/pr65168.cc:3:12: warning: address of reference ‘r’ may be assumed
to always convert to true because in C++ a reference cannot be bound to
dereferenced null pointer [-Waddress]
return !&r;
^
Does this patch work in your real-world code?
Index: c-common.c
===================================================================
--- c-common.c (revision 221557)
+++ c-common.c (working copy)
@@ -4776,11 +4776,18 @@ c_common_truthvalue_conversion (location
}
CASE_CONVERT:
{
tree totype = TREE_TYPE (expr);
- tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
+ tree op0 = TREE_OPERAND (expr, 0);
+ tree fromtype = TREE_TYPE (op0);
+
+ if (TREE_CODE (fromtype) == REFERENCE_TYPE
+ && POINTER_TYPE_P (totype) && DECL_P (op0))
+ warning_at (location, OPT_Waddress,
+ "address of reference %qD may be assumed to always
convert to true"
+ " because in C++ a reference cannot be bound to
dereferenced null pointer", op0);
/* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
since that affects how `default_conversion' will behave. */
if (TREE_CODE (totype) == REFERENCE_TYPE
|| TREE_CODE (fromtype) == REFERENCE_TYPE)