[clang] [Clang][Sema] Add regression tests for typeof with compound literals (PR #248383)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 3 02:34:06 PST 2026
https://github.com/YiHx updated https://github.com/llvm/llvm-project/pull/248383
>From 0f62770a2bfefb282044defeda3db724eaed4f02 Mon Sep 17 00:00:00 2001
From: YiHx <yhx2582413 at gmail.com>
Date: Thu, 3 Dec 2026 18:22:08 +0800
Subject: [PATCH] [Clang][Sema] Add regression tests for typeof with compound
literals
---
clang/test/Sema/compound-literal.c | 71 ++++++++++++++++++++++++++++++
clang/test/Sema/gh87867.c | 33 --------------
2 files changed, 71 insertions(+), 33 deletions(-)
delete mode 100644 clang/test/Sema/gh87867.c
diff --git a/clang/test/Sema/compound-literal.c b/clang/test/Sema/compound-literal.c
index 1026e5ece11b49..7b5bfdfdc457cb 100644
--- a/clang/test/Sema/compound-literal.c
+++ b/clang/test/Sema/compound-literal.c
@@ -54,3 +54,74 @@ void vla(int n) {
(void)(int[size]){1,2,3}; // expected-error {{compound literal cannot be of variable-length array type}}
(void)(int[size]){1,2,3,4,5}; // expected-error {{compound literal cannot be of variable-length array type}}
}
+
+// Compound literal doesn't need a constant expression inside a initializer-list if it is already inside a function
+// see: https://github.com/llvm/llvm-project/issues/87867
+int foo(int *a, int b) {
+ return 0;
+}
+
+int gh87867_x;
+struct{int t;} a = (struct {
+ __typeof__(foo(&(struct { int t; }){.t = gh87867_x}.t, 0)) t; // expected-error {{initializer element is not a compile-time constant}}
+}){0};
+
+void inside_a_func(void){
+ int x;
+ (void)(struct {
+ __typeof__(foo(&(struct { int t; }){.t = x}.t, 0)) t;
+ }){0};
+}
+
+// see: https://github.com/llvm/llvm-project/issues/143613
+#define bitcast(type, value) \
+ (((union{ __typeof__(value) src; type dst; }){ (value) }).dst)
+
+
+double placeholder = 10.0;
+double bar = bitcast(double, placeholder); // expected-error {{initializer element is not a compile-time constant}}
+
+int gh143613_test(void)
+{
+ int foo = 4;
+ foo = bitcast(int, bitcast(double, foo));
+ return 0;
+}
+
+// see: https://github.com/llvm/llvm-project/issues/25689
+extern void *blah(void *arg);
+
+const void *test2(const void *arg)
+{
+ return blah( ( { // expected-warning {{use of GNU statement expression extension}}
+ union {
+ __typeof__((*(const void * []) { (arg) })) _value;
+ void * _cast;
+ } _union;
+ _union._value = ((*(const void * []) { (arg) }));
+ _union._cast;
+ } ) );
+}
+
+#define CAST_FROM_TO(from_type, to_type, value) \
+ ( { \
+ union { \
+ from_type _value; \
+ to_type _cast; \
+ } _union = { ._value = (value) }; \
+ _union._cast; \
+ } )
+#define CAST_TO(to_type, value) CAST_FROM_TO(__typeof__(value), to_type, value)
+
+
+struct idelay {
+ unsigned int value : 5;
+};
+
+void hw_write_rev_clk_idelay(struct idelay *idelay, unsigned int delay)
+{
+ unsigned int temp_val = CAST_TO(unsigned int, // expected-warning {{use of GNU statement expression extension}}
+ ((struct idelay) { .value = delay & 0x1F } ));
+ unsigned int *temp_reg = (unsigned int *) idelay;
+ *temp_reg = temp_val;
+}
diff --git a/clang/test/Sema/gh87867.c b/clang/test/Sema/gh87867.c
deleted file mode 100644
index 0568c734424cac..00000000000000
--- a/clang/test/Sema/gh87867.c
+++ /dev/null
@@ -1,33 +0,0 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -std=c23 %s
-
-// Compound literal doesn't need a constant expression inside a initializer-list if it is already inside a function
-// see: https://github.com/llvm/llvm-project/issues/87867
-int foo(int *a, int b) {
- return 0;
-}
-
-int x;
-struct{int t;} a = (struct {
- typeof(foo(&(struct { int t; }){.t = x}.t, 0)) t; // expected-error {{initializer element is not a compile-time constant}}
-}){0};
-
-void inside_a_func(){
- int x;
- (void)(struct {
- typeof(foo(&(struct { int t; }){.t = x}.t, 0)) t;
- }){0};
-}
-
-// see: https://github.com/llvm/llvm-project/issues/143613
-#define bitcast(type, value) \
- (((union{ typeof(value) src; type dst; }){ (value) }).dst)
-
-double placeholder = 10.0;
-double bar = bitcast(double, placeholder); // expected-error {{initializer element is not a compile-time constant}}
-
-int main(void)
-{
- int foo = 4;
- foo = bitcast(int, bitcast(double, foo));
- return 0;
-}
More information about the cfe-commits
mailing list