From 1f214e1390b16148287eb80c8e365ec873d497fe Mon Sep 17 00:00:00 2001 From: Christopher Bazley Date: Sat, 26 Sep 2026 08:45:02 +0100 Subject: [PATCH] tccgen: stop ignoring qualifiers in array-like parameters To ensure that qualifiers within an array-style parameter declaration (e.g. int [const 2] are handled correctly, post_type() is modified to collect qualifiers and OR them into the t member of CType. The new test for restrict also passes, but only because the restrict keyword is ignored. --- tccgen.c | 12 ++++++++++-- tests/tests2/149_parameter_qualifiers.c | 10 ++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/tccgen.c b/tccgen.c index a899444c..ab04f30a 100644 --- a/tccgen.c +++ b/tccgen.c @@ -5144,6 +5144,7 @@ static int post_type(CType *type, AttributeDef *ad, int storage, int td) } else if (tok == '[') { int saved_nocode_wanted = nocode_wanted; + int array_qualifiers = 0; /* array definition */ next(); n = -1; @@ -5153,9 +5154,15 @@ static int post_type(CType *type, AttributeDef *ad, int storage, int td) in parameter decls. The '*' as well, and then even only in prototypes (not function defs). */ switch (tok) { + case TOK_CONST1: case TOK_CONST2: case TOK_CONST3: + array_qualifiers |= VT_CONSTANT; + next(); + continue; + case TOK_VOLATILE1: case TOK_VOLATILE2: case TOK_VOLATILE3: + array_qualifiers |= VT_VOLATILE; + next(); + continue; case TOK_RESTRICT1: case TOK_RESTRICT2: case TOK_RESTRICT3: - case TOK_CONST1: - case TOK_VOLATILE1: case TOK_STATIC: case '*': next(); @@ -5240,6 +5247,7 @@ check: s = sym_push(SYM_FIELD, type, 0, n); type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR; type->ref = s; + type->t |= array_qualifiers; if (vla_array_str) { /* for function args, the top dimension is converted to pointer */ diff --git a/tests/tests2/149_parameter_qualifiers.c b/tests/tests2/149_parameter_qualifiers.c index 663592c0..6f042173 100644 --- a/tests/tests2/149_parameter_qualifiers.c +++ b/tests/tests2/149_parameter_qualifiers.c @@ -8,6 +8,16 @@ void pointer_parameters(int *const cp, int *volatile vp, int *restrict rp) "restrict pointer parameter remains qualified"); } +void array_parameters(int ca[const 2], int va[volatile 2], int ra[restrict 2]) +{ + _Static_assert(_Generic(&ca, int *const *: 1, default: 0), + "const array parameter adjusts to qualified pointer"); + _Static_assert(_Generic(&va, int *volatile *: 1, default: 0), + "volatile array parameter adjusts to qualified pointer"); + _Static_assert(_Generic(&ra, int *restrict *: 1, default: 0), + "restrict array parameter adjusts to qualified pointer"); +} + void declared_const(int *const pointer); void declared_const(int *pointer) {