authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2017-09-11 21:37:11-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2017-09-11 21:37:11-07:00
log4adffea8d03f63650263e5ea984ecd47b0b3a903
treeb78861fa1657ad130a3a423585b735994b12ab49
parent99cb6e955a4a044a19f98abd1d486560ee9c9729

analysis of result used


1 files changed, 46 insertions(+), 33 deletions(-)

src/parsec.cpp+46-33
...@@ -159,6 +159,14 @@ static AstNode *trans_create_node_bin_op(Context *c, AstNode *lhs_node, BinOpTyp...@@ -159,6 +159,14 @@ static AstNode *trans_create_node_bin_op(Context *c, AstNode *lhs_node, BinOpTyp
159 return node;159 return node;
160}160}
161161
162static AstNode *maybe_suppress_result(Context *c, bool result_used, AstNode *node) {
163 if (result_used) return node;
164 return trans_create_node_bin_op(c,
165 trans_create_node_symbol_str(c, "_"),
166 BinOpTypeAssign,
167 node);
168}
169
162static AstNode *trans_create_node_addr_of(Context *c, bool is_const, bool is_volatile, AstNode *child_node) {170static AstNode *trans_create_node_addr_of(Context *c, bool is_const, bool is_volatile, AstNode *child_node) {
163 AstNode *node = trans_create_node(c, NodeTypeAddrOfExpr);171 AstNode *node = trans_create_node(c, NodeTypeAddrOfExpr);
164 node->data.addr_of_expr.is_const = is_const;172 node->data.addr_of_expr.is_const = is_const;
...@@ -433,11 +441,11 @@ static bool c_is_float(Context *c, QualType qt) {...@@ -433,11 +441,11 @@ static bool c_is_float(Context *c, QualType qt) {
433 }441 }
434}442}
435443
436static AstNode *trans_stmt(Context *c, AstNode *block, Stmt *stmt);444static AstNode *trans_stmt(Context *c, bool result_used, AstNode *block, Stmt *stmt);
437static AstNode *const skip_add_to_block_node = (AstNode *) 0x2;445static AstNode *const skip_add_to_block_node = (AstNode *) 0x2;
438446
439static AstNode *trans_expr(Context *c, AstNode *block, Expr *expr) {447static AstNode *trans_expr(Context *c, bool result_used, AstNode *block, Expr *expr) {
440 return trans_stmt(c, block, expr);448 return trans_stmt(c, result_used, block, expr);
441}449}
442450
443static AstNode *trans_type(Context *c, const Type *ty, const SourceLocation &source_loc) {451static AstNode *trans_type(Context *c, const Type *ty, const SourceLocation &source_loc) {
...@@ -781,7 +789,7 @@ static AstNode *trans_qual_type(Context *c, QualType qt, const SourceLocation &s...@@ -781,7 +789,7 @@ static AstNode *trans_qual_type(Context *c, QualType qt, const SourceLocation &s
781static AstNode *trans_compound_stmt(Context *c, AstNode *parent, CompoundStmt *stmt) {789static AstNode *trans_compound_stmt(Context *c, AstNode *parent, CompoundStmt *stmt) {
782 AstNode *child_block = trans_create_node(c, NodeTypeBlock);790 AstNode *child_block = trans_create_node(c, NodeTypeBlock);
783 for (CompoundStmt::body_iterator it = stmt->body_begin(), end_it = stmt->body_end(); it != end_it; ++it) {791 for (CompoundStmt::body_iterator it = stmt->body_begin(), end_it = stmt->body_end(); it != end_it; ++it) {
784 AstNode *child_node = trans_stmt(c, child_block, *it);792 AstNode *child_node = trans_stmt(c, false, child_block, *it);
785 if (child_node == nullptr)793 if (child_node == nullptr)
786 return nullptr;794 return nullptr;
787 if (child_node != skip_add_to_block_node)795 if (child_node != skip_add_to_block_node)
...@@ -797,7 +805,7 @@ static AstNode *trans_return_stmt(Context *c, AstNode *block, ReturnStmt *stmt)...@@ -797,7 +805,7 @@ static AstNode *trans_return_stmt(Context *c, AstNode *block, ReturnStmt *stmt)
797 return nullptr;805 return nullptr;
798 } else {806 } else {
799 AstNode *return_node = trans_create_node(c, NodeTypeReturnExpr);807 AstNode *return_node = trans_create_node(c, NodeTypeReturnExpr);
800 return_node->data.return_expr.expr = trans_expr(c, block, value_expr);808 return_node->data.return_expr.expr = trans_expr(c, true, block, value_expr);
801 if (return_node->data.return_expr.expr == nullptr)809 if (return_node->data.return_expr.expr == nullptr)
802 return nullptr;810 return nullptr;
803 return return_node;811 return return_node;
...@@ -813,44 +821,44 @@ static AstNode *trans_integer_literal(Context *c, IntegerLiteral *stmt) {...@@ -813,44 +821,44 @@ static AstNode *trans_integer_literal(Context *c, IntegerLiteral *stmt) {
813 return trans_create_node_apint(c, result);821 return trans_create_node_apint(c, result);
814}822}
815823
816static AstNode *trans_conditional_operator(Context *c, AstNode *block, ConditionalOperator *stmt) {824static AstNode *trans_conditional_operator(Context *c, bool result_used, AstNode *block, ConditionalOperator *stmt) {
817 AstNode *node = trans_create_node(c, NodeTypeIfBoolExpr);825 AstNode *node = trans_create_node(c, NodeTypeIfBoolExpr);
818826
819 Expr *cond_expr = stmt->getCond();827 Expr *cond_expr = stmt->getCond();
820 Expr *true_expr = stmt->getTrueExpr();828 Expr *true_expr = stmt->getTrueExpr();
821 Expr *false_expr = stmt->getFalseExpr();829 Expr *false_expr = stmt->getFalseExpr();
822830
823 node->data.if_bool_expr.condition = trans_expr(c, block, cond_expr);831 node->data.if_bool_expr.condition = trans_expr(c, true, block, cond_expr);
824 if (node->data.if_bool_expr.condition == nullptr)832 if (node->data.if_bool_expr.condition == nullptr)
825 return nullptr;833 return nullptr;
826834
827 node->data.if_bool_expr.then_block = trans_expr(c, block, true_expr);835 node->data.if_bool_expr.then_block = trans_expr(c, result_used, block, true_expr);
828 if (node->data.if_bool_expr.then_block == nullptr)836 if (node->data.if_bool_expr.then_block == nullptr)
829 return nullptr;837 return nullptr;
830838
831 node->data.if_bool_expr.else_node = trans_expr(c, block, false_expr);839 node->data.if_bool_expr.else_node = trans_expr(c, result_used, block, false_expr);
832 if (node->data.if_bool_expr.else_node == nullptr)840 if (node->data.if_bool_expr.else_node == nullptr)
833 return nullptr;841 return nullptr;
834842
835 return node;843 return maybe_suppress_result(c, result_used, node);
836}844}
837845
838static AstNode *trans_create_bin_op(Context *c, AstNode *block, Expr *lhs, BinOpType bin_op, Expr *rhs) {846static AstNode *trans_create_bin_op(Context *c, AstNode *block, Expr *lhs, BinOpType bin_op, Expr *rhs) {
839 AstNode *node = trans_create_node(c, NodeTypeBinOpExpr);847 AstNode *node = trans_create_node(c, NodeTypeBinOpExpr);
840 node->data.bin_op_expr.bin_op = bin_op;848 node->data.bin_op_expr.bin_op = bin_op;
841849
842 node->data.bin_op_expr.op1 = trans_expr(c, block, lhs);850 node->data.bin_op_expr.op1 = trans_expr(c, true, block, lhs);
843 if (node->data.bin_op_expr.op1 == nullptr)851 if (node->data.bin_op_expr.op1 == nullptr)
844 return nullptr;852 return nullptr;
845853
846 node->data.bin_op_expr.op2 = trans_expr(c, block, rhs);854 node->data.bin_op_expr.op2 = trans_expr(c, true, block, rhs);
847 if (node->data.bin_op_expr.op2 == nullptr)855 if (node->data.bin_op_expr.op2 == nullptr)
848 return nullptr;856 return nullptr;
849857
850 return node;858 return node;
851}859}
852860
853static AstNode *trans_binary_operator(Context *c, AstNode *block, BinaryOperator *stmt) {861static AstNode *trans_binary_operator(Context *c, bool result_used, AstNode *block, BinaryOperator *stmt) {
854 switch (stmt->getOpcode()) {862 switch (stmt->getOpcode()) {
855 case BO_PtrMemD:863 case BO_PtrMemD:
856 emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_PtrMemD");864 emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_PtrMemD");
...@@ -909,6 +917,7 @@ static AstNode *trans_binary_operator(Context *c, AstNode *block, BinaryOperator...@@ -909,6 +917,7 @@ static AstNode *trans_binary_operator(Context *c, AstNode *block, BinaryOperator
909 emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_LOr");917 emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_LOr");
910 return nullptr;918 return nullptr;
911 case BO_Assign:919 case BO_Assign:
920 (void)result_used;
912 emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_Assign");921 emit_warning(c, stmt->getLocStart(), "TODO handle more C binary operators: BO_Assign");
913 return nullptr;922 return nullptr;
914 case BO_MulAssign:923 case BO_MulAssign:
...@@ -949,7 +958,7 @@ static AstNode *trans_binary_operator(Context *c, AstNode *block, BinaryOperator...@@ -949,7 +958,7 @@ static AstNode *trans_binary_operator(Context *c, AstNode *block, BinaryOperator
949 zig_unreachable();958 zig_unreachable();
950}959}
951960
952static AstNode *trans_compound_assign_operator(Context *c, AstNode *block, CompoundAssignOperator *stmt) {961static AstNode *trans_compound_assign_operator(Context *c, bool result_used, AstNode *block, CompoundAssignOperator *stmt) {
953 switch (stmt->getOpcode()) {962 switch (stmt->getOpcode()) {
954 case BO_MulAssign:963 case BO_MulAssign:
955 emit_warning(c, stmt->getLocStart(), "TODO handle more C compound assign operators: BO_MulAssign");964 emit_warning(c, stmt->getLocStart(), "TODO handle more C compound assign operators: BO_MulAssign");
...@@ -983,7 +992,7 @@ static AstNode *trans_compound_assign_operator(Context *c, AstNode *block, Compo...@@ -983,7 +992,7 @@ static AstNode *trans_compound_assign_operator(Context *c, AstNode *block, Compo
983 AstNode *child_block = trans_create_node(c, NodeTypeBlock);992 AstNode *child_block = trans_create_node(c, NodeTypeBlock);
984993
985 // const _ref = &lhs;994 // const _ref = &lhs;
986 AstNode *lhs = trans_expr(c, child_block, stmt->getLHS());995 AstNode *lhs = trans_expr(c, true, child_block, stmt->getLHS());
987 if (lhs == nullptr) return nullptr;996 if (lhs == nullptr) return nullptr;
988 AstNode *addr_of_lhs = trans_create_node_addr_of(c, false, false, lhs);997 AstNode *addr_of_lhs = trans_create_node_addr_of(c, false, false, lhs);
989 // TODO: avoid name collisions with generated variable names998 // TODO: avoid name collisions with generated variable names
...@@ -993,7 +1002,7 @@ static AstNode *trans_compound_assign_operator(Context *c, AstNode *block, Compo...@@ -993,7 +1002,7 @@ static AstNode *trans_compound_assign_operator(Context *c, AstNode *block, Compo
9931002
994 // *_ref = result_type(operation_type(*_ref) >> u5(rhs));1003 // *_ref = result_type(operation_type(*_ref) >> u5(rhs));
9951004
996 AstNode *rhs = trans_expr(c, child_block, stmt->getRHS());1005 AstNode *rhs = trans_expr(c, true, child_block, stmt->getRHS());
997 if (rhs == nullptr) return nullptr;1006 if (rhs == nullptr) return nullptr;
998 const SourceLocation &rhs_location = stmt->getRHS()->getLocStart();1007 const SourceLocation &rhs_location = stmt->getRHS()->getLocStart();
999 AstNode *rhs_type = qual_type_to_log2_int_ref(c, stmt->getComputationLHSType(), rhs_location);1008 AstNode *rhs_type = qual_type_to_log2_int_ref(c, stmt->getComputationLHSType(), rhs_location);
...@@ -1015,11 +1024,13 @@ static AstNode *trans_compound_assign_operator(Context *c, AstNode *block, Compo...@@ -1015,11 +1024,13 @@ static AstNode *trans_compound_assign_operator(Context *c, AstNode *block, Compo
1015 rhs))));1024 rhs))));
1016 child_block->data.block.statements.append(assign_statement);1025 child_block->data.block.statements.append(assign_statement);
10171026
1018 // *_ref1027 if (result_used) {
1019 child_block->data.block.statements.append(1028 // *_ref
1020 trans_create_node_prefix_op(c, PrefixOpDereference,1029 child_block->data.block.statements.append(
1021 trans_create_node_symbol(c, tmp_var_name)));1030 trans_create_node_prefix_op(c, PrefixOpDereference,
1022 child_block->data.block.last_statement_is_result_expression = true;1031 trans_create_node_symbol(c, tmp_var_name)));
1032 child_block->data.block.last_statement_is_result_expression = true;
1033 }
10231034
1024 return child_block;1035 return child_block;
1025 }1036 }
...@@ -1063,10 +1074,10 @@ static AstNode *trans_compound_assign_operator(Context *c, AstNode *block, Compo...@@ -1063,10 +1074,10 @@ static AstNode *trans_compound_assign_operator(Context *c, AstNode *block, Compo
1063static AstNode *trans_implicit_cast_expr(Context *c, AstNode *block, ImplicitCastExpr *stmt) {1074static AstNode *trans_implicit_cast_expr(Context *c, AstNode *block, ImplicitCastExpr *stmt) {
1064 switch (stmt->getCastKind()) {1075 switch (stmt->getCastKind()) {
1065 case CK_LValueToRValue:1076 case CK_LValueToRValue:
1066 return trans_expr(c, block, stmt->getSubExpr());1077 return trans_expr(c, true, block, stmt->getSubExpr());
1067 case CK_IntegralCast:1078 case CK_IntegralCast:
1068 {1079 {
1069 AstNode *target_node = trans_expr(c, block, stmt->getSubExpr());1080 AstNode *target_node = trans_expr(c, true, block, stmt->getSubExpr());
1070 if (target_node == nullptr)1081 if (target_node == nullptr)
1071 return nullptr;1082 return nullptr;
1072 return trans_c_cast(c, stmt->getExprLoc(), stmt->getType(), target_node);1083 return trans_c_cast(c, stmt->getExprLoc(), stmt->getType(), target_node);
...@@ -1279,7 +1290,7 @@ static AstNode *trans_unary_operator(Context *c, AstNode *block, UnaryOperator *...@@ -1279,7 +1290,7 @@ static AstNode *trans_unary_operator(Context *c, AstNode *block, UnaryOperator *
1279 AstNode *node = trans_create_node(c, NodeTypePrefixOpExpr);1290 AstNode *node = trans_create_node(c, NodeTypePrefixOpExpr);
1280 node->data.prefix_op_expr.prefix_op = PrefixOpNegation;1291 node->data.prefix_op_expr.prefix_op = PrefixOpNegation;
12811292
1282 node->data.prefix_op_expr.primary_expr = trans_expr(c, block, op_expr);1293 node->data.prefix_op_expr.primary_expr = trans_expr(c, true, block, op_expr);
1283 if (node->data.prefix_op_expr.primary_expr == nullptr)1294 if (node->data.prefix_op_expr.primary_expr == nullptr)
1284 return nullptr;1295 return nullptr;
12851296
...@@ -1289,7 +1300,7 @@ static AstNode *trans_unary_operator(Context *c, AstNode *block, UnaryOperator *...@@ -1289,7 +1300,7 @@ static AstNode *trans_unary_operator(Context *c, AstNode *block, UnaryOperator *
1289 AstNode *node = trans_create_node(c, NodeTypeBinOpExpr);1300 AstNode *node = trans_create_node(c, NodeTypeBinOpExpr);
1290 node->data.bin_op_expr.op1 = trans_create_node_unsigned(c, 0);1301 node->data.bin_op_expr.op1 = trans_create_node_unsigned(c, 0);
12911302
1292 node->data.bin_op_expr.op2 = trans_expr(c, block, op_expr);1303 node->data.bin_op_expr.op2 = trans_expr(c, true, block, op_expr);
1293 if (node->data.bin_op_expr.op2 == nullptr)1304 if (node->data.bin_op_expr.op2 == nullptr)
1294 return nullptr;1305 return nullptr;
12951306
...@@ -1331,7 +1342,7 @@ static AstNode *trans_local_declaration(Context *c, AstNode *block, DeclStmt *st...@@ -1331,7 +1342,7 @@ static AstNode *trans_local_declaration(Context *c, AstNode *block, DeclStmt *st
1331 QualType qual_type = var_decl->getTypeSourceInfo()->getType();1342 QualType qual_type = var_decl->getTypeSourceInfo()->getType();
1332 AstNode *init_node = nullptr;1343 AstNode *init_node = nullptr;
1333 if (var_decl->hasInit()) {1344 if (var_decl->hasInit()) {
1334 init_node = trans_expr(c, block, var_decl->getInit());1345 init_node = trans_expr(c, true, block, var_decl->getInit());
1335 if (init_node == nullptr)1346 if (init_node == nullptr)
1336 return nullptr;1347 return nullptr;
13371348
...@@ -1572,18 +1583,18 @@ static AstNode *trans_local_declaration(Context *c, AstNode *block, DeclStmt *st...@@ -1572,18 +1583,18 @@ static AstNode *trans_local_declaration(Context *c, AstNode *block, DeclStmt *st
1572static AstNode *trans_while_loop(Context *c, AstNode *block, WhileStmt *stmt) {1583static AstNode *trans_while_loop(Context *c, AstNode *block, WhileStmt *stmt) {
1573 AstNode *while_node = trans_create_node(c, NodeTypeWhileExpr);1584 AstNode *while_node = trans_create_node(c, NodeTypeWhileExpr);
15741585
1575 while_node->data.while_expr.condition = trans_expr(c, block, stmt->getCond());1586 while_node->data.while_expr.condition = trans_expr(c, true, block, stmt->getCond());
1576 if (while_node->data.while_expr.condition == nullptr)1587 if (while_node->data.while_expr.condition == nullptr)
1577 return nullptr;1588 return nullptr;
15781589
1579 while_node->data.while_expr.body = trans_stmt(c, block, stmt->getBody());1590 while_node->data.while_expr.body = trans_stmt(c, false, block, stmt->getBody());
1580 if (while_node->data.while_expr.body == nullptr)1591 if (while_node->data.while_expr.body == nullptr)
1581 return nullptr;1592 return nullptr;
15821593
1583 return while_node;1594 return while_node;
1584}1595}
15851596
1586static AstNode *trans_stmt(Context *c, AstNode *block, Stmt *stmt) {1597static AstNode *trans_stmt(Context *c, bool result_used, AstNode *block, Stmt *stmt) {
1587 Stmt::StmtClass sc = stmt->getStmtClass();1598 Stmt::StmtClass sc = stmt->getStmtClass();
1588 switch (sc) {1599 switch (sc) {
1589 case Stmt::ReturnStmtClass:1600 case Stmt::ReturnStmtClass:
...@@ -1593,11 +1604,11 @@ static AstNode *trans_stmt(Context *c, AstNode *block, Stmt *stmt) {...@@ -1593,11 +1604,11 @@ static AstNode *trans_stmt(Context *c, AstNode *block, Stmt *stmt) {
1593 case Stmt::IntegerLiteralClass:1604 case Stmt::IntegerLiteralClass:
1594 return trans_integer_literal(c, (IntegerLiteral *)stmt);1605 return trans_integer_literal(c, (IntegerLiteral *)stmt);
1595 case Stmt::ConditionalOperatorClass:1606 case Stmt::ConditionalOperatorClass:
1596 return trans_conditional_operator(c, block, (ConditionalOperator *)stmt);1607 return trans_conditional_operator(c, result_used, block, (ConditionalOperator *)stmt);
1597 case Stmt::BinaryOperatorClass:1608 case Stmt::BinaryOperatorClass:
1598 return trans_binary_operator(c, block, (BinaryOperator *)stmt);1609 return trans_binary_operator(c, result_used, block, (BinaryOperator *)stmt);
1599 case Stmt::CompoundAssignOperatorClass:1610 case Stmt::CompoundAssignOperatorClass:
1600 return trans_compound_assign_operator(c, block, (CompoundAssignOperator *)stmt);1611 return trans_compound_assign_operator(c, result_used, block, (CompoundAssignOperator *)stmt);
1601 case Stmt::ImplicitCastExprClass:1612 case Stmt::ImplicitCastExprClass:
1602 return trans_implicit_cast_expr(c, block, (ImplicitCastExpr *)stmt);1613 return trans_implicit_cast_expr(c, block, (ImplicitCastExpr *)stmt);
1603 case Stmt::DeclRefExprClass:1614 case Stmt::DeclRefExprClass:
...@@ -1747,12 +1758,14 @@ static AstNode *trans_stmt(Context *c, AstNode *block, Stmt *stmt) {...@@ -1747,12 +1758,14 @@ static AstNode *trans_stmt(Context *c, AstNode *block, Stmt *stmt) {
1747 emit_warning(c, stmt->getLocStart(), "TODO handle C CXXUuidofExprClass");1758 emit_warning(c, stmt->getLocStart(), "TODO handle C CXXUuidofExprClass");
1748 return nullptr;1759 return nullptr;
1749 case Stmt::CallExprClass:1760 case Stmt::CallExprClass:
1761 (void)result_used;
1750 emit_warning(c, stmt->getLocStart(), "TODO handle C CallExprClass");1762 emit_warning(c, stmt->getLocStart(), "TODO handle C CallExprClass");
1751 return nullptr;1763 return nullptr;
1752 case Stmt::CUDAKernelCallExprClass:1764 case Stmt::CUDAKernelCallExprClass:
1753 emit_warning(c, stmt->getLocStart(), "TODO handle C CUDAKernelCallExprClass");1765 emit_warning(c, stmt->getLocStart(), "TODO handle C CUDAKernelCallExprClass");
1754 return nullptr;1766 return nullptr;
1755 case Stmt::CXXMemberCallExprClass:1767 case Stmt::CXXMemberCallExprClass:
1768 (void)result_used;
1756 emit_warning(c, stmt->getLocStart(), "TODO handle C CXXMemberCallExprClass");1769 emit_warning(c, stmt->getLocStart(), "TODO handle C CXXMemberCallExprClass");
1757 return nullptr;1770 return nullptr;
1758 case Stmt::CXXOperatorCallExprClass:1771 case Stmt::CXXOperatorCallExprClass:
...@@ -2219,7 +2232,7 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) {...@@ -2219,7 +2232,7 @@ static void visit_fn_decl(Context *c, const FunctionDecl *fn_decl) {
2219 // actual function definition with body2232 // actual function definition with body
22202233
2221 Stmt *body = fn_decl->getBody();2234 Stmt *body = fn_decl->getBody();
2222 AstNode *actual_body_node = trans_stmt(c, nullptr, body);2235 AstNode *actual_body_node = trans_stmt(c, false, nullptr, body);
2223 assert(actual_body_node != skip_add_to_block_node);2236 assert(actual_body_node != skip_add_to_block_node);
2224 if (actual_body_node == nullptr) {2237 if (actual_body_node == nullptr) {
2225 emit_warning(c, fn_decl->getLocation(), "unable to translate function");2238 emit_warning(c, fn_decl->getLocation(), "unable to translate function");