| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2017 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "install-event-filter.h" |
| 8 |
#include "HierarchyUtils.h" |
| 9 |
#include "TypeUtils.h" |
| 10 |
#include "Utils.h" |
| 11 |
|
| 12 |
#include <clang/AST/Decl.h> |
| 13 |
#include <clang/AST/DeclCXX.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 |
using namespace clang; |
| 21 |
|
| 22 |
void InstallEventFilter::VisitStmt(clang::Stmt *stmt) |
| 23 |
{ |
| 24 |
auto *memberCallExpr = dyn_cast<CXXMemberCallExpr>(stmt); |
| 25 |
if (!memberCallExpr || memberCallExpr->getNumArgs() != 1) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
FunctionDecl *func = memberCallExpr->getDirectCallee(); |
| 30 |
if (!func || func->getQualifiedNameAsString() != qtNamespaced("QObject::installEventFilter")) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
Expr *expr = memberCallExpr->getImplicitObjectArgument(); |
| 35 |
if (!expr) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
if (Stmt *firstChild = clazy::getFirstChildAtDepth(expr, 1); !firstChild || !isa<CXXThisExpr>(firstChild)) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
Expr *arg1 = memberCallExpr->getArg(0); |
| 44 |
arg1 = arg1 ? arg1->IgnoreCasts() : nullptr; |
| 45 |
|
| 46 |
const CXXRecordDecl *record = clazy::typeAsRecord(arg1); |
| 47 |
auto methods = Utils::methodsFromString(record, "eventFilter"); |
| 48 |
|
| 49 |
for (auto *method : methods) { |
| 50 |
if (method->getQualifiedNameAsString() != qtNamespaced("QObject::eventFilter")) { // It overrides it, probably on purpose then, don't warn. |
| 51 |
return; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
emitWarning(stmt, "'this' should usually be the filter object, not the monitored one."); |
| 56 |
} |