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/>.
32 #define PROGNAME "randomdecl"
33 static const char *progname = PROGNAME;
34 static const char sopts[] = "s:n:VH";
35 static const struct option lopts[] = {
36 { "seed", 1, NULL, 's' },
37 { "count", 1, NULL, 'n' },
38 { "version", 0, NULL, 'V' },
39 { "help", 0, NULL, 'H' },
43 static void print_usage(FILE *f)
45 fprintf(f, "Usage: %s [options]\n", progname);
48 static void print_help(void)
51 puts("Detailed help coming soon.");
54 static struct cdecl *random_decl(struct gen_rng *rng)
59 decl = malloc_nofail(sizeof *decl);
60 *decl = (struct cdecl) { 0 };
62 decl->declarators = gen_declarators(rng);
63 if (decl->declarators->type != CDECL_DECL_FUNCTION)
64 flags |= GEN_NO_FUNCTION;
65 if (cdecl_is_abstract(decl->declarators))
66 flags |= GEN_NO_STORAGE | GEN_NO_FUNCTION;
67 if (decl->declarators->type == CDECL_DECL_ARRAY
68 || decl->declarators->type == CDECL_DECL_IDENT)
71 decl->specifiers = gen_declspecs(rng, flags);
75 int main(int argc, char **argv)
77 const char *seed = "", *count_str = NULL;
80 unsigned long count = 0;
86 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
95 test_print_version(PROGNAME);
106 if (count_str && !strict_strtoul(&count, count_str, 0)) {
107 fprintf(stderr, "%s: invalid count: %s\n", progname, count_str);
111 rng = gen_alloc_rng(seed);
115 for (unsigned long i = 0; !count || i < count; i++) {
116 decl = random_decl(rng);
120 test_print_decl(decl);