| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: indent.c,v 1.375 2023/06/16 12:55:57 rillig Exp $ */ | | 1 | /* $NetBSD: indent.c,v 1.376 2023/06/16 14:12:10 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.375 2023/06/16 12:55:57 rillig Exp $"); | | 41 | __RCSID("$NetBSD: indent.c,v 1.376 2023/06/16 14:12:10 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 | |
52 | struct options opt = { | | 52 | struct 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, |
| @@ -607,38 +607,47 @@ process_lparen(void) | | | @@ -607,38 +607,47 @@ process_lparen(void) |
607 | enum paren_level_cast cast = cast_unknown; | | 607 | enum paren_level_cast cast = cast_unknown; |
608 | if (ps.prev_lsym == lsym_offsetof | | 608 | if (ps.prev_lsym == lsym_offsetof |
609 | || ps.prev_lsym == lsym_sizeof | | 609 | || ps.prev_lsym == lsym_sizeof |
610 | || ps.prev_lsym == lsym_for | | 610 | || ps.prev_lsym == lsym_for |
611 | || ps.prev_lsym == lsym_if | | 611 | || ps.prev_lsym == lsym_if |
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 | |
| | | 620 | static bool |
| | | 621 | rparen_is_cast(bool paren_cast) |
| | | 622 | { |
| | | 623 | if (ps.in_func_def_params) |
| | | 624 | return false; |
| | | 625 | if (ps.prev_lsym == lsym_unary_op) |
| | | 626 | return true; |
| | | 627 | if (ps.line_has_decl && !ps.in_init) |
| | | 628 | return false; |
| | | 629 | return paren_cast || ch_isalpha(inp_p[0]); |
| | | 630 | } |
| | | 631 | |
620 | static void | | 632 | static void |
621 | process_rparen(void) | | 633 | process_rparen(void) |
622 | { | | 634 | { |
623 | if (ps.paren.len == 0) | | 635 | if (ps.paren.len == 0) |
624 | diag(0, "Extra '%c'", *token.s); | | 636 | diag(0, "Extra '%c'", *token.s); |
625 | | | 637 | |
626 | ps.prev_paren_was_cast = ps.paren.len > 0 | | 638 | bool paren_cast = ps.paren.len > 0 |
627 | && ps.paren.item[--ps.paren.len].cast == cast_maybe | | 639 | && ps.paren.item[--ps.paren.len].cast == cast_maybe; |
628 | && !ps.in_func_def_params | | 640 | ps.prev_paren_was_cast = rparen_is_cast(paren_cast); |
629 | && !(ps.line_has_decl && !ps.in_init); | | | |
630 | if (ps.prev_lsym == lsym_unary_op) | | | |
631 | ps.prev_paren_was_cast = true; | | | |
632 | if (ps.prev_paren_was_cast) { | | 641 | if (ps.prev_paren_was_cast) { |
633 | ps.next_unary = true; | | 642 | ps.next_unary = true; |
634 | ps.want_blank = opt.space_after_cast; | | 643 | ps.want_blank = opt.space_after_cast; |
635 | } else | | 644 | } else |
636 | ps.want_blank = true; | | 645 | ps.want_blank = true; |
637 | | | 646 | |
638 | if (code.len == 0) | | 647 | if (code.len == 0) |
639 | ps.ind_paren_level = (int)ps.paren.len; | | 648 | ps.ind_paren_level = (int)ps.paren.len; |
640 | | | 649 | |
641 | buf_add_buf(&code, &token); | | 650 | buf_add_buf(&code, &token); |
642 | | | 651 | |
643 | if (ps.spaced_expr_psym != psym_0 && ps.paren.len == 0) { | | 652 | if (ps.spaced_expr_psym != psym_0 && ps.paren.len == 0) { |
644 | parse(ps.spaced_expr_psym); | | 653 | parse(ps.spaced_expr_psym); |