2 * Helper application to test normalization of declaration specifiers.
3 * Copyright © 2021-2023 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 <https://www.gnu.org/licenses/>.
26 #include "cdecl-internal.h"
29 #define PROGNAME "normalize"
30 static const char *progname = PROGNAME;
31 static const char sopts[] = "f:VH";
32 static const struct option lopts[] = {
33 { "file", 1, NULL, 'f' },
34 { "version", 0, NULL, 'V' },
35 { "help", 0, NULL, 'H' },
39 static void print_usage(FILE *f)
41 fprintf(f, "Usage: %s [options]\n", progname);
44 static void print_help(void)
46 const struct option *opt;
49 puts("Test normalization of declaration specifiers.\n");
50 test_print_options(lopts);
53 static unsigned to_spectype(const char *tok)
55 if (!strcmp(tok, "typedef")) return CDECL_STOR_TYPEDEF;
56 if (!strcmp(tok, "extern")) return CDECL_STOR_EXTERN;
57 if (!strcmp(tok, "static")) return CDECL_STOR_STATIC;
58 if (!strcmp(tok, "auto")) return CDECL_STOR_AUTO;
59 if (!strcmp(tok, "register")) return CDECL_STOR_REGISTER;
60 if (!strcmp(tok, "inline")) return CDECL_FUNC_INLINE;
61 if (!strcmp(tok, "restrict")) return CDECL_QUAL_RESTRICT;
62 if (!strcmp(tok, "volatile")) return CDECL_QUAL_VOLATILE;
63 if (!strcmp(tok, "const")) return CDECL_QUAL_CONST;
64 if (!strcmp(tok, "void")) return CDECL_TYPE_VOID;
65 if (!strcmp(tok, "signed")) return CDECL_TYPE_SIGNED;
66 if (!strcmp(tok, "unsigned")) return CDECL_TYPE_UNSIGNED;
67 if (!strcmp(tok, "char")) return CDECL_TYPE_CHAR;
68 if (!strcmp(tok, "short")) return CDECL_TYPE_SHORT;
69 if (!strcmp(tok, "long")) return CDECL_TYPE_LONG;
70 if (!strcmp(tok, "int")) return CDECL_TYPE_INT;
71 if (!strcmp(tok, "float")) return CDECL_TYPE_FLOAT;
72 if (!strcmp(tok, "double")) return CDECL_TYPE_DOUBLE;
73 if (!strcmp(tok, "_Complex")) return CDECL_TYPE_COMPLEX;
74 if (!strcmp(tok, "_Imaginary")) return CDECL_TYPE_IMAGINARY;
75 if (!strcmp(tok, "_Bool")) return CDECL_TYPE_BOOL;
76 if (!strcmp(tok, "struct")) return CDECL_TYPE_STRUCT;
77 if (!strcmp(tok, "union")) return CDECL_TYPE_UNION;
78 if (!strcmp(tok, "enum")) return CDECL_TYPE_ENUM;
80 return CDECL_TYPE_IDENT;
83 int do_normalize(char *line, size_t n)
85 struct cdecl_declspec *specs = NULL, **newspec = &specs;
88 while ((tok = strtok(tok ? NULL : line, " "))) {
89 struct cdecl_declspec *spec = malloc_nofail(sizeof *spec);
93 spec->type = to_spectype(tok);
95 case CDECL_TYPE_STRUCT:
96 case CDECL_TYPE_UNION:
98 spec->ident = strtok(NULL, " ");
100 case CDECL_TYPE_IDENT:
105 newspec = &spec->next;
108 line = malloc_nofail(n);
111 struct output_state dst = { line, n };
113 specs = cdecl__normalize_specs(specs);
114 cdecl__emit_specs(&dst, specs, -1);
116 printf("%s\n", line);
120 struct cdecl_declspec *c = specs;
128 int main(int argc, char **argv)
130 int opt, ret = EXIT_SUCCESS;
131 char *filename = NULL, *line = NULL;
132 FILE *infile = stdin;
136 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
142 test_print_version(PROGNAME);
154 infile = fopen(filename, "r");
161 while ((rc = getline(&line, &n, infile)) >= 0) {
162 if (rc > 0 && line[rc-1] == '\n')
164 if (do_normalize(line, n) < 0)