Skip to content

Commit a18e92d

Browse files
authored
[clang] Fix unexpected -Wconstant-logical-operand in C23 (llvm#80724)
C23 has `bool`, but logical operators still return int. Check that we're not in C to avoid false-positive -Wconstant-logical-operand. Fixes llvm#64356
1 parent a2e5287 commit a18e92d

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

clang/docs/ReleaseNotes.rst

+4
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ Bug Fixes in This Version
164164
- Clang now accepts qualified partial/explicit specializations of variable templates that
165165
are not nominable in the lookup context of the specialization.
166166

167+
- Clang now doesn't produce false-positive warning `-Wconstant-logical-operand`
168+
for logical operators in C23.
169+
Fixes (`#64356 <https://github.com/llvm/llvm-project/issues/64356>`_).
170+
167171
Bug Fixes to Compiler Builtins
168172
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
169173

clang/lib/Sema/SemaExpr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14073,7 +14073,7 @@ inline QualType Sema::CheckLogicalOperands(ExprResult &LHS, ExprResult &RHS,
1407314073
Expr::EvalResult EVResult;
1407414074
if (RHS.get()->EvaluateAsInt(EVResult, Context)) {
1407514075
llvm::APSInt Result = EVResult.Val.getInt();
14076-
if ((getLangOpts().Bool && !RHS.get()->getType()->isBooleanType() &&
14076+
if ((getLangOpts().CPlusPlus && !RHS.get()->getType()->isBooleanType() &&
1407714077
!RHS.get()->getExprLoc().isMacroID()) ||
1407814078
(Result != 0 && Result != 1)) {
1407914079
Diag(Loc, diag::warn_logical_instead_of_bitwise)

clang/test/Sema/warn-int-in-bool-context.c

+11
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,14 @@ int test(int a, unsigned b, enum num n) {
7979
// Don't warn in macros.
8080
return SHIFT(1, a);
8181
}
82+
83+
int GH64356(int arg) {
84+
if ((arg == 1) && (1 == 1)) return 1;
85+
return 0;
86+
87+
if ((64 > 32) && (32 < 64))
88+
return 2;
89+
90+
if ((1 == 1) && (arg == 1)) return 1;
91+
return 0;
92+
}

0 commit comments

Comments
 (0)