1 # Copyright © 2020-2021, 2023-2024 Nick Bowler
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 AT_BANNER([C declarations])
18 # _SIMPLE_DECL_ARG([n], [list])
19 # _SIMPLE_DECL_ARG([n1 n2], [list])
21 # Select one element (indexed from 1) from the given quoted list of quoted
22 # elements, either one or two elements may be indicated; in the two index form,
23 # element n1 is selected unless it is blank, in which case element n2 is used.
25 # The expansion of this macro is followed by a newline.
26 m4_define([_SIMPLE_DECL_ARG],
27 [m4_default(m4_map_args_w([$1], [m4_argn(], [, $2)], [,]))
30 # SIMPLE_DECLS_EXPLAIN([arg], [arg...])
32 # Generate tests to validate correct parsing and output of each specification
33 # argument. The arguments are quoted list of quoted items, with the first
34 # item of each list being the C declaration or type name to be explained.
36 # The expected output is the fourth item of the list, or, if that is empty,
37 # the second item in the list (this allows the same lists to be used for
38 # both SIMPLE_DECLS_DECLARE and SIMPLE_DECLS_EXPLAIN even in case of
39 # various equivalent syntactic forms)
41 # The items should be related to each other as the user-visible test group
42 # name is determined only by the first argument.
43 m4_define([SIMPLE_DECLS_EXPLAIN],
44 [AT_SETUP([Explain "]m4_car($1)["m4_ifnblank([$2], [ etc.])])
46 [m4_map_args([explain m4_curry([_SIMPLE_DECL_ARG], [1])], $@)])
48 [m4_map_args([m4_curry([_SIMPLE_DECL_ARG], [4 2])], $@)])
49 AT_CHECK([cdecl99 -f test.dat], [0], [expout])
52 # SIMPLE_DECLS_DECLARE([arg], [arg ...])
54 # Similar to SIMPLE_DECLS_EXPLAIN, except in the opposite direction. The
55 # arguments are quoted lists of quoted items, with the second item of each
56 # list being the input query.
58 # The expected output is the third item of the list, or, if that is empty,
59 # the first item in the list (this allows the same lists to be used for
60 # both SIMPLE_DECLS_DECLARE and SIMPLE_DECLS_EXPLAIN even in the case
61 # of various equivalent syntactic forsm)
63 # The items should be related to each other as the user-visible test group
64 # name is determined only by the first argument.
65 m4_define([SIMPLE_DECLS_DECLARE],
66 [AT_SETUP([Declare "]m4_car($1)["m4_ifnblank([$2], [ etc.])])
68 [m4_map_args([m4_curry([_SIMPLE_DECL_ARG], [2])], $@)])
70 [m4_map_args([m4_curry([_SIMPLE_DECL_ARG], [3 1])], $@)])
71 AT_CHECK([cdecl99 -f test.dat], [0], [expout])
74 # SIMPLE_DECLS_SIMPLIFY([arg], [arg ...])
76 # Generate tests to validate the expected operation of the "simplify"
77 # command. The arguments are a quoted list of quoted items, with the
78 # first item of each list being the input declaration and the second
79 # item being the expected simplified output.
81 # The items should be related to each other as the user-visible test
82 # group name is determined only by the first argument.
83 m4_define([SIMPLE_DECLS_SIMPLIFY],
84 [AT_SETUP([Simplify "]m4_car($1)["m4_ifnblank([$2], [ etc.])])
86 [m4_map_args([simplify m4_curry([_SIMPLE_DECL_ARG], [1])], $@)])
88 [m4_map_args([m4_curry([_SIMPLE_DECL_ARG], [2])], $@)])
89 AT_CHECK([cdecl99 -f test.dat], [0], [expout])
92 # SIMPLE_DECLS(arg, [arg ...])
93 m4_define([SIMPLE_DECLS],
94 [SIMPLE_DECLS_EXPLAIN($@)
95 SIMPLE_DECLS_DECLARE($@)])
98 [[int **], [type pointer to pointer to int]],
99 [[int **x], [declare x as pointer to pointer to int]])
102 [[int (*x)], [declare x as pointer to int], [int *x]],
103 [[int (*)], [type pointer to int], [int *]])
106 [[int (x*)], [type function (pointer to x) returning int], [int (x *)]])
109 [[int (x)()], [declare x as function returning int], [int x()]])
111 # Check that function reduction does not occur in english parses
113 [[int (x)], [type function (x) returning int], [], [declare x as int]])
116 [[[int x[]]], [declare x as array of int]],
117 [[[int []]], [type array of int]])
120 [[[int x[n]]], [declare x as variable-length array n of int]],
121 [[[int x[*]]], [declare x as variable-length array of int]],
122 [[[int [n]]], [type variable-length array n of int]],
123 [[[int [*]]], [type variable-length array of int]])
126 [[int f(a, b)], [declare f as function (a, b) returning int]],
127 [[int f(int, b)], [declare f as function (int, b) returning int]],
128 [[int f(a, int)], [declare f as function (a, int) returning int]],
129 [[int (a, b)], [type function (a, b) returning int]],
130 [[int (int, b)], [type function (int, b) returning int]],
131 [[int (a, int)], [type function (a, int) returning int]])
134 [[int f(a, b, ...)], [declare f as function (a, b, ...) returning int]],
135 [[int f(int, b, ...)], [declare f as function (int, b, ...) returning int]],
136 [[int f(a, int, ...)], [declare f as function (a, int, ...) returning int]],
137 [[int (a, b, ...)], [type function (a, b, ...) returning int]],
138 [[int (int, b, ...)], [type function (int, b, ...) returning int]],
139 [[int (a, int, ...)], [type function (a, int, ...) returning int]])
142 [[[int f(int (*)[])]],
143 [declare f as function (pointer to array of int) returning int]],
144 [[[int f(int (*)[][1])]],
145 [declare f as function (pointer to array of array 1 of int) returning int]],
146 [[[int f(int (*)())]],
147 [declare f as function (pointer to function returning int) returning int]],
149 [declare f as function (pointer to array of a) returning int]],
150 [[[int f(a (*)[][1])]],
151 [declare f as function (pointer to array of array 1 of a) returning int]],
153 [declare f as function (pointer to function returning a) returning int]],
154 [[[int (int (*)[])]],
155 [type function (pointer to array of int) returning int]],
156 [[[int (int (*)[][1])]],
157 [type function (pointer to array of array 1 of int) returning int]],
158 [[[int (int (*)())]],
159 [type function (pointer to function returning int) returning int]],
161 [type function (pointer to array of a) returning int]],
162 [[[int (a (*)[][1])]],
163 [type function (pointer to array of array 1 of a) returning int]],
165 [type function (pointer to function returning a) returning int]])
167 SIMPLE_DECLS_EXPLAIN(
168 [[[int f(int ([]))]],
169 [declare f as function (array of int) returning int],
172 [declare f as function (array of a) returning int],
174 [[[int f(int (()))]],
175 [declare f as function (function returning int) returning int],
178 [declare f as function (function returning a) returning int],
181 [type function (array of int) returning int],
184 [type function (array of a) returning int],
187 [type function (function returning int) returning int],
190 [type function (function returning a) returning int],
193 SIMPLE_DECLS_EXPLAIN(
194 [[int ((int))], [type function (int) returning int], [int (int)]],
195 [[int (x(int))], [declare x as function (int) returning int], [int x(int)]],
196 [[int (())], [type function returning int], [int ()]])
198 SIMPLE_DECLS_EXPLAIN(
199 [[int (x())], [declare x as function returning int], [int x()]],
200 [[int ((x)())], [declare x as function returning int], [int x()]])
202 # Test that english-only keywords are not rejected as idenfitiers in C mode.
203 SIMPLE_DECLS_EXPLAIN(
204 [[[of array[]]], [declare array as array of of]],
205 [[[returning as(function)]],
206 [declare as as function (function) returning returning]],
207 [[[pointer *declare]], [declare declare as pointer to pointer]],
208 [[[type to]], [declare to as type]])
210 # Test the explain command with multiple declarators, which produces
211 # multiple lines of English output.
212 SIMPLE_DECLS_EXPLAIN(
213 [[int x, y], [m4_n([declare x as int])declare y as int]])
215 # Check output spacing around qualified pointers
216 SIMPLE_DECLS_DECLARE(
217 [[[int (* const)[]]], [type const pointer to array of int]],
218 [[[int (* volatile const)()]],
219 [type volatile const pointer to function returning int]],
220 [[[int (int * restrict const)]],
221 [type function (restrict const pointer to int) returning int]],
222 [[[int * const x]], [declare x as const pointer to int]],
223 [[[int * volatile * restrict x]],
224 [declare x as restrict pointer to volatile pointer to int]])
226 # Test the simplify command with multiple declarators. This is the only
227 # command in cdecl99 which will print a single C declaration with more than
228 # one full declarator.
229 SIMPLE_DECLS_SIMPLIFY(
230 [[int x, y], [int x, y]],
231 [[int (x), ((y))], [int x, y]])