Commit 442025481c

Henrik Laxhuber <henrik@laxhuber.com>
2020-07-26 16:27:47
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.
1 parent 6163558
Changed files (2)
src-self-hosted
test
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);
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{