public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Alexandre Oliva <oliva@adacore.com>, gcc-patches@gcc.gnu.org
Cc: David Malcolm <dmalcolm@redhat.com>,
	Rainer Orth <ro@CeBiTec.Uni-Bielefeld.DE>,
	Mike Stump <mikestump@comcast.net>,
	Nathan Sidwell <nathan@acm.org>, "H.J. Lu" <hjl.tools@gmail.com>
Subject: Re: [PATCH #2/4] c++: mark short-enums as packed
Date: Mon, 20 Nov 2023 14:55:52 -0500	[thread overview]
Message-ID: <78e7499c-2278-44ff-8455-503e3690e3fd@redhat.com> (raw)
In-Reply-To: <orleatqhkx.fsf@lxoliva.fsfla.org>

On 11/19/23 21:33, Alexandre Oliva wrote:
> 
> Unlike C, C++ doesn't mark enums shortened by -fshort-enums as packed.
> This makes for undesirable warning differences between C and C++,
> e.g. c-c++-common/analyzer/null-deref-pr108251-smp_fetch_ssl_fc_has_early*.c
> triggers a warning about a type cast from a pointer to enum that, when
> packed, might not be sufficiently aligned.

I think the warning is wrong here.  The example given when it was added 
in r9-5005-gda77eace90fd99 was

struct pair_t
{
   char c;
   int i;
} __attribute__ ((packed));

extern struct pair_t p;
int *addr = &p.i;

in this case, we're assigning a pointer to unaligned int to a normal 
pointer to int, and that's what the warning is for; conversion from a 
misaligned member.  In the analyzer testcase, we have a cast from an 
enum pointer that we don't know what it points to, and even if it did 
point to the obj_type member of struct connection, that wouldn't be a 
problem because it's at offset 0.

Also, -fshort-enums has nothing to do with structure packing, it just 
affects the underlying type of the enum.  We don't warn about conversion 
from pointer to char to pointer to struct with char member, and this is 
the exact same situation.

And unlike a struct with __attribute__ ((packed)),
   enum A { a=1000 };
gets 16-bit alignment with -fshort-enums.

So it seems to me that setting TYPE_PACKED from -fshort-enums is wrong.
Or that the warning's use of TYPE_PACKED is wrong (e.g. it should look 
for a COMPONENT_REF of DECL_PACKED instead).  Or both.

Either way, I'm opposed to changing things so that the C++ front-end 
starts emitting the warning.

Jason


  reply	other threads:[~2023-11-20 19:55 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-19  7:36 [PATCH] testsuite: analyzer: expect alignment warning with -fshort-enums Alexandre Oliva
2023-11-19 15:13 ` Jeff Law
2023-11-20 22:19   ` David Malcolm
2023-11-20  2:33 ` [PATCH #2/4] c++: mark short-enums as packed Alexandre Oliva
2023-11-20 19:55   ` Jason Merrill [this message]
2023-11-22  8:17     ` Alexandre Oliva
2023-11-22 18:12       ` Jason Merrill
2023-11-22 18:26         ` Jason Merrill
2023-11-29  9:39           ` Alexandre Oliva
2023-11-29 19:03             ` Jason Merrill
2023-11-30  7:21               ` Alexandre Oliva
2023-11-20  2:34 ` [PATCH #3/4] warn on cast of pointer to packed plus constant Alexandre Oliva
2023-11-20  2:34 ` [PATCH #4/4] testsuite: discard c++ exclusion on underaligned pointer warning Alexandre Oliva
2023-11-23 20:27   ` Mike Stump

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=78e7499c-2278-44ff-8455-503e3690e3fd@redhat.com \
    --to=jason@redhat.com \
    --cc=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=hjl.tools@gmail.com \
    --cc=mikestump@comcast.net \
    --cc=nathan@acm.org \
    --cc=oliva@adacore.com \
    --cc=ro@CeBiTec.Uni-Bielefeld.DE \
    /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).