Skip to content

Commit

Permalink
translate-c: Ensure all local variables and function params are used
Browse files Browse the repository at this point in the history
  • Loading branch information
ehaas committed Jun 23, 2021
1 parent f1a64dc commit f12b004
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/translate_c.zig
Expand Up @@ -151,6 +151,12 @@ const Scope = struct {
return true;
return scope.base.parent.?.contains(name);
}

fn discardVariable(scope: *Block, c: *Context, name: []const u8) Error!void {
const name_node = try Tag.identifier.create(c.arena, name);
const discard = try Tag.discard.create(c.arena, name_node);
try scope.statements.append(discard);
}
};

const Root = struct {
Expand Down Expand Up @@ -625,6 +631,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void {
const redecl_node = try Tag.arg_redecl.create(c.arena, .{ .actual = mangled_param_name, .mangled = arg_name });
try block_scope.statements.append(redecl_node);
}
try block_scope.discardVariable(c, mangled_param_name);

param_id += 1;
}
Expand Down Expand Up @@ -1802,6 +1809,7 @@ fn transDeclStmtOne(
node = try Tag.static_local_var.create(c.arena, .{ .name = mangled_name, .init = node });
}
try block_scope.statements.append(node);
try block_scope.discardVariable(c, mangled_name);

const cleanup_attr = var_decl.getCleanupAttribute();
if (cleanup_attr) |fn_decl| {
Expand Down Expand Up @@ -4947,6 +4955,10 @@ fn transMacroDefine(c: *Context, m: *MacroCtx) ParseError!void {
const scope = &c.global_scope.base;

const init_node = try parseCExpr(c, m, scope);
if (init_node.castTag(.identifier)) |ident_node| {
if (mem.eql(u8, "_", ident_node.data))
return m.fail(c, "unable to translate C expr: illegal identifier _", .{});
}
const last = m.next().?;
if (last != .Eof and last != .Nl)
return m.fail(c, "unable to translate C expr: unexpected token .{s}", .{@tagName(last)});
Expand Down Expand Up @@ -4977,7 +4989,7 @@ fn transMacroFnDefine(c: *Context, m: *MacroCtx) ParseError!void {
.name = mangled_name,
.type = Tag.@"anytype".init(),
});

try block_scope.discardVariable(c, mangled_name);
if (m.peek().? != .Comma) break;
_ = m.next();
}
Expand Down

0 comments on commit f12b004

Please sign in to comment.