File indexing completed on 2024-04-28 16:57:51

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_LOOP_UTILS_H
0026 #define CLAZY_LOOP_UTILS_H
0027 
0028 #include "clazy_stl.h"
0029 
0030 #include <clang/AST/StmtCXX.h>
0031 #include <clang/AST/Stmt.h>
0032 #include <llvm/Support/Casting.h>
0033 
0034 namespace clang {
0035 class Stmt;
0036 class SourceManager;
0037 class SourceLocation;
0038 class Expr;
0039 class ParentMap;
0040 class VarDecl;
0041 }
0042 
0043 namespace clazy {
0044 /**
0045  * Returns the body of a for, range-foor, while or do-while loop
0046  */
0047 clang::Stmt *bodyFromLoop(clang::Stmt *);
0048 
0049 /**
0050  * Recursively goes through stmt's children and returns true if it finds a "break", "continue" or a "return" stmt
0051  * All child statements that are on a source code line <
0052  * If onlyBeforThisLoc is valid, then this function will only return true if the break/return/continue happens before
0053  */
0054 bool loopCanBeInterrupted(clang::Stmt *loop, const clang::SourceManager &sm,
0055                           clang::SourceLocation onlyBeforeThisLoc);
0056 
0057 /**
0058  * Returns true if stmt is a for, while or do-while loop
0059  */
0060 inline bool isLoop(clang::Stmt *stmt)
0061 {
0062     return llvm::isa<clang::DoStmt>(stmt)  || llvm::isa<clang::WhileStmt>(stmt) ||
0063            llvm::isa<clang::ForStmt>(stmt) || llvm::isa<clang::CXXForRangeStmt>(stmt);
0064 }
0065 
0066 /**
0067  * Returns the container expression for a range-loop or Q_FOREACH
0068  *
0069  * Q_FOREACH (auto f, expression) or for (auto i : expression)
0070  */
0071 clang::Expr* containerExprForLoop(clang::Stmt *loop);
0072 
0073 /**
0074  * Returns the container decl for a range-loop or Q_FOREACH
0075  *
0076  * Q_FOREACH (auto f, container) or for (auto i : container)
0077  */
0078 clang::VarDecl* containerDeclForLoop(clang::Stmt *loop);
0079 
0080 /**
0081  * Returns true of stmt is inside a for, while or do-while loop.
0082  * If yes, returns the loop statement, otherwise nullptr.
0083  */
0084 clang::Stmt* isInLoop(clang::ParentMap *pmap, clang::Stmt *stmt);
0085 }
0086 
0087 #endif