Debian Codegraph @

src/checks/level0/temporary-iterator.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company [email protected]
3 SPDX-FileCopyrightText: 2015 Nyall Dawson <[email protected]>
4 SPDX-FileCopyrightText: 2015-2016 Sergio Martins <[email protected]>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8
9 #include "temporary-iterator.h"
10 #include "ClazyContext.h"
11 #include "HierarchyUtils.h"
12 #include "StringUtils.h"
13 #include "Utils.h"
14 #include "clazy_stl.h"
15
16 #include <clang/AST/Decl.h>
17 #include <clang/AST/DeclCXX.h>
18 #include <clang/AST/Expr.h>
19 #include <clang/AST/ExprCXX.h>
20 #include <clang/AST/OperationKinds.h>
21 #include <clang/AST/ParentMap.h>
22 #include <clang/AST/Stmt.h>
23 #include <clang/AST/Type.h>
24 #include <clang/Basic/LLVM.h>
25 #include <llvm/Support/Casting.h>
26
27 #include <utility>
28
29 using namespace clang;
30
31 TemporaryIterator::TemporaryIterator(const std::string &name, Options options)
32 : CheckBase(name, options)
33 {
34 m_methodsByType["vector"] = {"begin", "end", "cbegin", "cend"}; // TODO: More stl support
35 m_methodsByType["QList"] = {"begin", "end", "constBegin", "constEnd", "cbegin", "cend"};
36 m_methodsByType["QVector"] = {"begin", "end", "constBegin", "constEnd", "cbegin", "cend", "insert"};
37 m_methodsByType["QMap"] = {"begin", "end", "constBegin", "constEnd", "find", "constFind", "lowerBound", "upperBound", "cbegin", "cend", "equal_range"};
38 m_methodsByType["QHash"] = {"begin", "end", "constBegin", "constEnd", "cbegin", "cend", "find", "constFind", "insert", "insertMulti"};
39 m_methodsByType["QLinkedList"] = {"begin", "end", "constBegin", "constEnd", "cbegin", "cend"};
40 m_methodsByType["QSet"] = {"begin", "end", "constBegin", "constEnd", "find", "constFind", "cbegin", "cend"};
41 m_methodsByType["QStack"] = m_methodsByType["QVector"];
42 m_methodsByType["QQueue"] = m_methodsByType["QList"];
43 m_methodsByType["QMultiMap"] = m_methodsByType["QMap"];
44 m_methodsByType["QMultiHash"] = m_methodsByType["QHash"];
45 }
46
47 static bool isBlacklistedFunction(const std::string &name)
48 {
49 // These are fine
50 static const std::vector<std::string> list = {"QVariant::toList", "QHash::operator[]", "QMap::operator[]", "QSet::operator[]"};
51 return clazy::contains(list, name);
52 }
53
54 void TemporaryIterator::VisitStmt(clang::Stmt *stm)
55 {
56 auto *memberExpr = dyn_cast<CXXMemberCallExpr>(stm);
57 if (!memberExpr) {
58 return;
59 }
60
61 const CXXRecordDecl *classDecl = memberExpr->getRecordDecl();
62 CXXMethodDecl *methodDecl = memberExpr->getMethodDecl();
63 if (!classDecl || !methodDecl) {
64 return;
65 }
66
67 // Check if it's a container
68 auto it = m_methodsByType.find(clazy::name(classDecl));
69 if (it == m_methodsByType.end()) {
70 return;
71 }
72
73 // Check if it's a method returning an iterator
74 const StringRef functionName = clazy::name(methodDecl);
75 const auto &allowedFunctions = it->second;
76 if (!clazy::contains(allowedFunctions, functionName)) {
77 return;
78 }
79
80 // Catch getList().cbegin().value(), which is ok
81 if (clazy::getFirstParentOfType<CXXMemberCallExpr>(m_context->parentMap, m_context->parentMap->getParent(memberExpr))) {
82 return;
83 }
84
85 // Catch variant.toList().cbegin(), which is ok
86 auto *chainedMemberCall = clazy::getFirstChildOfType<CXXMemberCallExpr>(memberExpr);
87 if (chainedMemberCall) {
88 if (isBlacklistedFunction(clazy::qualifiedMethodName(chainedMemberCall->getMethodDecl()))) {
89 return;
90 }
91 }
92
93 // catch map[foo].cbegin()
94 auto *chainedOperatorCall = clazy::getFirstChildOfType<CXXOperatorCallExpr>(memberExpr);
95 if (chainedOperatorCall) {
96 FunctionDecl *func = chainedOperatorCall->getDirectCallee();
97 if (func) {
98 auto *method = dyn_cast<CXXMethodDecl>(func);
99 if (method) {
100 if (isBlacklistedFunction(clazy::qualifiedMethodName(method))) {
101 return;
102 }
103 }
104 }
105 }
106
107 // If we deref it within the expression, then we'll copy the value before the iterator becomes invalid, so it's safe
108 if (Utils::isInDerefExpression(memberExpr, m_context->parentMap)) {
109 return;
110 }
111
112 Expr *expr = memberExpr->getImplicitObjectArgument();
113 // 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
114 if (!expr || (expr->isLValue() && !isa<MaterializeTemporaryExpr>(expr))) {
115 return;
116 }
117
118 const Type *containerType = expr->getType().getTypePtrOrNull();
119 if (!containerType || containerType->isPointerType()) {
120 return;
121 }
122
123 {
124 // *really* check for rvalue
125 auto *impl = dyn_cast<ImplicitCastExpr>(expr);
126 if (impl) {
127 if (impl->getCastKind() == CK_LValueToRValue) {
128 return;
129 }
130
131 Stmt *firstChild = clazy::getFirstChild(impl);
132 if (llvm::isa_and_nonnull<ImplicitCastExpr>(firstChild) && dyn_cast<ImplicitCastExpr>(firstChild)->getCastKind() == CK_LValueToRValue) {
133 return;
134 }
135 }
136 }
137
138 auto *possibleCtorCall = dyn_cast_or_null<CXXConstructExpr>(clazy::getFirstChildAtDepth(expr, 2));
139 if (possibleCtorCall) {
140 return;
141 }
142
143 auto *possibleThisCall = dyn_cast_or_null<CXXThisExpr>(clazy::getFirstChildAtDepth(expr, 1));
144 if (possibleThisCall) {
145 return;
146 }
147
148 std::string error = "Don't call " + clazy::qualifiedMethodName(methodDecl) + "() on temporary";
149 emitWarning(stm->getBeginLoc(), error);
150 }