140 lines · cpp
1// RUN: %clang_cc1 -std=c++03 -verify -ast-dump %s > %t-032// RUN: FileCheck --input-file=%t-03 %s3// RUN: %clang_cc1 -std=c++11 -verify -ast-dump %s > %t-114// RUN: FileCheck --input-file=%t-11 %s5// RUN: FileCheck --input-file=%t-11 %s --check-prefix=CHECK-CXX116// RUN: %clang_cc1 -verify -std=c++17 %s7 8// http://llvm.org/PR79059namespace PR7905 {10struct S; // expected-note {{forward declaration}}11void foo1() {12 (void)(S[]) {{3}}; // expected-error {{array has incomplete element type}}13}14 15template <typename T> struct M { T m; };16void foo2() {17 (void)(M<short> []) {{3}};18}19}20 21// Check compound literals mixed with C++11 list-initialization.22namespace brace_initializers {23 struct POD {24 int x, y;25 };26 struct HasCtor {27 HasCtor(int x, int y);28 };29 struct HasDtor {30 int x, y;31 ~HasDtor();32 };33 struct HasCtorDtor {34 HasCtorDtor(int x, int y);35 ~HasCtorDtor();36 };37 38 POD p = (POD){1, 2};39 // CHECK-NOT: CXXBindTemporaryExpr {{.*}} 'brace_initializers::POD'40 // CHECK: CompoundLiteralExpr {{.*}} 'POD'{{$}}41 // CHECK-NEXT: InitListExpr {{.*}} 'POD'{{$}}42 // CHECK-NEXT: ConstantExpr {{.*}}43 // CHECK-NEXT: IntegerLiteral {{.*}} 1{{$}}44 // CHECK-NEXT: ConstantExpr {{.*}}45 // CHECK-NEXT: IntegerLiteral {{.*}} 2{{$}}46 47 void test() {48 (void)(POD){1, 2};49 // CHECK-NOT: CXXBindTemporaryExpr {{.*}} 'POD'50 // CHECK-NOT: ConstantExpr {{.*}} 'POD'51 // CHECK: CompoundLiteralExpr {{.*}} 'POD'{{$}}52 // CHECK-NEXT: InitListExpr {{.*}} 'POD'{{$}}53 // CHECK-NEXT: IntegerLiteral {{.*}} 1{{$}}54 // CHECK-NEXT: IntegerLiteral {{.*}} 2{{$}}55 56 (void)(HasDtor){1, 2};57 // CHECK: CXXBindTemporaryExpr {{.*}} 'HasDtor'58 // CHECK-NEXT: CompoundLiteralExpr {{.*}} 'HasDtor'{{$}}59 // CHECK-NEXT: InitListExpr {{.*}} 'HasDtor'{{$}}60 // CHECK-NEXT: IntegerLiteral {{.*}} 1{{$}}61 // CHECK-NEXT: IntegerLiteral {{.*}} 2{{$}}62 63#if __cplusplus >= 201103L64 (void)(HasCtor){1, 2};65 // CHECK-CXX11-NOT: CXXBindTemporaryExpr {{.*}} 'HasCtor'66 // CHECK-CXX11-NOT: ConstantExpr {{.*}} 'HasCtor'67 // CHECK-CXX11: CompoundLiteralExpr {{.*}} 'HasCtor'{{$}}68 // CHECK-CXX11-NEXT: CXXTemporaryObjectExpr {{.*}} 'HasCtor'69 // CHECK-CXX11-NEXT: IntegerLiteral {{.*}} 1{{$}}70 // CHECK-CXX11-NEXT: IntegerLiteral {{.*}} 2{{$}}71 72 (void)(HasCtorDtor){1, 2};73 // CHECK-CXX11: CXXBindTemporaryExpr {{.*}} 'HasCtorDtor'74 // CHECK-CXX11-NOT: ConstantExpr {{.*}} 'HasCtorDtor'75 // CHECK-CXX11: CompoundLiteralExpr {{.*}} 'HasCtorDtor'{{$}}76 // CHECK-CXX11-NEXT: CXXTemporaryObjectExpr {{.*}} 'HasCtorDtor'77 // CHECK-CXX11-NEXT: IntegerLiteral {{.*}} 1{{$}}78 // CHECK-CXX11-NEXT: IntegerLiteral {{.*}} 2{{$}}79#endif80 }81 82 struct PrivateDtor {83 int x, y;84 private:85 ~PrivateDtor(); // expected-note {{declared private here}}86 };87 88 void testPrivateDtor() {89 (void)(PrivateDtor){1, 2}; // expected-error {{temporary of type 'PrivateDtor' has private destructor}}90 }91}92 93// This doesn't necessarily need to be an error, but CodeGen can't handle it94// at the moment.95int PR17415 = (int){PR17415}; // expected-error {{initializer element is not a compile-time constant}}96 97// Make sure we accept this. (Not sure if we actually should... but we do98// at the moment.)99template<unsigned> struct Value { };100template<typename T>101int &check_narrowed(Value<sizeof((T){1.1})>);102 103#if __cplusplus >= 201103L104// Compound literals in global lambdas have automatic storage duration105// and are not subject to the constant-initialization rules.106int computed_with_lambda = [] {107 int x = 5;108 int result = ((int[]) { x, x + 2, x + 4, x + 6 })[0];109 return result;110}();111#endif112 113namespace DynamicFileScopeLiteral {114// This covers the case where we have a file-scope compound literal with a115// non-constant initializer in C++. Previously, we had a bug where Clang forgot116// to consider initializer list elements for bases.117struct Empty {};118struct Foo : Empty { // expected-note 0+ {{candidate constructor}}119 int x;120 int y;121};122int f();123#if __cplusplus < 201103L124// expected-error@+6 {{non-aggregate type 'Foo' cannot be initialized with an initializer list}}125#elif __cplusplus < 201703L126// expected-error@+4 {{no matching constructor}}127#else128// expected-error@+2 {{initializer element is not a compile-time constant}}129#endif130Foo o = (Foo){ {}, 1, f() };131}132 133#if __cplusplus >= 201103L134namespace GH147949 {135 // Make sure we handle transparent InitListExprs correctly.136 struct S { int x : 3; };137 const S* x = (const S[]){S{S{3}}};138}139#endif140