| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> |
| 3 |
SPDX-FileContributor: Shivam Kunwar <[email protected]> |
| 4 |
|
| 5 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "compare-member-check.h" |
| 9 |
#include <clang/AST/AST.h> |
| 10 |
#include <clang/AST/ExprCXX.h> |
| 11 |
#include <clang/AST/ParentMap.h> |
| 12 |
#include <clang/ASTMatchers/ASTMatchFinder.h> |
| 13 |
#include <clang/ASTMatchers/ASTMatchers.h> |
| 14 |
#include <clang/ASTMatchers/ASTMatchersInternal.h> |
| 15 |
|
| 16 |
using namespace clang::ast_matchers; |
| 17 |
class ClazyContext; |
| 18 |
|
| 19 |
using namespace clang; |
| 20 |
|
| 21 |
void checkBinaryOperator(const BinaryOperator *binOp, const CXXRecordDecl *record, std::vector<const FieldDecl *> &classFields) |
| 22 |
{ |
| 23 |
if (const BinaryOperator *leftBO = dyn_cast<BinaryOperator>(binOp->getLHS())) { |
| 24 |
checkBinaryOperator(leftBO, record, classFields); |
| 25 |
} |
| 26 |
|
| 27 |
if (const BinaryOperator *rightBO = dyn_cast<BinaryOperator>(binOp->getRHS())) { |
| 28 |
checkBinaryOperator(rightBO, record, classFields); |
| 29 |
} |
| 30 |
|
| 31 |
const MemberExpr *lhsMemberExpr = nullptr; |
| 32 |
if (const ImplicitCastExpr *implicitCast = dyn_cast<ImplicitCastExpr>(binOp->getLHS())) { |
| 33 |
lhsMemberExpr = dyn_cast<MemberExpr>(implicitCast->getSubExpr()); |
| 34 |
} else if (const MemberExpr *temp = dyn_cast<MemberExpr>(binOp->getLHS())) { |
| 35 |
lhsMemberExpr = temp; |
| 36 |
} |
| 37 |
|
| 38 |
if (lhsMemberExpr) { |
| 39 |
FieldDecl *fieldDecl = dyn_cast<FieldDecl>(lhsMemberExpr->getMemberDecl()); |
| 40 |
if (fieldDecl && fieldDecl->getParent() == record) { |
| 41 |
auto it = std::find(classFields.begin(), classFields.end(), fieldDecl); |
| 42 |
|
| 43 |
if (it != classFields.end()) { |
| 44 |
classFields.erase(it); |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
class MemberCaller : public ClazyAstMatcherCallback |
| 51 |
{ |
| 52 |
public: |
| 53 |
MemberCaller(CheckBase *check) |
| 54 |
: ClazyAstMatcherCallback(check) |
| 55 |
{ |
| 56 |
} |
| 57 |
|
| 58 |
virtual void run(const MatchFinder::MatchResult &result) |
| 59 |
{ |
| 60 |
if (const auto *callExpr = result.Nodes.getNodeAs<CXXOperatorCallExpr>("callExpr")) { |
| 61 |
const Decl *calleeDecl = callExpr->getCalleeDecl(); |
| 62 |
if (!calleeDecl) |
| 63 |
return; |
| 64 |
|
| 65 |
const auto *methodDecl = dyn_cast<CXXMethodDecl>(calleeDecl); |
| 66 |
if (!methodDecl) |
| 67 |
return; |
| 68 |
|
| 69 |
// Get the class declaration |
| 70 |
const CXXRecordDecl *classDecl = methodDecl->getParent(); |
| 71 |
|
| 72 |
std::vector<const FieldDecl *> classFields; |
| 73 |
for (const auto *field : classDecl->fields()) { |
| 74 |
classFields.push_back(field); |
| 75 |
} |
| 76 |
for (const auto *stmt : methodDecl->getBody()->children()) { |
| 77 |
const auto *returnStmt = dyn_cast<ReturnStmt>(stmt); |
| 78 |
if (!returnStmt) |
| 79 |
return; |
| 80 |
const Expr *returnExpr = returnStmt->getRetValue(); |
| 81 |
if (const auto *exprWithCleanups = dyn_cast<ExprWithCleanups>(returnExpr)) { |
| 82 |
if (auto opCallExpr = dyn_cast<CXXOperatorCallExpr>(exprWithCleanups->getSubExpr())) { |
| 83 |
const Expr *lhs = opCallExpr->getArg(0); |
| 84 |
if (auto *materializeExpr = dyn_cast<MaterializeTemporaryExpr>(lhs)) { |
| 85 |
if (auto *implicitCastExpr = dyn_cast<ImplicitCastExpr>(materializeExpr->getSubExpr())) { |
| 86 |
if (auto *lhsCall = dyn_cast<CallExpr>(implicitCastExpr->getSubExpr())) { |
| 87 |
const auto &lhsArgs = lhsCall->arguments(); |
| 88 |
size_t numLhsArgs = std::distance(lhsArgs.begin(), lhsArgs.end()); |
| 89 |
|
| 90 |
if (classFields.size() != numLhsArgs) { |
| 91 |
m_check->emitWarning(callExpr->getExprLoc(), "Comparison operator does not use all member variables"); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
if (const auto *binOp = dyn_cast<BinaryOperator>(returnExpr)) { |
| 100 |
checkBinaryOperator(binOp, classDecl, classFields); |
| 101 |
if (!classFields.empty()) { |
| 102 |
m_check->emitWarning(callExpr->getExprLoc(), "Comparison operator does not use all member variables"); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
}; |
| 109 |
|
| 110 |
CompareMemberCheck::~CompareMemberCheck() = default; |
| 111 |
|
| 112 |
void CompareMemberCheck::registerASTMatchers(MatchFinder &finder) |
| 113 |
{ |
| 114 |
m_astMatcherCallBack = std::make_unique<MemberCaller>(this); |
| 115 |
finder.addMatcher(cxxOperatorCallExpr().bind("callExpr"), m_astMatcherCallBack.get()); |
| 116 |
} |