[PATCH] D142609: [Clang] Fix -Wconstant-logical-operand when LHS is a constant
Shivam Gupta via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 3 07:19:38 PST 2023
xgupta updated this revision to Diff 494629.
xgupta added a comment.
clang-format
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D142609/new/
https://reviews.llvm.org/D142609
Files:
clang/lib/Sema/SemaExpr.cpp
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -13600,19 +13600,18 @@
Diag(Loc, diag::warn_enum_constant_in_bool_context);
// Diagnose cases where the user write a logical and/or but probably meant a
- // bitwise one. We do this when one of the operand is a non-bool integer and
- // the other is a constant.
- if (!EnumConstantInBoolContext &&
- LHS.get()->getType()->isIntegerType() &&
- !LHS.get()->getType()->isBooleanType() &&
- RHS.get()->getType()->isIntegerType() &&
- !RHS.get()->isValueDependent() &&
+ // bitwise one. We do this when the LHS is a non-bool integer and the RHS
+ // is a constant.
+ if (!EnumConstantInBoolContext && LHS.get()->getType()->isIntegerType() &&
+ !LHS.get()->getType()->isBooleanType() &&
+ RHS.get()->getType()->isIntegerType() && !RHS.get()->isValueDependent() &&
// Don't warn in macros or template instantiations.
!Loc.isMacroID() && !inTemplateInstantiation()) {
- // If the operand can be constant folded, and if it constant folds to
- // something that isn't 0 or 1 (which indicate a potential logical operation
- // that happened to fold to true/false) then warn. Parens on the operand are
- // ignored.
+
+ // If the RHS can be constant folded, and if it constant folds to something
+ // that isn't 0 or 1 (which indicate a potential logical operation that
+ // happened to fold to true/false) then warn.
+ // Parens on the RHS are ignored.
Expr::EvalResult EVResult;
if (RHS.get()->EvaluateAsInt(EVResult, Context)) {
@@ -13636,25 +13635,21 @@
RHS.get()->getEndLoc()));
}
}
-}
-
+ }
// Diagnose cases where the user write a logical and/or but probably meant a
- // bitwise one. We do this when one of the operand is a non-bool integer and
- // the other is a constant.
- if (!EnumConstantInBoolContext &&
- RHS.get()->getType()->isIntegerType() &&
- !RHS.get()->getType()->isBooleanType() &&
- LHS.get()->getType()->isIntegerType() &&
- !LHS.get()->isValueDependent() &&
+ // bitwise one. We do this when the RHS is a non-bool integer and the LHS
+ // is a constant.
+ if (!EnumConstantInBoolContext && RHS.get()->getType()->isIntegerType() &&
+ !RHS.get()->getType()->isBooleanType() &&
+ LHS.get()->getType()->isIntegerType() && !LHS.get()->isValueDependent() &&
// Don't warn in macros or template instantiations.
!Loc.isMacroID() && !inTemplateInstantiation()) {
- // If the operand can be constant folded, and if it constant folds to
- // something that isn't 0 or 1 (which indicate a potential logical operation
- // that happened to fold to true/false) then warn. Parens on the operand are
- // ignored.
+ // If the LHS can be constant folded, and if it constant folds to something
+ // that isn't 0 or 1 (which indicate a potential logical operation that
+ // happened to fold to true/false) then warn.
+ // Parens on the LHS are ignored.
Expr::EvalResult EVResult;
-
if (LHS.get()->EvaluateAsInt(EVResult, Context)) {
llvm::APSInt Result = EVResult.Val.getInt();
if ((getLangOpts().Bool && !LHS.get()->getType()->isBooleanType() &&
@@ -13669,7 +13664,7 @@
SourceRange(Loc, getLocForEndOfToken(Loc)),
Opc == BO_LAnd ? "&" : "|");
if (Opc == BO_LAnd)
- // Suggest replacing "kNonZero && foo() " with "Foo()"
+ // Suggest replacing "kNonZero" && Foo() with "Foo()"
Diag(Loc, diag::note_logical_instead_of_bitwise_remove_constant)
<< FixItHint::CreateRemoval(
SourceRange(getLocForEndOfToken(RHS.get()->getEndLoc()),
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142609.494629.patch
Type: text/x-patch
Size: 3871 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230203/3883d81d/attachment.bin>
More information about the cfe-commits
mailing list