xref: /netbsd-src/tests/usr.bin/indent/lsym_lbrace.c (revision 95820b5370230cc0c1b63cb504d714d2aa7f7b4a)
1 /* $NetBSD: lsym_lbrace.c,v 1.10 2023/06/16 23:19:01 rillig Exp $ */
2 
3 /*
4  * Tests for the token lsym_lbrace, which represents a '{' in these contexts:
5  *
6  * In an initializer, '{' starts an inner group of initializers, usually to
7  * initialize a nested struct, union or array.
8  *
9  * In a function body, '{' starts a block.
10  *
11  * In an expression, '(type){' starts a compound literal that is typically
12  * used in an assignment to a struct or array.
13  *
14  * In macro arguments, a '{' is an ordinary character, it does not need to be
15  * balanced.  This is in contrast to '(', which must be balanced with ')'.
16  *
17  * TODO: try to split this token into lsym_lbrace_block and lsym_lbrace_init.
18  */
19 
20 /* Brace level in an initializer */
21 //indent input
22 void
function(void)23 function(void)
24 {
25 	struct person	p = {
26 		.name = "Name",
27 		.age = {{{35}}},	/* C11 6.7.9 allows this. */
28 	};
29 }
30 //indent end
31 
32 //indent run-equals-input
33 
34 
35 /* Begin of a block of statements */
36 //indent input
function(void)37 void function(void) {{{ body(); }}}
38 //indent end
39 
40 //indent run
41 void
function(void)42 function(void)
43 {
44 	{
45 		{
46 			body();
47 		}
48 	}
49 }
50 //indent end
51 
52 
53 /* Compound literal */
54 //indent input
55 struct point
origin(void)56 origin(void)
57 {
58 	return (struct point){
59 		.x = 0,
60 		.y = 0,
61 	}, actual_return_value;
62 }
63 //indent end
64 
65 //indent run-equals-input
66 
67 /* Ensure that the comma is not interpreted as separator for declarators. */
68 //indent run-equals-input -bc
69 
70 
71 //indent input
72 {
73 	const char *hello = (const char[]){
74 		'h', 'e', 'l', 'l', 'o',
75 	}, *world = (const char[]){
76 		'w', 'o', 'r', 'l', 'd',
77 	};
78 }
79 //indent end
80 
81 //indent run-equals-input -ldi0
82 
83 //indent run-equals-input -ldi0 -bc
84 
85 
86 //indent input
87 {
88 	if (cond rparen {
89 	}
90 	switch (expr rparen {
91 	}
92 }
93 //indent end
94 
95 //indent run
96 {
97 		if (cond rparen {
98 		}
99 		switch (expr rparen {
100 		}
101 }
102 // exit 1
103 // error: Standard Input:2: Unbalanced parentheses
104 // error: Standard Input:4: Unbalanced parentheses
105 //indent end
106 
107 
108 /*
109  * The -bl option does not force initializer braces on separate lines.
110  */
111 //indent input
112 struct {int member;} var = {1};
113 //indent end
114 
115 //indent run -bl
116 struct
117 {
118 	int		member;
119 }		var = {1};
120 //indent end
121 
122 
123 /*
124  * A comment in a single-line function definition is not a declaration comment
125  * and thus not in column 25.
126  */
127 //indent input
128 void function(void); /* comment */
129 void function(void) { /* comment */ }
130 //indent end
131 
132 //indent run -di0
133 void function(void);		/* comment */
134 void
135 function(void)
136 {				/* comment */
137 }
138 //indent end
139 
140 //indent run -di0 -nfbs
141 void function(void);		/* comment */
142 void
143 function(void) {		/* comment */
144 }
145 //indent end
146