Skip to content

Commit f7d3048

Browse files
committed
Fix -Wunsequenced false-positives in code controlled by a branch on
__builtin_constant_p. If the operand of __builtin_constant_p is not constant and has side-effects, then code controlled by a branch on it is unreachable and we should not emit runtime behavior warnings in such code. llvm-svn: 359844
1 parent 3af3900 commit f7d3048

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

clang/lib/AST/ExprConstant.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -8269,11 +8269,14 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
82698269

82708270
case Builtin::BI__builtin_constant_p: {
82718271
const Expr *Arg = E->getArg(0);
8272-
if (EvaluateBuiltinConstantP(Info, Arg))
8272+
if (EvaluateBuiltinConstantP(Info, Arg)) {
82738273
return Success(true, E);
8274-
else if (Info.InConstantContext)
8274+
} else if (Info.InConstantContext || Arg->HasSideEffects(Info.Ctx)) {
8275+
// Outside a constant context, eagerly evaluate to false in the presence
8276+
// of side-effects in order to avoid -Wunsequenced false-positives in
8277+
// a branch on __builtin_constant_p(expr).
82758278
return Success(false, E);
8276-
else {
8279+
} else {
82778280
Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr);
82788281
return false;
82798282
}

clang/test/Sema/warn-unsequenced.c

+2
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,6 @@ void test() {
9393
_Generic(++a, default: 0) + ++a; // ok
9494
sizeof(++a) + ++a; // ok
9595
_Alignof(++a) + ++a; // expected-warning {{extension}}
96+
97+
__builtin_constant_p(f(++a, 0)) ? f(f(++a, 0), f(++a, 0)) : 0;
9698
}

0 commit comments

Comments
 (0)