| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "mutable-container-key.h" |
| 8 |
#include "StringUtils.h" |
| 9 |
#include "Utils.h" |
| 10 |
#include "clazy_stl.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 |
#include <vector> |
| 19 |
|
| 20 |
using namespace clang; |
| 21 |
|
| 22 |
static bool isInterestingContainer(StringRef name) |
| 23 |
{ |
| 24 |
static const std::vector<StringRef> containers = {"QMap", "QHash"}; |
| 25 |
return clazy::contains(containers, name); |
| 26 |
} |
| 27 |
|
| 28 |
void MutableContainerKey::VisitDecl(clang::Decl *decl) |
| 29 |
{ |
| 30 |
auto *tsdecl = Utils::templateSpecializationFromVarDecl(decl); |
| 31 |
if (!tsdecl || !isInterestingContainer(clazy::name(tsdecl))) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
const TemplateArgumentList &templateArguments = tsdecl->getTemplateArgs(); |
| 36 |
if (templateArguments.size() != 2) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
QualType qt = templateArguments[0].getAsType(); |
| 41 |
const Type *t = qt.getTypePtrOrNull(); |
| 42 |
if (!t) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
auto *record = t->isRecordType() ? t->getAsCXXRecordDecl() : nullptr; |
| 47 |
if (!clazy::classIsOneOf(record, {"QPointer", "QWeakPointer", "QPersistentModelIndex", "weak_ptr"})) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
emitWarning(decl->getBeginLoc(), "Associative container key might be modified externally"); |
| 52 |
} |