On 12/28/23 15:44, Patrick Pelissier wrote:
Hi,
The following program doesn't compile with tinycc:
typedef int T[2];
int f(T t) {
return _Generic(t, __typeof__( ((void)0,(T){0})) : 1 );
}
whereas I was expecting it to compile (like for GCC/CLANG) due to
"promotion" of "array of int" type to "int pointer" type since it uses
the comma operator.
I made a patch. See attachment.
I cannot find a lot of information on this feature.
Is it a gcc/clang extension?
Does it only apply to typeof?
Herman
diff --git a/tccgen.c b/tccgen.c
index 29dae91..4d6f91a 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -45,6 +45,7 @@ static Sym *all_cleanups, *pending_gotos;
static int local_scope;
static int in_sizeof;
static int constant_p;
+static int comma_found;
ST_DATA char debug_modes;
ST_DATA SValue *vtop;
@@ -4782,9 +4783,12 @@ static int parse_btype(CType *type, AttributeDef *ad,
int ignore_label)
case TOK_TYPEOF2:
case TOK_TYPEOF3:
next();
+ comma_found = 0;
parse_expr_type(&type1);
/* remove all storage modifiers except typedef */
type1.t &= ~(VT_STORAGE&~VT_TYPEDEF);
+ if (comma_found)
+ type1.t &= ~(VT_CONSTANT | VT_VOLATILE | VT_ARRAY);
if (type1.ref)
sym_to_attr(ad, type1.ref);
goto basic_type2;
@@ -6553,6 +6557,7 @@ ST_FUNC void gexpr(void)
break;
constant_p &= (vtop->r & (VT_VALMASK | VT_LVAL)) == VT_CONST &&
!((vtop->r & VT_SYM) && vtop->sym->a.addrtaken);
+ comma_found = 1;
vpop();
next();
}
diff --git a/tests/tests2/94_generic.c b/tests/tests2/94_generic.c
index 1cb0164..600b311 100644
--- a/tests/tests2/94_generic.c
+++ b/tests/tests2/94_generic.c
@@ -30,6 +30,12 @@ void void_foo(int i) {}
typedef int int_type1;
+typedef int T[2];
+int f(T t)
+{
+ return _Generic(t, __typeof__( ((void)0,(T){0})) : 1 );
+}
+
#define gen_sw(a) _Generic(a, const char *: 1, default: 8, int: 123);
int main()
@@ -40,6 +46,7 @@ int main()
const int * const ptr;
const char *ti;
int_type1 i2;
+ T t;
i = _Generic(a, int: a_f, const int: b_f)();
printf("%d\n", i);
@@ -118,5 +125,7 @@ int main()
(void)(sizeof(struct { int x:_Generic( 0?(int (*)[5])0 : ar, int
(*)[5]:+1, int (*)[4]:(void)0); }));
}
+ printf ("%d\n", f(t));
+
return 0;
}
diff --git a/tests/tests2/94_generic.expect b/tests/tests2/94_generic.expect
index 80d4ed9..79be7ab 100644
--- a/tests/tests2/94_generic.expect
+++ b/tests/tests2/94_generic.expect
@@ -13,3 +13,4 @@ long
1
3
5
+1
_______________________________________________
Tinycc-devel mailing list
Tinycc-devel@nongnu.org
https://lists.nongnu.org/mailman/listinfo/tinycc-devel