Skip to content

Commit efd45b8

Browse files
Fznamznontstellar
authored andcommitted
[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 (cherry picked from commit a18e92d)
1 parent 054d5cd commit efd45b8

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,10 @@ Bug Fixes in This Version
861861
- Fixed assertion failure with deleted overloaded unary operators.
862862
Fixes (`#78314 <https://github.com/llvm/llvm-project/issues/78314>`_)
863863

864+
- Clang now doesn't produce false-positive warning `-Wconstant-logical-operand`
865+
for logical operators in C23.
866+
Fixes (`#64356 <https://github.com/llvm/llvm-project/issues/64356>`_).
867+
864868
Bug Fixes to Compiler Builtins
865869
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
866870

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14062,7 +14062,7 @@ inline QualType Sema::CheckLogicalOperands(ExprResult &LHS, ExprResult &RHS,
1406214062
Expr::EvalResult EVResult;
1406314063
if (RHS.get()->EvaluateAsInt(EVResult, Context)) {
1406414064
llvm::APSInt Result = EVResult.Val.getInt();
14065-
if ((getLangOpts().Bool && !RHS.get()->getType()->isBooleanType() &&
14065+
if ((getLangOpts().CPlusPlus && !RHS.get()->getType()->isBooleanType() &&
1406614066
!RHS.get()->getExprLoc().isMacroID()) ||
1406714067
(Result != 0 && Result != 1)) {
1406814068
Diag(Loc, diag::warn_logical_instead_of_bitwise)

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,14 @@ int test(int a, unsigned b, enum num n) {
7272
// Don't warn in macros.
7373
return SHIFT(1, a);
7474
}
75+
76+
int GH64356(int arg) {
77+
if ((arg == 1) && (1 == 1)) return 1;
78+
return 0;
79+
80+
if ((64 > 32) && (32 < 64))
81+
return 2;
82+
83+
if ((1 == 1) && (arg == 1)) return 1;
84+
return 0;
85+
}

0 commit comments

Comments
 (0)