Skip to content

Commit

Permalink
[clang] Fix unexpected -Wconstant-logical-operand in C23
Browse files Browse the repository at this point in the history
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 llvm#64356
  • Loading branch information
Fznamznon committed Feb 5, 2024
1 parent cb8d83a commit dd81e1a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 @@ -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;
}

0 comments on commit dd81e1a

Please sign in to comment.