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: use enum instead of magic numbers for...



details:   https://anonhg.NetBSD.org/src/rev/0352f0b98bdb
branches:  trunk
changeset: 374827:0352f0b98bdb
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat May 13 14:30:48 2023 +0000

description:
indent: use enum instead of magic numbers for tracking declarations

No functional change.

diffstat:

 usr.bin/indent/debug.c      |  12 +++++++++---
 usr.bin/indent/indent.c     |  14 +++++++-------
 usr.bin/indent/indent.h     |   8 ++++++--
 usr.bin/indent/io.c         |   8 ++++----
 usr.bin/indent/pr_comment.c |  10 +++++-----
 5 files changed, 31 insertions(+), 21 deletions(-)

diffs (189 lines):

diff -r 238bfa8f4bb6 -r 0352f0b98bdb usr.bin/indent/debug.c
--- a/usr.bin/indent/debug.c    Sat May 13 14:19:14 2023 +0000
+++ b/usr.bin/indent/debug.c    Sat May 13 14:30:48 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: debug.c,v 1.2 2023/05/13 13:45:24 rillig Exp $ */
+/*     $NetBSD: debug.c,v 1.3 2023/05/13 14:30:48 rillig Exp $ */
 
 /*-
  * Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.2 2023/05/13 13:45:24 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.3 2023/05/13 14:30:48 rillig Exp $");
 
 #include "indent.h"
 
@@ -90,6 +90,12 @@ const char *const psym_name[] = {
     "while_expr",
 };
 
+static const char *declaration_name[] = {
+    "no",
+    "begin",
+    "end",
+};
+
 static const char *in_enum_name[] = {
     "no",
     "enum",
@@ -217,7 +223,7 @@ debug_parser_state(lexer_symbol lsym)
     debug_ps_int(decl_level);
     debug_ps_bool(decl_on_line);
     debug_ps_bool(in_decl);
-    debug_ps_int(just_saw_decl);
+    debug_ps_enum(declaration, declaration_name);
     debug_ps_bool(in_func_def_params);
     debug_ps_enum(in_enum, in_enum_name);
     debug_ps_bool(decl_indent_done);
diff -r 238bfa8f4bb6 -r 0352f0b98bdb usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat May 13 14:19:14 2023 +0000
+++ b/usr.bin/indent/indent.c   Sat May 13 14:30:48 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.259 2023/05/13 12:31:02 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.260 2023/05/13 14:30:48 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)indent.c  5.1
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.259 2023/05/13 12:31:02 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.260 2023/05/13 14:30:48 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -630,7 +630,7 @@ process_semicolon(void)
        ps.in_func_def_params = false;
     ps.block_init = false;
     ps.block_init_level = 0;
-    ps.just_saw_decl--;
+    ps.declaration = ps.declaration == decl_begin ? decl_end : decl_no;
 
     if (ps.in_decl && code.s == code.e && !ps.block_init &&
        !ps.decl_indent_done && ps.line_start_nparen == 0) {
@@ -725,7 +725,7 @@ process_lbrace(void)
        *code.e++ = ' ';
     ps.want_blank = false;
     *code.e++ = '{';
-    ps.just_saw_decl = 0;
+    ps.declaration = decl_no;
 }
 
 static void
@@ -737,7 +737,7 @@ process_rbrace(void)
        ps.spaced_expr_psym = psym_0;
     }
 
-    ps.just_saw_decl = 0;
+    ps.declaration = decl_no;
     ps.block_init_level--;
 
     if (code.s != code.e && !ps.block_init) {  /* '}' must be first on line */
@@ -754,7 +754,7 @@ process_rbrace(void)
     if (ps.decl_level > 0) {   /* multi-level structure declaration */
        ps.decl_ind = ps.di_stack[--ps.decl_level];
        if (ps.decl_level == 0 && !ps.in_func_def_params) {
-           ps.just_saw_decl = 2;
+           ps.declaration = decl_begin;
            ps.decl_ind = ps.ind_level == 0
                ? opt.decl_indent : opt.local_decl_indent;
        }
@@ -817,7 +817,7 @@ process_type(void)
     ps.init_or_struct = /* maybe */ true;
     ps.in_decl = ps.decl_on_line = ps.prev_token != lsym_typedef;
     if (ps.decl_level <= 0)
-       ps.just_saw_decl = 2;
+       ps.declaration = decl_begin;
 
     int len = (int)buf_len(&token) + 1;
     int ind = ps.ind_level == 0 || ps.decl_level > 0
diff -r 238bfa8f4bb6 -r 0352f0b98bdb usr.bin/indent/indent.h
--- a/usr.bin/indent/indent.h   Sat May 13 14:19:14 2023 +0000
+++ b/usr.bin/indent/indent.h   Sat May 13 14:30:48 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.h,v 1.126 2023/05/13 13:48:54 rillig Exp $      */
+/*     $NetBSD: indent.h,v 1.127 2023/05/13 14:30:48 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -306,7 +306,11 @@ extern struct parser_state {
     bool in_decl;              /* whether we are in a declaration. The
                                 * processing of braces is then slightly
                                 * different */
-    int just_saw_decl;
+    enum declaration {
+       decl_no,                /* no declaration anywhere nearby */
+       decl_begin,             /* collecting tokens of a declaration */
+       decl_end,               /* finished a declaration */
+    } declaration;
     bool in_func_def_params;
     enum {
        in_enum_no,             /* outside any 'enum { ... }' */
diff -r 238bfa8f4bb6 -r 0352f0b98bdb usr.bin/indent/io.c
--- a/usr.bin/indent/io.c       Sat May 13 14:19:14 2023 +0000
+++ b/usr.bin/indent/io.c       Sat May 13 14:30:48 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: io.c,v 1.158 2023/05/13 13:48:54 rillig Exp $  */
+/*     $NetBSD: io.c,v 1.159 2023/05/13 14:30:48 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)io.c      8.1 (Be
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: io.c,v 1.158 2023/05/13 13:48:54 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.159 2023/05/13 14:30:48 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/io.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -302,8 +302,8 @@ output_complete_line(char line_terminato
 
        output_char(line_terminator);
 
-       if (ps.just_saw_decl == 1 && opt.blank_line_after_decl)
-           ps.just_saw_decl = 0;
+       if (ps.declaration == decl_end && opt.blank_line_after_decl)
+           ps.declaration = decl_no;
     }
 
     ps.decl_on_line = ps.in_decl;      /* for proper comment indentation */
diff -r 238bfa8f4bb6 -r 0352f0b98bdb usr.bin/indent/pr_comment.c
--- a/usr.bin/indent/pr_comment.c       Sat May 13 14:19:14 2023 +0000
+++ b/usr.bin/indent/pr_comment.c       Sat May 13 14:30:48 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pr_comment.c,v 1.131 2023/05/13 12:31:02 rillig Exp $  */
+/*     $NetBSD: pr_comment.c,v 1.132 2023/05/13 14:30:48 rillig Exp $  */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)pr_comment.c
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: pr_comment.c,v 1.131 2023/05/13 12:31:02 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.132 2023/05/13 14:30:48 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $");
 #endif
@@ -354,13 +354,13 @@ process_comment(void)
     int adj_max_line_length;
     bool may_wrap, break_delim;
 
-    ps.just_saw_decl = 0;
+    ps.declaration = decl_no;
 
-    int saved_just_saw_decl = ps.just_saw_decl;
+    enum declaration saved_just_saw_decl = ps.declaration;
     analyze_comment(&may_wrap, &break_delim, &adj_max_line_length);
     if (may_wrap)
        copy_comment_wrap(adj_max_line_length, break_delim);
     else
        copy_comment_nowrap();
-    ps.just_saw_decl = saved_just_saw_decl;
+    ps.declaration = saved_just_saw_decl;
 }



Home | Main Index | Thread Index | Old Index