xref: /llvm-project/compiler-rt/test/BlocksRuntime/recursive-test.c (revision 2946cd701067404b99c39fb29dc9c74bd7193eb3)
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 // CONFIG open rdar://6416474
7 // was  rdar://5847976
8 // was  rdar://6348320
9 
10 #include <stdio.h>
11 #include <Block.h>
12 
13 int verbose = 0;
14 
main(int argc,char * argv[])15 int main(int argc, char* argv[]) {
16 
17         if (argc > 1) verbose = 1;
18 
19 	__block void (^recursive_local_block)(int);
20 
21 	if (verbose) printf("recursive_local_block is a local recursive block\n");
22 	recursive_local_block = ^(int i) {
23 		if (verbose) printf("%d\n", i);
24 		if (i > 0) {
25 			recursive_local_block(i - 1);
26 		}
27     };
28 
29 	if (verbose) printf("recursive_local_block's address is %p, running it:\n", (void*)recursive_local_block);
30 	recursive_local_block(5);
31 
32 	if (verbose) printf("Creating other_local_block: a local block that calls recursive_local_block\n");
33 
34 	void (^other_local_block)(int) = ^(int i) {
35 		if (verbose) printf("other_local_block running\n");
36 		recursive_local_block(i);
37     };
38 
39 	if (verbose) printf("other_local_block's address is %p, running it:\n", (void*)other_local_block);
40 
41 	other_local_block(5);
42 
43 #if __APPLE_CC__ >= 5627
44 	if (verbose) printf("Creating other_copied_block: a Block_copy of a block that will call recursive_local_block\n");
45 
46 	void (^other_copied_block)(int) = Block_copy(^(int i) {
47 		if (verbose) printf("other_copied_block running\n");
48 		recursive_local_block(i);
49     });
50 
51 	if (verbose) printf("other_copied_block's address is %p, running it:\n", (void*)other_copied_block);
52 
53 	other_copied_block(5);
54 #endif
55 
56 	__block void (^recursive_copy_block)(int);
57 
58 	if (verbose) printf("Creating recursive_copy_block: a Block_copy of a block that will call recursive_copy_block recursively\n");
59 
60 	recursive_copy_block = Block_copy(^(int i) {
61 		if (verbose) printf("%d\n", i);
62 		if (i > 0) {
63 			recursive_copy_block(i - 1);
64 		}
65     });
66 
67 	if (verbose) printf("recursive_copy_block's address is %p, running it:\n", (void*)recursive_copy_block);
68 
69 	recursive_copy_block(5);
70 
71         printf("%s: Success\n", argv[0]);
72 	return 0;
73 }
74