[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 14 15:26:49 PST 2018


jgorbe created this revision.
jgorbe added a reviewer: rsmith.

This causes the compiler to crash when trying to compute a layout for
the lambda closure type (see included test).


Repository:
  rC Clang

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,13 @@
     = 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 (const CXXRecordDecl *RD = FieldType->getAsCXXRecordDecl()) {
+    if (RD->isInvalidDecl()) {
+      Lambda->setInvalidDecl();
+    }
+  }
   Field->setImplicit(true);
   Field->setAccess(AS_private);
   Lambda->addDecl(Field);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54550.174112.patch
Type: text/x-patch
Size: 1186 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181114/8a610c90/attachment.bin>


More information about the cfe-commits mailing list