Debian Codegraph @

src/checks/level1/post-event.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 "post-event.h"
8 #include "StringUtils.h"
9 #include "TypeUtils.h"
10
11 #include <clang/AST/Expr.h>
12 #include <clang/AST/Stmt.h>
13 #include <clang/Basic/LLVM.h>
14 #include <llvm/Support/Casting.h>
15
16 using namespace clang;
17
18 void PostEvent::VisitStmt(clang::Stmt *stmt)
19 {
20 auto *callexpr = dyn_cast<CallExpr>(stmt);
21 if (!callexpr) {
22 return;
23 }
24
25 auto name = trimQtNamespace(clazy::qualifiedMethodName(callexpr));
26
27 const bool isPostEvent = name == "QCoreApplication::postEvent";
28 const bool isSendEvent = name == "QCoreApplication::sendEvent";
29
30 // if (!isPostEvent && !isSendEvent)
31 // Send event has false-positives
32 if (!isPostEvent) {
33 return;
34 }
35
36 Expr *event = callexpr->getNumArgs() > 1 ? callexpr->getArg(1) : nullptr;
37 if (!event || clazy::simpleTypeName(event->getType(), lo()) != "QEvent *") {
38 return;
39 }
40
41 bool isStack = false;
42 bool isHeap = false;
43 clazy::heapOrStackAllocated(event, qtNamespaced("QEvent"), lo(), isStack, isHeap);
44
45 if (isStack || isHeap) {
46 if (isSendEvent && isHeap) {
47 emitWarning(stmt, "Events passed to sendEvent should be stack allocated");
48 } else if (isStack) {
49 emitWarning(stmt, "Events passed to postEvent should be heap allocated");
50 }
51 } else {
52 // It's something else, like an rvalue, ignore it
53 }
54 }