Debian Codegraph @

src/checks/level1/child-event-qobject-cast.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "child-event-qobject-cast.h"
8 #include "HierarchyUtils.h"
9 #include "QtUtils.h"
10 #include "StringUtils.h"
11 #include "clazy_stl.h"
12
13 #include <clang/AST/Decl.h>
14 #include <clang/AST/DeclBase.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/ADT/StringRef.h>
21 #include <llvm/Support/Casting.h>
22
23 #include <vector>
24
25 using namespace clang;
26
27 void ChildEventQObjectCast::VisitDecl(Decl *decl)
28 {
29 auto *childEventMethod = dyn_cast<CXXMethodDecl>(decl);
30 if (!childEventMethod) {
31 return;
32 }
33
34 Stmt *body = decl->getBody();
35 if (!body) {
36 return;
37 }
38
39 auto methodName = childEventMethod->getNameAsString();
40 if (!clazy::equalsAny(methodName, {"event", "childEvent", "eventFilter"})) {
41 return;
42 }
43
44 if (!clazy::isQObject(childEventMethod->getParent(), m_context->qtNamespace())) {
45 return;
46 }
47
48 auto callExprs = clazy::getStatements<CallExpr>(body, &(sm()));
49 for (auto *callExpr : callExprs) {
50 if (callExpr->getNumArgs() != 1) {
51 continue;
52 }
53
54 const FunctionDecl *fdecl = callExpr->getDirectCallee();
55 if (fdecl && clazy::name(fdecl) == "qobject_cast") {
56 auto *childCall = dyn_cast<CXXMemberCallExpr>(callExpr->getArg(0));
57 // The call to event->child()
58 if (!childCall) {
59 continue;
60 }
61
62 auto *childFDecl = childCall->getDirectCallee();
63 if (!childFDecl || childFDecl->getQualifiedNameAsString() != qtNamespaced("QChildEvent::child")) {
64 continue;
65 }
66
67 emitWarning(childCall, "qobject_cast in childEvent");
68 }
69 }
70 }