[clang-tools-extra] [clang-tidy] Address false positives in misc-redundant-expression checker (PR #121960)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 13 07:19:49 PST 2025


================
@@ -852,6 +877,58 @@ static bool areExprsMacroAndNonMacro(const Expr *&LhsExpr,
 
   return LhsLoc.isMacroID() != RhsLoc.isMacroID();
 }
+
+static bool areExprsSameMacroOrLiteral(const BinaryOperator *BinOp,
+                                       const ASTContext *Context) {
+
+  if (!BinOp)
+    return false;
+
+  const auto *Lhs = BinOp->getLHS();
+  const auto *Rhs = BinOp->getRHS();
+  const SourceManager &SM = Context->getSourceManager();
+
+  SourceRange Lsr = Lhs->getSourceRange();
+  SourceRange Rsr = Rhs->getSourceRange();
+  if (Lsr.getBegin().isMacroID()) {
+    // Left is macro so right macro too
+    if (Rsr.getBegin().isMacroID()) {
+      // Both sides are macros so they are same macro or literal
+      llvm::StringRef L = Lexer::getSourceText(
+          CharSourceRange::getTokenRange(Lsr), SM, LangOptions(), 0);
+      llvm::StringRef R = Lexer::getSourceText(
+          CharSourceRange::getTokenRange(Rsr), SM, LangOptions(), 0);
+      return L.compare(R) == 0;
----------------
earnol wrote:

I agree, but in this case the both sides are not literally the same.
I propose to make it space for improvement.

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


More information about the cfe-commits mailing list