clang-tools
22.0.0git
llvm-project
clang-tools-extra
clang-tidy
cppcoreguidelines
AvoidCapturingLambdaCoroutinesCheck.cpp
Go to the documentation of this file.
1
//===----------------------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "
AvoidCapturingLambdaCoroutinesCheck.h
"
10
#include "clang/AST/ASTContext.h"
11
#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13
using namespace
clang::ast_matchers
;
14
15
namespace
clang::tidy::cppcoreguidelines
{
16
17
namespace
{
18
AST_MATCHER(LambdaExpr, hasCoroutineBody) {
19
const
Stmt *Body = Node.getBody();
20
return
Body !=
nullptr
&& CoroutineBodyStmt::classof(Body);
21
}
22
23
AST_MATCHER(LambdaExpr, hasCaptures) {
return
Node.capture_size() != 0U; }
24
}
// namespace
25
26
void
AvoidCapturingLambdaCoroutinesCheck::registerMatchers
(
27
MatchFinder *Finder) {
28
Finder->addMatcher(
29
lambdaExpr(hasCaptures(), hasCoroutineBody()).bind(
"lambda"
),
this
);
30
}
31
32
bool
AvoidCapturingLambdaCoroutinesCheck::isLanguageVersionSupported
(
33
const
LangOptions &LangOpts)
const
{
34
return
LangOpts.CPlusPlus20;
35
}
36
37
void
AvoidCapturingLambdaCoroutinesCheck::check
(
38
const
MatchFinder::MatchResult &Result) {
39
const
auto
*MatchedLambda = Result.Nodes.getNodeAs<LambdaExpr>(
"lambda"
);
40
diag(MatchedLambda->getExprLoc(),
41
"coroutine lambda may cause use-after-free, avoid captures or ensure "
42
"lambda closure object has guaranteed lifetime"
);
43
}
44
45
}
// namespace clang::tidy::cppcoreguidelines
AvoidCapturingLambdaCoroutinesCheck.h
clang::tidy::cppcoreguidelines::AvoidCapturingLambdaCoroutinesCheck::isLanguageVersionSupported
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Definition
AvoidCapturingLambdaCoroutinesCheck.cpp:32
clang::tidy::cppcoreguidelines::AvoidCapturingLambdaCoroutinesCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Definition
AvoidCapturingLambdaCoroutinesCheck.cpp:26
clang::tidy::cppcoreguidelines::AvoidCapturingLambdaCoroutinesCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
Definition
AvoidCapturingLambdaCoroutinesCheck.cpp:37
clang::ast_matchers
Definition
AbseilMatcher.h:13
clang::tidy::cppcoreguidelines
Definition
AvoidCapturingLambdaCoroutinesCheck.cpp:15
Generated on
for clang-tools by
1.14.0