| 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 "qvariant-template-instantiation.h" |
| 11 |
#include "StringUtils.h" |
| 12 |
#include "TemplateUtils.h" |
| 13 |
#include "clazy_stl.h" |
| 14 |
|
| 15 |
#include <clang/AST/DeclCXX.h> |
| 16 |
#include <clang/AST/ExprCXX.h> |
| 17 |
#include <clang/AST/Stmt.h> |
| 18 |
#include <clang/AST/Type.h> |
| 19 |
#include <clang/Basic/LLVM.h> |
| 20 |
#include <llvm/ADT/StringRef.h> |
| 21 |
#include <llvm/Support/Casting.h> |
| 22 |
|
| 23 |
#include <cctype> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
using namespace clang; |
| 27 |
|
| 28 |
static bool isMatchingClass(StringRef name) |
| 29 |
{ |
| 30 |
static const std::vector<StringRef> classes = { |
| 31 |
"QBitArray", "QByteArray", "QChar", "QDate", "QDateTime", "QEasingCurve", "QJsonArray", "QJsonDocument", "QJsonObject", |
| 32 |
"QJsonValue", "QLocale", "QModelIndex", "QPoint", "QPointF", "QRect", "QRectF", "QRegExp", "QString", |
| 33 |
"QRegularExpression", "QSize", "QSizeF", "QStringList", "QTime", "QUrl", "QUuid", |
| 34 |
}; |
| 35 |
|
| 36 |
return clazy::contains(classes, name); |
| 37 |
} |
| 38 |
|
| 39 |
void QVariantTemplateInstantiation::VisitStmt(clang::Stmt *stm) |
| 40 |
{ |
| 41 |
auto *callExpr = dyn_cast<CXXMemberCallExpr>(stm); |
| 42 |
if (!callExpr) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
CXXMethodDecl *methodDecl = callExpr->getMethodDecl(); |
| 47 |
if (!methodDecl || clazy::name(methodDecl) != "value") { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
const auto *memberExpr = dyn_cast<MemberExpr>(callExpr->getCallee()); |
| 52 |
if (!memberExpr) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
const auto *decl = memberExpr->getBase()->getType()->getAsCXXRecordDecl(); |
| 57 |
if (!decl || !decl->getDefinition() |
| 58 |
|| !(decl->getNameAsString() == "QVariant" || decl->getNameAsString() == "QHash" || decl->getNameAsString() == "QMap")) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
if (const auto *specDecl = dyn_cast<ClassTemplateSpecializationDecl>(decl)) { |
| 63 |
if (const auto *templateDecl = specDecl->getSpecializedTemplate()) { |
| 64 |
if (templateDecl->getNameAsString() == "QHash" || templateDecl->getNameAsString() == "QMap") { |
| 65 |
const TemplateArgumentList &templateArgs = specDecl->getTemplateArgs(); |
| 66 |
if (!(templateArgs.size() == 2) || !(templateArgs.get(1).getAsType().getAsString() == "QVariant")) { |
| 67 |
return; |
| 68 |
} |
| 69 |
} else if (templateDecl->getNameAsString() == "QList") { |
| 70 |
const TemplateArgumentList &templateArgs = specDecl->getTemplateArgs(); |
| 71 |
if (!(templateArgs.size() == 1) || !(templateArgs.get(0).getAsType().getAsString() == "QVariant")) { |
| 72 |
return; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
std::vector<QualType> typeList = clazy::getTemplateArgumentsTypes(methodDecl); |
| 79 |
const Type *t = typeList.empty() ? nullptr : typeList[0].getTypePtrOrNull(); |
| 80 |
if (!t) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
bool matches = false; |
| 85 |
if (t->isBooleanType() || t->isFloatingType() || (t->isIntegerType() && !t->isEnumeralType())) { |
| 86 |
matches = true; |
| 87 |
} else { |
| 88 |
const CXXRecordDecl *recordDecl = t->getAsCXXRecordDecl(); |
| 89 |
matches = recordDecl && t->isClassType() && isMatchingClass(clazy::name(recordDecl)); |
| 90 |
} |
| 91 |
|
| 92 |
if (matches) { |
| 93 |
std::string typeName = trimQtNamespace(clazy::simpleTypeName(typeList[0], lo())); |
| 94 |
std::string typeName2 = typeName; |
| 95 |
typeName2[0] = toupper(typeName2[0]); |
| 96 |
|
| 97 |
if (typeName[0] == 'Q') { |
| 98 |
typeName2.erase(0, 1); // Remove first letter |
| 99 |
} |
| 100 |
std::string error = std::string("Use QVariant::to" + typeName2 + "() instead of QVariant::value<" + typeName + ">()"); |
| 101 |
emitWarning(stm->getBeginLoc(), error); |
| 102 |
} |
| 103 |
} |