From ef668aae1ee2b8bc904c50a13bf58df613b2f0b0 Mon Sep 17 00:00:00 2001 From: Petr Skocik Date: Fri, 23 Mar 2018 13:19:58 +0100 Subject: [PATCH] Don't fail on const/restrict/static/* inside [] This patch makes tcc ignore them. Normally (as per the C standard), They should be only applicable inside parameter arrays and affect (const/restrict) the pointer the array gets converted to. [matz: fix formatting, add volatile handling, add testcase, add comment about above deficiency] --- tccgen.c | 19 ++++++++++++-- tests/tests2/100_c99array-decls.c | 34 ++++++++++++++++++++++++++ tests/tests2/100_c99array-decls.expect | 0 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 tests/tests2/100_c99array-decls.c create mode 100644 tests/tests2/100_c99array-decls.expect diff --git a/tccgen.c b/tccgen.c index 7ed89acd..6ef40e45 100644 --- a/tccgen.c +++ b/tccgen.c @@ -4348,8 +4348,23 @@ static int post_type(CType *type, AttributeDef *ad, int storage, int td) int saved_nocode_wanted = nocode_wanted; /* array definition */ next(); - if (tok == TOK_RESTRICT1) - next(); + while (1) { + /* XXX The optional type-quals and static should only be accepted + in parameter decls. The '*' as well, and then even only + in prototypes (not function defs). */ + switch (tok) { + case TOK_RESTRICT1: case TOK_RESTRICT2: case TOK_RESTRICT3: + case TOK_CONST1: + case TOK_VOLATILE1: + case TOK_STATIC: + case '*': + next(); + continue; + default: + break; + } + break; + } n = -1; t1 = 0; if (tok != ']') { diff --git a/tests/tests2/100_c99array-decls.c b/tests/tests2/100_c99array-decls.c new file mode 100644 index 00000000..46950aa2 --- /dev/null +++ b/tests/tests2/100_c99array-decls.c @@ -0,0 +1,34 @@ +void foo(int [5]); +void fooc(int x[const 5]); +void foos(int x[static 5]); +void foov(int x[volatile 5]); +void foor(int x[restrict 5]); +void fooc(int [const 5]); +void foos(int [static 5]); +void foov(int [volatile 5]); +void foor(int [restrict 5]); +void fooc(int (* const x)); +void foos(int *x); +void foov(int * volatile x); +void foor(int * restrict x); +void fooc(int x[volatile 5]) +{ + x[3] = 42; +#ifdef INVALID + x = 0; +#endif +} +void foovm(int x[const *]); +void foovm(int * const x); +#ifdef INVALID +void wrongc(int x[3][const 4]); +void wrongvm(int x[static *]); +void foovm(int x[const *]) +{ + x[2] = 1; +} +#endif +int main() +{ + return 0; +} diff --git a/tests/tests2/100_c99array-decls.expect b/tests/tests2/100_c99array-decls.expect new file mode 100644 index 00000000..e69de29b