10#include "../utils/LexerUtils.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "llvm/Support/raw_ostream.h"
26 MatchFinder *Finder) {
27 Finder->addMatcher(lambdaExpr(hasAnyCapture(capturesThis())).bind(
"lambda"),
32 ASTContext &Context) {
33 for (
const LambdaCapture &Capture : Lambda->explicit_captures()) {
34 if (Capture.isExplicit()) {
35 if (Capture.getCaptureKind() == LCK_ByRef) {
36 const SourceManager &SourceMgr = Context.getSourceManager();
38 Capture.getLocation(), SourceMgr, Context.getLangOpts(), tok::amp);
41 return Capture.getLocation();
44 return Lambda->getIntroducerRange().getEnd();
48 std::string Replacement;
49 llvm::raw_string_ostream Stream(Replacement);
51 auto AppendName = [&](llvm::StringRef
Name) {
52 if (!Replacement.empty()) {
55 if (Lambda->getCaptureDefault() == LCD_ByRef &&
Name !=
"this") {
56 Stream <<
"&" <<
Name;
62 for (
const LambdaCapture &Capture : Lambda->implicit_captures()) {
63 assert(Capture.isImplicit());
64 if (Capture.capturesVariable() && Capture.isImplicit()) {
65 AppendName(Capture.getCapturedVar()->getName());
66 }
else if (Capture.capturesThis()) {
70 if (!Replacement.empty() && !Lambda->explicit_captures().empty()) {
78 const MatchFinder::MatchResult &Result) {
79 const auto *Lambda = Result.Nodes.getNodeAs<LambdaExpr>(
"lambda");
83 if (Lambda->getCaptureDefault() == LCD_ByCopy) {
84 bool IsThisImplicitlyCaptured = std::any_of(
85 Lambda->implicit_capture_begin(), Lambda->implicit_capture_end(),
86 [](
const LambdaCapture &Capture) { return Capture.capturesThis(); });
87 auto Diag =
diag(Lambda->getCaptureDefaultLoc(),
88 "lambdas that %select{|implicitly }0capture 'this' "
89 "should not specify a by-value capture default")
90 << IsThisImplicitlyCaptured;
93 SourceLocation DefaultCaptureEnd =
95 Diag << FixItHint::CreateReplacement(
96 CharSourceRange::getCharRange(Lambda->getCaptureDefaultLoc(),
llvm::SmallString< 256U > Name
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
MisleadingCaptureDefaultByValueCheck(StringRef Name, ClangTidyContext *Context)
static std::string createReplacementText(const LambdaExpr *Lambda)
static SourceLocation findDefaultCaptureEnd(const LambdaExpr *Lambda, ASTContext &Context)
SourceLocation findPreviousTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, tok::TokenKind TK)