ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
35.19k stars 2.57k forks source link

translate-c fails on escaped backslash char literal #2924

Closed Stenodyon closed 5 years ago

Stenodyon commented 5 years ago

Version: 0.4.0+f429f4dc

Original C function:

nk_decode_85_byte(char c)
{
    return (unsigned int)((c >= '\\') ? c-36 : c-35);
}

Generated Zig code:

pub fn nk_decode_85_byte(c: u8) c_uint {
    return c_uint(if (c_int(c) >= '\') c_int(c) - 36 else c_int(c) - 35);
}//                                ^
//                                 | Backslash not escaped, causing
//                                 | a parse error on the )

This happened when cImporting the nuklear library:

usingnamespace @cImport({
    @cDefine("NK_IMPLEMENTATION", "1");
    @cDefine("NK_INCLUDE_DEFAULT_ALLOCATOR", "1");
    @cDefine("NK_INCLUDE_VERTEX_BUFFER_OUTPUT", "1");
    @cDefine("NK_INCLUDE_FONT_BAKING", "1");
    @cInclude("nuklear.h");
});
Stenodyon commented 5 years ago

I found another occurrence of what I believe to be the same bug:

// nuklear.h:23514
buffer[NK_MIN(NK_MAX(max-1,0), len)] = '\0';

The \0 is rendered as a null byte in the zig output:

buffer[if (if ((max - 1) < 0) 0 else max - 1 < len) if ((max - 1) < 0) 0 else max - 1 else len] = u8('^@');