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

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 FOREACH_DETACHMENTS_H
0026 #define FOREACH_DETACHMENTS_H
0027 
0028 #include "checkbase.h"
0029 
0030 #include <string>
0031 
0032 class ClazyContext;
0033 
0034 namespace clang {
0035 class ForStmt;
0036 class ValueDecl;
0037 class Stmt;
0038 class CXXForRangeStmt;
0039 }
0040 
0041 /**
0042  * - Foreach:
0043  *   - Finds places where you're detaching the foreach container.
0044  *   - Finds places where big or non-trivial types are passed by value instead of const-ref.
0045  *   - Finds places where you're using foreach on STL containers. It causes deep-copy.
0046  * - For Range Loops:
0047  *   - Finds places where you're using C++11 for range loops with Qt containers. (potential detach)
0048  */
0049 class Foreach
0050     : public CheckBase
0051 {
0052 public:
0053     Foreach(const std::string &name, ClazyContext *context);
0054     void VisitStmt(clang::Stmt *stmt) override;
0055 private:
0056     void checkBigTypeMissingRef();
0057     bool containsDetachments(clang::Stmt *stmt, clang::ValueDecl *containerValueDecl);
0058     clang::ForStmt *m_lastForStmt = nullptr;
0059 };
0060 
0061 #endif