File indexing completed on 2024-05-12 05:40:56

0001 /*
0002     SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company info@kdab.com
0003     SPDX-FileContributor: SĂ©rgio Martins <sergio.martins@kdab.com>
0004 
0005     SPDX-FileCopyrightText: 2015 Sergio Martins <smartins@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #include "qmap-with-pointer-key.h"
0011 #include "StringUtils.h"
0012 #include "Utils.h"
0013 
0014 #include <clang/AST/DeclBase.h>
0015 #include <clang/AST/DeclTemplate.h>
0016 #include <clang/AST/TemplateBase.h>
0017 #include <clang/AST/Type.h>
0018 #include <llvm/ADT/StringRef.h>
0019 
0020 class ClazyContext;
0021 
0022 using namespace clang;
0023 
0024 QMapWithPointerKey::QMapWithPointerKey(const std::string &name, ClazyContext *context)
0025     : CheckBase(name, context)
0026 {
0027 }
0028 
0029 void QMapWithPointerKey::VisitDecl(clang::Decl *decl)
0030 {
0031     auto *tsdecl = Utils::templateSpecializationFromVarDecl(decl);
0032     if (!tsdecl || clazy::name(tsdecl) != "QMap") {
0033         return;
0034     }
0035 
0036     const TemplateArgumentList &templateArguments = tsdecl->getTemplateArgs();
0037     if (templateArguments.size() != 2) {
0038         return;
0039     }
0040 
0041     QualType qt = templateArguments[0].getAsType();
0042     const Type *t = qt.getTypePtrOrNull();
0043     if (t && t->isPointerType()) {
0044         emitWarning(decl->getBeginLoc(), "Use QHash<K,T> instead of QMap<K,T> when K is a pointer");
0045     }
0046 }