| 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 "implicit-casts.h" |
| 11 |
#include "ClazyContext.h" |
| 12 |
#include "HierarchyUtils.h" |
| 13 |
#include "Utils.h" |
| 14 |
#include "clazy_stl.h" |
| 15 |
|
| 16 |
#include <clang/AST/Decl.h> |
| 17 |
#include <clang/AST/DeclCXX.h> |
| 18 |
#include <clang/AST/Expr.h> |
| 19 |
#include <clang/AST/ExprCXX.h> |
| 20 |
#include <clang/AST/OperationKinds.h> |
| 21 |
#include <clang/AST/Stmt.h> |
| 22 |
#include <clang/AST/Type.h> |
| 23 |
#include <clang/Basic/LLVM.h> |
| 24 |
#include <clang/Basic/Linkage.h> |
| 25 |
#include <clang/Basic/SourceLocation.h> |
| 26 |
#include <clang/Lex/Lexer.h> |
| 27 |
#include <llvm/ADT/ArrayRef.h> |
| 28 |
#include <llvm/ADT/StringRef.h> |
| 29 |
#include <llvm/Support/Casting.h> |
| 30 |
|
| 31 |
#include <vector> |
| 32 |
|
| 33 |
using namespace clang; |
| 34 |
|
| 35 |
ImplicitCasts::ImplicitCasts(const std::string &name, Options options) |
| 36 |
: CheckBase(name, options) |
| 37 |
{ |
| 38 |
m_filesToIgnore = {"qobject_impl.h", "qdebug.h", "hb-", "qdbusintegrator.cpp", "harfbuzz-", "qunicodetools.cpp"}; |
| 39 |
} |
| 40 |
|
| 41 |
static bool isInterestingFunction(FunctionDecl *func) |
| 42 |
{ |
| 43 |
if (!func) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
// The interesting function calls for the pointertoBool check are those having bool and also pointer arguments, |
| 48 |
// which might get mixed |
| 49 |
|
| 50 |
bool hasBoolArgument = false; |
| 51 |
bool hasPointerArgument = false; |
| 52 |
|
| 53 |
for (auto *param : Utils::functionParameters(func)) { |
| 54 |
const Type *t = param->getType().getTypePtrOrNull(); |
| 55 |
hasBoolArgument |= (t && t->isBooleanType()); |
| 56 |
hasPointerArgument |= (t && t->isPointerType()); |
| 57 |
|
| 58 |
if (hasBoolArgument && hasPointerArgument) { |
| 59 |
return true; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
// Checks for pointer->bool implicit casts |
| 67 |
template<typename T> |
| 68 |
static bool iterateCallExpr(T *callExpr, CheckBase *check) |
| 69 |
{ |
| 70 |
if (!callExpr) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
bool result = false; |
| 75 |
|
| 76 |
int i = 0; |
| 77 |
for (auto arg : callExpr->arguments()) { |
| 78 |
++i; |
| 79 |
auto implicitCast = dyn_cast<ImplicitCastExpr>(arg); |
| 80 |
if (!implicitCast || implicitCast->getCastKind() != clang::CK_PointerToBoolean) { |
| 81 |
continue; |
| 82 |
} |
| 83 |
|
| 84 |
check->emitWarning(implicitCast->getBeginLoc(), "Implicit pointer to bool cast (argument " + std::to_string(i) + ')'); |
| 85 |
result = true; |
| 86 |
} |
| 87 |
|
| 88 |
return result; |
| 89 |
} |
| 90 |
|
| 91 |
// Checks for bool->int implicit casts |
| 92 |
template<typename T> |
| 93 |
static bool iterateCallExpr2(T *callExpr, CheckBase *check, ParentMap *parentMap) |
| 94 |
{ |
| 95 |
if (!callExpr) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
bool result = false; |
| 100 |
|
| 101 |
int i = 0; |
| 102 |
for (auto arg : callExpr->arguments()) { |
| 103 |
++i; |
| 104 |
auto implicitCast = dyn_cast<ImplicitCastExpr>(arg); |
| 105 |
if (!implicitCast || implicitCast->getCastKind() != clang::CK_IntegralCast) { |
| 106 |
continue; |
| 107 |
} |
| 108 |
|
| 109 |
if (implicitCast->getType().getTypePtrOrNull()->isBooleanType()) { |
| 110 |
continue; |
| 111 |
} |
| 112 |
|
| 113 |
Expr *expr = implicitCast->getSubExpr(); |
| 114 |
QualType qt = expr->getType(); |
| 115 |
|
| 116 |
if (!qt.getTypePtrOrNull()->isBooleanType()) { // Filter out some bool to const bool |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
if (clazy::getFirstChildOfType<CXXFunctionalCastExpr>(implicitCast)) { |
| 121 |
continue; |
| 122 |
} |
| 123 |
|
| 124 |
if (clazy::getFirstChildOfType<CStyleCastExpr>(implicitCast)) { |
| 125 |
continue; |
| 126 |
} |
| 127 |
|
| 128 |
if (Utils::isInsideOperatorCall(parentMap, implicitCast, {"QTextStream", "QAtomicInt", "QBasicAtomicInt"})) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
if (Utils::insideCTORCall(parentMap, implicitCast, {"QAtomicInt", "QBasicAtomicInt"})) { |
| 133 |
continue; |
| 134 |
} |
| 135 |
|
| 136 |
check->emitWarning(implicitCast->getBeginLoc(), "Implicit bool to int cast (argument " + std::to_string(i) + ')'); |
| 137 |
result = true; |
| 138 |
} |
| 139 |
|
| 140 |
return result; |
| 141 |
} |
| 142 |
|
| 143 |
void ImplicitCasts::VisitStmt(clang::Stmt *stmt) |
| 144 |
{ |
| 145 |
// Lets check only in function calls. Otherwise there are too many false positives, it's common |
| 146 |
// to implicit cast to bool when checking pointers for validity, like if (ptr) |
| 147 |
|
| 148 |
auto *callExpr = dyn_cast<CallExpr>(stmt); |
| 149 |
CXXConstructExpr *ctorExpr = nullptr; |
| 150 |
if (!callExpr) { |
| 151 |
ctorExpr = dyn_cast<CXXConstructExpr>(stmt); |
| 152 |
if (!ctorExpr) { |
| 153 |
return; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if (isa<CXXOperatorCallExpr>(stmt)) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
if (isMacroToIgnore(stmt->getBeginLoc())) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
if (shouldIgnoreFile(stmt->getBeginLoc())) { |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
FunctionDecl *func = callExpr ? callExpr->getDirectCallee() : ctorExpr->getConstructor(); |
| 170 |
|
| 171 |
if (isInterestingFunction(func)) { |
| 172 |
// Check pointer->bool implicit casts |
| 173 |
iterateCallExpr<CallExpr>(callExpr, this); |
| 174 |
iterateCallExpr<CXXConstructExpr>(ctorExpr, this); |
| 175 |
} else if (isBoolToInt(func)) { |
| 176 |
// Check bool->int implicit casts |
| 177 |
iterateCallExpr2<CallExpr>(callExpr, this, m_context->parentMap); |
| 178 |
iterateCallExpr2<CXXConstructExpr>(ctorExpr, this, m_context->parentMap); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
bool ImplicitCasts::isBoolToInt(FunctionDecl *func) const |
| 183 |
{ |
| 184 |
if (!func || !isOptionSet("bool-to-int")) { |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
if (func->getLanguageLinkage() != CXXLanguageLinkage || func->isVariadic()) { |
| 189 |
return false; // Disabled for now, too many false-positives when interacting with C code |
| 190 |
} |
| 191 |
|
| 192 |
static const std::vector<std::string> functions = {"QString::arg"}; |
| 193 |
return !clazy::contains(functions, func->getQualifiedNameAsString()); |
| 194 |
} |
| 195 |
|
| 196 |
bool ImplicitCasts::isMacroToIgnore(SourceLocation loc) const |
| 197 |
{ |
| 198 |
static const std::vector<StringRef> macros = {"QVERIFY", "Q_UNLIKELY", "Q_LIKELY"}; |
| 199 |
if (!loc.isMacroID()) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
StringRef macro = Lexer::getImmediateMacroName(loc, sm(), lo()); |
| 203 |
return clazy::contains(macros, macro); |
| 204 |
} |