Skip to content

Commit 0e5b47c

Browse files
committed
translate-c: support goto: label not found error (should be caught by clang)
1 parent 7a6f780 commit 0e5b47c

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/translate_c.zig

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,18 @@ fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void {
491491
param_id += 1;
492492
}
493493

494-
c.goto = try createGotoContext(c, body_stmt, &block_scope);
494+
c.goto = createGotoContext(c, body_stmt, &block_scope) catch |err| switch (err) {
495+
error.OutOfMemory => |e| return e,
496+
error.UnsupportedTranslation,
497+
error.UnsupportedType,
498+
=> {
499+
proto_node.data.is_extern = true;
500+
proto_node.data.is_export = false;
501+
proto_node.data.is_inline = false;
502+
try warn(c, &c.global_scope.base, fn_decl_loc, "unable to translate function, demoted to extern", .{});
503+
return addTopLevelDecl(c, fn_name, Node.initPayload(&proto_node.base));
504+
},
505+
};
495506
defer c.goto = null;
496507

497508
for (c.goto.?.label_variable_names.values()) |variable| {
@@ -6950,7 +6961,7 @@ fn createGotoContext(
69506961
c: *Context,
69516962
stmt: *const clang.Stmt,
69526963
block: *Scope.Block,
6953-
) Error!GotoContext {
6964+
) TransError!GotoContext {
69546965
var label_branches: std.StringArrayHashMapUnmanaged(std.ArrayListUnmanaged(*const clang.Stmt)) = .{};
69556966
defer {
69566967
for (label_branches.values()) |*label_branch| {
@@ -6974,6 +6985,18 @@ fn createGotoContext(
69746985

69756986
try createGotoContextStmt(c, block, stmt, &transformations, &goto.label_variable_names, &goto.goto_targets, &label_branches, &goto_branches);
69766987

6988+
if (goto_branches.items.len != 0) {
6989+
// This should not be possible to happen.
6990+
// https://github.com/llvm/llvm-project/issues/63682
6991+
return fail(
6992+
c,
6993+
error.UnsupportedTranslation,
6994+
goto_branches.items[0].items[0].getBeginLoc(),
6995+
"TODO complex goto with label '{s}'",
6996+
.{try c.str(@as(*const clang.GotoStmt, @ptrCast(goto_branches.items[0].items[0])).getLabel().getName_bytes_begin())},
6997+
);
6998+
}
6999+
69777000
var iter = transformations.iterator();
69787001

69797002
while (iter.next()) |transformation_entry| {
@@ -7221,14 +7244,6 @@ fn createGotoContextCombineStmts(
72217244

72227245
try goto_targets.put(c.arena, goto_stmt, goto_target.value_ptr.*);
72237246

7224-
// if (from_stmt_to_goto_target.get(goto_branch.getLast())) |goto_target| {
7225-
// try goto_targets.put(c.arena, goto_stmt, goto_target);
7226-
// } else {
7227-
// const goto_target = try c.arena.create(?[]const u8);
7228-
// goto_target.* = null;
7229-
7230-
// try from_stmt_to_goto_target.put(c.gpa, goto_branch.getLast(), goto_target);
7231-
72327247
const variable = try label_variable_names.getOrPut(c.arena, label_str);
72337248
if (!variable.found_existing) {
72347249
const bare_variable = try std.fmt.allocPrint(c.arena, "goto_{s}", .{label_str});

0 commit comments

Comments
 (0)