[PATCH] C-amily: Properly strip NOP_EXPR
H.J. Lu
hjl.tools@gmail.com
Sun Jan 13 05:05:00 GMT 2019
gcc/c-family/
PR c/51628
* c-warn.c (warn_for_address_or_pointer_of_packed_member): Move
NOP_EXPR check to ...
(check_and_warn_address_of_packed_member): Here.
gcc/testsuite/
PR c/51628
* c-c++-common/pr51628-33.c: New test.
---
gcc/c-family/c-warn.c | 6 +++---
gcc/testsuite/c-c++-common/pr51628-33.c | 19 +++++++++++++++++++
2 files changed, 22 insertions(+), 3 deletions(-)
create mode 100644 gcc/testsuite/c-c++-common/pr51628-33.c
diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c
index 79b2d8ad449..070934ab2b6 100644
--- a/gcc/c-family/c-warn.c
+++ b/gcc/c-family/c-warn.c
@@ -2755,6 +2755,9 @@ check_and_warn_address_of_packed_member (tree type, tree rhs)
while (TREE_CODE (rhs) == COMPOUND_EXPR)
rhs = TREE_OPERAND (rhs, 1);
+ if (TREE_CODE (rhs) == NOP_EXPR)
+ rhs = TREE_OPERAND (rhs, 0);
+
tree context = check_address_of_packed_member (type, rhs);
if (context)
{
@@ -2844,9 +2847,6 @@ warn_for_address_or_pointer_of_packed_member (bool convert_p, tree type,
/* Get the type of the pointer pointing to. */
type = TREE_TYPE (type);
- if (TREE_CODE (rhs) == NOP_EXPR)
- rhs = TREE_OPERAND (rhs, 0);
-
check_and_warn_address_of_packed_member (type, rhs);
}
}
diff --git a/gcc/testsuite/c-c++-common/pr51628-33.c b/gcc/testsuite/c-c++-common/pr51628-33.c
new file mode 100644
index 00000000000..0092f32202f
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/pr51628-33.c
@@ -0,0 +1,19 @@
+/* PR c/51628. */
+/* { dg-do compile } */
+/* { dg-options "-O" } */
+
+struct pair_t
+{
+ char x;
+ int i[4];
+} __attribute__ ((packed, aligned (4)));
+
+extern struct pair_t p;
+extern void bar (int *);
+
+void
+foo (struct pair_t *p)
+{
+ bar (p ? p->i : (int *) 0);
+/* { dg-warning "may result in an unaligned pointer value" "" { target *-*-* } .-1 } */
+}
--
2.20.1
More information about the Gcc-patches
mailing list