authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-12-26 13:01:58+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-12-26 13:02:17+02:00
log50e8c3882a3f753fe504a82ebc0d853a1fa1e0ed
treed5ae1724c040279e040891d2b0ea0bb3101274a0
parent641bf4c46eb2d5c1f3e95898ed74848a56e0d999
signaturelock-open Commit is signed but in an unrecognized format.

translate-c: demote variadic functions to declarations


2 files changed, 16 insertions(+), 1 deletions(-)

src/translate_c.zig+7-1
...@@ -550,7 +550,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void {...@@ -550,7 +550,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void {
550 const fn_decl_loc = fn_decl.getLocation();550 const fn_decl_loc = fn_decl.getLocation();
551 const has_body = fn_decl.hasBody();551 const has_body = fn_decl.hasBody();
552 const storage_class = fn_decl.getStorageClass();552 const storage_class = fn_decl.getStorageClass();
553 const decl_ctx = FnDeclContext{553 var decl_ctx = FnDeclContext{
554 .fn_name = fn_name,554 .fn_name = fn_name,
555 .has_body = has_body,555 .has_body = has_body,
556 .storage_class = storage_class,556 .storage_class = storage_class,
...@@ -584,6 +584,12 @@ fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void {...@@ -584,6 +584,12 @@ fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void {
584 const proto_node = switch (fn_type.getTypeClass()) {584 const proto_node = switch (fn_type.getTypeClass()) {
585 .FunctionProto => blk: {585 .FunctionProto => blk: {
586 const fn_proto_type = @ptrCast(*const clang.FunctionProtoType, fn_type);586 const fn_proto_type = @ptrCast(*const clang.FunctionProtoType, fn_type);
587 if (has_body and fn_proto_type.isVariadic()) {
588 decl_ctx.has_body = false;
589 decl_ctx.storage_class = .Extern;
590 decl_ctx.is_export = false;
591 try emitWarning(c, fn_decl_loc, "TODO unable to translate variadic function, demoted to declaration", .{});
592 }
587 break :blk transFnProto(rp, fn_decl, fn_proto_type, fn_decl_loc, decl_ctx, true) catch |err| switch (err) {593 break :blk transFnProto(rp, fn_decl, fn_proto_type, fn_decl_loc, decl_ctx, true) catch |err| switch (err) {
588 error.UnsupportedType => {594 error.UnsupportedType => {
589 return failDecl(c, fn_decl_loc, fn_name, "unable to resolve prototype of function", .{});595 return failDecl(c, fn_decl_loc, fn_name, "unable to resolve prototype of function", .{});
test/translate_c.zig+9
...@@ -3,6 +3,15 @@ const std = @import("std");...@@ -3,6 +3,15 @@ const std = @import("std");
3const CrossTarget = std.zig.CrossTarget;3const CrossTarget = std.zig.CrossTarget;
44
5pub fn addCases(cases: *tests.TranslateCContext) void {5pub fn addCases(cases: *tests.TranslateCContext) void {
6 cases.add("variadic function demoted to prototype",
7 \\int foo(int bar, ...) {
8 \\ return 1;
9 \\}
10 , &[_][]const u8{
11 \\warning: TODO unable to translate variadic function, demoted to declaration
12 \\pub extern fn foo(bar: c_int, ...) c_int;
13 });
14
6 cases.add("pointer to opaque demoted struct",15 cases.add("pointer to opaque demoted struct",
7 \\typedef struct {16 \\typedef struct {
8 \\ _Atomic int foo;17 \\ _Atomic int foo;