authorgravatar for henrik@laxhuber.comHenrik Laxhuber <henrik@laxhuber.com> 2020-07-26 16:27:47+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-07-27 13:43:49+03:00
log442025481c40625f8010355d2790935e5add0db5
tree6b97bea31427e2df675bfec4871977fb4abcf3b0
parent616355807fc8b0a2e7ccc2e54cd4453776c0e187

Fix parsing of `unsigned` in translate-c.

Previously, `unsigned` was parsed as the shorthand for `unsigned int`. This commit introduces code to parse `unsigned short`, `unsigned int`, `unsigned long`, and `unsigned long long`. There is a comment in the code about std.c.parse` - Im not familiar with zig internals, but it seems like this is a separate C parsing implementation. In the long run, it probably makes sense to merge both implementations, so this commit should be regarded as a quick fix that doesn't address an apparently underlying issue.

2 files changed, 26 insertions(+), 1 deletions(-)

src-self-hosted/translate_c.zig+16-1
...@@ -5764,7 +5764,22 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,...@@ -5764,7 +5764,22 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
5764 .Keyword_float => return transCreateNodeIdentifierUnchecked(c, "f32"),5764 .Keyword_float => return transCreateNodeIdentifierUnchecked(c, "f32"),
5765 .Keyword_short => return transCreateNodeIdentifierUnchecked(c, "c_short"),5765 .Keyword_short => return transCreateNodeIdentifierUnchecked(c, "c_short"),
5766 .Keyword_char => return transCreateNodeIdentifierUnchecked(c, "c_char"),5766 .Keyword_char => return transCreateNodeIdentifierUnchecked(c, "c_char"),
5767 .Keyword_unsigned => return transCreateNodeIdentifierUnchecked(c, "c_uint"),5767 .Keyword_unsigned => if (it.next()) |t| {
5768 switch (t.id) {
5769 .Keyword_short => return transCreateNodeIdentifierUnchecked(c, "c_ushort"),
5770 .Keyword_int => return transCreateNodeIdentifierUnchecked(c, "c_uint"),
5771 .Keyword_long => if (it.peek() != null and it.peek().?.id == .Keyword_long) {
5772 _ = it.next();
5773 return transCreateNodeIdentifierUnchecked(c, "c_ulonglong");
5774 } else return transCreateNodeIdentifierUnchecked(c, "c_ulong"),
5775 else => {
5776 _ = it.prev();
5777 return transCreateNodeIdentifierUnchecked(c, "c_uint");
5778 },
5779 }
5780 } else {
5781 return transCreateNodeIdentifierUnchecked(c, "c_uint");
5782 },
5768 .Identifier => {5783 .Identifier => {
5769 const mangled_name = scope.getAlias(source[tok.start..tok.end]);5784 const mangled_name = scope.getAlias(source[tok.start..tok.end]);
5770 return transCreateNodeIdentifier(c, mangled_name);5785 return transCreateNodeIdentifier(c, mangled_name);
test/translate_c.zig+10
...@@ -2715,6 +2715,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2715,6 +2715,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2715 \\pub const BAR = (@import("std").meta.cast(?*c_void, a));2715 \\pub const BAR = (@import("std").meta.cast(?*c_void, a));
2716 });2716 });
27172717
2718 cases.add("macro with cast to unsigned short, long, and long long",
2719 \\#define CURLAUTH_BASIC_BUT_USHORT ((unsigned short) 1)
2720 \\#define CURLAUTH_BASIC ((unsigned long) 1)
2721 \\#define CURLAUTH_BASIC_BUT_ULONGLONG ((unsigned long long) 1)
2722 , &[_][]const u8{
2723 \\pub const CURLAUTH_BASIC_BUT_USHORT = (@import("std").meta.cast(c_ushort, 1));
2724 \\pub const CURLAUTH_BASIC = (@import("std").meta.cast(c_ulong, 1));
2725 \\pub const CURLAUTH_BASIC_BUT_ULONGLONG = (@import("std").meta.cast(c_ulonglong, 1));
2726 });
2727
2718 cases.add("macro conditional operator",2728 cases.add("macro conditional operator",
2719 \\#define FOO a ? b : c2729 \\#define FOO a ? b : c
2720 , &[_][]const u8{2730 , &[_][]const u8{