| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016-2018 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "function-args-by-value.h" |
| 8 |
#include "ClazyContext.h" |
| 9 |
#include "FixItUtils.h" |
| 10 |
#include "QtUtils.h" |
| 11 |
#include "StringUtils.h" |
| 12 |
#include "TypeUtils.h" |
| 13 |
#include "Utils.h" |
| 14 |
#include "clazy_stl.h" |
| 15 |
|
| 16 |
#include <clang/AST/ASTContext.h> |
| 17 |
#include <clang/AST/Decl.h> |
| 18 |
#include <clang/AST/DeclCXX.h> |
| 19 |
#include <clang/AST/Expr.h> |
| 20 |
#include <clang/AST/ExprCXX.h> |
| 21 |
#include <clang/AST/PrettyPrinter.h> |
| 22 |
#include <clang/AST/Redeclarable.h> |
| 23 |
#include <clang/AST/Stmt.h> |
| 24 |
#include <clang/AST/Type.h> |
| 25 |
#include <clang/Basic/LLVM.h> |
| 26 |
#include <clang/Basic/SourceLocation.h> |
| 27 |
#include <llvm/ADT/ArrayRef.h> |
| 28 |
#include <llvm/ADT/StringRef.h> |
| 29 |
#include <llvm/Support/Casting.h> |
| 30 |
#include <llvm/Support/raw_ostream.h> |
| 31 |
|
| 32 |
#include <iterator> |
| 33 |
#include <vector> |
| 34 |
|
| 35 |
using namespace clang; |
| 36 |
|
| 37 |
// TODO, go over all these |
| 38 |
bool FunctionArgsByValue::shouldIgnoreClass(CXXRecordDecl *record) |
| 39 |
{ |
| 40 |
if (!record) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
if (clazy::isSharedPointer(record, m_context->qtNamespace())) { |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
static const std::vector<std::string> ignoreList = { |
| 49 |
"QDebug", // Too many warnings |
| 50 |
"QGenericReturnArgument", |
| 51 |
"QColor", // TODO: Remove in Qt6 |
| 52 |
"QStringRef", // TODO: Remove in Qt6 |
| 53 |
"QList::const_iterator", // TODO: Remove in Qt6 |
| 54 |
"QJsonArray::const_iterator", // TODO: Remove in Qt6 |
| 55 |
"QList<QString>::const_iterator", // TODO: Remove in Qt6 |
| 56 |
"QtMetaTypePrivate::QSequentialIterableImpl", |
| 57 |
"QtMetaTypePrivate::QAssociativeIterableImpl", |
| 58 |
"QVariantComparisonHelper", |
| 59 |
"QHashDummyValue", |
| 60 |
"QCharRef", |
| 61 |
"QString::Null", |
| 62 |
}; |
| 63 |
return clazy::contains(ignoreList, record->getQualifiedNameAsString()); |
| 64 |
} |
| 65 |
|
| 66 |
bool FunctionArgsByValue::shouldIgnoreOperator(FunctionDecl *function) |
| 67 |
{ |
| 68 |
OverloadedOperatorKind op = function->getOverloadedOperator(); |
| 69 |
// Too many warnings in operator<<, unrelated warnings for = or == operators |
| 70 |
return op == clang::OO_LessLess || op == clang::OO_Equal || op == clang::OO_EqualEqual; |
| 71 |
} |
| 72 |
|
| 73 |
bool FunctionArgsByValue::shouldIgnoreFunction(clang::FunctionDecl *function) |
| 74 |
{ |
| 75 |
static const std::vector<std::string> qualifiedIgnoreList = { |
| 76 |
"QDBusMessage::createErrorReply", // Fixed in Qt6 |
| 77 |
"QMenu::exec", // Fixed in Qt6 |
| 78 |
"QTextFrame::iterator", // Fixed in Qt6 |
| 79 |
"QGraphicsWidget::addActions", // Fixed in Qt6 |
| 80 |
"QListWidget::mimeData", // Fixed in Qt6 |
| 81 |
"QTableWidget::mimeData", // Fixed in Qt6 |
| 82 |
"QTreeWidget::mimeData", // Fixed in Qt6 |
| 83 |
"QWidget::addActions", // Fixed in Qt6 |
| 84 |
"QSslCertificate::verify", // Fixed in Qt6 |
| 85 |
"QSslConfiguration::setAllowedNextProtocols", // Fixed in Qt6 |
| 86 |
}; |
| 87 |
|
| 88 |
return clazy::contains(qualifiedIgnoreList, function->getQualifiedNameAsString()); |
| 89 |
} |
| 90 |
|
| 91 |
void FunctionArgsByValue::VisitDecl(Decl *decl) |
| 92 |
{ |
| 93 |
processFunction(dyn_cast<FunctionDecl>(decl)); |
| 94 |
} |
| 95 |
|
| 96 |
void FunctionArgsByValue::VisitStmt(Stmt *stmt) |
| 97 |
{ |
| 98 |
if (auto *lambda = dyn_cast<LambdaExpr>(stmt)) { |
| 99 |
processFunction(lambda->getCallOperator()); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
void FunctionArgsByValue::processFunction(FunctionDecl *func) |
| 104 |
{ |
| 105 |
if (!func || !func->isThisDeclarationADefinition() || func->isDeleted()) { |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
if (func->isDefaulted()) { |
| 110 |
// The C++ compiler enforces refs or do being used for defaulted methods |
| 111 |
// https://invent.kde.org/sdk/clazy/-/issues/25 |
| 112 |
return; |
| 113 |
} |
| 114 |
auto *ctor = dyn_cast<CXXConstructorDecl>(func); |
| 115 |
if (ctor && ctor->isCopyConstructor()) { |
| 116 |
return; // copy-ctor must take by ref |
| 117 |
} |
| 118 |
|
| 119 |
const bool warnForOverriddenMethods = isOptionSet("warn-for-overridden-methods"); |
| 120 |
if (!warnForOverriddenMethods && Utils::methodOverrides(dyn_cast<CXXMethodDecl>(func))) { |
| 121 |
// When overriding you can't change the signature. You should fix the base classes first |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
if (shouldIgnoreOperator(func)) { |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
if (m_context->isQtDeveloper() && shouldIgnoreFunction(func)) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
Stmt *body = func->getBody(); |
| 134 |
|
| 135 |
int i = -1; |
| 136 |
for (auto *param : Utils::functionParameters(func)) { |
| 137 |
i++; |
| 138 |
const QualType paramQt = clazy::unrefQualType(param->getType()); |
| 139 |
const Type *paramType = paramQt.getTypePtrOrNull(); |
| 140 |
if (!paramType || paramType->isIncompleteType() || paramType->isDependentType()) { |
| 141 |
continue; |
| 142 |
} |
| 143 |
|
| 144 |
if (shouldIgnoreClass(paramType->getAsCXXRecordDecl())) { |
| 145 |
continue; |
| 146 |
} |
| 147 |
|
| 148 |
clazy::QualTypeClassification classif; |
| 149 |
bool success = clazy::classifyQualType(m_context, param->getType(), param, classif, body); |
| 150 |
if (!success) { |
| 151 |
continue; |
| 152 |
} |
| 153 |
|
| 154 |
if (classif.passSmallTrivialByValue) { |
| 155 |
if (ctor) { // Implements fix for Bug #379342 |
| 156 |
std::vector<CXXCtorInitializer *> initializers = Utils::ctorInitializer(ctor, param); |
| 157 |
bool found_by_ref_member_init = false; |
| 158 |
for (auto *initializer : initializers) { |
| 159 |
if (!initializer->isMemberInitializer()) { |
| 160 |
continue; // skip base class initializer |
| 161 |
} |
| 162 |
FieldDecl *field = initializer->getMember(); |
| 163 |
if (!field) { |
| 164 |
continue; |
| 165 |
} |
| 166 |
|
| 167 |
QualType type = field->getType(); |
| 168 |
if (type.isNull() || type->isReferenceType()) { |
| 169 |
found_by_ref_member_init = true; |
| 170 |
break; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
if (found_by_ref_member_init) { |
| 175 |
continue; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
std::vector<FixItHint> fixits; |
| 180 |
auto *method = dyn_cast<CXXMethodDecl>(func); |
| 181 |
const bool isVirtualMethod = method && method->isVirtual(); |
| 182 |
if (!isVirtualMethod || warnForOverriddenMethods) { // Don't try to fix virtual methods, as build can fail |
| 183 |
for (auto *redecl : func->redecls()) { // Fix in both header and .cpp |
| 184 |
auto *fdecl = dyn_cast<FunctionDecl>(redecl); |
| 185 |
const ParmVarDecl *param = fdecl->getParamDecl(i); |
| 186 |
fixits.push_back(fixit(fdecl, param, classif)); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
std::string paramStr = param->getType().getAsString(lo()); |
| 191 |
|
| 192 |
if (const std::string paramName = param->getNameAsString(); !paramName.empty()) |
| 193 |
paramStr.append(" " + paramName); |
| 194 |
|
| 195 |
std::string funcName = func->getQualifiedNameAsString(); |
| 196 |
std::string error = funcName + ": Pass small and trivially-copyable type by value (" + paramStr + ')'; |
| 197 |
emitWarning(param->getBeginLoc(), error, fixits); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
FixItHint FunctionArgsByValue::fixit(FunctionDecl *func, const ParmVarDecl *param, clazy::QualTypeClassification) |
| 203 |
{ |
| 204 |
QualType qt = clazy::unrefQualType(param->getType()); |
| 205 |
qt.removeLocalConst(); |
| 206 |
const std::string typeName = qt.getAsString(PrintingPolicy(lo())); |
| 207 |
std::string replacement = typeName + ' ' + std::string(clazy::name(param)); |
| 208 |
SourceLocation startLoc = param->getBeginLoc(); |
| 209 |
SourceLocation endLoc = param->getEndLoc(); |
| 210 |
|
| 211 |
const int numRedeclarations = std::distance(func->redecls_begin(), func->redecls_end()); |
| 212 |
const bool definitionIsAlsoDeclaration = numRedeclarations == 1; |
| 213 |
const bool isDeclarationButNotDefinition = !func->doesThisDeclarationHaveABody(); |
| 214 |
|
| 215 |
if (param->hasDefaultArg() && (isDeclarationButNotDefinition || definitionIsAlsoDeclaration)) { |
| 216 |
endLoc = param->getDefaultArg()->getBeginLoc().getLocWithOffset(-1); |
| 217 |
replacement += " ="; |
| 218 |
} |
| 219 |
|
| 220 |
if (!startLoc.isValid() || !endLoc.isValid()) { |
| 221 |
llvm::errs() << "Internal error could not apply fixit " << startLoc.printToString(sm()) << ';' << endLoc.printToString(sm()) << "\n"; |
| 222 |
return {}; |
| 223 |
} |
| 224 |
|
| 225 |
return clazy::createReplacement({startLoc, endLoc}, replacement); |
| 226 |
} |