File indexing completed on 2024-04-28 05:38:34

0001 /*
0002     SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company info@kdab.com
0003     SPDX-FileContributor: SĂ©rgio Martins <sergio.martins@kdab.com>
0004 
0005     SPDX-FileCopyrightText: 2015 Sergio Martins <smartins@kde.org>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef CLAZY_RESERVE_CANDIDATES
0011 #define CLAZY_RESERVE_CANDIDATES
0012 
0013 #include "checkbase.h"
0014 
0015 #include <string>
0016 #include <vector>
0017 
0018 class ClazyContext;
0019 
0020 namespace clang
0021 {
0022 class ValueDecl;
0023 class Expr;
0024 class CallExpr;
0025 class SourceLocation;
0026 class Stmt;
0027 }
0028 
0029 /**
0030  * Recommends places that are missing QList::reserve() or QVector::reserve().
0031  *
0032  * Only local variables are contemplated, containers that are members of a class are ignored due to
0033  * high false-positive rate.
0034  *
0035  * There some chance of false-positives.
0036  */
0037 class ReserveCandidates : public CheckBase
0038 {
0039 public:
0040     ReserveCandidates(const std::string &name, ClazyContext *context);
0041     void VisitStmt(clang::Stmt *stm) override;
0042 
0043 private:
0044     bool registerReserveStatement(clang::Stmt *stmt);
0045     bool containerWasReserved(clang::ValueDecl *) const;
0046     bool acceptsValueDecl(clang::ValueDecl *valueDecl) const;
0047     bool expressionIsComplex(clang::Expr *) const;
0048     bool loopIsComplex(clang::Stmt *, bool &isLoop) const;
0049     bool isInComplexLoop(clang::Stmt *, clang::SourceLocation declLocation, bool isMemberVariable) const;
0050     bool isReserveCandidate(clang::ValueDecl *valueDecl, clang::Stmt *loopBody, clang::CallExpr *callExpr) const;
0051 
0052     std::vector<clang::ValueDecl *> m_foundReserves;
0053 };
0054 
0055 #endif