/* SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company SPDX-FileContributor: Shivam Kunwar SPDX-License-Identifier: LGPL-2.0-or-later */ #include "compare-member-check.h" #include #include #include #include #include #include using namespace clang::ast_matchers; class ClazyContext; using namespace clang; void checkBinaryOperator(const BinaryOperator *binOp, const CXXRecordDecl *record, std::vector &classFields) { if (const BinaryOperator *leftBO = dyn_cast(binOp->getLHS())) { checkBinaryOperator(leftBO, record, classFields); } if (const BinaryOperator *rightBO = dyn_cast(binOp->getRHS())) { checkBinaryOperator(rightBO, record, classFields); } const MemberExpr *lhsMemberExpr = nullptr; if (const ImplicitCastExpr *implicitCast = dyn_cast(binOp->getLHS())) { lhsMemberExpr = dyn_cast(implicitCast->getSubExpr()); } else if (const MemberExpr *temp = dyn_cast(binOp->getLHS())) { lhsMemberExpr = temp; } if (lhsMemberExpr) { FieldDecl *fieldDecl = dyn_cast(lhsMemberExpr->getMemberDecl()); if (fieldDecl && fieldDecl->getParent() == record) { auto it = std::find(classFields.begin(), classFields.end(), fieldDecl); if (it != classFields.end()) { classFields.erase(it); } } } } class MemberCaller : public ClazyAstMatcherCallback { public: MemberCaller(CheckBase *check) : ClazyAstMatcherCallback(check) { } virtual void run(const MatchFinder::MatchResult &result) { if (const auto *callExpr = result.Nodes.getNodeAs("callExpr")) { const Decl *calleeDecl = callExpr->getCalleeDecl(); if (!calleeDecl) return; const auto *methodDecl = dyn_cast(calleeDecl); if (!methodDecl) return; // Get the class declaration const CXXRecordDecl *classDecl = methodDecl->getParent(); std::vector classFields; for (const auto *field : classDecl->fields()) { classFields.push_back(field); } for (const auto *stmt : methodDecl->getBody()->children()) { const auto *returnStmt = dyn_cast(stmt); if (!returnStmt) return; const Expr *returnExpr = returnStmt->getRetValue(); if (const auto *exprWithCleanups = dyn_cast(returnExpr)) { if (auto opCallExpr = dyn_cast(exprWithCleanups->getSubExpr())) { const Expr *lhs = opCallExpr->getArg(0); if (auto *materializeExpr = dyn_cast(lhs)) { if (auto *implicitCastExpr = dyn_cast(materializeExpr->getSubExpr())) { if (auto *lhsCall = dyn_cast(implicitCastExpr->getSubExpr())) { const auto &lhsArgs = lhsCall->arguments(); size_t numLhsArgs = std::distance(lhsArgs.begin(), lhsArgs.end()); if (classFields.size() != numLhsArgs) { m_check->emitWarning(callExpr->getExprLoc(), "Comparison operator does not use all member variables"); } } } } } } if (const auto *binOp = dyn_cast(returnExpr)) { checkBinaryOperator(binOp, classDecl, classFields); if (!classFields.empty()) { m_check->emitWarning(callExpr->getExprLoc(), "Comparison operator does not use all member variables"); } } } } } }; CompareMemberCheck::~CompareMemberCheck() = default; void CompareMemberCheck::registerASTMatchers(MatchFinder &finder) { m_astMatcherCallBack = std::make_unique(this); finder.addMatcher(cxxOperatorCallExpr().bind("callExpr"), m_astMatcherCallBack.get()); }