[PATCH] sim/d10v: Use offsetof in a static assertion about structure layout.

John Baldwin jhb@FreeBSD.org
Fri May 21 17:46:04 GMT 2021


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


More information about the Gdb-patches mailing list