jgorbe updated this revision to Diff 174118.
jgorbe added a comment.

Fixed some issues pointed out in review comments:

- call to getBaseElementType before checking type validity.
- when the type is incomplete, mark not only the lambda closure type as invalid 
but also the field

Also, added a couple of checks to resemble more closely the code that checks 
user-declared fields in classes.


https://reviews.llvm.org/D54550

Files:
  clang/lib/Sema/SemaExpr.cpp
  clang/test/SemaCXX/lambda-invalid-capture.cpp


Index: clang/test/SemaCXX/lambda-invalid-capture.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/lambda-invalid-capture.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// Don't crash.
+
+struct g {
+  j; // expected-error {{C++ requires a type specifier for all declarations}}
+};
+
+void h() {
+  g child;
+  auto q = [child]{};
+  const int n = sizeof(q);
+}
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -14941,6 +14941,22 @@
     = 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.
+  QualType EltTy = S.Context.getBaseElementType(FieldType);
+  if (!EltTy->isDependentType()) {
+    if (S.RequireCompleteType(Loc, EltTy, diag::err_field_incomplete)) {
+      Lambda->setInvalidDecl();
+      Field->setInvalidDecl();
+    } else {
+      NamedDecl *Def;
+      EltTy->isIncompleteType(&Def);
+      if (Def && Def->isInvalidDecl()) {
+        Lambda->setInvalidDecl();
+        Field->setInvalidDecl();
+      }
+    }
+  }
   Field->setImplicit(true);
   Field->setAccess(AS_private);
   Lambda->addDecl(Field);


Index: clang/test/SemaCXX/lambda-invalid-capture.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/lambda-invalid-capture.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// Don't crash.
+
+struct g {
+  j; // expected-error {{C++ requires a type specifier for all declarations}}
+};
+
+void h() {
+  g child;
+  auto q = [child]{};
+  const int n = sizeof(q);
+}
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -14941,6 +14941,22 @@
     = 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.
+  QualType EltTy = S.Context.getBaseElementType(FieldType);
+  if (!EltTy->isDependentType()) {
+    if (S.RequireCompleteType(Loc, EltTy, diag::err_field_incomplete)) {
+      Lambda->setInvalidDecl();
+      Field->setInvalidDecl();
+    } else {
+      NamedDecl *Def;
+      EltTy->isIncompleteType(&Def);
+      if (Def && Def->isInvalidDecl()) {
+        Lambda->setInvalidDecl();
+        Field->setInvalidDecl();
+      }
+    }
+  }
   Field->setImplicit(true);
   Field->setAccess(AS_private);
   Lambda->addDecl(Field);
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to