Debian Codegraph @

src/checks/level0/lambda-unique-connection.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2017 Sergio Martins <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "lambda-unique-connection.h"
8 #include "HierarchyUtils.h"
9 #include "QtUtils.h"
10
11 #include <clang/AST/Decl.h>
12 #include <clang/AST/DeclTemplate.h>
13 #include <clang/AST/Expr.h>
14 #include <clang/AST/Stmt.h>
15 #include <clang/Basic/LLVM.h>
16 #include <llvm/ADT/StringRef.h>
17 #include <llvm/Support/Casting.h>
18
19 #include <vector>
20
21 using namespace clang;
22
23 void LambdaUniqueConnection::VisitStmt(clang::Stmt *stmt)
24 {
25 auto *call = dyn_cast<CallExpr>(stmt);
26 if (!call) {
27 return;
28 }
29
30 // We want this signature:
31 // connect(const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
32
33 FunctionDecl *func = call->getDirectCallee();
34 if (!func || func->getNumParams() != 5 || !func->isTemplateInstantiation() || !clazy::isConnect(func, m_context->qtNamespace())
35 || !clazy::connectHasPMFStyle(func)) {
36 return;
37 }
38
39 Expr *typeArg = call->getArg(4); // The type
40
41 std::vector<DeclRefExpr *> result;
42 clazy::getChilds(typeArg, result);
43
44 bool found = false;
45 for (auto *declRef : result) {
46 if (auto *enumConstant = dyn_cast<EnumConstantDecl>(declRef->getDecl())) {
47 if (clazy::name(enumConstant) == "UniqueConnection") {
48 found = true;
49 break;
50 }
51 }
52 }
53
54 if (!found) {
55 return;
56 }
57
58 FunctionTemplateSpecializationInfo *tsi = func->getTemplateSpecializationInfo();
59 if (!tsi) {
60 return;
61 }
62 FunctionTemplateDecl *temp = tsi->getTemplate();
63 const TemplateParameterList *tempParams = temp->getTemplateParameters();
64 if (tempParams->size() != 2) {
65 return;
66 }
67
68 const CXXMethodDecl *method = clazy::pmfFromConnect(call, 3, m_context->qtNamespace());
69 if (method) {
70 // How else to detect if it's the right overload ? It's all templated stuff with the same
71 // names for all the template arguments
72 return;
73 }
74
75 emitWarning(typeArg, "UniqueConnection is not supported with non-member functions");
76 }