[clang-tools-extra] [clang-tidy] Address false positives in misc-redundant-expression checker (PR #121960)
Piotr Zegar via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 11 00:55:21 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;
----------------
PiotrZSL wrote:
what with
```
#define getX(x) x
int var = getX(1) > getX( 1);
```
In such case string compare won't work.
https://github.com/llvm/llvm-project/pull/121960
More information about the cfe-commits
mailing list