need help to get bitoffsetof bitwidth for clang

Tim tim.dlang at t-online.de
Wed Jul 3 21:47:32 UTC 2024


On Tuesday, 2 July 2024 at 05:34:19 UTC, Dakota wrote:
> I want to get `bitoffsetof` and `bitwidth` in clang, to verify 
> the d memory layout.
>
> I try this but not work:
>
> ```c
> #define offsetof_bit(type, member) ((offsetof(type, member) * 
> 8) + __builtin_ctz(((type *)0)->member))
> ```
>
>
> get this error:
>
> ```sh
> cannot compute offset of bit-field 'field'
> ```

If you only want to see the layout, you can use the following 
command to dump it:
```
clang test.c -Xclang -fdump-record-layouts -c
```

File test.c could contain the following:
```
struct S
{
     int a : 3;
     int b : 29;
};
struct S dummy;
```

The above clang command would then output:
```
*** Dumping AST Record Layout
          0 | struct S
      0:0-2 |   int a
     0:3-31 |   int b
            | [sizeof=4, align=4]
```

Inside a C/C++ program you could initialize a struct with all 
0xff, set one bitfield to zero and look at the bits. I have used 
this to compare the bitfield layout between D and C++ in a test 
added in https://github.com/dlang/dmd/pull/16590.


More information about the Digitalmars-d-learn mailing list