Clang Project

clang_source_code/test/SemaCXX/compound-literal.cpp
1// RUN: %clang_cc1 -fsyntax-only -std=c++03 -verify -ast-dump %s > %t-03
2// RUN: FileCheck --input-file=%t-03 %s
3// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify -ast-dump %s > %t-11
4// RUN: FileCheck --input-file=%t-11 %s
5// RUN: FileCheck --input-file=%t-11 %s --check-prefix=CHECK-CXX11
6
7// http://llvm.org/PR7905
8namespace PR7905 {
9struct S; // expected-note {{forward declaration}}
10void foo1() {
11  (void)(S[]) {{3}}; // expected-error {{array has incomplete element type}}
12}
13
14template <typename T> struct M { T m; };
15void foo2() {
16  (void)(M<short> []) {{3}};
17}
18}
19
20// Check compound literals mixed with C++11 list-initialization.
21namespace brace_initializers {
22  struct POD {
23    int x, y;
24  };
25  struct HasCtor {
26    HasCtor(int x, int y);
27  };
28  struct HasDtor {
29    int x, y;
30    ~HasDtor();
31  };
32  struct HasCtorDtor {
33    HasCtorDtor(int x, int y);
34    ~HasCtorDtor();
35  };
36
37  POD p = (POD){1, 2};
38  // CHECK-NOT: CXXBindTemporaryExpr {{.*}} 'brace_initializers::POD'
39  // CHECK: CompoundLiteralExpr {{.*}} 'brace_initializers::POD'
40  // CHECK-NEXT: InitListExpr {{.*}} 'brace_initializers::POD'
41  // CHECK-NEXT: ConstantExpr {{.*}}
42  // CHECK-NEXT: IntegerLiteral {{.*}} 1{{$}}
43  // CHECK-NEXT: ConstantExpr {{.*}}
44  // CHECK-NEXT: IntegerLiteral {{.*}} 2{{$}}
45
46  void test() {
47    (void)(POD){1, 2};
48    // CHECK-NOT: CXXBindTemporaryExpr {{.*}} 'brace_initializers::POD'
49    // CHECK-NOT: ConstantExpr {{.*}} 'brace_initializers::POD'
50    // CHECK: CompoundLiteralExpr {{.*}} 'brace_initializers::POD'
51    // CHECK-NEXT: InitListExpr {{.*}} 'brace_initializers::POD'
52    // CHECK-NEXT: IntegerLiteral {{.*}} 1{{$}}
53    // CHECK-NEXT: IntegerLiteral {{.*}} 2{{$}}
54
55    (void)(HasDtor){1, 2};
56    // CHECK: CXXBindTemporaryExpr {{.*}} 'brace_initializers::HasDtor'
57    // CHECK-NEXT: CompoundLiteralExpr {{.*}} 'brace_initializers::HasDtor'
58    // CHECK-NEXT: InitListExpr {{.*}} 'brace_initializers::HasDtor'
59    // CHECK-NEXT: IntegerLiteral {{.*}} 1{{$}}
60    // CHECK-NEXT: IntegerLiteral {{.*}} 2{{$}}
61
62#if __cplusplus >= 201103L
63    (void)(HasCtor){1, 2};
64    // CHECK-CXX11-NOT: CXXBindTemporaryExpr {{.*}} 'brace_initializers::HasCtor'
65    // CHECK-CXX11-NOT: ConstantExpr {{.*}} 'brace_initializers::HasCtor'
66    // CHECK-CXX11: CompoundLiteralExpr {{.*}} 'brace_initializers::HasCtor'
67    // CHECK-CXX11-NEXT: CXXTemporaryObjectExpr {{.*}} 'brace_initializers::HasCtor'
68    // CHECK-CXX11-NEXT: IntegerLiteral {{.*}} 1{{$}}
69    // CHECK-CXX11-NEXT: IntegerLiteral {{.*}} 2{{$}}
70
71    (void)(HasCtorDtor){1, 2};
72    // CHECK-CXX11: CXXBindTemporaryExpr {{.*}} 'brace_initializers::HasCtorDtor'
73    // CHECK-CXX11-NOT: ConstantExpr {{.*}} 'brace_initializers::HasCtorDtor'
74    // CHECK-CXX11: CompoundLiteralExpr {{.*}} 'brace_initializers::HasCtorDtor'
75    // CHECK-CXX11-NEXT: CXXTemporaryObjectExpr {{.*}} 'brace_initializers::HasCtorDtor'
76    // CHECK-CXX11-NEXT: IntegerLiteral {{.*}} 1{{$}}
77    // CHECK-CXX11-NEXT: IntegerLiteral {{.*}} 2{{$}}
78#endif
79  }
80
81  struct PrivateDtor {
82    int x, y;
83  private:
84    ~PrivateDtor(); // expected-note {{declared private here}}
85  };
86
87  void testPrivateDtor() {
88    (void)(PrivateDtor){1, 2}; // expected-error {{temporary of type 'brace_initializers::PrivateDtor' has private destructor}}
89  }
90}
91
92// This doesn't necessarily need to be an error, but CodeGen can't handle it
93// at the moment.
94int PR17415 = (int){PR17415}; // expected-error {{initializer element is not a compile-time constant}}
95
96// Make sure we accept this.  (Not sure if we actually should... but we do
97// at the moment.)
98template<unsigned> struct Value { };
99template<typename T>
100int &check_narrowed(Value<sizeof((T){1.1})>);
101
102#if __cplusplus >= 201103L
103// Compound literals in global lambdas have automatic storage duration
104// and are not subject to the constant-initialization rules.
105int computed_with_lambda = [] {
106  int x = 5;
107  int result = ((int[]) { x, x + 2, x + 4, x + 6 })[0];
108  return result;
109}();
110#endif
111