aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-family
diff options
context:
space:
mode:
authorJason Merrill <jason@redhat.com>2023-11-22 13:20:58 -0500
committerJason Merrill <jason@redhat.com>2023-12-13 15:34:13 -0500
commitb7e4a4c626eeeb32c291d5bbbaa148c5081b6bfd (patch)
treecde073c8479e884ecb5dc8745b2afd56c0995c3f /gcc/c-family
parent7362543f00c9d3359d9377b800080fb421414ee5 (diff)
downloadgcc-b7e4a4c626eeeb32c291d5bbbaa148c5081b6bfd.zip
gcc-b7e4a4c626eeeb32c291d5bbbaa148c5081b6bfd.tar.gz
gcc-b7e4a4c626eeeb32c291d5bbbaa148c5081b6bfd.tar.bz2
c-family: -Waddress-of-packed-member and casts
-Waddress-of-packed-member, in addition to the documented warning about actually taking the address of a packed member, also warns about casting from a pointer to a TYPE_PACKED type to a pointer to a type with greater alignment. This wrongly warns if the source is a pointer to enum when -fshort-enums is on, since that is also represented by TYPE_PACKED. And there's already -Wcast-align to catch casting from pointer to less aligned type (packed or otherwise) to pointer to more aligned type; even apart from the enum problem, this seems like a somewhat arbitrary subset of that warning. So, this patch removes the undocumented type-based warning from -Waddress-of-packed-member. Some of the tests where the warning is desirable I changed to use -Wcast-align=strict instead. The ones that require -Wno-incompatible-pointer-types I just removed. gcc/c-family/ChangeLog: * c-warn.cc (check_address_or_pointer_of_packed_member): Remove warning based on TYPE_PACKED. gcc/testsuite/ChangeLog: * c-c++-common/Waddress-of-packed-member-1.c: Don't expect a warning on the cast cases. * c-c++-common/pr51628-35.c: Use -Wcast-align=strict. * g++.dg/warn/Waddress-of-packed-member3.C: Likewise. * gcc.dg/pr88928.c: Likewise. * gcc.dg/pr51628-20.c: Removed. * gcc.dg/pr51628-21.c: Removed. * gcc.dg/pr51628-25.c: Removed.
Diffstat (limited to 'gcc/c-family')
-rw-r--r--gcc/c-family/c-warn.cc58
1 files changed, 3 insertions, 55 deletions
diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc
index d2938b9..2a399ba 100644
--- a/gcc/c-family/c-warn.cc
+++ b/gcc/c-family/c-warn.cc
@@ -2991,10 +2991,9 @@ check_alignment_of_packed_member (tree type, tree field, bool rvalue)
return NULL_TREE;
}
-/* Return struct or union type if the right hand value, RHS:
- 1. Is a pointer value which isn't aligned to a pointer type TYPE.
- 2. Is an address which takes the unaligned address of packed member
- of struct or union when assigning to TYPE.
+/* Return struct or union type if the right hand value, RHS
+ is an address which takes the unaligned address of packed member
+ of struct or union when assigning to TYPE.
Otherwise, return NULL_TREE. */
static tree
@@ -3021,57 +3020,6 @@ check_address_or_pointer_of_packed_member (tree type, tree rhs)
type = TREE_TYPE (type);
- if (TREE_CODE (rhs) == PARM_DECL
- || VAR_P (rhs)
- || TREE_CODE (rhs) == CALL_EXPR)
- {
- tree rhstype = TREE_TYPE (rhs);
- if (TREE_CODE (rhs) == CALL_EXPR)
- {
- rhs = CALL_EXPR_FN (rhs); /* Pointer expression. */
- if (rhs == NULL_TREE)
- return NULL_TREE;
- rhs = TREE_TYPE (rhs); /* Pointer type. */
- /* We could be called while processing a template and RHS could be
- a functor. In that case it's a class, not a pointer. */
- if (!rhs || !POINTER_TYPE_P (rhs))
- return NULL_TREE;
- rhs = TREE_TYPE (rhs); /* Function type. */
- rhstype = TREE_TYPE (rhs);
- if (!rhstype || !POINTER_TYPE_P (rhstype))
- return NULL_TREE;
- rvalue = true;
- }
- if (rvalue && POINTER_TYPE_P (rhstype))
- rhstype = TREE_TYPE (rhstype);
- while (TREE_CODE (rhstype) == ARRAY_TYPE)
- rhstype = TREE_TYPE (rhstype);
- if (TYPE_PACKED (rhstype))
- {
- unsigned int type_align = min_align_of_type (type);
- unsigned int rhs_align = min_align_of_type (rhstype);
- if (rhs_align < type_align)
- {
- auto_diagnostic_group d;
- location_t location = EXPR_LOC_OR_LOC (rhs, input_location);
- if (warning_at (location, OPT_Waddress_of_packed_member,
- "converting a packed %qT pointer (alignment %d) "
- "to a %qT pointer (alignment %d) may result in "
- "an unaligned pointer value",
- rhstype, rhs_align, type, type_align))
- {
- tree decl = TYPE_STUB_DECL (rhstype);
- if (decl)
- inform (DECL_SOURCE_LOCATION (decl), "defined here");
- decl = TYPE_STUB_DECL (type);
- if (decl)
- inform (DECL_SOURCE_LOCATION (decl), "defined here");
- }
- }
- }
- return NULL_TREE;
- }
-
tree context = NULL_TREE;
/* Check alignment of the object. */