xref: /llvm-project/compiler-rt/test/BlocksRuntime/recursive-block.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 #include <stdio.h>
7 #include <Block.h>
8 #include <Block_private.h>
9 #include <stdlib.h>
10 
11 // CONFIG
12 
13 
14 int cumulation = 0;
15 
doSomething(int i)16 int doSomething(int i) {
17     cumulation += i;
18     return cumulation;
19 }
20 
dirtyStack()21 void dirtyStack() {
22     int i = random();
23     int j = doSomething(i);
24     int k = doSomething(j);
25     doSomething(i + j + k);
26 }
27 
28 typedef void (^voidVoid)(void);
29 
testFunction()30 voidVoid testFunction() {
31     int i = random();
32     __block voidVoid inner = ^{ doSomething(i); };
33     //printf("inner, on stack, is %p\n", (void*)inner);
34     /*__block*/ voidVoid outer = ^{
35         //printf("will call inner block %p\n", (void *)inner);
36         inner();
37     };
38     //printf("outer looks like: %s\n", _Block_dump(outer));
39     voidVoid result = Block_copy(outer);
40     //Block_release(inner);
41     return result;
42 }
43 
44 
main(int argc,char ** argv)45 int main(int argc, char **argv) {
46     voidVoid block = testFunction();
47     dirtyStack();
48     block();
49     Block_release(block);
50 
51     printf("%s: success\n", argv[0]);
52 
53     return 0;
54 }
55