Skip to content

Commit a509366

Browse files
jakub-labenskinv-cmeerw
authored andcommitted
Preserve GNU statement expressions in folded logical operands. [GH #577]
1 parent ceb823f commit a509366

5 files changed

Lines changed: 83 additions & 17 deletions

File tree

‎src/exprutil.c‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15708,29 +15708,28 @@ of a subscript operation).
1570815708
operand_2->variant.constant.folded_statement_expression;
1570915709
} /* if */
1571015710
} /* if */
15711-
} else if (op == eok_lor && constexpr_enabled &&
15711+
} else if ((op == eok_lor || op == eok_land) && constexpr_enabled &&
1571215712
!expr_stack->possible_rescan_context &&
1571315713
is_constant_operand(operand_1) &&
1571415714
constant_bool_value_known_at_compile_time(
1571515715
&operand_1->variant.constant) &&
15716-
!op_is_false_constant(operand_1)) {
15717-
/* (1 || nonconstant) produces true in C++11. */
15718-
eliminate_stmt_expr_refs_if_operand_dropped(operand_2);
15719-
discard_operand(operand_2);
15720-
make_integer_constant_operand(result, (a_host_large_integer)1);
15721-
cast_operand(result_type, result, /*is_implicit_cast=*/TRUE);
15722-
did_not_fold = FALSE;
15723-
} else if (op == eok_land && constexpr_enabled &&
15724-
!expr_stack->possible_rescan_context &&
15725-
is_constant_operand(operand_1) &&
15726-
constant_bool_value_known_at_compile_time(
15727-
&operand_1->variant.constant) &&
15728-
op_is_false_constant(operand_1)) {
15729-
/* (0 && nonconstant) produces false in C++11. */
15730-
eliminate_stmt_expr_refs_if_operand_dropped(operand_2);
15716+
op_is_false_constant(operand_1) == (op == eok_land)) {
15717+
/* The left operand determines the value of the operation. Preserve
15718+
statement-expression entries if the right operand will appear in
15719+
the backing expression. */
15720+
a_boolean record_backing_expr =
15721+
curr_expr_kind_is_one_in_which_const_exprs_are_recorded();
15722+
if (!record_backing_expr) {
15723+
eliminate_stmt_expr_refs_if_operand_dropped(operand_2);
15724+
} /* if */
1573115725
discard_operand(operand_2);
15732-
make_integer_constant_operand(result, (a_host_large_integer)0);
15726+
make_integer_constant_operand(result,
15727+
(a_host_large_integer)(op == eok_lor));
1573315728
cast_operand(result_type, result, /*is_implicit_cast=*/TRUE);
15729+
if (record_backing_expr) {
15730+
result->variant.constant.folded_statement_expression =
15731+
expr_stack->statement_expression_seen;
15732+
} /* if */
1573415733
did_not_fold = FALSE;
1573515734
#if GNU_EXTENSIONS_ALLOWED
1573615735
} else if ((op == eok_pdiff || op == eok_eq || op == eok_ne) &&
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[GH #577]
2+
C++-generating back end: Abort on GNU statement expressions in logical operands
3+
4+
The C++-generating back end could abort with a failed assertion in
5+
check_for_and_take_source_seq_entry when the right operand of a
6+
short-circuited && or || contained a GNU statement expression with a local
7+
declaration. For example, with --gnu_version=150200:
8+
9+
struct S { bool operator&&(int); };
10+
bool f(int n) {
11+
return false && ({ int i = n; i; }); // Previously triggered an abort.
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpfe -DTEST_NUMBER=1 --clang --clang_version=220100 Test_name.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpfe -DTEST_NUMBER=2 --g++ --gnu_version=150200 Test_name.c
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//type: cp
2+
//options:--clang --clang_version=220100:--g++ --gnu_version=150200
3+
4+
bool builtin_operators(bool b) {
5+
// With no overloads declared yet, the scanner knows from the constant
6+
// left operands that both initializers skip the right operand.
7+
bool left_false = false && ({ int value = b; value; });
8+
bool left_true = true || ({ int value = b; value; });
9+
// The final statement expression executes when b is true.
10+
return (left_false == left_true) !=
11+
(b && ({ int value = b; value; }));
12+
}
13+
14+
// These declarations make the scanner consider overloading when it sees
15+
// either operator name.
16+
struct operators {
17+
bool operator&&(int);
18+
bool operator||(int);
19+
};
20+
21+
struct wrapper {
22+
bool has_value();
23+
};
24+
25+
void test() {
26+
// Both expressions use built-in operators. The calls to has_value keep
27+
// the right operands nonconstant, so the compiler short-circuits during
28+
// binary folding. The C++ generator still needs both local declarations.
29+
(void)(0 && ({
30+
wrapper value;
31+
value;
32+
}).has_value());
33+
(void)(1 || ({
34+
wrapper value;
35+
value;
36+
}).has_value());
37+
}
38+
39+
bool nested_short_circuit(int i) {
40+
// The parameter keeps the right operand of && nonconstant. The right
41+
// operand of || folds on its own. The enclosing comparison folds both
42+
// results and the C++ generator still needs the declarations in both blocks.
43+
return (false && ({
44+
int value = i;
45+
value;
46+
})) == (true || ({
47+
struct local {
48+
bool value;
49+
};
50+
local item{false};
51+
item.value;
52+
}));
53+
}

0 commit comments

Comments
 (0)