Debian Codegraph @

src/checks/manuallevel/isempty-vs-count.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2015 Sergio Martins <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "isempty-vs-count.h"
8 #include "HierarchyUtils.h"
9 #include "QtUtils.h"
10 #include "StringUtils.h"
11 #include "clang/AST/ParentMap.h"
12
13 #include <clang/AST/DeclCXX.h>
14 #include <clang/AST/Expr.h>
15 #include <clang/AST/ExprCXX.h>
16 #include <clang/AST/OperationKinds.h>
17 #include <clang/AST/Stmt.h>
18 #include <clang/AST/StmtIterator.h>
19 #include <clang/Basic/LLVM.h>
20 #include <llvm/ADT/StringRef.h>
21 #include <llvm/Support/Casting.h>
22
23 using namespace clang;
24
25 void IsEmptyVSCount::VisitStmt(clang::Stmt *stmt)
26 {
27 if (auto op = dyn_cast<ExprWithCleanups>(stmt)) {
28 if (auto binaryOpt = dyn_cast<BinaryOperator>(op->getSubExpr())) {
29 const bool isGreater = binaryOpt->getOpcode() == BinaryOperator::Opcode::BO_GT;
30 const bool isSmaller = binaryOpt->getOpcode() == BinaryOperator::Opcode::BO_LT;
31 const bool isEqual = binaryOpt->getOpcode() == BinaryOperator::Opcode::BO_EQ;
32 if (!isGreater && !isSmaller && !isEqual) {
33 return;
34 }
35 auto *method = dyn_cast<CXXMemberCallExpr>(binaryOpt->getLHS());
36 if (!method || !clazy::functionIsOneOf(method->getMethodDecl(), {"size", "count", "length"})) {
37 return;
38 }
39 if (!clazy::classIsOneOf(method->getRecordDecl(), {"QList", "QHash", "QMap", "QMultiHash", "QMultiMap"}) || method->getNumArgs() != 0) {
40 return;
41 }
42
43 // In qt5, it there is no implicit cast inbetween
44 auto intliteral = isa<ImplicitCastExpr>(binaryOpt->getRHS())
45 ? dyn_cast<IntegerLiteral>(dyn_cast<ImplicitCastExpr>(binaryOpt->getRHS())->getSubExpr())
46 : dyn_cast<IntegerLiteral>(binaryOpt->getRHS());
47
48 if (!intliteral) {
49 return;
50 }
51 auto *baseExpr = dyn_cast<MemberExpr>(method->getCallee())->getBase()->IgnoreParenImpCasts();
52 StringRef baseText = Lexer::getSourceText(CharSourceRange::getTokenRange(baseExpr->getSourceRange()), sm(), lo());
53
54 if ((isEqual && intliteral->getValue().isZero()) || (isSmaller && intliteral->getValue().isOne())) {
55 emitWarning(stmt->getBeginLoc(),
56 "use isEmpty() instead",
57 {FixItHint::CreateReplacement(binaryOpt->getSourceRange(), baseText.str() + ".isEmpty()")});
58 }
59
60 if (isGreater && intliteral->getValue().isZero()) {
61 emitWarning(stmt->getBeginLoc(),
62 "use isEmpty() instead",
63 {FixItHint::CreateReplacement(binaryOpt->getSourceRange(), "!" + baseText.str() + ".isEmpty()")});
64 }
65 }
66 }
67 auto *cast = dyn_cast<ImplicitCastExpr>(stmt);
68 if (!cast || cast->getCastKind() != clang::CK_IntegralToBoolean) {
69 return;
70 }
71
72 auto *memberCall = dyn_cast<CXXMemberCallExpr>(*(cast->child_begin()));
73 CXXMethodDecl *method = memberCall ? memberCall->getMethodDecl() : nullptr;
74
75 if (!clazy::functionIsOneOf(method, {"size", "count", "length"})) {
76 return;
77 }
78
79 if (!clazy::classIsOneOf(method->getParent(), clazy::qtContainers())) {
80 return;
81 }
82 auto *memberExpr = dyn_cast<MemberExpr>(memberCall->getCallee());
83 if (!memberExpr) {
84 return;
85 }
86
87 auto *baseExpr = memberExpr->getBase()->IgnoreParenImpCasts();
88 if (clazy::classIsOneOf(method->getParent(), {"QMultiHash", "QMultiMap"}) && memberCall->getNumArgs() == 2) {
89 StringRef baseText = Lexer::getSourceText(CharSourceRange::getTokenRange(baseExpr->getSourceRange()), sm(), lo());
90 StringRef argText = Lexer::getSourceText(CharSourceRange::getTokenRange(memberCall->getArg(0)->getSourceRange()), sm(), lo());
91 const auto fixit = FixItHint::CreateReplacement(stmt->getSourceRange(), (baseText + ".contains(" + argText + ")").str());
92
93 emitWarning(stmt->getBeginLoc(), "use contains() instead", {fixit});
94 return;
95 }
96
97 if (clazy::classIsOneOf(method->getParent(), {"QHash", "QMap", "QMultiHash", "QMultiMap"}) && memberCall->getNumArgs() == 1) {
98 emitWarning(stmt->getBeginLoc(), "use contains() instead", {FixItHint::CreateReplacement(memberExpr->getMemberLoc(), "contains")});
99 return;
100 }
101
102 SourceRange fixitRange = memberCall->getSourceRange();
103 std::string operatorPrefix = "!";
104 auto *parent = m_context->parentMap->getParent(stmt);
105 if (auto unaryOp = dyn_cast<UnaryOperator>(parent)) {
106 if (unaryOp->getOpcode() == UnaryOperator::Opcode::UO_LNot) {
107 operatorPrefix = "";
108 fixitRange = unaryOp->getSourceRange();
109 }
110 }
111
112 StringRef BaseText = Lexer::getSourceText(CharSourceRange::getTokenRange(baseExpr->getSourceRange()), sm(), lo());
113 const auto fixit = FixItHint::CreateReplacement(fixitRange, (operatorPrefix + BaseText + ".isEmpty()").str());
114
115 emitWarning(stmt->getBeginLoc(), "use isEmpty() instead", {fixit});
116 }