File indexing completed on 2024-04-14 05:32:07

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