[PATCH] D54550: Mark lambda decl as invalid if a captured variable has an invalid type.

Jorge Gorbe Moya via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 21 09:34:28 PST 2018


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

Folded the two test cases (capturing an invalid type and capturing an invalid array type) into a single file.


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,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);
+}
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -14967,6 +14967,21 @@
     = 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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54550.174944.patch
Type: text/x-patch
Size: 1559 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181121/3ce10519/attachment.bin>


More information about the cfe-commits mailing list