Fri Jun 16 14:26:27 2023 UTC ()
indent: improve heuristics for cast expressions


(rillig)
diff -r1.7 -r1.8 src/tests/usr.bin/indent/lsym_rparen_or_rbracket.c
diff -r1.376 -r1.377 src/usr.bin/indent/indent.c

cvs diff -r1.7 -r1.8 src/tests/usr.bin/indent/lsym_rparen_or_rbracket.c (expand / switch to unified diff)

--- src/tests/usr.bin/indent/lsym_rparen_or_rbracket.c 2023/06/16 14:12:10 1.7
+++ src/tests/usr.bin/indent/lsym_rparen_or_rbracket.c 2023/06/16 14:26:27 1.8
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lsym_rparen_or_rbracket.c,v 1.7 2023/06/16 14:12:10 rillig Exp $ */ 1/* $NetBSD: lsym_rparen_or_rbracket.c,v 1.8 2023/06/16 14:26:27 rillig Exp $ */
2 2
3/* 3/*
4 * Tests for the token lsym_rparen_or_lbracket, which represents ')' or ']', 4 * Tests for the token lsym_rparen_or_lbracket, which represents ')' or ']',
5 * the counterparts for '(' and '['. 5 * the counterparts for '(' and '['.
6 * 6 *
7 * See also: 7 * See also:
8 * lsym_lparen_or_lbracket.c 8 * lsym_lparen_or_lbracket.c
9 */ 9 */
10 10
11//indent input 11//indent input
12int var = (3); 12int var = (3);
13int cast = (int)3; 13int cast = (int)3;
14int cast = (int)(3); 14int cast = (int)(3);
@@ -46,31 +46,28 @@ int a = array[ @@ -46,31 +46,28 @@ int a = array[
46//indent run -di0 -nlp 46//indent run -di0 -nlp
47int a = array[ 47int a = array[
48 3 48 3
49]; 49];
50{ 50{
51 int a = array[ 51 int a = array[
52 3 52 3
53// $ FIXME: Should be one level to the left since it is the outermost bracket. 53// $ FIXME: Should be one level to the left since it is the outermost bracket.
54 ]; 54 ];
55} 55}
56//indent end 56//indent end
57 57
58 58
 59/*
 60 * Cast expressions and compound expressions, taken from lint and make.
 61 */
59//indent input 62//indent input
 63// This ')' is not a cast.
 64char *Buf_DoneData(Buffer *) MAKE_ATTR_USE;
 65
60{ 66{
61 dcs_align((u_int)dcs); 67 dcs_align((u_int)dcs);
62 mpools.pools[i] = (memory_pool){NULL, 0, 0}; 68 mpools.pools[i] = (memory_pool){NULL, 0, 0};
63 list_add(l, (const char[3]){'-', (char)c, '\0'}); 69 list_add(l, (const char[3]){'-', (char)c, '\0'});
64} 70}
65//indent end 71//indent end
66 72
67//indent run -ci4 -di0 -nlp 73//indent run-equals-input -ci4 -di0 -nlp
68{ 
69 dcs_align((u_int)dcs); 
70// $ FIXME: Don't add newlines. 
71 mpools.pools[i] = (memory_pool) { 
72 NULL, 0, 0 
73 }; 
74 list_add(l, (const char[3]){'-', (char)c, '\0'}); 
75} 
76//indent end 

cvs diff -r1.376 -r1.377 src/usr.bin/indent/indent.c (expand / switch to unified diff)

--- src/usr.bin/indent/indent.c 2023/06/16 14:12:10 1.376
+++ src/usr.bin/indent/indent.c 2023/06/16 14:26:26 1.377
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: indent.c,v 1.376 2023/06/16 14:12:10 rillig Exp $ */ 1/* $NetBSD: indent.c,v 1.377 2023/06/16 14:26:26 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) 1976 Board of Trustees of the University of Illinois. 7 * Copyright (c) 1976 Board of Trustees of the University of Illinois.
8 * Copyright (c) 1980, 1993 8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved. 9 * The Regents of the University of California. 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: indent.c,v 1.376 2023/06/16 14:12:10 rillig Exp $"); 41__RCSID("$NetBSD: indent.c,v 1.377 2023/06/16 14:26:26 rillig Exp $");
42 42
43#include <sys/param.h> 43#include <sys/param.h>
44#include <err.h> 44#include <err.h>
45#include <stdarg.h> 45#include <stdarg.h>
46#include <stdio.h> 46#include <stdio.h>
47#include <stdlib.h> 47#include <stdlib.h>
48#include <string.h> 48#include <string.h>
49 49
50#include "indent.h" 50#include "indent.h"
51 51
52struct options opt = { 52struct options opt = {
53 .brace_same_line = true, 53 .brace_same_line = true,
54 .comment_delimiter_on_blank_line = true, 54 .comment_delimiter_on_blank_line = true,
@@ -612,31 +612,33 @@ process_lparen(void) @@ -612,31 +612,33 @@ process_lparen(void)
612 || ps.prev_lsym == lsym_switch 612 || ps.prev_lsym == lsym_switch
613 || ps.prev_lsym == lsym_while 613 || ps.prev_lsym == lsym_while
614 || ps.line_has_func_def) 614 || ps.line_has_func_def)
615 cast = cast_no; 615 cast = cast_no;
616 616
617 paren_stack_push(&ps.paren, ind_add(0, code.s, code.len), cast); 617 paren_stack_push(&ps.paren, ind_add(0, code.s, code.len), cast);
618} 618}
619 619
620static bool 620static bool
621rparen_is_cast(bool paren_cast) 621rparen_is_cast(bool paren_cast)
622{ 622{
623 if (ps.in_func_def_params) 623 if (ps.in_func_def_params)
624 return false; 624 return false;
 625 if (ps.line_has_decl && !ps.in_init)
 626 return false;
625 if (ps.prev_lsym == lsym_unary_op) 627 if (ps.prev_lsym == lsym_unary_op)
626 return true; 628 return true;
627 if (ps.line_has_decl && !ps.in_init) 629 if (ps.spaced_expr_psym != psym_0 && ps.paren.len == 0)
628 return false; 630 return false;
629 return paren_cast || ch_isalpha(inp_p[0]); 631 return paren_cast || ch_isalpha(inp_p[0]) || inp_p[0] == '{';
630} 632}
631 633
632static void 634static void
633process_rparen(void) 635process_rparen(void)
634{ 636{
635 if (ps.paren.len == 0) 637 if (ps.paren.len == 0)
636 diag(0, "Extra '%c'", *token.s); 638 diag(0, "Extra '%c'", *token.s);
637 639
638 bool paren_cast = ps.paren.len > 0 640 bool paren_cast = ps.paren.len > 0
639 && ps.paren.item[--ps.paren.len].cast == cast_maybe; 641 && ps.paren.item[--ps.paren.len].cast == cast_maybe;
640 ps.prev_paren_was_cast = rparen_is_cast(paren_cast); 642 ps.prev_paren_was_cast = rparen_is_cast(paren_cast);
641 if (ps.prev_paren_was_cast) { 643 if (ps.prev_paren_was_cast) {
642 ps.next_unary = true; 644 ps.next_unary = true;