Module Name:    src
Committed By:   rillig
Date:           Tue Oct 26 20:43:35 UTC 2021

Modified Files:
        src/usr.bin/indent: indent.c indent.h lexi.c

Log Message:
indent: make ps.keyword easier to understand

Previously, ps.keyword did not have any documentation and was not
straight-forward. In some cases it was reset to kw_0, in others it was
set to an interesting value. The idea behind it was to remember the kind
of word of the previous token, to decide whether to have a space between
sizeof or offsetof and a following '('.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.160 -r1.161 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.51 -r1.52 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.104 -r1.105 src/usr.bin/indent/lexi.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/indent.c
diff -u src/usr.bin/indent/indent.c:1.160 src/usr.bin/indent/indent.c:1.161
--- src/usr.bin/indent/indent.c:1.160	Tue Oct 26 19:36:30 2021
+++ src/usr.bin/indent/indent.c	Tue Oct 26 20:43:35 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.c,v 1.160 2021/10/26 19:36:30 rillig Exp $	*/
+/*	$NetBSD: indent.c,v 1.161 2021/10/26 20:43:35 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.160 2021/10/26 19:36:30 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.161 2021/10/26 20:43:35 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -333,8 +333,7 @@ search_stmt_lookahead(lexer_symbol *lsym
 	    inbuf_read_line();
     }
 
-    struct parser_state transient_state;
-    transient_state = ps;
+    struct parser_state transient_state = ps;
     *lsym = lexi(&transient_state);	/* read another token */
     if (*lsym != lsym_newline && *lsym != lsym_form_feed &&
 	*lsym != lsym_comment && !transient_state.search_stmt) {
@@ -707,9 +706,9 @@ want_blank_before_lparen(void)
 	return true;
     if (opt.proc_calls_space)
 	return true;
-    if (ps.keyword == kw_sizeof)
+    if (ps.prev_keyword == kw_sizeof)
 	return opt.blank_after_sizeof;
-    return ps.keyword != kw_0 && ps.keyword != kw_offsetof;
+    return ps.prev_keyword != kw_0 && ps.prev_keyword != kw_offsetof;
 }
 
 static void
@@ -753,7 +752,7 @@ process_lparen_or_lbracket(int decl_ind,
     }
 
     /* parenthesized type following sizeof or offsetof is not a cast */
-    if (ps.keyword == kw_offsetof || ps.keyword == kw_sizeof)
+    if (ps.prev_keyword == kw_offsetof || ps.prev_keyword == kw_sizeof)
 	ps.not_cast_mask |= 1 << ps.p_l_follow;
 }
 

Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.51 src/usr.bin/indent/indent.h:1.52
--- src/usr.bin/indent/indent.h:1.51	Tue Oct 26 19:36:30 2021
+++ src/usr.bin/indent/indent.h	Tue Oct 26 20:43:35 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: indent.h,v 1.51 2021/10/26 19:36:30 rillig Exp $	*/
+/*	$NetBSD: indent.h,v 1.52 2021/10/26 20:43:35 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -332,7 +332,8 @@ extern struct parser_state {
     bool want_blank;		/* whether the following token should be
 				 * prefixed by a blank. (Said prefixing is
 				 * ignored in some cases.) */
-    enum keyword_kind keyword;
+    enum keyword_kind prev_keyword;
+    enum keyword_kind curr_keyword;
     bool dumped_decl_indent;
     bool in_parameter_declaration;
     char procname[100];		/* The name of the current procedure */

Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.104 src/usr.bin/indent/lexi.c:1.105
--- src/usr.bin/indent/lexi.c:1.104	Tue Oct 26 20:17:42 2021
+++ src/usr.bin/indent/lexi.c	Tue Oct 26 20:43:35 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: lexi.c,v 1.104 2021/10/26 20:17:42 rillig Exp $	*/
+/*	$NetBSD: lexi.c,v 1.105 2021/10/26 20:43:35 rillig Exp $	*/
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -43,7 +43,7 @@ static char sccsid[] = "@(#)lexi.c	8.1 (
 
 #include <sys/cdefs.h>
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: lexi.c,v 1.104 2021/10/26 20:17:42 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.105 2021/10/26 20:43:35 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/lexi.c 337862 2018-08-15 18:19:45Z pstef $");
 #endif
@@ -305,8 +305,10 @@ debug_lexi(const struct parser_state *st
     debug_print_buf("code", &code);
     debug_print_buf("comment", &com);
     debug_printf("lexi returns '%s'", lsym_name(lsym));
-    if (state->keyword != kw_0)
-	debug_printf(" keyword '%s'", kw_name(state->keyword));
+    if (state->curr_keyword != kw_0)
+	debug_printf(" keyword '%s'", kw_name(state->curr_keyword));
+    if (state->prev_keyword != kw_0)
+	debug_printf(" previous keyword '%s'", kw_name(state->prev_keyword));
     debug_println("");
     debug_print_buf("token", &token);
 }
@@ -455,7 +457,6 @@ lexi_alnum(struct parser_state *state)
 
     while (is_hspace(inbuf_peek()))
 	inbuf_skip();
-    state->keyword = kw_0;
 
     if (state->last_token == lsym_tag && state->p_l_follow == 0) {
 	state->next_unary = true;
@@ -469,13 +470,13 @@ lexi_alnum(struct parser_state *state)
 	array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
     if (kw == NULL) {
 	if (is_typename()) {
-	    state->keyword = kw_type;
+	    state->curr_keyword = kw_type;
 	    state->next_unary = true;
 	    goto found_typename;
 	}
 
     } else {			/* we have a keyword */
-	state->keyword = kw->kind;
+	state->curr_keyword = kw->kind;
 	state->next_unary = true;
 
 	switch (kw->kind) {
@@ -493,10 +494,8 @@ lexi_alnum(struct parser_state *state)
 		state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask;
 	    }
 	    if (state->last_token == lsym_period ||
-		    state->last_token == lsym_unary_op) {
-		state->keyword = kw_0;
+		    state->last_token == lsym_unary_op)
 		break;
-	    }
 	    if (kw != NULL && kw->kind == kw_struct_or_union_or_enum)
 		return lsym_tag;
 	    if (state->p_l_follow != 0)
@@ -544,7 +543,7 @@ lexi_alnum(struct parser_state *state)
 not_proc:;
 
     } else if (probably_typename(state)) {
-	state->keyword = kw_type;
+	state->curr_keyword = kw_type;
 	state->next_unary = true;
 	return lsym_type;
     }
@@ -563,6 +562,8 @@ lexi(struct parser_state *state)
     token.e = token.s;
     state->col_1 = state->last_nl;
     state->last_nl = false;
+    state->prev_keyword = state->curr_keyword;
+    state->curr_keyword = kw_0;
 
     while (is_hspace(*inp.s)) {
 	state->col_1 = false;

Reply via email to