1 //===--- MisleadingCaptureDefaultByValueCheck.cpp - clang-tidy-------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "MisleadingCaptureDefaultByValueCheck.h"
10 #include "../utils/LexerUtils.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "llvm/Support/raw_ostream.h"
17 using namespace clang::ast_matchers
;
19 namespace clang::tidy::cppcoreguidelines
{
21 MisleadingCaptureDefaultByValueCheck::MisleadingCaptureDefaultByValueCheck(
22 StringRef Name
, ClangTidyContext
*Context
)
23 : ClangTidyCheck(Name
, Context
) {}
25 void MisleadingCaptureDefaultByValueCheck::registerMatchers(
26 MatchFinder
*Finder
) {
27 Finder
->addMatcher(lambdaExpr(hasAnyCapture(capturesThis())).bind("lambda"),
31 static SourceLocation
findDefaultCaptureEnd(const LambdaExpr
*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();
37 SourceLocation AddressofLoc
= utils::lexer::findPreviousTokenKind(
38 Capture
.getLocation(), SourceMgr
, Context
.getLangOpts(), tok::amp
);
41 return Capture
.getLocation();
45 return Lambda
->getIntroducerRange().getEnd();
48 static std::string
createReplacementText(const LambdaExpr
*Lambda
) {
49 std::string Replacement
;
50 llvm::raw_string_ostream
Stream(Replacement
);
52 auto AppendName
= [&](llvm::StringRef Name
) {
53 if (Replacement
.size() != 0) {
56 if (Lambda
->getCaptureDefault() == LCD_ByRef
&& Name
!= "this") {
57 Stream
<< "&" << Name
;
63 for (const LambdaCapture
&Capture
: Lambda
->implicit_captures()) {
64 assert(Capture
.isImplicit());
65 if (Capture
.capturesVariable() && Capture
.isImplicit()) {
66 AppendName(Capture
.getCapturedVar()->getName());
67 } else if (Capture
.capturesThis()) {
71 if (Replacement
.size() &&
72 Lambda
->explicit_capture_begin() != Lambda
->explicit_capture_end()) {
73 // Add back separator if we are adding explicit capture variables.
79 void MisleadingCaptureDefaultByValueCheck::check(
80 const MatchFinder::MatchResult
&Result
) {
81 const auto *Lambda
= Result
.Nodes
.getNodeAs
<LambdaExpr
>("lambda");
85 if (Lambda
->getCaptureDefault() == LCD_ByCopy
) {
86 bool IsThisImplicitlyCaptured
= std::any_of(
87 Lambda
->implicit_capture_begin(), Lambda
->implicit_capture_end(),
88 [](const LambdaCapture
&Capture
) { return Capture
.capturesThis(); });
89 auto Diag
= diag(Lambda
->getCaptureDefaultLoc(),
90 "lambdas that %select{|implicitly }0capture 'this' "
91 "should not specify a by-value capture default")
92 << IsThisImplicitlyCaptured
;
94 std::string ReplacementText
= createReplacementText(Lambda
);
95 SourceLocation DefaultCaptureEnd
=
96 findDefaultCaptureEnd(Lambda
, *Result
.Context
);
97 Diag
<< FixItHint::CreateReplacement(
98 CharSourceRange::getCharRange(Lambda
->getCaptureDefaultLoc(),
104 } // namespace clang::tidy::cppcoreguidelines