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
99 lines
3.4 KiB
C++
99 lines
3.4 KiB
C++
// RUN: %clang_cc1 -fsyntax-only -std=c++03 -verify -ast-dump %s > %t-03
|
|
// RUN: FileCheck --input-file=%t-03 %s
|
|
// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify -ast-dump %s > %t-11
|
|
// RUN: FileCheck --input-file=%t-11 %s
|
|
// RUN: FileCheck --input-file=%t-11 %s --check-prefix=CHECK-CXX11
|
|
|
|
// http://llvm.org/PR7905
|
|
namespace PR7905 {
|
|
struct S; // expected-note {{forward declaration}}
|
|
void foo1() {
|
|
(void)(S[]) {{3}}; // expected-error {{array has incomplete element type}}
|
|
}
|
|
|
|
template <typename T> struct M { T m; };
|
|
void foo2() {
|
|
(void)(M<short> []) {{3}};
|
|
}
|
|
}
|
|
|
|
// Check compound literals mixed with C++11 list-initialization.
|
|
namespace brace_initializers {
|
|
struct POD {
|
|
int x, y;
|
|
};
|
|
struct HasCtor {
|
|
HasCtor(int x, int y);
|
|
};
|
|
struct HasDtor {
|
|
int x, y;
|
|
~HasDtor();
|
|
};
|
|
struct HasCtorDtor {
|
|
HasCtorDtor(int x, int y);
|
|
~HasCtorDtor();
|
|
};
|
|
|
|
void test() {
|
|
(void)(POD){1, 2};
|
|
// CHECK-NOT: CXXBindTemporaryExpr {{.*}} 'struct brace_initializers::POD'
|
|
// CHECK: CompoundLiteralExpr {{.*}} 'struct brace_initializers::POD'
|
|
// CHECK-NEXT: InitListExpr {{.*}} 'struct brace_initializers::POD'
|
|
// CHECK-NEXT: IntegerLiteral {{.*}} 1{{$}}
|
|
// CHECK-NEXT: IntegerLiteral {{.*}} 2{{$}}
|
|
|
|
(void)(HasDtor){1, 2};
|
|
// CHECK: CXXBindTemporaryExpr {{.*}} 'struct brace_initializers::HasDtor'
|
|
// CHECK-NEXT: CompoundLiteralExpr {{.*}} 'struct brace_initializers::HasDtor'
|
|
// CHECK-NEXT: InitListExpr {{.*}} 'struct brace_initializers::HasDtor'
|
|
// CHECK-NEXT: IntegerLiteral {{.*}} 1{{$}}
|
|
// CHECK-NEXT: IntegerLiteral {{.*}} 2{{$}}
|
|
|
|
#if __cplusplus >= 201103L
|
|
(void)(HasCtor){1, 2};
|
|
// CHECK-CXX11-NOT: CXXBindTemporaryExpr {{.*}} 'struct brace_initializers::HasCtor'
|
|
// CHECK-CXX11: CompoundLiteralExpr {{.*}} 'struct brace_initializers::HasCtor'
|
|
// CHECK-CXX11-NEXT: CXXTemporaryObjectExpr {{.*}} 'struct brace_initializers::HasCtor'
|
|
// CHECK-CXX11-NEXT: IntegerLiteral {{.*}} 1{{$}}
|
|
// CHECK-CXX11-NEXT: IntegerLiteral {{.*}} 2{{$}}
|
|
|
|
(void)(HasCtorDtor){1, 2};
|
|
// CHECK-CXX11: CXXBindTemporaryExpr {{.*}} 'struct brace_initializers::HasCtorDtor'
|
|
// CHECK-CXX11-NEXT: CompoundLiteralExpr {{.*}} 'struct brace_initializers::HasCtorDtor'
|
|
// CHECK-CXX11-NEXT: CXXTemporaryObjectExpr {{.*}} 'struct brace_initializers::HasCtorDtor'
|
|
// CHECK-CXX11-NEXT: IntegerLiteral {{.*}} 1{{$}}
|
|
// CHECK-CXX11-NEXT: IntegerLiteral {{.*}} 2{{$}}
|
|
#endif
|
|
}
|
|
|
|
struct PrivateDtor {
|
|
int x, y;
|
|
private:
|
|
~PrivateDtor(); // expected-note {{declared private here}}
|
|
};
|
|
|
|
void testPrivateDtor() {
|
|
(void)(PrivateDtor){1, 2}; // expected-error {{temporary of type 'brace_initializers::PrivateDtor' has private destructor}}
|
|
}
|
|
}
|
|
|
|
// This doesn't necessarily need to be an error, but CodeGen can't handle it
|
|
// at the moment.
|
|
int PR17415 = (int){PR17415}; // expected-error {{initializer element is not a compile-time constant}}
|
|
|
|
// Make sure we accept this. (Not sure if we actually should... but we do
|
|
// at the moment.)
|
|
template<unsigned> struct Value { };
|
|
template<typename T>
|
|
int &check_narrowed(Value<sizeof((T){1.1})>);
|
|
|
|
#if __cplusplus >= 201103L
|
|
// Compound literals in global lambdas have automatic storage duration
|
|
// and are not subject to the constant-initialization rules.
|
|
int computed_with_lambda = [] {
|
|
int x = 5;
|
|
int result = ((int[]) { x, x + 2, x + 4, x + 6 })[0];
|
|
return result;
|
|
}();
|
|
#endif
|