translate-c: incorrect type for pointers to cv-qualified values #23329
Labels
bug
Observed behavior contradicts documented or intended behavior
translate-c
C to Zig source translation feature (@cImport)
Zig Version
0.15.0-dev.33+539f3effd
Steps to Reproduce and Observed Behavior
zig translate-c
does not correctly translate pointer types when the pointee is cv-qualified in some cases. Unlike Zig, C can have const/volatile qualified values, so cv-qualified non-pointer types are translated without those qualifiers (as expected). However, pointers to such values do not always translate to correctly-qualified pointers, even if the qualified pointer type is representable in zig.Example C code and zig translation that demonstrate this in various ways:
AFAIK this isn't esoteric C code - its pretty common in the embedded world (e.g. official arm CMSIS tooling generates this kind of thing).
Expected Behavior
Expected: Pointers to cv-qualified values in C-source code should be correctly translated.
It's hard to say what the exact output should be. For consistency with how other untranslateable C concepts are handled in translation, I would expect to see an explicit type-cast to the correct type whenever the address of a cv-qualified value-type is taken (directly or indirectly, ie. with
&
or->
), and would expect the type to appear without qualifiers when it appears a value-type (status quo). Something like this:Another approach could be something like how flexible array struct members are handled, which is to say such types are supported by
std.zig.c_translation
somehow.More specifically, I would expect internal code in the translation that uses pointers to volatile qualified value-types to be correctly typed. I think it should be left to the user (for now, at least) to properly handle pointer-qualifiers themselves when they consume the API, it's trivial to write some wrapper functions/types to handle that. But there is no easy user-land solution to diagnose internal usages of incorrectly-qualified pointers without having to literally rewrite those functions entirely. For HAL libraries and SDKs where such code is abundant (e.g. pico-sdk), there can be hundreds (if not thousands) of inline functions (i.e. function body is mis-translated by zig) using wrongly-qualified pointers that call into each other. So in that case, basically the entire code base would have to be manually rewritten.
The text was updated successfully, but these errors were encountered: