| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2018 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "qhash-with-char-pointer-key.h" |
| 8 |
#include "StringUtils.h" |
| 9 |
#include "TypeUtils.h" |
| 10 |
#include "Utils.h" |
| 11 |
|
| 12 |
#include <clang/AST/DeclBase.h> |
| 13 |
#include <clang/AST/DeclTemplate.h> |
| 14 |
#include <clang/AST/TemplateBase.h> |
| 15 |
#include <clang/AST/Type.h> |
| 16 |
#include <llvm/ADT/StringRef.h> |
| 17 |
|
| 18 |
using namespace clang; |
| 19 |
|
| 20 |
void QHashWithCharPointerKey::VisitDecl(clang::Decl *decl) |
| 21 |
{ |
| 22 |
auto *tsdecl = Utils::templateSpecializationFromVarDecl(decl); |
| 23 |
if (!tsdecl || clazy::name(tsdecl) != "QHash") { // For QMap you shouldn't use any kind of pointers, that's handled in another check |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
const TemplateArgumentList &templateArguments = tsdecl->getTemplateArgs(); |
| 28 |
if (templateArguments.size() != 2) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
QualType qt = templateArguments[0].getAsType(); |
| 33 |
if (!qt.isNull() && qt->isPointerType()) { |
| 34 |
qt = clazy::pointeeQualType(qt); |
| 35 |
if (!qt.isNull() && !qt->isPointerType() && qt->isCharType()) { |
| 36 |
emitWarning(decl->getBeginLoc(), "Using QHash<const char *, T> is dangerous"); |
| 37 |
} |
| 38 |
} |
| 39 |
} |