Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions src/exprutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -15708,29 +15708,28 @@ of a subscript operation).
operand_2->variant.constant.folded_statement_expression;
} /* if */
} /* if */
} else if (op == eok_lor && constexpr_enabled &&
} else if ((op == eok_lor || op == eok_land) && constexpr_enabled &&
!expr_stack->possible_rescan_context &&
is_constant_operand(operand_1) &&
constant_bool_value_known_at_compile_time(
&operand_1->variant.constant) &&
!op_is_false_constant(operand_1)) {
/* (1 || nonconstant) produces true in C++11. */
eliminate_stmt_expr_refs_if_operand_dropped(operand_2);
discard_operand(operand_2);
make_integer_constant_operand(result, (a_host_large_integer)1);
cast_operand(result_type, result, /*is_implicit_cast=*/TRUE);
did_not_fold = FALSE;
} else if (op == eok_land && constexpr_enabled &&
!expr_stack->possible_rescan_context &&
is_constant_operand(operand_1) &&
constant_bool_value_known_at_compile_time(
&operand_1->variant.constant) &&
op_is_false_constant(operand_1)) {
/* (0 && nonconstant) produces false in C++11. */
eliminate_stmt_expr_refs_if_operand_dropped(operand_2);
op_is_false_constant(operand_1) == (op == eok_land)) {
/* The left operand determines the value of the operation. Preserve
statement-expression entries if the right operand will appear in
the backing expression. */
a_boolean record_backing_expr =
curr_expr_kind_is_one_in_which_const_exprs_are_recorded();
if (!record_backing_expr) {
eliminate_stmt_expr_refs_if_operand_dropped(operand_2);
} /* if */
discard_operand(operand_2);
make_integer_constant_operand(result, (a_host_large_integer)0);
make_integer_constant_operand(result,
(a_host_large_integer)(op == eok_lor));
cast_operand(result_type, result, /*is_implicit_cast=*/TRUE);
if (record_backing_expr) {
result->variant.constant.folded_statement_expression =
expr_stack->statement_expression_seen;
} /* if */
did_not_fold = FALSE;
#if GNU_EXTENSIONS_ALLOWED
} else if ((op == eok_pdiff || op == eok_eq || op == eok_ne) &&
Expand Down
12 changes: 12 additions & 0 deletions src/pending_changes/gh-577-short-circuit-statement-expr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[GH #577]
C++-generating back end: Abort on GNU statement expressions in logical operands

The C++-generating back end could abort with a failed assertion in
check_for_and_take_source_seq_entry when the right operand of a
short-circuited && or || contained a GNU statement expression with a local
declaration. For example, with --gnu_version=150200:

struct S { bool operator&&(int); };
bool f(int n) {
return false && ({ int i = n; i; }); // Previously triggered an abort.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpfe -DTEST_NUMBER=1 --clang --clang_version=220100 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpfe -DTEST_NUMBER=2 --g++ --gnu_version=150200 Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//type: cp
//options:--clang --clang_version=220100:--g++ --gnu_version=150200

bool builtin_operators(bool b) {
// With no overloads declared yet, the scanner knows from the constant
// left operands that both initializers skip the right operand.
bool left_false = false && ({ int value = b; value; });
bool left_true = true || ({ int value = b; value; });
// The final statement expression executes when b is true.
return (left_false == left_true) !=
(b && ({ int value = b; value; }));
}

// These declarations make the scanner consider overloading when it sees
// either operator name.
struct operators {
bool operator&&(int);
bool operator||(int);
};

struct wrapper {
bool has_value();
};

void test() {
// Both expressions use built-in operators. The calls to has_value keep
// the right operands nonconstant, so the compiler short-circuits during
// binary folding. The C++ generator still needs both local declarations.
(void)(0 && ({
wrapper value;
value;
}).has_value());
(void)(1 || ({
wrapper value;
value;
}).has_value());
}

bool nested_short_circuit(int i) {
// The parameter keeps the right operand of && nonconstant. The right
// operand of || folds on its own. The enclosing comparison folds both
// results and the C++ generator still needs the declarations in both blocks.
return (false && ({
int value = i;
value;
})) == (true || ({
struct local {
bool value;
};
local item{false};
item.value;
}));
}
Loading