This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[C PATCH] Don't print -Waddress comparison warnings for macros (PR c/48778)
- From: Marek Polacek <polacek at redhat dot com>
- To: GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Tue, 1 Mar 2016 15:10:52 +0100
- Subject: [C PATCH] Don't print -Waddress comparison warnings for macros (PR c/48778)
- Authentication-results: sourceware.org; auth=none
This PR from 2011 reports that -Waddress prints unhelpful warning when the
comparison comes from a macro. Since I've added from_macro_expansion_at,
this is easy to circumvent. I'm not so sure we actually want to disable
the warning in the case of a macro, but probably yes.
Bootstrapped/regtested on x86_64-linux, ok for trunk or should I defer to
GCC 7?
2016-03-01 Marek Polacek <polacek@redhat.com>
PR c/48778
* c-typeck.c (build_binary_op): Don't issue -Waddress warnings
for macro expansions.
* gcc.dg/Waddress-2.c: New test.
diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index 6aa0f03..0f415e0 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -11087,7 +11087,8 @@ build_binary_op (location_t location, enum tree_code code,
else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
{
if (TREE_CODE (op0) == ADDR_EXPR
- && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
+ && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
+ && !from_macro_expansion_at (location))
{
if (code == EQ_EXPR)
warning_at (location,
@@ -11107,7 +11108,8 @@ build_binary_op (location_t location, enum tree_code code,
else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
{
if (TREE_CODE (op1) == ADDR_EXPR
- && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
+ && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
+ && !from_macro_expansion_at (location))
{
if (code == EQ_EXPR)
warning_at (location,
diff --git gcc/testsuite/gcc.dg/Waddress-2.c gcc/testsuite/gcc.dg/Waddress-2.c
index e69de29..4d927f6 100644
--- gcc/testsuite/gcc.dg/Waddress-2.c
+++ gcc/testsuite/gcc.dg/Waddress-2.c
@@ -0,0 +1,24 @@
+/* PR c/48778 */
+/* { dg-do compile } */
+/* { dg-options "-Waddress" } */
+
+#define NULL ((void *) 0)
+
+#define M1(b) ((b) != NULL ? 0 : (b))
+#define M2(b) ((b) == NULL ? 0 : (b))
+#define M3(b) (NULL != (b) ? 0 : (b))
+#define M4(b) (NULL == (b) ? 0 : (b))
+
+int
+func (int b)
+{
+ if (M1 (&b) > 0)
+ return 1;
+ if (M2 (&b) > 0)
+ return 2;
+ if (M3 (&b) > 0)
+ return 3;
+ if (M4 (&b) > 0)
+ return 4;
+ return 0;
+}
Marek