2 * Generate random C declarations for testing.
3 * Copyright © 2011 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <gsl/gsl_rng.h>
37 * Generate a random identifier. We arbitrarily pick 10 as the largest
38 * possible. Avoid generating keywords, including the English ones, by
39 * excluding the letters t, o, a and n. Reserved words are OK.
41 char *gen_identifier(struct gen_rng *rng)
43 static const char valid[59] = "_bcdefghijklmpqrsuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
47 n = gsl_rng_uniform_int(rng->rng, 10)+1;
48 str = malloc_nofail(n+1);
50 /* Exclude 10 digits from the first character. */
51 str[0] = valid[gsl_rng_uniform_int(rng->rng, sizeof valid - 10)];
52 for (size_t i = 1; i < n; i++)
53 str[i] = valid[gsl_rng_uniform_int(rng->rng, sizeof valid)];
60 * Generate random qualifiers. Qualifiers can appear multiple times, so the
61 * list is (potentially) unbounded. We handle the potential unboundedness by
62 * generating a list of n qualifiers with probability 1/2^(n+1). Each
63 * qualifier is chosen uniformly at random from the set of possibilities:
64 * const, volatile and, if restrictqual is true, restrict.
66 struct cdecl_declspec *gen_qualifiers(struct gen_rng *rng, bool restrictqual)
68 struct cdecl_declspec *s = NULL, *p;
70 while (gsl_rng_uniform(rng->rng) < 0.5) {
72 s = malloc_nofail(sizeof *s);
73 *s = (struct cdecl_declspec) {
77 switch (gsl_rng_uniform_int(rng->rng, 2+restrictqual)) {
79 s->type = CDECL_QUAL_CONST;
82 s->type = CDECL_QUAL_VOLATILE;
84 case 2: /* Only if restrictqual is true. */
85 s->type = CDECL_QUAL_RESTRICT;
96 * Generate random function specifiers. Like qualifiers, function specifiers
97 * can appear multiple times.
99 struct cdecl_declspec *gen_funcspecs(struct gen_rng *rng)
101 struct cdecl_declspec *s = NULL, *p;
103 while (gsl_rng_uniform(rng->rng) < 0.5) {
105 s = malloc_nofail(sizeof *s);
107 *s = (struct cdecl_declspec) {
108 .type = CDECL_FUNC_INLINE,
117 * Generate zero or one random storage-class specifier. If registeronly is
118 * true, then the only possible storage-class specifier is "register".
119 * Otherwise, a specifier type will be selected uniformly at random.
121 struct cdecl_declspec *gen_storspecs(struct gen_rng *rng, bool registeronly)
123 struct cdecl_declspec *s;
125 if (gsl_rng_uniform(rng->rng) < 0.5)
128 s = malloc_nofail(sizeof *s);
129 *s = (struct cdecl_declspec) {
130 .type = CDECL_STOR_REGISTER,
136 switch (gsl_rng_uniform_int(rng->rng, 5)) {
137 case 0: s->type = CDECL_STOR_TYPEDEF; break;
138 case 1: s->type = CDECL_STOR_REGISTER; break;
139 case 2: s->type = CDECL_STOR_STATIC; break;
140 case 3: s->type = CDECL_STOR_AUTO; break;
141 case 4: s->type = CDECL_STOR_EXTERN; break;
149 * Generate random type specifiers. There is a short list of valid
150 * combinations, from which we can select one uniformly at random.
152 static const unsigned long total_types;
153 static struct cdecl_declspec *gen_raw_typespecs(struct gen_rng *rng)
155 switch (gsl_rng_uniform_int(rng->rng, total_types)) {
156 # include "typegen.h"
159 static const unsigned long total_types = TOTAL_TYPES;
161 struct cdecl_declspec *gen_typespecs(struct gen_rng *rng, bool voidtype)
163 struct cdecl_declspec *specs;
166 specs = gen_raw_typespecs(rng);
168 switch (specs->type) {
169 /* void is not always valid, so we might need to pick again. */
170 case CDECL_TYPE_VOID:
174 /* A few kinds of type specifiers need identifiers to go with them. */
175 case CDECL_TYPE_STRUCT:
176 case CDECL_TYPE_UNION:
177 case CDECL_TYPE_ENUM:
178 case CDECL_TYPE_IDENT:
179 assert(!specs->next);
180 specs->ident = gen_identifier(rng);
183 /* Nothing to be done. */
190 struct cdecl_declspec *
191 gen_randomize_specs(struct gen_rng *rng, struct cdecl_declspec *specs)
193 struct cdecl_declspec **p;
199 for (struct cdecl_declspec *s = specs; s; s = s->next)
202 p = malloc_nofail((n+1) * sizeof *p);
204 /* Build a temporary array for easy element swapping. */
207 for (size_t i = 1; i < n; i++)
211 for (size_t i = 0; i < n; i++) {
212 struct cdecl_declspec *tmp;
215 r = gsl_rng_uniform_int(rng->rng, n-i);
221 /* Fix up the pointers. */
222 for (size_t i = 1; i <= n; i++)
231 * Generate random declaration specifiers. This consists of random qualifiers
232 * and type specifiers, as described in the functions above, plus up to one
233 * storage-class specifier and zero or more function specifiers.
235 * The flags parameter controls what sort of specifiers can be generated in
236 * order to handle various special cases in the C language. It is the
237 * bitwise-or of zero or more values, which have the following meanings:
239 * GEN_NO_FUNCTION: Do not generate any function specifiers at all.
240 * GEN_NO_STORAGE: Do not generate any storage-class specifiers at all.
241 * GEN_NO_VOID: Do not generate the "void" type specifier.
242 * GEN_ONLY_REGISTER: Never generate any storage-class specifiers other than
245 * Notwithstanding any of the above, the "restrict" type qualifier will never
246 * be generated by this function: use gen_qualifiers directly.
248 struct cdecl_declspec *
249 gen_declspecs(struct gen_rng *rng, unsigned flags)
251 struct cdecl_declspec *s, *p;
253 s = gen_typespecs(rng, ~flags & GEN_NO_VOID);
254 for (p = s; p->next;)
257 if (~flags & GEN_NO_FUNCTION)
258 p->next = gen_funcspecs(rng);
259 for (p = s; p->next;)
262 if (~flags & GEN_NO_STORAGE)
263 p->next = gen_storspecs(rng, flags & GEN_ONLY_REGISTER);
264 for (p = s; p->next;)
267 p->next = gen_qualifiers(rng, false);
268 return gen_randomize_specs(rng, s);
272 * Generate a random array declarator, selecting one of four possibilities
273 * uniformly at random.
275 static void gen_array(struct gen_rng *rng, struct cdecl_declarator *d)
277 d->type = CDECL_DECL_ARRAY;
278 d->u.array = (struct cdecl_array){0};
280 switch (gsl_rng_uniform_int(rng->rng, 4)) {
282 d->u.array.vla = malloc_nofail(1);
283 d->u.array.vla[0] = 0;
286 d->u.array.vla = gen_identifier(rng);
289 d->u.array.length = 0;
292 d->u.array.length = gsl_rng_uniform_int(rng->rng, -1);
299 /* Generate a function declarator. */
300 static void gen_function(struct gen_rng *rng, struct cdecl_declarator *d)
302 d->type = CDECL_DECL_FUNCTION;
303 d->u.function.parameters = NULL;
305 while (gsl_rng_uniform(rng->rng) < 0.5) {
306 unsigned flags = GEN_ONLY_REGISTER | GEN_NO_FUNCTION;
309 param = malloc_nofail(sizeof *param);
310 *param = (struct cdecl) {
311 .next = d->u.function.parameters,
312 .declarators = gen_declarators(rng),
315 if (param->declarators->type != CDECL_DECL_POINTER
316 && param->declarators->type != CDECL_DECL_FUNCTION)
317 flags |= GEN_NO_VOID;
319 param->specifiers = gen_declspecs(rng, flags);
320 d->u.function.parameters = param;
323 if (d->u.function.parameters) {
324 d->u.function.variadic = gsl_rng_uniform(rng->rng) < 0.5;
329 * Generate a random chain of declarators. Like qualifiers above, a chain of
330 * length N has probability 1/2^(N+1) of occurring. All declarator chains
331 * are ultimately terminated by either a null declarator or an identifier,
332 * selected uniformly at random.
334 struct cdecl_declarator *gen_declarators(struct gen_rng *rng)
336 struct cdecl_declarator *d, *p;
339 d = malloc_nofail(sizeof *d);
340 if (gsl_rng_uniform(rng->rng) < 0.5) {
341 *d = (struct cdecl_declarator) {
342 .type = CDECL_DECL_NULL,
345 *d = (struct cdecl_declarator) {
346 .type = CDECL_DECL_IDENT,
347 .u.ident = gen_identifier(rng),
351 while (gsl_rng_uniform(rng->rng) < 0.5) {
353 d = malloc_nofail(sizeof *d);
354 *d = (struct cdecl_declarator) {
358 switch (gsl_rng_uniform_int(rng->rng, limit)) {
360 d->type = CDECL_DECL_POINTER;
361 d->u.pointer = (struct cdecl_pointer) {
362 .qualifiers = gen_qualifiers(rng, true),
371 gen_function(rng, d);
382 struct gen_rng *gen_alloc_rng(const char *seed_str)
389 seed = strtoul(seed_str, &end, 0);
391 fprintf(stderr, "%s: invalid seed\n", seed_str);
393 } else if (errno != 0) {
394 fprintf(stderr, "%s: invalid seed: %s\n",
395 seed_str, strerror(errno));
399 rng = malloc_nofail(sizeof *rng);
400 rng->rng = gsl_rng_alloc(gsl_rng_mt19937);
401 gsl_rng_set(rng->rng, seed);
406 void gen_free_rng(struct gen_rng *rng)
409 gsl_rng_free(rng->rng);
413 static void gen_free_parameters(struct cdecl *x)
420 gen_free_declspecs(x->specifiers);
421 gen_free_declarators(x->declarators);
428 void gen_free_declspecs(struct cdecl_declspec *x)
430 struct cdecl_declspec *p;
440 void gen_free_declarators(struct cdecl_declarator *x)
442 struct cdecl_declarator *p;
448 case CDECL_DECL_NULL:
450 case CDECL_DECL_IDENT:
453 case CDECL_DECL_POINTER:
454 gen_free_declspecs(x->u.pointer.qualifiers);
456 case CDECL_DECL_ARRAY:
457 free(x->u.array.vla);
459 case CDECL_DECL_FUNCTION:
460 gen_free_parameters(x->u.function.parameters);