| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company [email protected] |
| 3 |
SPDX-FileContributor: Sérgio Martins <[email protected]> |
| 4 |
|
| 5 |
SPDX-FileCopyrightText: 2015 Sergio Martins <[email protected]> |
| 6 |
|
| 7 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "LoopUtils.h" |
| 11 |
#include "StringUtils.h" |
| 12 |
|
| 13 |
#include <clang/AST/Decl.h> |
| 14 |
#include <clang/AST/Expr.h> |
| 15 |
#include <clang/AST/ExprCXX.h> |
| 16 |
#include <clang/AST/ParentMap.h> |
| 17 |
#include <clang/AST/StmtCXX.h> |
| 18 |
#include <clang/Basic/LLVM.h> |
| 19 |
#include <clang/Basic/SourceLocation.h> |
| 20 |
#include <llvm/ADT/StringRef.h> |
| 21 |
|
| 22 |
namespace clang |
| 23 |
{ |
| 24 |
class CXXConstructorDecl; |
| 25 |
} // namespace clang |
| 26 |
|
| 27 |
using namespace clang; |
| 28 |
|
| 29 |
Stmt *clazy::bodyFromLoop(Stmt *loop) |
| 30 |
{ |
| 31 |
if (!loop) { |
| 32 |
return nullptr; |
| 33 |
} |
| 34 |
|
| 35 |
if (auto *forstm = dyn_cast<ForStmt>(loop)) { |
| 36 |
return forstm->getBody(); |
| 37 |
} |
| 38 |
|
| 39 |
if (auto *rangeLoop = dyn_cast<CXXForRangeStmt>(loop)) { |
| 40 |
return rangeLoop->getBody(); |
| 41 |
} |
| 42 |
|
| 43 |
if (auto *whilestm = dyn_cast<WhileStmt>(loop)) { |
| 44 |
return whilestm->getBody(); |
| 45 |
} |
| 46 |
|
| 47 |
if (auto *dostm = dyn_cast<DoStmt>(loop)) { |
| 48 |
return dostm->getBody(); |
| 49 |
} |
| 50 |
|
| 51 |
return nullptr; |
| 52 |
} |
| 53 |
|
| 54 |
bool clazy::loopCanBeInterrupted(clang::Stmt *stmt, const clang::SourceManager &sm, clang::SourceLocation onlyBeforeThisLoc) |
| 55 |
{ |
| 56 |
if (!stmt) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
if (isa<ReturnStmt>(stmt) || isa<BreakStmt>(stmt) || isa<ContinueStmt>(stmt)) { |
| 61 |
if (onlyBeforeThisLoc.isValid()) { |
| 62 |
FullSourceLoc sourceLoc(stmt->getBeginLoc(), sm); |
| 63 |
FullSourceLoc otherSourceLoc(onlyBeforeThisLoc, sm); |
| 64 |
if (sourceLoc.isBeforeInTranslationUnitThan(otherSourceLoc)) { |
| 65 |
return true; |
| 66 |
} |
| 67 |
} else { |
| 68 |
return true; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return clazy::any_of(stmt->children(), [&sm, onlyBeforeThisLoc](Stmt *s) { |
| 73 |
return clazy::loopCanBeInterrupted(s, sm, onlyBeforeThisLoc); |
| 74 |
}); |
| 75 |
} |
| 76 |
|
| 77 |
clang::Expr *clazy::containerExprForLoop(Stmt *loop, const std::string &qtNamespace) |
| 78 |
{ |
| 79 |
if (!loop) { |
| 80 |
return nullptr; |
| 81 |
} |
| 82 |
|
| 83 |
if (auto *rangeLoop = dyn_cast<CXXForRangeStmt>(loop)) { |
| 84 |
return rangeLoop->getRangeInit(); |
| 85 |
} |
| 86 |
|
| 87 |
// foreach with C++14 |
| 88 |
if (auto *constructExpr = dyn_cast<CXXConstructExpr>(loop)) { |
| 89 |
if (constructExpr->getNumArgs() < 1) { |
| 90 |
return nullptr; |
| 91 |
} |
| 92 |
|
| 93 |
const CXXConstructorDecl *constructorDecl = constructExpr->getConstructor(); |
| 94 |
if (!constructorDecl || constructorDecl->getQualifiedNameAsString() != clazy::qtNamespaced("QForeachContainer", qtNamespace)) { |
| 95 |
return nullptr; |
| 96 |
} |
| 97 |
|
| 98 |
return constructExpr; |
| 99 |
} |
| 100 |
|
| 101 |
// foreach with C++17 |
| 102 |
if (auto *bindTempExpr = dyn_cast<CXXBindTemporaryExpr>(loop)) { |
| 103 |
auto *callExpr = dyn_cast<CallExpr>(bindTempExpr->getSubExpr()); |
| 104 |
if (!callExpr) |
| 105 |
return nullptr; |
| 106 |
FunctionDecl *func = callExpr->getDirectCallee(); |
| 107 |
if (!func || func->getQualifiedNameAsString() != clazy::qtNamespaced("QtPrivate::qMakeForeachContainer", qtNamespace)) { |
| 108 |
return nullptr; |
| 109 |
} |
| 110 |
if (callExpr->getNumArgs() < 1) { |
| 111 |
return nullptr; |
| 112 |
} |
| 113 |
return callExpr->getArg(0); |
| 114 |
} |
| 115 |
|
| 116 |
return nullptr; |
| 117 |
} |
| 118 |
|
| 119 |
VarDecl *clazy::containerDeclForLoop(clang::Stmt *loop, const std::string &qtNamespace) |
| 120 |
{ |
| 121 |
Expr *expr = containerExprForLoop(loop, qtNamespace); |
| 122 |
if (!expr) { |
| 123 |
return nullptr; |
| 124 |
} |
| 125 |
|
| 126 |
auto *declRef = dyn_cast<DeclRefExpr>(expr); |
| 127 |
if (!declRef) { |
| 128 |
return nullptr; |
| 129 |
} |
| 130 |
|
| 131 |
ValueDecl *valueDecl = declRef->getDecl(); |
| 132 |
return valueDecl ? dyn_cast<VarDecl>(valueDecl) : nullptr; |
| 133 |
} |
| 134 |
|
| 135 |
Stmt *clazy::isInLoop(clang::ParentMap *pmap, clang::Stmt *stmt) |
| 136 |
{ |
| 137 |
if (!stmt) { |
| 138 |
return nullptr; |
| 139 |
} |
| 140 |
|
| 141 |
Stmt *p = pmap->getParent(stmt); |
| 142 |
while (p) { |
| 143 |
if (clazy::isLoop(p)) { |
| 144 |
return p; |
| 145 |
} |
| 146 |
p = pmap->getParent(p); |
| 147 |
} |
| 148 |
|
| 149 |
return nullptr; |
| 150 |
} |