r347402 - Mark lambda decl as invalid if a captured variable has an invalid type.
Jorge Gorbe Moya via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 21 09:49:38 PST 2018
Author: jgorbe
Date: Wed Nov 21 09:49:37 2018
New Revision: 347402
URL: http://llvm.org/viewvc/llvm-project?rev=347402&view=rev
Log:
Mark lambda decl as invalid if a captured variable has an invalid type.
This causes the compiler to crash when trying to compute a layout for
the lambda closure type (see included test).
Added:
cfe/trunk/test/SemaCXX/lambda-invalid-capture.cpp
Modified:
cfe/trunk/lib/Sema/SemaExpr.cpp
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=347402&r1=347401&r2=347402&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Nov 21 09:49:37 2018
@@ -14966,6 +14966,21 @@ static void addAsFieldToClosureType(Sema
= FieldDecl::Create(S.Context, Lambda, Loc, Loc, nullptr, FieldType,
S.Context.getTrivialTypeSourceInfo(FieldType, Loc),
nullptr, false, ICIS_NoInit);
+ // If the variable being captured has an invalid type, mark the lambda class
+ // as invalid as well.
+ if (!FieldType->isDependentType()) {
+ if (S.RequireCompleteType(Loc, FieldType, diag::err_field_incomplete)) {
+ Lambda->setInvalidDecl();
+ Field->setInvalidDecl();
+ } else {
+ NamedDecl *Def;
+ FieldType->isIncompleteType(&Def);
+ if (Def && Def->isInvalidDecl()) {
+ Lambda->setInvalidDecl();
+ Field->setInvalidDecl();
+ }
+ }
+ }
Field->setImplicit(true);
Field->setAccess(AS_private);
Lambda->addDecl(Field);
Added: cfe/trunk/test/SemaCXX/lambda-invalid-capture.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/lambda-invalid-capture.cpp?rev=347402&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/lambda-invalid-capture.cpp (added)
+++ cfe/trunk/test/SemaCXX/lambda-invalid-capture.cpp Wed Nov 21 09:49:37 2018
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// Don't crash.
+
+struct g {
+ j; // expected-error {{C++ requires a type specifier for all declarations}}
+};
+
+void captures_invalid_type() {
+ g child;
+ auto q = [child]{};
+ const int n = sizeof(q);
+}
+
+void captures_invalid_array_type() {
+ g child[100];
+ auto q = [child]{};
+ const int n = sizeof(q);
+}
More information about the cfe-commits
mailing list