From 37feb2d6a65f84baa4c299871636a49a8e3a8eb9 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Thu, 15 Nov 2012 00:29:07 +0000 Subject: [PATCH] Fix DiagnoseBitwisePrecedence so it doesn't cast "-1" to the type BinaryOperator::Opcode. This is bad form, and the behavior of the static_cast in this case is unspecified according to the standard. Fixes a warning that showed up from r167992 on self-host. llvm-svn: 168010 --- clang/lib/Sema/SemaExpr.cpp | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 2b3cc13..e419ba5 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -8467,46 +8467,38 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, SourceLocation OpLoc, Expr *LHSExpr, Expr *RHSExpr) { - typedef BinaryOperator BinOp; - BinOp::Opcode LHSopc = static_cast(-1), - RHSopc = static_cast(-1); - if (BinOp *BO = dyn_cast(LHSExpr)) - LHSopc = BO->getOpcode(); - if (BinOp *BO = dyn_cast(RHSExpr)) - RHSopc = BO->getOpcode(); - - // Subs are not binary operators. - if (LHSopc == -1 && RHSopc == -1) + BinaryOperator *LHSBO = dyn_cast(LHSExpr); + BinaryOperator *RHSBO = dyn_cast(RHSExpr); + + // Check that one of the sides is a comparison operator. + bool isLeftComp = LHSBO && LHSBO->isComparisonOp(); + bool isRightComp = RHSBO && RHSBO->isComparisonOp(); + if (!isLeftComp && !isRightComp) return; // Bitwise operations are sometimes used as eager logical ops. // Don't diagnose this. - if ((BinOp::isComparisonOp(LHSopc) || BinOp::isBitwiseOp(LHSopc)) && - (BinOp::isComparisonOp(RHSopc) || BinOp::isBitwiseOp(RHSopc))) + bool isLeftBitwise = LHSBO && LHSBO->isBitwiseOp(); + bool isRightBitwise = RHSBO && RHSBO->isBitwiseOp(); + if ((isLeftComp || isLeftBitwise) && (isRightComp || isRightBitwise)) return; - bool isLeftComp = BinOp::isComparisonOp(LHSopc); - bool isRightComp = BinOp::isComparisonOp(RHSopc); - if (!isLeftComp && !isRightComp) return; - SourceRange DiagRange = isLeftComp ? SourceRange(LHSExpr->getLocStart(), OpLoc) : SourceRange(OpLoc, RHSExpr->getLocEnd()); - StringRef OpStr = isLeftComp ? BinOp::getOpcodeStr(LHSopc) - : BinOp::getOpcodeStr(RHSopc); + StringRef OpStr = isLeftComp ? LHSBO->getOpcodeStr() : RHSBO->getOpcodeStr(); SourceRange ParensRange = isLeftComp ? - SourceRange(cast(LHSExpr)->getRHS()->getLocStart(), - RHSExpr->getLocEnd()) - : SourceRange(LHSExpr->getLocStart(), - cast(RHSExpr)->getLHS()->getLocStart()); + SourceRange(LHSBO->getRHS()->getLocStart(), RHSExpr->getLocEnd()) + : SourceRange(LHSExpr->getLocStart(), RHSBO->getLHS()->getLocStart()); Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) - << DiagRange << BinOp::getOpcodeStr(Opc) << OpStr; + << DiagRange << BinaryOperator::getOpcodeStr(Opc) << OpStr; SuggestParentheses(Self, OpLoc, Self.PDiag(diag::note_precedence_silence) << OpStr, (isLeftComp ? LHSExpr : RHSExpr)->getSourceRange()); SuggestParentheses(Self, OpLoc, - Self.PDiag(diag::note_precedence_bitwise_first) << BinOp::getOpcodeStr(Opc), + Self.PDiag(diag::note_precedence_bitwise_first) + << BinaryOperator::getOpcodeStr(Opc), ParensRange); } -- 2.7.4