[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:30 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);
----------------
AaronBallman wrote:

```suggestion
      SourceManager &SM = S.getSourceManager();
      const LangOptions &LO = S.getLangOpts();
      SourceLocation BLoc = BO->getOperatorLoc();
      SourceLocation ELoc =
          Lexer::getLocForEndOfToken(BLoc, 0, SM, LO);
      StringRef SR = Lexer::getSourceText(
          CharSourceRange::getTokenRange(BLoc, ELoc), SM, LO);
```
You shouldn't need to fully qualify any of these from here. Also, no need to copy the `LangOptions` object.

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


More information about the cfe-commits mailing list