[clang] fix unnecessary warning when using bitand with boolean operators (PR #81976)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 15 09:05:29 PDT 2024


================
@@ -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)
----------------
AaronBallman wrote:

```suggestion

      // 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)
```
You may need to adjust for 80-col limits.

https://github.com/llvm/llvm-project/pull/81976


More information about the cfe-commits mailing list