| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "container-anti-pattern.h" |
| 8 |
#include "HierarchyUtils.h" |
| 9 |
#include "LoopUtils.h" |
| 10 |
#include "StringUtils.h" |
| 11 |
#include "Utils.h" |
| 12 |
#include "clazy_stl.h" |
| 13 |
|
| 14 |
#include <clang/AST/Decl.h> |
| 15 |
#include <clang/AST/DeclCXX.h> |
| 16 |
#include <clang/AST/Expr.h> |
| 17 |
#include <clang/AST/ExprCXX.h> |
| 18 |
#include <clang/AST/Stmt.h> |
| 19 |
#include <clang/Basic/LLVM.h> |
| 20 |
#include <llvm/Support/Casting.h> |
| 21 |
|
| 22 |
#include <vector> |
| 23 |
|
| 24 |
using namespace clang; |
| 25 |
|
| 26 |
static bool isInterestingCall(CallExpr *call) |
| 27 |
{ |
| 28 |
FunctionDecl *func = call ? call->getDirectCallee() : nullptr; |
| 29 |
if (!func) { |
| 30 |
return false; |
| 31 |
} |
| 32 |
|
| 33 |
static const std::vector<std::string> methods = { |
| 34 |
"QVector::toList", |
| 35 |
"QList::toVector", |
| 36 |
"QMap::values", |
| 37 |
"QMap::keys", |
| 38 |
"QSet::toList", |
| 39 |
"QSet::values", |
| 40 |
"QHash::values", |
| 41 |
"QHash::keys", |
| 42 |
"QList::toList", // In case one has code compiling against Qt6, but Qt5 compat |
| 43 |
}; |
| 44 |
|
| 45 |
return clazy::contains(methods, clazy::qualifiedMethodName(func)); |
| 46 |
} |
| 47 |
|
| 48 |
void ContainerAntiPattern::VisitStmt(clang::Stmt *stmt) |
| 49 |
{ |
| 50 |
if (handleLoop(stmt)) { // catch for (auto i : map.values()) and such |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
if (VisitQSet(stmt)) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
std::vector<CallExpr *> calls = Utils::callListForChain(dyn_cast<CallExpr>(stmt)); |
| 59 |
if (calls.size() < 2) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
// For an expression like set.toList().count()... |
| 64 |
CallExpr *callexpr1 = calls[calls.size() - 1]; // ...this would be toList() |
| 65 |
// CallExpr *callexpr2 = calls[calls.size() - 2]; // ...and this would be count() |
| 66 |
|
| 67 |
if (!isInterestingCall(callexpr1)) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
emitWarning(stmt->getBeginLoc(), "allocating an unneeded temporary container"); |
| 72 |
} |
| 73 |
|
| 74 |
bool ContainerAntiPattern::VisitQSet(Stmt *stmt) |
| 75 |
{ |
| 76 |
auto *secondCall = dyn_cast<CXXMemberCallExpr>(stmt); |
| 77 |
if (!secondCall || !secondCall->getMethodDecl()) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
CXXMethodDecl *secondMethod = secondCall->getMethodDecl(); |
| 82 |
const std::string secondMethodName = clazy::qualifiedMethodName(secondMethod); |
| 83 |
if (secondMethodName != "QSet::isEmpty") { |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
std::vector<CallExpr *> chainedCalls = Utils::callListForChain(secondCall); |
| 88 |
if (chainedCalls.size() < 2) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
CallExpr *firstCall = chainedCalls[chainedCalls.size() - 1]; |
| 93 |
FunctionDecl *firstFunc = firstCall->getDirectCallee(); |
| 94 |
if (!firstFunc) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
auto *firstMethod = dyn_cast<CXXMethodDecl>(firstFunc); |
| 99 |
if (!firstMethod || clazy::qualifiedMethodName(firstMethod) != "QSet::intersect") { |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
emitWarning(stmt->getBeginLoc(), "Use QSet::intersects() instead"); |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
bool ContainerAntiPattern::handleLoop(Stmt *stm) |
| 108 |
{ |
| 109 |
Expr *containerExpr = clazy::containerExprForLoop(stm, m_context->qtNamespace()); |
| 110 |
if (!containerExpr) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
auto *memberExpr = clazy::getFirstChildOfType2<CXXMemberCallExpr>(containerExpr); |
| 115 |
if (isInterestingCall(memberExpr)) { |
| 116 |
emitWarning(stm->getBeginLoc(), "allocating an unneeded temporary container"); |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
return false; |
| 121 |
} |