2014-12-14 20:16:43 +08:00
|
|
|
// RUN: %clang_cc1 -std=c++11 -triple armv7-none-eabi -emit-llvm -o - %s | FileCheck %s
|
2011-06-17 12:59:12 +08:00
|
|
|
|
|
|
|
struct X {
|
|
|
|
X();
|
|
|
|
X(const X&);
|
|
|
|
X(const char*);
|
|
|
|
~X();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Y {
|
|
|
|
int i;
|
|
|
|
X x;
|
|
|
|
};
|
|
|
|
|
2016-12-05 15:49:14 +08:00
|
|
|
// CHECK: @.compoundliteral = internal global [5 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5], align 4
|
|
|
|
// CHECK: @q = global i32* getelementptr inbounds ([5 x i32], [5 x i32]* @.compoundliteral, i32 0, i32 0), align 4
|
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define i32 @_Z1fv()
|
2011-06-17 12:59:12 +08:00
|
|
|
int f() {
|
|
|
|
// CHECK: [[LVALUE:%[a-z0-9.]+]] = alloca
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK-NEXT: [[I:%[a-z0-9]+]] = getelementptr inbounds {{.*}}, {{.*}}* [[LVALUE]], i32 0, i32 0
|
2011-06-17 12:59:12 +08:00
|
|
|
// CHECK-NEXT: store i32 17, i32* [[I]]
|
|
|
|
// CHECK-NEXT: [[X:%[a-z0-9]+]] = getelementptr inbounds {{.*}} [[LVALUE]], i32 0, i32 1
|
2012-09-20 02:46:03 +08:00
|
|
|
// CHECK-NEXT: call %struct.X* @_ZN1XC1EPKc({{.*}}[[X]]
|
2011-06-17 12:59:12 +08:00
|
|
|
// CHECK-NEXT: [[I:%[a-z0-9]+]] = getelementptr inbounds {{.*}} [[LVALUE]], i32 0, i32 0
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[RESULT:%[a-z0-9]+]] = load i32, i32*
|
2012-09-20 02:46:03 +08:00
|
|
|
// CHECK-NEXT: call %struct.Y* @_ZN1YD1Ev
|
2011-06-17 12:59:12 +08:00
|
|
|
// CHECK-NEXT: ret i32 [[RESULT]]
|
|
|
|
return ((Y){17, "seventeen"}).i;
|
|
|
|
}
|
2011-11-13 08:51:30 +08:00
|
|
|
|
2013-08-15 14:47:53 +08:00
|
|
|
// CHECK-LABEL: define i32 @_Z1gv()
|
2011-11-13 08:51:30 +08:00
|
|
|
int g() {
|
2015-04-10 06:54:53 +08:00
|
|
|
// CHECK: store [2 x i32]* %{{[a-z0-9.]+}}, [2 x i32]** [[V:%[a-z0-9.]+]]
|
2011-11-13 08:51:30 +08:00
|
|
|
const int (&v)[2] = (int [2]) {1,2};
|
|
|
|
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK: [[A:%[a-z0-9.]+]] = load [2 x i32]*, [2 x i32]** [[V]]
|
2015-02-28 03:18:17 +08:00
|
|
|
// CHECK-NEXT: [[A0ADDR:%[a-z0-9.]+]] = getelementptr inbounds [2 x i32], [2 x i32]* [[A]], i32 0, {{.*}} 0
|
2015-02-28 05:19:58 +08:00
|
|
|
// CHECK-NEXT: [[A0:%[a-z0-9.]+]] = load i32, i32* [[A0ADDR]]
|
2011-11-13 08:51:30 +08:00
|
|
|
// CHECK-NEXT: ret i32 [[A0]]
|
|
|
|
return v[0];
|
|
|
|
}
|
2011-11-23 06:48:32 +08:00
|
|
|
|
2016-12-03 09:14:32 +08:00
|
|
|
// GCC's compound-literals-in-C++ extension lifetime-extends a compound literal
|
|
|
|
// (or a C++11 list-initialized temporary!) if:
|
|
|
|
// - it is at global scope
|
|
|
|
// - it has array type
|
|
|
|
// - it has a constant initializer
|
|
|
|
|
2011-11-23 06:48:32 +08:00
|
|
|
struct Z { int i[3]; };
|
|
|
|
int *p = (Z){ {1, 2, 3} }.i;
|
|
|
|
// CHECK: define {{.*}}__cxx_global_var_init()
|
2016-12-03 09:14:32 +08:00
|
|
|
// CHECK: alloca %struct.Z
|
|
|
|
// CHECK: store i32* %{{.*}}, i32** @p
|
2014-12-14 20:16:43 +08:00
|
|
|
|
2016-12-03 09:14:32 +08:00
|
|
|
int *q = (int [5]){1, 2, 3, 4, 5};
|
2016-12-05 15:49:14 +08:00
|
|
|
// (constant initialization, checked above)
|
|
|
|
|
|
|
|
extern int n;
|
|
|
|
int *r = (int [5]){1, 2, 3, 4, 5} + n;
|
2016-12-03 09:14:32 +08:00
|
|
|
// CHECK-LABEL: define {{.*}}__cxx_global_var_init.1()
|
2016-12-05 15:49:14 +08:00
|
|
|
// CHECK: %[[PTR:.*]] = getelementptr inbounds i32, i32* getelementptr inbounds ([5 x i32], [5 x i32]* @.compoundliteral.2, i32 0, i32 0), i32 %
|
|
|
|
// CHECK: store i32* %[[PTR]], i32** @r
|
2014-12-14 20:16:43 +08:00
|
|
|
|
2016-12-05 15:49:14 +08:00
|
|
|
int *PR21912_1 = (int []){} + n;
|
|
|
|
// CHECK-LABEL: define {{.*}}__cxx_global_var_init.3()
|
|
|
|
// CHECK: %[[PTR:.*]] = getelementptr inbounds i32, i32* getelementptr inbounds ([0 x i32], [0 x i32]* @.compoundliteral.4, i32 0, i32 0), i32 %
|
|
|
|
// CHECK: store i32* %[[PTR]], i32** @PR21912_1
|
2014-12-14 20:16:43 +08:00
|
|
|
|
|
|
|
union PR21912Ty {
|
|
|
|
long long l;
|
|
|
|
double d;
|
|
|
|
};
|
2016-12-05 15:49:14 +08:00
|
|
|
union PR21912Ty *PR21912_2 = (union PR21912Ty[]){{.d = 2.0}, {.l = 3}} + n;
|
|
|
|
// CHECK-LABEL: define {{.*}}__cxx_global_var_init.5()
|
|
|
|
// CHECK: %[[PTR:.*]] = getelementptr inbounds %union.PR21912Ty, %union.PR21912Ty* getelementptr inbounds ([2 x %union.PR21912Ty], [2 x %union.PR21912Ty]* bitcast (<{ { double }, %union.PR21912Ty }>* @.compoundliteral.6 to [2 x %union.PR21912Ty]*), i32 0, i32 0), i32 %
|
|
|
|
// CHECK: store %union.PR21912Ty* %[[PTR]], %union.PR21912Ty** @PR21912_2, align 4
|
A compound literal within a global lambda or block is still within
the body of a function for the purposes of computing its storage
duration and deciding whether its initializer must be constant.
There are a number of problems in our current treatment of compound
literals. C specifies that a compound literal yields an l-value
referring to an object with either static or automatic storage
duration, depending on where it was written; in the latter case,
the literal object has a lifetime tied to the enclosing scope (much
like an ObjC block), not the enclosing full-expression. To get these
semantics fully correct in our current design, we would need to
collect compound literals on the ExprWithCleanups, just like we do
with ObjC blocks; we would probably also want to identify literals
like we do with materialized temporaries. But it gets stranger;
GCC adds compound literals to C++ as an extension, but makes them
r-values, which are generally assumed to have temporary storage
duration. Ignoring destructor ordering, the difference only matters
if the object's address escapes the full-expression, which for an
r-value can only happen with reference binding (which extends
temporaries) or array-to-pointer decay (which does not). GCC then
attempts to lock down on array-to-pointer decay in ad hoc ways.
Arguably a far superior language solution for C++ (and perhaps even
array r-values in C, which can occur in other ways) would be to
propagate lifetime extension through array-to-pointer decay, so
that initializing a pointer object to a decayed r-value array
extends the lifetime of the complete object containing the array.
But this would be a major change in semantics which arguably ought
to be blessed by the committee(s).
Anyway, I'm not fixing any of that in this patch; I did try, but
it got out of hand.
Fixes rdar://28949016.
llvm-svn: 285643
2016-11-01 05:56:26 +08:00
|
|
|
|
|
|
|
// This compound literal should have local scope.
|
|
|
|
int computed_with_lambda = [] {
|
|
|
|
int *array = (int[]) { 1, 3, 5, 7 };
|
|
|
|
return array[0];
|
|
|
|
}();
|
|
|
|
// CHECK-LABEL: define internal i32 @{{.*}}clEv
|
|
|
|
// CHECK: alloca [4 x i32]
|