Debian Codegraph @

src/checks/level0/strict-iterators.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2017 Sergio Martins <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "strict-iterators.h"
8 #include "ClazyContext.h"
9 #include "QtUtils.h"
10 #include "StringUtils.h"
11 #include "TypeUtils.h"
12 #include "clazy_stl.h"
13
14 #include <clang/AST/Decl.h>
15 #include <clang/AST/DeclCXX.h>
16 #include <clang/AST/Expr.h>
17 #include <clang/AST/ExprCXX.h>
18 #include <clang/AST/OperationKinds.h>
19 #include <clang/AST/ParentMap.h>
20 #include <clang/AST/Stmt.h>
21 #include <clang/AST/Type.h>
22 #include <clang/Basic/LLVM.h>
23 #include <clang/Basic/SourceManager.h>
24 #include <clang/Frontend/CompilerInstance.h>
25 #include <llvm/ADT/StringRef.h>
26 #include <llvm/Support/Casting.h>
27
28 #include <cassert>
29
30 using namespace clang;
31
32 static bool isMemberVariable(Expr *expr)
33 {
34 if (isa<MemberExpr>(expr)) {
35 return true;
36 }
37
38 if (auto *ice = dyn_cast<ImplicitCastExpr>(expr)) {
39 return isMemberVariable(ice->getSubExpr());
40 }
41
42 return false;
43 }
44
45 // This got a bit messy since each Qt container produces a different AST, for example
46 // QVector::iterator isn't even a class, it's a typedef.
47
48 void StrictIterators::VisitStmt(clang::Stmt *stmt)
49 {
50 if (handleOperator(dyn_cast<CXXOperatorCallExpr>(stmt))) {
51 return;
52 }
53
54 // QVector's aren't actual classes, they are just typedefs to T* and const T*
55 handleImplicitCast(dyn_cast<ImplicitCastExpr>(stmt));
56 }
57
58 bool StrictIterators::handleImplicitCast(ImplicitCastExpr *implicitCast)
59 {
60 if (!implicitCast) {
61 return false;
62 }
63
64 const std::string nameTo = clazy::simpleTypeName(implicitCast->getType(), lo());
65
66 const QualType typeTo = implicitCast->getType();
67 CXXRecordDecl *recordTo = clazy::parentRecordForTypedef(typeTo);
68 const std::string qtNamespace = m_context->qtNamespace();
69 if (recordTo && !clazy::isQtCOWIterableClass(recordTo, qtNamespace)) {
70 return false;
71 }
72
73 recordTo = clazy::typeAsRecord(typeTo);
74 if (recordTo && !clazy::isQtCOWIterator(recordTo, qtNamespace)) {
75 return false;
76 }
77
78 assert(implicitCast->getSubExpr());
79
80 if (isMemberVariable(implicitCast->getSubExpr())) {
81 // Comparing a const_iterator against a member QVector<T>::iterator won't detach the container
82 return false;
83 }
84
85 QualType typeFrom = implicitCast->getSubExpr()->getType();
86 CXXRecordDecl *recordFrom = clazy::parentRecordForTypedef(typeFrom);
87 if (recordFrom && !clazy::isQtCOWIterableClass(recordFrom, qtNamespace)) {
88 return false;
89 }
90
91 // const_iterator might be a typedef to pointer, like const T *, instead of a class, so just check for const qualification in that case
92 if (!(clazy::pointeeQualType(typeTo).isConstQualified() || clazy::endsWith(nameTo, "const_iterator"))) {
93 return false;
94 }
95
96 // Allow conversions for mutating member functions of Qt container classes
97 if (implicitCast->getCastKind() == CK_ConstructorConversion) {
98 if (auto *memberCall = dyn_cast_or_null<CXXMemberCallExpr>(m_context->parentMap->getParent(implicitCast))) {
99 auto memberFunctionDecl = memberCall->getMethodDecl();
100 if (auto *parentClass = memberFunctionDecl->getParent()) {
101 static const std::vector<std::string> allow = {
102 "QMap<>::insert",
103 "QMap<>::erase",
104 "QHash<>::erase",
105 "QMultiHash<>::erase",
106 "QList<>::emplace",
107 "QList<>::erase",
108 "QList<>::insert",
109 "QVarLengthArray<>::emplace",
110 "QVarLengthArray<>::erase",
111 "QVarLengthArray<>::insert",
112 "QSet<>::erase",
113 "QSet<>::insert",
114 "QMultiMap<>::erase",
115 "QMultiMap<>::insert",
116 };
117
118 const auto qualifiedName = parentClass->getNameAsString() + "<>::" + memberFunctionDecl->getNameAsString();
119 if (clazy::contains(allow, qualifiedName)) {
120 return false;
121 }
122 }
123 }
124
125 emitWarning(implicitCast, "Mixing iterators with const_iterators");
126 return true;
127 }
128
129 // TODO: some util function to get the name of a nested class
130 const bool nameToIsIterator = nameTo == "iterator" || clazy::endsWith(nameTo, "::iterator");
131 if (nameToIsIterator) {
132 return false;
133 }
134
135 const std::string nameFrom = clazy::simpleTypeName(typeFrom, lo());
136 const bool nameFromIsIterator = nameFrom == "iterator" || clazy::endsWith(nameFrom, "::iterator");
137 if (!nameFromIsIterator) {
138 return false;
139 }
140
141 auto *p = m_context->parentMap->getParent(implicitCast);
142 if (isa<CXXOperatorCallExpr>(p)) {
143 return false;
144 }
145
146 emitWarning(implicitCast, "Mixing iterators with const_iterators");
147
148 return true;
149 }
150
151 bool StrictIterators::handleOperator(CXXOperatorCallExpr *op)
152 {
153 if (!op) {
154 return false;
155 }
156
157 auto *method = dyn_cast_or_null<CXXMethodDecl>(op->getDirectCallee());
158 if (!method || method->getNumParams() != 1) {
159 return false;
160 }
161
162 CXXRecordDecl *record = method->getParent();
163 if (!clazy::isQtCOWIterator(record, m_context->qtNamespace())) {
164 return false;
165 }
166
167 if (clazy::name(record) != "iterator") {
168 return false;
169 }
170
171 ParmVarDecl *p = method->getParamDecl(0);
172 const CXXRecordDecl *paramClass = p ? clazy::typeAsRecord(clazy::pointeeQualType(p->getType())) : nullptr;
173 if (!paramClass || clazy::name(paramClass) != "const_iterator") {
174 return false;
175 }
176
177 emitWarning(op, "Mixing iterators with const_iterators");
178 return true;
179 }