Merge pull request #5223 from iansimonson/translate_c_bool_to_int

Translate-C convert bools to int in complex expressions
This commit is contained in:
Vexu 2020-04-30 18:28:38 +03:00 committed by GitHub
commit 155029b709
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 1 deletions

View File

@ -1293,7 +1293,22 @@ fn transBinaryOperator(
}
const rhs_node = try transExpr(rp, scope, ZigClangBinaryOperator_getRHS(stmt), .used, .r_value);
return transCreateNodeInfixOp(rp, scope, lhs_node, op_id, op_token, rhs_node, result_used, true);
const lhs = if (isBoolRes(lhs_node)) init: {
const cast_node = try transCreateNodeBuiltinFnCall(rp.c, "@boolToInt");
try cast_node.params.push(lhs_node);
cast_node.rparen_token = try appendToken(rp.c, .RParen, ")");
break :init &cast_node.base;
} else lhs_node;
const rhs = if (isBoolRes(rhs_node)) init: {
const cast_node = try transCreateNodeBuiltinFnCall(rp.c, "@boolToInt");
try cast_node.params.push(rhs_node);
cast_node.rparen_token = try appendToken(rp.c, .RParen, ")");
break :init &cast_node.base;
} else rhs_node;
return transCreateNodeInfixOp(rp, scope, lhs, op_id, op_token, rhs, result_used, true);
}
fn transCompoundStmtInline(

View File

@ -195,4 +195,52 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
\\ return 0;
\\}
, "");
cases.add("case boolean expression converted to int",
\\#include <stdlib.h>
\\int main(int argc, char **argv) {
\\ int value = 1 + 2 * 3 + 4 * 5 + 6 << 7 | 8 == 9;
\\ if (value != 4224) abort();
\\ return 0;
\\}
, "");
cases.add("case boolean expression on left converted to int",
\\#include <stdlib.h>
\\int main(int argc, char **argv) {
\\ int value = 8 == 9 | 1 + 2 * 3 + 4 * 5 + 6 << 7;
\\ if (value != 4224) abort();
\\ return 0;
\\}
, "");
cases.add("case boolean and operator+ converts bool to int",
\\#include <stdlib.h>
\\int main(int argc, char **argv) {
\\ int value = (8 == 9) + 3;
\\ int value2 = 3 + (8 == 9);
\\ if (value != value2) abort();
\\ return 0;
\\}
, "");
cases.add("case boolean and operator<",
\\#include <stdlib.h>
\\int main(int argc, char **argv) {
\\ int value = (8 == 9) < 3;
\\ if (value == 0) abort();
\\ return 0;
\\}
, "");
cases.add("case boolean and operator*",
\\#include <stdlib.h>
\\int main(int argc, char **argv) {
\\ int value = (8 == 9) * 3;
\\ int value2 = 3 * (9 == 9);
\\ if (value != 0) abort();
\\ if (value2 == 0) abort();
\\ return 0;
\\}
, "");
}