| 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, 2018 Sergio Martins <[email protected]> |
| 6 |
|
| 7 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "function-args-by-ref.h" |
| 11 |
#include "ClazyContext.h" |
| 12 |
#include "FixItUtils.h" |
| 13 |
#include "QtUtils.h" |
| 14 |
#include "TypeUtils.h" |
| 15 |
#include "Utils.h" |
| 16 |
#include "clazy_stl.h" |
| 17 |
|
| 18 |
#include <clang/AST/Decl.h> |
| 19 |
#include <clang/AST/DeclCXX.h> |
| 20 |
#include <clang/AST/ExprCXX.h> |
| 21 |
#include <clang/AST/Stmt.h> |
| 22 |
#include <clang/AST/Type.h> |
| 23 |
#include <clang/Basic/Diagnostic.h> |
| 24 |
#include <clang/Basic/LLVM.h> |
| 25 |
#include <llvm/ADT/ArrayRef.h> |
| 26 |
#include <llvm/ADT/StringRef.h> |
| 27 |
#include <llvm/Support/Casting.h> |
| 28 |
|
| 29 |
#include <vector> |
| 30 |
|
| 31 |
using namespace clang; |
| 32 |
|
| 33 |
bool FunctionArgsByRef::shouldIgnoreClass(CXXRecordDecl *record) |
| 34 |
{ |
| 35 |
if (!record) { |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
if (clazy::isSharedPointer(record, m_context->qtNamespace())) { |
| 40 |
return true; |
| 41 |
} |
| 42 |
|
| 43 |
static const std::vector<std::string> ignoreList = { |
| 44 |
"QDebug", // Too many warnings |
| 45 |
"QGenericReturnArgument", |
| 46 |
"QColor", // TODO: Remove in Qt6 |
| 47 |
"QStringRef", // TODO: Remove in Qt6 |
| 48 |
"QList::const_iterator", // TODO: Remove in Qt6 |
| 49 |
"QJsonArray::const_iterator", // TODO: Remove in Qt6 |
| 50 |
"QList<QString>::const_iterator", // TODO: Remove in Qt6 |
| 51 |
"QtMetaTypePrivate::QSequentialIterableImpl", |
| 52 |
"QtMetaTypePrivate::QAssociativeIterableImpl", |
| 53 |
"QVariantComparisonHelper", |
| 54 |
"QHashDummyValue", |
| 55 |
"QCharRef", |
| 56 |
"QString::Null", |
| 57 |
}; |
| 58 |
return clazy::contains(ignoreList, trimQtNamespace(record->getQualifiedNameAsString())); |
| 59 |
} |
| 60 |
|
| 61 |
bool FunctionArgsByRef::shouldIgnoreOperator(FunctionDecl *function) |
| 62 |
{ |
| 63 |
// Too many warnings in operator<< |
| 64 |
OverloadedOperatorKind op = function->getOverloadedOperator(); |
| 65 |
return op == clang::OO_LessLess; |
| 66 |
} |
| 67 |
|
| 68 |
bool FunctionArgsByRef::shouldIgnoreFunction(clang::FunctionDecl *function) |
| 69 |
{ |
| 70 |
static const std::vector<std::string> qualifiedIgnoreList = { |
| 71 |
"QDBusMessage::createErrorReply", // Fixed in Qt6 |
| 72 |
"QMenu::exec", // Fixed in Qt6 |
| 73 |
"QTextFrame::iterator", // Fixed in Qt6 |
| 74 |
"QGraphicsWidget::addActions", // Fixed in Qt6 |
| 75 |
"QListWidget::mimeData", // Fixed in Qt6 |
| 76 |
"QTableWidget::mimeData", // Fixed in Qt6 |
| 77 |
"QTreeWidget::mimeData", // Fixed in Qt6 |
| 78 |
"QWidget::addActions", // Fixed in Qt6 |
| 79 |
"QSslCertificate::verify", // Fixed in Qt6 |
| 80 |
"QSslConfiguration::setAllowedNextProtocols" // Fixed in Qt6 |
| 81 |
}; |
| 82 |
|
| 83 |
return clazy::contains(qualifiedIgnoreList, function->getQualifiedNameAsString()); |
| 84 |
} |
| 85 |
|
| 86 |
static std::string warningMsgForSmallType(int sizeOf, const std::string &typeName) |
| 87 |
{ |
| 88 |
std::string sizeStr = std::to_string(sizeOf); |
| 89 |
return "Missing reference on large type (sizeof " + typeName + " is " + sizeStr + " bytes)"; |
| 90 |
} |
| 91 |
|
| 92 |
void FunctionArgsByRef::processFunction(FunctionDecl *func) |
| 93 |
{ |
| 94 |
if (!func || !func->isThisDeclarationADefinition() || func->isDeleted() || shouldIgnoreOperator(func)) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
if (m_context->isQtDeveloper() && shouldIgnoreFunction(func)) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
Stmt *body = func->getBody(); |
| 103 |
|
| 104 |
auto funcParams = Utils::functionParameters(func); |
| 105 |
for (unsigned int i = 0; i < funcParams.size(); ++i) { |
| 106 |
ParmVarDecl *param = funcParams[i]; |
| 107 |
const QualType paramQt = clazy::unrefQualType(param->getType()); |
| 108 |
const Type *paramType = paramQt.getTypePtrOrNull(); |
| 109 |
if (!paramType || paramType->isIncompleteType() || paramType->isDependentType()) { |
| 110 |
continue; |
| 111 |
} |
| 112 |
|
| 113 |
if (shouldIgnoreClass(paramType->getAsCXXRecordDecl())) { |
| 114 |
continue; |
| 115 |
} |
| 116 |
|
| 117 |
clazy::QualTypeClassification classif; |
| 118 |
bool success = clazy::classifyQualType(m_context, param->getType(), param, classif, body); |
| 119 |
if (!success) { |
| 120 |
continue; |
| 121 |
} |
| 122 |
|
| 123 |
std::vector<CXXCtorInitializer *> ctorInits = Utils::ctorInitializer(dyn_cast<CXXConstructorDecl>(func), param); |
| 124 |
if (Utils::ctorInitializerContainsMove(ctorInits)) { |
| 125 |
continue; |
| 126 |
} |
| 127 |
|
| 128 |
if (classif.passBigTypeByConstRef || classif.passNonTriviallyCopyableByConstRef) { |
| 129 |
std::string error; |
| 130 |
std::vector<FixItHint> fixits; |
| 131 |
|
| 132 |
std::string paramStr = param->getType().getAsString(lo()); |
| 133 |
const std::string funcName = func->getQualifiedNameAsString(); |
| 134 |
|
| 135 |
if (const std::string paramName = param->getNameAsString(); !paramName.empty()) |
| 136 |
paramStr.append(" " + paramName); |
| 137 |
|
| 138 |
if (classif.passNonTriviallyCopyableByConstRef) { // Prefer this warning, because we might otherwise annoy user with specific size of Qt classes |
| 139 |
error = funcName + ": Missing reference on non-trivial type (" + paramStr + ')'; |
| 140 |
} else if (classif.passBigTypeByConstRef) { |
| 141 |
error = warningMsgForSmallType(classif.size_of_T, paramStr); |
| 142 |
} |
| 143 |
|
| 144 |
addFixits(fixits, func, i); |
| 145 |
emitWarning(param->getBeginLoc(), error, fixits); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
void FunctionArgsByRef::addFixits(std::vector<FixItHint> &fixits, FunctionDecl *func, unsigned int parmIndex) |
| 151 |
{ |
| 152 |
for (auto *funcRedecl : func->redecls()) { |
| 153 |
auto funcParams = Utils::functionParameters(funcRedecl); |
| 154 |
if (funcParams.size() <= parmIndex) { |
| 155 |
return; |
| 156 |
} |
| 157 |
|
| 158 |
ParmVarDecl *param = funcParams[parmIndex]; |
| 159 |
QualType paramQt = clazy::unrefQualType(param->getType()); |
| 160 |
|
| 161 |
const bool isConst = paramQt.isConstQualified(); |
| 162 |
|
| 163 |
if (!isConst) { |
| 164 |
SourceLocation start = param->getBeginLoc(); |
| 165 |
fixits.push_back(clazy::createInsertion(start, "const ")); |
| 166 |
} |
| 167 |
|
| 168 |
SourceLocation end = param->getLocation(); |
| 169 |
fixits.push_back(clazy::createInsertion(end, "&")); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
void FunctionArgsByRef::VisitDecl(Decl *decl) |
| 174 |
{ |
| 175 |
processFunction(dyn_cast<FunctionDecl>(decl)); |
| 176 |
} |
| 177 |
|
| 178 |
void FunctionArgsByRef::VisitStmt(Stmt *stmt) |
| 179 |
{ |
| 180 |
if (auto *lambda = dyn_cast<LambdaExpr>(stmt)) { |
| 181 |
if (!shouldIgnoreFile(stmt->getBeginLoc())) { |
| 182 |
processFunction(lambda->getCallOperator()); |
| 183 |
} |
| 184 |
} |
| 185 |
} |