/* SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company info@kdab.com SPDX-FileCopyrightText: 2015 Nyall Dawson SPDX-FileCopyrightText: 2015-2016 Sergio Martins SPDX-License-Identifier: LGPL-2.0-or-later */ #include "temporary-iterator.h" #include "ClazyContext.h" #include "HierarchyUtils.h" #include "StringUtils.h" #include "Utils.h" #include "clazy_stl.h" #include #include #include #include #include #include #include #include #include #include #include using namespace clang; TemporaryIterator::TemporaryIterator(const std::string &name, Options options) : CheckBase(name, options) { m_methodsByType["vector"] = {"begin", "end", "cbegin", "cend"}; // TODO: More stl support m_methodsByType["QList"] = {"begin", "end", "constBegin", "constEnd", "cbegin", "cend"}; m_methodsByType["QVector"] = {"begin", "end", "constBegin", "constEnd", "cbegin", "cend", "insert"}; m_methodsByType["QMap"] = {"begin", "end", "constBegin", "constEnd", "find", "constFind", "lowerBound", "upperBound", "cbegin", "cend", "equal_range"}; m_methodsByType["QHash"] = {"begin", "end", "constBegin", "constEnd", "cbegin", "cend", "find", "constFind", "insert", "insertMulti"}; m_methodsByType["QLinkedList"] = {"begin", "end", "constBegin", "constEnd", "cbegin", "cend"}; m_methodsByType["QSet"] = {"begin", "end", "constBegin", "constEnd", "find", "constFind", "cbegin", "cend"}; m_methodsByType["QStack"] = m_methodsByType["QVector"]; m_methodsByType["QQueue"] = m_methodsByType["QList"]; m_methodsByType["QMultiMap"] = m_methodsByType["QMap"]; m_methodsByType["QMultiHash"] = m_methodsByType["QHash"]; } static bool isBlacklistedFunction(const std::string &name) { // These are fine static const std::vector list = {"QVariant::toList", "QHash::operator[]", "QMap::operator[]", "QSet::operator[]"}; return clazy::contains(list, name); } void TemporaryIterator::VisitStmt(clang::Stmt *stm) { auto *memberExpr = dyn_cast(stm); if (!memberExpr) { return; } const CXXRecordDecl *classDecl = memberExpr->getRecordDecl(); CXXMethodDecl *methodDecl = memberExpr->getMethodDecl(); if (!classDecl || !methodDecl) { return; } // Check if it's a container auto it = m_methodsByType.find(clazy::name(classDecl)); if (it == m_methodsByType.end()) { return; } // Check if it's a method returning an iterator const StringRef functionName = clazy::name(methodDecl); const auto &allowedFunctions = it->second; if (!clazy::contains(allowedFunctions, functionName)) { return; } // Catch getList().cbegin().value(), which is ok if (clazy::getFirstParentOfType(m_context->parentMap, m_context->parentMap->getParent(memberExpr))) { return; } // Catch variant.toList().cbegin(), which is ok auto *chainedMemberCall = clazy::getFirstChildOfType(memberExpr); if (chainedMemberCall) { if (isBlacklistedFunction(clazy::qualifiedMethodName(chainedMemberCall->getMethodDecl()))) { return; } } // catch map[foo].cbegin() auto *chainedOperatorCall = clazy::getFirstChildOfType(memberExpr); if (chainedOperatorCall) { FunctionDecl *func = chainedOperatorCall->getDirectCallee(); if (func) { auto *method = dyn_cast(func); if (method) { if (isBlacklistedFunction(clazy::qualifiedMethodName(method))) { return; } } } } // If we deref it within the expression, then we'll copy the value before the iterator becomes invalid, so it's safe if (Utils::isInDerefExpression(memberExpr, m_context->parentMap)) { return; } Expr *expr = memberExpr->getImplicitObjectArgument(); // This check is about detaching temporaries, so check for r value. But with clang20, the same code seems to be an l-value. Check type of expression instead if (!expr || (expr->isLValue() && !isa(expr))) { return; } const Type *containerType = expr->getType().getTypePtrOrNull(); if (!containerType || containerType->isPointerType()) { return; } { // *really* check for rvalue auto *impl = dyn_cast(expr); if (impl) { if (impl->getCastKind() == CK_LValueToRValue) { return; } Stmt *firstChild = clazy::getFirstChild(impl); if (llvm::isa_and_nonnull(firstChild) && dyn_cast(firstChild)->getCastKind() == CK_LValueToRValue) { return; } } } auto *possibleCtorCall = dyn_cast_or_null(clazy::getFirstChildAtDepth(expr, 2)); if (possibleCtorCall) { return; } auto *possibleThisCall = dyn_cast_or_null(clazy::getFirstChildAtDepth(expr, 1)); if (possibleThisCall) { return; } std::string error = "Don't call " + clazy::qualifiedMethodName(methodDecl) + "() on temporary"; emitWarning(stm->getBeginLoc(), error); }