[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang-tools-extra / clang-tidy / cppcoreguidelines / MisleadingCaptureDefaultByValueCheck.cpp
blob00dfa17a1ccf611e0eeb8928028581da3dc51f75
1 //===--- MisleadingCaptureDefaultByValueCheck.cpp - clang-tidy-------------===//
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 //===----------------------------------------------------------------------===//
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"
15 #include <algorithm>
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"),
28 this);
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);
39 return AddressofLoc;
41 return Capture.getLocation();
44 return Lambda->getIntroducerRange().getEnd();
47 static std::string createReplacementText(const LambdaExpr *Lambda) {
48 std::string Replacement;
49 llvm::raw_string_ostream Stream(Replacement);
51 auto AppendName = [&](llvm::StringRef Name) {
52 if (!Replacement.empty()) {
53 Stream << ", ";
55 if (Lambda->getCaptureDefault() == LCD_ByRef && Name != "this") {
56 Stream << "&" << Name;
57 } else {
58 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()) {
67 AppendName("this");
70 if (!Replacement.empty() &&
71 Lambda->explicit_capture_begin() != Lambda->explicit_capture_end()) {
72 // Add back separator if we are adding explicit capture variables.
73 Stream << ", ";
75 return Replacement;
78 void MisleadingCaptureDefaultByValueCheck::check(
79 const MatchFinder::MatchResult &Result) {
80 const auto *Lambda = Result.Nodes.getNodeAs<LambdaExpr>("lambda");
81 if (!Lambda)
82 return;
84 if (Lambda->getCaptureDefault() == LCD_ByCopy) {
85 bool IsThisImplicitlyCaptured = std::any_of(
86 Lambda->implicit_capture_begin(), Lambda->implicit_capture_end(),
87 [](const LambdaCapture &Capture) { return Capture.capturesThis(); });
88 auto Diag = diag(Lambda->getCaptureDefaultLoc(),
89 "lambdas that %select{|implicitly }0capture 'this' "
90 "should not specify a by-value capture default")
91 << IsThisImplicitlyCaptured;
93 std::string ReplacementText = createReplacementText(Lambda);
94 SourceLocation DefaultCaptureEnd =
95 findDefaultCaptureEnd(Lambda, *Result.Context);
96 Diag << FixItHint::CreateReplacement(
97 CharSourceRange::getCharRange(Lambda->getCaptureDefaultLoc(),
98 DefaultCaptureEnd),
99 ReplacementText);
103 } // namespace clang::tidy::cppcoreguidelines