1 /*
2 This file is part of the clazy static checker.
3
4 Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
5 Author: Sérgio Martins <sergio.martins@kdab.com>
6
7 Copyright (C) 2015 Sergio Martins <smartins@kde.org>
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
18
19 You should have received a copy of the GNU Library General Public License
20 along with this library; see the file COPYING.LIB. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.
23 */
24
25 #include "assert-with-side-effects.h"
26 #include "MacroUtils.h"
27 #include "StringUtils.h"
28 #include "SourceCompatibilityHelpers.h"
29 #include "clazy_stl.h"
30
31 #include <clang/AST/Expr.h>
32 #include <clang/AST/Stmt.h>
33 #include <clang/AST/Decl.h>
34 #include <clang/AST/DeclCXX.h>
35 #include <clang/AST/ExprCXX.h>
36 #include <clang/AST/OperationKinds.h>
37 #include <clang/Basic/LLVM.h>
38 #include <clang/Basic/SourceLocation.h>
39 #include <clang/Basic/SourceManager.h>
40 #include <llvm/ADT/StringRef.h>
41 #include <llvm/Support/Casting.h>
42
43 #include <vector>
44
45 class ClazyContext;
46
47 using namespace clang;
48 using namespace std;
49
50
51 enum Aggressiveness
52 {
53 NormalAggressiveness = 0,
54 AlsoCheckFunctionCallsAggressiveness = 1 // too many false positives
55 };
56
AssertWithSideEffects(const std::string & name,ClazyContext * context)57 AssertWithSideEffects::AssertWithSideEffects(const std::string &name, ClazyContext *context)
58 : CheckBase(name, context, Option_CanIgnoreIncludes)
59 , m_aggressiveness(NormalAggressiveness)
60 {
61 }
62
functionIsOk(StringRef name)63 static bool functionIsOk(StringRef name)
64 {
65 static const vector<StringRef> whitelist = {"qFuzzyIsNull", "qt_noop", "qt_assert", "qIsFinite", "qIsInf",
66 "qIsNaN", "qIsNumericType", "operator==", "operator<", "operator>", "operator<=", "operator>=", "operator!=", "operator+", "operator-"
67 "q_func", "d_func", "isEmptyHelper"
68 "qCross", "qMin", "qMax", "qBound", "priv", "qobject_cast", "dbusService"};
69 return clazy::contains(whitelist, name);
70 }
71
methodIsOK(const string & name)72 static bool methodIsOK(const string &name)
73 {
74 static const vector<string> whitelist = {"QList::begin", "QList::end", "QVector::begin",
75 "QVector::end", "QHash::begin", "QHash::end",
76 "QByteArray::data", "QBasicMutex::isRecursive",
77 "QLinkedList::begin", "QLinkedList::end", "QDataBuffer::first",
78 "QOpenGLFunctions::glIsRenderbuffer"};
79 return clazy::contains(whitelist, name);
80 }
81
VisitStmt(Stmt * stm)82 void AssertWithSideEffects::VisitStmt(Stmt *stm)
83 {
84 const SourceLocation stmStart = clazy::getLocStart(stm);
85 if (!clazy::isInMacro(&m_astContext, stmStart, "Q_ASSERT"))
86 return;
87
88 bool warn = false;
89 const bool checkfunctions = m_aggressiveness & AlsoCheckFunctionCallsAggressiveness;
90
91 auto memberCall = dyn_cast<CXXMemberCallExpr>(stm);
92 if (memberCall) {
93 if (checkfunctions) {
94 CXXMethodDecl *method = memberCall->getMethodDecl();
95 if (!method->isConst() && !methodIsOK(clazy::qualifiedMethodName(method)) && !functionIsOk(clazy::name(method))) {
96 // llvm::errs() << "reason1 " << clazy::qualifiedMethodName(method) << "\n";
97 warn = true;
98 }
99 }
100 } else if (auto call = dyn_cast<CallExpr>(stm)) {
101 // Non member function calls not allowed
102
103 FunctionDecl *func = call->getDirectCallee();
104 if (func && checkfunctions) {
105
106 if (isa<CXXMethodDecl>(func)) // This will be visited next, so ignore it now
107 return;
108
109 if (functionIsOk(clazy::name(func))) {
110 return;
111 }
112
113 warn = true;
114 }
115 } else if (auto op = dyn_cast<BinaryOperator>(stm)) {
116 if (op->isAssignmentOp()) {
117 if (DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(op->getLHS())) {
118 ValueDecl *valueDecl = declRef->getDecl();
119 if (valueDecl && sm().isBeforeInSLocAddrSpace(clazy::getLocStart(valueDecl), stmStart)) {
120 // llvm::errs() << "reason3\n";
121 warn = true;
122 }
123 }
124 }
125 } else if (auto op = dyn_cast<UnaryOperator>(stm)) {
126 if (auto declRef = dyn_cast<DeclRefExpr>(op->getSubExpr())) {
127 ValueDecl *valueDecl = declRef->getDecl();
128 auto type = op->getOpcode();
129 if (type != UnaryOperatorKind::UO_Deref && type != UnaryOperatorKind::UO_AddrOf) {
130 if (valueDecl && sm().isBeforeInSLocAddrSpace(clazy::getLocStart(valueDecl), stmStart)) {
131 // llvm::errs() << "reason5 " << op->getOpcodeStr() << "\n";
132 warn = true;
133 }
134 }
135 }
136 }
137
138 if (warn) {
139 emitWarning(stmStart, "Code inside Q_ASSERT has side-effects but won't be built in release mode");
140 }
141 }
142