]> granicus.if.org Git - clang/commitdiff
Refactoring of DiagnoseBitwisePrecedence() in SemaExpr.cpp to reduce code duplication.
authorRichard Trieu <rtrieu@google.com>
Wed, 10 Aug 2011 22:41:34 +0000 (22:41 +0000)
committerRichard Trieu <rtrieu@google.com>
Wed, 10 Aug 2011 22:41:34 +0000 (22:41 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@137259 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaExpr.cpp

index f55a6babd8cfa063506987c255b6ee81e59f7e9f..1f0e518afecc6704d7ff31b9147f22fd7ca226a0 100644 (file)
@@ -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<BinOp>(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<BinOp>(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<BinOp>(lhs)->getRHS()->getLocStart(),
+                  rhs->getLocEnd())
+    : SourceRange(lhs->getLocStart(),
+                  cast<BinOp>(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.