[PATCH] D130510: updating the function for the tautological warnings for the unary operators
Muhammad Usman Shahid via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 25 12:36:05 PDT 2022
Codesbyusman created this revision.
Codesbyusman added reviewers: aaron.ballman, erichkeane, xgupta.
Herald added a reviewer: NoQ.
Herald added a project: All.
Codesbyusman requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Repository:
rG LLVM Github Monorepo
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,33 +964,43 @@
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 (IsIntegerLiteralConstantExpr(LHSExpr)) {
+ LHSExpr->EvaluateAsInt(IntExprResult, *Context); // Evaluating value.
+ BoolExpr = RHSExpr;
+ }
+ else if (IsIntegerLiteralConstantExpr(RHSExpr)) {
+ RHSExpr->EvaluateAsInt(IntExprResult, *Context); // Evaluating value.
BoolExpr = LHSExpr;
}
-
- if (!IntLiteral)
+ else
+ {
return TryResult();
+ }
const BinaryOperator *BitOp = dyn_cast<BinaryOperator>(BoolExpr);
if (BitOp && (BitOp->getOpcode() == BO_And ||
BitOp->getOpcode() == BO_Or)) {
const Expr *LHSExpr2 = BitOp->getLHS()->IgnoreParens();
const Expr *RHSExpr2 = BitOp->getRHS()->IgnoreParens();
-
- const IntegerLiteral *IntLiteral2 = dyn_cast<IntegerLiteral>(LHSExpr2);
-
- if (!IntLiteral2)
- IntLiteral2 = dyn_cast<IntegerLiteral>(RHSExpr2);
-
- if (!IntLiteral2)
+
+ // If integer literal in expression identified then will save value.
+ Expr::EvalResult IntExprResult2;
+
+
+ if (IsIntegerLiteralConstantExpr(LHSExpr2))
+ LHSExpr2->EvaluateAsInt(IntExprResult2, *Context); // Evaluating value.
+ else if (IsIntegerLiteralConstantExpr(RHSExpr2))
+ RHSExpr2->EvaluateAsInt(IntExprResult2, *Context); // Evaluating value.
+ 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)) {
if (BuildOpts.Observer)
@@ -999,7 +1009,7 @@
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.447434.patch
Type: text/x-patch
Size: 3466 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220725/771ba7e4/attachment.bin>
More information about the cfe-commits
mailing list