| 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 "detaching-temporary.h" |
| 11 |
#include "QtUtils.h" |
| 12 |
#include "StringUtils.h" |
| 13 |
#include "Utils.h" |
| 14 |
#include "checkbase.h" |
| 15 |
#include "clazy_stl.h" |
| 16 |
|
| 17 |
#include <clang/AST/Decl.h> |
| 18 |
#include <clang/AST/DeclCXX.h> |
| 19 |
#include <clang/AST/Expr.h> |
| 20 |
#include <clang/AST/Stmt.h> |
| 21 |
#include <clang/AST/Type.h> |
| 22 |
#include <clang/Basic/LLVM.h> |
| 23 |
#include <llvm/Support/Casting.h> |
| 24 |
|
| 25 |
#include <unordered_map> |
| 26 |
#include <utility> |
| 27 |
|
| 28 |
using namespace clang; |
| 29 |
|
| 30 |
DetachingTemporary::DetachingTemporary(const std::string &name, Options options) |
| 31 |
: DetachingBase(name, options) |
| 32 |
{ |
| 33 |
// Extra stuff that isn't really related to detachments but doesn't make sense to call on temporaries |
| 34 |
m_writeMethodsByType["QString"] = {"push_back", "push_front", "clear", "chop"}; |
| 35 |
m_writeMethodsByType["QList"] = {"takeAt", "takeFirst", "takeLast", "removeOne", "removeAll", "erase"}; |
| 36 |
m_writeMethodsByType["QVector"] = {"fill", "insert"}; |
| 37 |
m_writeMethodsByType["QMap"] = {"erase", "insert", "insertMulti", "remove", "take"}; |
| 38 |
m_writeMethodsByType["QHash"] = {"erase", "insert", "insertMulti", "remove", "take"}; |
| 39 |
m_writeMethodsByType["QMultiHash"] = m_writeMethodsByType["QHash"]; |
| 40 |
m_writeMethodsByType["QMultiMap"] = m_writeMethodsByType["QMap"]; |
| 41 |
m_writeMethodsByType["QLinkedList"] = {"takeFirst", "takeLast", "removeOne", "removeAll", "erase"}; |
| 42 |
m_writeMethodsByType["QSet"] = {"erase", "insert"}; |
| 43 |
m_writeMethodsByType["QStack"] = {"push", "swap"}; |
| 44 |
m_writeMethodsByType["QQueue"] = {"enqueue", "swap"}; |
| 45 |
m_writeMethodsByType["QListSpecialMethods"] = {"sort", "replaceInStrings", "removeDuplicates"}; |
| 46 |
m_writeMethodsByType["QStringList"] = m_writeMethodsByType["QListSpecialMethods"]; |
| 47 |
} |
| 48 |
|
| 49 |
bool isAllowedChainedClass(const std::string &className) |
| 50 |
{ |
| 51 |
// Members of those classes return values that have refCount of 1, so no detaching actually happens |
| 52 |
static const std::vector<std::string> allowed = {"QString", "QByteArray"}; |
| 53 |
return clazy::contains(allowed, className); |
| 54 |
} |
| 55 |
|
| 56 |
bool isAllowedChainedMethod(const std::string &methodName) |
| 57 |
{ |
| 58 |
static const std::vector<std::string> allowed = { |
| 59 |
"QMap::keys", |
| 60 |
"QMap::values", |
| 61 |
"QHash::keys", |
| 62 |
"QMap::values", |
| 63 |
"QApplication::topLevelWidgets", |
| 64 |
"QAbstractItemView::selectedIndexes", |
| 65 |
"QListWidget::selectedItems", |
| 66 |
"QFile::encodeName", |
| 67 |
"QFile::decodeName", |
| 68 |
"QItemSelectionModel::selectedRows", |
| 69 |
"QTreeWidget::selectedItems", |
| 70 |
"QTableWidget::selectedItems", |
| 71 |
"QNetworkReply::rawHeaderList", |
| 72 |
"Mailbox::address", |
| 73 |
"QItemSelection::indexes", |
| 74 |
"QItemSelectionModel::selectedIndexes", |
| 75 |
"QMimeData::formats", |
| 76 |
"i18n", |
| 77 |
"QAbstractTransition::targetStates", |
| 78 |
}; |
| 79 |
return clazy::contains(allowed, methodName); |
| 80 |
} |
| 81 |
|
| 82 |
void DetachingTemporary::VisitStmt(clang::Stmt *stm) |
| 83 |
{ |
| 84 |
auto *callExpr = dyn_cast<CallExpr>(stm); // Member or operator call |
| 85 |
if (!callExpr) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
// For a chain like getList().first(), returns {first(), getList()} |
| 90 |
std::vector<CallExpr *> callExprs = Utils::callListForChain(callExpr); // callExpr would be first() |
| 91 |
if (callExprs.size() < 2) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
CallExpr *firstCallToBeEvaluated = callExprs.at(1); // This is the call to getList() |
| 96 |
FunctionDecl *firstFunc = firstCallToBeEvaluated->getDirectCallee(); |
| 97 |
if (!firstFunc) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
QualType qt = firstFunc->getReturnType(); |
| 102 |
const Type *firstFuncReturnType = qt.getTypePtrOrNull(); |
| 103 |
if (!firstFuncReturnType) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
if (firstFuncReturnType->isReferenceType() || firstFuncReturnType->isPointerType()) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
if (qt.isConstQualified()) { |
| 112 |
return; // const doesn't detach |
| 113 |
} |
| 114 |
|
| 115 |
auto *firstMethod = dyn_cast<CXXMethodDecl>(firstFunc); |
| 116 |
if (isAllowedChainedMethod(clazy::qualifiedMethodName(firstFunc))) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
if (firstMethod && isAllowedChainedClass(firstMethod->getParent()->getNameAsString())) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
// Check if this is a QGlobalStatic |
| 125 |
if (firstMethod && clazy::name(firstMethod->getParent()) == "QGlobalStatic") { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
CallExpr *secondCallToBeEvaluated = callExprs.at(0); // This is the call to first(), this might be the detaching call |
| 130 |
FunctionDecl *detachingFunc = secondCallToBeEvaluated->getDirectCallee(); |
| 131 |
auto *detachingMethod = detachingFunc ? dyn_cast<CXXMethodDecl>(detachingFunc) : nullptr; |
| 132 |
const Type *detachingMethodReturnType = detachingMethod ? detachingMethod->getReturnType().getTypePtrOrNull() : nullptr; |
| 133 |
if (!detachingMethod || !detachingMethodReturnType) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
// Check if it's one of the implicit shared classes |
| 138 |
const CXXRecordDecl *classDecl = detachingMethod->getParent(); |
| 139 |
StringRef className = clazy::name(classDecl); |
| 140 |
|
| 141 |
const std::unordered_map<std::string, std::vector<StringRef>> &methodsByType = clazy::detachingMethods(); |
| 142 |
auto it = methodsByType.find(className.str()); |
| 143 |
auto it2 = m_writeMethodsByType.find(className); |
| 144 |
|
| 145 |
std::vector<StringRef> allowedFunctions; |
| 146 |
std::vector<StringRef> allowedWriteFunctions; |
| 147 |
if (it != methodsByType.end()) { |
| 148 |
allowedFunctions = it->second; |
| 149 |
} |
| 150 |
|
| 151 |
if (it2 != m_writeMethodsByType.end()) { |
| 152 |
allowedWriteFunctions = it2->second; |
| 153 |
} |
| 154 |
|
| 155 |
// Check if it's one of the detaching methods |
| 156 |
StringRef functionName = clazy::name(detachingMethod); |
| 157 |
|
| 158 |
std::string error; |
| 159 |
|
| 160 |
const bool isReadFunction = clazy::contains(allowedFunctions, functionName); |
| 161 |
const bool isWriteFunction = clazy::contains(allowedWriteFunctions, functionName); |
| 162 |
|
| 163 |
if (isReadFunction || isWriteFunction) { |
| 164 |
bool returnTypeIsIterator = false; |
| 165 |
if (const CXXRecordDecl *returnRecord = detachingMethodReturnType->getAsCXXRecordDecl()) { |
| 166 |
returnTypeIsIterator = clazy::name(returnRecord) == "iterator"; |
| 167 |
} |
| 168 |
|
| 169 |
if (isWriteFunction && (detachingMethodReturnType->isVoidType() || returnTypeIsIterator)) { |
| 170 |
error = std::string("Modifying temporary container is pointless and it also detaches"); |
| 171 |
} else { |
| 172 |
error = std::string("Don't call ") + clazy::qualifiedMethodName(detachingMethod) + std::string("() on temporary"); |
| 173 |
} |
| 174 |
|
| 175 |
std::vector<FixItHint> hints = getFixitHints(className, functionName, secondCallToBeEvaluated); |
| 176 |
emitWarning(stm->getBeginLoc(), error, hints); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
bool DetachingTemporary::isDetachingMethod(CXXMethodDecl *method) const |
| 181 |
{ |
| 182 |
if (!method) { |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
const CXXRecordDecl *record = method->getParent(); |
| 187 |
if (!record) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
if (DetachingBase::isDetachingMethod(method)) { |
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
StringRef className = clazy::name(record); |
| 196 |
if (auto it = m_writeMethodsByType.find(className); it != m_writeMethodsByType.cend()) { |
| 197 |
return clazy::contains(it->second, clazy::name(method)); |
| 198 |
} |
| 199 |
|
| 200 |
return false; |
| 201 |
} |