xref: /netbsd-src/tests/usr.bin/indent/lsym_comma.c (revision ff059387eda55e4756cf72b2369fc0da8e0930c2)
1 /* $NetBSD: lsym_comma.c,v 1.6 2023/05/13 06:52:48 rillig Exp $ */
2 
3 /*
4  * Tests for the token lsym_comma, which represents a ',' in these contexts:
5  *
6  * In an expression, the binary operator ',' evaluates its left operand before
7  * its right operand, inserting a sequence point.
8  *
9  * In a declaration, a ',' separates the declarators.
10  *
11  * In a parameter list of a function type, a ',' separates the parameter
12  * declarations.
13  *
14  * In a traditional function definition, a ',' separates the parameter names.
15  *
16  * In a prototype function definition, a ',' separates the parameter
17  * declarations.
18  *
19  * In a function call expression, a ',' separates the arguments.
20  *
21  * In a macro definition, a ',' separates the parameter names.
22  *
23  * In a macro invocation, a ',' separates the arguments.
24  *
25  * In an initializer list, a ',' separates the initializer expressions.
26  */
27 
28 /*
29  * The ',' is a binary operator with very low precedence.
30  */
31 //indent input
32 int
comma_expression(void)33 comma_expression(void)
34 {
35 	return 1, 3;
36 	return a = b, c = d;
37 }
38 //indent end
39 
40 //indent run-equals-input
41 
42 
43 /*
44  * In a declaration, a ',' separates the declarators.
45  */
46 //indent input
47 int decl, old_style(), prototype(const char *, double *);
48 int a, b, c;
49 //indent end
50 
51 //indent run-equals-input -di0
52 
53 
54 /*
55  * In a parameter list of a function type, a ',' separates the parameter
56  * declarations.
57  */
58 //indent input
59 double dbl_reduce(double init, const double *s, const double *e, double (*merge)(double, double));
60 double dbl_reduce(double, const double *, const double *, double (*)(double, double));
61 void debug_printf(const char *, ...);
62 //indent end
63 
64 //indent run-equals-input -di0
65 
66 
67 /*
68  * In a traditional function definition, a ',' separates the parameter names.
69  */
70 //indent input
71 double
trad_dbl_reduce(init,s,e,merge)72 trad_dbl_reduce(init, s, e, merge)
73 	double init;
74 	double *s, *e;
75 	double (*merge)()
76 {
77 	double x = init;
78 	while (s < e)
79 		x = merge(x, *s++);
80 	return x;
81 }
82 //indent end
83 
84 //indent run-equals-input -di0
85 
86 
87 /*
88  * In a prototype function definition, a ',' separates the parameter
89  * declarations.
90  */
91 //indent input
92 void
dbl_reduce(double init,const double * s,const double * e,double (* merge)(double,double))93 dbl_reduce(double init, const double *s, const double *e, double (*merge)(double, double))
94 {
95 	double x = init;
96 	while (s < e)
97 		x = merge(x, *s++);
98 	return x;
99 }
100 //indent end
101 
102 //indent run-equals-input -di0
103 
104 
105 /*
106  * In a function call expression, a ',' separates the arguments.
107  */
108 //indent input
109 void
function(void)110 function(void)
111 {
112 	function_call(arg1, arg2);
113 	(*indirect_function_call)(arg1, arg2);
114 }
115 //indent end
116 
117 //indent run-equals-input -di0
118 
119 
120 /*
121  * In a macro definition, a ',' separates the parameter names.
122  */
123 //indent input
124 #define no_space(a,b) a ## b
125 #define normal_space(a, b) a ## b
126 #define wide_space(a  ,  b) a ## b
127 //indent end
128 
129 /*
130  * Indent does not touch preprocessor directives, except for the spacing
131  * between the '#' and the directive.
132  */
133 //indent run-equals-input
134 
135 
136 /*
137  * In a macro invocation, a ',' separates the arguments.
138  */
139 //indent input
140 void
function(void)141 function(void)
142 {
143 	macro_invocation(arg1, arg2);
144 	empty_arguments(,,,);
145 }
146 //indent end
147 
148 //indent run-equals-input -di0
149 
150 
151 /*
152  * In an initializer list, a ',' separates the initializer expressions.
153  */
154 //indent input
155 int arr[] = {1, 2, 3};
156 int arr[] = {
157 	1,
158 	2,
159 	3,			/* there may be a trailing comma */
160 };
161 //indent end
162 
163 //indent run-equals-input -di0
164 
165 
166 /*
167  * If a ',' starts a line, indent doesn't put a space before it. This style is
168  * uncommon and looks unbalanced since the '1' is not aligned to the other
169  * numbers.
170  */
171 //indent input
172 int arr[] = {
173 	1
174 	,2
175 	,3
176 };
177 //indent end
178 
179 //indent run-equals-input -di0
180