xref: /llvm-project/clang/test/CodeGenCXX/compound-literals.cpp (revision 4497ec293a6e745be817dc88027169bd5e4f7246)
1 // RUN: %clang_cc1 -std=c++11 -triple armv7-none-eabi -emit-llvm -o - %s | FileCheck %s
2 
3 struct X {
4   X();
5   X(const X&);
6   X(const char*);
7   ~X();
8 };
9 
10 struct Y {
11   int i;
12   X x;
13 };
14 
15 // CHECK: @.compoundliteral = internal global [5 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5], align 4
16 // CHECK: @q ={{.*}} global ptr @.compoundliteral, align 4
17 
18 // CHECK-LABEL: define{{.*}} i32 @_Z1fv()
19 int f() {
20   // CHECK: [[LVALUE:%[a-z0-9.]+]] = alloca
21   // CHECK-NEXT: [[I:%[a-z0-9]+]] = getelementptr inbounds {{.*}}, ptr [[LVALUE]], i32 0, i32 0
22   // CHECK-NEXT: store i32 17, ptr [[I]]
23   // CHECK-NEXT: [[X:%[a-z0-9]+]] = getelementptr inbounds {{.*}} [[LVALUE]], i32 4
24   // CHECK-NEXT: call noundef ptr @_ZN1XC1EPKc({{.*}}[[X]]
25   // CHECK-NEXT: [[I:%[a-z0-9]+]] = getelementptr inbounds {{.*}} [[LVALUE]], i32 0, i32 0
26   // CHECK-NEXT: [[RESULT:%[a-z0-9]+]] = load i32, ptr
27   // CHECK-NEXT: call noundef ptr @_ZN1YD1Ev
28   // CHECK-NEXT: ret i32 [[RESULT]]
29   return ((Y){17, "seventeen"}).i;
30 }
31 
32 // CHECK-LABEL: define{{.*}} i32 @_Z1gv()
33 int g() {
34   // CHECK: store ptr %{{[a-z0-9.]+}}, ptr [[V:%[a-z0-9.]+]]
35   const int (&v)[2] = (int [2]) {1,2};
36 
37   // CHECK: [[A:%[a-z0-9.]+]] = load ptr, ptr [[V]]
38   // CHECK-NEXT: [[A0ADDR:%[a-z0-9.]+]] = getelementptr inbounds [2 x i32], ptr [[A]], i32 0, {{.*}} 0
39   // CHECK-NEXT: [[A0:%[a-z0-9.]+]] = load i32, ptr [[A0ADDR]]
40   // CHECK-NEXT: ret i32 [[A0]]
41   return v[0];
42 }
43 
44 // GCC's compound-literals-in-C++ extension lifetime-extends a compound literal
45 // (or a C++11 list-initialized temporary!) if:
46 //  - it is at global scope
47 //  - it has array type
48 //  - it has a constant initializer
49 
50 struct Z { int i[3]; };
51 int *p = (Z){ {1, 2, 3} }.i;
52 // CHECK: define {{.*}}__cxx_global_var_init()
53 // CHECK: alloca %struct.Z
54 // CHECK: store ptr %{{.*}}, ptr @p
55 
56 int *q = (int [5]){1, 2, 3, 4, 5};
57 // (constant initialization, checked above)
58 
59 extern int n;
60 int *r = (int [5]){1, 2, 3, 4, 5} + n;
61 // CHECK-LABEL: define {{.*}}__cxx_global_var_init.1()
62 // CHECK: %[[PTR:.*]] = getelementptr inbounds i32, ptr @.compoundliteral.2, i32 %
63 // CHECK: store ptr %[[PTR]], ptr @r
64 
65 int *PR21912_1 = (int []){} + n;
66 // CHECK-LABEL: define {{.*}}__cxx_global_var_init.3()
67 // CHECK: %[[PTR:.*]] = getelementptr inbounds i32, ptr @.compoundliteral.4, i32 %
68 // CHECK: store ptr %[[PTR]], ptr @PR21912_1
69 
70 union PR21912Ty {
71   long long l;
72   double d;
73 };
74 union PR21912Ty *PR21912_2 = (union PR21912Ty[]){{.d = 2.0}, {.l = 3}} + n;
75 // CHECK-LABEL: define {{.*}}__cxx_global_var_init.5()
76 // CHECK: %[[PTR:.*]] = getelementptr inbounds %union.PR21912Ty, ptr @.compoundliteral.6, i32 %
77 // CHECK: store ptr %[[PTR]], ptr @PR21912_2, align 4
78 
79 // This compound literal should have local scope.
80 int computed_with_lambda = [] {
81   int *array = (int[]) { 1, 3, 5, 7 };
82   return array[0];
83 }();
84 // CHECK-LABEL: define internal noundef i32 @{{.*}}clEv
85 // CHECK:         alloca [4 x i32]
86