| 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 "assert-with-side-effects.h" |
| 11 |
#include "MacroUtils.h" |
| 12 |
#include "StringUtils.h" |
| 13 |
#include "clazy_stl.h" |
| 14 |
|
| 15 |
#include <clang/AST/Decl.h> |
| 16 |
#include <clang/AST/DeclCXX.h> |
| 17 |
#include <clang/AST/Expr.h> |
| 18 |
#include <clang/AST/ExprCXX.h> |
| 19 |
#include <clang/AST/OperationKinds.h> |
| 20 |
#include <clang/AST/Stmt.h> |
| 21 |
#include <clang/Basic/LLVM.h> |
| 22 |
#include <clang/Basic/SourceLocation.h> |
| 23 |
#include <clang/Basic/SourceManager.h> |
| 24 |
#include <llvm/ADT/StringRef.h> |
| 25 |
#include <llvm/Support/Casting.h> |
| 26 |
|
| 27 |
#include <vector> |
| 28 |
|
| 29 |
using namespace clang; |
| 30 |
|
| 31 |
static bool functionIsOk(StringRef name) |
| 32 |
{ |
| 33 |
static const std::vector<StringRef> whitelist = { |
| 34 |
"qFuzzyIsNull", "qt_noop", "qt_assert", "qIsFinite", "qIsInf", "qIsNaN", "qIsNumericType", "operator==", "operator<", |
| 35 |
"operator>", "operator<=", "operator>=", "operator!=", "operator+", "operator-", "q_func", "d_func", "isEmptyHelper", |
| 36 |
"qCross", "qMin", "qMax", "qBound", "priv", "qobject_cast", "dbusService", |
| 37 |
}; |
| 38 |
return clazy::contains(whitelist, name); |
| 39 |
} |
| 40 |
|
| 41 |
static bool methodIsOK(const std::string &name) |
| 42 |
{ |
| 43 |
static const std::vector<std::string> whitelist = { |
| 44 |
"QList::begin", |
| 45 |
"QList::end", |
| 46 |
"QVector::begin", |
| 47 |
"QVector::end", |
| 48 |
"QHash::begin", |
| 49 |
"QHash::end", |
| 50 |
"QByteArray::data", |
| 51 |
"QBasicMutex::isRecursive", |
| 52 |
"QLinkedList::begin", |
| 53 |
"QLinkedList::end", |
| 54 |
"QDataBuffer::first", |
| 55 |
"QOpenGLFunctions::glIsRenderbuffer", |
| 56 |
}; |
| 57 |
return clazy::contains(whitelist, name); |
| 58 |
} |
| 59 |
|
| 60 |
void AssertWithSideEffects::VisitStmt(Stmt *stm) |
| 61 |
{ |
| 62 |
const SourceLocation stmStart = stm->getBeginLoc(); |
| 63 |
if (!clazy::isInMacro(astContext(), stmStart, "Q_ASSERT")) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
bool warn = false; |
| 68 |
const bool checkfunctions = m_aggressiveness & AlsoCheckFunctionCallsAggressiveness; |
| 69 |
|
| 70 |
auto *memberCall = dyn_cast<CXXMemberCallExpr>(stm); |
| 71 |
if (memberCall) { |
| 72 |
if (checkfunctions) { |
| 73 |
CXXMethodDecl *method = memberCall->getMethodDecl(); |
| 74 |
if (!method->isConst() && !methodIsOK(clazy::qualifiedMethodName(method)) && !functionIsOk(clazy::name(method))) { |
| 75 |
// llvm::errs() << "reason1 " << clazy::qualifiedMethodName(method) << "\n"; |
| 76 |
warn = true; |
| 77 |
} |
| 78 |
} |
| 79 |
} else if (auto *call = dyn_cast<CallExpr>(stm)) { |
| 80 |
// Non member function calls not allowed |
| 81 |
|
| 82 |
FunctionDecl *func = call->getDirectCallee(); |
| 83 |
if (func && checkfunctions) { |
| 84 |
if (isa<CXXMethodDecl>(func)) { // This will be visited next, so ignore it now |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
if (functionIsOk(clazy::name(func))) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
warn = true; |
| 93 |
} |
| 94 |
} else if (auto *op = dyn_cast<BinaryOperator>(stm)) { |
| 95 |
if (op->isAssignmentOp()) { |
| 96 |
if (auto *declRef = dyn_cast<DeclRefExpr>(op->getLHS())) { |
| 97 |
ValueDecl *valueDecl = declRef->getDecl(); |
| 98 |
if (valueDecl && sm().isBeforeInSLocAddrSpace(valueDecl->getBeginLoc(), stmStart)) { |
| 99 |
// llvm::errs() << "reason3\n"; |
| 100 |
warn = true; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} else if (auto *op = dyn_cast<UnaryOperator>(stm)) { |
| 105 |
if (auto *declRef = dyn_cast<DeclRefExpr>(op->getSubExpr())) { |
| 106 |
ValueDecl *valueDecl = declRef->getDecl(); |
| 107 |
auto type = op->getOpcode(); |
| 108 |
if (type != UnaryOperatorKind::UO_Deref && type != UnaryOperatorKind::UO_AddrOf) { |
| 109 |
if (valueDecl && sm().isBeforeInSLocAddrSpace(valueDecl->getBeginLoc(), stmStart)) { |
| 110 |
// llvm::errs() << "reason5 " << op->getOpcodeStr() << "\n"; |
| 111 |
warn = true; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
if (warn) { |
| 118 |
emitWarning(stmStart, "Code inside Q_ASSERT has side-effects but won't be built in release mode"); |
| 119 |
} |
| 120 |
} |