[PATCH] D130510: Missing tautological compare warnings due to unary operators
Muhammad Usman Shahid via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 25 13:37:11 PDT 2022
Codesbyusman updated this revision to Diff 447453.
Codesbyusman added a comment.
also updating for the Xor bitwise operator
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D130510/new/
https://reviews.llvm.org/D130510
Files:
clang/lib/Analysis/CFG.cpp
clang/test/SemaCXX/warn-unreachable.cpp
Index: clang/test/SemaCXX/warn-unreachable.cpp
===================================================================
--- clang/test/SemaCXX/warn-unreachable.cpp
+++ clang/test/SemaCXX/warn-unreachable.cpp
@@ -399,13 +399,13 @@
// TODO: Extend warning to the following code:
if (x < -1)
calledFun();
- if (x == -1)
- calledFun();
+ if (x == -1) // expected-note {{silence}}
+ calledFun(); // expected-warning {{will never be executed}}
- if (x != -1)
+ if (x != -1) // expected-note {{silence}}
calledFun();
else
- calledFun();
+ calledFun(); // expected-warning {{will never be executed}}
if (-1 > x)
calledFun();
else
Index: clang/lib/Analysis/CFG.cpp
===================================================================
--- clang/lib/Analysis/CFG.cpp
+++ clang/lib/Analysis/CFG.cpp
@@ -964,42 +964,51 @@
const Expr *LHSExpr = B->getLHS()->IgnoreParens();
const Expr *RHSExpr = B->getRHS()->IgnoreParens();
- const IntegerLiteral *IntLiteral = dyn_cast<IntegerLiteral>(LHSExpr);
- const Expr *BoolExpr = RHSExpr;
+ const Expr *BoolExpr = nullptr; // To store the expression.
+ Expr::EvalResult IntExprResult; // If integer literal then will save value.
- if (!IntLiteral) {
- IntLiteral = dyn_cast<IntegerLiteral>(RHSExpr);
+ if (LHSExpr->EvaluateAsInt(IntExprResult, *Context)) {
+ // Evaluating value.
+ BoolExpr = RHSExpr;
+ }
+ else if (RHSExpr->EvaluateAsInt(IntExprResult, *Context)) {
BoolExpr = LHSExpr;
}
-
- if (!IntLiteral)
+ else
+ {
return TryResult();
+ }
const BinaryOperator *BitOp = dyn_cast<BinaryOperator>(BoolExpr);
if (BitOp && (BitOp->getOpcode() == BO_And ||
- BitOp->getOpcode() == BO_Or)) {
+ BitOp->getOpcode() == BO_Or ||
+ BitOp->getOpcode() == BO_Xor)) {
const Expr *LHSExpr2 = BitOp->getLHS()->IgnoreParens();
const Expr *RHSExpr2 = BitOp->getRHS()->IgnoreParens();
+
+ // If integer literal in expression identified then will save value.
+ Expr::EvalResult IntExprResult2;
- const IntegerLiteral *IntLiteral2 = dyn_cast<IntegerLiteral>(LHSExpr2);
-
- if (!IntLiteral2)
- IntLiteral2 = dyn_cast<IntegerLiteral>(RHSExpr2);
-
- if (!IntLiteral2)
+
+ if (LHSExpr2->EvaluateAsInt(IntExprResult2, *Context));
+ else if ( RHSExpr2->EvaluateAsInt(IntExprResult2, *Context));
+ else
return TryResult();
- llvm::APInt L1 = IntLiteral->getValue();
- llvm::APInt L2 = IntLiteral2->getValue();
+ // Getting the values as integer from the evaluation expression to use for comparision.
+ llvm::APInt L1 = IntExprResult.Val.getInt();
+ llvm::APInt L2 = IntExprResult2.Val.getInt();
+
if ((BitOp->getOpcode() == BO_And && (L2 & L1) != L1) ||
- (BitOp->getOpcode() == BO_Or && (L2 | L1) != L1)) {
+ (BitOp->getOpcode() == BO_Or && (L2 | L1) != L1) ||
+ (BitOp->getOpcode() == BO_Xor && (L2 ^ L1) != L1) ) {
if (BuildOpts.Observer)
BuildOpts.Observer->compareBitwiseEquality(B,
B->getOpcode() != BO_EQ);
TryResult(B->getOpcode() != BO_EQ);
}
} else if (BoolExpr->isKnownToHaveBooleanValue()) {
- llvm::APInt IntValue = IntLiteral->getValue();
+ llvm::APInt IntValue = IntExprResult.Val.getInt(); // Getting the value.
if ((IntValue == 1) || (IntValue == 0)) {
return TryResult();
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130510.447453.patch
Type: text/x-patch
Size: 3572 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220725/fc501d01/attachment-0001.bin>
More information about the cfe-commits
mailing list