Debian Codegraph @

src/checks/level0/qmap-with-pointer-key.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company [email protected]
3 SPDX-FileContributor: Sérgio Martins <[email protected]>
4
5 SPDX-FileCopyrightText: 2015 Sergio Martins <[email protected]>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9
10 #include "qmap-with-pointer-key.h"
11 #include "StringUtils.h"
12 #include "Utils.h"
13
14 #include <clang/AST/DeclBase.h>
15 #include <clang/AST/DeclTemplate.h>
16 #include <clang/AST/TemplateBase.h>
17 #include <clang/AST/Type.h>
18 #include <llvm/ADT/StringRef.h>
19
20 using namespace clang;
21
22 void QMapWithPointerKey::VisitDecl(clang::Decl *decl)
23 {
24 auto *tsdecl = Utils::templateSpecializationFromVarDecl(decl);
25 if (!tsdecl || clazy::name(tsdecl) != "QMap") {
26 return;
27 }
28
29 const TemplateArgumentList &templateArguments = tsdecl->getTemplateArgs();
30 if (templateArguments.size() != 2) {
31 return;
32 }
33
34 QualType qt = templateArguments[0].getAsType();
35 const Type *t = qt.getTypePtrOrNull();
36 if (t && t->isPointerType()) {
37 emitWarning(decl->getBeginLoc(), "Use QHash<K,T> instead of QMap<K,T> when K is a pointer");
38 }
39 }