Debian Codegraph @

src/checks/manuallevel/thread-with-slots.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 "thread-with-slots.h"
8 #include "AccessSpecifierManager.h"
9 #include "ClazyContext.h"
10 #include "HierarchyUtils.h"
11 #include "QtUtils.h"
12 #include "TypeUtils.h"
13
14 #include <clang/AST/Decl.h>
15 #include <clang/AST/DeclCXX.h>
16 #include <clang/AST/Expr.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 static bool hasMutexes(Stmt *body)
25 {
26 auto declrefs = clazy::getStatements<DeclRefExpr>(body);
27 for (auto *declref : declrefs) {
28 ValueDecl *valueDecl = declref->getDecl();
29 if (const CXXRecordDecl *record = clazy::typeAsRecord(valueDecl->getType())) {
30 if (clazy::name(record) == "QMutex" || clazy::name(record) == "QBasicMutex") {
31 return true;
32 }
33 }
34 }
35
36 return false;
37 }
38
39 void ThreadWithSlots::VisitStmt(clang::Stmt *stmt)
40 {
41 const std::string qThreadClass = qtNamespaced("QThread");
42 // Here we catch slots not marked as slots, we warn when the connect is made
43
44 auto *callExpr = dyn_cast<CallExpr>(stmt);
45 if (!callExpr || !m_context->accessSpecifierManager) {
46 return;
47 }
48
49 FunctionDecl *connectFunc = callExpr->getDirectCallee();
50 if (!clazy::isConnect(connectFunc, m_context->qtNamespace())) {
51 return;
52 }
53
54 CXXMethodDecl *slot = clazy::receiverMethodForConnect(callExpr, m_context->qtNamespace());
55 if (!slot || !clazy::derivesFrom(slot->getParent(), qThreadClass)) {
56 return;
57 }
58
59 if (slot->getParent()->getQualifiedNameAsString() == qThreadClass) { // The slots in QThread are thread safe, we're only worried about derived classes
60 return;
61 }
62
63 QtAccessSpecifierType specifierType = m_context->accessSpecifierManager->qtAccessSpecifierType(slot);
64 if (specifierType == QtAccessSpecifier_Slot || specifierType == QtAccessSpecifier_Signal) {
65 return; // For stuff explicitly marked as slots or signals we use VisitDecl
66 }
67
68 emitWarning(slot, "Slot " + slot->getQualifiedNameAsString() + " might not run in the expected thread");
69 }
70
71 void ThreadWithSlots::VisitDecl(Decl *decl)
72 {
73 const std::string qThreadClass = qtNamespaced("QThread");
74 // Here we catch slots marked as such, and warn when they are declared
75 auto *method = dyn_cast<CXXMethodDecl>(decl);
76 if (!method || !m_context->accessSpecifierManager || !method->isThisDeclarationADefinition() || !method->hasBody()
77 || !clazy::derivesFrom(method->getParent(), qThreadClass)) {
78 return;
79 }
80
81 // The slots in QThread are thread safe, we're only worried about derived classes:
82 if (method->getParent()->getQualifiedNameAsString() == qThreadClass) {
83 return;
84 }
85
86 // We're only interested in slots:
87 if (m_context->accessSpecifierManager->qtAccessSpecifierType(method) != QtAccessSpecifier_Slot) {
88 return;
89 }
90
91 // Look for a mutex, or mutex locker, to avoid some false-positives
92 Stmt *body = method->getBody();
93 if (hasMutexes(body)) {
94 return;
95 }
96
97 // If we use member mutexes, let's not warn either
98 bool accessesNonMutexMember = false;
99 auto memberexprs = clazy::getStatements<MemberExpr>(body);
100 for (auto *memberexpr : memberexprs) {
101 ValueDecl *valueDecl = memberexpr->getMemberDecl();
102 if (const CXXRecordDecl *record = clazy::typeAsRecord(valueDecl->getType())) {
103 if (clazy::name(record) == "QMutex" || clazy::name(record) == "QBasicMutex") {
104 return;
105 }
106 }
107 accessesNonMutexMember = true;
108 }
109
110 if (!accessesNonMutexMember) {
111 return;
112 }
113
114 emitWarning(method, "Slot " + method->getQualifiedNameAsString() + " might not run in the expected thread");
115 }