From: John Baldwin <jhb@FreeBSD.org>
To: gdb-patches@sourceware.org, luis.machado@linaro.org
Cc: Tom Tromey <tom@tromey.com>
Subject: Re: [PATCH] sim/d10v: Use offsetof in a static assertion about structure layout.
Date: Fri, 21 May 2021 10:46:04 -0700 [thread overview]
Message-ID: <5d80d289-9a53-ef2e-c40a-f9766eef5dc0@FreeBSD.org> (raw)
In-Reply-To: <YKWguEtbEFKMxxVs@vapier>
On 5/19/21 4:35 PM, Mike Frysinger wrote:
> On 19 May 2021 10:45, John Baldwin wrote:
>> clang 11 fails to compile the static assertion as it cannot compute
>> the pointer value at a compile time:
>>
>> gdb/sim/d10v/interp.c:1149:37: error: static_assert expression is not an integral constant expression
>> static_assert ((uintptr_t) &State == (uintptr_t) &State.regs,
>> ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
>> 1 error generated.
>>
>> Instead, assert that the offset of State.regs is 0.
>
> idea is good ...
>
>> --- a/sim/d10v/interp.c
>> +++ b/sim/d10v/interp.c
>>
>> - static_assert ((uintptr_t) &State == (uintptr_t) &State.regs,
>> - "&State != &State.regs");
>> + static_assert (offsetof(typeof(State), regs) == 0,
>
> stylewise:
> * need space before the (
> * i think we want __typeof since it's not in C11 afaik
Oh, yes. I had forgotten that it typeof() is an extension. It might be
simpler to just use the type directly:
diff --git a/sim/d10v/interp.c b/sim/d10v/interp.c
index b56b204c72d..b587cc18654 100644
--- a/sim/d10v/interp.c
+++ b/sim/d10v/interp.c
@@ -1146,8 +1146,8 @@ sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
bfd_vma start_address;
/* Make sure we have the right structure for the following memset. */
- static_assert ((uintptr_t) &State == (uintptr_t) &State.regs,
- "&State != &State.regs");
+ static_assert (offsetof (struct _state, regs) == 0,
+ "State.regs is not at offset 0");
/* Reset state from the regs field until the mem field. */
memset (&State, 0, (uintptr_t) &State.mem - (uintptr_t) &State.regs);
--
John Baldwin
next prev parent reply other threads:[~2021-05-21 17:46 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-19 17:45 John Baldwin
2021-05-19 23:35 ` Mike Frysinger via Gdb-patches
2021-05-21 17:46 ` John Baldwin [this message]
2021-05-21 22:39 ` Mike Frysinger via Gdb-patches
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=5d80d289-9a53-ef2e-c40a-f9766eef5dc0@FreeBSD.org \
--to=jhb@freebsd.org \
--cc=gdb-patches@sourceware.org \
--cc=luis.machado@linaro.org \
--cc=tom@tromey.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