From: Jason Merrill <jason@redhat.com>
To: "H.J. Lu" <hjl.tools@gmail.com>,
Richard Guenther <richard.guenther@gmail.com>
Cc: "Joseph S. Myers" <joseph@codesourcery.com>,
Martin Sebor <msebor@gmail.com>,
GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: V6 [PATCH] C/C++: Add -Waddress-of-packed-member
Date: Mon, 17 Dec 2018 13:53:00 -0000 [thread overview]
Message-ID: <bd7ff807-0918-9d66-8043-969d4d2ff8b0@redhat.com> (raw)
In-Reply-To: <CAMe9rOq93B0tO_SkJ=aPP4rYfDG8pf4+_-7WhKy60mn5PCrEZw@mail.gmail.com>
On 12/17/18 7:42 AM, H.J. Lu wrote:
> On Mon, Dec 17, 2018 at 1:39 AM Richard Biener
> <richard.guenther@gmail.com> wrote:
>>
>> On Fri, Dec 14, 2018 at 11:48 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>>>
>>> On Fri, Dec 14, 2018 at 2:10 PM Jason Merrill <jason@redhat.com> wrote:
>>>>
>>>> On 12/13/18 6:56 PM, H.J. Lu wrote:
>>>>> On Thu, Dec 13, 2018 at 12:50 PM Jason Merrill <jason@redhat.com> wrote:
>>>>>>
>>>>>> On 9/25/18 11:46 AM, H.J. Lu wrote:
>>>>>>> On Fri, Aug 31, 2018 at 2:04 PM, Jason Merrill <jason@redhat.com> wrote:
>>>>>>>> On 07/23/2018 05:24 PM, H.J. Lu wrote:
>>>>>>>>>
>>>>>>>>> On Mon, Jun 18, 2018 at 12:26 PM, Joseph Myers <joseph@codesourcery.com>
>>>>>>>>> wrote:
>>>>>>>>>>
>>>>>>>>>> On Mon, 18 Jun 2018, Jason Merrill wrote:
>>>>>>>>>>
>>>>>>>>>>> On Mon, Jun 18, 2018 at 11:59 AM, Joseph Myers <joseph@codesourcery.com>
>>>>>>>>>>> wrote:
>>>>>>>>>>>>
>>>>>>>>>>>> On Mon, 18 Jun 2018, Jason Merrill wrote:
>>>>>>>>>>>>
>>>>>>>>>>>>>> + if (TREE_CODE (rhs) == COND_EXPR)
>>>>>>>>>>>>>> + {
>>>>>>>>>>>>>> + /* Check the THEN path first. */
>>>>>>>>>>>>>> + tree op1 = TREE_OPERAND (rhs, 1);
>>>>>>>>>>>>>> + context = check_address_of_packed_member (type, op1);
>>>>>>>>>>>>>
>>>>>>>>>>>>>
>>>>>>>>>>>>> This should handle the GNU extension of re-using operand 0 if operand
>>>>>>>>>>>>> 1 is omitted.
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> Doesn't that just use a SAVE_EXPR?
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> Hmm, I suppose it does, but many places in the compiler seem to expect
>>>>>>>>>>> that it produces a COND_EXPR with TREE_OPERAND 1 as NULL_TREE.
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> Maybe that's used somewhere inside the C++ front end. For C a SAVE_EXPR
>>>>>>>>>> is produced directly.
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> Here is the updated patch. Changes from the last one:
>>>>>>>>>
>>>>>>>>> 1. Handle COMPOUND_EXPR.
>>>>>>>>> 2. Fixed typos in comments.
>>>>>>>>> 3. Combined warn_for_pointer_of_packed_member and
>>>>>>>>> warn_for_address_of_packed_member into
>>>>>>>>> warn_for_address_or_pointer_of_packed_member.
>>>>>>>>
>>>>>>>>
>>>>>>>>> c.i:4:33: warning: converting a packed âstruct C *â pointer increases the
>>>>>>>>> alignment of âlong int *â pointer from 1 to 8 [-Waddress-of-packed-member]
>>>>>>>>
>>>>>>>>
>>>>>>>> I think this would read better as
>>>>>>>>
>>>>>>>> c.i:4:33: warning: converting a packed âstruct C *â pointer (alignment 1) to
>>>>>>>> âlong int *â (alignment 8) may result in an unaligned pointer value
>>>>>>>> [-Waddress-of-packed-member]
>>>>>>>
>>>>>>> Fixed.
>>>>>>>
>>>>>>>>> + while (TREE_CODE (base) == ARRAY_REF)
>>>>>>>>> + base = TREE_OPERAND (base, 0);
>>>>>>>>> + if (TREE_CODE (base) != COMPONENT_REF)
>>>>>>>>> + return NULL_TREE;
>>>>>>>>
>>>>>>>>
>>>>>>>> Are you deliberately not handling the other handled_component_p cases? If
>>>>>>>> so, there should be a comment.
>>>>>>>
>>>>>>> I changed it to
>>>>>>>
>>>>>>> while (handled_component_p (base))
>>>>>>> {
>>>>>>> enum tree_code code = TREE_CODE (base);
>>>>>>> if (code == COMPONENT_REF)
>>>>>>> break;
>>>>>>> switch (code)
>>>>>>> {
>>>>>>> case ARRAY_REF:
>>>>>>> base = TREE_OPERAND (base, 0);
>>>>>>> break;
>>>>>>> default:
>>>>>>> /* FIXME: Can it ever happen? */
>>>>>>> gcc_unreachable ();
>>>>>>> break;
>>>>>>> }
>>>>>>> }
>>>>>>>
>>>>>>> Is there a testcase to trigger this ICE? I couldn't find one.
>>>>>>
>>>>>> You can take the address of an element of complex:
>>>>>>
>>>>>> __complex int i;
>>>>>> int *p = &__real(i);
>>>>>>
>>>>>> You may get VIEW_CONVERT_EXPR with location wrappers.
>>>>>
>>>>> Fixed. I replaced gcc_unreachable with return NULL_TREE;
>>>>
>>>> Then we're back to my earlier question: are you deliberately not
>>>> handling the other cases? Why not look through them as well? What if
>>>> e.g. the operand of __real is a packed field?
>>>>
>>>
>>> Here is the updated patch with
>>>
>>> diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c
>>> index 615134cfdac..f105742598e 100644
>>> --- a/gcc/c-family/c-warn.c
>>> +++ b/gcc/c-family/c-warn.c
>>> @@ -2669,6 +2669,9 @@ check_address_of_packed_member (tree type, tree rhs)
>>> switch (code)
>>> {
>>> case ARRAY_REF:
>>> + case REALPART_EXPR:
>>> + case IMAGPART_EXPR:
>>> + case VIEW_CONVERT_EXPR:
>>> base = TREE_OPERAND (base, 0);
>>> break;
>>> default:
>>
>> don't we have handled_component_p () for this? (you're still
>> missing BIT_FIELD_REF which might be used for vector
>> element accesses)
>>
>
> Do you have a testcase?
Is there a reason you only want to handle some component references and
not others? If not, checking handled_component_p is simpler and more
future proof than enumerating specific codes.
Jason
next prev parent reply other threads:[~2018-12-17 13:53 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-25 15:47 V4 " H.J. Lu
2018-11-04 15:16 ` PING: " H.J. Lu
2018-11-25 14:38 ` PING^2: " H.J. Lu
2018-12-13 20:50 ` Jason Merrill
2018-12-14 0:09 ` V5 " H.J. Lu
2018-12-14 22:10 ` Jason Merrill
2018-12-14 22:48 ` V6 " H.J. Lu
2018-12-17 9:39 ` Richard Biener
2018-12-17 12:43 ` H.J. Lu
2018-12-17 13:34 ` Richard Biener
2018-12-17 13:53 ` Jason Merrill [this message]
2018-12-18 14:11 ` V7 " H.J. Lu
2018-12-18 20:36 ` Jason Merrill
2018-12-18 21:13 ` V8 " H.J. Lu
2018-12-18 22:14 ` Jason Merrill
2018-12-19 14:52 ` H.J. Lu
2018-12-19 17:36 ` V9 " H.J. Lu
2018-12-20 19:40 ` Jason Merrill
2018-12-20 20:07 ` V10 " H.J. Lu
2018-12-20 21:31 ` Jason Merrill
2018-12-20 21:47 ` H.J. Lu
2018-12-19 3:19 ` V8 " Sandra Loosemore
2018-12-19 14:53 ` H.J. Lu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=bd7ff807-0918-9d66-8043-969d4d2ff8b0@redhat.com \
--to=jason@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=hjl.tools@gmail.com \
--cc=joseph@codesourcery.com \
--cc=msebor@gmail.com \
--cc=richard.guenther@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).