https://github.com/Sirraide updated https://github.com/llvm/llvm-project/pull/169690
>From 5463475279c6dccab08abe2bc20beed5c7713e27 Mon Sep 17 00:00:00 2001 From: Sirraide <[email protected]> Date: Wed, 26 Nov 2025 18:03:57 +0100 Subject: [PATCH] [Clang] [C++26] Expansion Statements (Part 11) --- clang/docs/ReleaseNotes.rst | 2 + .../clang/Basic/DiagnosticCommonKinds.td | 4 - clang/test/AST/ast-dump-expansion-stmt.cpp | 54 +++++++++ clang/test/AST/ast-print-expansion-stmts.cpp | 113 ++++++++++++++++++ clang/www/cxx_status.html | 8 +- 5 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 clang/test/AST/ast-dump-expansion-stmt.cpp create mode 100644 clang/test/AST/ast-print-expansion-stmts.cpp diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index fc17eead02589..1fab855bb1fc2 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -126,6 +126,8 @@ C++ Language Changes C++2c Feature Support ^^^^^^^^^^^^^^^^^^^^^ +- Clang now has partial support for `P1306R5 <https://wg21.link/P1306R5>`_ Expansion Statements. Iterating expansion + statements currently cannot be expanded and will result in a diagnostic, but other types of expansion statements work. C++23 Feature Support ^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/include/clang/Basic/DiagnosticCommonKinds.td b/clang/include/clang/Basic/DiagnosticCommonKinds.td index f1ee130eb1f11..cb267e3ee05c1 100644 --- a/clang/include/clang/Basic/DiagnosticCommonKinds.td +++ b/clang/include/clang/Basic/DiagnosticCommonKinds.td @@ -22,10 +22,6 @@ def select_constexpr_spec_kind : TextSubstitution< def fatal_too_many_errors : Error<"too many errors emitted, stopping now">, DefaultFatal; -// TODO: Remove this. -def err_expansion_statements_todo : Error< - "TODO (expansion statements)">; - def warn_stack_exhausted : Warning< "stack nearly exhausted; compilation time may suffer, and " "crashes due to stack overflow are likely">, diff --git a/clang/test/AST/ast-dump-expansion-stmt.cpp b/clang/test/AST/ast-dump-expansion-stmt.cpp new file mode 100644 index 0000000000000..94f0ee1449f17 --- /dev/null +++ b/clang/test/AST/ast-dump-expansion-stmt.cpp @@ -0,0 +1,54 @@ +// Test without serialization: +// RUN: %clang_cc1 -std=c++26 -triple x86_64-unknown-unknown -ast-dump %s +// +// Test with serialization: +// RUN: %clang_cc1 -std=c++26 -triple x86_64-unknown-unknown -emit-pch -o %t %s +// RUN: %clang_cc1 -x c++ -std=c++26 -triple x86_64-unknown-unknown -include-pch %t -ast-dump-all /dev/null \ +// RUN: | sed -e "s/ <undeserialized declarations>//" -e "s/ imported//" + +#if 0 // Disabled until we support iterating expansion statements. +template <typename T, __SIZE_TYPE__ size> +struct Array { + T data[size]{}; + constexpr const T* begin() const { return data; } + constexpr const T* end() const { return data + size; } +}; +#endif // 0 + +void foo(int); + +template <typename T> +void test(T t) { + // CHECK: CXXExpansionStmtDecl + // CHECK-NEXT: CXXExpansionStmtPattern {{.*}} enumerating + // CHECK: CXXExpansionStmtInstantiation + template for (auto x : {1, 2, 3}) { + foo(x); + } + +#if 0 // Disabled until we support iterating expansion statements. + // NOTE: Remove 'DISABLED-' when the '#if 0' is removed. + // DISABLED-CHECK: CXXExpansionStmtDecl + // DISABLED-CHECK-NEXT: CXXExpansionStmtPattern {{.*}} iterating + // DISABLED-CHECK: CXXExpansionStmtInstantiation + static constexpr Array<int, 3> a; + template for (auto x : a) { + foo(x); + } +#endif + + // CHECK: CXXExpansionStmtDecl + // CHECK-NEXT: CXXExpansionStmtPattern {{.*}} destructuring + // CHECK: CXXExpansionStmtInstantiation + int arr[3]{1, 2, 3}; + template for (auto x : arr) { + foo(x); + } + + // CHECK: CXXExpansionStmtDecl + // CHECK-NEXT: CXXExpansionStmtPattern {{.*}} dependent + // CHECK-NOT: CXXExpansionStmtInstantiation + template for (auto x : t) { + foo(x); + } +} diff --git a/clang/test/AST/ast-print-expansion-stmts.cpp b/clang/test/AST/ast-print-expansion-stmts.cpp new file mode 100644 index 0000000000000..880e17a9d3e2f --- /dev/null +++ b/clang/test/AST/ast-print-expansion-stmts.cpp @@ -0,0 +1,113 @@ +// Without serialization: +// RUN: %clang_cc1 -std=c++26 -ast-print %s | FileCheck %s +// +// With serialization: +// RUN: %clang_cc1 -std=c++26 -emit-pch -o %t %s +// RUN: %clang_cc1 -x c++ -std=c++26 -include-pch %t -ast-print /dev/null | FileCheck %s + +#if 0 // Disabled until we support iterating expansion statements. +template <typename T, __SIZE_TYPE__ size> +struct Array { + T data[size]{}; + constexpr const T* begin() const { return data; } + constexpr const T* end() const { return data + size; } +}; +#endif // 0 + +// CHECK: void foo(int); +void foo(int); + +// CHECK: template <typename T> void test(T t) { +template <typename T> +void test(T t) { + // Enumerating expansion statement. + // + // CHECK: template for (auto x : {1, 2, 3}) { + // CHECK-NEXT: foo(x); + // CHECK-NEXT: } + template for (auto x : {1, 2, 3}) { + foo(x); + } + +#if 0 // Disabled until we support iterating expansion statements. + // Iterating expansion statement. + // + // NOTE: Remove 'DISABLED-' when the '#if 0' is removed. + // DISABLED-CHECK: static constexpr Array<int, 3> a; + // DISABLED-CHECK-NEXT: template for (auto x : (a)) { + // DISABLED-CHECK-NEXT: foo(x); + // DISABLED-CHECK-NEXT: } + static constexpr Array<int, 3> a; + template for (auto x : a) { + foo(x); + } +#endif // 0 + + // Destructuring expansion statement. + // + // CHECK: int arr[3]{1, 2, 3}; + // CHECK-NEXT: template for (auto x : arr) { + // CHECK-NEXT: foo(x); + // CHECK-NEXT: } + int arr[3]{1, 2, 3}; + template for (auto x : arr) { + foo(x); + } + + // Dependent expansion statement. + // + // CHECK: template for (auto x : t) { + // CHECK-NEXT: foo(x); + // CHECK-NEXT: } + template for (auto x : t) { + foo(x); + } +} + +// CHECK: template <typename T> void test2(T t) { +template <typename T> +void test2(T t) { + // Enumerating expansion statement. + // + // CHECK: template for (int x : {1, 2, 3}) { + // CHECK-NEXT: foo(x); + // CHECK-NEXT: } + template for (int x : {1, 2, 3}) { + foo(x); + } + +#if 0 // Disabled until we support iterating expansion statements. + // Iterating expansion statement. + // + // NOTE: Remove 'DISABLED-' when the '#if 0' is removed. + // DISABLED-CHECK: static constexpr Array<int, 3> a; + // DISABLED-CHECK-NEXT: template for (int x : (a)) { + // DISABLED-CHECK-NEXT: foo(x); + // DISABLED-CHECK-NEXT: } + + static constexpr Array<int, 3> a; + template for (int x : a) { + foo(x); + } +#endif // 0 + + // Destructuring expansion statement. + // + // CHECK: int arr[3]{1, 2, 3}; + // CHECK-NEXT: template for (int x : arr) { + // CHECK-NEXT: foo(x); + // CHECK-NEXT: } + int arr[3]{1, 2, 3}; + template for (int x : arr) { + foo(x); + } + + // Dependent expansion statement. + // + // CHECK: template for (int x : t) { + // CHECK-NEXT: foo(x); + // CHECK-NEXT: } + template for (int x : t) { + foo(x); + } +} diff --git a/clang/www/cxx_status.html b/clang/www/cxx_status.html index 2c834b07f9a8f..9ff43c713d5b3 100755 --- a/clang/www/cxx_status.html +++ b/clang/www/cxx_status.html @@ -320,7 +320,13 @@ <h2 id="cxx26">C++2c implementation status</h2> <tr> <td>Expansion Statements</td> <td><a href="https://wg21.link/P1306">P1306R5</a></td> - <td class="none" align="center">No</td> + <td class="partial" align="center"> + <details> + <summary>Clang 23 (Partial)</summary> + Iterating expansion statements currently cannot be expanded and will + result in a diagnostic, but other types of expansion statements work. + </details> + </td> </tr> <tr> <td>constexpr virtual inheritance</td> _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
