A LambdaCapture does not have sufficient information
to correctly determine whether it is an init-capture or not.
Doing so requires knowledge held in the LambdaExpr itself.
It the case of a nested capture of an init-capture it is not
sufficient to check (as LambdaCapture::isInitCapture did)
whether the associated VarDecl was from an init-capture.
This patch moves isInitCapture to LambdaExpr and updates
Capture->isInitCapture() to Lambda->isInitCapture(Capture).
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@236760
91177308-0d34-0410-b5e6-
96231b3b80d8
bool
RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr *LE,
const LambdaCapture *C) {
- if (C->isInitCapture())
+ if (LE->isInitCapture(C))
TRY_TO(TraverseDecl(C->getCapturedVar()));
return true;
}
return CaptureDefaultLoc;
}
+ /// \brief Determine whether one of this lambda's captures is an init-capture.
+ bool isInitCapture(const LambdaCapture *Capture) const;
+
/// \brief An iterator that walks over the captures of the lambda,
/// both implicit and explicit.
typedef const Capture *capture_iterator;
(DeclAndBits.getInt() & Capture_ByCopy);
}
- /// \brief Determine whether this is an init-capture.
- bool isInitCapture() const {
- return capturesVariable() && getCapturedVar()->isInitCapture();
- }
-
/// \brief Retrieve the declaration of the local variable being
/// captured.
///
bool
RecursiveASTVisitor<Derived>::TraverseLambdaCapture(LambdaExpr *LE,
const LambdaCapture *C) {
- if (C->isInitCapture())
+ if (LE->isInitCapture(C))
TRY_TO(TraverseDecl(C->getCapturedVar()));
return true;
}
return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
}
+bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
+ return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
+ (getCallOperator() == C->getCapturedVar()->getDeclContext()));
+}
+
LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
return getLambdaClass()->getLambdaData().Captures;
}
break;
case LCK_ByRef:
- if (Node->getCaptureDefault() != LCD_ByRef || C->isInitCapture())
+ if (Node->getCaptureDefault() != LCD_ByRef || Node->isInitCapture(C))
OS << '&';
OS << C->getCapturedVar()->getName();
break;
llvm_unreachable("VLA type in explicit captures.");
}
- if (C->isInitCapture())
+ if (Node->isInitCapture(C))
PrintExpr(C->getCapturedVar()->getInit());
}
OS << ']';
for (LambdaExpr::capture_iterator C = E->capture_begin(),
CEnd = E->capture_end();
C != CEnd; ++C) {
- if (!C->isInitCapture())
+ if (!E->isInitCapture(C))
continue;
EnterExpressionEvaluationContext EEEC(getSema(),
Sema::PotentiallyEvaluated);
continue;
// Rebuild init-captures, including the implied field declaration.
- if (C->isInitCapture()) {
+ if (E->isInitCapture(C)) {
InitCaptureInfoTy InitExprTypePair =
InitCaptureExprsAndTypes[C - E->capture_begin()];
ExprResult Init = InitExprTypePair.first;
int run = test(); //expected-note {{instantiation}}
-}
\ No newline at end of file
+}
+
+namespace classification_of_captures_of_init_captures {
+
+template <typename T>
+void f() {
+ [a = 24] () mutable {
+ [&a] { a = 3; }();
+ }();
+}
+
+template <typename T>
+void h() {
+ [a = 24] (auto param) mutable {
+ [&a] { a = 3; }();
+ }(42);
+}
+
+int run() {
+ f<int>();
+ h<int>();
+}
+
+}