authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2021-01-18 11:03:14-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-20 22:26:18-08:00
logbea791b63992d57501c0f878408923806650069b
tree9cf0b88ae167679a3457fe87e32bd1f26b4d9689
parent58344e0017a1e866ee9744b4862c57dbf39284b6

translate-c: fix variadic function calls

1702b413 introduced a bug with variadic function calls - trying to access the paramType of non-existent parameters.

2 files changed, 18 insertions(+), 6 deletions(-)

src/translate_c.zig+9-6
...@@ -3130,12 +3130,15 @@ fn transCallExpr(rp: RestorePoint, scope: *Scope, stmt: *const clang.CallExpr, r...@@ -3130,12 +3130,15 @@ fn transCallExpr(rp: RestorePoint, scope: *Scope, stmt: *const clang.CallExpr, r
3130 if (fn_ty) |ty| {3130 if (fn_ty) |ty| {
3131 switch (ty) {3131 switch (ty) {
3132 .Proto => |fn_proto| {3132 .Proto => |fn_proto| {
3133 const param_qt = fn_proto.getParamType(@intCast(c_uint, i));3133 const param_count = fn_proto.getNumParams();
3134 if (isBoolRes(call_param) and cIsNativeInt(param_qt)) {3134 if (i < param_count) {
3135 const builtin_node = try rp.c.createBuiltinCall("@boolToInt", 1);3135 const param_qt = fn_proto.getParamType(@intCast(c_uint, i));
3136 builtin_node.params()[0] = call_param;3136 if (isBoolRes(call_param) and cIsNativeInt(param_qt)) {
3137 builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")");3137 const builtin_node = try rp.c.createBuiltinCall("@boolToInt", 1);
3138 call_param = &builtin_node.base;3138 builtin_node.params()[0] = call_param;
3139 builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")");
3140 call_param = &builtin_node.base;
3141 }
3139 }3142 }
3140 },3143 },
3141 else => {},3144 else => {},
test/run_translated_c.zig+9
...@@ -737,4 +737,13 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {...@@ -737,4 +737,13 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
737 \\ return 0;737 \\ return 0;
738 \\}738 \\}
739 , "");739 , "");
740
741 cases.add("Variadic function call",
742 \\#define _NO_CRT_STDIO_INLINE 1
743 \\#include <stdio.h>
744 \\int main(void) {
745 \\ printf("%d %d\n", 1, 2);
746 \\ return 0;
747 \\}
748 , "1 2" ++ nl);
740}749}