1// RUN: %clang_cc1 -emit-llvm -fblocks -debug-info-kind=limited  -triple x86_64-apple-darwin10 -fobjc-dispatch-method=mixed -x objective-c < %s -o - | FileCheck %s
2
3// rdar://problem/9279956
4// Test that we generate the proper debug location for a captured self.
5// The second half of this test is in llvm/tests/DebugInfo/debug-info-blocks.ll
6
7// CHECK: define {{.*}}_block_invoke
8// CHECK: %[[BLOCK:.*]] = bitcast i8* %.block_descriptor to <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, %0* }>*, !dbg
9// CHECK-NEXT: store <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, %0* }>* %[[BLOCK]], <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, %0* }>** %[[ALLOCA:.*]], align
10// CHECK-NEXT: call void @llvm.dbg.declare(metadata <{ i8*, i32, i32, i8*, %struct.__block_descriptor*, %0* }>** %[[ALLOCA]], metadata ![[SELF:[0-9]+]], metadata !{{.*}})
11// CHECK-NEXT: call void @llvm.dbg.declare(metadata %1** %d, metadata ![[D:[0-9]+]], metadata !{{.*}})
12
13// Test that we do emit scope info for the helper functions, and that the
14// parameters to these functions are marked as artificial (so the debugger
15// doesn't accidentally step into the function).
16// CHECK: define {{.*}} @__copy_helper_block_{{.*}}(i8*, i8*)
17// CHECK-NOT: ret
18// CHECK: call {{.*}}, !dbg ![[DBG_LINE:[0-9]+]]
19// CHECK-NOT: ret
20// CHECK: load {{.*}}, !dbg ![[COPY_LINE:[0-9]+]]
21// CHECK: ret void, !dbg ![[COPY_LINE]]
22// CHECK: define {{.*}} @__destroy_helper_block_{{.*}}(i8*)
23// CHECK-NOT: ret
24// CHECK: load {{.*}}, !dbg ![[DESTROY_LINE:[0-9]+]]
25// CHECK: ret void, !dbg ![[DESTROY_LINE]]
26
27typedef unsigned int NSUInteger;
28
29@protocol NSObject
30@end
31
32@interface NSObject <NSObject>
33- (id)init;
34+ (id)alloc;
35@end
36
37@interface NSDictionary : NSObject
38- (NSUInteger)count;
39@end
40
41@interface NSMutableDictionary : NSDictionary
42@end
43
44@interface A : NSObject {
45@public
46    int ivar;
47}
48@end
49
50static void run(void (^block)(void))
51{
52    block();
53}
54
55@implementation A
56
57- (id)init
58{
59    if ((self = [super init])) {
60      // CHECK-DAG: [[DBG_LINE]] = !DILocation(line: 0, scope: ![[COPY_SP:[0-9]+]])
61      // CHECK-DAG: [[COPY_LINE]] = !DILocation(line: [[@LINE+7]], scope: ![[COPY_SP:[0-9]+]])
62      // CHECK-DAG: [[COPY_SP]] = distinct !DISubprogram(name: "__copy_helper_block_8_32o"
63      // CHECK-DAG: [[DESTROY_LINE]] = !DILocation(line: [[@LINE+5]], scope: ![[DESTROY_SP:[0-9]+]])
64      // CHECK-DAG: [[DESTROY_SP]] = distinct !DISubprogram(name: "__destroy_helper_block_8_32o"
65      // CHECK-DAG: !DILocalVariable(arg: 1, scope: ![[COPY_SP]], {{.*}}, flags: DIFlagArtificial)
66      // CHECK-DAG: !DILocalVariable(arg: 2, scope: ![[COPY_SP]], {{.*}}, flags: DIFlagArtificial)
67      // CHECK-DAG: !DILocalVariable(arg: 1, scope: ![[DESTROY_SP]], {{.*}}, flags: DIFlagArtificial)
68      run(^{
69          // CHECK-DAG: ![[SELF]] = !DILocalVariable(name: "self", scope:{{.*}}, line: [[@LINE+4]],
70          // CHECK-DAG: ![[D]] = !DILocalVariable(name: "d", scope:{{.*}}, line: [[@LINE+1]],
71          NSMutableDictionary *d = [[NSMutableDictionary alloc] init];
72          ivar = 42 + (int)[d count];
73        });
74    }
75    return self;
76}
77
78@end
79
80int main()
81{
82	A *a = [[A alloc] init];
83	return 0;
84}
85