Skip to content

Commit

Permalink
[clang] Fix unexpected -Wconstant-logical-operand in C23 (#80724)
Browse files Browse the repository at this point in the history
C23 has `bool`, but logical operators still return int. Check that
we're not in C to avoid false-positive -Wconstant-logical-operand.

Fixes #64356

(cherry picked from commit a18e92d)
  • Loading branch information
Fznamznon authored and tstellar committed Feb 9, 2024
1 parent e6fa3ff commit a8158d8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,10 @@ Bug Fixes in This Version
- Fixed assertion failure with deleted overloaded unary operators.
Fixes (`#78314 <https://github.com/llvm/llvm-project/issues/78314>`_)

- Clang now doesn't produce false-positive warning `-Wconstant-logical-operand`
for logical operators in C23.
Fixes (`#64356 <https://github.com/llvm/llvm-project/issues/64356>`_).

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14062,7 +14062,7 @@ 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().CPlusPlus && !RHS.get()->getType()->isBooleanType() &&
!RHS.get()->getExprLoc().isMacroID()) ||
(Result != 0 && Result != 1)) {
Diag(Loc, diag::warn_logical_instead_of_bitwise)
Expand Down
11 changes: 11 additions & 0 deletions clang/test/Sema/warn-int-in-bool-context.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,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;
}

0 comments on commit a8158d8

Please sign in to comment.