File indexing completed on 2024-05-12 17:13:19

0001 /*
0002     This file is part of the clazy static checker.
0003 
0004     Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
0005     Author: SĂ©rgio Martins <sergio.martins@kdab.com>
0006 
0007     Copyright (C) 2015 Sergio Martins <smartins@kde.org>
0008 
0009     This library is free software; you can redistribute it and/or
0010     modify it under the terms of the GNU Library General Public
0011     License as published by the Free Software Foundation; either
0012     version 2 of the License, or (at your option) any later version.
0013 
0014     This library is distributed in the hope that it will be useful,
0015     but WITHOUT ANY WARRANTY; without even the implied warranty of
0016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0017     Library General Public License for more details.
0018 
0019     You should have received a copy of the GNU Library General Public License
0020     along with this library; see the file COPYING.LIB.  If not, write to
0021     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0022     Boston, MA 02110-1301, USA.
0023 */
0024 
0025 #ifndef CLAZY_RESERVE_CANDIDATES
0026 #define CLAZY_RESERVE_CANDIDATES
0027 
0028 #include "checkbase.h"
0029 
0030 #include <vector>
0031 #include <string>
0032 
0033 class ClazyContext;
0034 
0035 namespace clang {
0036 class ValueDecl;
0037 class Expr;
0038 class CallExpr;
0039 class SourceLocation;
0040 class Stmt;
0041 }
0042 
0043 /**
0044  * Recommends places that are missing QList::reserve() or QVector::reserve().
0045  *
0046  * Only local variables are contemplated, containers that are members of a class are ignored due to
0047  * high false-positive rate.
0048  *
0049  * There some chance of false-positives.
0050  */
0051 class ReserveCandidates
0052     : public CheckBase
0053 {
0054 public:
0055     ReserveCandidates(const std::string &name, ClazyContext *context);
0056     void VisitStmt(clang::Stmt *stm) override;
0057 
0058 private:
0059     bool registerReserveStatement(clang::Stmt *stmt);
0060     bool containerWasReserved(clang::ValueDecl*) const;
0061     bool acceptsValueDecl(clang::ValueDecl *valueDecl) const;
0062     bool expressionIsComplex(clang::Expr *) const;
0063     bool loopIsComplex(clang::Stmt *, bool &isLoop) const;
0064     bool isInComplexLoop(clang::Stmt *, clang::SourceLocation declLocation, bool isMemberVariable) const;
0065     bool isReserveCandidate(clang::ValueDecl *valueDecl, clang::Stmt *loopBody, clang::CallExpr *callExpr) const;
0066 
0067     std::vector<clang::ValueDecl*> m_foundReserves;
0068 };
0069 
0070 #endif