Debian Codegraph @

src/checks/level0/use-static-qregularexpression.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2021 Klarälvdalens Datakonsult AB a KDAB Group company [email protected]
3 SPDX-FileContributor: Waqar Ahmed <[email protected]>
4 SPDX-FileCopyrightText: 2021 Waqar Ahmed <[email protected]>
5 SPDX-FileCopyrightText: 2023 Alexander Lohnau <[email protected]>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9
10 #include "use-static-qregularexpression.h"
11 #include "HierarchyUtils.h"
12 #include "QtUtils.h"
13 #include "TypeUtils.h"
14 #include "Utils.h"
15
16 #include <clang/AST/AST.h>
17 #include <clang/AST/ExprCXX.h>
18
19 using namespace clang;
20
21 static MaterializeTemporaryExpr *isArgTemporaryObj(Expr *arg0)
22 {
23 return dyn_cast_or_null<MaterializeTemporaryExpr>(arg0);
24 }
25
26 static VarDecl *getVarDecl(Expr *arg)
27 {
28 auto *declRefExpr = dyn_cast<DeclRefExpr>(arg);
29 declRefExpr = declRefExpr ? declRefExpr : clazy::getFirstChildOfType<DeclRefExpr>(arg);
30 if (!declRefExpr) {
31 return nullptr;
32 }
33 return dyn_cast_or_null<VarDecl>(declRefExpr->getDecl());
34 }
35
36 static Expr *getVarInitExpr(VarDecl *VDef)
37 {
38 return VDef->getDefinition() ? VDef->getDefinition()->getInit() : nullptr;
39 }
40
41 static bool isQStringModifiedAfterCreation(clang::Stmt *expr, LangOptions lo)
42 {
43 // This QString is the result of a .arg/.mid or similar call, check all args if they are a literal
44 if (auto *methodCall = clazy::getFirstChildOfType<CXXMemberCallExpr>(expr)) {
45 if (auto *callee = methodCall->getMethodDecl()) {
46 if (callee->getReturnType().getAsString(lo) == "QString") {
47 return true;
48 }
49 }
50 }
51 return false;
52 }
53
54 static bool isQStringFromStringLiteral(Expr *qstring, LangOptions lo)
55 {
56 if (isArgTemporaryObj(qstring)) {
57 // Is it compile time known QString i.e., not from a function call
58 auto *qstringCtor = clazy::getFirstChildOfType<CXXConstructExpr>(qstring);
59 if (!qstringCtor) {
60 return false;
61 }
62
63 return clazy::getFirstChildOfType<StringLiteral>(qstringCtor);
64 }
65
66 if (auto *VD = getVarDecl(qstring)) {
67 const auto *stringLit = clazy::getFirstChildOfType<StringLiteral>(getVarInitExpr(VD));
68 if (stringLit) {
69 // If we have a string literal somewhere in there, but modify it using QString::arg or friends, we don't have a literal for th regex
70 if (auto *constructExpr = clazy::getFirstChildOfType<CXXConstructExpr>(VD->getInit())) {
71 return !isQStringModifiedAfterCreation(constructExpr, lo);
72 } else {
73 return true;
74 }
75 }
76 }
77 return false;
78 }
79
80 bool UseStaticQRegularExpression::isTemporaryQRegexObj(Expr *qregexVar, const LangOptions &lo)
81 {
82 // Get the QRegularExpression ctor
83 auto *ctor = clazy::getFirstChildOfType<CXXConstructExpr>(qregexVar);
84 if (!ctor || ctor->getNumArgs() == 0) {
85 return false;
86 }
87
88 // Check if its first arg is "QString"
89 auto *qstrArg = ctor->getArg(0);
90 if (!qstrArg || clazy::typeName(qstrArg->getType(), lo, true) != qtNamespaced("QString")) {
91 return false;
92 }
93
94 return isQStringFromStringLiteral(qstrArg, lo) && !isQStringModifiedAfterCreation(qstrArg, lo);
95 }
96
97 static bool isQRegexpFromStringLiteral(VarDecl *qregexVarDecl, LangOptions lo)
98 {
99 Expr *initExpr = getVarInitExpr(qregexVarDecl);
100 if (!initExpr) {
101 return false;
102 }
103
104 auto *ctorCall = dyn_cast<CXXConstructExpr>(initExpr);
105 if (!ctorCall) {
106 ctorCall = clazy::getFirstChildOfType<CXXConstructExpr>(initExpr);
107 if (!ctorCall) {
108 return false;
109 }
110 }
111
112 if (ctorCall->getNumArgs() < 2) {
113 return false;
114 }
115
116 auto *qstringArg = ctorCall->getArg(0);
117 if (!qstringArg) {
118 return false;
119 }
120
121 // For C++17, we have to put in more effort to resolving the initialization
122 if (auto *expr = clazy::getFirstChildOfType<DeclRefExpr>(qstringArg)) {
123 if (auto *exprClean = dyn_cast<VarDecl>(expr->getDecl())) {
124 if (isQStringModifiedAfterCreation(exprClean->getInit(), lo)) {
125 return false;
126 }
127 }
128 }
129 return isQStringFromStringLiteral(qstringArg, lo) && !isQStringModifiedAfterCreation(qstringArg, lo);
130 }
131
132 static bool isArgNonStaticLocalVar(Expr *qregexp, LangOptions lo)
133 {
134 auto *varDecl = getVarDecl(qregexp);
135 if (!varDecl) {
136 return false;
137 }
138
139 if (!isQRegexpFromStringLiteral(varDecl, lo)) {
140 return false;
141 }
142
143 return varDecl->isLocalVarDecl() && !varDecl->isStaticLocal();
144 }
145
146 static bool isOfAcceptableType(CXXMethodDecl *methodDecl)
147 {
148 const auto type = clazy::classNameFor(methodDecl);
149 return type == "QString" || type == "QStringList" || type == "QRegularExpression" || type == "QListSpecialMethods" /* for QStringList in Qt6 */;
150 }
151
152 static bool firstArgIsQRegularExpression(CXXMethodDecl *methodDecl, const LangOptions &lo)
153 {
154 return clazy::simpleArgTypeName(methodDecl, 0, lo) == "QRegularExpression";
155 }
156
157 void UseStaticQRegularExpression::VisitStmt(clang::Stmt *stmt)
158 {
159 if (!stmt) {
160 return;
161 }
162
163 auto *method = dyn_cast_or_null<CXXMemberCallExpr>(stmt);
164 if (!method) {
165 return;
166 }
167
168 if (method->getNumArgs() == 0) {
169 return;
170 }
171
172 auto *methodDecl = method->getMethodDecl();
173 if (!methodDecl || !methodDecl->getDeclName().isIdentifier()) {
174 return;
175 }
176
177 if (!isOfAcceptableType(methodDecl)) {
178 return;
179 }
180
181 // QRegularExpression.match()
182 const auto methodName = methodDecl->getName();
183 if (methodName == "match" || methodName == "globalMatch") {
184 auto *obj = method->getImplicitObjectArgument()->IgnoreImpCasts();
185 if (!obj) {
186 return;
187 }
188
189 if (obj->isLValue()) {
190 if (isArgNonStaticLocalVar(obj, lo())) {
191 emitWarning(obj->getBeginLoc(), "Don't create temporary QRegularExpression objects. Use a static QRegularExpression object instead");
192 return;
193 }
194
195 // In clang20, "auto m2 = QRegularExpression("[123]").globalMatch(selectedText);" is apparently an l-value
196 if (auto *temp = dyn_cast<MaterializeTemporaryExpr>(obj); temp && isTemporaryQRegexObj(temp, lo())) {
197 emitWarning(temp->getBeginLoc(), "Don't create temporary QRegularExpression objects. Use a static QRegularExpression object instead");
198 }
199
200 } else if (obj->isXValue()) {
201 // is it a temporary?
202 auto *temp = dyn_cast<MaterializeTemporaryExpr>(obj);
203 if (!temp) {
204 return;
205 }
206 if (isTemporaryQRegexObj(temp, lo())) {
207 emitWarning(temp->getBeginLoc(), "Don't create temporary QRegularExpression objects. Use a static QRegularExpression object instead");
208 }
209 }
210 return;
211 }
212
213 if (!firstArgIsQRegularExpression(methodDecl, lo())) {
214 return;
215 }
216
217 Expr *qregexArg = method->getArg(0);
218 if (!qregexArg) {
219 return;
220 }
221
222 // Its a QString*().method(QRegularExpression(arg)) ?
223 if (auto *temp = isArgTemporaryObj(qregexArg)) {
224 if (isTemporaryQRegexObj(temp, lo())) {
225 emitWarning(qregexArg->getBeginLoc(), "Don't create temporary QRegularExpression objects. Use a static QRegularExpression object instead");
226 }
227 }
228
229 // Its a local QRegularExpression variable?
230 if (isArgNonStaticLocalVar(qregexArg, lo())) {
231 emitWarning(qregexArg->getBeginLoc(), "Don't create temporary QRegularExpression objects. Use a static QRegularExpression object instead");
232 }
233 }