aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenrik Laxhuber <henrik@laxhuber.com>2020-07-26 16:27:47 +0200
committerVeikka Tuominen <git@vexu.eu>2020-07-27 13:43:49 +0300
commit442025481c40625f8010355d2790935e5add0db5 (patch)
tree6b97bea31427e2df675bfec4871977fb4abcf3b0
parent616355807fc8b0a2e7ccc2e54cd4453776c0e187 (diff)
downloadzig-442025481c40625f8010355d2790935e5add0db5.tar.gz
zig-442025481c40625f8010355d2790935e5add0db5.zip
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.
-rw-r--r--src-self-hosted/translate_c.zig17
-rw-r--r--test/translate_c.zig10
2 files changed, 26 insertions, 1 deletions
diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig
index 30cf0bc071..9de3dff94f 100644
--- a/src-self-hosted/translate_c.zig
+++ b/src-self-hosted/translate_c.zig
@@ -5764,7 +5764,22 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
.Keyword_float => return transCreateNodeIdentifierUnchecked(c, "f32"),
.Keyword_short => return transCreateNodeIdentifierUnchecked(c, "c_short"),
.Keyword_char => return transCreateNodeIdentifierUnchecked(c, "c_char"),
- .Keyword_unsigned => return transCreateNodeIdentifierUnchecked(c, "c_uint"),
+ .Keyword_unsigned => if (it.next()) |t| {
+ switch (t.id) {
+ .Keyword_short => return transCreateNodeIdentifierUnchecked(c, "c_ushort"),
+ .Keyword_int => return transCreateNodeIdentifierUnchecked(c, "c_uint"),
+ .Keyword_long => if (it.peek() != null and it.peek().?.id == .Keyword_long) {
+ _ = it.next();
+ return transCreateNodeIdentifierUnchecked(c, "c_ulonglong");
+ } else return transCreateNodeIdentifierUnchecked(c, "c_ulong"),
+ else => {
+ _ = it.prev();
+ return transCreateNodeIdentifierUnchecked(c, "c_uint");
+ },
+ }
+ } else {
+ return transCreateNodeIdentifierUnchecked(c, "c_uint");
+ },
.Identifier => {
const mangled_name = scope.getAlias(source[tok.start..tok.end]);
return transCreateNodeIdentifier(c, mangled_name);
diff --git a/test/translate_c.zig b/test/translate_c.zig
index d12b2ab860..c1c4ef07fa 100644
--- a/test/translate_c.zig
+++ b/test/translate_c.zig
@@ -2715,6 +2715,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\pub const BAR = (@import("std").meta.cast(?*c_void, a));
});
+ cases.add("macro with cast to unsigned short, long, and long long",
+ \\#define CURLAUTH_BASIC_BUT_USHORT ((unsigned short) 1)
+ \\#define CURLAUTH_BASIC ((unsigned long) 1)
+ \\#define CURLAUTH_BASIC_BUT_ULONGLONG ((unsigned long long) 1)
+ , &[_][]const u8{
+ \\pub const CURLAUTH_BASIC_BUT_USHORT = (@import("std").meta.cast(c_ushort, 1));
+ \\pub const CURLAUTH_BASIC = (@import("std").meta.cast(c_ulong, 1));
+ \\pub const CURLAUTH_BASIC_BUT_ULONGLONG = (@import("std").meta.cast(c_ulonglong, 1));
+ });
+
cases.add("macro conditional operator",
\\#define FOO a ? b : c
, &[_][]const u8{