From 0795f17db23fff5aada41008348a63cbc9e0321b Mon Sep 17 00:00:00 2001 From: Vexu Date: Thu, 12 Dec 2019 21:14:55 +0200 Subject: [PATCH] translate-c-2 incompleteArray --- src-self-hosted/clang.zig | 2 ++ src-self-hosted/translate_c.zig | 15 +++++++++- test/translate_c.zig | 50 ++++++++++----------------------- 3 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src-self-hosted/clang.zig b/src-self-hosted/clang.zig index 5b7605c02..eea6d757d 100644 --- a/src-self-hosted/clang.zig +++ b/src-self-hosted/clang.zig @@ -938,6 +938,7 @@ pub extern fn ZigClangImplicitCastExpr_getCastKind(*const ZigClangImplicitCastEx pub extern fn ZigClangImplicitCastExpr_getSubExpr(*const ZigClangImplicitCastExpr) *const ZigClangExpr; pub extern fn ZigClangArrayType_getElementType(*const ZigClangArrayType) ZigClangQualType; +pub extern fn ZigClangIncompleteArrayType_getElementType(*const ZigClangIncompleteArrayType) ZigClangQualType; pub extern fn ZigClangConstantArrayType_getElementType(self: *const struct_ZigClangConstantArrayType) ZigClangQualType; pub extern fn ZigClangConstantArrayType_getSize(self: *const struct_ZigClangConstantArrayType) *const struct_ZigClangAPInt; @@ -968,6 +969,7 @@ pub const struct_ZigClangAPValue = extern struct { Kind: ZigClangAPValueKind, Data: if (builtin.os == .windows and builtin.abi == .msvc) [52]u8 else [68]u8, }; +pub extern fn ZigClangVarDecl_getTypeSourceInfo_getType(self: *const struct_ZigClangVarDecl) struct_ZigClangQualType; pub extern fn ZigClangIntegerLiteral_EvaluateAsInt(*const ZigClangIntegerLiteral, *ZigClangExprEvalResult, *const ZigClangASTContext) bool; pub extern fn ZigClangIntegerLiteral_getBeginLoc(*const ZigClangIntegerLiteral) ZigClangSourceLocation; diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index db3e1c5e4..80d0e2198 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -609,7 +609,7 @@ fn transDeclStmt(rp: RestorePoint, parent_scope: *Scope, stmt: *const ZigClangDe null else try appendToken(c, .Keyword_threadlocal, "threadlocal"); - const qual_type = ZigClangVarDecl_getType(var_decl); + const qual_type = ZigClangVarDecl_getTypeSourceInfo_getType(var_decl); const mut_token = if (ZigClangQualType_isConstQualified(qual_type)) try appendToken(c, .Keyword_const, "const") else @@ -1454,6 +1454,19 @@ fn transType(rp: RestorePoint, ty: *const ZigClangType, source_loc: ZigClangSour node.rhs = try transQualType(rp, ZigClangConstantArrayType_getElementType(const_arr_ty), source_loc); return &node.base; }, + .IncompleteArray => { + const incomplete_array_ty = @ptrCast(*const ZigClangIncompleteArrayType, ty); + + const child_qt = ZigClangIncompleteArrayType_getElementType(incomplete_array_ty); + var node = try transCreateNodePtrType( + rp.c, + ZigClangQualType_isConstQualified(child_qt), + ZigClangQualType_isVolatileQualified(child_qt), + .Identifier, + ); + node.rhs = try transQualType(rp, child_qt, source_loc); + return &node.base; + }, else => { const type_name = rp.c.str(ZigClangType_getTypeClassName(ty)); return revertAndWarn(rp, error.UnsupportedType, source_loc, "unsupported type: '{}'", .{type_name}); diff --git a/test/translate_c.zig b/test/translate_c.zig index 5772763a6..3b067d1be 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -1,11 +1,11 @@ const tests = @import("tests.zig"); const builtin = @import("builtin"); -// add_both - test for stage1 and stage2, in #include mode -// add - test stage1 only, in #include mode -// add_2 - test stage2 only, in #include mode +// add_both - test for stage1 and stage2 +// add - test stage1 only +// add_2 - test stage2 only -pub export fn addCases(cases: *tests.TranslateCContext) void { +pub fn addCases(cases: *tests.TranslateCContext) void { /////////////// Cases that pass for both stage1/stage2 //////////////// cases.add_both("simple function prototypes", \\void __attribute__((noreturn)) foo(void); @@ -66,21 +66,17 @@ pub export fn addCases(cases: *tests.TranslateCContext) void { \\pub var v0: [*c]const u8 = "0.0.0"; }); - /////////////// Cases that pass for only stage2 //////////////// - - cases.add_2("Parameterless function prototypes", - \\void a() {} - \\void b(void) {} - \\void c(); - \\void d(void); + cases.add_both("static incomplete array inside function", + \\void foo(void) { + \\ static const char v2[] = "2.2.2"; + \\} , &[_][]const u8{ - \\pub export fn a() void {} - \\pub export fn b() void {} - \\pub extern fn c(...) void; - \\pub extern fn d() void; + \\pub export fn foo() void { + \\ const v2: [*c]const u8 = "2.2.2"; + \\} }); - cases.add_2("simple function definition", + cases.add_both("simple function definition", \\void foo(void) {} \\static void bar(void) {} , &[_][]const u8{ @@ -88,7 +84,9 @@ pub export fn addCases(cases: *tests.TranslateCContext) void { \\pub fn bar() void {} }); - cases.add_2("parameterless function prototypes", + /////////////// Cases that pass for only stage2 //////////////// + + cases.add_2("Parameterless function prototypes", \\void a() {} \\void b(void) {} \\void c(); @@ -130,14 +128,6 @@ pub export fn addCases(cases: *tests.TranslateCContext) void { \\}; }); - cases.add_both("simple function definition", - \\void foo(void) {} - \\static void bar(void) {} - , &[_][]const u8{ - \\pub export fn foo() void {} - \\pub fn bar() void {} - }); - cases.add("macro with left shift", \\#define REDISMODULE_READ (1<<0) , &[_][]const u8{ @@ -1521,16 +1511,6 @@ pub export fn addCases(cases: *tests.TranslateCContext) void { \\} }); - cases.add("static incomplete array inside function", - \\void foo(void) { - \\ static const char v2[] = "2.2.2"; - \\} - , &[_][]const u8{ - \\pub export fn foo() void { - \\ const v2: [*c]const u8 = "2.2.2"; - \\} - }); - cases.add("macro pointer cast", \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE) , &[_][]const u8{