Debian Codegraph @

src/checks/level0/wrong-qevent-cast.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2018 Sergio Martins <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "wrong-qevent-cast.h"
8 #include "ClazyContext.h"
9 #include "HierarchyUtils.h"
10 #include "StringUtils.h"
11 #include "TypeUtils.h"
12 #include "Utils.h"
13
14 #include <algorithm>
15 #include <clang/AST/Decl.h>
16 #include <clang/AST/DeclCXX.h>
17 #include <clang/AST/Expr.h>
18 #include <clang/AST/ExprCXX.h>
19 #include <clang/AST/ParentMap.h>
20 #include <clang/AST/Stmt.h>
21 #include <clang/AST/Type.h>
22 #include <clang/Basic/LLVM.h>
23 #include <llvm/ADT/APSInt.h>
24 #include <llvm/ADT/StringRef.h>
25 #include <llvm/Support/Casting.h>
26
27 #include <unordered_map>
28 #include <vector>
29
30 using namespace clang;
31
32 using ClassNameList = std::vector<StringRef>;
33
34 enum QtUnregularlyNamedEventTypes {
35 DragEnter = 60,
36 DragLeave = 62,
37 OrientationChange = 208,
38 ActionAdded = 114,
39 ActionRemoved = 115,
40 ActionChanged = 99,
41 ChildAdded = 68,
42 ChildRemoved = 71,
43 ChildPolished = 69,
44 MouseButtonPress = 2,
45 MouseButtonRelease = 3,
46 MouseButtonDblClick = 4,
47 MouseMove = 5,
48 NonClientAreaMouseMove = 173,
49 NonClientAreaMouseButtonPress = 174,
50 NonClientAreaMouseButtonRelease = 175,
51 NonClientAreaMouseButtonDblClick = 176,
52 FocusIn = 8,
53 FocusOut = 9,
54 FocusAboutToChange = 23,
55 Gesture = 198,
56 GestureOverride = 202,
57 HoverEnter = 127,
58 HoverLeave = 128,
59 HoverMove = 129,
60 TabletEnterProximity = 171,
61 TabletLeaveProximity = 172,
62 TabletPress = 92,
63 TabletMove = 87,
64 TabletRelease = 93,
65 ToolTip = 110,
66 Wheel = 31,
67 KeyPress = 6,
68 KeyRelease = 7,
69 ShortcutOverride = 51,
70 DragMove = 61,
71 GraphicsSceneMouseMove = 155,
72 GraphicsSceneMousePress = 156,
73 GraphicsSceneMouseRelease = 157,
74 GraphicsSceneMouseDoubleClick = 158,
75 GraphicsSceneContextMenu = 159,
76 GraphicsSceneHoverEnter = 160,
77 GraphicsSceneHoverMove = 161,
78 GraphicsSceneHoverLeave = 162,
79 GraphicsSceneHelp = 163,
80 GraphicsSceneDragEnter = 164,
81 GraphicsSceneDragMove = 165,
82 GraphicsSceneDragLeave = 166,
83 GraphicsSceneDrop = 167,
84 GraphicsSceneWheel = 168,
85 GraphicsSceneResize = 181,
86 TouchBegin = 194,
87 TouchEnd = 196,
88 TouchCancel = 209,
89 TouchUpdate = 195,
90 NativeGesture = 197,
91 MetaCall = 43,
92 WhatsThis = 111,
93 ContextMenu = 82,
94 QueryWhatsThis = 123
95 // StatusTip = 112 not irregular, but qtbase casts it to QHelpEvent for some reason, needs investigation
96 };
97
98 static bool eventTypeMatchesClass(QtUnregularlyNamedEventTypes eventType, const std::string &eventTypeStr, StringRef className)
99 {
100 // In the simplest case, the class is "Q" + eventType + "Event"
101 std::string expectedClassName = std::string("Q") + eventTypeStr + std::string("Event");
102 if (expectedClassName == className) {
103 return true;
104 }
105
106 // Otherwise it's unregular and we need a map:
107
108 static std::unordered_map<QtUnregularlyNamedEventTypes, ClassNameList, std::hash<int>> map = {
109 {ActionAdded, {"QActionEvent"}},
110 {ActionRemoved, {"QActionEvent"}},
111 {ActionChanged, {"QActionEvent"}},
112 {ChildAdded, {"QChildEvent"}},
113 {ChildRemoved, {"QChildEvent"}},
114 {ChildPolished, {"QChildEvent"}},
115 {MetaCall, {"QDBusSpyCallEvent", "QDBusCallDeliveryEvent"}},
116 {DragEnter, {"QDragEnterEvent", "QDragMoveEvent", "QDropEvent"}},
117 {DragLeave, {"QDragLeaveEvent", "QDragMoveEvent", "QDropEvent"}},
118 {DragMove, {"QDragMoveEvent", "QDropEvent"}},
119 {FocusIn, {"QFocusEvent"}},
120 {FocusOut, {"QFocusEvent"}},
121 {FocusAboutToChange, {"QFocusEvent"}},
122 {Gesture, {"QGestureEvent"}},
123 {GestureOverride, {"QGestureEvent"}},
124 {GraphicsSceneContextMenu, {"QGraphicsSceneEvent"}},
125 {GraphicsSceneHoverEnter, {"QGraphicsSceneHoverEvent", "QGraphicsSceneEvent"}},
126 {GraphicsSceneHoverMove, {"QGraphicsSceneHoverEvent", "QGraphicsSceneEvent"}},
127 {GraphicsSceneHoverLeave, {"QGraphicsSceneHoverEvent", "QGraphicsSceneEvent"}},
128 {GraphicsSceneHelp, {"QGraphicsSceneEvent"}},
129 {GraphicsSceneDragEnter, {"QGraphicsSceneDragDropEvent", "QGraphicsSceneEvent"}},
130 {GraphicsSceneDragMove, {"QGraphicsSceneDragDropEvent", "QGraphicsSceneEvent"}},
131 {GraphicsSceneDragLeave, {"QGraphicsSceneDragDropEvent", "QGraphicsSceneEvent"}},
132 {GraphicsSceneDrop, {"QGraphicsSceneDragDropEvent", "QGraphicsSceneEvent"}},
133 {GraphicsSceneWheel, {"QGraphicsSceneEvent"}},
134 {GraphicsSceneResize, {"QGraphicsSceneEvent"}},
135 {GraphicsSceneMouseMove, {"QGraphicsSceneMouseEvent"}},
136 {GraphicsSceneMousePress, {"QGraphicsSceneMouseEvent"}},
137 {GraphicsSceneMouseRelease, {"QGraphicsSceneMouseEvent"}},
138 {GraphicsSceneMouseDoubleClick, {"QGraphicsSceneMouseEvent"}},
139 //{ StatusTip, {"QStatusTipEvent" } },
140 {ToolTip, {"QHelpEvent"}},
141 {WhatsThis, {"QHelpEvent"}},
142 {QueryWhatsThis, {"QHelpEvent"}},
143 {HoverEnter, {"QHoverEvent", "QInputEvent"}},
144 {HoverLeave, {"QHoverEvent", "QInputEvent"}},
145 {HoverMove, {"QHoverEvent", "QInputEvent"}},
146 {KeyPress, {"QKeyEvent", "QInputEvent"}},
147 {KeyRelease, {"QKeyEvent", "QInputEvent"}},
148 {ShortcutOverride, {"QKeyEvent", "QInputEvent"}},
149 {MouseButtonPress, {"QMouseEvent"}},
150 {MouseButtonRelease, {"QMouseEvent"}},
151 {MouseButtonDblClick, {"QMouseEvent"}},
152 {MouseMove, {"QMouseEvent"}},
153 {NonClientAreaMouseMove, {"QMouseEvent"}},
154 {NonClientAreaMouseButtonPress, {"QMouseEvent"}},
155 {NonClientAreaMouseButtonRelease, {"QMouseEvent"}},
156 {NonClientAreaMouseButtonRelease, {"QMouseEvent"}},
157 {NonClientAreaMouseButtonDblClick, {"QMouseEvent"}},
158 {NativeGesture, {"QInputEvent"}},
159 {OrientationChange, {"QScreenOrientationChangeEvent"}},
160 {TabletEnterProximity, {"QTabletEvent", "QInputEvent"}},
161 {TabletLeaveProximity, {"QTabletEvent", "QInputEvent"}},
162 {TabletPress, {"QTabletEvent", "QInputEvent"}},
163 {TabletMove, {"QTabletEvent", "QInputEvent"}},
164 {TabletRelease, {"QTabletEvent", "QInputEvent"}},
165 {TouchBegin, {"QTouchEvent", "QInputEvent"}},
166 {TouchCancel, {"QTouchEvent", "QInputEvent"}},
167 {TouchEnd, {"QTouchEvent", "QInputEvent"}},
168 {TouchUpdate, {"QTouchEvent", "QInputEvent"}},
169 {Wheel, {"QInputEvent"}},
170 {ContextMenu, {"QInputEvent"}}};
171
172 auto it = map.find(eventType);
173 if (it == map.cend()) {
174 return false;
175 }
176
177 const ClassNameList &classes = it->second;
178 const bool found = std::ranges::find(classes, className) != classes.cend();
179
180 return found;
181 }
182
183 // TODO: Use iterators
184 CaseStmt *getCaseStatement(clang::ParentMap *pmap, Stmt *stmt, DeclRefExpr *event)
185 {
186 Stmt *s = pmap->getParent(stmt);
187
188 while (s) {
189 if (auto *ifStmt = dyn_cast<IfStmt>(s)) {
190 // if there's we're inside an if statement then skip, to avoid false-positives
191 auto *declRef = clazy::getFirstChildOfType2<DeclRefExpr>(ifStmt->getCond());
192 if (declRef && declRef->getDecl() == event->getDecl()) {
193 return nullptr;
194 }
195 }
196
197 if (auto *caseStmt = dyn_cast<CaseStmt>(s)) {
198 auto *switchStmt = clazy::getSwitchFromCase(pmap, caseStmt);
199 if (switchStmt) {
200 auto *declRef = clazy::getFirstChildOfType2<DeclRefExpr>(switchStmt->getCond());
201 // Does this switch refer to the same QEvent ?
202 if (declRef && declRef->getDecl() == event->getDecl()) {
203 return caseStmt;
204 }
205 }
206 }
207
208 s = pmap->getParent(s);
209 }
210
211 return nullptr;
212 }
213
214 void WrongQEventCast::VisitStmt(clang::Stmt *stmt)
215 {
216 auto *cast = dyn_cast<CXXStaticCastExpr>(stmt);
217 if (!cast) {
218 return;
219 }
220
221 Expr *e = cast->getSubExpr();
222
223 QualType t = e ? e->getType() : QualType();
224 QualType pointeeType = t.isNull() ? QualType() : clazy::pointeeQualType(t);
225 const CXXRecordDecl *rec = pointeeType.isNull() ? nullptr : pointeeType->getAsCXXRecordDecl();
226
227 if (!rec || clazy::name(rec) != "QEvent") {
228 return;
229 }
230
231 const CXXRecordDecl *castTo = Utils::namedCastOuterDecl(cast);
232 if (!castTo) {
233 return;
234 }
235
236 auto *declref = clazy::getFirstChildOfType2<DeclRefExpr>(cast->getSubExpr());
237 if (!declref) {
238 return;
239 }
240
241 auto *caseStmt = getCaseStatement(m_context->parentMap, stmt, declref);
242 if (!caseStmt) {
243 return;
244 }
245
246 auto *caseValue = clazy::getFirstChildOfType2<DeclRefExpr>(caseStmt->getLHS());
247 if (!caseValue) {
248 return;
249 }
250
251 auto *enumeratorDecl = dyn_cast<EnumConstantDecl>(caseValue->getDecl());
252 if (!enumeratorDecl) {
253 return;
254 }
255
256 auto enumeratorVal = static_cast<QtUnregularlyNamedEventTypes>(enumeratorDecl->getInitVal().getExtValue());
257
258 std::string eventTypeStr = enumeratorDecl->getNameAsString();
259 StringRef castToName = clazy::name(castTo);
260
261 if (eventTypeMatchesClass(enumeratorVal, eventTypeStr, castToName)) {
262 return;
263 }
264
265 emitWarning(stmt, std::string("Cast from a QEvent::") + eventTypeStr + " event to " + std::string(castToName) + " looks suspicious.");
266 }