xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/microsoft-abi-array-cookies.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -emit-llvm %s -o - -cxx-abi microsoft -triple=i386-pc-win32 | FileCheck %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc struct ClassWithoutDtor {
4*f4a2713aSLionel Sambuc   char x;
5*f4a2713aSLionel Sambuc };
6*f4a2713aSLionel Sambuc 
7*f4a2713aSLionel Sambuc void check_array_no_cookies() {
8*f4a2713aSLionel Sambuc // CHECK: define void @"\01?check_array_no_cookies@@YAXXZ"() [[NUW:#[0-9]+]]
9*f4a2713aSLionel Sambuc 
10*f4a2713aSLionel Sambuc // CHECK: call noalias i8* @"\01??_U@YAPAXI@Z"(i32 42)
11*f4a2713aSLionel Sambuc   ClassWithoutDtor *array = new ClassWithoutDtor[42];
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc // CHECK: call void @"\01??_V@YAXPAX@Z"(
14*f4a2713aSLionel Sambuc   delete [] array;
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc }
17*f4a2713aSLionel Sambuc 
18*f4a2713aSLionel Sambuc struct ClassWithDtor {
19*f4a2713aSLionel Sambuc   char x;
20*f4a2713aSLionel Sambuc   ~ClassWithDtor() {}
21*f4a2713aSLionel Sambuc };
22*f4a2713aSLionel Sambuc 
23*f4a2713aSLionel Sambuc void check_array_cookies_simple() {
24*f4a2713aSLionel Sambuc // CHECK: define {{.*}} @"\01?check_array_cookies_simple@@YAXXZ"()
25*f4a2713aSLionel Sambuc 
26*f4a2713aSLionel Sambuc   ClassWithDtor *array = new ClassWithDtor[42];
27*f4a2713aSLionel Sambuc // CHECK: [[ALLOCATED:%.*]] = call noalias i8* @"\01??_U@YAPAXI@Z"(i32 46)
28*f4a2713aSLionel Sambuc // 46 = 42 + size of cookie (4)
29*f4a2713aSLionel Sambuc // CHECK: [[COOKIE:%.*]] = bitcast i8* [[ALLOCATED]] to i32*
30*f4a2713aSLionel Sambuc // CHECK: store i32 42, i32* [[COOKIE]]
31*f4a2713aSLionel Sambuc // CHECK: [[ARRAY:%.*]] = getelementptr inbounds i8* [[ALLOCATED]], i64 4
32*f4a2713aSLionel Sambuc // CHECK: bitcast i8* [[ARRAY]] to [[CLASS:%.*]]*
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc   delete [] array;
35*f4a2713aSLionel Sambuc // CHECK: [[ARRAY_AS_CHAR:%.*]] = bitcast [[CLASS]]* {{%.*}} to i8*
36*f4a2713aSLionel Sambuc // CHECK: getelementptr inbounds i8* [[ARRAY_AS_CHAR]], i64 -4
37*f4a2713aSLionel Sambuc }
38*f4a2713aSLionel Sambuc 
39*f4a2713aSLionel Sambuc struct __attribute__((aligned(8))) ClassWithAlignment {
40*f4a2713aSLionel Sambuc   // FIXME: replace __attribute__((aligned(8))) with __declspec(align(8)) once
41*f4a2713aSLionel Sambuc   // http://llvm.org/bugs/show_bug.cgi?id=12631 is fixed.
42*f4a2713aSLionel Sambuc   int *x, *y;
43*f4a2713aSLionel Sambuc   ~ClassWithAlignment() {}
44*f4a2713aSLionel Sambuc };
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc void check_array_cookies_aligned() {
47*f4a2713aSLionel Sambuc // CHECK: define {{.*}} @"\01?check_array_cookies_aligned@@YAXXZ"()
48*f4a2713aSLionel Sambuc   ClassWithAlignment *array = new ClassWithAlignment[42];
49*f4a2713aSLionel Sambuc // CHECK: [[ALLOCATED:%.*]] = call noalias i8* @"\01??_U@YAPAXI@Z"(i32 344)
50*f4a2713aSLionel Sambuc //   344 = 42*8 + size of cookie (8, due to alignment)
51*f4a2713aSLionel Sambuc // CHECK: [[COOKIE:%.*]] = bitcast i8* [[ALLOCATED]] to i32*
52*f4a2713aSLionel Sambuc // CHECK: store i32 42, i32* [[COOKIE]]
53*f4a2713aSLionel Sambuc // CHECK: [[ARRAY:%.*]] = getelementptr inbounds i8* [[ALLOCATED]], i64 8
54*f4a2713aSLionel Sambuc // CHECK: bitcast i8* [[ARRAY]] to [[CLASS:%.*]]*
55*f4a2713aSLionel Sambuc 
56*f4a2713aSLionel Sambuc   delete [] array;
57*f4a2713aSLionel Sambuc // CHECK: [[ARRAY_AS_CHAR:%.*]] = bitcast [[CLASS]]*
58*f4a2713aSLionel Sambuc // CHECK: getelementptr inbounds i8* [[ARRAY_AS_CHAR]], i64 -8
59*f4a2713aSLionel Sambuc }
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc // CHECK: attributes [[NUW]] = { nounwind{{.*}} }
62