Module Name:    src
Committed By:   rillig
Date:           Sat May 13 14:30:49 UTC 2023

Modified Files:
        src/usr.bin/indent: debug.c indent.c indent.h io.c pr_comment.c

Log Message:
indent: use enum instead of magic numbers for tracking declarations

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.259 -r1.260 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.126 -r1.127 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.158 -r1.159 src/usr.bin/indent/io.c
cvs rdiff -u -r1.131 -r1.132 src/usr.bin/indent/pr_comment.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/indent/debug.c
diff -u src/usr.bin/indent/debug.c:1.2 src/usr.bin/indent/debug.c:1.3
--- src/usr.bin/indent/debug.c:1.2	Sat May 13 13:45:24 2023
+++ src/usr.bin/indent/debug.c	Sat May 13 14:30:48 2023
@@ -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);

Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.259 src/usr.bin/indent/indent.c:1.260
--- src/usr.bin/indent/indent.c:1.259	Sat May 13 12:31:02 2023
+++ src/usr.bin/indent/indent.c	Sat May 13 14:30:48 2023
@@ -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

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.126 src/usr.bin/indent/indent.h:1.127
--- src/usr.bin/indent/indent.h:1.126	Sat May 13 13:48:54 2023
+++ src/usr.bin/indent/indent.h	Sat May 13 14:30:48 2023
@@ -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 { ... }' */

Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.158 src/usr.bin/indent/io.c:1.159
--- src/usr.bin/indent/io.c:1.158	Sat May 13 13:48:54 2023
+++ src/usr.bin/indent/io.c	Sat May 13 14:30:48 2023
@@ -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 */

Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.131 src/usr.bin/indent/pr_comment.c:1.132
--- src/usr.bin/indent/pr_comment.c:1.131	Sat May 13 12:31:02 2023
+++ src/usr.bin/indent/pr_comment.c	Sat May 13 14:30:48 2023
@@ -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;
 }

Reply via email to