|
| 1 | +//===--- CapturingThisInMemberVariableCheck.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 "CapturingThisInMemberVariableCheck.h" |
| 10 | +#include "../utils/Matchers.h" |
| 11 | +#include "../utils/OptionsUtils.h" |
| 12 | +#include "clang/AST/DeclCXX.h" |
| 13 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 14 | +#include "clang/ASTMatchers/ASTMatchers.h" |
| 15 | +#include "clang/ASTMatchers/ASTMatchersMacros.h" |
| 16 | + |
| 17 | +using namespace clang::ast_matchers; |
| 18 | + |
| 19 | +namespace clang::tidy::bugprone { |
| 20 | + |
| 21 | +namespace { |
| 22 | + |
| 23 | +AST_MATCHER(CXXRecordDecl, correctHandleCaptureThisLambda) { |
| 24 | + // unresolved |
| 25 | + if (Node.needsOverloadResolutionForCopyConstructor() && |
| 26 | + Node.needsImplicitCopyConstructor()) |
| 27 | + return false; |
| 28 | + if (Node.needsOverloadResolutionForMoveConstructor() && |
| 29 | + Node.needsImplicitMoveConstructor()) |
| 30 | + return false; |
| 31 | + if (Node.needsOverloadResolutionForCopyAssignment() && |
| 32 | + Node.needsImplicitCopyAssignment()) |
| 33 | + return false; |
| 34 | + if (Node.needsOverloadResolutionForMoveAssignment() && |
| 35 | + Node.needsImplicitMoveAssignment()) |
| 36 | + return false; |
| 37 | + // default but not deleted |
| 38 | + if (Node.hasSimpleCopyConstructor()) |
| 39 | + return false; |
| 40 | + if (Node.hasSimpleMoveConstructor()) |
| 41 | + return false; |
| 42 | + if (Node.hasSimpleCopyAssignment()) |
| 43 | + return false; |
| 44 | + if (Node.hasSimpleMoveAssignment()) |
| 45 | + return false; |
| 46 | + |
| 47 | + for (CXXConstructorDecl const *C : Node.ctors()) { |
| 48 | + if (C->isCopyOrMoveConstructor() && C->isDefaulted() && !C->isDeleted()) |
| 49 | + return false; |
| 50 | + } |
| 51 | + for (CXXMethodDecl const *M : Node.methods()) { |
| 52 | + if (M->isCopyAssignmentOperator()) |
| 53 | + llvm::errs() << M->isDeleted() << "\n"; |
| 54 | + if (M->isCopyAssignmentOperator() && M->isDefaulted() && !M->isDeleted()) |
| 55 | + return false; |
| 56 | + if (M->isMoveAssignmentOperator() && M->isDefaulted() && !M->isDeleted()) |
| 57 | + return false; |
| 58 | + } |
| 59 | + // FIXME: find ways to identifier correct handle capture this lambda |
| 60 | + return true; |
| 61 | +} |
| 62 | + |
| 63 | +} // namespace |
| 64 | + |
| 65 | +constexpr const char *DefaultFunctionWrapperTypes = |
| 66 | + "::std::function;::std::move_only_function;::boost::function"; |
| 67 | + |
| 68 | +CapturingThisInMemberVariableCheck::CapturingThisInMemberVariableCheck( |
| 69 | + StringRef Name, ClangTidyContext *Context) |
| 70 | + : ClangTidyCheck(Name, Context), |
| 71 | + FunctionWrapperTypes(utils::options::parseStringList( |
| 72 | + Options.get("FunctionWrapperTypes", DefaultFunctionWrapperTypes))) {} |
| 73 | +void CapturingThisInMemberVariableCheck::storeOptions( |
| 74 | + ClangTidyOptions::OptionMap &Opts) { |
| 75 | + Options.store(Opts, "FunctionWrapperTypes", |
| 76 | + utils::options::serializeStringList(FunctionWrapperTypes)); |
| 77 | +} |
| 78 | + |
| 79 | +void CapturingThisInMemberVariableCheck::registerMatchers(MatchFinder *Finder) { |
| 80 | + auto IsStdFunctionField = |
| 81 | + fieldDecl(hasType(cxxRecordDecl( |
| 82 | + matchers::matchesAnyListedName(FunctionWrapperTypes)))) |
| 83 | + .bind("field"); |
| 84 | + auto CaptureThis = lambdaCapture(anyOf( |
| 85 | + // [this] |
| 86 | + capturesThis(), |
| 87 | + // [self = this] |
| 88 | + capturesVar(varDecl(hasInitializer(cxxThisExpr()))))); |
| 89 | + auto IsLambdaCapturingThis = |
| 90 | + lambdaExpr(hasAnyCapture(CaptureThis.bind("capture"))).bind("lambda"); |
| 91 | + auto IsInitWithLambda = |
| 92 | + anyOf(IsLambdaCapturingThis, |
| 93 | + cxxConstructExpr(hasArgument(0, IsLambdaCapturingThis))); |
| 94 | + Finder->addMatcher( |
| 95 | + cxxRecordDecl( |
| 96 | + anyOf(has(cxxConstructorDecl( |
| 97 | + unless(isCopyConstructor()), unless(isMoveConstructor()), |
| 98 | + hasAnyConstructorInitializer(cxxCtorInitializer( |
| 99 | + isMemberInitializer(), forField(IsStdFunctionField), |
| 100 | + withInitializer(IsInitWithLambda))))), |
| 101 | + has(fieldDecl(IsStdFunctionField, |
| 102 | + hasInClassInitializer(IsInitWithLambda)))), |
| 103 | + unless(correctHandleCaptureThisLambda())), |
| 104 | + this); |
| 105 | +} |
| 106 | + |
| 107 | +void CapturingThisInMemberVariableCheck::check( |
| 108 | + const MatchFinder::MatchResult &Result) { |
| 109 | + const auto *Capture = Result.Nodes.getNodeAs<LambdaCapture>("capture"); |
| 110 | + const auto *Lambda = Result.Nodes.getNodeAs<LambdaExpr>("lambda"); |
| 111 | + const auto *Field = Result.Nodes.getNodeAs<FieldDecl>("field"); |
| 112 | + diag(Lambda->getBeginLoc(), |
| 113 | + "'this' captured by a lambda and stored in a class member variable; " |
| 114 | + "disable implicit class copying/moving to prevent potential " |
| 115 | + "use-after-free") |
| 116 | + << Capture->getLocation(); |
| 117 | + diag(Field->getLocation(), |
| 118 | + "class member of type '%0' that stores captured 'this'", |
| 119 | + DiagnosticIDs::Note) |
| 120 | + << Field->getType().getAsString(); |
| 121 | +} |
| 122 | + |
| 123 | +} // namespace clang::tidy::bugprone |
0 commit comments