Fri Jun 9 06:36:58 2023 UTC ()
indent: indent multi-line expressions according to parentheses

This reverts the FreeBSD change from 2004-02-12 that had been imported
on 2019-04-04.


(rillig)
diff -r1.9 -r1.10 src/tests/usr.bin/indent/opt_ci.c
diff -r1.9 -r1.10 src/tests/usr.bin/indent/opt_lp.c
diff -r1.32 -r1.33 src/usr.bin/indent/indent.1
diff -r1.204 -r1.205 src/usr.bin/indent/io.c

cvs diff -r1.9 -r1.10 src/tests/usr.bin/indent/opt_ci.c (expand / switch to unified diff)

--- src/tests/usr.bin/indent/opt_ci.c 2023/05/18 05:33:27 1.9
+++ src/tests/usr.bin/indent/opt_ci.c 2023/06/09 06:36:58 1.10
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: opt_ci.c,v 1.9 2023/05/18 05:33:27 rillig Exp $ */ 1/* $NetBSD: opt_ci.c,v 1.10 2023/06/09 06:36:58 rillig Exp $ */
2 2
3/* 3/*
4 * Tests for the option '-ci', which controls the indentation of continuation 4 * Tests for the option '-ci', which controls the indentation of continuation
5 * lines in statements and declarations, but only inside a function. 5 * lines in statements and declarations, but only inside a function.
6 */ 6 */
7 7
8/* 8/*
9 * Top level expressions with and without parentheses. 9 * Top level expressions with and without parentheses.
10 */ 10 */
11//indent input 11//indent input
12int top_level = 1 + 12int top_level = 1 +
13 2; 13 2;
14int top_level = (1 + 14int top_level = (1 +
@@ -37,37 +37,38 @@ int top_level = (1 + @@ -37,37 +37,38 @@ int top_level = (1 +
37 2 + ( 37 2 + (
38 3)); 38 3));
39//indent end 39//indent end
40 40
41//indent run -ci2 -nlp 41//indent run -ci2 -nlp
42int top_level = 1 + 42int top_level = 1 +
432; 432;
44int top_level = (1 + 44int top_level = (1 +
45 2 + ( 45 2 + (
46 3)); 46 3));
47//indent end 47//indent end
48 48
49/* 49/*
50 * Since '-ci4' is half an indentation level, indent all continuations using 50 * Between 2019-04-04 and 2023-06-09, there was a special rule that prevented
51 * the same level, no matter how many parentheses there are. The rationale for 51 * indentation based on the number of open parentheses, in the case that the
52 * this may have been to prevent that the continuation line has the same 52 * continuation indentation is half an indentation level, maybe to prevent that
53 * indentation as a follow-up statement, such as in 'if' statements. 53 * the continuation line has the same indentation as a follow-up statement,
 54 * such as in 'if' statements. To prevent such ambiguities, see '-eei'.
54 */ 55 */
55//indent run -ci4 -nlp 56//indent run -ci4 -nlp
56int top_level = 1 + 57int top_level = 1 +
572; 582;
58int top_level = (1 + 59int top_level = (1 +
59 2 + ( 60 2 + (
60 3)); 61 3));
61//indent end 62//indent end
62 63
63 64
64/* 65/*
65 * Declarations in functions without parentheses. 66 * Declarations in functions without parentheses.
66 */ 67 */
67//indent input 68//indent input
68int 69int
69sum(int a, int b) 70sum(int a, int b)
70{ 71{
71 return a + 72 return a +
72 b; 73 b;
73 return first + 74 return first +
@@ -155,41 +156,35 @@ sum(int a, int b) @@ -155,41 +156,35 @@ sum(int a, int b)
155 156
156//indent run -ci2 -nlp 157//indent run -ci2 -nlp
157int 158int
158sum(int a, int b) 159sum(int a, int b)
159{ 160{
160 return (a + 161 return (a +
161 b); 162 b);
162 return (first + 163 return (first +
163 second + ( 164 second + (
164 third)); 165 third));
165} 166}
166//indent end 167//indent end
167 168
168/* 
169 * Since '-ci4' is half an indentation level, indent all continuations using 
170 * the same level, no matter how many parentheses there are. The rationale for 
171 * this may have been to prevent that the continuation line has the same 
172 * indentation as a follow-up statement, such as in 'if' statements. 
173 */ 
174//indent run -ci4 -nlp 169//indent run -ci4 -nlp
175int 170int
176sum(int a, int b) 171sum(int a, int b)
177{ 172{
178 return (a + 173 return (a +
179 b); 174 b);
180 return (first + 175 return (first +
181 second + ( 176 second + (
182 third)); 177 third));
183} 178}
184//indent end 179//indent end
185 180
186//indent run -ci8 -nlp 181//indent run -ci8 -nlp
187int 182int
188sum(int a, int b) 183sum(int a, int b)
189{ 184{
190 return (a + 185 return (a +
191 b); 186 b);
192 return (first + 187 return (first +
193 second + ( 188 second + (
194 third)); 189 third));
195} 190}

cvs diff -r1.9 -r1.10 src/tests/usr.bin/indent/opt_lp.c (expand / switch to unified diff)

--- src/tests/usr.bin/indent/opt_lp.c 2023/06/08 20:36:35 1.9
+++ src/tests/usr.bin/indent/opt_lp.c 2023/06/09 06:36:58 1.10
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: opt_lp.c,v 1.9 2023/06/08 20:36:35 rillig Exp $ */ 1/* $NetBSD: opt_lp.c,v 1.10 2023/06/09 06:36:58 rillig Exp $ */
2 2
3/* 3/*
4 * Tests for the options '-lp' and '-nlp'. 4 * Tests for the options '-lp' and '-nlp'.
5 * 5 *
6 * The option '-lp' lines up code surrounded by parentheses in continuation 6 * The option '-lp' lines up code surrounded by parentheses in continuation
7 * lines. With '-lp', if a line has a left parenthesis that is not closed on 7 * lines. With '-lp', if a line has a left parenthesis that is not closed on
8 * that line, continuation lines are lined up to start at the character 8 * that line, continuation lines are lined up to start at the character
9 * position just after the left parenthesis. 9 * position just after the left parenthesis.
10 * 10 *
11 * The option '-nlp' indents continuation lines with the continuation 11 * The option '-nlp' indents continuation lines with the continuation
12 * indentation; see '-ci'. 12 * indentation; see '-ci'.
13 */ 13 */
14 14
@@ -56,41 +56,37 @@ example(void) @@ -56,41 +56,37 @@ example(void)
56 third_procedure(p4, p5)); 56 third_procedure(p4, p5));
57 57
58 p1 = first_procedure(second_procedure(p2, 58 p1 = first_procedure(second_procedure(p2,
59 p3), 59 p3),
60 third_procedure(p4, 60 third_procedure(p4,
61 p5)); 61 p5));
62 62
63 p1 = first_procedure( 63 p1 = first_procedure(
64 second_procedure(p2, p3), 64 second_procedure(p2, p3),
65 third_procedure(p4, p5)); 65 third_procedure(p4, p5));
66} 66}
67//indent end 67//indent end
68 68
69/* 
70 * XXX: Combining the options '-nlp' and '-ci4' is counterproductive as the 
71 * indentation does not make the nesting level of the function calls visible. 
72 */ 
73//indent run -nlp -ci4 69//indent run -nlp -ci4
74void 70void
75example(void) 71example(void)
76{ 72{
77 p1 = first_procedure(second_procedure(p2, p3), 73 p1 = first_procedure(second_procedure(p2, p3),
78 third_procedure(p4, p5)); 74 third_procedure(p4, p5));
79 75
80 p1 = first_procedure(second_procedure(p2, 76 p1 = first_procedure(second_procedure(p2,
81 p3), 77 p3),
82 third_procedure(p4, 78 third_procedure(p4,
83 p5)); 79 p5));
84 80
85 p1 = first_procedure( 81 p1 = first_procedure(
86 second_procedure(p2, p3), 82 second_procedure(p2, p3),
87 third_procedure(p4, p5)); 83 third_procedure(p4, p5));
88} 84}
89//indent end 85//indent end
90 86
91 87
92/* 88/*
93 * Ensure that in multi-line else-if conditions, all lines are indented by the 89 * Ensure that in multi-line else-if conditions, all lines are indented by the
94 * correct amount. The 'else if' condition is tricky because it has the same 90 * correct amount. The 'else if' condition is tricky because it has the same
95 * indentation as the preceding 'if' condition. 91 * indentation as the preceding 'if' condition.
96 */ 92 */

cvs diff -r1.32 -r1.33 src/usr.bin/indent/indent.1 (expand / switch to unified diff)

--- src/usr.bin/indent/indent.1 2023/06/05 10:12:21 1.32
+++ src/usr.bin/indent/indent.1 2023/06/09 06:36:57 1.33
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1.\" $NetBSD: indent.1,v 1.32 2023/06/05 10:12:21 rillig Exp $ 1.\" $NetBSD: indent.1,v 1.33 2023/06/09 06:36:57 rillig Exp $
2.\" 2.\"
3.\" Copyright (c) 1980, 1990, 1993 3.\" Copyright (c) 1980, 1990, 1993
4.\" The Regents of the University of California. All rights reserved. 4.\" The Regents of the University of California. All rights reserved.
5.\" Copyright (c) 1976 Board of Trustees of the University of Illinois. 5.\" Copyright (c) 1976 Board of Trustees of the University of Illinois.
6.\" All rights reserved. 6.\" All rights reserved.
7.\" 7.\"
8.\" Redistribution and use in source and binary forms, with or without 8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions 9.\" modification, are permitted provided that the following conditions
10.\" are met: 10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright 11.\" 1. Redistributions of source code must retain the above copyright
12.\" notice, this list of conditions and the following disclaimer. 12.\" notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\" notice, this list of conditions and the following disclaimer in the 14.\" notice, this list of conditions and the following disclaimer in the
@@ -22,27 +22,27 @@ @@ -22,27 +22,27 @@
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" @(#)indent.1 8.1 (Berkeley) 7/1/93 32.\" @(#)indent.1 8.1 (Berkeley) 7/1/93
33.\" $FreeBSD: head/usr.bin/indent/indent.1 334944 2018-06-11 05:35:57Z pstef $ 33.\" $FreeBSD: head/usr.bin/indent/indent.1 334944 2018-06-11 05:35:57Z pstef $
34.\" 34.\"
35.Dd June 5, 2023 35.Dd June 9, 2023
36.Dt INDENT 1 36.Dt INDENT 1
37.Os 37.Os
38.Sh NAME 38.Sh NAME
39.Nm indent 39.Nm indent
40.Nd indent and format C program source 40.Nd indent and format C program source
41.Sh SYNOPSIS 41.Sh SYNOPSIS
42.Nm 42.Nm
43.Op Ar input-file Op Ar output-file 43.Op Ar input-file Op Ar output-file
44.Op Fl bacc | Fl nbacc 44.Op Fl bacc | Fl nbacc
45.Op Fl bad | Fl nbad 45.Op Fl bad | Fl nbad
46.Op Fl badp | Fl nbadp 46.Op Fl badp | Fl nbadp
47.Op Fl bap | Fl nbap 47.Op Fl bap | Fl nbap
48.Op Fl bbb | Fl nbbb 48.Op Fl bbb | Fl nbbb
@@ -236,28 +236,27 @@ The default is @@ -236,28 +236,27 @@ The default is
236Enables (disables) forcing of `else's to cuddle up to the immediately preceding 236Enables (disables) forcing of `else's to cuddle up to the immediately preceding
237`}'. 237`}'.
238The default is 238The default is
239.Fl \&ce . 239.Fl \&ce .
240.It Fl \&ci Ns Ar n 240.It Fl \&ci Ns Ar n
241Sets the continuation indent to be 241Sets the continuation indent to be
242.Ar n . 242.Ar n .
243Continuation 243Continuation
244lines will be indented that far from the beginning of the first line of the 244lines will be indented that far from the beginning of the first line of the
245statement. 245statement.
246Parenthesized expressions have extra indentation added to 246Parenthesized expressions have extra indentation added to
247indicate the nesting, unless 247indicate the nesting, unless
248.Fl \&lp 248.Fl \&lp
249is in effect 249is in effect.
250or the continuation indent is exactly half of the main indent. 
251.Fl \&ci 250.Fl \&ci
252defaults to the same value as 251defaults to the same value as
253.Fl i . 252.Fl i .
254.It Fl cli Ns Ar n 253.It Fl cli Ns Ar n
255Causes case labels to be indented 254Causes case labels to be indented
256.Ar n 255.Ar n
257indentation levels to the right of the containing 256indentation levels to the right of the containing
258.Ic switch 257.Ic switch
259statement. 258statement.
260.Fl cli0.5 259.Fl cli0.5
261causes case labels to be indented half an indentation level. 260causes case labels to be indented half an indentation level.
262The 261The
263default is 262default is

cvs diff -r1.204 -r1.205 src/usr.bin/indent/io.c (expand / switch to unified diff)

--- src/usr.bin/indent/io.c 2023/06/08 20:36:35 1.204
+++ src/usr.bin/indent/io.c 2023/06/09 06:36:57 1.205
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: io.c,v 1.204 2023/06/08 20:36:35 rillig Exp $ */ 1/* $NetBSD: io.c,v 1.205 2023/06/09 06:36:57 rillig Exp $ */
2 2
3/*- 3/*-
4 * SPDX-License-Identifier: BSD-4-Clause 4 * SPDX-License-Identifier: BSD-4-Clause
5 * 5 *
6 * Copyright (c) 1985 Sun Microsystems, Inc. 6 * Copyright (c) 1985 Sun Microsystems, Inc.
7 * Copyright (c) 1980, 1993 7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved. 8 * The Regents of the University of California. All rights reserved.
9 * All rights reserved. 9 * All rights reserved.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
@@ -28,27 +28,27 @@ @@ -28,27 +28,27 @@
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE. 37 * SUCH DAMAGE.
38 */ 38 */
39 39
40#include <sys/cdefs.h> 40#include <sys/cdefs.h>
41__RCSID("$NetBSD: io.c,v 1.204 2023/06/08 20:36:35 rillig Exp $"); 41__RCSID("$NetBSD: io.c,v 1.205 2023/06/09 06:36:57 rillig Exp $");
42 42
43#include <stdio.h> 43#include <stdio.h>
44 44
45#include "indent.h" 45#include "indent.h"
46 46
47struct buffer inp; 47struct buffer inp;
48const char *inp_p; 48const char *inp_p;
49 49
50struct output_state out; 50struct output_state out;
51static int out_ind; /* width of the line that is being written */ 51static int out_ind; /* width of the line that is being written */
52static unsigned wrote_newlines = 2; /* 0 in the middle of a line, 1 after a 52static unsigned wrote_newlines = 2; /* 0 in the middle of a line, 1 after a
53 * single '\n', > 1 means there were (n 53 * single '\n', > 1 means there were (n
54 * - 1) blank lines above */ 54 * - 1) blank lines above */
@@ -242,31 +242,27 @@ compute_code_indent(void) @@ -242,31 +242,27 @@ compute_code_indent(void)
242 return base_ind + opt.continuation_indent; 242 return base_ind + opt.continuation_indent;
243 return base_ind; 243 return base_ind;
244 } 244 }
245 245
246 if (opt.lineup_to_parens) { 246 if (opt.lineup_to_parens) {
247 if (opt.lineup_to_parens_always) 247 if (opt.lineup_to_parens_always)
248 return paren_indent; 248 return paren_indent;
249 return compute_code_indent_lineup(base_ind); 249 return compute_code_indent_lineup(base_ind);
250 } 250 }
251 251
252 if (ps.extra_expr_indent != eei_no) 252 if (ps.extra_expr_indent != eei_no)
253 return base_ind + 2 * opt.continuation_indent; 253 return base_ind + 2 * opt.continuation_indent;
254 254
255 if (2 * opt.continuation_indent == opt.indent_size) 255 return base_ind + opt.continuation_indent * ps.line_start_nparen;
256 return base_ind + opt.continuation_indent; 
257 else 
258 return base_ind + 
259 opt.continuation_indent * ps.line_start_nparen; 
260} 256}
261 257
262static void 258static void
263output_line_code(void) 259output_line_code(void)
264{ 260{
265 int target_ind = compute_code_indent(); 261 int target_ind = compute_code_indent();
266 for (int i = 0; i < ps.nparen; i++) { 262 for (int i = 0; i < ps.nparen; i++) {
267 int paren_ind = ps.paren[i].indent; 263 int paren_ind = ps.paren[i].indent;
268 if (paren_ind >= 0) { 264 if (paren_ind >= 0) {
269 ps.paren[i].indent = -1 - (paren_ind + target_ind); 265 ps.paren[i].indent = -1 - (paren_ind + target_ind);
270 debug_println( 266 debug_println(
271 "setting paren_indents[%d] from %d to %d " 267 "setting paren_indents[%d] from %d to %d "
272 "for column %d", 268 "for column %d",