]> git.draconx.ca Git - cdecl99.git/blob - test/normalize.c
Use common help formatting in test cases.
[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         test_print_options(lopts);
51 }
52
53 static unsigned to_spectype(const char *tok)
54 {
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;
79
80         return CDECL_TYPE_IDENT;
81 }
82
83 int do_normalize(char *line, size_t n)
84 {
85         struct cdecl_declspec *specs = NULL, **newspec = &specs;
86         char *tok = NULL;
87
88         while ((tok = strtok(tok ? NULL : line, " "))) {
89                 struct cdecl_declspec *spec = malloc_nofail(sizeof *spec);
90
91                 spec->next = NULL;
92                 spec->ident = NULL;
93                 spec->type = to_spectype(tok);
94                 switch (spec->type) {
95                 case CDECL_TYPE_STRUCT:
96                 case CDECL_TYPE_UNION:
97                 case CDECL_TYPE_ENUM:
98                         spec->ident = strtok(NULL, " ");
99                         break;
100                 case CDECL_TYPE_IDENT:
101                         spec->ident = tok;
102                 }
103
104                 *newspec = spec;
105                 newspec = &spec->next;
106         }
107
108         line = malloc_nofail(n);
109         line[0] = 0;
110         if (specs) {
111                 specs = cdecl__normalize_specs(specs);
112                 cdecl__explain_specs(line, n, specs, -1);
113         }
114         printf("%s\n", line);
115         free(line);
116
117         while (specs) {
118                 struct cdecl_declspec *c = specs;
119                 specs = specs->next;
120                 free(c);
121         }
122
123         return 0;
124 }
125
126 int main(int argc, char **argv)
127 {
128         int opt, ret = EXIT_SUCCESS;
129         char *filename = NULL, *line = NULL;
130         FILE *infile = stdin;
131         size_t n = 0;
132         ssize_t rc;
133
134         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
135                 switch (opt) {
136                 case 'f':
137                         filename = optarg;
138                         break;
139                 case 'V':
140                         test_print_version(PROGNAME);
141                         return EXIT_SUCCESS;
142                 case 'H':
143                         print_help();
144                         return EXIT_SUCCESS;
145                 default:
146                         print_usage(stderr);
147                         return EXIT_FAILURE;
148                 }
149         }
150
151         if (filename) {
152                 infile = fopen(filename, "r");
153                 if (!infile) {
154                         perror(filename);
155                         return EXIT_FAILURE;
156                 }
157         }
158
159         while ((rc = getline(&line, &n, infile)) >= 0) {
160                 if (rc > 0 && line[rc-1] == '\n')
161                         line[rc-1] = 0;
162                 if (do_normalize(line, n) < 0)
163                         ret = EXIT_FAILURE;
164         }
165
166         free(line);
167         return ret;
168 }