@@ -2299,13 +2299,6 @@ cp_fold (tree x)
case VEC_COND_EXPR:
case COND_EXPR:
-
- /* Don't bother folding a void condition, since it can't produce a
- constant value. Also, some statement-level uses of COND_EXPR leave
- one of the branches NULL, so folding would crash. */
- if (VOID_TYPE_P (TREE_TYPE (x)))
- return x;
-
loc = EXPR_LOCATION (x);
op0 = cp_fold_rvalue (TREE_OPERAND (x, 0));
op1 = cp_fold (TREE_OPERAND (x, 1));
@@ -2319,6 +2312,29 @@ cp_fold (tree x)
if (!VOID_TYPE_P (TREE_TYPE (op2)))
op2 = cp_truthvalue_conversion (op2);
}
+ else if (VOID_TYPE_P (TREE_TYPE (x)))
+ {
+ if (TREE_CODE (op0) == INTEGER_CST)
+ {
+ /* If the condition is constant, fold can fold away
+ the COND_EXPR. If some statement-level uses of COND_EXPR
+ have one of the branches NULL, avoid folding crash. */
+ if (!op1)
+ op1 = build_empty_stmt (loc);
+ if (!op2)
+ op2 = build_empty_stmt (loc);
+ }
+ else
+ {
+ /* Otherwise, don't bother folding a void condition, since
+ it can't produce a constant value. */
+ if (op0 != TREE_OPERAND (x, 0)
+ || op1 != TREE_OPERAND (x, 1)
+ || op2 != TREE_OPERAND (x, 2))
+ x = build3_loc (loc, code, TREE_TYPE (x), op0, op1, op2);
+ break;
+ }
+ }
if (op0 != TREE_OPERAND (x, 0)
|| op1 != TREE_OPERAND (x, 1)
@@ -0,0 +1,15 @@
+// PR c++/81675
+// { dg-do compile }
+// { dg-options "-Wall" }
+
+struct S
+{
+ ~S () __attribute__((noreturn));
+ int a;
+};
+
+int
+foo ()
+{
+ false ? 5 : S ().a;
+}