2 * Helper application to test normalization of declaration specifiers.
3 * Copyright © 2021-2024 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/>.
27 #include "cdecl-internal.h"
29 #define PROGNAME "normalize"
32 static const char sopts[] = "f:VH";
33 static const struct option lopts[] = {
34 { "file", 1, NULL, 'f' },
35 { "version", 0, NULL, 'V' },
36 { "help", 0, NULL, 'H' },
40 static void print_usage(FILE *f)
42 fprintf(f, "Usage: %s [options]\n", progname);
45 static void print_help(void)
47 const struct option *opt;
50 puts("Test normalization of declaration specifiers.\n");
51 test_print_options(lopts);
54 static unsigned to_spectype(const char *tok)
56 if (!strcmp(tok, "typedef")) return CDECL_STOR_TYPEDEF;
57 if (!strcmp(tok, "extern")) return CDECL_STOR_EXTERN;
58 if (!strcmp(tok, "static")) return CDECL_STOR_STATIC;
59 if (!strcmp(tok, "auto")) return CDECL_STOR_AUTO;
60 if (!strcmp(tok, "register")) return CDECL_STOR_REGISTER;
61 if (!strcmp(tok, "inline")) return CDECL_FUNC_INLINE;
62 if (!strcmp(tok, "restrict")) return CDECL_QUAL_RESTRICT;
63 if (!strcmp(tok, "volatile")) return CDECL_QUAL_VOLATILE;
64 if (!strcmp(tok, "const")) return CDECL_QUAL_CONST;
65 if (!strcmp(tok, "void")) return CDECL_TYPE_VOID;
66 if (!strcmp(tok, "signed")) return CDECL_TYPE_SIGNED;
67 if (!strcmp(tok, "unsigned")) return CDECL_TYPE_UNSIGNED;
68 if (!strcmp(tok, "char")) return CDECL_TYPE_CHAR;
69 if (!strcmp(tok, "short")) return CDECL_TYPE_SHORT;
70 if (!strcmp(tok, "long")) return CDECL_TYPE_LONG;
71 if (!strcmp(tok, "int")) return CDECL_TYPE_INT;
72 if (!strcmp(tok, "float")) return CDECL_TYPE_FLOAT;
73 if (!strcmp(tok, "double")) return CDECL_TYPE_DOUBLE;
74 if (!strcmp(tok, "_Complex")) return CDECL_TYPE_COMPLEX;
75 if (!strcmp(tok, "_Imaginary")) return CDECL_TYPE_IMAGINARY;
76 if (!strcmp(tok, "_Bool")) return CDECL_TYPE_BOOL;
77 if (!strcmp(tok, "struct")) return CDECL_TYPE_STRUCT;
78 if (!strcmp(tok, "union")) return CDECL_TYPE_UNION;
79 if (!strcmp(tok, "enum")) return CDECL_TYPE_ENUM;
81 return CDECL_TYPE_IDENT;
84 static const char *to_specstring(unsigned type)
87 case CDECL_STOR_TYPEDEF: return "typedef";
88 case CDECL_STOR_EXTERN: return "extern";
89 case CDECL_STOR_STATIC: return "static";
90 case CDECL_STOR_AUTO: return "auto";
91 case CDECL_STOR_REGISTER: return "register";
92 case CDECL_FUNC_INLINE: return "inline";
93 case CDECL_QUAL_RESTRICT: return "restrict";
94 case CDECL_QUAL_VOLATILE: return "volatile";
95 case CDECL_QUAL_CONST: return "const";
96 case CDECL_TYPE_VOID: return "void";
97 case CDECL_TYPE_SIGNED: return "signed";
98 case CDECL_TYPE_UNSIGNED: return "unsigned";
99 case CDECL_TYPE_CHAR: return "char";
100 case CDECL_TYPE_SHORT: return "short";
101 case CDECL_TYPE_LONG: return "long";
102 case CDECL_TYPE_INT: return "int";
103 case CDECL_TYPE_FLOAT: return "float";
104 case CDECL_TYPE_DOUBLE: return "double";
105 case CDECL_TYPE_COMPLEX: return "_Complex";
106 case CDECL_TYPE_IMAGINARY: return "_Imaginary";
107 case CDECL_TYPE_BOOL: return "_Bool";
108 case CDECL_TYPE_STRUCT: return "struct";
109 case CDECL_TYPE_UNION: return "union";
110 case CDECL_TYPE_ENUM: return "enum";
116 static void print_spec(struct cdecl_declspec *spec)
118 const char *s = to_specstring(spec->type);
124 printf("%s", spec->ident);
128 int do_normalize(char *line, size_t n)
130 struct cdecl_declspec *s, *specs = NULL, **newspec = &specs;
133 while ((tok = strtok(tok ? NULL : line, " "))) {
134 struct cdecl_declspec *spec = malloc_nofail(sizeof *spec);
138 spec->type = to_spectype(tok);
139 switch (spec->type) {
140 case CDECL_TYPE_STRUCT:
141 case CDECL_TYPE_UNION:
142 case CDECL_TYPE_ENUM:
143 spec->ident = strtok(NULL, " ");
145 case CDECL_TYPE_IDENT:
150 newspec = &spec->next;
153 specs = cdecl__normalize_specs(specs);
154 for (s = specs; s; s = s->next) {
162 struct cdecl_declspec *c = specs;
170 int main(int argc, char **argv)
172 int opt, ret = EXIT_SUCCESS;
173 char *filename = NULL, *line = NULL;
174 FILE *infile = stdin;
178 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
184 test_print_version(PROGNAME);
195 if (filename && !freopen(filename, "r", stdin)) {
196 print_error("%s: %s", filename, strerror(errno));
200 while (test_getline(&line, &n)) {
201 if (do_normalize(line, n) < 0)