diff --git a/src-self-hosted/clang.zig b/src-self-hosted/clang.zig index 37f1eebe2742..a569113b9d3c 100644 --- a/src-self-hosted/clang.zig +++ b/src-self-hosted/clang.zig @@ -809,6 +809,7 @@ pub extern fn ZigClangQualType_isRestrictQualified(self: struct_ZigClangQualType pub extern fn ZigClangType_getTypeClass(self: ?*const struct_ZigClangType) ZigClangTypeClass; pub extern fn ZigClangType_getPointeeType(self: ?*const struct_ZigClangType) struct_ZigClangQualType; pub extern fn ZigClangType_isVoidType(self: ?*const struct_ZigClangType) bool; +pub extern fn ZigClangType_isConstantArrayType(self: ?*const struct_ZigClangType) bool; pub extern fn ZigClangType_isRecordType(self: ?*const struct_ZigClangType) bool; pub extern fn ZigClangType_isArrayType(self: ?*const struct_ZigClangType) bool; pub extern fn ZigClangType_isBooleanType(self: ?*const struct_ZigClangType) bool; diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index bade253a0748..093acf1083f7 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -1982,7 +1982,8 @@ fn transInitListExprArray( const arr_type = ZigClangType_getAsArrayTypeUnsafe(ty); const child_qt = ZigClangArrayType_getElementType(arr_type); const init_count = ZigClangInitListExpr_getNumInits(expr); - const const_arr_ty = @ptrCast(*const ZigClangConstantArrayType, ty); + assert(ZigClangType_isConstantArrayType(@ptrCast(*const ZigClangType, arr_type))); + const const_arr_ty = @ptrCast(*const ZigClangConstantArrayType, arr_type); const size_ap_int = ZigClangConstantArrayType_getSize(const_arr_ty); const all_count = ZigClangAPInt_getLimitedValue(size_ap_int, math.maxInt(usize)); const leftover_count = all_count - init_count; diff --git a/src/zig_clang.cpp b/src/zig_clang.cpp index 5769d2fc73cc..348c85b87ba0 100644 --- a/src/zig_clang.cpp +++ b/src/zig_clang.cpp @@ -1877,6 +1877,11 @@ bool ZigClangType_isRecordType(const ZigClangType *self) { return casted->isRecordType(); } +bool ZigClangType_isConstantArrayType(const ZigClangType *self) { + auto casted = reinterpret_cast(self); + return casted->isConstantArrayType(); +} + const char *ZigClangType_getTypeClassName(const ZigClangType *self) { auto casted = reinterpret_cast(self); return casted->getTypeClassName(); diff --git a/src/zig_clang.h b/src/zig_clang.h index f9ced941cbf2..6f8d786bf6bf 100644 --- a/src/zig_clang.h +++ b/src/zig_clang.h @@ -945,6 +945,7 @@ ZIG_EXTERN_C bool ZigClangType_isBooleanType(const struct ZigClangType *self); ZIG_EXTERN_C bool ZigClangType_isVoidType(const struct ZigClangType *self); ZIG_EXTERN_C bool ZigClangType_isArrayType(const struct ZigClangType *self); ZIG_EXTERN_C bool ZigClangType_isRecordType(const struct ZigClangType *self); +ZIG_EXTERN_C bool ZigClangType_isConstantArrayType(const ZigClangType *self); ZIG_EXTERN_C const char *ZigClangType_getTypeClassName(const struct ZigClangType *self); ZIG_EXTERN_C const struct ZigClangArrayType *ZigClangType_getAsArrayTypeUnsafe(const struct ZigClangType *self); ZIG_EXTERN_C const ZigClangRecordType *ZigClangType_getAsRecordType(const ZigClangType *self); diff --git a/test/translate_c.zig b/test/translate_c.zig index df33d9b145f1..df2c872bf909 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -2,6 +2,31 @@ const tests = @import("tests.zig"); const builtin = @import("builtin"); pub fn addCases(cases: *tests.TranslateCContext) void { + cases.add("array initializer w/ typedef", + \\typedef unsigned char uuid_t[16]; + \\static const uuid_t UUID_NULL __attribute__ ((unused)) = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; + , &[_][]const u8{ + \\pub const uuid_t = [16]u8; + \\pub const UUID_NULL: uuid_t = .{ + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\ @bitCast(u8, @truncate(i8, @as(c_int, 0))), + \\}; + }); + cases.add("empty declaration", \\; , &[_][]const u8{""});