authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-18 14:43:17-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-03-18 14:43:17-04:00
loga17bf219c6f67d31f52e2ae7b379784b36c07bd5
treed606e902544a6170ef8a5583b606f3532be784b0
parent7dfbeca13eca48a506bbeba6ce7a18b2a8d25ce1
parent9171e7a7c11d2f5623a30683e1a0f510fe30b4de
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2069 from ziglang/glibc-assert-translation

Implemented enough of translate-c to translate assert

2 files changed, 268 insertions(+), 104 deletions(-)

src/translate_c.cpp+155-71
......@@ -690,6 +690,13 @@ static bool qual_type_child_is_fn_proto(const clang::QualType &qt) {
690690static AstNode* trans_c_cast(Context *c, const clang::SourceLocation &source_location, clang::QualType dest_type,
691691 clang::QualType src_type, AstNode *expr)
692692{
693 // The only way void pointer casts are valid C code, is if
694 // the value of the expression is ignored. We therefore just
695 // return the expr, and let the system that ignores values
696 // translate this correctly.
697 if (qual_type_canon(dest_type)->isVoidType()) {
698 return expr;
699 }
693700 if (qual_types_equal(dest_type, src_type)) {
694701 return expr;
695702 }
......@@ -1225,6 +1232,31 @@ static AstNode *trans_compound_stmt(Context *c, TransScope *scope, const clang::
12251232 return child_scope_block->node;
12261233}
12271234
1235static AstNode *trans_stmt_expr(Context *c, ResultUsed result_used, TransScope *scope,
1236 const clang::StmtExpr *stmt, TransScope **out_node_scope)
1237{
1238 AstNode *block = trans_compound_stmt(c, scope, stmt->getSubStmt(), out_node_scope);
1239 if (block == nullptr)
1240 return block;
1241 assert(block->type == NodeTypeBlock);
1242 if (block->data.block.statements.length == 0)
1243 return block;
1244
1245 Buf *label = buf_create_from_str("x");
1246 block->data.block.name = label;
1247 AstNode *return_expr = block->data.block.statements.pop();
1248 if (return_expr->type == NodeTypeBinOpExpr &&
1249 return_expr->data.bin_op_expr.bin_op == BinOpTypeAssign &&
1250 return_expr->data.bin_op_expr.op1->type == NodeTypeSymbol)
1251 {
1252 Buf *symbol_buf = return_expr->data.bin_op_expr.op1->data.symbol_expr.symbol;
1253 if (strcmp("_", buf_ptr(symbol_buf)) == 0)
1254 return_expr = return_expr->data.bin_op_expr.op2;
1255 }
1256 block->data.block.statements.append(trans_create_node_break(c, label, return_expr));
1257 return maybe_suppress_result(c, result_used, block);
1258}
1259
12281260static AstNode *trans_return_stmt(Context *c, TransScope *scope, const clang::ReturnStmt *stmt) {
12291261 const clang::Expr *value_expr = stmt->getRetValue();
12301262 if (value_expr == nullptr) {
......@@ -1238,13 +1270,15 @@ static AstNode *trans_return_stmt(Context *c, TransScope *scope, const clang::Re
12381270 }
12391271}
12401272
1241static AstNode *trans_integer_literal(Context *c, const clang::IntegerLiteral *stmt) {
1273static AstNode *trans_integer_literal(Context *c, ResultUsed result_used, const clang::IntegerLiteral *stmt) {
12421274 llvm::APSInt result;
12431275 if (!stmt->EvaluateAsInt(result, *reinterpret_cast<clang::ASTContext *>(c->ctx))) {
12441276 emit_warning(c, stmt->getLocStart(), "invalid integer literal");
12451277 return nullptr;
12461278 }
1247 return trans_create_node_apint(c, result);
1279
1280 AstNode *node = trans_create_node_apint(c, result);
1281 return maybe_suppress_result(c, result_used, node);
12481282}
12491283
12501284static AstNode *trans_conditional_operator(Context *c, ResultUsed result_used, TransScope *scope,
......@@ -1381,14 +1415,17 @@ static AstNode *trans_binary_operator(Context *c, ResultUsed result_used, TransS
13811415 case clang::BO_Cmp:
13821416 emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: clang::BO_Cmp");
13831417 return nullptr;
1384 case clang::BO_Mul:
1385 return trans_create_bin_op(c, scope, stmt->getLHS(),
1418 case clang::BO_Mul: {
1419 AstNode *node = trans_create_bin_op(c, scope, stmt->getLHS(),
13861420 qual_type_has_wrapping_overflow(c, stmt->getType()) ? BinOpTypeMultWrap : BinOpTypeMult,
13871421 stmt->getRHS());
1422 return maybe_suppress_result(c, result_used, node);
1423 }
13881424 case clang::BO_Div:
13891425 if (qual_type_has_wrapping_overflow(c, stmt->getType())) {
13901426 // unsigned/float division uses the operator
1391 return trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeDiv, stmt->getRHS());
1427 AstNode *node = trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeDiv, stmt->getRHS());
1428 return maybe_suppress_result(c, result_used, node);
13921429 } else {
13931430 // signed integer division uses @divTrunc
13941431 AstNode *fn_call = trans_create_node_builtin_fn_call_str(c, "divTrunc");
......@@ -1398,12 +1435,13 @@ static AstNode *trans_binary_operator(Context *c, ResultUsed result_used, TransS
13981435 AstNode *rhs = trans_expr(c, ResultUsedYes, scope, stmt->getRHS(), TransLValue);
13991436 if (rhs == nullptr) return nullptr;
14001437 fn_call->data.fn_call_expr.params.append(rhs);
1401 return fn_call;
1438 return maybe_suppress_result(c, result_used, fn_call);
14021439 }
14031440 case clang::BO_Rem:
14041441 if (qual_type_has_wrapping_overflow(c, stmt->getType())) {
14051442 // unsigned/float division uses the operator
1406 return trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeMod, stmt->getRHS());
1443 AstNode *node = trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeMod, stmt->getRHS());
1444 return maybe_suppress_result(c, result_used, node);
14071445 } else {
14081446 // signed integer division uses @rem
14091447 AstNode *fn_call = trans_create_node_builtin_fn_call_str(c, "rem");
......@@ -1413,42 +1451,72 @@ static AstNode *trans_binary_operator(Context *c, ResultUsed result_used, TransS
14131451 AstNode *rhs = trans_expr(c, ResultUsedYes, scope, stmt->getRHS(), TransLValue);
14141452 if (rhs == nullptr) return nullptr;
14151453 fn_call->data.fn_call_expr.params.append(rhs);
1416 return fn_call;
1454 return maybe_suppress_result(c, result_used, fn_call);
14171455 }
1418 case clang::BO_Add:
1419 return trans_create_bin_op(c, scope, stmt->getLHS(),
1456 case clang::BO_Add: {
1457 AstNode *node = trans_create_bin_op(c, scope, stmt->getLHS(),
14201458 qual_type_has_wrapping_overflow(c, stmt->getType()) ? BinOpTypeAddWrap : BinOpTypeAdd,
14211459 stmt->getRHS());
1422 case clang::BO_Sub:
1423 return trans_create_bin_op(c, scope, stmt->getLHS(),
1460 return maybe_suppress_result(c, result_used, node);
1461 }
1462 case clang::BO_Sub: {
1463 AstNode *node = trans_create_bin_op(c, scope, stmt->getLHS(),
14241464 qual_type_has_wrapping_overflow(c, stmt->getType()) ? BinOpTypeSubWrap : BinOpTypeSub,
14251465 stmt->getRHS());
1426 case clang::BO_Shl:
1427 return trans_create_shift_op(c, scope, stmt->getType(), stmt->getLHS(), BinOpTypeBitShiftLeft, stmt->getRHS());
1428 case clang::BO_Shr:
1429 return trans_create_shift_op(c, scope, stmt->getType(), stmt->getLHS(), BinOpTypeBitShiftRight, stmt->getRHS());
1430 case clang::BO_LT:
1431 return trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeCmpLessThan, stmt->getRHS());
1432 case clang::BO_GT:
1433 return trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeCmpGreaterThan, stmt->getRHS());
1434 case clang::BO_LE:
1435 return trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeCmpLessOrEq, stmt->getRHS());
1436 case clang::BO_GE:
1437 return trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeCmpGreaterOrEq, stmt->getRHS());
1438 case clang::BO_EQ:
1439 return trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeCmpEq, stmt->getRHS());
1440 case clang::BO_NE:
1441 return trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeCmpNotEq, stmt->getRHS());
1442 case clang::BO_And:
1443 return trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeBinAnd, stmt->getRHS());
1444 case clang::BO_Xor:
1445 return trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeBinXor, stmt->getRHS());
1446 case clang::BO_Or:
1447 return trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeBinOr, stmt->getRHS());
1448 case clang::BO_LAnd:
1449 return trans_create_bool_bin_op(c, scope, stmt->getLHS(), BinOpTypeBoolAnd, stmt->getRHS());
1450 case clang::BO_LOr:
1451 return trans_create_bool_bin_op(c, scope, stmt->getLHS(), BinOpTypeBoolOr, stmt->getRHS());
1466 return maybe_suppress_result(c, result_used, node);
1467 }
1468 case clang::BO_Shl: {
1469 AstNode *node = trans_create_shift_op(c, scope, stmt->getType(), stmt->getLHS(), BinOpTypeBitShiftLeft, stmt->getRHS());
1470 return maybe_suppress_result(c, result_used, node);
1471 }
1472 case clang::BO_Shr: {
1473 AstNode *node = trans_create_shift_op(c, scope, stmt->getType(), stmt->getLHS(), BinOpTypeBitShiftRight, stmt->getRHS());
1474 return maybe_suppress_result(c, result_used, node);
1475 }
1476 case clang::BO_LT: {
1477 AstNode *node =trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeCmpLessThan, stmt->getRHS());
1478 return maybe_suppress_result(c, result_used, node);
1479 }
1480 case clang::BO_GT: {
1481 AstNode *node = trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeCmpGreaterThan, stmt->getRHS());
1482 return maybe_suppress_result(c, result_used, node);
1483 }
1484 case clang::BO_LE: {
1485 AstNode *node = trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeCmpLessOrEq, stmt->getRHS());
1486 return maybe_suppress_result(c, result_used, node);
1487 }
1488 case clang::BO_GE: {
1489 AstNode *node = trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeCmpGreaterOrEq, stmt->getRHS());
1490 return maybe_suppress_result(c, result_used, node);
1491 }
1492 case clang::BO_EQ: {
1493 AstNode *node = trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeCmpEq, stmt->getRHS());
1494 return maybe_suppress_result(c, result_used, node);
1495 }
1496 case clang::BO_NE: {
1497 AstNode *node = trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeCmpNotEq, stmt->getRHS());
1498 return maybe_suppress_result(c, result_used, node);
1499 }
1500 case clang::BO_And: {
1501 AstNode *node = trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeBinAnd, stmt->getRHS());
1502 return maybe_suppress_result(c, result_used, node);
1503 }
1504 case clang::BO_Xor: {
1505 AstNode *node = trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeBinXor, stmt->getRHS());
1506 return maybe_suppress_result(c, result_used, node);
1507 }
1508 case clang::BO_Or: {
1509 AstNode *node = trans_create_bin_op(c, scope, stmt->getLHS(), BinOpTypeBinOr, stmt->getRHS());
1510 return maybe_suppress_result(c, result_used, node);
1511 }
1512 case clang::BO_LAnd: {
1513 AstNode *node = trans_create_bool_bin_op(c, scope, stmt->getLHS(), BinOpTypeBoolAnd, stmt->getRHS());
1514 return maybe_suppress_result(c, result_used, node);
1515 }
1516 case clang::BO_LOr: {
1517 AstNode *node = trans_create_bool_bin_op(c, scope, stmt->getLHS(), BinOpTypeBoolOr, stmt->getRHS());
1518 return maybe_suppress_result(c, result_used, node);
1519 }
14521520 case clang::BO_Assign:
14531521 return trans_create_assign(c, result_used, scope, stmt->getLHS(), stmt->getRHS());
14541522 case clang::BO_Comma:
......@@ -1460,13 +1528,15 @@ static AstNode *trans_binary_operator(Context *c, ResultUsed result_used, TransS
14601528 AstNode *lhs = trans_expr(c, ResultUsedNo, &scope_block->base, stmt->getLHS(), TransRValue);
14611529 if (lhs == nullptr)
14621530 return nullptr;
1463 scope_block->node->data.block.statements.append(maybe_suppress_result(c, ResultUsedNo, lhs));
1531 scope_block->node->data.block.statements.append(lhs);
14641532
1465 AstNode *rhs = trans_expr(c, result_used, &scope_block->base, stmt->getRHS(), TransRValue);
1533 AstNode *rhs = trans_expr(c, ResultUsedYes, &scope_block->base, stmt->getRHS(), TransRValue);
14661534 if (rhs == nullptr)
14671535 return nullptr;
1468 scope_block->node->data.block.statements.append(trans_create_node_break(c, label_name, maybe_suppress_result(c, result_used, rhs)));
1469 return scope_block->node;
1536
1537 rhs = trans_create_node_break(c, label_name, rhs);
1538 scope_block->node->data.block.statements.append(rhs);
1539 return maybe_suppress_result(c, result_used, scope_block->node);
14701540 }
14711541 case clang::BO_MulAssign:
14721542 case clang::BO_DivAssign:
......@@ -1692,7 +1762,7 @@ static AstNode *trans_compound_assign_operator(Context *c, ResultUsed result_use
16921762 zig_unreachable();
16931763}
16941764
1695static AstNode *trans_implicit_cast_expr(Context *c, TransScope *scope, const clang::ImplicitCastExpr *stmt) {
1765static AstNode *trans_implicit_cast_expr(Context *c, ResultUsed result_used, TransScope *scope, const clang::ImplicitCastExpr *stmt) {
16961766 switch (stmt->getCastKind()) {
16971767 case clang::CK_LValueToRValue:
16981768 return trans_expr(c, ResultUsedYes, scope, stmt->getSubExpr(), TransRValue);
......@@ -1701,8 +1771,9 @@ static AstNode *trans_implicit_cast_expr(Context *c, TransScope *scope, const cl
17011771 AstNode *target_node = trans_expr(c, ResultUsedYes, scope, stmt->getSubExpr(), TransRValue);
17021772 if (target_node == nullptr)
17031773 return nullptr;
1704 return trans_c_cast(c, stmt->getExprLoc(), stmt->getType(),
1774 AstNode *node = trans_c_cast(c, stmt->getExprLoc(), stmt->getType(),
17051775 stmt->getSubExpr()->getType(), target_node);
1776 return maybe_suppress_result(c, result_used, node);
17061777 }
17071778 case clang::CK_FunctionToPointerDecay:
17081779 case clang::CK_ArrayToPointerDecay:
......@@ -1710,7 +1781,7 @@ static AstNode *trans_implicit_cast_expr(Context *c, TransScope *scope, const cl
17101781 AstNode *target_node = trans_expr(c, ResultUsedYes, scope, stmt->getSubExpr(), TransRValue);
17111782 if (target_node == nullptr)
17121783 return nullptr;
1713 return target_node;
1784 return maybe_suppress_result(c, result_used, target_node);
17141785 }
17151786 case clang::CK_BitCast:
17161787 {
......@@ -1727,7 +1798,7 @@ static AstNode *trans_implicit_cast_expr(Context *c, TransScope *scope, const cl
17271798 AstNode *node = trans_create_node_builtin_fn_call_str(c, "ptrCast");
17281799 node->data.fn_call_expr.params.append(dest_type_node);
17291800 node->data.fn_call_expr.params.append(target_node);
1730 return node;
1801 return maybe_suppress_result(c, result_used, node);
17311802 }
17321803 case clang::CK_NullToPointer:
17331804 return trans_create_node_unsigned(c, 0);
......@@ -2103,8 +2174,7 @@ static AstNode *trans_unary_operator(Context *c, ResultUsed result_used, TransSc
21032174 emit_warning(c, stmt->getLocStart(), "TODO handle C translation clang::UO_Imag");
21042175 return nullptr;
21052176 case clang::UO_Extension:
2106 emit_warning(c, stmt->getLocStart(), "TODO handle C translation clang::UO_Extension");
2107 return nullptr;
2177 return trans_expr(c, result_used, scope, stmt->getSubExpr(), TransLValue);
21082178 case clang::UO_Coawait:
21092179 emit_warning(c, stmt->getLocStart(), "TODO handle C translation clang::UO_Coawait");
21102180 return nullptr;
......@@ -2721,7 +2791,9 @@ static AstNode *trans_call_expr(Context *c, ResultUsed result_used, TransScope *
27212791 return node;
27222792}
27232793
2724static AstNode *trans_member_expr(Context *c, TransScope *scope, const clang::MemberExpr *stmt) {
2794static AstNode *trans_member_expr(Context *c, ResultUsed result_used, TransScope *scope,
2795 const clang::MemberExpr *stmt)
2796{
27252797 AstNode *container_node = trans_expr(c, ResultUsedYes, scope, stmt->getBase(), TransRValue);
27262798 if (container_node == nullptr)
27272799 return nullptr;
......@@ -2733,10 +2805,10 @@ static AstNode *trans_member_expr(Context *c, TransScope *scope, const clang::Me
27332805 const char *name = decl_name(stmt->getMemberDecl());
27342806
27352807 AstNode *node = trans_create_node_field_access_str(c, container_node, name);
2736 return node;
2808 return maybe_suppress_result(c, result_used, node);
27372809}
27382810
2739static AstNode *trans_array_subscript_expr(Context *c, TransScope *scope, const clang::ArraySubscriptExpr *stmt) {
2811static AstNode *trans_array_subscript_expr(Context *c, ResultUsed result_used, TransScope *scope, const clang::ArraySubscriptExpr *stmt) {
27402812 AstNode *container_node = trans_expr(c, ResultUsedYes, scope, stmt->getBase(), TransRValue);
27412813 if (container_node == nullptr)
27422814 return nullptr;
......@@ -2749,21 +2821,25 @@ static AstNode *trans_array_subscript_expr(Context *c, TransScope *scope, const
27492821 AstNode *node = trans_create_node(c, NodeTypeArrayAccessExpr);
27502822 node->data.array_access_expr.array_ref_expr = container_node;
27512823 node->data.array_access_expr.subscript = idx_node;
2752 return node;
2824 return maybe_suppress_result(c, result_used, node);
27532825}
27542826
27552827static AstNode *trans_c_style_cast_expr(Context *c, ResultUsed result_used, TransScope *scope,
27562828 const clang::CStyleCastExpr *stmt, TransLRValue lrvalue)
27572829{
2758 AstNode *sub_expr_node = trans_expr(c, result_used, scope, stmt->getSubExpr(), lrvalue);
2830 AstNode *sub_expr_node = trans_expr(c, ResultUsedYes, scope, stmt->getSubExpr(), lrvalue);
27592831 if (sub_expr_node == nullptr)
27602832 return nullptr;
27612833
2762 return trans_c_cast(c, stmt->getLocStart(), stmt->getType(), stmt->getSubExpr()->getType(), sub_expr_node);
2834 AstNode *cast = trans_c_cast(c, stmt->getLocStart(), stmt->getType(), stmt->getSubExpr()->getType(), sub_expr_node);
2835 if (cast == nullptr)
2836 return nullptr;
2837
2838 return maybe_suppress_result(c, result_used, cast);
27632839}
27642840
2765static AstNode *trans_unary_expr_or_type_trait_expr(Context *c, TransScope *scope,
2766 const clang::UnaryExprOrTypeTraitExpr *stmt)
2841static AstNode *trans_unary_expr_or_type_trait_expr(Context *c, ResultUsed result_used,
2842 TransScope *scope, const clang::UnaryExprOrTypeTraitExpr *stmt)
27672843{
27682844 AstNode *type_node = trans_qual_type(c, stmt->getTypeOfArgument(), stmt->getLocStart());
27692845 if (type_node == nullptr)
......@@ -2771,7 +2847,7 @@ static AstNode *trans_unary_expr_or_type_trait_expr(Context *c, TransScope *scop
27712847
27722848 AstNode *node = trans_create_node_builtin_fn_call_str(c, "sizeOf");
27732849 node->data.fn_call_expr.params.append(type_node);
2774 return node;
2850 return maybe_suppress_result(c, result_used, node);
27752851}
27762852
27772853static AstNode *trans_do_loop(Context *c, TransScope *parent_scope, const clang::DoStmt *stmt) {
......@@ -3049,11 +3125,13 @@ static int trans_switch_default(Context *c, TransScope *parent_scope, const clan
30493125 return ErrorNone;
30503126}
30513127
3052static AstNode *trans_string_literal(Context *c, TransScope *scope, const clang::StringLiteral *stmt) {
3128static AstNode *trans_string_literal(Context *c, ResultUsed result_used, TransScope *scope, const clang::StringLiteral *stmt) {
30533129 switch (stmt->getKind()) {
30543130 case clang::StringLiteral::Ascii:
3055 case clang::StringLiteral::UTF8:
3056 return trans_create_node_str_lit_c(c, string_ref_to_buf(stmt->getString()));
3131 case clang::StringLiteral::UTF8: {
3132 AstNode *node = trans_create_node_str_lit_c(c, string_ref_to_buf(stmt->getString()));
3133 return maybe_suppress_result(c, result_used, node);
3134 }
30573135 case clang::StringLiteral::UTF16:
30583136 emit_warning(c, stmt->getLocStart(), "TODO support UTF16 string literals");
30593137 return nullptr;
......@@ -3085,6 +3163,12 @@ static AstNode *trans_continue_stmt(Context *c, TransScope *scope, const clang::
30853163 return trans_create_node(c, NodeTypeContinue);
30863164}
30873165
3166static AstNode *trans_predefined_expr(Context *c, ResultUsed result_used, TransScope *scope,
3167 const clang::PredefinedExpr *expr)
3168{
3169 return trans_string_literal(c, result_used, scope, expr->getFunctionName());
3170}
3171
30883172static int wrap_stmt(AstNode **out_node, TransScope **out_scope, TransScope *in_scope, AstNode *result_node) {
30893173 if (result_node == nullptr)
30903174 return ErrorUnexpected;
......@@ -3109,7 +3193,7 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const clang::Stmt *st
31093193 trans_compound_stmt(c, scope, (const clang::CompoundStmt *)stmt, out_node_scope));
31103194 case clang::Stmt::IntegerLiteralClass:
31113195 return wrap_stmt(out_node, out_child_scope, scope,
3112 trans_integer_literal(c, (const clang::IntegerLiteral *)stmt));
3196 trans_integer_literal(c, result_used, (const clang::IntegerLiteral *)stmt));
31133197 case clang::Stmt::ConditionalOperatorClass:
31143198 return wrap_stmt(out_node, out_child_scope, scope,
31153199 trans_conditional_operator(c, result_used, scope, (const clang::ConditionalOperator *)stmt));
......@@ -3121,7 +3205,7 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const clang::Stmt *st
31213205 trans_compound_assign_operator(c, result_used, scope, (const clang::CompoundAssignOperator *)stmt));
31223206 case clang::Stmt::ImplicitCastExprClass:
31233207 return wrap_stmt(out_node, out_child_scope, scope,
3124 trans_implicit_cast_expr(c, scope, (const clang::ImplicitCastExpr *)stmt));
3208 trans_implicit_cast_expr(c, result_used, scope, (const clang::ImplicitCastExpr *)stmt));
31253209 case clang::Stmt::DeclRefExprClass:
31263210 return wrap_stmt(out_node, out_child_scope, scope,
31273211 trans_decl_ref_expr(c, scope, (const clang::DeclRefExpr *)stmt, lrvalue));
......@@ -3152,28 +3236,28 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const clang::Stmt *st
31523236 return wrap_stmt(out_node, out_child_scope, scope,
31533237 trans_call_expr(c, result_used, scope, (const clang::CallExpr *)stmt));
31543238 case clang::Stmt::NullStmtClass:
3155 *out_node = nullptr;
3239 *out_node = trans_create_node(c, NodeTypeBlock);
31563240 *out_child_scope = scope;
31573241 return ErrorNone;
31583242 case clang::Stmt::MemberExprClass:
31593243 return wrap_stmt(out_node, out_child_scope, scope,
3160 trans_member_expr(c, scope, (const clang::MemberExpr *)stmt));
3244 trans_member_expr(c, result_used, scope, (const clang::MemberExpr *)stmt));
31613245 case clang::Stmt::ArraySubscriptExprClass:
31623246 return wrap_stmt(out_node, out_child_scope, scope,
3163 trans_array_subscript_expr(c, scope, (const clang::ArraySubscriptExpr *)stmt));
3247 trans_array_subscript_expr(c, result_used, scope, (const clang::ArraySubscriptExpr *)stmt));
31643248 case clang::Stmt::CStyleCastExprClass:
31653249 return wrap_stmt(out_node, out_child_scope, scope,
31663250 trans_c_style_cast_expr(c, result_used, scope, (const clang::CStyleCastExpr *)stmt, lrvalue));
31673251 case clang::Stmt::UnaryExprOrTypeTraitExprClass:
31683252 return wrap_stmt(out_node, out_child_scope, scope,
3169 trans_unary_expr_or_type_trait_expr(c, scope, (const clang::UnaryExprOrTypeTraitExpr *)stmt));
3253 trans_unary_expr_or_type_trait_expr(c, result_used, scope, (const clang::UnaryExprOrTypeTraitExpr *)stmt));
31703254 case clang::Stmt::ForStmtClass: {
31713255 AstNode *node = trans_for_loop(c, scope, (const clang::ForStmt *)stmt);
31723256 return wrap_stmt(out_node, out_child_scope, scope, node);
31733257 }
31743258 case clang::Stmt::StringLiteralClass:
31753259 return wrap_stmt(out_node, out_child_scope, scope,
3176 trans_string_literal(c, scope, (const clang::StringLiteral *)stmt));
3260 trans_string_literal(c, result_used, scope, (const clang::StringLiteral *)stmt));
31773261 case clang::Stmt::BreakStmtClass:
31783262 return wrap_stmt(out_node, out_child_scope, scope,
31793263 trans_break_stmt(c, scope, (const clang::BreakStmt *)stmt));
......@@ -3479,8 +3563,8 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const clang::Stmt *st
34793563 emit_warning(c, stmt->getLocStart(), "TODO handle C ParenListExprClass");
34803564 return ErrorUnexpected;
34813565 case clang::Stmt::PredefinedExprClass:
3482 emit_warning(c, stmt->getLocStart(), "TODO handle C PredefinedExprClass");
3483 return ErrorUnexpected;
3566 return wrap_stmt(out_node, out_child_scope, scope,
3567 trans_predefined_expr(c, result_used, scope, (const clang::PredefinedExpr *)stmt));
34843568 case clang::Stmt::PseudoObjectExprClass:
34853569 emit_warning(c, stmt->getLocStart(), "TODO handle C PseudoObjectExprClass");
34863570 return ErrorUnexpected;
......@@ -3491,8 +3575,8 @@ static int trans_stmt_extra(Context *c, TransScope *scope, const clang::Stmt *st
34913575 emit_warning(c, stmt->getLocStart(), "TODO handle C SizeOfPackExprClass");
34923576 return ErrorUnexpected;
34933577 case clang::Stmt::StmtExprClass:
3494 emit_warning(c, stmt->getLocStart(), "TODO handle C StmtExprClass");
3495 return ErrorUnexpected;
3578 return wrap_stmt(out_node, out_child_scope, scope,
3579 trans_stmt_expr(c, result_used, scope, (const clang::StmtExpr *)stmt, out_node_scope));
34963580 case clang::Stmt::SubstNonTypeTemplateParmExprClass:
34973581 emit_warning(c, stmt->getLocStart(), "TODO handle C SubstNonTypeTemplateParmExprClass");
34983582 return ErrorUnexpected;
test/translate_c.zig+113-33
......@@ -59,6 +59,40 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
5959 );
6060 }
6161
62 cases.add("predefined expressions",
63 \\void foo(void) {
64 \\ __func__;
65 \\ __FUNCTION__;
66 \\ __PRETTY_FUNCTION__;
67 \\}
68 ,
69 \\pub fn foo() void {
70 \\ _ = c"foo";
71 \\ _ = c"foo";
72 \\ _ = c"void foo(void)";
73 \\}
74 );
75
76 cases.add("ignore result",
77 \\void foo() {
78 \\ int a;
79 \\ 1;
80 \\ "hey";
81 \\ 1 + 1;
82 \\ 1 - 1;
83 \\ a = 1;
84 \\}
85 ,
86 \\pub fn foo() void {
87 \\ var a: c_int = undefined;
88 \\ _ = 1;
89 \\ _ = c"hey";
90 \\ _ = (1 + 1);
91 \\ _ = (1 - 1);
92 \\ a = 1;
93 \\}
94 );
95
6296 cases.add("for loop with var init but empty body",
6397 \\void foo(void) {
6498 \\ for (int x = 0; x < 10; x++);
......@@ -79,6 +113,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
79113 , // TODO this should be if (1 != 0) break
80114 \\pub fn foo() void {
81115 \\ while (true) {
116 \\ {}
82117 \\ if (!1) break;
83118 \\ }
84119 \\}
......@@ -511,11 +546,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
511546 \\ return b;
512547 \\ else
513548 \\ return a;
549 \\
550 \\ if (a < b) ; else ;
514551 \\}
515552 ,
516553 \\pub export fn max(a: c_int, b: c_int) c_int {
517554 \\ if (a < b) return b;
518555 \\ if (a < b) return b else return a;
556 \\ if (a < b) {} else {}
519557 \\}
520558 );
521559
......@@ -720,7 +758,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
720758 \\ ;;;;;
721759 \\}
722760 ,
723 \\pub export fn foo() void {}
761 \\pub export fn foo() void {
762 \\ {}
763 \\ {}
764 \\ {}
765 \\ {}
766 \\ {}
767 \\}
724768 );
725769
726770 cases.add("undefined array global",
......@@ -751,6 +795,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
751795 \\}
752796 );
753797
798 cases.addC("void cast",
799 \\void foo(int a) {
800 \\ (void) a;
801 \\}
802 ,
803 \\pub export fn foo(a: c_int) void {
804 \\ _ = a;
805 \\}
806 );
807
754808 cases.addC("implicit cast to void *",
755809 \\void *foo(unsigned short *x) {
756810 \\ return x;
......@@ -795,6 +849,32 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
795849 \\}
796850 );
797851
852 cases.addC("statement expression",
853 \\int foo(void) {
854 \\ return ({
855 \\ int a = 1;
856 \\ a;
857 \\ });
858 \\}
859 ,
860 \\pub export fn foo() c_int {
861 \\ return x: {
862 \\ var a: c_int = 1;
863 \\ break :x a;
864 \\ };
865 \\}
866 );
867
868 cases.addC("__extension__ cast",
869 \\int foo(void) {
870 \\ return __extension__ 1;
871 \\}
872 ,
873 \\pub export fn foo() c_int {
874 \\ return 1;
875 \\}
876 );
877
798878 cases.addC("bitshift",
799879 \\int foo(void) {
800880 \\ return (1 << 2) >> 1;
......@@ -1425,52 +1505,52 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
14251505 \\pub export fn bar() void {}
14261506 );
14271507
1428 cases.addC("u integer suffix after 0 (zero) in macro definition",
1429 "#define ZERO 0U"
1430 ,
1431 "pub const ZERO = c_uint(0);"
1508 cases.addC(
1509 "u integer suffix after 0 (zero) in macro definition",
1510 "#define ZERO 0U",
1511 "pub const ZERO = c_uint(0);",
14321512 );
14331513
1434 cases.addC("l integer suffix after 0 (zero) in macro definition",
1435 "#define ZERO 0L"
1436 ,
1437 "pub const ZERO = c_long(0);"
1514 cases.addC(
1515 "l integer suffix after 0 (zero) in macro definition",
1516 "#define ZERO 0L",
1517 "pub const ZERO = c_long(0);",
14381518 );
14391519
1440 cases.addC("ul integer suffix after 0 (zero) in macro definition",
1441 "#define ZERO 0UL"
1442 ,
1443 "pub const ZERO = c_ulong(0);"
1520 cases.addC(
1521 "ul integer suffix after 0 (zero) in macro definition",
1522 "#define ZERO 0UL",
1523 "pub const ZERO = c_ulong(0);",
14441524 );
14451525
1446 cases.addC("lu integer suffix after 0 (zero) in macro definition",
1447 "#define ZERO 0LU"
1448 ,
1449 "pub const ZERO = c_ulong(0);"
1526 cases.addC(
1527 "lu integer suffix after 0 (zero) in macro definition",
1528 "#define ZERO 0LU",
1529 "pub const ZERO = c_ulong(0);",
14501530 );
14511531
1452 cases.addC("ll integer suffix after 0 (zero) in macro definition",
1453 "#define ZERO 0LL"
1454 ,
1455 "pub const ZERO = c_longlong(0);"
1532 cases.addC(
1533 "ll integer suffix after 0 (zero) in macro definition",
1534 "#define ZERO 0LL",
1535 "pub const ZERO = c_longlong(0);",
14561536 );
14571537
1458 cases.addC("ull integer suffix after 0 (zero) in macro definition",
1459 "#define ZERO 0ULL"
1460 ,
1461 "pub const ZERO = c_ulonglong(0);"
1538 cases.addC(
1539 "ull integer suffix after 0 (zero) in macro definition",
1540 "#define ZERO 0ULL",
1541 "pub const ZERO = c_ulonglong(0);",
14621542 );
14631543
1464 cases.addC("llu integer suffix after 0 (zero) in macro definition",
1465 "#define ZERO 0LLU"
1466 ,
1467 "pub const ZERO = c_ulonglong(0);"
1544 cases.addC(
1545 "llu integer suffix after 0 (zero) in macro definition",
1546 "#define ZERO 0LLU",
1547 "pub const ZERO = c_ulonglong(0);",
14681548 );
14691549
1470 cases.addC("bitwise not on u-suffixed 0 (zero) in macro definition",
1471 "#define NOT_ZERO (~0U)"
1472 ,
1473 "pub const NOT_ZERO = ~c_uint(0);"
1550 cases.addC(
1551 "bitwise not on u-suffixed 0 (zero) in macro definition",
1552 "#define NOT_ZERO (~0U)",
1553 "pub const NOT_ZERO = ~c_uint(0);",
14741554 );
14751555
14761556 // cases.add("empty array with initializer",