From 70979d4de873382d0ba13b5bd8fb3ee797384582 Mon Sep 17 00:00:00 2001 From: Richard Trieu Date: Wed, 10 Aug 2011 22:41:34 +0000 Subject: [PATCH] Refactoring of DiagnoseBitwisePrecedence() in SemaExpr.cpp to reduce code duplication. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@137259 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaExpr.cpp | 48 ++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index f55a6babd8..1f0e518afe 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -7776,32 +7776,28 @@ static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) return; - if (BinOp::isComparisonOp(lhsopc)) { - Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) - << SourceRange(lhs->getLocStart(), OpLoc) - << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc); - SuggestParentheses(Self, OpLoc, - Self.PDiag(diag::note_precedence_bitwise_silence) - << BinOp::getOpcodeStr(lhsopc), - lhs->getSourceRange()); - SuggestParentheses(Self, OpLoc, - Self.PDiag(diag::note_precedence_bitwise_first) - << BinOp::getOpcodeStr(Opc), - SourceRange(cast(lhs)->getRHS()->getLocStart(), rhs->getLocEnd())); - } else if (BinOp::isComparisonOp(rhsopc)) { - Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) - << SourceRange(OpLoc, rhs->getLocEnd()) - << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc); - SuggestParentheses(Self, OpLoc, - Self.PDiag(diag::note_precedence_bitwise_silence) - << BinOp::getOpcodeStr(rhsopc), - rhs->getSourceRange()); - SuggestParentheses(Self, OpLoc, - Self.PDiag(diag::note_precedence_bitwise_first) - << BinOp::getOpcodeStr(Opc), - SourceRange(lhs->getLocStart(), - cast(rhs)->getLHS()->getLocStart())); - } + bool isLeftComp = BinOp::isComparisonOp(lhsopc); + bool isRightComp = BinOp::isComparisonOp(rhsopc); + if (!isLeftComp && !isRightComp) return; + + SourceRange DiagRange = isLeftComp ? SourceRange(lhs->getLocStart(), OpLoc) + : SourceRange(OpLoc, rhs->getLocEnd()); + std::string OpStr = isLeftComp ? BinOp::getOpcodeStr(lhsopc) + : BinOp::getOpcodeStr(rhsopc); + SourceRange ParensRange = isLeftComp ? + SourceRange(cast(lhs)->getRHS()->getLocStart(), + rhs->getLocEnd()) + : SourceRange(lhs->getLocStart(), + cast(rhs)->getLHS()->getLocStart()); + + Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) + << DiagRange << BinOp::getOpcodeStr(Opc) << OpStr; + SuggestParentheses(Self, OpLoc, + Self.PDiag(diag::note_precedence_bitwise_silence) << OpStr, + rhs->getSourceRange()); + SuggestParentheses(Self, OpLoc, + Self.PDiag(diag::note_precedence_bitwise_first) << BinOp::getOpcodeStr(Opc), + ParensRange); } /// \brief It accepts a '&' expr that is inside a '|' one. -- 2.40.0