]> git.draconx.ca Git - cdecl99.git/blob - tests/decl-good.at
Release 1.3.
[cdecl99.git] / tests / decl-good.at
1 # Copyright © 2020-2021, 2023-2024 Nick Bowler
2 #
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.
7 #
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.
12 #
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/>.
15
16 AT_BANNER([C declarations])
17
18 # _SIMPLE_DECL_ARG([n], [list])
19 # _SIMPLE_DECL_ARG([n1 n2], [list])
20 #
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.
24 #
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)], [,]))
28 ])
29
30 # SIMPLE_DECLS_EXPLAIN([arg], [arg...])
31 #
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.
35 #
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)
40 #
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.])])
45 AT_DATA([test.dat],
46 [m4_map_args([explain m4_curry([_SIMPLE_DECL_ARG], [1])], $@)])
47 AT_DATA([expout],
48 [m4_map_args([m4_curry([_SIMPLE_DECL_ARG], [4 2])], $@)])
49 AT_CHECK([cdecl99 -f test.dat], [0], [expout])
50 AT_CLEANUP])
51
52 # SIMPLE_DECLS_DECLARE([arg], [arg ...])
53 #
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.
57 #
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)
62 #
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.])])
67 AT_DATA([test.dat],
68 [m4_map_args([m4_curry([_SIMPLE_DECL_ARG], [2])], $@)])
69 AT_DATA([expout],
70 [m4_map_args([m4_curry([_SIMPLE_DECL_ARG], [3 1])], $@)])
71 AT_CHECK([cdecl99 -f test.dat], [0], [expout])
72 AT_CLEANUP])
73
74 # SIMPLE_DECLS_SIMPLIFY([arg], [arg ...])
75 #
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.
80 #
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.])])
85 AT_DATA([test.dat],
86 [m4_map_args([simplify m4_curry([_SIMPLE_DECL_ARG], [1])], $@)])
87 AT_DATA([expout],
88 [m4_map_args([m4_curry([_SIMPLE_DECL_ARG], [2])], $@)])
89 AT_CHECK([cdecl99 -f test.dat], [0], [expout])
90 AT_CLEANUP])
91
92 # SIMPLE_DECLS(arg, [arg ...])
93 m4_define([SIMPLE_DECLS],
94 [SIMPLE_DECLS_EXPLAIN($@)
95 SIMPLE_DECLS_DECLARE($@)])
96
97 SIMPLE_DECLS(
98   [[int **],  [type pointer to pointer to int]],
99   [[int **x], [declare x as pointer to pointer to int]])
100
101 SIMPLE_DECLS(
102   [[int (*x)], [declare x as pointer to int], [int *x]],
103   [[int (*)],  [type pointer to int], [int *]])
104
105 SIMPLE_DECLS(
106   [[int (x*)], [type function (pointer to x) returning int], [int (x *)]])
107
108 SIMPLE_DECLS(
109   [[int (x)()], [declare x as function returning int], [int x()]])
110
111 # Check that function reduction does not occur in english parses
112 SIMPLE_DECLS(
113   [[int (x)], [type function (x) returning int], [], [declare x as int]])
114
115 SIMPLE_DECLS(
116   [[[int x[]]], [declare x as array of int]],
117   [[[int []]],  [type array of int]])
118
119 SIMPLE_DECLS(
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]])
124
125 SIMPLE_DECLS(
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]])
132
133 SIMPLE_DECLS(
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]])
140
141 SIMPLE_DECLS(
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]],
148   [[[int f(a (*)[])]],
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]],
152   [[[int f(a (*)())]],
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]],
160   [[[int (a (*)[])]],
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]],
164   [[[int (a (*)())]],
165     [type function (pointer to function returning a) returning int]])
166
167 SIMPLE_DECLS_EXPLAIN(
168   [[[int f(int ([]))]],
169     [declare f as function (array of int) returning int],
170     [[int f(int [])]]],
171   [[[int f(a ([]))]],
172     [declare f as function (array of a) returning int],
173     [[int f(a [])]]],
174   [[[int f(int (()))]],
175     [declare f as function (function returning int) returning int],
176     [[int f(int ())]]],
177   [[[int f(a (()))]],
178     [declare f as function (function returning a) returning int],
179     [[int f(a ())]]],
180   [[[int (int ([]))]],
181     [type function (array of int) returning int],
182     [[int (int [])]]],
183   [[[int (a ([]))]],
184     [type function (array of a) returning int],
185     [[int (a [])]]],
186   [[[int (int (()))]],
187     [type function (function returning int) returning int],
188     [[int (int ())]]],
189   [[[int (a (()))]],
190     [type function (function returning a) returning int],
191     [[int (a ())]]])
192
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 ()]])
197
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()]])
201
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]])
209
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]])
214
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]])
225
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]])