| 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 "requiredresults.h" |
| 11 |
#include "Utils.h" |
| 12 |
|
| 13 |
#include <clang/AST/DeclCXX.h> |
| 14 |
#include <clang/AST/Expr.h> |
| 15 |
#include <clang/AST/ExprCXX.h> |
| 16 |
|
| 17 |
using namespace clang; |
| 18 |
|
| 19 |
RequiredResults::RequiredResults(const std::string &name) |
| 20 |
: CheckBase(name) |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
bool RequiredResults::shouldIgnoreMethod(const StringRef &qualifiedName) |
| 25 |
{ |
| 26 |
static const std::vector<StringRef> files = { |
| 27 |
"QDir::mkdir", |
| 28 |
"QDir::rmdir", |
| 29 |
"QDir::mkpath", |
| 30 |
"QDBusConnection::send", |
| 31 |
|
| 32 |
"QRegExp::indexIn", |
| 33 |
"QRegExp::exactMatch", |
| 34 |
"QQmlProperty::write", |
| 35 |
"QQmlProperty::reset", |
| 36 |
"QWidget::winId", |
| 37 |
"QtWaylandClient::QWaylandEglWindow::contentFBO", |
| 38 |
"ProString::updatedHash", |
| 39 |
|
| 40 |
// kdepim |
| 41 |
"KCalCore::Incidence::recurrence", |
| 42 |
"KCalCore::RecurrenceRule::Private::buildCache", |
| 43 |
"KAlarmCal::KAEvent::updateKCalEvent", |
| 44 |
"Akonadi::Server::Collection::clearMimeTypes", |
| 45 |
"Akonadi::Server::Collection::addMimeType", |
| 46 |
"Akonadi::Server::PimItem::addFlag", |
| 47 |
"Akonadi::Server::PimItem::addTag", |
| 48 |
|
| 49 |
// kf5 libs |
| 50 |
"KateVi::Command::execute", |
| 51 |
"KArchiveDirectory::copyTo", |
| 52 |
"KBookmarkManager::saveAs", |
| 53 |
"KBookmarkManager::save", |
| 54 |
"KLineEditPrivate::copySqueezedText", |
| 55 |
"KJS::UString::Rep::hash", |
| 56 |
"KCModuleProxy::realModule", |
| 57 |
"KCategorizedView::visualRect", |
| 58 |
"KateLineLayout::textLine", |
| 59 |
"DOM::HTMLCollectionImpl::firstItem", |
| 60 |
"DOM::HTMLCollectionImpl::nextItem", |
| 61 |
"DOM::HTMLCollectionImpl::firstItem", |
| 62 |
"ImapResourceBase::settings", |
| 63 |
}; |
| 64 |
|
| 65 |
return clazy::contains(files, qualifiedName); |
| 66 |
} |
| 67 |
|
| 68 |
void RequiredResults::VisitStmt(clang::Stmt *stm) |
| 69 |
{ |
| 70 |
auto compound = dyn_cast<CompoundStmt>(stm); |
| 71 |
if (!compound) |
| 72 |
return; |
| 73 |
|
| 74 |
for (auto child : compound->children()) { |
| 75 |
auto callExpr = dyn_cast<CXXMemberCallExpr>(child); |
| 76 |
if (!callExpr) |
| 77 |
continue; |
| 78 |
|
| 79 |
CXXMethodDecl *methodDecl = callExpr->getMethodDecl(); |
| 80 |
if (!methodDecl || !methodDecl->isConst()) |
| 81 |
continue; |
| 82 |
|
| 83 |
std::string methodName = methodDecl->getQualifiedNameAsString(); |
| 84 |
if (shouldIgnoreMethod(methodName)) // Filter out some false positives |
| 85 |
continue; |
| 86 |
|
| 87 |
QualType qt = methodDecl->getReturnType(); |
| 88 |
const Type *type = qt.getTypePtrOrNull(); |
| 89 |
if (!type || type->isVoidType()) |
| 90 |
continue; |
| 91 |
|
| 92 |
// Bail-out if any parameter is a non-const-ref or pointer bool bailout = false; |
| 93 |
bool bailout = false; |
| 94 |
for (auto paramVarDecl : Utils::functionParameters(methodDecl)) { |
| 95 |
QualType qt = paramVarDecl->getType(); |
| 96 |
const Type *type = qt.getTypePtrOrNull(); |
| 97 |
if (!type || type->isPointerType()) { |
| 98 |
bailout = true; |
| 99 |
break; |
| 100 |
} |
| 101 |
|
| 102 |
// qt.isConstQualified() not working !? TODO: Replace this string parsing when I figure it out |
| 103 |
if (type->isReferenceType() && !clazy::contains(qt.getAsString(), "const ")) { |
| 104 |
bailout = true; |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
if (!bailout) { |
| 110 |
std::string error = std::string("Unused result of const member (") + methodName + ')'; |
| 111 |
emitWarning(callExpr->getBeginLoc(), error); |
| 112 |
} |
| 113 |
} |
| 114 |
} |