]> git.draconx.ca Git - cdecl99.git/blob - tests/decl-good.at
libcdecl: Fix output regression with multiple declarators.
[cdecl99.git] / tests / decl-good.at
1 # Copyright © 2020-2021, 2023 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 dnl _SIMPLE_DECL_ARG([n1 n2], [arg1], ..., [argn])
19 dnl
20 dnl Similar to m4_argn, except the first argument has (up to) two argument
21 dnl numbers, and the first nonempty matching argument is selected.
22 dnl
23 dnl The expansion is followed by a newline.
24 m4_define([_SIMPLE_DECL_ARG],
25 [m4_default(m4_map_args_w([$1], [m4_argn(], [, $2)], [,]))
26 ])
27
28 dnl SIMPLE_DECLS_EXPLAIN([arg], [arg...])
29 dnl
30 dnl Generate tests to validate correct parsing and output of each specification
31 dnl argument.  The arguments are quoted list of quoted items, with the first
32 dnl item of each list being the C declaration or type name to be explained.
33 dnl
34 dnl The expected output is the fourth item of the list, or, if that is empty,
35 dnl the second item in the list (this allows the same lists to be used for
36 dnl both SIMPLE_DECLS_DECLARE and SIMPLE_DECLS_EXPLAIN even in case of
37 dnl various equivalent syntactic forms)
38 dnl
39 dnl The items should be related to each other as the user-visible test group
40 dnl name is determined only by the first argument.
41 m4_define([SIMPLE_DECLS_EXPLAIN],
42 [AT_SETUP([Explain "m4_car($1)"m4_ifnblank([$2], [ etc.])])
43 AT_DATA([test.dat],
44 [m4_map_args([explain m4_curry([_SIMPLE_DECL_ARG], [1])], $@)])
45 AT_DATA([expout],
46 [m4_map_args([m4_curry([_SIMPLE_DECL_ARG], [4 2])], $@)])
47 AT_CHECK([cdecl99 -f test.dat], [0], [expout])
48 AT_CLEANUP])
49
50 dnl SIMPLE_DECLS_DECLARE([arg], [arg ...])
51 dnl
52 dnl Similar to SIMPLE_DECLS_EXPLAIN, except in the opposite direction.  The
53 dnl arguments are quoted lists of quoted items, with the second item of each
54 dnl list being the input query.
55 dnl
56 dnl The expected output is the third item of the list, or, if that is empty,
57 dnl the first item in the list (this allows the same lists to be used for
58 dnl both SIMPLE_DECLS_DECLARE and SIMPLE_DECLS_EXPLAIN even in the case
59 dnl of various equivalent syntactic forsm)
60 dnl
61 dnl The items should be related to each other as the user-visible test group
62 dnl name is determined only by the first argument.
63 m4_define([SIMPLE_DECLS_DECLARE],
64 [AT_SETUP([Declare "m4_car($1)"m4_ifnblank([$2], [ etc.])])
65 AT_DATA([test.dat],
66 [m4_map_args([m4_curry([_SIMPLE_DECL_ARG], [2])], $@)])
67 AT_DATA([expout],
68 [m4_map_args([m4_curry([_SIMPLE_DECL_ARG], [3 1])], $@)])
69 AT_CHECK([cdecl99 -f test.dat], [0], [expout])
70 AT_CLEANUP])
71
72 dnl SIMPLE_DECLS_SIMPLIFY([arg], [arg ...])
73 dnl
74 dnl Generate tests to validate the expected operation of the "simplify"
75 dnl command.  The arguments are a quoted list of quoted items, with the
76 dnl first item of each list being the input declaration and the second
77 dnl item being the expected simplified output.
78 dnl
79 dnl The items should be related to each other as the user-visible test
80 dnl group name is determined only by the first argument.
81 m4_define([SIMPLE_DECLS_SIMPLIFY],
82 [AT_SETUP([Simplify "m4_car($1)"m4_ifnblank([$2], [ etc.])])
83 AT_DATA([test.dat],
84 [m4_map_args([simplify m4_curry([_SIMPLE_DECL_ARG], [1])], $@)])
85 AT_DATA([expout],
86 [m4_map_args([m4_curry([_SIMPLE_DECL_ARG], [2])], $@)])
87 AT_CHECK([cdecl99 -f test.dat], [0], [expout])
88 AT_CLEANUP])
89
90 dnl SIMPLE_DECLS(arg, [arg ...])
91 m4_define([SIMPLE_DECLS],
92 [SIMPLE_DECLS_EXPLAIN($@)
93 SIMPLE_DECLS_DECLARE($@)])
94
95 SIMPLE_DECLS(
96   [[int **],  [type pointer to pointer to int]],
97   [[int **x], [declare x as pointer to pointer to int]])
98
99 SIMPLE_DECLS(
100   [[int (*x)], [declare x as pointer to int], [int *x]],
101   [[int (*)],  [type pointer to int], [int *]])
102
103 SIMPLE_DECLS(
104   [[int (x*)], [type function (pointer to x) returning int], [int (x *)]])
105
106 SIMPLE_DECLS(
107   [[int (x)()], [declare x as function returning int], [int x()]])
108
109 dnl Check that function reduction does not occur in english parses
110 SIMPLE_DECLS(
111   [[int (x)], [type function (x) returning int], [], [declare x as int]])
112
113 SIMPLE_DECLS(
114   [[int x@<:@@:>@], [declare x as array of int]],
115   [[int @<:@@:>@],  [type array of int]])
116
117 SIMPLE_DECLS(
118   [[int x@<:@n@:>@], [declare x as variable-length array n of int]],
119   [[int x@<:@*@:>@], [declare x as variable-length array of int]],
120   [[int @<:@n@:>@],  [type variable-length array n of int]],
121   [[int @<:@*@:>@],  [type variable-length array of int]])
122
123 SIMPLE_DECLS_EXPLAIN(
124   [[int ((int))], [type function (int) returning int], [int (int)]],
125   [[int (x(int))], [declare x as function (int) returning int], [int x(int)]],
126   [[int (())], [type function returning int], [int ()]])
127
128 SIMPLE_DECLS_EXPLAIN(
129   [[int (x())], [declare x as function returning int], [int x()]],
130   [[int ((x)())], [declare x as function returning int], [int x()]])
131
132 dnl Test that english-only keywords are not rejected as idenfitiers in C mode.
133 SIMPLE_DECLS_EXPLAIN(
134   [[of array@<:@@:>@], [declare array as array of of]],
135   [[returning as(function)],
136     [declare as as function (function) returning returning]],
137   [[pointer *declare], [declare declare as pointer to pointer]],
138   [[type to], [declare to as type]])
139
140 dnl Test the explain command with multiple declarators, which produces
141 dnl multiple lines of English output.
142 SIMPLE_DECLS_EXPLAIN(
143   [[int x, y], [m4_n([declare x as int])declare y as int]])
144
145 dnl Test the simplify command with multiple declarators.  This is the only
146 dnl command in cdecl99 which will print a single C declaration with more than
147 dnl one full declarator.
148 SIMPLE_DECLS_SIMPLIFY(
149   [[int x, y], [int x, y]],
150   [[int (x), ((y))], [int x, y]])