/// the lambda.
bool NonODRUsed = false;
+ /// Whether the capture is invalid (a capture was required but the entity is
+ /// non-capturable).
+ bool Invalid = false;
+
public:
Capture(VarDecl *Var, bool Block, bool ByRef, bool IsNested,
- SourceLocation Loc, SourceLocation EllipsisLoc,
- QualType CaptureType, Expr *Cpy)
+ SourceLocation Loc, SourceLocation EllipsisLoc, QualType CaptureType,
+ Expr *Cpy, bool Invalid)
: VarAndNestedAndThis(Var, IsNested ? IsNestedCapture : 0),
InitExprAndCaptureKind(
- Cpy, !Var ? Cap_VLA : Block ? Cap_Block : ByRef ? Cap_ByRef
- : Cap_ByCopy),
- Loc(Loc), EllipsisLoc(EllipsisLoc), CaptureType(CaptureType) {}
+ Cpy, !Var ? Cap_VLA
+ : Block ? Cap_Block : ByRef ? Cap_ByRef : Cap_ByCopy),
+ Loc(Loc), EllipsisLoc(EllipsisLoc), CaptureType(CaptureType),
+ Invalid(Invalid) {}
enum IsThisCapture { ThisCapture };
Capture(IsThisCapture, bool IsNested, SourceLocation Loc,
- QualType CaptureType, Expr *Cpy, const bool ByCopy)
+ QualType CaptureType, Expr *Cpy, const bool ByCopy, bool Invalid)
: VarAndNestedAndThis(
nullptr, (IsThisCaptured | (IsNested ? IsNestedCapture : 0))),
- InitExprAndCaptureKind(Cpy, ByCopy ? Cap_ByCopy : Cap_ByRef),
- Loc(Loc), CaptureType(CaptureType) {}
+ InitExprAndCaptureKind(Cpy, ByCopy ? Cap_ByCopy : Cap_ByRef), Loc(Loc),
+ CaptureType(CaptureType), Invalid(Invalid) {}
bool isThisCapture() const {
return VarAndNestedAndThis.getInt() & IsThisCaptured;
return VarAndNestedAndThis.getInt() & IsNestedCapture;
}
+ bool isInvalid() const { return Invalid; }
+
bool isODRUsed() const { return ODRUsed; }
bool isNonODRUsed() const { return NonODRUsed; }
void markUsed(bool IsODRUse) { (IsODRUse ? ODRUsed : NonODRUsed) = true; }
void addCapture(VarDecl *Var, bool isBlock, bool isByref, bool isNested,
SourceLocation Loc, SourceLocation EllipsisLoc,
- QualType CaptureType, Expr *Cpy) {
+ QualType CaptureType, Expr *Cpy, bool Invalid) {
Captures.push_back(Capture(Var, isBlock, isByref, isNested, Loc,
- EllipsisLoc, CaptureType, Cpy));
+ EllipsisLoc, CaptureType, Cpy, Invalid));
CaptureMap[Var] = Captures.size();
}
Captures.push_back(Capture(/*Var*/ nullptr, /*isBlock*/ false,
/*isByref*/ false, /*isNested*/ false, Loc,
/*EllipsisLoc*/ SourceLocation(), CaptureType,
- /*Cpy*/ nullptr));
+ /*Cpy*/ nullptr, /*Invalid*/ false));
}
// Note, we do not need to add the type of 'this' since that is always
Expr *Cpy,
const bool ByCopy) {
Captures.push_back(Capture(Capture::ThisCapture, isNested, Loc, QualType(),
- Cpy, ByCopy));
+ Cpy, ByCopy, /*Invalid*/ false));
CXXThisCaptureIndex = Captures.size();
}
QualType BlockTy;
// Set the captured variables on the block.
- // FIXME: Share capture structure between BlockDecl and CapturingScopeInfo!
SmallVector<BlockDecl::Capture, 4> Captures;
for (Capture &Cap : BSI->Captures) {
- if (Cap.isThisCapture())
+ if (Cap.isInvalid() || Cap.isThisCapture())
continue;
BlockDecl::Capture NewCap(Cap.getVariable(), Cap.isBlockCapture(),
Cap.isNested(), Cap.getInitExpr());
QualType &CaptureType,
QualType &DeclRefType,
const bool Nested,
- Sema &S) {
+ Sema &S, bool Invalid) {
Expr *CopyExpr = nullptr;
bool ByRef = false;
// Blocks are not allowed to capture arrays, excepting OpenCL.
// OpenCL v2.0 s1.12.5 (revision 40): arrays are captured by reference
// (decayed to pointers).
- if (!S.getLangOpts().OpenCL && CaptureType->isArrayType()) {
+ if (!Invalid && !S.getLangOpts().OpenCL && CaptureType->isArrayType()) {
if (BuildAndDiagnose) {
S.Diag(Loc, diag::err_ref_array_type);
S.Diag(Var->getLocation(), diag::note_previous_decl)
<< Var->getDeclName();
+ Invalid = true;
+ } else {
+ return false;
}
- return false;
}
// Forbid the block-capture of autoreleasing variables.
- if (CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) {
+ if (!Invalid &&
+ CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) {
if (BuildAndDiagnose) {
S.Diag(Loc, diag::err_arc_autoreleasing_capture)
<< /*block*/ 0;
S.Diag(Var->getLocation(), diag::note_previous_decl)
<< Var->getDeclName();
+ Invalid = true;
+ } else {
+ return false;
}
- return false;
}
// Warn about implicitly autoreleasing indirect parameters captured by blocks.
QualType PointeeTy = PT->getPointeeType();
- if (PointeeTy->getAs<ObjCObjectPointerType>() &&
+ if (!Invalid && PointeeTy->getAs<ObjCObjectPointerType>() &&
PointeeTy.getObjCLifetime() == Qualifiers::OCL_Autoreleasing &&
!IsObjCOwnershipAttributedType(PointeeTy)) {
if (BuildAndDiagnose) {
// Actually capture the variable.
if (BuildAndDiagnose)
- BSI->addCapture(Var, HasBlocksAttr, ByRef, Nested, Loc,
- SourceLocation(), CaptureType, CopyExpr);
-
- return true;
+ BSI->addCapture(Var, HasBlocksAttr, ByRef, Nested, Loc, SourceLocation(),
+ CaptureType, CopyExpr, Invalid);
+ return !Invalid;
}
QualType &CaptureType,
QualType &DeclRefType,
const bool RefersToCapturedVariable,
- Sema &S) {
+ Sema &S, bool Invalid) {
// By default, capture variables by reference.
bool ByRef = true;
// Using an LValue reference type is consistent with Lambdas (see below).
// Actually capture the variable.
if (BuildAndDiagnose)
- RSI->addCapture(Var, /*isBlock*/false, ByRef, RefersToCapturedVariable, Loc,
- SourceLocation(), CaptureType, CopyExpr);
+ RSI->addCapture(Var, /*isBlock*/ false, ByRef, RefersToCapturedVariable,
+ Loc, SourceLocation(), CaptureType, CopyExpr,
+ Invalid);
-
- return true;
+ return !Invalid;
}
/// Create a field within the lambda class for the variable
const Sema::TryCaptureKind Kind,
SourceLocation EllipsisLoc,
const bool IsTopScope,
- Sema &S) {
-
+ Sema &S, bool Invalid) {
// Determine whether we are capturing by reference or by value.
bool ByRef = false;
if (IsTopScope && Kind != Sema::TryCapture_Implicit) {
}
// Forbid the lambda copy-capture of autoreleasing variables.
- if (CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) {
+ if (!Invalid &&
+ CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) {
if (BuildAndDiagnose) {
S.Diag(Loc, diag::err_arc_autoreleasing_capture) << /*lambda*/ 1;
S.Diag(Var->getLocation(), diag::note_previous_decl)
<< Var->getDeclName();
+ Invalid = true;
+ } else {
+ return false;
}
- return false;
}
// Make sure that by-copy captures are of a complete and non-abstract type.
- if (BuildAndDiagnose) {
+ if (!Invalid && BuildAndDiagnose) {
if (!CaptureType->isDependentType() &&
S.RequireCompleteType(Loc, CaptureType,
diag::err_capture_of_incomplete_type,
Var->getDeclName()))
- return false;
-
- if (S.RequireNonAbstractType(Loc, CaptureType,
- diag::err_capture_of_abstract_type))
- return false;
+ Invalid = true;
+ else if (S.RequireNonAbstractType(Loc, CaptureType,
+ diag::err_capture_of_abstract_type))
+ Invalid = true;
}
}
// Capture this variable in the lambda.
- if (BuildAndDiagnose)
+ if (BuildAndDiagnose && !Invalid)
addAsFieldToClosureType(S, LSI, CaptureType, DeclRefType, Loc,
RefersToCapturedVariable);
// Add the capture.
if (BuildAndDiagnose)
LSI->addCapture(Var, /*IsBlock=*/false, ByRef, RefersToCapturedVariable,
- Loc, EllipsisLoc, CaptureType, /*CopyExpr=*/nullptr);
+ Loc, EllipsisLoc, CaptureType, /*CopyExpr=*/nullptr,
+ Invalid);
- return true;
+ return !Invalid;
}
bool Sema::tryCaptureVariable(
}
return true;
}
- // Certain capturing entities (lambdas, blocks etc.) are not allowed to capture
- // certain types of variables (unnamed, variably modified types etc.)
- // so check for eligibility.
- if (!isVariableCapturable(CSI, Var, ExprLoc, BuildAndDiagnose, *this))
- return true;
// Try to capture variable-length arrays types.
if (Var->getType()->isVariablyModifiedType()) {
// requirements, and adding captures if requested.
// If the variable had already been captured previously, we start capturing
// at the lambda nested within that one.
+ bool Invalid = false;
for (unsigned I = ++FunctionScopesIndex, N = MaxFunctionScopesIndex + 1; I != N;
++I) {
CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[I]);
+ // Certain capturing entities (lambdas, blocks etc.) are not allowed to capture
+ // certain types of variables (unnamed, variably modified types etc.)
+ // so check for eligibility.
+ if (!Invalid)
+ Invalid =
+ !isVariableCapturable(CSI, Var, ExprLoc, BuildAndDiagnose, *this);
+
+ // After encountering an error, if we're actually supposed to capture, keep
+ // capturing in nested contexts to suppress any follow-on diagnostics.
+ if (Invalid && !BuildAndDiagnose)
+ return true;
+
if (BlockScopeInfo *BSI = dyn_cast<BlockScopeInfo>(CSI)) {
- if (!captureInBlock(BSI, Var, ExprLoc,
- BuildAndDiagnose, CaptureType,
- DeclRefType, Nested, *this))
- return true;
+ Invalid = !captureInBlock(BSI, Var, ExprLoc, BuildAndDiagnose, CaptureType,
+ DeclRefType, Nested, *this, Invalid);
Nested = true;
} else if (CapturedRegionScopeInfo *RSI = dyn_cast<CapturedRegionScopeInfo>(CSI)) {
- if (!captureInCapturedRegion(RSI, Var, ExprLoc,
- BuildAndDiagnose, CaptureType,
- DeclRefType, Nested, *this))
- return true;
+ Invalid = !captureInCapturedRegion(RSI, Var, ExprLoc, BuildAndDiagnose,
+ CaptureType, DeclRefType, Nested,
+ *this, Invalid);
Nested = true;
} else {
LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(CSI);
- if (!captureInLambda(LSI, Var, ExprLoc,
- BuildAndDiagnose, CaptureType,
+ Invalid =
+ !captureInLambda(LSI, Var, ExprLoc, BuildAndDiagnose, CaptureType,
DeclRefType, Nested, Kind, EllipsisLoc,
- /*IsTopScope*/I == N - 1, *this))
- return true;
+ /*IsTopScope*/ I == N - 1, *this, Invalid);
Nested = true;
}
+
+ if (Invalid && !BuildAndDiagnose)
+ return true;
}
- return false;
+ return Invalid;
}
bool Sema::tryCaptureVariable(VarDecl *Var, SourceLocation Loc,