File indexing completed on 2024-11-24 05:05:11

0001 /*
0002     SPDX-FileCopyrightText: 2020 Sergio Martins <smartins@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "qstring-comparison-to-implicit-char.h"
0008 #include "HierarchyUtils.h"
0009 #include "QtUtils.h"
0010 #include "TypeUtils.h"
0011 #include "Utils.h"
0012 
0013 #include <clang/AST/AST.h>
0014 
0015 using namespace clang;
0016 
0017 QStringComparisonToImplicitChar::QStringComparisonToImplicitChar(const std::string &name, ClazyContext *context)
0018     : CheckBase(name, context)
0019 {
0020 }
0021 
0022 void QStringComparisonToImplicitChar::VisitStmt(clang::Stmt *stmt)
0023 {
0024     auto *callExpr = dyn_cast<CXXOperatorCallExpr>(stmt);
0025     if (!callExpr || !callExpr->getCalleeDecl() || callExpr->getNumArgs() != 2) {
0026         return;
0027     }
0028 
0029     Expr *arg1 = callExpr->getArg(1);
0030     auto *il = clazy::getFirstChildOfType2<IntegerLiteral>(arg1);
0031     if (!il) {
0032         return;
0033     }
0034 
0035     auto *functionDecl = dyn_cast<FunctionDecl>(callExpr->getCalleeDecl());
0036     if (!functionDecl || functionDecl->getQualifiedNameAsString() != "operator==") {
0037         return;
0038     }
0039 
0040     ParmVarDecl *parm1 = functionDecl->getParamDecl(0);
0041     if (parm1->getType().getAsString(lo()) != "const QString &") {
0042         return;
0043     }
0044 
0045     ParmVarDecl *parm2 = functionDecl->getParamDecl(1);
0046     if (parm2->getType().getAsString(lo()) != "QChar") {
0047         return;
0048     }
0049 
0050     emitWarning(stmt, "QString being compared to implicit QChar");
0051 }