fix unnecessary warning when using bitand with boolean operators (#81976)
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. Fixes #77601
This commit is contained in:
parent
daebe5c4f2
commit
ab28c1de23
@ -250,6 +250,10 @@ Improvements to Clang's diagnostics
|
||||
such as attempting to call ``free`` on an unallocated object. Fixes
|
||||
`#79443 <https://github.com/llvm/llvm-project/issues/79443>`_.
|
||||
|
||||
- Clang no longer warns when the ``bitand`` operator is used with boolean
|
||||
operands, distinguishing it from potential typographical errors or unintended
|
||||
bitwise operations. Fixes #GH77601.
|
||||
|
||||
Improvements to Clang's time-trace
|
||||
----------------------------------
|
||||
|
||||
|
@ -16605,12 +16605,26 @@ 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);
|
||||
SourceManager &SM = S.getSourceManager();
|
||||
const LangOptions &LO = S.getLangOpts();
|
||||
SourceLocation BLoc = BO->getOperatorLoc();
|
||||
SourceLocation ELoc = Lexer::getLocForEndOfToken(BLoc, 0, SM, LO);
|
||||
StringRef SR = clang::Lexer::getSourceText(
|
||||
clang::CharSourceRange::getTokenRange(BLoc, ELoc), SM, LO);
|
||||
// To reduce false positives, only issue the diagnostic if the operator
|
||||
// is explicitly spelled as a punctuator. This suppresses the diagnostic
|
||||
// when using 'bitand' or 'bitor' either as keywords in C++ or as macros
|
||||
// in C, along with other macro spellings the user might invent.
|
||||
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
|
||||
|
@ -19,6 +19,8 @@ boolean baz(void) __attribute__((const));
|
||||
void sink(boolean);
|
||||
|
||||
#define FOO foo()
|
||||
#define MY_AND &
|
||||
#define My_BITAND bitand
|
||||
|
||||
void test(boolean a, boolean b, int *p, volatile int *q, int i) {
|
||||
b = a & b;
|
||||
@ -44,9 +46,12 @@ void test(boolean a, boolean b, int *p, volatile int *q, int i) {
|
||||
b = b & foo();
|
||||
b = bar() & (i > 4);
|
||||
b = (i == 7) & foo();
|
||||
b = b MY_AND foo(); // OK, no warning expected
|
||||
|
||||
#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
|
||||
b = foo() My_BITAND bar(); // Ok, no warning expected
|
||||
|
||||
#endif
|
||||
|
||||
if (foo() & bar()) // expected-warning {{use of bitwise '&' with boolean operands}}
|
||||
@ -60,4 +65,5 @@ void test(boolean a, boolean b, int *p, volatile int *q, int i) {
|
||||
|
||||
int n = i + 10;
|
||||
b = (n & (n - 1));
|
||||
|
||||
}
|
||||
|
@ -19,6 +19,8 @@ boolean baz(void) __attribute__((const));
|
||||
void sink(boolean);
|
||||
|
||||
#define FOO foo()
|
||||
#define MY_OR |
|
||||
#define My_BITOR bitor
|
||||
|
||||
void test(boolean a, boolean b, int *p, volatile int *q, int i) {
|
||||
b = a | b;
|
||||
@ -44,9 +46,11 @@ void test(boolean a, boolean b, int *p, volatile int *q, int i) {
|
||||
b = b | foo();
|
||||
b = bar() | (i > 4);
|
||||
b = (i == 7) | foo();
|
||||
b = b MY_OR foo(); // OK, no warning expected
|
||||
#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
|
||||
b = foo() My_BITOR bar(); // Ok, no warning expected
|
||||
|
||||
#endif
|
||||
|
||||
if (foo() | bar()) // expected-warning {{use of bitwise '|' with boolean operands}}
|
||||
|
Loading…
x
Reference in New Issue
Block a user