clang-tools 22.0.0git
MisleadingCaptureDefaultByValueCheck.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
10#include "../utils/LexerUtils.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "llvm/Support/raw_ostream.h"
14
15#include <algorithm>
16
17using namespace clang::ast_matchers;
18
20
24
26 MatchFinder *Finder) {
27 Finder->addMatcher(lambdaExpr(hasAnyCapture(capturesThis())).bind("lambda"),
28 this);
29}
30
31static 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);
39 return AddressofLoc;
40 }
41 return Capture.getLocation();
42 }
43 }
44 return Lambda->getIntroducerRange().getEnd();
45}
46
47static std::string createReplacementText(const LambdaExpr *Lambda) {
48 std::string Replacement;
49 llvm::raw_string_ostream Stream(Replacement);
50
51 auto AppendName = [&](llvm::StringRef Name) {
52 if (!Replacement.empty())
53 Stream << ", ";
54 if (Lambda->getCaptureDefault() == LCD_ByRef && Name != "this")
55 Stream << "&" << Name;
56 else
57 Stream << Name;
58 };
59
60 for (const LambdaCapture &Capture : Lambda->implicit_captures()) {
61 assert(Capture.isImplicit());
62 if (Capture.capturesVariable() && Capture.isImplicit())
63 AppendName(Capture.getCapturedVar()->getName());
64 else if (Capture.capturesThis())
65 AppendName("this");
66 }
67 if (!Replacement.empty() && !Lambda->explicit_captures().empty()) {
68 // Add back separator if we are adding explicit capture variables.
69 Stream << ", ";
70 }
71 return Replacement;
72}
73
75 const MatchFinder::MatchResult &Result) {
76 const auto *Lambda = Result.Nodes.getNodeAs<LambdaExpr>("lambda");
77 if (!Lambda)
78 return;
79
80 if (Lambda->getCaptureDefault() == LCD_ByCopy) {
81 const bool IsThisImplicitlyCaptured = std::any_of(
82 Lambda->implicit_capture_begin(), Lambda->implicit_capture_end(),
83 [](const LambdaCapture &Capture) { return Capture.capturesThis(); });
84 auto Diag = diag(Lambda->getCaptureDefaultLoc(),
85 "lambdas that %select{|implicitly }0capture 'this' "
86 "should not specify a by-value capture default")
87 << IsThisImplicitlyCaptured;
88
89 const std::string ReplacementText = createReplacementText(Lambda);
90 const SourceLocation DefaultCaptureEnd =
91 findDefaultCaptureEnd(Lambda, *Result.Context);
92 Diag << FixItHint::CreateReplacement(
93 CharSourceRange::getCharRange(Lambda->getCaptureDefaultLoc(),
94 DefaultCaptureEnd),
95 ReplacementText);
96 }
97}
98
99} // namespace clang::tidy::cppcoreguidelines
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
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)