New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix unnecessary warning when using bitand with boolean operators #81976
Conversation
Signed-off-by: 11happy <soni5happy@gmail.com>
@llvm/pr-subscribers-clang Author: Bhuminjay Soni (11happy) ChangesOverview: Testing: Dependencies:
CC:
Full diff: https://github.com/llvm/llvm-project/pull/81976.diff 3 Files Affected:
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}}
|
Humble Ping! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the improvement! Please be sure to add a release note about the changes as well.
b = foo() bitand bar(); // Ok, no warning expected | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should also add test coverage for C with a macro definition for bitand
and bitor
, and it'd be good to add a test (both C and C++) that does something like:
#define my_fancy_bit_and &
b = foo() my_fancy_bit_and bar();
to show this intentionally works with user-defined macros as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Signed-off-by: 11happy <soni5happy@gmail.com>
currently the macro definition for both |
Signed-off-by: 11happy <soni5happy@gmail.com>
Signed-off-by: 11happy <soni5happy@gmail.com>
The current behavior is what I'd expect. The idea being: if the user is using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a small tweak to the release notes.
Minor tweak to the release notes; NFC
…m#81976) This pull request fixes llvm#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. Fixes llvm#77601
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:
Dependencies:
CC: