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