Debian Codegraph @

src/checks/level0/qstring-arg.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2015 Sergio Martins <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "qstring-arg.h"
8 #include "ClazyContext.h"
9 #include "HierarchyUtils.h"
10 #include "PreProcessorVisitor.h"
11 #include "StringUtils.h"
12 #include "Utils.h"
13 #include "clazy_stl.h"
14
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/Stmt.h>
20 #include <clang/Basic/Diagnostic.h>
21 #include <clang/Basic/LLVM.h>
22 #include <clang/Basic/SourceLocation.h>
23 #include <clang/Lex/Lexer.h>
24 #include <llvm/ADT/StringRef.h>
25 #include <llvm/Support/Casting.h>
26
27 #include <vector>
28
29 using namespace clang;
30
31 QStringArg::QStringArg(const std::string &name, Options options)
32 : CheckBase(name, options)
33 {
34 m_filesToIgnore = {"qstring.h"};
35 }
36
37 static std::string variableNameFromArg(Expr *arg)
38 {
39 std::vector<DeclRefExpr *> declRefs;
40 clazy::getChilds<DeclRefExpr>(arg, declRefs);
41 if (declRefs.size() == 1) {
42 ValueDecl *decl = declRefs.at(0)->getDecl();
43 return decl ? decl->getNameAsString() : std::string();
44 }
45
46 return {};
47 }
48
49 static CXXMethodDecl *isArgMethod(FunctionDecl *func, const char *className)
50 {
51 if (!func) {
52 return nullptr;
53 }
54
55 auto *method = dyn_cast<CXXMethodDecl>(func);
56 if (!method || clazy::name(method) != "arg") {
57 return nullptr;
58 }
59
60 const CXXRecordDecl *record = method->getParent();
61 if (!record || clazy::name(record) != className) {
62 return nullptr;
63 }
64
65 return method;
66 }
67
68 static bool isArgFuncWithOnlyQString(CallExpr *callExpr)
69 {
70 if (!callExpr) {
71 return false;
72 }
73
74 CXXMethodDecl *method = isArgMethod(callExpr->getDirectCallee(), "QString");
75 if (!method) {
76 return false;
77 }
78
79 ParmVarDecl *secondParam = method->getParamDecl(1);
80 if (clazy::classNameFor(secondParam) == "QString") {
81 return true;
82 }
83
84 ParmVarDecl *firstParam = method->getParamDecl(0);
85 if (clazy::classNameFor(firstParam) != "QString" && !clazy::startsWith(firstParam->getType().getAsString(), "const char &")) {
86 return false;
87 }
88
89 // This is a arg(QString, int, QChar) call, it's good if the second parameter is a default param
90 return isa<CXXDefaultArgExpr>(callExpr->getArg(1));
91 }
92
93 bool QStringArg::checkMultiArgWarningCase(const std::vector<clang::CallExpr *> &calls)
94 {
95 if (calls.size() <= 1) {
96 return false; // Nothing to do
97 }
98 std::string replacement;
99 SourceLocation beginLoc;
100 int argAggregated = 0;
101 for (CallExpr *call : calls) {
102 for (auto *arg : call->arguments()) {
103 if (!isa<CXXDefaultArgExpr>(arg)) {
104 ++argAggregated;
105 }
106 }
107 if (argAggregated > 9) { // Relevant for Qt5
108 return false;
109 }
110 if (!beginLoc.isValid()) {
111 beginLoc = call->getBeginLoc();
112 }
113
114 std::string callArgs;
115 for (auto *arg : call->arguments()) {
116 if (!isa<CXXDefaultArgExpr>(arg)) {
117 if (!callArgs.empty()) {
118 callArgs += ", ";
119 }
120 callArgs += Lexer::getSourceText(CharSourceRange::getTokenRange(arg->getSourceRange()), sm(), lo()).str();
121 }
122 }
123 // The args for the chained calls have to be prepended instead of appended
124 replacement = callArgs + (replacement.empty() ? "" : ", ") + replacement;
125 }
126 if (auto *subexprCall = clazy::getFirstChildOfType<MemberExpr>(calls.back())) {
127 emitWarning(beginLoc,
128 "Use multi-arg instead",
129 {FixItHint::CreateReplacement(SourceRange(subexprCall->getEndLoc(), calls.at(0)->getEndLoc()), "arg(" + replacement + ")")});
130 return true;
131 }
132
133 return false;
134 }
135
136 void QStringArg::checkForMultiArgOpportunities(CXXMemberCallExpr *memberCall)
137 {
138 if (!isArgFuncWithOnlyQString(memberCall)) {
139 return;
140 }
141
142 if (memberCall->getBeginLoc().isMacroID()) {
143 auto macroName = Lexer::getImmediateMacroName(memberCall->getBeginLoc(), sm(), lo());
144 if (macroName == "QT_REQUIRE_VERSION") { // bug #391851
145 return;
146 }
147 }
148
149 std::vector<clang::CallExpr *> callExprs = Utils::callListForChain(memberCall);
150 std::vector<clang::CallExpr *> argCalls;
151 for (auto *call : callExprs) {
152 if (!clazy::contains(m_alreadyProcessedChainedCalls, call) && isArgFuncWithOnlyQString(call)) {
153 argCalls.push_back(call);
154 m_alreadyProcessedChainedCalls.push_back(call);
155 } else {
156 if (checkMultiArgWarningCase(argCalls)) {
157 return;
158 }
159 argCalls.clear();
160 }
161 }
162
163 checkMultiArgWarningCase(argCalls);
164 }
165
166 bool QStringArg::checkQLatin1StringCase(CXXMemberCallExpr *memberCall)
167 {
168 const PreProcessorVisitor *preProcessorVisitor = m_context->preprocessorVisitor;
169 if (!preProcessorVisitor || preProcessorVisitor->qtVersion() < 51400) {
170 // QLatin1String::arg() was introduced in Qt 5.14
171 return false;
172 }
173
174 if (!isArgMethod(memberCall->getDirectCallee(), "QLatin1String")) {
175 return false;
176 }
177
178 if (memberCall->getNumArgs() == 0) {
179 return false;
180 }
181
182 Expr *arg = memberCall->getArg(0);
183 QualType t = arg->getType();
184 if (!t->isIntegerType() || t->isCharType()) {
185 return false;
186 }
187
188 emitWarning(memberCall, "Argument passed to QLatin1String::arg() will be implicitly cast to QChar");
189 return true;
190 }
191
192 void QStringArg::VisitStmt(clang::Stmt *stmt)
193 {
194 auto *memberCall = dyn_cast<CXXMemberCallExpr>(stmt);
195 if (!memberCall) {
196 return;
197 }
198
199 if (shouldIgnoreFile(stmt->getBeginLoc())) {
200 return;
201 }
202
203 checkForMultiArgOpportunities(memberCall);
204
205 if (checkQLatin1StringCase(memberCall)) {
206 return;
207 }
208
209 if (!isOptionSet("fillChar-overloads")) {
210 return;
211 }
212
213 CXXMethodDecl *method = isArgMethod(memberCall->getDirectCallee(), "QString");
214 if (!method) {
215 return;
216 }
217
218 if (clazy::simpleArgTypeName(method, method->getNumParams() - 1, lo()) == qtNamespaced("QChar")) {
219 // The second arg wasn't passed, so this is a safe and unambiguous use, like .arg(1)
220 if (isa<CXXDefaultArgExpr>(memberCall->getArg(1))) {
221 return;
222 }
223
224 const ParmVarDecl *p = method->getParamDecl(2);
225 if (p && clazy::name(p) == "base") {
226 // User went through the trouble specifying a base, lets allow it if it's a literal.
227 std::vector<IntegerLiteral *> literals;
228 clazy::getChilds<IntegerLiteral>(memberCall->getArg(2), literals);
229 if (!literals.empty()) {
230 return;
231 }
232
233 std::string variableName = clazy::toLower(variableNameFromArg(memberCall->getArg(2)));
234 if (clazy::contains(variableName, "base")) {
235 return;
236 }
237 }
238
239 p = method->getParamDecl(1);
240 if (p && clazy::name(p) == "fieldWidth") {
241 // He specified a literal, so he knows what he's doing, otherwise he would have put it directly in the string
242 std::vector<IntegerLiteral *> literals;
243 clazy::getChilds<IntegerLiteral>(memberCall->getArg(1), literals);
244 if (!literals.empty()) {
245 return;
246 }
247
248 // the variable is named "width", user knows what he's doing
249 std::string variableName = clazy::toLower(variableNameFromArg(memberCall->getArg(1)));
250 if (clazy::contains(variableName, "width")) {
251 return;
252 }
253 }
254
255 emitWarning(stmt->getBeginLoc(), "Using QString::arg() with fillChar overload");
256 }
257 }