aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-common.c
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@google.com>2007-01-05 19:05:46 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2007-01-05 19:05:46 +0000
commitb3c6d2ea006d4c688964c044a081ddea7884148a (patch)
tree5758b8cb24576b1e3eb71016f81cfeab25bbd494 /gcc/c-common.c
parent97af925ba7b16e64c9f8b48cbfd5b4ccaa905f36 (diff)
downloadgcc-b3c6d2ea006d4c688964c044a081ddea7884148a.zip
gcc-b3c6d2ea006d4c688964c044a081ddea7884148a.tar.gz
gcc-b3c6d2ea006d4c688964c044a081ddea7884148a.tar.bz2
c-common.c (decl_with_nonnull_addr_p): New function.
./: * c-common.c (decl_with_nonnull_addr_p): New function. (c_common_truthvalue_conversion): Call it. * c-typeck.c (build_binary_op): Likewise. * c-common.h (decl_with_nonnull_addr_p): Declare. cp/: * typeck.c (build_binary_op): Warn about comparing a non-weak address to NULL. testsuite/: * gcc.dg/Walways-true-1.c: New test. * gcc.dg/Walways-true-2.c: New test. * g++.dg/warn/Walways-true-1.C: New test. * g++.dg/warn/Walways-true-2.C: New test. From-SVN: r120493
Diffstat (limited to 'gcc/c-common.c')
-rw-r--r--gcc/c-common.c35
1 files changed, 23 insertions, 12 deletions
diff --git a/gcc/c-common.c b/gcc/c-common.c
index 2fd2a81..1794bd8 100644
--- a/gcc/c-common.c
+++ b/gcc/c-common.c
@@ -2591,6 +2591,18 @@ pointer_int_sum (enum tree_code resultcode, tree ptrop, tree intop)
return fold_build2 (resultcode, result_type, ptrop, intop);
}
+/* Return whether EXPR is a declaration whose address can never be
+ NULL. */
+
+bool
+decl_with_nonnull_addr_p (tree expr)
+{
+ return (DECL_P (expr)
+ && (TREE_CODE (expr) == PARM_DECL
+ || TREE_CODE (expr) == LABEL_DECL
+ || !DECL_WEAK (expr)));
+}
+
/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
or for an `if' or `while' statement or ?..: exp. It should already
have been validated to be of suitable type; otherwise, a bad
@@ -2656,23 +2668,22 @@ c_common_truthvalue_conversion (tree expr)
case ADDR_EXPR:
{
tree inner = TREE_OPERAND (expr, 0);
- if (DECL_P (inner)
- && (TREE_CODE (inner) == PARM_DECL
- || TREE_CODE (inner) == LABEL_DECL
- || !DECL_WEAK (inner)))
+ if (decl_with_nonnull_addr_p (inner))
{
- /* Common Ada/Pascal programmer's mistake. We always warn
- about this since it is so bad. */
- warning (OPT_Walways_true, "the address of %qD will always evaluate as %<true%>",
+ /* Common Ada/Pascal programmer's mistake. */
+ warning (OPT_Walways_true,
+ "the address of %qD will always evaluate as %<true%>",
inner);
return truthvalue_true_node;
}
- /* If we are taking the address of an external decl, it might be
- zero if it is weak, so we cannot optimize. */
- if (DECL_P (inner)
- && DECL_EXTERNAL (inner))
- break;
+ /* If we still have a decl, it is possible for its address to
+ be NULL, so we cannot optimize. */
+ if (DECL_P (inner))
+ {
+ gcc_assert (DECL_WEAK (inner));
+ break;
+ }
if (TREE_SIDE_EFFECTS (inner))
return build2 (COMPOUND_EXPR, truthvalue_type_node,