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.
This commit is contained in:
Christopher Bazley 2026-09-26 08:45:02 +01:00
parent 5f1425ef21
commit 1f214e1390
2 changed files with 20 additions and 2 deletions

View File

@ -5144,6 +5144,7 @@ static int post_type(CType *type, AttributeDef *ad, int storage, int td)
} else if (tok == '[') { } else if (tok == '[') {
int saved_nocode_wanted = nocode_wanted; int saved_nocode_wanted = nocode_wanted;
int array_qualifiers = 0;
/* array definition */ /* array definition */
next(); next();
n = -1; 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 parameter decls. The '*' as well, and then even only
in prototypes (not function defs). */ in prototypes (not function defs). */
switch (tok) { 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_RESTRICT1: case TOK_RESTRICT2: case TOK_RESTRICT3:
case TOK_CONST1:
case TOK_VOLATILE1:
case TOK_STATIC: case TOK_STATIC:
case '*': case '*':
next(); next();
@ -5240,6 +5247,7 @@ check:
s = sym_push(SYM_FIELD, type, 0, n); s = sym_push(SYM_FIELD, type, 0, n);
type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR; type->t = (t1 ? VT_VLA : VT_ARRAY) | VT_PTR;
type->ref = s; type->ref = s;
type->t |= array_qualifiers;
if (vla_array_str) { if (vla_array_str) {
/* for function args, the top dimension is converted to pointer */ /* for function args, the top dimension is converted to pointer */

View File

@ -8,6 +8,16 @@ void pointer_parameters(int *const cp, int *volatile vp, int *restrict rp)
"restrict pointer parameter remains qualified"); "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 *const pointer);
void declared_const(int *pointer) void declared_const(int *pointer)
{ {