Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/indent indent: fix formatting of parenthesized name ...



details:   https://anonhg.NetBSD.org/src/rev/6463ac055180
branches:  trunk
changeset: 376622:6463ac055180
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jun 25 19:19:42 2023 +0000

description:
indent: fix formatting of parenthesized name in function definition

diffstat:

 tests/usr.bin/indent/fmt_decl.c |  17 +++++------
 usr.bin/indent/indent.c         |   7 ++--
 usr.bin/indent/lexi.c           |  55 ++++++++++++++++++++++++++++++++--------
 3 files changed, 56 insertions(+), 23 deletions(-)

diffs (189 lines):

diff -r 65c1cd16498b -r 6463ac055180 tests/usr.bin/indent/fmt_decl.c
--- a/tests/usr.bin/indent/fmt_decl.c   Sun Jun 25 18:41:03 2023 +0000
+++ b/tests/usr.bin/indent/fmt_decl.c   Sun Jun 25 19:19:42 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fmt_decl.c,v 1.59 2023/06/16 23:51:32 rillig Exp $     */
+/*     $NetBSD: fmt_decl.c,v 1.60 2023/06/25 19:19:42 rillig Exp $     */
 
 /*
  * Tests for declarations of global variables, external functions, and local
@@ -963,6 +963,12 @@ debug_printf(const char *fmt, ...)
 //indent end
 
 
+/*
+ * When a name is defined both as a function and as a macro, the name in the
+ * function definition must be enclosed in parentheses, to prevent the macro
+ * from being expanded. It is also possible to undefine the macro, but that is
+ * often not done in practice.
+ */
 //indent input
 void
 (error_at)(int msgid, const pos_t *pos, ...)
@@ -970,14 +976,7 @@ void
 }
 //indent end
 
-//indent run -ci4 -di0 -ndj -nlp
-void
-// $ FIXME: Wrong indentation, should be 0 instead.
-// $ FIXME: There should be no space after the '*'.
-     (error_at)(int msgid, const pos_t * pos, ...)
-{
-}
-//indent end
+//indent run-equals-input
 
 
 //indent input
diff -r 65c1cd16498b -r 6463ac055180 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sun Jun 25 18:41:03 2023 +0000
+++ b/usr.bin/indent/indent.c   Sun Jun 25 19:19:42 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.382 2023/06/23 20:43:21 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.383 2023/06/25 19:19:42 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.382 2023/06/23 20:43:21 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.383 2023/06/25 19:19:42 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -961,7 +961,8 @@ process_word(lexer_symbol lsym)
        if (ps.in_decl) {
                if (lsym == lsym_funcname) {
                        ps.in_decl = false;
-                       if (opt.procnames_start_line && code.len > 0)
+                       if (opt.procnames_start_line
+                           && code.len > (*inp_p == ')' ? 1 : 0))
                                output_line();
                        else if (ps.want_blank)
                                buf_add_char(&code, ' ');
diff -r 65c1cd16498b -r 6463ac055180 usr.bin/indent/lexi.c
--- a/usr.bin/indent/lexi.c     Sun Jun 25 18:41:03 2023 +0000
+++ b/usr.bin/indent/lexi.c     Sun Jun 25 19:19:42 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lexi.c,v 1.233 2023/06/25 18:41:03 rillig Exp $        */
+/*     $NetBSD: lexi.c,v 1.234 2023/06/25 19:19:42 rillig Exp $        */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.233 2023/06/25 18:41:03 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.234 2023/06/25 19:19:42 rillig Exp $");
 
 #include <stdlib.h>
 #include <string.h>
@@ -333,10 +333,10 @@ cmp_keyword_by_name(const void *key, con
  * function declaration.
  */
 static bool
-probably_function_definition(void)
+probably_function_definition(const char *p)
 {
        int paren_level = 0;
-       for (const char *p = inp_p; *p != '\n'; p++) {
+       for (; *p != '\n'; p++) {
                if (*p == '(')
                        paren_level++;
                if (*p == ')' && --paren_level == 0) {
@@ -438,10 +438,17 @@ found_typename:
                }
        }
 
-       if (*inp_p == '(' && ps.psyms.len < 3 && ps.ind_level == 0 &&
+       const char *p = inp_p;
+       if (*p == ')')
+               p++;
+       if (*p == '(' && ps.psyms.len < 3 && ps.ind_level == 0 &&
            !ps.in_func_def_params && !ps.in_init) {
 
-               if (ps.paren.len == 0 && probably_function_definition()) {
+               bool maybe_function_definition = *inp_p == ')'
+                   ? ps.paren.len == 1 && ps.prev_lsym != lsym_unary_op
+                   : ps.paren.len == 0;
+               if (maybe_function_definition
+                   && probably_function_definition(p)) {
                        ps.line_has_func_def = true;
                        if (ps.in_decl)
                                ps.in_func_def_params = true;
@@ -456,8 +463,28 @@ found_typename:
        return lsym;
 }
 
+static void
+check_parenthesized_function_definition(void)
+{
+       const char *p = inp_p;
+       while (ch_isblank(*p))
+               p++;
+       if (is_identifier_start(*p))
+               while (is_identifier_part(*p))
+                       p++;
+       while (ch_isblank(*p))
+               p++;
+       if (*p == ')') {
+               p++;
+               while (ch_isblank(*p))
+                       p++;
+               if (*p == '(' && probably_function_definition(p))
+                       ps.line_has_func_def = true;
+       }
+}
+
 static bool
-is_asterisk_pointer(void)
+is_asterisk_unary(void)
 {
        const char *p = inp_p;
        while (*p == '*' || ch_isblank(*p))
@@ -490,7 +517,7 @@ probably_in_function_definition(void)
 }
 
 static void
-lex_asterisk_pointer(void)
+lex_asterisk_unary(void)
 {
        while (*inp_p == '*' || ch_isspace(*inp_p)) {
                if (*inp_p == '*')
@@ -575,7 +602,6 @@ lexi(void)
                break;
 
        /* INDENT OFF */
-       case '(':       lsym = lsym_lparen;     next_unary = true;      break;
        case ')':       lsym = lsym_rparen;     next_unary = false;     break;
        case '[':       lsym = lsym_lbracket;   next_unary = true;      break;
        case ']':       lsym = lsym_rbracket;   next_unary = false;     break;
@@ -587,6 +613,13 @@ lexi(void)
        case ';':       lsym = lsym_semicolon;  next_unary = true;      break;
        /* INDENT ON */
 
+       case '(':
+               if (inp_p == inp.s + 1)
+                       check_parenthesized_function_definition();
+               lsym = lsym_lparen;
+               next_unary = true;
+               break;
+
        case '+':
        case '-':
                lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
@@ -625,8 +658,8 @@ lexi(void)
                if (*inp_p == '=') {
                        token_add_char(*inp_p++);
                        lsym = lsym_binary_op;
-               } else if (is_asterisk_pointer()) {
-                       lex_asterisk_pointer();
+               } else if (is_asterisk_unary()) {
+                       lex_asterisk_unary();
                        lsym = lsym_unary_op;
                } else
                        lsym = lsym_binary_op;



Home | Main Index | Thread Index | Old Index