Fri Jun 16 12:55:57 2023 UTC ()
indent: improve heuristics for casts


(rillig)
diff -r1.8 -r1.9 src/tests/usr.bin/indent/fmt_expr.c
diff -r1.10 -r1.11 src/tests/usr.bin/indent/lsym_unary_op.c
diff -r1.374 -r1.375 src/usr.bin/indent/indent.c

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

--- src/tests/usr.bin/indent/fmt_expr.c 2023/06/14 17:07:32 1.8
+++ src/tests/usr.bin/indent/fmt_expr.c 2023/06/16 12:55:57 1.9
@@ -1,36 +1,35 @@ @@ -1,36 +1,35 @@
1/* $NetBSD: fmt_expr.c,v 1.8 2023/06/14 17:07:32 rillig Exp $ */ 1/* $NetBSD: fmt_expr.c,v 1.9 2023/06/16 12:55:57 rillig Exp $ */
2 2
3/* 3/*
4 * Tests for all kinds of expressions that are not directly related to unary 4 * Tests for all kinds of expressions that are not directly related to unary
5 * or binary operators. 5 * or binary operators.
6 * 6 *
7 * See also: 7 * See also:
8 * lsym_binary_op.c 8 * lsym_binary_op.c
9 * lsym_unary_op.c 9 * lsym_unary_op.c
10 */ 10 */
11 11
12//indent input 12//indent input
13{ 13{
14 // See lsym_offsetof.c. 14 // See lsym_offsetof.c.
15 malloc(offsetof(struct s, f) + 1); 15 malloc(offsetof(struct s, f) + 1);
16 16
17 // C99 compound literals use initializer braces. 17 // C99 compound literals use initializer braces.
18 println((const char[3]){'-', c, '\0'}); 18 println((const char[3]){'-', c, '\0'});
19 x = ((struct point){0, 0}).x; 19 x = ((struct point){0, 0}).x;
20 20
21 for (ln = gnodes->first; ln != NULL; ln = ln->next) 21 for (ln = gnodes->first; ln != NULL; ln = ln->next)
22// $ FIXME: No space after the cast. 22 *(GNode **)Vector_Push(&vec) = ln->datum;
23 *(GNode **) Vector_Push(&vec) = ln->datum; 
24} 23}
25//indent end 24//indent end
26 25
27//indent run-equals-input 26//indent run-equals-input
28 27
29 28
30/* 29/*
31 * GCC statement expressions are not supported yet. 30 * GCC statement expressions are not supported yet.
32 */ 31 */
33//indent input 32//indent input
34{ 33{
35 int var = ({1}); 34 int var = ({1});
36 int var = ({ 35 int var = ({

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

--- src/tests/usr.bin/indent/lsym_unary_op.c 2023/06/08 21:18:54 1.10
+++ src/tests/usr.bin/indent/lsym_unary_op.c 2023/06/16 12:55:57 1.11
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lsym_unary_op.c,v 1.10 2023/06/08 21:18:54 rillig Exp $ */ 1/* $NetBSD: lsym_unary_op.c,v 1.11 2023/06/16 12:55:57 rillig Exp $ */
2 2
3/* 3/*
4 * Tests for the token lsym_unary_op, which represents a unary operator. 4 * Tests for the token lsym_unary_op, which represents a unary operator.
5 * 5 *
6 * In an expression, a unary operator is written without blank next to its 6 * In an expression, a unary operator is written without blank next to its
7 * argument. 7 * argument.
8 * 8 *
9 * In a type name, the "unary operator" '*' represents the derivation of a 9 * In a type name, the "unary operator" '*' represents the derivation of a
10 * pointer type. 10 * pointer type.
11 * 11 *
12 * See also: 12 * See also:
13 * lsym_binary_op.c 13 * lsym_binary_op.c
14 * lsym_postfix_op.c 14 * lsym_postfix_op.c
@@ -75,27 +75,20 @@ unary_operators(void) @@ -75,27 +75,20 @@ unary_operators(void)
75 bool negated = !condition; 75 bool negated = !condition;
76} 76}
77//indent end 77//indent end
78 78
79//indent run-equals-input -di0 79//indent run-equals-input -di0
80 80
81 81
82/* 82/*
83 * Ensure that a '*' is not interpreted as unary operator in situations that 83 * Ensure that a '*' is not interpreted as unary operator in situations that
84 * may look like a cast expression. 84 * may look like a cast expression.
85 */ 85 */
86//indent input 86//indent input
87{ 87{
88sbuf_t *sb = *(sbuf_t **)sp; 88 sbuf_t *sb = *(sbuf_t **)sp;
89return (int)(a * (float)b); 
90a = (2 * b == c); 
91} 
92//indent end 
93 
94//indent run -di0 
95{ 
96// $ FIXME: Wrong spacing after the cast. 
97 sbuf_t *sb = *(sbuf_t **) sp; 
98 return (int)(a * (float)b); 89 return (int)(a * (float)b);
99 a = (2 * b == c); 90 a = (2 * b == c);
100} 91}
101//indent end 92//indent end
 93
 94//indent run-equals-input -di0

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

--- src/usr.bin/indent/indent.c 2023/06/16 12:30:45 1.374
+++ src/usr.bin/indent/indent.c 2023/06/16 12:55:57 1.375
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: indent.c,v 1.374 2023/06/16 12:30:45 rillig Exp $ */ 1/* $NetBSD: indent.c,v 1.375 2023/06/16 12:55: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) 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.374 2023/06/16 12:30:45 rillig Exp $"); 41__RCSID("$NetBSD: indent.c,v 1.375 2023/06/16 12:55:57 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,
@@ -617,26 +617,28 @@ process_lparen(void) @@ -617,26 +617,28 @@ process_lparen(void)
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 void 620static void
621process_rparen(void) 621process_rparen(void)
622{ 622{
623 if (ps.paren.len == 0) 623 if (ps.paren.len == 0)
624 diag(0, "Extra '%c'", *token.s); 624 diag(0, "Extra '%c'", *token.s);
625 625
626 ps.prev_paren_was_cast = ps.paren.len > 0 626 ps.prev_paren_was_cast = ps.paren.len > 0
627 && ps.paren.item[--ps.paren.len].cast == cast_maybe 627 && ps.paren.item[--ps.paren.len].cast == cast_maybe
628 && !ps.in_func_def_params 628 && !ps.in_func_def_params
629 && !(ps.line_has_decl && !ps.in_init); 629 && !(ps.line_has_decl && !ps.in_init);
 630 if (ps.prev_lsym == lsym_unary_op)
 631 ps.prev_paren_was_cast = true;
630 if (ps.prev_paren_was_cast) { 632 if (ps.prev_paren_was_cast) {
631 ps.next_unary = true; 633 ps.next_unary = true;
632 ps.want_blank = opt.space_after_cast; 634 ps.want_blank = opt.space_after_cast;
633 } else 635 } else
634 ps.want_blank = true; 636 ps.want_blank = true;
635 637
636 if (code.len == 0) 638 if (code.len == 0)
637 ps.ind_paren_level = (int)ps.paren.len; 639 ps.ind_paren_level = (int)ps.paren.len;
638 640
639 buf_add_buf(&code, &token); 641 buf_add_buf(&code, &token);
640 642
641 if (ps.spaced_expr_psym != psym_0 && ps.paren.len == 0) { 643 if (ps.spaced_expr_psym != psym_0 && ps.paren.len == 0) {
642 parse(ps.spaced_expr_psym); 644 parse(ps.spaced_expr_psym);