mirror of
https://github.com/ziglang/zig.git
synced 2025-02-01 14:55:08 +00:00
translate-c: properly handle enums used as boolean expressions
This commit is contained in:
parent
fc8791c133
commit
fa6546ba74
@ -1931,7 +1931,8 @@ fn finishBoolExpr(
|
||||
},
|
||||
.Enum => {
|
||||
// node != 0
|
||||
return Tag.not_equal.create(c.arena, .{ .lhs = node, .rhs = Tag.zero_literal.init() });
|
||||
const int_val = try Tag.enum_to_int.create(c.arena, node);
|
||||
return Tag.not_equal.create(c.arena, .{ .lhs = int_val, .rhs = Tag.zero_literal.init() });
|
||||
},
|
||||
.Elaborated => {
|
||||
const elaborated_ty = @ptrCast(*const clang.ElaboratedType, ty);
|
||||
|
@ -1508,4 +1508,15 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
|
||||
\\ return 0;
|
||||
\\}
|
||||
, "");
|
||||
|
||||
cases.add("enum used as boolean expression",
|
||||
\\#include <stdlib.h>
|
||||
\\enum FOO {BAR, BAZ};
|
||||
\\int main(void) {
|
||||
\\ enum FOO x = BAR;
|
||||
\\ if (x) abort();
|
||||
\\ if (!BAZ) abort();
|
||||
\\ return 0;
|
||||
\\}
|
||||
, "");
|
||||
}
|
||||
|
@ -2668,7 +2668,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
\\ if (a != 0) return 0;
|
||||
\\ if (b != 0) return 1;
|
||||
\\ if (c != null) return 2;
|
||||
\\ if (d != 0) return 3;
|
||||
\\ if (@enumToInt(d) != 0) return 3;
|
||||
\\ return 4;
|
||||
\\}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user