Bug 23189 – importC: __builtin_offsetof without struct/union/enum should emit proper error message
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-06-15T20:26:23Z
Last change time
2023-04-09T06:07:38Z
Keywords
ImportC
Assigned to
No Owner
Creator
ryuukk_
Comments
Comment #0 by ryuukk.dev — 2022-06-15T20:26:23Z
The following code is valid in C, but doesn't compile in ImportC
```
union nk_page_data *pd = (union nk_page_data*)((void*)((char*)(1 ? (tbl): &((union nk_page_data*)0)->tbl) - (__builtin_offsetof(nk_page_data,tbl))));
```
Comment #1 by ryuukk.dev — 2022-06-15T20:36:41Z
Here is a usable code to test:
```
struct nk_table {
void* test;
};
union nk_page_data {
struct nk_table tbl;
};
void test(void)
{
struct nk_table *tbl;
union nk_page_data *pd = (union nk_page_data*)((void*)((char*)(1 ? (tbl): &((union nk_page_data*)0)->tbl) - (__builtin_offsetof(union nk_page_data,tbl))));
}
```
Comment #2 by ryuukk.dev — 2022-06-15T20:55:04Z
Oops the code above is the actual fix..
Here the actual problematic code:
```
struct nk_table {
void* test;
};
union nk_page_data {
struct nk_table tbl;
};
void test(void)
{
struct nk_table *tbl;
union nk_page_data *pd = (union nk_page_data*)((void*)((char*)(1 ? (tbl): &((union nk_page_data*)0)->tbl) - (__builtin_offsetof(k_page_data,tbl))));
}
```
__builtin_offsetof requires 'union', without it, i get the error:
```
bug.c(15): Error: expression expected, not `)`
bug.c(15): Error: found `0` when expecting `)`
```
Comment #3 by ryuukk.dev — 2022-06-15T21:19:56Z
Ok apparently, it is required to have them, but that was the output of the clang preprocessor.. hmm
Anyways.. i think a proper error message would be more useful that what is currently emitted!
Comment #4 by bugzilla — 2022-09-22T02:43:18Z
(In reply to ryuukk_ from comment #0)
> The following code is valid in C, but doesn't compile in ImportC
It doesn't compile with gcc either.
Comment #5 by bugzilla — 2022-09-22T02:45:40Z
I'm not real sure what the problem is you're reporting. It's not clear what you expect to compile?
Comment #6 by ryuukk.dev — 2022-10-28T00:51:13Z
Here is a reduced code:
```
#define NK_OFFSETOF(st,m) (__builtin_offsetof(st,m))
#define NK_ALIGNOF(t) NK_OFFSETOF(struct {char c; t _h;}, _h)
struct nk_command{};
void test()
{
const int align = NK_ALIGNOF(struct nk_command);
}
```
// using cpp -P -E
```
void test()
{
const int align = (__builtin_offsetof(struct {char c; struct nk_command _h;},_h));
}
```
This code compile with GCC, but not with D importc:
bar.c(4): Error: expression expected, not `struct`
bar.c(4): Error: found `{` when expecting `)`
bar.c(4): Error: found `char` when expecting `)`
bar.c(4): Error: missing comma or semicolon after declaration of `extern ()`, found `c` instead
bar.c(4): Error: identifier or `(` expected
bar.c(5): Error: identifier or `(` expected