]> git.draconx.ca Git - cdecl99.git/blob - test/normalize.c
Hand-code the normalized specifier ordering.
[cdecl99.git] / test / normalize.c
1 /*
2  * Helper application to test normalization of declaration specifiers.
3  * Copyright © 2021 Nick Bowler
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <getopt.h>
24
25 #include "cdecl.h"
26 #include "cdecl-internal.h"
27 #include "test.h"
28
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' },
36         { 0 }
37 };
38
39 static void print_usage(FILE *f)
40 {
41         fprintf(f, "Usage: %s [options]\n", progname);
42 }
43
44 static void print_help(void)
45 {
46         const struct option *opt;
47
48         print_usage(stdout);
49         puts("Test normalization of declaration specifiers.\n");
50
51         puts("Options:");
52         for (opt = lopts; opt->val; opt++) {
53                 int w = print_option_start(opt, NULL);
54
55                 if (w)
56                         putchar('\n');
57         }
58 }
59
60 static unsigned to_spectype(const char *tok)
61 {
62         if (!strcmp(tok, "typedef")) return CDECL_STOR_TYPEDEF;
63         if (!strcmp(tok, "extern")) return CDECL_STOR_EXTERN;
64         if (!strcmp(tok, "static")) return CDECL_STOR_STATIC;
65         if (!strcmp(tok, "auto")) return CDECL_STOR_AUTO;
66         if (!strcmp(tok, "register")) return CDECL_STOR_REGISTER;
67         if (!strcmp(tok, "inline")) return CDECL_FUNC_INLINE;
68         if (!strcmp(tok, "restrict")) return CDECL_QUAL_RESTRICT;
69         if (!strcmp(tok, "volatile")) return CDECL_QUAL_VOLATILE;
70         if (!strcmp(tok, "const")) return CDECL_QUAL_CONST;
71         if (!strcmp(tok, "void")) return CDECL_TYPE_VOID;
72         if (!strcmp(tok, "signed")) return CDECL_TYPE_SIGNED;
73         if (!strcmp(tok, "unsigned")) return CDECL_TYPE_UNSIGNED;
74         if (!strcmp(tok, "char")) return CDECL_TYPE_CHAR;
75         if (!strcmp(tok, "short")) return CDECL_TYPE_SHORT;
76         if (!strcmp(tok, "long")) return CDECL_TYPE_LONG;
77         if (!strcmp(tok, "int")) return CDECL_TYPE_INT;
78         if (!strcmp(tok, "float")) return CDECL_TYPE_FLOAT;
79         if (!strcmp(tok, "double")) return CDECL_TYPE_DOUBLE;
80         if (!strcmp(tok, "_Complex")) return CDECL_TYPE_COMPLEX;
81         if (!strcmp(tok, "_Imaginary")) return CDECL_TYPE_IMAGINARY;
82         if (!strcmp(tok, "_Bool")) return CDECL_TYPE_BOOL;
83         if (!strcmp(tok, "struct")) return CDECL_TYPE_STRUCT;
84         if (!strcmp(tok, "union")) return CDECL_TYPE_UNION;
85         if (!strcmp(tok, "enum")) return CDECL_TYPE_ENUM;
86
87         return CDECL_TYPE_IDENT;
88 }
89
90 int do_normalize(char *line, size_t n)
91 {
92         struct cdecl_declspec *specs = NULL, **newspec = &specs;
93         char *tok = NULL;
94
95         while ((tok = strtok(tok ? NULL : line, " "))) {
96                 struct cdecl_declspec *spec = malloc_nofail(sizeof *spec);
97
98                 spec->next = NULL;
99                 spec->ident = NULL;
100                 spec->type = to_spectype(tok);
101                 switch (spec->type) {
102                 case CDECL_TYPE_STRUCT:
103                 case CDECL_TYPE_UNION:
104                 case CDECL_TYPE_ENUM:
105                         spec->ident = strtok(NULL, " ");
106                         break;
107                 case CDECL_TYPE_IDENT:
108                         spec->ident = tok;
109                 }
110
111                 *newspec = spec;
112                 newspec = &spec->next;
113         }
114
115         line = malloc_nofail(n);
116         line[0] = 0;
117         if (specs) {
118                 specs = cdecl__normalize_specs(specs);
119                 cdecl__explain_specs(line, n, specs, -1);
120         }
121         printf("%s\n", line);
122         free(line);
123
124         while (specs) {
125                 struct cdecl_declspec *c = specs;
126                 specs = specs->next;
127                 free(c);
128         }
129
130         return 0;
131 }
132
133 int main(int argc, char **argv)
134 {
135         int opt, ret = EXIT_SUCCESS;
136         char *filename = NULL, *line = NULL;
137         FILE *infile = stdin;
138         size_t n = 0;
139         ssize_t rc;
140
141         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
142                 switch (opt) {
143                 case 'f':
144                         filename = optarg;
145                         break;
146                 case 'V':
147                         test_print_version(PROGNAME);
148                         return EXIT_SUCCESS;
149                 case 'H':
150                         print_help();
151                         return EXIT_SUCCESS;
152                 default:
153                         print_usage(stderr);
154                         return EXIT_FAILURE;
155                 }
156         }
157
158         if (filename) {
159                 infile = fopen(filename, "r");
160                 if (!infile) {
161                         perror(filename);
162                         return EXIT_FAILURE;
163                 }
164         }
165
166         while ((rc = getline(&line, &n, infile)) >= 0) {
167                 if (rc > 0 && line[rc-1] == '\n')
168                         line[rc-1] = 0;
169                 if (do_normalize(line, n) < 0)
170                         ret = EXIT_FAILURE;
171         }
172
173         free(line);
174         return ret;
175 }