-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
improve C macro translation of R_CAST from Ruby #8062
Comments
Hi Julia! Thanks for taking the time to make an issue report and a nice small reproducer. For the most part, zig's translate-c feature relies on Clang to do the heavy lifting; however we also have our own C tokenizer & parser to translate C macros to Zig in a heuristics-based, best-effort manner. Some C macros cannot be translated, however, they are supposed to fail gracefully, certainly not by emitting a syntax error into zig code! I actually get a crash when I run a debug build of the compiler on your test case, so this is a great find:
The good news is, you have excellent timing, because I am right about to merge this chonkster #7920 which thanks to @Vexu uses a more maintainable, less buggy way of emitting Zig code from C code. And I just checked, in this branch, the issue is at least partially solved. The macro is translated thusly: pub fn R_CAST(st: anytype) callconv(.Inline) @TypeOf([*c]struct_st) {
return [*c]struct_st;
} No longer a syntax error, but I doubt that is going to be useful to use in Zig code. Perhaps if we have a look at how the C code uses this macro, we can improve the heuristics, or perhaps this is one of those macros that is not very translatable. |
With the merge of d7049fc, this is no longer a bug, but a proposal to look at this macro and determine whether it is feasible to improve the heuristics of C macro translation or not. |
I'm extremely impressed by how fast you fixed that :). I think the problem I had is fixed -- I didn't actually need to use that Ruby macro, I just needed there to not be a syntax error. |
Alrighty then, given that C macro translation is heuristics based, and there is no actual interest that we know of to actually use this macro, I'll close the issue for now. But if somebody thinks it is worth re-opening, let me know and I'll do that. |
I was trying to import a bunch of header files from Ruby with zig as an experiment, and I ran into an error with this
#define
in one of MRI's header files:When translating that line into Zig (using
@cImport
), I get this syntax error:Here's the full function that Zig generated for that #define, which does seem to have a syntax error:
I think this is because this
R_CAST
#define is kind of weird and it's tripping up the C -> Zig translation code. I don't know if Zig is intended to support arbitrary C preprocessor directives like this but opening the issue anyway in case it's of interest.I made a very small repo that reproduces this issue here: https://github.com/jvns/zig-include -- you should be able to repro just by cloning it and running
zig build
.I also might have just made some beginner mistake, it's my first time trying zig :)
The text was updated successfully, but these errors were encountered: