| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2015 Albert Astals Cid <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "qdeleteall.h" |
| 8 |
#include "ClazyContext.h" |
| 9 |
#include "HierarchyUtils.h" |
| 10 |
#include "QtUtils.h" |
| 11 |
#include "StringUtils.h" |
| 12 |
|
| 13 |
#include <clang/AST/Decl.h> |
| 14 |
#include <clang/AST/DeclCXX.h> |
| 15 |
#include <clang/AST/Expr.h> |
| 16 |
#include <clang/AST/ExprCXX.h> |
| 17 |
#include <clang/AST/Stmt.h> |
| 18 |
#include <clang/Basic/LLVM.h> |
| 19 |
#include <llvm/ADT/StringRef.h> |
| 20 |
#include <llvm/Support/Casting.h> |
| 21 |
|
| 22 |
using namespace clang; |
| 23 |
|
| 24 |
void QDeleteAll::VisitStmt(clang::Stmt *stmt) |
| 25 |
{ |
| 26 |
// Find a call to QMap/QSet/QHash::values/keys |
| 27 |
auto *offendingCall = dyn_cast<CXXMemberCallExpr>(stmt); |
| 28 |
FunctionDecl *func = offendingCall ? offendingCall->getDirectCallee() : nullptr; |
| 29 |
if (!func) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
const std::string funcName = func->getNameAsString(); |
| 34 |
const bool isValues = funcName == "values"; |
| 35 |
if (!isValues && funcName != "keys") { |
| 36 |
return; |
| 37 |
} |
| 38 |
std::string offendingClassName; |
| 39 |
// In case QMultiHash::values is used in Qt5, values is defined in the QHash baseclass, look up the original record to be sure we have a QMultiHash |
| 40 |
if (auto *cast = dyn_cast<ImplicitCastExpr>(offendingCall->getImplicitObjectArgument())) { |
| 41 |
if (auto *subExpr = dyn_cast<DeclRefExpr>(cast->getSubExpr())) { |
| 42 |
if (auto *ptr = subExpr->getType().getTypePtrOrNull(); ptr && ptr->isRecordType()) { |
| 43 |
offendingClassName = ptr->getAsRecordDecl()->getNameAsString(); |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
// Check if we have whitelisted the classname |
| 48 |
if (offendingClassName.empty() || !clazy::isQtAssociativeContainer(offendingClassName)) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
// Once found see if the first parent call is qDeleteAll |
| 53 |
int i = 1; |
| 54 |
Stmt *p = clazy::parent(m_context->parentMap, stmt, i); |
| 55 |
while (p) { |
| 56 |
auto *pc = dyn_cast<CallExpr>(p); |
| 57 |
if (const FunctionDecl *f = pc ? pc->getDirectCallee() : nullptr) { |
| 58 |
if ((clazy::name(f) == "qDeleteAll") |
| 59 |
&& (func->getNumParams() == 0)) { // Ignore values method calls where lookup-parameter is given, like the deprecated QHash::values(Key) |
| 60 |
std::string msg = "qDeleteAll() is being used on an unnecessary temporary container created by " + offendingClassName + "::" + funcName + "()"; |
| 61 |
if (isValues) { |
| 62 |
msg += ", use qDeleteAll(mycontainer) instead"; |
| 63 |
} else { |
| 64 |
msg += ", use qDeleteAll(mycontainer.keyBegin(), mycontainer.keyEnd()) instead"; |
| 65 |
} |
| 66 |
emitWarning(p->getBeginLoc(), msg); |
| 67 |
} |
| 68 |
break; |
| 69 |
} |
| 70 |
++i; |
| 71 |
p = clazy::parent(m_context->parentMap, stmt, i); |
| 72 |
} |
| 73 |
} |