[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / CodeGen / blocksignature.c
blobfb7fd0eeee093c5883d7056a651780dd9afa1219
1 // RUN: %clang_cc1 -fblocks -triple x86_64-apple-darwin9 %s -emit-llvm -o - | FileCheck %s -check-prefix=X64
2 // RUN: %clang_cc1 -fblocks -triple i686-apple-darwin9 %s -emit-llvm -o - | FileCheck %s -check-prefix=X32
4 // X64: @.str = private unnamed_addr constant [6 x i8] c"v8@?0\00"
5 // X64: @__block_literal_global = internal constant {{.*}} { ptr @_NSConcreteGlobalBlock, i32 1342177280,
6 // X64: @.str.1 = private unnamed_addr constant [12 x i8] c"i16@?0c8f12\00"
7 // X64: store i32 1073741824, ptr
9 // X32: [[STR1:@.*]] = private unnamed_addr constant [6 x i8] c"v4@?0\00"
10 // X32: @__block_descriptor_tmp = internal constant [[FULL_DESCRIPTOR_T:.*]] { i32 0, i32 20, ptr [[STR1]], ptr null }
11 // X32: @__block_literal_global = internal constant [[GLOBAL_LITERAL_T:.*]] { ptr @_NSConcreteGlobalBlock, i32 1342177280, i32 0, ptr @global_block_invoke{{.*}}, ptr @__block_descriptor_tmp }
12 // X32: [[STR2:@.*]] = private unnamed_addr constant [11 x i8] c"i12@?0c4f8\00"
13 // X32: @__block_descriptor_tmp{{.*}} = internal constant [[FULL_DESCRIPTOR_T]] { i32 0, i32 24, ptr [[STR2]], ptr null }
14 // X32: store i32 1073741824, ptr
16 int globalInt;
17 void (^global)(void) = ^{ ++globalInt; };
20 void foo(int param) {
21 extern int rand(void);
22 extern void rand_r(int (^b)(char x, float y)); // name a function present at runtime
23 while (param--)
24 rand_r(^(char x, float y){ return x + (int)y + param + rand(); }); // generate a local block binding param
27 #if 0
28 #include <stdio.h>
29 enum {
30 BLOCK_HAS_COPY_DISPOSE = (1 << 25),
31 BLOCK_HAS_CXX_OBJ = (1 << 26),
32 BLOCK_IS_GLOBAL = (1 << 28),
33 BLOCK_HAS_DESCRIPTOR = (1 << 29),
34 BLOCK_HAS_OBJC_TYPE = (1 << 30)
37 struct block_descriptor_big {
38 unsigned long int reserved;
39 unsigned long int size;
40 void (*copy)(void *dst, void *src); // conditional on BLOCK_HAS_COPY_DISPOSE
41 void (*dispose)(void *); // conditional on BLOCK_HAS_COPY_DISPOSE
42 const char *signature; // conditional on BLOCK_HAS_OBJC
43 const char *layout; // conditional on BLOCK_HAS_OBJC
45 struct block_descriptor_small {
46 unsigned long int reserved;
47 unsigned long int size;
48 const char *signature; // conditional on BLOCK_HAS_OBJC
49 const char *layout; // conditional on BLOCK_HAS_OBJC
52 struct block_layout_abi { // can't change
53 void *isa;
54 int flags;
55 int reserved;
56 void (*invoke)(void *, ...);
57 struct block_descriptor_big *descriptor;
60 const char *getBlockSignature(void *block) {
61 struct block_layout_abi *layout = (struct block_layout_abi *)block;
62 if ((layout->flags & BLOCK_HAS_OBJC_TYPE) != BLOCK_HAS_OBJC_TYPE) return NULL;
63 if (layout->flags & BLOCK_HAS_COPY_DISPOSE)
64 return layout->descriptor->signature;
65 else
66 return ((struct block_descriptor_small *)layout->descriptor)->signature;
71 int main(int argc, char *argv[]) {
72 printf("desired global flags: %d\n", BLOCK_IS_GLOBAL | BLOCK_HAS_OBJC_TYPE);
73 printf("desired stack flags: %d\n", BLOCK_HAS_OBJC_TYPE);
75 printf("types for global: %s\n", getBlockSignature(global));
76 printf("types for local: %s\n", getBlockSignature(^int(char x, float y) { return (int)(y + x); }));
77 return 0;
81 x86_64
82 desired global flags: 1342177280
83 desired stack flags: 1073741824
84 types for global: v8@?0
85 types for local: i16@?0c8f12
87 i386
88 desired global flags: 1342177280
89 desired stack flags: 1073741824
90 types for global: v4@?0
91 types for local: i12@?0c4f8
93 #endif