Thread overview
[Issue 23441] importc: array length macro preprocessed with cpp doesn't compile
Oct 28, 2022
ryuukk_
Oct 28, 2022
ryuukk_
Oct 28, 2022
kinke
Oct 28, 2022
ryuukk_
Oct 28, 2022
ryuukk_
Jan 19, 2023
Walter Bright
Jan 19, 2023
Walter Bright
October 28, 2022
https://issues.dlang.org/show_bug.cgi?id=23441

ryuukk_ <ryuukk.dev@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |ImportC

--
October 28, 2022
https://issues.dlang.org/show_bug.cgi?id=23441

--- Comment #1 from ryuukk_ <ryuukk.dev@gmail.com> ---
If that's not valid C11, then what would be the proper fix? so i could notify the project owners about a possible PR to make it compatible with C11

--
October 28, 2022
https://issues.dlang.org/show_bug.cgi?id=23441

kinke <kinke@gmx.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kinke@gmx.net

--- Comment #2 from kinke <kinke@gmx.net> ---
That's invalid C in general, neither gcc nor clang accept it. `nk_utfmask`
needs to be an array, e.g., `char nk_utfmask[5];`, which should be closer to
the presumable original code
(https://github.com/vurtun/nuklear/blob/6b9f937475db9280d966f44f469bc80191b5092a/nuklear.h#L7865).

That NK_LEN macro apparently makes use of a weird special syntax to get the length of a C array, see the last 'notes' paragraph in https://en.cppreference.com/w/c/language/sizeof.

--
October 28, 2022
https://issues.dlang.org/show_bug.cgi?id=23441

--- Comment #3 from ryuukk_ <ryuukk.dev@gmail.com> ---
Oops i copied the wrong code above, it's not a struct you are right

Here is the source: https://github.com/Immediate-Mode-UI/Nuklear/blob/6e80e2a646f35be4afc157a932f2936392ec8f74/src/nuklear_utf8.c

I now remember, i sent a PR a while ago to fix it: https://github.com/Immediate-Mode-UI/Nuklear/pull/467

--
October 28, 2022
https://issues.dlang.org/show_bug.cgi?id=23441

--- Comment #4 from ryuukk_ <ryuukk.dev@gmail.com> ---
> That NK_LEN macro apparently makes use of a weird special syntax to get the length of a C array, see the last 'notes' paragraph in https://en.cppreference.com/w/c/language/sizeof.



So it is valid C11 code? or should this issue be closed?

--
January 19, 2023
https://issues.dlang.org/show_bug.cgi?id=23441

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com

--- Comment #5 from Walter Bright <bugzilla@digitalmars.com> ---
(In reply to kinke from comment #2)
> That NK_LEN macro apparently makes use of a weird special syntax to get the length of a C array, see the last 'notes' paragraph in https://en.cppreference.com/w/c/language/sizeof.

The cite says:

"Number of elements in any array a including VLA (since C99) may be determined with the expression sizeof a / sizeof a[0]. Note that if a has pointer type (such as after array-to-pointer conversion of function parameter type adjustment), this expression would simply divide the number of bytes in a pointer type by the number of bytes in the pointed type."

--
January 19, 2023
https://issues.dlang.org/show_bug.cgi?id=23441

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #6 from Walter Bright <bugzilla@digitalmars.com> ---
(In reply to ryuukk_ from comment #0)
>     // #define NK_LEN(a) (sizeof(a)/sizeof(a)[0])

The usual way this is written:

    #define NK_LEN(a) (sizeof(a)/sizeof((a)[0]))

But in order to work, `a` must be a static array. nk_utfmask is a struct. It will also fail because in C, it would have to be `struct nk_utfmask`, not just `nk_utfmask`.

I'm going to mark this as invalid.

--