| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016-2017 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "lambda-in-connect.h" |
| 8 |
#include "ClazyContext.h" |
| 9 |
#include "ContextUtils.h" |
| 10 |
#include "HierarchyUtils.h" |
| 11 |
#include "QtUtils.h" |
| 12 |
#include "StringUtils.h" |
| 13 |
|
| 14 |
#include <clang/AST/Decl.h> |
| 15 |
#include <clang/AST/Expr.h> |
| 16 |
#include <clang/AST/ExprCXX.h> |
| 17 |
#include <clang/AST/LambdaCapture.h> |
| 18 |
#include <clang/AST/Stmt.h> |
| 19 |
#include <clang/AST/Type.h> |
| 20 |
#include <clang/Basic/LLVM.h> |
| 21 |
#include <clang/Basic/Lambda.h> |
| 22 |
#include <llvm/ADT/iterator_range.h> |
| 23 |
#include <llvm/Support/Casting.h> |
| 24 |
|
| 25 |
using namespace clang; |
| 26 |
|
| 27 |
void LambdaInConnect::VisitStmt(clang::Stmt *stmt) |
| 28 |
{ |
| 29 |
auto *lambda = dyn_cast<LambdaExpr>(stmt); |
| 30 |
if (!lambda) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
auto captures = lambda->captures(); |
| 35 |
if (captures.begin() == captures.end()) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
auto *callExpr = clazy::getFirstParentOfType<CallExpr>(m_context->parentMap, lambda); |
| 40 |
if (clazy::qualifiedMethodName(callExpr) != "QObject::connect") { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
ValueDecl *senderDecl = clazy::signalSenderForConnect(callExpr); |
| 45 |
if (senderDecl) { |
| 46 |
const Type *t = senderDecl->getType().getTypePtrOrNull(); |
| 47 |
if (t && !t->isPointerType()) { |
| 48 |
return; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
ValueDecl *receiverDecl = clazy::signalReceiverForConnect(callExpr); |
| 53 |
if (receiverDecl) { |
| 54 |
const Type *t = receiverDecl->getType().getTypePtrOrNull(); |
| 55 |
if (t && !t->isPointerType()) { |
| 56 |
return; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
for (auto capture : captures) { |
| 61 |
if (capture.getCaptureKind() == clang::LCK_ByRef) { |
| 62 |
auto *declForCapture = capture.getCapturedVar(); |
| 63 |
if (declForCapture && declForCapture != receiverDecl && clazy::isValueDeclInFunctionContext(declForCapture)) { |
| 64 |
emitWarning(capture.getLocation(), "captured local variable by reference might go out of scope before lambda is called"); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} |