From c7ec5fcf24308e6151845e9c3e8740f7c8682e06 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Thu, 13 Nov 2008 00:03:19 +0000 Subject: Fix bug in constant evaluation exposed by 176.gcc. - Evaluation of , operator used bogus assumption that LHS could be evaluated as an integral expression even though its type is unspecified. This change is making isICE very permissive of the LHS in non-evaluated contexts because it is not clear what predicate we would use to reject code here. The standard didn't offer me any guidance; opinions? llvm-svn: 59196 --- clang/lib/AST/Expr.cpp | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'clang/lib/AST/Expr.cpp') diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index c54fc40ecb2..e9903df6128 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -870,10 +870,26 @@ bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, const BinaryOperator *Exp = cast(this); llvm::APSInt LHS, RHS; + // Comma operator requires special handling. + if (Exp->getOpcode() == BinaryOperator::Comma) { + // C99 6.6p3: "shall not contain assignment, ..., or comma operators, + // *except* when they are contained within a subexpression that is not + // evaluated". Note that Assignment can never happen due to constraints + // on the LHS subexpr, so we don't need to check it here. + if (isEvaluated) { + if (Loc) *Loc = getLocStart(); + return false; + } + + // The result of the constant expr is the RHS. + return Exp->getRHS()->isIntegerConstantExpr(Result, Ctx, Loc, + isEvaluated); + } + // Initialize result to have correct signedness and width. Result = llvm::APSInt(static_cast(Ctx.getTypeSize(getType())), - !getType()->isSignedIntegerType()); - + !getType()->isSignedIntegerType()); + // The LHS of a constant expr is always evaluated and needed. if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated)) return false; @@ -945,20 +961,6 @@ bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, case BinaryOperator::LOr: Result = LHS != 0 || RHS != 0; break; - - case BinaryOperator::Comma: - // C99 6.6p3: "shall not contain assignment, ..., or comma operators, - // *except* when they are contained within a subexpression that is not - // evaluated". Note that Assignment can never happen due to constraints - // on the LHS subexpr, so we don't need to check it here. - if (isEvaluated) { - if (Loc) *Loc = getLocStart(); - return false; - } - - // The result of the constant expr is the RHS. - Result = RHS; - return true; } assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!"); -- cgit v1.2.3