2016-12-18 00:35:53 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "RefCountedInsideLambdaChecker.h"
|
|
|
|
#include "CustomMatchers.h"
|
|
|
|
|
|
|
|
RefCountedMap RefCountedClasses;
|
|
|
|
|
2017-10-20 20:11:50 +03:00
|
|
|
void RefCountedInsideLambdaChecker::registerMatchers(MatchFinder *AstMatcher) {
|
2016-12-18 00:35:53 +03:00
|
|
|
// We want to reject any code which captures a pointer to an object of a
|
|
|
|
// refcounted type, and then lets that value escape. As a primitive analysis,
|
|
|
|
// we reject any occurances of the lambda as a template parameter to a class
|
|
|
|
// (which could allow it to escape), as well as any presence of such a lambda
|
|
|
|
// in a return value (either from lambdas, or in c++14, auto functions).
|
|
|
|
//
|
|
|
|
// We check these lambdas' capture lists for raw pointers to refcounted types.
|
2017-10-20 20:11:50 +03:00
|
|
|
AstMatcher->addMatcher(functionDecl(returns(recordType(hasDeclaration(
|
|
|
|
cxxRecordDecl(isLambdaDecl()).bind("decl"))))),
|
|
|
|
this);
|
|
|
|
AstMatcher->addMatcher(lambdaExpr().bind("lambdaExpr"), this);
|
2016-12-18 05:14:37 +03:00
|
|
|
AstMatcher->addMatcher(
|
2017-10-20 20:11:50 +03:00
|
|
|
classTemplateSpecializationDecl(
|
|
|
|
hasAnyTemplateArgument(refersToType(recordType(
|
|
|
|
hasDeclaration(cxxRecordDecl(isLambdaDecl()).bind("decl")))))),
|
2016-12-18 00:35:53 +03:00
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
2017-02-04 00:53:03 +03:00
|
|
|
void RefCountedInsideLambdaChecker::emitDiagnostics(SourceLocation Loc,
|
|
|
|
StringRef Name,
|
|
|
|
QualType Type) {
|
2017-10-20 20:11:50 +03:00
|
|
|
diag(Loc,
|
|
|
|
"Refcounted variable '%0' of type %1 cannot be captured by a lambda",
|
|
|
|
DiagnosticIDs::Error)
|
|
|
|
<< Name << Type;
|
|
|
|
diag(Loc, "Please consider using a smart pointer", DiagnosticIDs::Note);
|
2017-02-04 00:53:03 +03:00
|
|
|
}
|
|
|
|
|
2016-12-18 05:14:37 +03:00
|
|
|
void RefCountedInsideLambdaChecker::check(
|
2016-12-18 00:35:53 +03:00
|
|
|
const MatchFinder::MatchResult &Result) {
|
2017-10-20 20:11:50 +03:00
|
|
|
static DenseSet<const CXXRecordDecl *> CheckedDecls;
|
2016-12-18 00:35:53 +03:00
|
|
|
|
|
|
|
const CXXRecordDecl *Lambda = Result.Nodes.getNodeAs<CXXRecordDecl>("decl");
|
|
|
|
|
|
|
|
if (const LambdaExpr *OuterLambda =
|
2017-10-20 20:11:50 +03:00
|
|
|
Result.Nodes.getNodeAs<LambdaExpr>("lambdaExpr")) {
|
2016-12-18 00:35:53 +03:00
|
|
|
const CXXMethodDecl *OpCall = OuterLambda->getCallOperator();
|
|
|
|
QualType ReturnTy = OpCall->getReturnType();
|
|
|
|
if (const CXXRecordDecl *Record = ReturnTy->getAsCXXRecordDecl()) {
|
|
|
|
Lambda = Record;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Lambda || !Lambda->isLambda()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't report errors on the same declarations more than once.
|
|
|
|
if (CheckedDecls.count(Lambda)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CheckedDecls.insert(Lambda);
|
|
|
|
|
2017-02-04 00:53:03 +03:00
|
|
|
bool StrongRefToThisCaptured = false;
|
|
|
|
|
2017-10-20 20:11:50 +03:00
|
|
|
for (const LambdaCapture &Capture : Lambda->captures()) {
|
2017-02-04 00:53:03 +03:00
|
|
|
// Check if any of the captures are ByRef. If they are, we have nothing to
|
2017-10-20 20:11:50 +03:00
|
|
|
// report, as it's OK to capture raw pointers to refcounted objects so long
|
|
|
|
// as the Lambda doesn't escape the current scope, which is required by
|
|
|
|
// ByRef captures already.
|
2017-02-04 00:53:03 +03:00
|
|
|
if (Capture.getCaptureKind() == LCK_ByRef) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-20 20:11:50 +03:00
|
|
|
// Check if this capture is byvalue, and captures a strong reference to
|
|
|
|
// this.
|
|
|
|
// XXX: Do we want to make sure that this type which we are capturing is a
|
|
|
|
// "Smart Pointer" somehow?
|
|
|
|
if (!StrongRefToThisCaptured && Capture.capturesVariable() &&
|
2017-02-04 00:53:03 +03:00
|
|
|
Capture.getCaptureKind() == LCK_ByCopy) {
|
|
|
|
const VarDecl *Var = Capture.getCapturedVar();
|
|
|
|
if (Var->hasInit()) {
|
|
|
|
const Stmt *Init = Var->getInit();
|
|
|
|
|
|
|
|
// Ignore single argument constructors, and trivial nodes.
|
|
|
|
while (true) {
|
|
|
|
auto NewInit = IgnoreTrivials(Init);
|
|
|
|
if (auto ConstructExpr = dyn_cast<CXXConstructExpr>(NewInit)) {
|
|
|
|
if (ConstructExpr->getNumArgs() == 1) {
|
|
|
|
NewInit = ConstructExpr->getArg(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Init == NewInit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Init = NewInit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<CXXThisExpr>(Init)) {
|
|
|
|
StrongRefToThisCaptured = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 20:11:50 +03:00
|
|
|
// Now we can go through and produce errors for any captured variables or this
|
|
|
|
// pointers.
|
|
|
|
for (const LambdaCapture &Capture : Lambda->captures()) {
|
2017-02-04 00:53:03 +03:00
|
|
|
if (Capture.capturesVariable()) {
|
2016-12-18 00:35:53 +03:00
|
|
|
QualType Pointee = Capture.getCapturedVar()->getType()->getPointeeType();
|
|
|
|
|
|
|
|
if (!Pointee.isNull() && isClassRefCounted(Pointee)) {
|
2017-10-20 20:11:50 +03:00
|
|
|
emitDiagnostics(Capture.getLocation(),
|
|
|
|
Capture.getCapturedVar()->getName(), Pointee);
|
2017-02-04 00:53:03 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The situation with captures of `this` is more complex. All captures of
|
|
|
|
// `this` look the same-ish (they are LCK_This). We want to complain about
|
|
|
|
// captures of `this` where `this` is a refcounted type, and the capture is
|
|
|
|
// actually used in the body of the lambda (if the capture isn't used, then
|
|
|
|
// we don't care, because it's only being captured in order to give access
|
|
|
|
// to private methods).
|
|
|
|
//
|
|
|
|
// In addition, we don't complain about this, even if it is used, if it was
|
|
|
|
// captured implicitly when the LambdaCaptureDefault was LCD_ByRef, as that
|
|
|
|
// expresses the intent that the lambda won't leave the enclosing scope.
|
|
|
|
bool ImplicitByRefDefaultedCapture =
|
2017-10-20 20:11:50 +03:00
|
|
|
Capture.isImplicit() && Lambda->getLambdaCaptureDefault() == LCD_ByRef;
|
|
|
|
if (Capture.capturesThis() && !ImplicitByRefDefaultedCapture &&
|
2017-02-04 00:53:03 +03:00
|
|
|
!StrongRefToThisCaptured) {
|
|
|
|
ThisVisitor V(*this);
|
2017-10-20 20:11:50 +03:00
|
|
|
bool NotAborted = V.TraverseDecl(
|
|
|
|
const_cast<CXXMethodDecl *>(Lambda->getLambdaCallOperator()));
|
2017-02-04 00:53:03 +03:00
|
|
|
if (!NotAborted) {
|
2016-12-18 00:35:53 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-04 00:53:03 +03:00
|
|
|
|
2017-10-20 20:11:50 +03:00
|
|
|
bool RefCountedInsideLambdaChecker::ThisVisitor::VisitCXXThisExpr(
|
|
|
|
CXXThisExpr *This) {
|
2017-02-04 00:53:03 +03:00
|
|
|
QualType Pointee = This->getType()->getPointeeType();
|
|
|
|
if (!Pointee.isNull() && isClassRefCounted(Pointee)) {
|
2018-11-03 16:41:36 +03:00
|
|
|
Checker.emitDiagnostics(This->getBeginLoc(), "this", Pointee);
|
2017-02-04 00:53:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|