-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
support more macros in translate-c #1085
Comments
Macros as functions: #define FOO__INIT (X) (foo(X, X+X))
int foo(int x, int x2) {
return x2 - x;
} Currently only exports foo, would want FOO_INIT as a function too. |
That one is actually not possible to translate perfectly. Consider: int x = 0;
int y = FOO__INIT(++x); In C the |
Thats true that macros suck, but there is a lot of C code that makes macro functions, having a way to translate them, or maybe simpler would be to at least make them a comment so you can translate them by hand would help. One example I was dealing with was protobuffer, it generates a .c and .h file based on your .proto so tweaking it every time would be a pita. |
That's a brilliant idea! Easy to do too. |
Just ran into this one from SDL2 /**
* \brief Used to indicate that you don't care what the window position is.
*/
#define SDL_WINDOWPOS_UNDEFINED_MASK 0x1FFF0000u
#define SDL_WINDOWPOS_UNDEFINED_DISPLAY(X) (SDL_WINDOWPOS_UNDEFINED_MASK|(X))
#define SDL_WINDOWPOS_UNDEFINED SDL_WINDOWPOS_UNDEFINED_DISPLAY(0)
#define SDL_WINDOWPOS_ISUNDEFINED(X) \
(((X)&0xFFFF0000) == SDL_WINDOWPOS_UNDEFINED_MASK)
/**
* \brief Used to indicate that the window position should be centered.
*/
#define SDL_WINDOWPOS_CENTERED_MASK 0x2FFF0000u
#define SDL_WINDOWPOS_CENTERED_DISPLAY(X) (SDL_WINDOWPOS_CENTERED_MASK|(X))
#define SDL_WINDOWPOS_CENTERED SDL_WINDOWPOS_CENTERED_DISPLAY(0)
#define SDL_WINDOWPOS_ISCENTERED(X) \
(((X)&0xFFFF0000) == SDL_WINDOWPOS_CENTERED_MASK) |
I'm wondering about the current way macros are expanded. Basically, Zig tries to tokenize and parse the expression, putting the result into |
Some macros are expected to be configured by command line parameters. |
re
|
I'm also fine with essentially commenting the source. This is more of an issue for more complex macros. For example, a for each loop macro would probably break. For a good idea of this, check out the array and object loop macros in Jansson. Edit: Here is the Jansson example:
|
Here's one I just ran into while trying to rewrite a debugger in Zig. It may be the same class of macro as the one in @bheads' comment. #define __WIFSTOPPED(status) (((status) & 0xff) == 0x7f)
#define WIFSTOPPED(status) __WIFSTOPPED (status) These macros are found in my system's So being new to Zig, I was perplexed at first when I compiled this: const c = @cImport({
@cDefine("_NO_CRT_STDIO_INLINE", "1");
@cInclude("sys/wait.h");
// ...
});
export fn main(argc: c_int, argv: **u8) c_int {
// ...
while (c.WIFSTOPPED(wait_status)) {
// ...
}
// ...
} And got this error: $ zig build-exe --library c main.zig
~/code/dbg/main.zig:41:17: error: no member named 'WIFSTOPPED' in '(C import)'
while (c.WIFSTOPPED(wait_status)) {
^ But sure enough: $ zig translate-c /usr/include/sys/wait.h | grep WIFSTOPPED
$ It's easy enough to simply rewrite the necessary code in Zig and skip |
ncurses has some funny macros that don't work, too
I ended up creating a C file just to figure out the resulting value of #include <curses.h>
int main(void) { printf("%x\n", A_STANDOUT); } Then I put it in my zig code pub const A_STANDOUT: c_int = 0x10000; Couldn't zig invoke the C preprocessor or something when it can't parse the macro? That wouldn't need to happen for most macros, so I don't think that would be noticably slower. |
The C preprocessor is fully supported in terms of actual C top level declarations. If the .h file had used The problem in general here is that what the .h file is exporting with a Consider, what should happen for this macro? One thing that is possible is if the programmer informed Zig of the "type" of a macro. Then Zig could emit extra C code to make it work. Something like this: const c = @cImport({
@cMacroHint("A_STANDOUT", c_int);
@cInclude("curses.h");
}); Then when Zig creates its C source text to hand off to clang, it would look like this: #include "curses.h"
static const int MAGICPREFIX_A_STANDOUT = A_STANDOUT; Then when translating symbols, Zig looks for the |
lua has some quite simple macros that aren't translated, e.g. LUA_API int (lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc,
lua_KContext ctx, lua_KFunction k);
#define lua_pcall(L,n,r,f) lua_pcallk(L, (n), (r), (f), 0, NULL) #define lua_pushcfunction(L,f) lua_pushcclosure(L, (f), 0) |
can translate to
Related: need #3672 to allow casting these values to arrays. |
translate-c doesn't appear to handle a sizeof() in a macro:
|
Alright enough of these are solved that it's time to start trying to close this issue in favor of individual issues. Please, from now on, new issues for each different macro, and let's make sure the examples in this issue have test coverage and then it can be closed. |
this is an issue to collect examples of macros that fail to translate, but there would be a very clear way to correctly translate the macro into a zig.
here's one for starters:
The text was updated successfully, but these errors were encountered: