| 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 | //===----------------------------------------------------------------------===// |
| 8 | |
| 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" |
| 14 | |
| 15 | #include <algorithm> |
| 16 | |
| 17 | using namespace clang::ast_matchers; |
| 18 | |
| 19 | namespace clang::tidy::cppcoreguidelines { |
| 20 | |
| 21 | MisleadingCaptureDefaultByValueCheck::MisleadingCaptureDefaultByValueCheck( |
| 22 | StringRef Name, ClangTidyContext *Context) |
| 23 | : ClangTidyCheck(Name, Context) {} |
| 24 | |
| 25 | void MisleadingCaptureDefaultByValueCheck::registerMatchers( |
| 26 | MatchFinder *Finder) { |
| 27 | Finder->addMatcher(NodeMatch: lambdaExpr(hasAnyCapture(InnerMatcher: capturesThis())).bind(ID: "lambda" ), |
| 28 | Action: this); |
| 29 | } |
| 30 | |
| 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 | Start: Capture.getLocation(), SM: SourceMgr, LangOpts: Context.getLangOpts(), TK: tok::amp); |
| 39 | return AddressofLoc; |
| 40 | } |
| 41 | return Capture.getLocation(); |
| 42 | } |
| 43 | } |
| 44 | return Lambda->getIntroducerRange().getEnd(); |
| 45 | } |
| 46 | |
| 47 | static 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 | } |
| 55 | if (Lambda->getCaptureDefault() == LCD_ByRef && Name != "this" ) { |
| 56 | Stream << "&" << Name; |
| 57 | } else { |
| 58 | Stream << Name; |
| 59 | } |
| 60 | }; |
| 61 | |
| 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" ); |
| 68 | } |
| 69 | } |
| 70 | if (!Replacement.empty() && !Lambda->explicit_captures().empty()) { |
| 71 | // Add back separator if we are adding explicit capture variables. |
| 72 | Stream << ", " ; |
| 73 | } |
| 74 | return Replacement; |
| 75 | } |
| 76 | |
| 77 | void MisleadingCaptureDefaultByValueCheck::check( |
| 78 | const MatchFinder::MatchResult &Result) { |
| 79 | const auto *Lambda = Result.Nodes.getNodeAs<LambdaExpr>(ID: "lambda" ); |
| 80 | if (!Lambda) |
| 81 | return; |
| 82 | |
| 83 | if (Lambda->getCaptureDefault() == LCD_ByCopy) { |
| 84 | bool IsThisImplicitlyCaptured = std::any_of( |
| 85 | first: Lambda->implicit_capture_begin(), last: Lambda->implicit_capture_end(), |
| 86 | pred: [](const LambdaCapture &Capture) { return Capture.capturesThis(); }); |
| 87 | auto Diag = diag(Loc: Lambda->getCaptureDefaultLoc(), |
| 88 | Description: "lambdas that %select{|implicitly }0capture 'this' " |
| 89 | "should not specify a by-value capture default" ) |
| 90 | << IsThisImplicitlyCaptured; |
| 91 | |
| 92 | std::string ReplacementText = createReplacementText(Lambda); |
| 93 | SourceLocation DefaultCaptureEnd = |
| 94 | findDefaultCaptureEnd(Lambda, Context&: *Result.Context); |
| 95 | Diag << FixItHint::CreateReplacement( |
| 96 | RemoveRange: CharSourceRange::getCharRange(B: Lambda->getCaptureDefaultLoc(), |
| 97 | E: DefaultCaptureEnd), |
| 98 | Code: ReplacementText); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | } // namespace clang::tidy::cppcoreguidelines |
| 103 | |