[clang] [clang] Fix unexpected `-Wconstant-logical-operand` in C23 (PR #80724)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 5 10:48:01 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Mariya Podchishchaeva (Fznamznon)
<details>
<summary>Changes</summary>
C23 has bool, but logical operators still return int. Double check that we're not in C23 to avoid false-positive -Wconstant-logical-operand.
Fixes https://github.com/llvm/llvm-project/issues/64356
---
Full diff: https://github.com/llvm/llvm-project/pull/80724.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaExpr.cpp (+2-1)
- (modified) clang/test/Sema/warn-int-in-bool-context.c (+11)
``````````diff
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index d15278bce5a6ba..f65b3abe5eaa24 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -14073,7 +14073,8 @@ inline QualType Sema::CheckLogicalOperands(ExprResult &LHS, ExprResult &RHS,
Expr::EvalResult EVResult;
if (RHS.get()->EvaluateAsInt(EVResult, Context)) {
llvm::APSInt Result = EVResult.Val.getInt();
- if ((getLangOpts().Bool && !RHS.get()->getType()->isBooleanType() &&
+ if ((getLangOpts().Bool && !getLangOpts().C23 &&
+ !RHS.get()->getType()->isBooleanType() &&
!RHS.get()->getExprLoc().isMacroID()) ||
(Result != 0 && Result != 1)) {
Diag(Loc, diag::warn_logical_instead_of_bitwise)
diff --git a/clang/test/Sema/warn-int-in-bool-context.c b/clang/test/Sema/warn-int-in-bool-context.c
index a6890161b5af89..c111a5af23f577 100644
--- a/clang/test/Sema/warn-int-in-bool-context.c
+++ b/clang/test/Sema/warn-int-in-bool-context.c
@@ -79,3 +79,14 @@ int test(int a, unsigned b, enum num n) {
// Don't warn in macros.
return SHIFT(1, a);
}
+
+int GH64356(int arg) {
+ if ((arg == 1) && (1 == 1)) return 1;
+ return 0;
+
+ if ((64 > 32) && (32 < 64))
+ return 2;
+
+ if ((1 == 1) && (arg == 1)) return 1;
+ return 0;
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/80724
More information about the cfe-commits
mailing list