X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/blobdiff_plain/d994d4c8bc35514ab5fafff7177cdf6d4e3c61ce..refs/heads/master:/t/declgen.c diff --git a/t/declgen.c b/t/declgen.c index 459a68e..9824c40 100644 --- a/t/declgen.c +++ b/t/declgen.c @@ -1,6 +1,6 @@ /* * Generate random C declarations for testing. - * Copyright © 2012, 2021-2023 Nick Bowler + * Copyright © 2012, 2021-2024 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 @@ -23,7 +23,6 @@ #include #include #include -#include #include "declgen.h" #include "cdecl.h" @@ -33,7 +32,7 @@ * Return a newly-allocated null declarator. The child member is set by the * argument; other members are initialized to zero. */ -struct cdecl_declarator *new_declarator(struct cdecl_declarator *child) +static struct cdecl_declarator *new_declarator(struct cdecl_declarator *child) { struct cdecl_declarator *d, init = { child, CDECL_DECL_NULL }; @@ -47,7 +46,7 @@ struct cdecl_declarator *new_declarator(struct cdecl_declarator *child) * Return a newly-allocated void specifier. The next member is set by the * argument; other members are initialized to zero. */ -struct cdecl_declspec *new_specifier(struct cdecl_declspec *next) +static struct cdecl_declspec *new_specifier(struct cdecl_declspec *next) { struct cdecl_declspec *d, init = { next, CDECL_TYPE_VOID }; @@ -61,7 +60,7 @@ struct cdecl_declspec *new_specifier(struct cdecl_declspec *next) * Return a newly-allocated declaration. The next member is set by the * argument; other members are initialized to zero. */ -struct cdecl *new_cdecl(struct cdecl *next) +static struct cdecl *new_cdecl(struct cdecl *next) { struct cdecl *d, init = { next }; @@ -76,18 +75,18 @@ struct cdecl *new_cdecl(struct cdecl *next) * possible. Avoid generating keywords, including the English ones, by * excluding the letters t, o, a and n. Reserved words are OK. */ -char *gen_identifier(struct test_rng *rng) +static 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; @@ -99,16 +98,17 @@ char *gen_identifier(struct test_rng *rng) * list is (potentially) unbounded. We handle the potential unboundedness by * generating a list of n qualifiers with probability 1/2^(n+1). Each * qualifier is chosen uniformly at random from the set of possibilities: - * const, volatile and, if restrictqual is true, restrict. + * const, volatile and, if restrictqual is nonzero, restrict. */ -struct cdecl_declspec *gen_qualifiers(struct test_rng *rng, bool restrictqual) +static struct cdecl_declspec * +gen_qualifiers(struct test_rng *rng, int restrictqual) { struct cdecl_declspec *s = NULL; while (test_rng_50_50(rng)) { s = new_specifier(s); - switch (test_rng_uniform_int(rng, 2+restrictqual)) { + switch (test_rng_uniform_int(rng, 2+!!restrictqual)) { case 0: s->type = CDECL_QUAL_CONST; break; @@ -130,7 +130,7 @@ struct cdecl_declspec *gen_qualifiers(struct test_rng *rng, bool restrictqual) * Generate random function specifiers. Like qualifiers, function specifiers * can appear multiple times. */ -struct cdecl_declspec *gen_funcspecs(struct test_rng *rng) +static struct cdecl_declspec *gen_funcspecs(struct test_rng *rng) { struct cdecl_declspec *s = NULL; @@ -144,10 +144,11 @@ struct cdecl_declspec *gen_funcspecs(struct test_rng *rng) /* * Generate zero or one random storage-class specifier. If registeronly is - * true, then the only possible storage-class specifier is "register". + * nonzero, then the only possible storage-class specifier is "register". * Otherwise, a specifier type will be selected uniformly at random. */ -struct cdecl_declspec *gen_storspecs(struct test_rng *rng, bool registeronly) +static struct cdecl_declspec * +gen_storspecs(struct test_rng *rng, int registeronly) { struct cdecl_declspec *s; @@ -173,7 +174,8 @@ struct cdecl_declspec *gen_storspecs(struct test_rng *rng, bool registeronly) */ #include "typegen.h" -struct cdecl_declspec *gen_typespecs(struct test_rng *rng, bool voidtype) +static struct cdecl_declspec * +gen_typespecs(struct test_rng *rng, int voidtype) { struct cdecl_declspec *specs; @@ -205,16 +207,16 @@ retry: return specs; } -struct cdecl_declspec * +static 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 +224,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 +239,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]; @@ -282,16 +284,17 @@ gen_declspecs(struct test_rng *rng, unsigned flags) for (p = s; p->next;) p = p->next; - p->next = gen_qualifiers(rng, false); + p->next = gen_qualifiers(rng, 0); return gen_randomize_specs(rng, s); } -static uintmax_t gen_uintmax(struct test_rng *rng) +static cdecl_uintmax gen_uintmax(struct test_rng *rng) { + cdecl_uintmax ret = 0; 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; @@ -386,7 +389,7 @@ struct cdecl_declarator *gen_declarators(struct test_rng *rng) switch (test_rng_uniform_int(rng, limit)) { case 0: d->type = CDECL_DECL_POINTER; - d->u.pointer.qualifiers = gen_qualifiers(rng, true); + d->u.pointer.qualifiers = gen_qualifiers(rng, 1); limit = 3; break; case 1: @@ -399,7 +402,7 @@ struct cdecl_declarator *gen_declarators(struct test_rng *rng) struct cdecl_pointer *ptr = &p->u.pointer; gen_free_declspecs(ptr->qualifiers); - ptr->qualifiers = gen_qualifiers(rng, false); + ptr->qualifiers = gen_qualifiers(rng, 0); } limit = 1; break;