File indexing completed on 2024-05-19 05:41:42

0001 /*
0002     SPDX-FileCopyrightText: 2018 Sergio Martins <smartins@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "qhash-with-char-pointer-key.h"
0008 #include "StringUtils.h"
0009 #include "TypeUtils.h"
0010 #include "Utils.h"
0011 
0012 #include <clang/AST/DeclBase.h>
0013 #include <clang/AST/DeclTemplate.h>
0014 #include <clang/AST/TemplateBase.h>
0015 #include <clang/AST/Type.h>
0016 #include <llvm/ADT/StringRef.h>
0017 
0018 class ClazyContext;
0019 
0020 using namespace clang;
0021 
0022 QHashWithCharPointerKey::QHashWithCharPointerKey(const std::string &name, ClazyContext *context)
0023     : CheckBase(name, context)
0024 {
0025 }
0026 
0027 void QHashWithCharPointerKey::VisitDecl(clang::Decl *decl)
0028 {
0029     auto *tsdecl = Utils::templateSpecializationFromVarDecl(decl);
0030     if (!tsdecl || clazy::name(tsdecl) != "QHash") { // For QMap you shouldn't use any kind of pointers, that's handled in another check
0031         return;
0032     }
0033 
0034     const TemplateArgumentList &templateArguments = tsdecl->getTemplateArgs();
0035     if (templateArguments.size() != 2) {
0036         return;
0037     }
0038 
0039     QualType qt = templateArguments[0].getAsType();
0040     if (!qt.isNull() && qt->isPointerType()) {
0041         qt = clazy::pointeeQualType(qt);
0042         if (!qt.isNull() && !qt->isPointerType() && qt->isCharType()) {
0043             emitWarning(decl->getBeginLoc(), "Using QHash<const char *, T> is dangerous");
0044         }
0045     }
0046 }