aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-family
diff options
context:
space:
mode:
authorPatrick Palka <ppalka@gcc.gnu.org>2015-06-13 16:11:15 +0000
committerPatrick Palka <ppalka@gcc.gnu.org>2015-06-13 16:11:15 +0000
commit076fecad0db4fb76524af6785b146491d4060dd4 (patch)
treee76304d58d1deaf94d7fd2dc50a09d6c2979c651 /gcc/c-family
parent313d38e359bfcd8b56c946233294061c66ae21dc (diff)
downloadgcc-076fecad0db4fb76524af6785b146491d4060dd4.zip
gcc-076fecad0db4fb76524af6785b146491d4060dd4.tar.gz
gcc-076fecad0db4fb76524af6785b146491d4060dd4.tar.bz2
Emit -Waddress warnings for comparing address of reference against NULL
gcc/c-family/ChangeLog: PR c++/65168 * c-common.c (c_common_truthvalue_conversion): Warn when converting an address of a reference to a truth value. gcc/cp/ChangeLog: PR c++/65168 * typeck.c (cp_build_binary_op): Warn when comparing an address of a reference against NULL. gcc/testsuite/ChangeLog: PR c++/65168 g++.dg/warn/Walways-true-3.C: New test. From-SVN: r224455
Diffstat (limited to 'gcc/c-family')
-rw-r--r--gcc/c-family/ChangeLog6
-rw-r--r--gcc/c-family/c-common.c14
2 files changed, 20 insertions, 0 deletions
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog
index a2324a4..80c3e48 100644
--- a/gcc/c-family/ChangeLog
+++ b/gcc/c-family/ChangeLog
@@ -1,3 +1,9 @@
+2015-06-13 Patrick Palka <ppalka@gcc.gnu.org>
+
+ PR c++/65168
+ * c-common.c (c_common_truthvalue_conversion): Warn when
+ converting an address of a reference to a truth value.
+
2015-06-08 Andrew MacLeod <amacleod@redhat.com>
* array-notation-common.c : Adjust include files.
diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 5b76567..b1af682 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -4973,6 +4973,20 @@ c_common_truthvalue_conversion (location_t location, tree expr)
tree totype = TREE_TYPE (expr);
tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
+ if (POINTER_TYPE_P (totype)
+ && TREE_CODE (fromtype) == REFERENCE_TYPE)
+ {
+ tree inner = expr;
+ STRIP_NOPS (inner);
+
+ if (DECL_P (inner))
+ warning_at (location,
+ OPT_Waddress,
+ "the compiler can assume that the address of "
+ "%qD will always evaluate to %<true%>",
+ inner);
+ }
+
/* 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