Skip to content

Commit c955f90

Browse files
committed
[Clang] [C++26] Expansion Statements (Part 11)
1 parent 7314e79 commit c955f90

File tree

5 files changed

+156
-5
lines changed

5 files changed

+156
-5
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ C++2c Feature Support
187187
At this timem, references to constexpr and decomposition of *tuple-like* types are not supported
188188
(only arrays and aggregates are).
189189

190+
- Implemented `P1306R5 <https://wg21.link/P1306R5>`_ Expansion Statements.
191+
190192
C++23 Feature Support
191193
^^^^^^^^^^^^^^^^^^^^^
192194

clang/include/clang/Basic/DiagnosticCommonKinds.td

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ def select_constexpr_spec_kind : TextSubstitution<
2222
def fatal_too_many_errors
2323
: Error<"too many errors emitted, stopping now">, DefaultFatal;
2424

25-
// TODO: Remove this.
26-
def err_expansion_statements_todo : Error<
27-
"TODO (expansion statements)">;
28-
2925
def warn_stack_exhausted : Warning<
3026
"stack nearly exhausted; compilation time may suffer, and "
3127
"crashes due to stack overflow are likely">,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Test without serialization:
2+
// RUN: %clang_cc1 -std=c++26 -triple x86_64-unknown-unknown -ast-dump %s
3+
//
4+
// Test with serialization:
5+
// RUN: %clang_cc1 -std=c++26 -triple x86_64-unknown-unknown -emit-pch -o %t %s
6+
// RUN: %clang_cc1 -x c++ -std=c++26 -triple x86_64-unknown-unknown -include-pch %t -ast-dump-all /dev/null \
7+
// RUN: | sed -e "s/ <undeserialized declarations>//" -e "s/ imported//"
8+
9+
template <typename T, __SIZE_TYPE__ size>
10+
struct Array {
11+
T data[size]{};
12+
constexpr const T* begin() const { return data; }
13+
constexpr const T* end() const { return data + size; }
14+
};
15+
16+
void foo(int);
17+
18+
template <typename T>
19+
void test(T t) {
20+
// CHECK: CXXExpansionStmtDecl
21+
// CHECK-NEXT: CXXEnumeratingExpansionStmtPattern
22+
// CHECK: CXXExpansionStmtInstantiation
23+
template for (auto x : {1, 2, 3}) {
24+
foo(x);
25+
}
26+
27+
// CHECK: CXXExpansionStmtDecl
28+
// CHECK-NEXT: CXXIteratingExpansionStmtPattern
29+
// CHECK: CXXExpansionStmtInstantiation
30+
static constexpr Array<int, 3> a;
31+
template for (auto x : a) {
32+
foo(x);
33+
}
34+
35+
// CHECK: CXXExpansionStmtDecl
36+
// CHECK-NEXT: CXXDestructuringExpansionStmtPattern
37+
// CHECK: CXXExpansionStmtInstantiation
38+
int arr[3]{1, 2, 3};
39+
template for (auto x : arr) {
40+
foo(x);
41+
}
42+
43+
// CHECK: CXXExpansionStmtDecl
44+
// CHECK-NEXT: CXXDependentExpansionStmtPattern
45+
// CHECK-NOT: CXXExpansionStmtInstantiation
46+
template for (auto x : t) {
47+
foo(x);
48+
}
49+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Without serialization:
2+
// RUN: %clang_cc1 -std=c++26 -ast-print %s | FileCheck %s
3+
//
4+
// With serialization:
5+
// RUN: %clang_cc1 -std=c++26 -emit-pch -o %t %s
6+
// RUN: %clang_cc1 -x c++ -std=c++26 -include-pch %t -ast-print /dev/null | FileCheck %s
7+
8+
template <typename T, __SIZE_TYPE__ size>
9+
struct Array {
10+
T data[size]{};
11+
constexpr const T* begin() const { return data; }
12+
constexpr const T* end() const { return data + size; }
13+
};
14+
15+
// CHECK: void foo(int);
16+
void foo(int);
17+
18+
// CHECK: template <typename T> void test(T t) {
19+
template <typename T>
20+
void test(T t) {
21+
// Enumerating expansion statement.
22+
//
23+
// CHECK: template for (auto x : { 1, 2, 3 }) {
24+
// CHECK-NEXT: foo(x);
25+
// CHECK-NEXT: }
26+
template for (auto x : {1, 2, 3}) {
27+
foo(x);
28+
}
29+
30+
// Iterating expansion statement.
31+
//
32+
// CHECK: static constexpr Array<int, 3> a;
33+
// CHECK-NEXT: template for (auto x : a) {
34+
// CHECK-NEXT: foo(x);
35+
// CHECK-NEXT: }
36+
static constexpr Array<int, 3> a;
37+
template for (auto x : a) {
38+
foo(x);
39+
}
40+
41+
// Destructuring expansion statement.
42+
//
43+
// CHECK: int arr[3]{1, 2, 3};
44+
// CHECK-NEXT: template for (auto x : arr) {
45+
// CHECK-NEXT: foo(x);
46+
// CHECK-NEXT: }
47+
int arr[3]{1, 2, 3};
48+
template for (auto x : arr) {
49+
foo(x);
50+
}
51+
52+
// Dependent expansion statement.
53+
//
54+
// CHECK: template for (auto x : t) {
55+
// CHECK-NEXT: foo(x);
56+
// CHECK-NEXT: }
57+
template for (auto x : t) {
58+
foo(x);
59+
}
60+
}
61+
62+
// CHECK: template <typename T> void test2(T t) {
63+
template <typename T>
64+
void test2(T t) {
65+
// Enumerating expansion statement.
66+
//
67+
// CHECK: template for (int x : { 1, 2, 3 }) {
68+
// CHECK-NEXT: foo(x);
69+
// CHECK-NEXT: }
70+
template for (int x : {1, 2, 3}) {
71+
foo(x);
72+
}
73+
74+
// Iterating expansion statement.
75+
//
76+
// CHECK: static constexpr Array<int, 3> a;
77+
// CHECK-NEXT: template for (int x : a) {
78+
// CHECK-NEXT: foo(x);
79+
// CHECK-NEXT: }
80+
static constexpr Array<int, 3> a;
81+
template for (int x : a) {
82+
foo(x);
83+
}
84+
85+
// Destructuring expansion statement.
86+
//
87+
// CHECK: int arr[3]{1, 2, 3};
88+
// CHECK-NEXT: template for (int x : arr) {
89+
// CHECK-NEXT: foo(x);
90+
// CHECK-NEXT: }
91+
int arr[3]{1, 2, 3};
92+
template for (int x : arr) {
93+
foo(x);
94+
}
95+
96+
// Dependent expansion statement.
97+
//
98+
// CHECK: template for (int x : t) {
99+
// CHECK-NEXT: foo(x);
100+
// CHECK-NEXT: }
101+
template for (int x : t) {
102+
foo(x);
103+
}
104+
}

clang/www/cxx_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ <h2 id="cxx26">C++2c implementation status</h2>
317317
<tr>
318318
<td>Expansion Statements</td>
319319
<td><a href="https://wg21.link/P1306">P1306R5</a></td>
320-
<td class="none" align="center">No</td>
320+
<td class="unreleased" align="center">Clang 22</td>
321321
</tr>
322322
<tr>
323323
<td>constexpr virtual inheritance</td>

0 commit comments

Comments
 (0)