public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] sim/d10v: Use offsetof in a static assertion about structure layout.
@ 2021-05-19 17:45 John Baldwin
  2021-05-19 23:35 ` Mike Frysinger
  0 siblings, 1 reply; 4+ messages in thread
From: John Baldwin @ 2021-05-19 17:45 UTC (permalink / raw)
  To: gdb-patches

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.

sim/d10v/ChangeLog:

	* interp.c (sim_create_inferior): Use offsetof in static
	assertion.
---
 sim/d10v/ChangeLog | 5 +++++
 sim/d10v/interp.c  | 4 ++--
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/sim/d10v/ChangeLog b/sim/d10v/ChangeLog
index e45bd3833b0..d799d238db9 100644
--- a/sim/d10v/ChangeLog
+++ b/sim/d10v/ChangeLog
@@ -1,3 +1,8 @@
+2021-05-19  John Baldwin  <jhb@FreeBSD.org>
+
+	* interp.c (sim_create_inferior): Use offsetof in static
+	assertion.
+
 2021-05-17  Mike Frysinger  <vapier@gentoo.org>
 
 	* sim-main.h (SIM_HAVE_COMMON_SIM_STATE): Delete.
diff --git a/sim/d10v/interp.c b/sim/d10v/interp.c
index b56b204c72d..ee242f766f6 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(typeof(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);
-- 
2.31.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] sim/d10v: Use offsetof in a static assertion about structure layout.
  2021-05-19 17:45 [PATCH] sim/d10v: Use offsetof in a static assertion about structure layout John Baldwin
@ 2021-05-19 23:35 ` Mike Frysinger
  2021-05-21 17:46   ` John Baldwin
  0 siblings, 1 reply; 4+ messages in thread
From: Mike Frysinger @ 2021-05-19 23:35 UTC (permalink / raw)
  To: John Baldwin; +Cc: gdb-patches, luis.machado

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
-mike

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] sim/d10v: Use offsetof in a static assertion about structure layout.
  2021-05-19 23:35 ` Mike Frysinger
@ 2021-05-21 17:46   ` John Baldwin
  2021-05-21 22:39     ` Mike Frysinger
  0 siblings, 1 reply; 4+ messages in thread
From: John Baldwin @ 2021-05-21 17:46 UTC (permalink / raw)
  To: gdb-patches, luis.machado; +Cc: Tom Tromey

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] sim/d10v: Use offsetof in a static assertion about structure layout.
  2021-05-21 17:46   ` John Baldwin
@ 2021-05-21 22:39     ` Mike Frysinger
  0 siblings, 0 replies; 4+ messages in thread
From: Mike Frysinger @ 2021-05-21 22:39 UTC (permalink / raw)
  To: John Baldwin; +Cc: gdb-patches, luis.machado, Tom Tromey

On 21 May 2021 10:46, John Baldwin wrote:
> 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:

looks fine, thanks
-mike

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-05-21 22:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-19 17:45 [PATCH] sim/d10v: Use offsetof in a static assertion about structure layout John Baldwin
2021-05-19 23:35 ` Mike Frysinger
2021-05-21 17:46   ` John Baldwin
2021-05-21 22:39     ` Mike Frysinger

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).