[PATCH] Fix for PR15365 (lambda field destructor not generated)

Erik Olofsson erik.olofsson at hansoft.se
Sun Oct 20 15:55:47 PDT 2013


Hi eli.friedman,

Fix makes sure that field type sent to addAsFieldToClosureType is a complete type. This makes the rest of the code correctly generate a destructor for the field.

I didn't have time to research the the proper way to fail if the type cannot be made complete, so that needs to be fixed.

Also I don't know the proper way to check the code generation is correct in the test. Currently the test is done as x64 assembly, it should probably be done at some other level.

http://llvm-reviews.chandlerc.com/D1983

Files:
  lib/Sema/SemaExpr.cpp
  test/CodeGenCXX/PR15365-lambda-destruction.cpp

Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -11532,6 +11532,10 @@
                                   bool RefersToEnclosingLocal) {
   CXXRecordDecl *Lambda = LSI->Lambda;
 
+  if (S.RequireCompleteType(Loc, FieldType, 0)) {
+    // What should be done here in case of failure?
+  }
+  
   // Build the non-static data member.
   FieldDecl *Field
     = FieldDecl::Create(S.Context, Lambda, Loc, Loc, 0, FieldType,
Index: test/CodeGenCXX/PR15365-lambda-destruction.cpp
===================================================================
--- /dev/null
+++ test/CodeGenCXX/PR15365-lambda-destruction.cpp
@@ -0,0 +1,38 @@
+// REQUIRES: x86-64-registered-target
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -std=c++11 -S %s -o %t-64.s
+// RUN: FileCheck -check-prefix LP64 --input-file=%t-64.s %s
+
+template <typename Type>
+struct ScopedCounter {
+  Type *CountPtr;
+  ScopedCounter(Type &Count)
+    : CountPtr(&Count) {
+    ++(*CountPtr);
+  }
+  ~ScopedCounter() {
+    --(*CountPtr);
+  }
+  ScopedCounter(ScopedCounter const &Right)
+    : CountPtr(Right.CountPtr) {
+    ++*CountPtr;
+  }
+};
+
+void Capture(ScopedCounter<int> const &Counter) {
+  auto fTest
+    = [Counter]() {
+    }
+  ;
+}
+
+int main() {
+  int Count = 0;
+  {
+    ScopedCounter<int> Counter(Count);
+    Capture(Counter);
+  }
+  
+  return Count != 0;
+}
+
+// CHECK-LP64: callq	__ZZ7CaptureRK13ScopedCounterIiEEN3$_0D1Ev
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1983.1.patch
Type: text/x-patch
Size: 1515 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131020/1d4f6a69/attachment.bin>


More information about the cfe-commits mailing list