From 7f52c6d4be39c4d5ece844011de3581fe718b199 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Wed, 29 Nov 2023 20:56:44 -0500 Subject: [PATCH] Avoid the use of for loop declarations. This C99 syntax is now almost entirely contained to test programs; at this point using such syntax is just an obstacle to a successful build rather than anything useful. It is now possible to build with gcc in C89 mode, and probably some other pre-C99 compilers. One annoyance is the reference code for the RNG uses such declarations. We avoid changing the reference code and instead arrange for the test to be skipped unless compiler support is available. --- common | 2 +- configure.ac | 8 +++++--- src/parse-decl.c | 7 ++++--- t/crossparse.c | 3 ++- t/declgen.c | 19 ++++++++++--------- t/randomdecl.c | 8 ++++---- t/rng-test.c | 7 +++++++ 7 files changed, 33 insertions(+), 21 deletions(-) diff --git a/common b/common index ed04bed..6405aa8 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit ed04bed43efece46d2b476fbb0260f1d1b8aa1fe +Subproject commit 6405aa895740d960d6f6e70976eeb44bd403f952 diff --git a/configure.ac b/configure.ac index b963324..724b1b0 100644 --- a/configure.ac +++ b/configure.ac @@ -25,11 +25,13 @@ AC_PROG_CC_C99 AM_PROG_CC_C_O gl_EARLY -LT_INIT -gl_INIT - AC_HEADER_ASSERT AC_C_FLEXIBLE_ARRAY_MEMBER +AC_C_INLINE +DX_C_FOR_DECLARATIONS + +LT_INIT +gl_INIT # Work around quoting bug in Gnulib threadlib.m4 which prevents # correct detection on e.g., Solaris 8. These platforms require diff --git a/src/parse-decl.c b/src/parse-decl.c index 1268fc3..8f0bd38 100644 --- a/src/parse-decl.c +++ b/src/parse-decl.c @@ -73,9 +73,10 @@ struct parse_item *cdecl__alloc_item(size_t s_sz) */ static int valid_typespec(struct cdecl_declspec *s) { + struct cdecl_declspec *c; unsigned long map = 0; - for (struct cdecl_declspec *c = s; c; c = c->next) { + for (c = s; c; c = c->next) { unsigned long bit; if (cdecl_spec_kind(c) != CDECL_SPEC_TYPE) @@ -117,7 +118,7 @@ static int valid_typespec(struct cdecl_declspec *s) */ static bool valid_declspecs(struct cdecl *decl, bool top) { - struct cdecl_declspec *specs = decl->specifiers; + struct cdecl_declspec *c, *specs = decl->specifiers; struct cdecl_declarator *d = decl->declarators; bool abstract = cdecl_is_abstract(d); unsigned num_storage = 0; @@ -125,7 +126,7 @@ static bool valid_declspecs(struct cdecl *decl, bool top) if (!valid_typespec(specs)) return false; - for (struct cdecl_declspec *c = specs; c; c = c->next) { + for (c = specs; c; c = c->next) { switch (cdecl_spec_kind(c)) { case CDECL_SPEC_TYPE: if (c->type == CDECL_TYPE_VOID && diff --git a/t/crossparse.c b/t/crossparse.c index 630e02e..4122b1c 100644 --- a/t/crossparse.c +++ b/t/crossparse.c @@ -137,6 +137,7 @@ int main(int argc, char **argv) { int opt, mode = MODE_CDECL; int ret = EXIT_SUCCESS; + int i; const char *filename = NULL; FILE *infile = NULL; @@ -192,7 +193,7 @@ int main(int argc, char **argv) free(line); fclose(infile); } else if (argv[optind]) { - for (int i = optind; i < argc; i++) { + for (i = optind; i < argc; i++) { if (!test_crossparse(argv[i], mode)) ret = EXIT_FAILURE; } diff --git a/t/declgen.c b/t/declgen.c index 459a68e..4526c0e 100644 --- a/t/declgen.c +++ b/t/declgen.c @@ -79,15 +79,15 @@ struct cdecl *new_cdecl(struct cdecl *next) char *gen_identifier(struct test_rng *rng) { static const char valid[59] = "_bcdefghijklmpqrsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + size_t i, n; char *str; - size_t n; n = test_rng_uniform_int(rng, 10)+1; str = malloc_nofail(n+1); /* Exclude 10 digits from the first character. */ str[0] = valid[test_rng_uniform_int(rng, sizeof valid - 10)]; - for (size_t i = 1; i < n; i++) + for (i = 1; i < n; i++) str[i] = valid[test_rng_uniform_int(rng, sizeof valid)]; str[n] = 0; @@ -208,13 +208,13 @@ retry: struct cdecl_declspec * gen_randomize_specs(struct test_rng *rng, struct cdecl_declspec *specs) { - struct cdecl_declspec **p; - size_t n = 0; + struct cdecl_declspec *s, **p; + size_t i, n = 0; if (!specs) return specs; - for (struct cdecl_declspec *s = specs; s; s = s->next) + for (s = specs; s; s = s->next) n++; p = malloc_nofail((n+1) * sizeof *p); @@ -222,11 +222,11 @@ gen_randomize_specs(struct test_rng *rng, struct cdecl_declspec *specs) /* Build a temporary array for easy element swapping. */ p[0] = specs; p[n] = NULL; - for (size_t i = 1; i < n; i++) + for (i = 1; i < n; i++) p[i] = p[i-1]->next; /* Knuth shuffle. */ - for (size_t i = 0; i < n; i++) { + for (i = 0; i < n; i++) { struct cdecl_declspec *tmp; size_t r; @@ -237,7 +237,7 @@ gen_randomize_specs(struct test_rng *rng, struct cdecl_declspec *specs) } /* Fix up the pointers. */ - for (size_t i = 1; i <= n; i++) + for (i = 1; i <= n; i++) p[i-1]->next = p[i]; specs = p[0]; @@ -290,8 +290,9 @@ static uintmax_t gen_uintmax(struct test_rng *rng) { unsigned char tmp; uintmax_t ret = 0; + size_t i; - for (size_t i = 0; i < sizeof ret; i++) { + for (i = 0; i < sizeof ret; i++) { tmp = test_rng_uniform_int(rng, UCHAR_MAX+1); ret <<= CHAR_BIT; ret |= tmp; diff --git a/t/randomdecl.c b/t/randomdecl.c index 9e9271e..1fbd2d9 100644 --- a/t/randomdecl.c +++ b/t/randomdecl.c @@ -1,6 +1,6 @@ /* * Generate random C declarations for testing. - * Copyright © 2012, 2020, 2022 Nick Bowler + * Copyright © 2012, 2020, 2022-2023 Nick Bowler * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -85,10 +85,10 @@ static struct cdecl *random_decl(struct test_rng *rng) int main(int argc, char **argv) { const char *seed = "", *count_str = NULL; + unsigned long i, count = 0; + int opt, mode = MODE_CDECL; struct test_rng *rng; struct cdecl *decl; - unsigned long count = 0; - int opt, mode = MODE_CDECL; if (argc > 0) progname = argv[0]; @@ -128,7 +128,7 @@ int main(int argc, char **argv) if (!rng) return EXIT_FAILURE; - for (unsigned long i = 0; !count || i < count; i++) { + for (i = 0; !count || i < count; i++) { decl = random_decl(rng); if (mode == MODE_ENGLISH) { diff --git a/t/rng-test.c b/t/rng-test.c index 1d5c6e9..a4ba6bb 100644 --- a/t/rng-test.c +++ b/t/rng-test.c @@ -21,6 +21,12 @@ #include #include "tap.h" +#if !HAVE_FOR_DECLS +int main(void) +{ + tap_skip_all("cannot compile reference xoshiro256+"); +} +#else #include "rng.c" #include "xos256p.c" @@ -71,3 +77,4 @@ int main(void) tap_done(); } +#endif -- 2.43.2