| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2015-2016 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "QtUtils.h" |
| 8 |
#include "FunctionUtils.h" |
| 9 |
#include "HierarchyUtils.h" |
| 10 |
#include "MacroUtils.h" |
| 11 |
#include "StringUtils.h" |
| 12 |
#include "TypeUtils.h" |
| 13 |
#include "Utils.h" |
| 14 |
#include "clazy_stl.h" |
| 15 |
|
| 16 |
#include <clang/AST/ExprCXX.h> |
| 17 |
#include <clang/Basic/LLVM.h> |
| 18 |
#include <llvm/ADT/ArrayRef.h> |
| 19 |
#include <llvm/Support/raw_ostream.h> |
| 20 |
|
| 21 |
using namespace clang; |
| 22 |
|
| 23 |
bool clazy::isQtIterableClass(clang::CXXRecordDecl *record) |
| 24 |
{ |
| 25 |
return record && isQtIterableClass(record->getQualifiedNameAsString()); |
| 26 |
} |
| 27 |
|
| 28 |
std::string trimQtNamespace(const std::string &name, const std::string qtNamespace) |
| 29 |
{ |
| 30 |
if (!qtNamespace.empty() && name.starts_with(qtNamespace + "::")) { |
| 31 |
std::string nameCopy = name; |
| 32 |
nameCopy.erase(0, qtNamespace.size() + 2); |
| 33 |
return nameCopy; |
| 34 |
} |
| 35 |
return name; |
| 36 |
} |
| 37 |
|
| 38 |
const std::vector<StringRef> &clazy::qtContainers() |
| 39 |
{ |
| 40 |
static const std::vector<StringRef> classes = { |
| 41 |
"QListSpecialMethods", |
| 42 |
"QListSpecialMethodsBase", // Qt6 |
| 43 |
"QList", |
| 44 |
"QVector", |
| 45 |
"QVarLengthArray", |
| 46 |
"QMap", |
| 47 |
"QHash", |
| 48 |
"QMultiMap", |
| 49 |
"QMultiHash", |
| 50 |
"QSet", |
| 51 |
"QStack", |
| 52 |
"QQueue", |
| 53 |
"QString", |
| 54 |
"QStringRef", |
| 55 |
"QByteArray", |
| 56 |
"QSequentialIterable", |
| 57 |
"QAssociativeIterable", |
| 58 |
"QJsonArray", |
| 59 |
"QJsonObject", |
| 60 |
"QLinkedList", |
| 61 |
}; |
| 62 |
return classes; |
| 63 |
} |
| 64 |
|
| 65 |
const std::vector<StringRef> &clazy::qtCOWContainers() |
| 66 |
{ |
| 67 |
static const std::vector<StringRef> classes = { |
| 68 |
"QListSpecialMethods", |
| 69 |
"QListSpecialMethodsBase", // Qt6 |
| 70 |
"QList", |
| 71 |
"QVector", |
| 72 |
"QMap", |
| 73 |
"QHash", |
| 74 |
"QMultiMap", |
| 75 |
"QMultiHash", |
| 76 |
"QSet", |
| 77 |
"QStack", |
| 78 |
"QQueue", |
| 79 |
"QString", |
| 80 |
"QStringRef", |
| 81 |
"QByteArray", |
| 82 |
"QJsonArray", |
| 83 |
"QJsonObject", |
| 84 |
"QLinkedList", |
| 85 |
}; |
| 86 |
return classes; |
| 87 |
} |
| 88 |
|
| 89 |
std::unordered_map<std::string, std::vector<StringRef>> clazy::detachingMethods() |
| 90 |
{ |
| 91 |
static std::unordered_map<std::string, std::vector<StringRef>> map; |
| 92 |
if (map.empty()) { |
| 93 |
const auto &withConst = detachingMethodsWithConstCounterParts(); |
| 94 |
map.reserve(withConst.size()); |
| 95 |
for (const auto &[qtClassname, methods] : withConst) { |
| 96 |
auto &dst = map[qtClassname]; |
| 97 |
dst.reserve(methods.size()); |
| 98 |
for (const auto &p : methods) |
| 99 |
dst.emplace_back(p.first); |
| 100 |
} |
| 101 |
|
| 102 |
map["QVector"].emplace_back("fill"); |
| 103 |
} |
| 104 |
|
| 105 |
return map; |
| 106 |
} |
| 107 |
|
| 108 |
std::unordered_map<std::string, std::vector<clazy::MemberConstcounterpartPair>> clazy::detachingMethodsWithConstCounterParts() |
| 109 |
{ |
| 110 |
static std::unordered_map<std::string, std::vector<std::pair<StringRef, StringRef>>> map; |
| 111 |
|
| 112 |
if (map.empty()) { |
| 113 |
using Vec = std::vector<clazy::MemberConstcounterpartPair>; |
| 114 |
const Vec seqApi = { |
| 115 |
{"first", "constFirst"}, |
| 116 |
{"last", "constLast"}, |
| 117 |
{"begin", "cbegin"}, |
| 118 |
{"end", "cend"}, |
| 119 |
{"rbegin", "crbegin"}, |
| 120 |
{"rend", "crend"}, |
| 121 |
{"front", "constFirst"}, |
| 122 |
{"back", "constLast"}, |
| 123 |
{"operator[]", "at"}, |
| 124 |
}; |
| 125 |
|
| 126 |
Vec vecApi = seqApi; |
| 127 |
vecApi.emplace_back("data", "constData"); |
| 128 |
|
| 129 |
const Vec mapApi = { |
| 130 |
{"begin", "cbegin"}, |
| 131 |
{"end", "cend"}, |
| 132 |
{"first", "constFirst"}, |
| 133 |
{"find", "constFind"}, |
| 134 |
{"last", "constLast"}, |
| 135 |
{"operator[]", "value"}, |
| 136 |
{"lowerBound", "lowerBound"}, // const overload exists, same name |
| 137 |
{"upperBound", "upperBound"}, |
| 138 |
{"keyValueBegin", "constKeyValueBegin"}, |
| 139 |
{"keyValueEnd", "constKeyValueEnd"}, |
| 140 |
}; |
| 141 |
|
| 142 |
map.emplace("QList", seqApi); |
| 143 |
map.emplace("QVector", vecApi); |
| 144 |
map.emplace("QLinkedList", seqApi); |
| 145 |
|
| 146 |
Vec stackApi = vecApi; |
| 147 |
stackApi.emplace_back("top", "top"); |
| 148 |
map.emplace("QStack", std::move(stackApi)); |
| 149 |
|
| 150 |
Vec queueApi = vecApi; |
| 151 |
queueApi.emplace_back("head", "head"); |
| 152 |
map.emplace("QQueue", std::move(queueApi)); |
| 153 |
|
| 154 |
map.emplace("QMap", mapApi); |
| 155 |
map.emplace("QMultiMap", mapApi); |
| 156 |
|
| 157 |
const Vec hashApi = {{"begin", "cbegin"}, {"end", "cend"}, {"find", "constFind"}, {"operator[]", "value"}}; |
| 158 |
map.emplace("QHash", hashApi); |
| 159 |
map.emplace("QMultiHash", hashApi); |
| 160 |
map.emplace("QSet", hashApi); |
| 161 |
|
| 162 |
const Vec stringApi = { |
| 163 |
{"begin", "cbegin"}, |
| 164 |
{"end", "cend"}, |
| 165 |
{"rbegin", "crbegin"}, |
| 166 |
{"rend", "crend"}, |
| 167 |
{"data", "constData"}, |
| 168 |
{"operator[]", "at"}, |
| 169 |
}; |
| 170 |
map.emplace("QString", stringApi); |
| 171 |
map.emplace("QByteArray", stringApi); |
| 172 |
|
| 173 |
map.emplace("QImage", Vec{{"bits", "constBits"}, {"scanLine", "constScanLine"}}); |
| 174 |
|
| 175 |
map.emplace("QJsonObject", Vec{{"begin", "cbegin"}, {"end", "cend"}, {"operator[]", "value"}, {"find", "constFind"}}); |
| 176 |
} |
| 177 |
|
| 178 |
return map; |
| 179 |
} |
| 180 |
|
| 181 |
bool clazy::isQMetaMethod(CallExpr *Call, unsigned int argIndex, const std::string &qtNamespace) |
| 182 |
{ |
| 183 |
Expr *arg = Call->getArg(argIndex); |
| 184 |
QualType type = arg->getType(); |
| 185 |
if (!type->isRecordType()) { |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
CXXRecordDecl *recordDecl = type->getAsCXXRecordDecl(); |
| 190 |
if (!recordDecl) { |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
return recordDecl->getQualifiedNameAsString() == clazy::qtNamespaced("QMetaMethod", qtNamespace); |
| 195 |
} |
| 196 |
|
| 197 |
bool clazy::isQtCOWIterableClass(clang::CXXRecordDecl *record, const std::string &qtNamespace) |
| 198 |
{ |
| 199 |
return record && isQtCOWIterableClass(record->getQualifiedNameAsString(), qtNamespace); |
| 200 |
} |
| 201 |
|
| 202 |
bool clazy::isQtCOWIterableClass(const std::string &className, const std::string &qtNamespace) |
| 203 |
{ |
| 204 |
const auto &classes = qtCOWContainers(); |
| 205 |
return clazy::contains(classes, trimQtNamespace(className, qtNamespace)); |
| 206 |
} |
| 207 |
|
| 208 |
bool clazy::isQtIterableClass(StringRef className) |
| 209 |
{ |
| 210 |
const auto &classes = qtContainers(); |
| 211 |
return clazy::contains(classes, className); |
| 212 |
} |
| 213 |
|
| 214 |
bool clazy::isQtAssociativeContainer(clang::CXXRecordDecl *record) |
| 215 |
{ |
| 216 |
return record && isQtAssociativeContainer(record->getNameAsString()); |
| 217 |
} |
| 218 |
|
| 219 |
bool clazy::isQtAssociativeContainer(StringRef className) |
| 220 |
{ |
| 221 |
static const std::vector<StringRef> classes = {"QSet", "QMap", "QHash", "QMultiHash", "QMultiMap"}; |
| 222 |
return clazy::contains(classes, className); |
| 223 |
} |
| 224 |
|
| 225 |
bool clazy::isQObject(const CXXRecordDecl *decl, const std::string &qtNamespace) |
| 226 |
{ |
| 227 |
return clazy::derivesFrom(decl, clazy::qtNamespaced("QObject", qtNamespace)); |
| 228 |
} |
| 229 |
bool clazy::isSharedPointer(CXXRecordDecl *record, const std::string &qtNamespace) |
| 230 |
{ |
| 231 |
static const std::vector<std::string> names = {"std::shared_ptr", "QSharedPointer", "boost::shared_ptr"}; |
| 232 |
return record ? clazy::contains(names, trimQtNamespace(record->getQualifiedNameAsString(), qtNamespace)) : false; |
| 233 |
} |
| 234 |
|
| 235 |
bool clazy::isConvertibleTo(const Type *source, const Type *target) |
| 236 |
{ |
| 237 |
if (!source || !target) { |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
if (source->isPointerType() ^ target->isPointerType()) { |
| 242 |
return false; |
| 243 |
} |
| 244 |
|
| 245 |
if (source == target) { |
| 246 |
return true; |
| 247 |
} |
| 248 |
|
| 249 |
if (source->getPointeeCXXRecordDecl() && source->getPointeeCXXRecordDecl() == target->getPointeeCXXRecordDecl()) { |
| 250 |
return true; |
| 251 |
} |
| 252 |
|
| 253 |
if (source->isIntegerType() && target->isIntegerType()) { |
| 254 |
return true; |
| 255 |
} |
| 256 |
|
| 257 |
if (source->isFloatingType() && target->isFloatingType()) { |
| 258 |
return true; |
| 259 |
} |
| 260 |
|
| 261 |
// "QString" can convert to "const QString &" and vice versa |
| 262 |
if (clazy::isConstRef(source) && source->getPointeeType().getTypePtrOrNull() == target) { |
| 263 |
return true; |
| 264 |
} |
| 265 |
if (clazy::isConstRef(target) && target->getPointeeType().getTypePtrOrNull() == source) { |
| 266 |
return true; |
| 267 |
} |
| 268 |
|
| 269 |
return false; |
| 270 |
} |
| 271 |
|
| 272 |
bool clazy::isJavaIterator(CXXRecordDecl *record) |
| 273 |
{ |
| 274 |
if (!record) { |
| 275 |
return false; |
| 276 |
} |
| 277 |
|
| 278 |
static const std::vector<StringRef> names = { |
| 279 |
"QHashIterator", |
| 280 |
"QMapIterator", |
| 281 |
"QSetIterator", |
| 282 |
"QListIterator", |
| 283 |
"QVectorIterator", // typedef in Qt6 |
| 284 |
"QStringListIterator", // typedef in Qt6 |
| 285 |
"QLinkedListIterator", // removed in Qt6 |
| 286 |
}; |
| 287 |
|
| 288 |
return clazy::contains(names, clazy::name(record)); |
| 289 |
} |
| 290 |
|
| 291 |
bool clazy::isJavaIterator(CXXMemberCallExpr *call) |
| 292 |
{ |
| 293 |
return call && isJavaIterator(call->getRecordDecl()); |
| 294 |
} |
| 295 |
|
| 296 |
bool clazy::isQtContainer(QualType t) |
| 297 |
{ |
| 298 |
const CXXRecordDecl *record = clazy::typeAsRecord(t); |
| 299 |
return record && isQtContainer(record); |
| 300 |
} |
| 301 |
|
| 302 |
bool clazy::isQtContainer(const CXXRecordDecl *record) |
| 303 |
{ |
| 304 |
const StringRef typeName = clazy::name(record); |
| 305 |
return std::ranges::any_of(clazy::qtContainers(), [typeName](StringRef container) { |
| 306 |
return container == typeName; |
| 307 |
}); |
| 308 |
} |
| 309 |
|
| 310 |
bool clazy::isAReserveClass(CXXRecordDecl *recordDecl, const std::string &qtNamespace) |
| 311 |
{ |
| 312 |
if (!recordDecl) { |
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
static const std::vector<std::string> classes = { |
| 317 |
clazy::qtNamespaced("QVector", qtNamespace), |
| 318 |
"std::vector", |
| 319 |
clazy::qtNamespaced("QList", qtNamespace), |
| 320 |
clazy::qtNamespaced("QSet", qtNamespace), |
| 321 |
}; |
| 322 |
return std::ranges::any_of(classes, [recordDecl](const std::string &className) { |
| 323 |
return clazy::derivesFrom(recordDecl, className); |
| 324 |
}); |
| 325 |
} |
| 326 |
|
| 327 |
clang::CXXRecordDecl *clazy::getQObjectBaseClass(clang::CXXRecordDecl *recordDecl, const std::string &qtNamespace) |
| 328 |
{ |
| 329 |
if (!recordDecl) { |
| 330 |
return nullptr; |
| 331 |
} |
| 332 |
|
| 333 |
for (auto baseClass : recordDecl->bases()) { |
| 334 |
if (CXXRecordDecl *record = clazy::recordFromBaseSpecifier(baseClass); isQObject(record, qtNamespace)) { |
| 335 |
return record; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
return nullptr; |
| 340 |
} |
| 341 |
|
| 342 |
bool clazy::isConnect(FunctionDecl *func, const std::string &qtNamespace) |
| 343 |
{ |
| 344 |
return func && func->getQualifiedNameAsString() == clazy::qtNamespaced("QObject::connect", qtNamespace); |
| 345 |
} |
| 346 |
|
| 347 |
bool clazy::connectHasPMFStyle(FunctionDecl *func) |
| 348 |
{ |
| 349 |
// Look for char* arguments |
| 350 |
for (auto *param : Utils::functionParameters(func)) { |
| 351 |
QualType qt = param->getType(); |
| 352 |
const Type *t = qt.getTypePtrOrNull(); |
| 353 |
if (!t || !t->isPointerType()) { |
| 354 |
continue; |
| 355 |
} |
| 356 |
|
| 357 |
const Type *ptt = t->getPointeeType().getTypePtrOrNull(); |
| 358 |
if (ptt && ptt->isCharType()) { |
| 359 |
return false; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
return true; |
| 364 |
} |
| 365 |
|
| 366 |
CXXMethodDecl *clazy::pmfFromConnect(CallExpr *funcCall, int argIndex, const std::string &qtNamespace) |
| 367 |
{ |
| 368 |
if (!funcCall) { |
| 369 |
return nullptr; |
| 370 |
} |
| 371 |
|
| 372 |
const int numArgs = funcCall->getNumArgs(); |
| 373 |
if (numArgs < 3) { |
| 374 |
llvm::errs() << "error, connect call has less than 3 arguments\n"; |
| 375 |
return nullptr; |
| 376 |
} |
| 377 |
|
| 378 |
if (argIndex >= numArgs) { |
| 379 |
return nullptr; |
| 380 |
} |
| 381 |
|
| 382 |
Expr *expr = funcCall->getArg(argIndex); |
| 383 |
if (auto casted = dyn_cast<ImplicitCastExpr>(expr)) { |
| 384 |
if (auto *declRef = dyn_cast<DeclRefExpr>(casted->getSubExpr())) { |
| 385 |
if (auto *varDecl = dyn_cast<VarDecl>(declRef->getDecl())) { |
| 386 |
auto *varDeclInit = varDecl->getInit(); |
| 387 |
// In Qt5, we have multiple DeclRefExprs, only the last one is actually relevant for determining if we have a signal or not |
| 388 |
std::vector<DeclRefExpr *> res; |
| 389 |
clazy::getChilds<DeclRefExpr>(varDeclInit, res); |
| 390 |
if (!res.empty()) { |
| 391 |
return dyn_cast<CXXMethodDecl>(res.at(res.size() - 1)->getFoundDecl()); |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
return pmfFromExpr(expr, qtNamespace); |
| 397 |
} |
| 398 |
|
| 399 |
CXXMethodDecl *clazy::pmfFromExpr(Expr *expr, const std::string &qtNamespace) |
| 400 |
{ |
| 401 |
if (auto *uo = dyn_cast<UnaryOperator>(expr)) { |
| 402 |
return pmfFromUnary(uo); |
| 403 |
} |
| 404 |
if (auto *call = dyn_cast<CXXOperatorCallExpr>(expr)) { |
| 405 |
if (call->getNumArgs() <= 1) { |
| 406 |
return nullptr; |
| 407 |
} |
| 408 |
|
| 409 |
FunctionDecl *func = call->getDirectCallee(); |
| 410 |
if (!func) { |
| 411 |
return nullptr; |
| 412 |
} |
| 413 |
|
| 414 |
auto *context = func->getParent(); |
| 415 |
if (!context) { |
| 416 |
return nullptr; |
| 417 |
} |
| 418 |
|
| 419 |
auto *record = dyn_cast<CXXRecordDecl>(context); |
| 420 |
if (!record) { |
| 421 |
return nullptr; |
| 422 |
} |
| 423 |
|
| 424 |
const std::string className = record->getQualifiedNameAsString(); |
| 425 |
if (className != clazy::qtNamespaced("QNonConstOverload", qtNamespace) && className != clazy::qtNamespaced("QConstOverload", qtNamespace)) { |
| 426 |
return nullptr; |
| 427 |
} |
| 428 |
|
| 429 |
return pmfFromUnary(dyn_cast<UnaryOperator>(call->getArg(1))); |
| 430 |
} else if (auto *staticCast = dyn_cast<CXXStaticCastExpr>(expr)) { |
| 431 |
return pmfFromExpr(staticCast->getSubExpr(), qtNamespace); |
| 432 |
} else if (auto *callexpr = dyn_cast<CallExpr>(expr)) { |
| 433 |
// QOverload case, go deeper one level to get to the UnaryOperator |
| 434 |
if (callexpr->getNumArgs() == 1) { |
| 435 |
return pmfFromExpr(callexpr->getArg(0), qtNamespace); |
| 436 |
} |
| 437 |
} else if (auto *matTempExpr = dyn_cast<MaterializeTemporaryExpr>(expr)) { |
| 438 |
// Qt6 PMF, go deeper one level to get to the UnaryOperator |
| 439 |
return pmfFromExpr(matTempExpr->getSubExpr(), qtNamespace); |
| 440 |
} else if (auto *impl = dyn_cast<ImplicitCastExpr>(expr)) { |
| 441 |
return pmfFromExpr(impl->getSubExpr(), qtNamespace); |
| 442 |
} |
| 443 |
|
| 444 |
return nullptr; |
| 445 |
} |
| 446 |
|
| 447 |
CXXMethodDecl *clazy::pmfFromUnary(UnaryOperator *uo) |
| 448 |
{ |
| 449 |
if (!uo) { |
| 450 |
return nullptr; |
| 451 |
} |
| 452 |
|
| 453 |
Expr *subExpr = uo->getSubExpr(); |
| 454 |
if (!subExpr) { |
| 455 |
return nullptr; |
| 456 |
} |
| 457 |
|
| 458 |
auto *declref = dyn_cast<DeclRefExpr>(subExpr); |
| 459 |
|
| 460 |
if (declref) { |
| 461 |
return dyn_cast<CXXMethodDecl>(declref->getDecl()); |
| 462 |
} |
| 463 |
|
| 464 |
return nullptr; |
| 465 |
} |
| 466 |
|
| 467 |
bool clazy::recordHasCtorWithParam(clang::CXXRecordDecl *record, const std::string ¶mType, bool &ok, int &numCtors) |
| 468 |
{ |
| 469 |
ok = true; |
| 470 |
numCtors = 0; |
| 471 |
if (!record || !record->hasDefinition() || record->getDefinition() != record) { // Means fwd decl |
| 472 |
ok = false; |
| 473 |
return false; |
| 474 |
} |
| 475 |
|
| 476 |
for (auto *ctor : record->ctors()) { |
| 477 |
if (ctor->isCopyOrMoveConstructor()) { |
| 478 |
continue; |
| 479 |
} |
| 480 |
numCtors++; |
| 481 |
for (auto *param : ctor->parameters()) { |
| 482 |
QualType qt = clazy::pointeeQualType(param->getType()); |
| 483 |
if (!qt.isConstQualified() && clazy::derivesFrom(qt, paramType)) { |
| 484 |
return true; |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
return false; |
| 490 |
} |
| 491 |
|
| 492 |
clang::ValueDecl *clazy::signalReceiverForConnect(clang::CallExpr *call) |
| 493 |
{ |
| 494 |
if (!call || call->getNumArgs() < 5) { |
| 495 |
return nullptr; |
| 496 |
} |
| 497 |
|
| 498 |
return clazy::valueDeclForCallArgument(call, 2); |
| 499 |
} |
| 500 |
|
| 501 |
clang::ValueDecl *clazy::signalSenderForConnect(clang::CallExpr *call) |
| 502 |
{ |
| 503 |
return clazy::valueDeclForCallArgument(call, 0); |
| 504 |
} |
| 505 |
|
| 506 |
bool clazy::isBootstrapping(const clang::PreprocessorOptions &ppOpts) |
| 507 |
{ |
| 508 |
return clazy::isPredefined(ppOpts, "QT_BOOTSTRAPPED"); |
| 509 |
} |
| 510 |
|
| 511 |
bool clazy::isInForeach(const clang::ASTContext *context, clang::SourceLocation loc) |
| 512 |
{ |
| 513 |
return clazy::isInAnyMacro(context, loc, {"Q_FOREACH", "foreach"}); |
| 514 |
} |