| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016-2017 Sergio Martins <[email protected]> |
| 3 |
SPDX-FileCopyrightText: 2023 Alexander Lohnau <[email protected]> |
| 4 |
|
| 5 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "unused-non-trivial-variable.h" |
| 9 |
#include "ContextUtils.h" |
| 10 |
#include "HierarchyUtils.h" |
| 11 |
#include "QtUtils.h" |
| 12 |
#include "StringUtils.h" |
| 13 |
#include "TypeUtils.h" |
| 14 |
#include "clazy_stl.h" |
| 15 |
|
| 16 |
#include <clang/AST/Attr.h> |
| 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 <clang/Basic/SourceLocation.h> |
| 24 |
#include <clang/Basic/SourceManager.h> |
| 25 |
#include <llvm/ADT/StringRef.h> |
| 26 |
#include <llvm/Support/Casting.h> |
| 27 |
|
| 28 |
#include <cstdlib> |
| 29 |
#include <string> |
| 30 |
#include <vector> |
| 31 |
|
| 32 |
using namespace clang; |
| 33 |
|
| 34 |
UnusedNonTrivialVariable::UnusedNonTrivialVariable(const std::string &name, Options options) |
| 35 |
: CheckBase(name, options) |
| 36 |
{ |
| 37 |
const char *user_blacklist = getenv("CLAZY_UNUSED_NON_TRIVIAL_VARIABLE_BLACKLIST"); |
| 38 |
const char *user_whitelist = getenv("CLAZY_UNUSED_NON_TRIVIAL_VARIABLE_WHITELIST"); |
| 39 |
|
| 40 |
if (user_blacklist) { |
| 41 |
m_userBlacklist = clazy::splitString(user_blacklist, ','); |
| 42 |
} |
| 43 |
|
| 44 |
if (user_whitelist) { |
| 45 |
m_userWhitelist = clazy::splitString(user_whitelist, ','); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
void UnusedNonTrivialVariable::VisitStmt(clang::Stmt *stmt) |
| 50 |
{ |
| 51 |
auto *declStmt = dyn_cast<DeclStmt>(stmt); |
| 52 |
if (!declStmt) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
for (auto *decl : declStmt->decls()) { |
| 57 |
handleVarDecl(dyn_cast<VarDecl>(decl)); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
bool UnusedNonTrivialVariable::isUninterestingType(const CXXRecordDecl *record) const |
| 62 |
{ |
| 63 |
static const std::vector<StringRef> blacklist = { |
| 64 |
"QMutexLocker", |
| 65 |
"QDebugStateSaver", |
| 66 |
"QTextBlockFormat", |
| 67 |
"QWriteLocker", |
| 68 |
"QSignalBlocker", |
| 69 |
"QReadLocker", |
| 70 |
"PRNGLocker", |
| 71 |
"QDBusWriteLocker", |
| 72 |
"QDBusBlockingCallWatcher", |
| 73 |
"QBoolBlocker", |
| 74 |
"QOrderedMutexLocker", |
| 75 |
"QTextLine", |
| 76 |
"QScopedScopeLevelCounter", |
| 77 |
}; |
| 78 |
|
| 79 |
// Check some obvious candidates first |
| 80 |
StringRef typeName = clazy::name(record); |
| 81 |
bool any = std::ranges::any_of(blacklist, [typeName](StringRef container) { |
| 82 |
return container == typeName; |
| 83 |
}); |
| 84 |
|
| 85 |
if (any) { |
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
static const std::vector<StringRef> blacklistedTemplates = { |
| 90 |
"QScopedPointer", |
| 91 |
"QSetValueOnDestroy", |
| 92 |
"QScopedValueRollback", |
| 93 |
"QScopeGuard", |
| 94 |
}; |
| 95 |
StringRef className = clazy::name(record); |
| 96 |
for (StringRef templateName : blacklistedTemplates) { |
| 97 |
if (clazy::startsWith(className, templateName)) { |
| 98 |
return true; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// Now check the user's blacklist, set by env-variable |
| 103 |
any = std::ranges::any_of(m_userBlacklist, [typeName](const std::string &container) { |
| 104 |
return container == typeName; |
| 105 |
}); |
| 106 |
|
| 107 |
return any; |
| 108 |
} |
| 109 |
|
| 110 |
bool UnusedNonTrivialVariable::isInterestingType(QualType t) const |
| 111 |
{ |
| 112 |
// TODO Remove QColor in Qt6 |
| 113 |
static const std::vector<StringRef> nonTrivialTypes = { |
| 114 |
"QColor", |
| 115 |
"QVariant", |
| 116 |
"QFont", |
| 117 |
"QUrl", |
| 118 |
"QIcon", |
| 119 |
"QImage", |
| 120 |
"QPixmap", |
| 121 |
"QPicture", |
| 122 |
"QBitmap", |
| 123 |
"QBrush", |
| 124 |
"QPen", |
| 125 |
"QBuffer", |
| 126 |
"QCache", |
| 127 |
"QDateTime", |
| 128 |
"QDir", |
| 129 |
"QEvent", |
| 130 |
"QFileInfo", |
| 131 |
"QFontInfo", |
| 132 |
"QFontMetrics", |
| 133 |
"QJSValue", |
| 134 |
"QLocale", |
| 135 |
"QRegularExpression", |
| 136 |
"QRegExp", |
| 137 |
"QUrlQuery", |
| 138 |
"QStorageInfo", |
| 139 |
"QPersistentModelIndex", |
| 140 |
"QJsonArray", |
| 141 |
"QJsonValue", |
| 142 |
"QJsonDocument", |
| 143 |
"QMimeType", |
| 144 |
"QBitArray", |
| 145 |
"QCollator", |
| 146 |
"QByteArrayList", |
| 147 |
"QCollatorSortKey", |
| 148 |
"QCursor", |
| 149 |
"QPalette", |
| 150 |
"QPainterPath", |
| 151 |
"QRegion", |
| 152 |
"QFontInfo", |
| 153 |
"QTextCursor", |
| 154 |
"QStaticText", |
| 155 |
"QFontMetricsF", |
| 156 |
"QTextFrameFormat", |
| 157 |
"QTextImageFormat", |
| 158 |
"QNetworkCookie", |
| 159 |
"QNetworkRequest", |
| 160 |
"QNetworkConfiguration", |
| 161 |
"QHostAddress", |
| 162 |
"QSqlQuery", |
| 163 |
"QSqlRecord", |
| 164 |
"QSqlField", |
| 165 |
"QLine", |
| 166 |
"QLineF", |
| 167 |
"QRect", |
| 168 |
"QRectF", |
| 169 |
"QDomNode", |
| 170 |
}; |
| 171 |
|
| 172 |
const CXXRecordDecl *record = clazy::typeAsRecord(t); |
| 173 |
if (!record) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
if (isOptionSet("no-whitelist")) { |
| 178 |
// Will cause too many false-positives, like RAII classes. Use suppressing comments to silence them. |
| 179 |
return !isUninterestingType(record); |
| 180 |
} |
| 181 |
|
| 182 |
if (clazy::isQtContainer(record)) { |
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
StringRef typeName = clazy::name(record); |
| 187 |
bool any = std::ranges::any_of(nonTrivialTypes, [typeName](StringRef container) { |
| 188 |
return container == typeName; |
| 189 |
}); |
| 190 |
|
| 191 |
if (any) { |
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
return std::ranges::any_of(m_userWhitelist, [typeName](const std::string &container) { |
| 196 |
return container == typeName; |
| 197 |
}); |
| 198 |
} |
| 199 |
|
| 200 |
void UnusedNonTrivialVariable::handleVarDecl(VarDecl *varDecl) |
| 201 |
{ |
| 202 |
if (!varDecl || !isInterestingType(varDecl->getType())) { |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
auto *currentFunc = clazy::firstContextOfType<FunctionDecl>(varDecl->getDeclContext()); |
| 207 |
Stmt *body = currentFunc ? currentFunc->getBody() : nullptr; |
| 208 |
if (!body) { |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
SourceLocation locStart = varDecl->getBeginLoc(); |
| 213 |
locStart = sm().getExpansionLoc(locStart); |
| 214 |
auto declRefs = clazy::getStatements<DeclRefExpr>(body, &sm(), locStart); |
| 215 |
|
| 216 |
auto pred = [varDecl](DeclRefExpr *declRef) { |
| 217 |
return declRef->getDecl() == varDecl; |
| 218 |
}; |
| 219 |
|
| 220 |
if (!std::ranges::any_of(declRefs, pred)) { |
| 221 |
// Check for [[maybe_unused]] attribute |
| 222 |
if (!varDecl->hasAttr<clang::UnusedAttr>()) { |
| 223 |
const std::string varName = varDecl->getQualifiedNameAsString(); |
| 224 |
emitWarning(locStart, "unused " + clazy::simpleTypeName(varDecl->getType(), lo()) + " " + varName); |
| 225 |
} |
| 226 |
} |
| 227 |
} |