]> git.draconx.ca Git - cdecl99.git/commitdiff
Avoid the use of for loop declarations.
authorNick Bowler <nbowler@draconx.ca>
Thu, 30 Nov 2023 01:56:44 +0000 (20:56 -0500)
committerNick Bowler <nbowler@draconx.ca>
Thu, 30 Nov 2023 02:03:06 +0000 (21:03 -0500)
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
configure.ac
src/parse-decl.c
t/crossparse.c
t/declgen.c
t/randomdecl.c
t/rng-test.c

diff --git a/common b/common
index ed04bed43efece46d2b476fbb0260f1d1b8aa1fe..6405aa895740d960d6f6e70976eeb44bd403f952 160000 (submodule)
--- a/common
+++ b/common
@@ -1 +1 @@
-Subproject commit ed04bed43efece46d2b476fbb0260f1d1b8aa1fe
+Subproject commit 6405aa895740d960d6f6e70976eeb44bd403f952
index b9633244cd13b551b049ae9e3ed4f2d3f80cb96e..724b1b05efa0d112be39c424461bd28e17dda123 100644 (file)
@@ -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
index 1268fc387810cbd2c7a1ef380b01bc09097e3342..8f0bd388a1bcad91f83b23a4676e16652e2ccee8 100644 (file)
@@ -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 &&
index 630e02e05c0417e6b4cca4b224adef56cd3b826a..4122b1ce884d2463b4fd6e05fb3a5c48c149c922 100644 (file)
@@ -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;
                }
index 459a68e077ae19958ef5d8ce2d2651e75df22b67..4526c0ee69f0f86cc047c5066705600f91e5b07b 100644 (file)
@@ -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;
index 9e9271e5b0ad12303a44dd3159641a090fc98b6f..1fbd2d913b360114649b310a9e4983449024954c 100644 (file)
@@ -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) {
index 1d5c6e9373f16acb663e234ee6467d60f0dd0b31..a4ba6bb51c514bc8685d54a4d009dd93f248b7f6 100644 (file)
 #include <config.h>
 #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