https://github.com/11happy created https://github.com/llvm/llvm-project/pull/81976
**Overview:** This pull request fixes #77601 where using the `bitand` operator with boolean operands should not trigger the warning, as it would indicate an intentional use of bitwise AND rather than a typo or error. **Testing:** - Tested the updated code. - Verified that other functionalities remain unaffected.  **Dependencies:** - No dependencies on other pull requests. **CC:** - @dtcxzyw >From 07c73f087a430f5115ecdfec23730c5afcab37f3 Mon Sep 17 00:00:00 2001 From: 11happy <soni5ha...@gmail.com> Date: Fri, 16 Feb 2024 14:26:36 +0530 Subject: [PATCH] fix unnecessary warning when using bitand with boolean Signed-off-by: 11happy <soni5ha...@gmail.com> --- clang/lib/Sema/SemaChecking.cpp | 24 ++++++++++++++++++------ clang/test/Sema/warn-bitwise-and-bool.c | 4 ++-- clang/test/Sema/warn-bitwise-or-bool.c | 4 ++-- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 502b24bcdf8b42..e43892cbf35890 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -16191,12 +16191,24 @@ static void AnalyzeImplicitConversions( BO->getRHS()->isKnownToHaveBooleanValue() && BO->getLHS()->HasSideEffects(S.Context) && BO->getRHS()->HasSideEffects(S.Context)) { - S.Diag(BO->getBeginLoc(), diag::warn_bitwise_instead_of_logical) - << (BO->getOpcode() == BO_And ? "&" : "|") << OrigE->getSourceRange() - << FixItHint::CreateReplacement( - BO->getOperatorLoc(), - (BO->getOpcode() == BO_And ? "&&" : "||")); - S.Diag(BO->getBeginLoc(), diag::note_cast_operand_to_int); + clang::SourceManager &SM = S.getSourceManager(); + clang::LangOptions LO = S.getLangOpts(); + clang::SourceLocation BLoc = BO->getOperatorLoc(); + clang::SourceLocation ELoc = + clang::Lexer::getLocForEndOfToken(BLoc, 0, SM, LO); + llvm::StringRef SR = clang::Lexer::getSourceText( + clang::CharSourceRange::getTokenRange(BLoc, ELoc), SM, LO); + + if (SR.str() == "&" || SR.str() == "|") { + + S.Diag(BO->getBeginLoc(), diag::warn_bitwise_instead_of_logical) + << (BO->getOpcode() == BO_And ? "&" : "|") + << OrigE->getSourceRange() + << FixItHint::CreateReplacement( + BO->getOperatorLoc(), + (BO->getOpcode() == BO_And ? "&&" : "||")); + S.Diag(BO->getBeginLoc(), diag::note_cast_operand_to_int); + } } // For conditional operators, we analyze the arguments as if they diff --git a/clang/test/Sema/warn-bitwise-and-bool.c b/clang/test/Sema/warn-bitwise-and-bool.c index 6bec1be1abdef6..23e7afcc59f8aa 100644 --- a/clang/test/Sema/warn-bitwise-and-bool.c +++ b/clang/test/Sema/warn-bitwise-and-bool.c @@ -45,8 +45,8 @@ void test(boolean a, boolean b, int *p, volatile int *q, int i) { b = bar() & (i > 4); b = (i == 7) & foo(); #ifdef __cplusplus - b = foo() bitand bar(); // expected-warning {{use of bitwise '&' with boolean operands}} - // expected-note@-1 {{cast one or both operands to int to silence this warning}} + b = foo() bitand bar(); // Ok, no warning expected + #endif if (foo() & bar()) // expected-warning {{use of bitwise '&' with boolean operands}} diff --git a/clang/test/Sema/warn-bitwise-or-bool.c b/clang/test/Sema/warn-bitwise-or-bool.c index ae86790901aac5..e84f59cf8f766a 100644 --- a/clang/test/Sema/warn-bitwise-or-bool.c +++ b/clang/test/Sema/warn-bitwise-or-bool.c @@ -45,8 +45,8 @@ void test(boolean a, boolean b, int *p, volatile int *q, int i) { b = bar() | (i > 4); b = (i == 7) | foo(); #ifdef __cplusplus - b = foo() bitor bar(); // expected-warning {{use of bitwise '|' with boolean operands}} - // expected-note@-1 {{cast one or both operands to int to silence this warning}} + b = foo() bitor bar(); //Ok, no warning expected + #endif if (foo() | bar()) // expected-warning {{use of bitwise '|' with boolean operands}} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits