Debian Codegraph @

src/checks/level0/qstring-comparison-to-implicit-char.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2020 Sergio Martins <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "qstring-comparison-to-implicit-char.h"
8 #include "HierarchyUtils.h"
9 #include "QtUtils.h"
10 #include "TypeUtils.h"
11 #include "Utils.h"
12
13 #include <clang/AST/AST.h>
14
15 using namespace clang;
16
17 void QStringComparisonToImplicitChar::VisitStmt(clang::Stmt *stmt)
18 {
19 auto *callExpr = dyn_cast<CXXOperatorCallExpr>(stmt);
20 if (!callExpr || !callExpr->getCalleeDecl() || callExpr->getNumArgs() != 2) {
21 return;
22 }
23
24 Expr *arg1 = callExpr->getArg(1);
25 const auto *il = clazy::getFirstChildOfType2<IntegerLiteral>(arg1);
26 if (!il) {
27 return;
28 }
29
30 auto *functionDecl = dyn_cast<FunctionDecl>(callExpr->getCalleeDecl());
31 // We only care about the non-member functions
32 if (!functionDecl || functionDecl->getOverloadedOperator() != clang::OO_EqualEqual || isa<CXXMethodDecl>(functionDecl)
33 || functionDecl->getNumParams() != 2) {
34 return;
35 }
36
37 ParmVarDecl *parm1 = functionDecl->getParamDecl(0);
38 if (parm1->getType().getAsString(lo()) != "const QString &") {
39 return;
40 }
41
42 ParmVarDecl *parm2 = functionDecl->getParamDecl(1);
43 if (parm2->getType().getAsString(lo()) != "QChar") {
44 return;
45 }
46
47 emitWarning(stmt, "QString being compared to implicit QChar");
48 }