| 1 | // |
| 2 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 3 | // See https://llvm.org/LICENSE.txt for license information. |
| 4 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | |
| 6 | #import <Block.h> |
| 7 | #import <stdio.h> |
| 8 | #import <stdlib.h> |
| 9 | |
| 10 | // CONFIG C++ |
| 11 | |
| 12 | int recovered = 0; |
| 13 | |
| 14 | |
| 15 | |
| 16 | int constructors = 0; |
| 17 | int destructors = 0; |
| 18 | |
| 19 | #define CONST const |
| 20 | |
| 21 | class TestObject |
| 22 | { |
| 23 | public: |
| 24 | TestObject(CONST TestObject& inObj); |
| 25 | TestObject(); |
| 26 | ~TestObject(); |
| 27 | |
| 28 | TestObject& operator=(CONST TestObject& inObj); |
| 29 | |
| 30 | void test(void); |
| 31 | |
| 32 | int version() CONST { return _version; } |
| 33 | private: |
| 34 | mutable int _version; |
| 35 | }; |
| 36 | |
| 37 | TestObject::TestObject(CONST TestObject& inObj) |
| 38 | |
| 39 | { |
| 40 | ++constructors; |
| 41 | _version = inObj._version; |
| 42 | //printf("%p (%d) -- TestObject(const TestObject&) called", this, _version); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | TestObject::TestObject() |
| 47 | { |
| 48 | _version = ++constructors; |
| 49 | //printf("%p (%d) -- TestObject() called\n", this, _version); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | TestObject::~TestObject() |
| 54 | { |
| 55 | //printf("%p -- ~TestObject() called\n", this); |
| 56 | ++destructors; |
| 57 | } |
| 58 | |
| 59 | #if 1 |
| 60 | TestObject& TestObject::operator=(CONST TestObject& inObj) |
| 61 | { |
| 62 | //printf("%p -- operator= called", this); |
| 63 | _version = inObj._version; |
| 64 | return *this; |
| 65 | } |
| 66 | #endif |
| 67 | |
| 68 | void TestObject::test(void) { |
| 69 | void (^b)(void) = ^{ recovered = _version; }; |
| 70 | void (^b2)(void) = Block_copy(b); |
| 71 | b2(); |
| 72 | Block_release(b2); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | |
| 77 | void testRoutine() { |
| 78 | TestObject one; |
| 79 | |
| 80 | |
| 81 | one.test(); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | |
| 86 | int main(int argc, char *argv[]) { |
| 87 | testRoutine(); |
| 88 | if (recovered == 1) { |
| 89 | printf(format: "%s: success\n" , argv[0]); |
| 90 | exit(status: 0); |
| 91 | } |
| 92 | printf(format: "%s: *** didn't recover byref block variable\n" , argv[0]); |
| 93 | exit(status: 1); |
| 94 | } |
| 95 | |