/* SPDX-FileCopyrightText: 2016 Sergio Martins SPDX-License-Identifier: LGPL-2.0-or-later */ #include "container-anti-pattern.h" #include "HierarchyUtils.h" #include "LoopUtils.h" #include "StringUtils.h" #include "Utils.h" #include "clazy_stl.h" #include #include #include #include #include #include #include #include using namespace clang; static bool isInterestingCall(CallExpr *call) { FunctionDecl *func = call ? call->getDirectCallee() : nullptr; if (!func) { return false; } static const std::vector methods = { "QVector::toList", "QList::toVector", "QMap::values", "QMap::keys", "QSet::toList", "QSet::values", "QHash::values", "QHash::keys", "QList::toList", // In case one has code compiling against Qt6, but Qt5 compat }; return clazy::contains(methods, clazy::qualifiedMethodName(func)); } void ContainerAntiPattern::VisitStmt(clang::Stmt *stmt) { if (handleLoop(stmt)) { // catch for (auto i : map.values()) and such return; } if (VisitQSet(stmt)) { return; } std::vector calls = Utils::callListForChain(dyn_cast(stmt)); if (calls.size() < 2) { return; } // For an expression like set.toList().count()... CallExpr *callexpr1 = calls[calls.size() - 1]; // ...this would be toList() // CallExpr *callexpr2 = calls[calls.size() - 2]; // ...and this would be count() if (!isInterestingCall(callexpr1)) { return; } emitWarning(stmt->getBeginLoc(), "allocating an unneeded temporary container"); } bool ContainerAntiPattern::VisitQSet(Stmt *stmt) { auto *secondCall = dyn_cast(stmt); if (!secondCall || !secondCall->getMethodDecl()) { return false; } CXXMethodDecl *secondMethod = secondCall->getMethodDecl(); const std::string secondMethodName = clazy::qualifiedMethodName(secondMethod); if (secondMethodName != "QSet::isEmpty") { return false; } std::vector chainedCalls = Utils::callListForChain(secondCall); if (chainedCalls.size() < 2) { return false; } CallExpr *firstCall = chainedCalls[chainedCalls.size() - 1]; FunctionDecl *firstFunc = firstCall->getDirectCallee(); if (!firstFunc) { return false; } auto *firstMethod = dyn_cast(firstFunc); if (!firstMethod || clazy::qualifiedMethodName(firstMethod) != "QSet::intersect") { return false; } emitWarning(stmt->getBeginLoc(), "Use QSet::intersects() instead"); return true; } bool ContainerAntiPattern::handleLoop(Stmt *stm) { Expr *containerExpr = clazy::containerExprForLoop(stm, m_context->qtNamespace()); if (!containerExpr) { return false; } auto *memberExpr = clazy::getFirstChildOfType2(containerExpr); if (isInterestingCall(memberExpr)) { emitWarning(stm->getBeginLoc(), "allocating an unneeded temporary container"); return true; } return false; }