mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
parent
4e5a88b288
commit
c6b4fe0066
2 changed files with 20 additions and 1 deletions
|
|
@ -3648,7 +3648,13 @@ fn transUnaryOperator(c: *Context, scope: *Scope, stmt: *const clang.UnaryOperat
|
||||||
.Plus => return transExpr(c, scope, op_expr, used),
|
.Plus => return transExpr(c, scope, op_expr, used),
|
||||||
.Minus => {
|
.Minus => {
|
||||||
if (!qualTypeHasWrappingOverflow(op_expr.getType())) {
|
if (!qualTypeHasWrappingOverflow(op_expr.getType())) {
|
||||||
return Tag.negate.create(c.arena, try transExpr(c, scope, op_expr, .used));
|
const sub_expr_node = try transExpr(c, scope, op_expr, .used);
|
||||||
|
const to_negate = if (isBoolRes(sub_expr_node)) blk: {
|
||||||
|
const ty_node = try Tag.type.create(c.arena, "c_int");
|
||||||
|
const int_node = try Tag.bool_to_int.create(c.arena, sub_expr_node);
|
||||||
|
break :blk try Tag.as.create(c.arena, .{ .lhs = ty_node, .rhs = int_node });
|
||||||
|
} else sub_expr_node;
|
||||||
|
return Tag.negate.create(c.arena, to_negate);
|
||||||
} else if (cIsUnsignedInteger(op_expr.getType())) {
|
} else if (cIsUnsignedInteger(op_expr.getType())) {
|
||||||
// use -% x for unsigned integers
|
// use -% x for unsigned integers
|
||||||
return Tag.negate_wrap.create(c.arena, try transExpr(c, scope, op_expr, .used));
|
return Tag.negate_wrap.create(c.arena, try transExpr(c, scope, op_expr, .used));
|
||||||
|
|
|
||||||
|
|
@ -1796,4 +1796,17 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
|
||||||
\\ return 0;
|
\\ return 0;
|
||||||
\\}
|
\\}
|
||||||
, "");
|
, "");
|
||||||
|
|
||||||
|
cases.add("Boolean expression coerced to int. Issue #10175",
|
||||||
|
\\#include <stdlib.h>
|
||||||
|
\\int sign(int v) {
|
||||||
|
\\ return -(v < 0);
|
||||||
|
\\}
|
||||||
|
\\int main(void) {
|
||||||
|
\\ if (sign(-5) != -1) abort();
|
||||||
|
\\ if (sign(5) != 0) abort();
|
||||||
|
\\ if (sign(0) != 0) abort();
|
||||||
|
\\ return 0;
|
||||||
|
\\}
|
||||||
|
, "");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue