translate-c silently ignores non-equivalent macro redefinitions and macros shadowing non-macros #20301
Labels
bug
Observed behavior contradicts documented or intended behavior
translate-c
C to Zig source translation feature (@cImport)
Milestone
Zig Version
0.13.0
Steps to Reproduce and Observed Behavior
EDIT: Added examples showing related shadowing issues
Macro redefinitions
Create a file named
header.h
with these contents:Then run
zig translate-c header.h
and observe that the macro is translated into:The redefinition was silently ignored. Meanwhile, Clang and GCC accept the redefinition and issue a warning. To be clear, this means that Clang and GCC would replace
MACRO
with1
, not0
, see Godbolt. My understanding is that the C standard forbids macro redefinitions unless the replacements are equivalent in some sense.Macros shadowing non-macros
There are similar issues when a macro has the same name as a non-macro:
translate-c
again treatsas if the second line didn't exist, but C code including this snippet would subsequently replace
MACRO
with1
.As for function-like macros,
translate-c
turns bothand
into the following Zig code as if the macro definition wasn't there:
Meanwhile, in C code including either of the above snippets,
f(x)
has the same value asx+1
, see Godbolt.Expected Behavior
Macro redefinitions (snippet 1)
Since Zig doesn't issue warnings, I think
translate-c
should do one of the following:translate-c
well enough to see whether this is easy to implement.Macros shadowing non-macros (snippets 2-4)
I think the macro definitions should take precedence in these snippets, because this is how they would behave if included in C code. These semantics shouldn't change if the same code is
@cInclude
d instead. But note that while C code can invoke(f)(x)
to circumvent the macro, the translated Zig would not even export the function.The text was updated successfully, but these errors were encountered: