| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2017 Sergio Martins <[email protected]> |
| 3 |
SPDX-FileCopyrightText: 2024 Alexander Lohnau <[email protected]> |
| 4 |
|
| 5 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "connect-3arg-lambda.h" |
| 9 |
#include "HierarchyUtils.h" |
| 10 |
#include "QtUtils.h" |
| 11 |
#include "TypeUtils.h" |
| 12 |
|
| 13 |
#include <clang/AST/Decl.h> |
| 14 |
#include <clang/AST/Expr.h> |
| 15 |
#include <clang/AST/ExprCXX.h> |
| 16 |
#include <clang/AST/Stmt.h> |
| 17 |
#include <clang/Basic/LLVM.h> |
| 18 |
#include <llvm/Support/Casting.h> |
| 19 |
|
| 20 |
#include <vector> |
| 21 |
|
| 22 |
using namespace clang; |
| 23 |
|
| 24 |
using uint = unsigned; |
| 25 |
|
| 26 |
void Connect3ArgLambda::VisitStmt(clang::Stmt *stmt) |
| 27 |
{ |
| 28 |
auto *callExpr = dyn_cast<CallExpr>(stmt); |
| 29 |
if (!callExpr) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
FunctionDecl *fdecl = callExpr->getDirectCallee(); |
| 34 |
if (!fdecl) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
const uint numParams = fdecl->getNumParams(); |
| 39 |
if (numParams != 2 && numParams != 3) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
const std::string qualifiedName = trimQtNamespace(fdecl->getQualifiedNameAsString()); |
| 44 |
if (qualifiedName == "QTimer::singleShot") { |
| 45 |
processQTimer(fdecl, stmt); |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
if (qualifiedName == "QMenu::addAction") { |
| 50 |
processQMenu(fdecl, stmt); |
| 51 |
return; |
| 52 |
} |
| 53 |
if (qualifiedName == "QWidget::addAction") { |
| 54 |
processWidget(fdecl, stmt); |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
if (numParams != 3 || !clazy::isConnect(fdecl, m_context->qtNamespace())) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
auto *arg3 = callExpr->getArg(2); |
| 63 |
auto *lambda = clang::dyn_cast_or_null<LambdaExpr>(arg3); |
| 64 |
if (!lambda) { |
| 65 |
lambda = clazy::getFirstChildOfType2<LambdaExpr>(arg3); |
| 66 |
if (!lambda) { |
| 67 |
return; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
DeclRefExpr *senderDeclRef = nullptr; |
| 72 |
|
| 73 |
Stmt *s = callExpr->getArg(0); |
| 74 |
while (s) { |
| 75 |
if ((senderDeclRef = dyn_cast<DeclRefExpr>(s))) { |
| 76 |
break; |
| 77 |
} |
| 78 |
|
| 79 |
if (isa<MemberExpr>(s)) { |
| 80 |
break; |
| 81 |
} |
| 82 |
|
| 83 |
s = clazy::getFirstChild(s); |
| 84 |
} |
| 85 |
|
| 86 |
// The sender can be: this |
| 87 |
const auto *senderThis = clazy::unpeal<CXXThisExpr>(callExpr->getArg(0), clazy::IgnoreImplicitCasts); |
| 88 |
|
| 89 |
// The variables used inside the lambda |
| 90 |
auto declrefs = clazy::getStatements<DeclRefExpr>(lambda->getBody()); |
| 91 |
|
| 92 |
const ValueDecl *senderDecl = senderDeclRef ? senderDeclRef->getDecl() : nullptr; |
| 93 |
|
| 94 |
// We'll only warn if the lambda is dereferencing another QObject (besides the sender) |
| 95 |
bool found = false; |
| 96 |
for (auto *declref : declrefs) { |
| 97 |
ValueDecl *decl = declref->getDecl(); |
| 98 |
if (decl == senderDecl) { |
| 99 |
continue; // It's the sender, continue. |
| 100 |
} |
| 101 |
|
| 102 |
if (clazy::isQObject((clazy::pointeeQualType(decl->getType()))->getAsCXXRecordDecl(), m_context->qtNamespace())) { |
| 103 |
found = true; |
| 104 |
break; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if (!found) { |
| 109 |
auto thisexprs = clazy::getStatements<CXXThisExpr>(lambda->getBody()); |
| 110 |
if (!thisexprs.empty() && !senderThis) { |
| 111 |
found = true; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
if (found) { |
| 116 |
emitWarning(stmt, "Pass a context object as 3rd connect parameter"); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
void Connect3ArgLambda::processQTimer(FunctionDecl *func, Stmt *stmt) |
| 121 |
{ |
| 122 |
// Signatures to catch: |
| 123 |
// QTimer::singleShot(int msec, Functor functor) |
| 124 |
// QTimer::singleShot(int msec, Qt::TimerType timerType, Functor functor) |
| 125 |
|
| 126 |
const uint numParams = func->getNumParams(); |
| 127 |
if (numParams == 2) { |
| 128 |
if (func->getParamDecl(0)->getNameAsString() == "interval" && func->getParamDecl(1)->getNameAsString() == "slot") { |
| 129 |
emitWarning(stmt, "Pass a context object as 2nd singleShot parameter"); |
| 130 |
} |
| 131 |
} else if (numParams == 3) { |
| 132 |
if (func->getParamDecl(0)->getNameAsString() == "interval" && func->getParamDecl(1)->getNameAsString() == "timerType" |
| 133 |
&& func->getParamDecl(2)->getNameAsString() == "slot") { |
| 134 |
emitWarning(stmt, "Pass a context object as 3rd singleShot parameter"); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
void Connect3ArgLambda::processQMenu(FunctionDecl *func, Stmt *stmt) |
| 140 |
{ |
| 141 |
// Signatures to catch: |
| 142 |
// QMenu::addAction(const QString &text, Func1 slot, const QKeySequence &shortcut = 0) |
| 143 |
const uint numParams = func->getNumParams(); |
| 144 |
if (numParams == 3) { |
| 145 |
if (func->getParamDecl(0)->getNameAsString() == "text" && func->getParamDecl(1)->getNameAsString() == "slot" |
| 146 |
&& func->getParamDecl(2)->getNameAsString() == "shortcut") { |
| 147 |
emitWarning(stmt, "Pass a context object as 2nd addAction parameter"); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
void Connect3ArgLambda::processWidget(clang::FunctionDecl *func, clang::Stmt *stmt) |
| 153 |
{ |
| 154 |
if (const uint numParams = func->getNumParams(); numParams > 1) { |
| 155 |
auto *possiblyFwdArgToConnect = func->getParamDecl(numParams - 2); |
| 156 |
auto *fwdArgToConnect = func->getParamDecl(numParams - 1); |
| 157 |
if (possiblyFwdArgToConnect->getNameAsString() != "args" && fwdArgToConnect->getNameAsString() == "args") { |
| 158 |
emitWarning(stmt, "Pass a context object as 2nd addAction parameter"); |
| 159 |
} |
| 160 |
} |
| 161 |
} |