From 72a53430c7b72293f68d675a5a1c1848d4d682dd Mon Sep 17 00:00:00 2001 From: pstef Date: Sun, 3 Jun 2018 16:21:15 +0000 Subject: [PATCH] indent(1): disjoint parser state from lexi() The function is sometimes used as a look-ahead, so ideally it should bear no information about parser state. --- usr.bin/indent/indent.c | 3 ++ usr.bin/indent/indent_codes.h | 2 +- usr.bin/indent/lexi.c | 42 +++++++++------------------- usr.bin/indent/tests/struct.0 | 8 ++++++ usr.bin/indent/tests/struct.0.stdout | 10 +++++++ 5 files changed, 35 insertions(+), 30 deletions(-) diff --git a/usr.bin/indent/indent.c b/usr.bin/indent/indent.c index d19d2c100dfd..1d55435fbaf1 100644 --- a/usr.bin/indent/indent.c +++ b/usr.bin/indent/indent.c @@ -995,6 +995,9 @@ main(int argc, char **argv) prefix_blankline_requested = 0; goto copy_id; + case structure: + if (ps.p_l_follow > 0) + goto copy_id; case decl: /* we have a declaration type (int, etc.) */ parse(decl); /* let parser worry about indentation */ if (ps.last_token == rparen && ps.tos <= 1) { diff --git a/usr.bin/indent/indent_codes.h b/usr.bin/indent/indent_codes.h index 5e7c37f4af35..0d043279c4e1 100644 --- a/usr.bin/indent/indent_codes.h +++ b/usr.bin/indent/indent_codes.h @@ -74,4 +74,4 @@ #define storage 34 #define funcname 35 #define type_def 36 - +#define structure 37 diff --git a/usr.bin/indent/lexi.c b/usr.bin/indent/lexi.c index ef76776f2ca4..1cc6ff19e2e6 100644 --- a/usr.bin/indent/lexi.c +++ b/usr.bin/indent/lexi.c @@ -145,8 +145,6 @@ lexi(struct parser_state *state) { int unary_delim; /* this is set to 1 if the current token * forces a following operator to be unary */ - static int last_code; /* the last token type returned */ - static int l_struct; /* set to 1 if the last token was 'struct' */ int code; /* internal code to be returned */ char qchar; /* the delimiter character for a string */ @@ -283,21 +281,17 @@ lexi(struct parser_state *state) fill_buffer(); } state->keyword = 0; - if (l_struct && !state->p_l_follow) { + if (state->last_token == structure && !state->p_l_follow) { /* if last token was 'struct' and we're not * in parentheses, then this token * should be treated as a declaration */ - l_struct = false; - last_code = ident; state->last_u_d = true; return (decl); } - state->last_u_d = l_struct; /* Operator after identifier is - * binary unless last token was - * 'struct' */ - l_struct = false; - last_code = ident; /* Remember that this is the code we will - * return */ + /* + * Operator after identifier is binary unless last token was 'struct' + */ + state->last_u_d = (state->last_token == structure); p = bsearch(s_token, specials, @@ -326,21 +320,17 @@ lexi(struct parser_state *state) return (casestmt); case 3: /* a "struct" */ - /* - * Next time around, we will want to know that we have had a - * 'struct' - */ - l_struct = true; /* FALLTHROUGH */ - case 4: /* one of the declaration keywords */ found_typename: if (state->p_l_follow) { /* inside parens: cast, param list, offsetof or sizeof */ state->cast_mask |= (1 << state->p_l_follow) & ~state->not_cast_mask; - break; } - last_code = decl; + if (p != NULL && p->rwcode == 3) + return (structure); + if (state->p_l_follow) + break; return (decl); case 5: /* if, while, for */ @@ -369,7 +359,7 @@ lexi(struct parser_state *state) strncpy(state->procname, token, sizeof state->procname - 1); if (state->in_decl) state->in_parameter_declaration = 1; - return (last_code = funcname); + return (funcname); not_proc:; } /* @@ -385,13 +375,11 @@ lexi(struct parser_state *state) state->last_token == lbrace || state->last_token == rbrace)) { state->keyword = 4; /* a type name */ state->last_u_d = true; - last_code = decl; return decl; } - if (last_code == decl) /* if this is a declared variable, then - * following sign is unary */ + if (state->last_token == decl) /* if this is a declared variable, + * then following sign is unary */ state->last_u_d = true; /* will make "int a -1" work */ - last_code = ident; return (ident); /* the ident is not in the list */ } /* end of procesing for alpanum character */ @@ -536,7 +524,7 @@ lexi(struct parser_state *state) /* check for doubled character */ *e_token++ = *buf_ptr++; /* buffer overflow will be checked at end of loop */ - if (last_code == ident || last_code == rparen) { + if (state->last_token == ident || state->last_token == rparen) { code = (state->last_u_d ? unary_op : postop); /* check for following ++ or -- */ unary_delim = false; @@ -617,10 +605,6 @@ lexi(struct parser_state *state) } /* end of switch */ - if (code != newline) { - l_struct = false; - last_code = code; - } if (buf_ptr >= buf_end) /* check for input buffer empty */ fill_buffer(); state->last_u_d = unary_delim; diff --git a/usr.bin/indent/tests/struct.0 b/usr.bin/indent/tests/struct.0 index 9dfc31de9a03..83142bfb1972 100644 --- a/usr.bin/indent/tests/struct.0 +++ b/usr.bin/indent/tests/struct.0 @@ -1,4 +1,7 @@ /* $FreeBSD$ */ + +int f(struct x *a); + /* See r303485 */ void t(void) @@ -11,3 +14,8 @@ t(void) { F, G } }; } + +void u(struct x a) { + int b; + struct y c = (struct y *)&a; +} diff --git a/usr.bin/indent/tests/struct.0.stdout b/usr.bin/indent/tests/struct.0.stdout index ef62a12e8f8e..38613128654f 100644 --- a/usr.bin/indent/tests/struct.0.stdout +++ b/usr.bin/indent/tests/struct.0.stdout @@ -1,4 +1,7 @@ /* $FreeBSD$ */ + +int f(struct x *a); + /* See r303485 */ void t(void) @@ -11,3 +14,10 @@ t(void) {F, G} }; } + +void +u(struct x a) +{ + int b; + struct y c = (struct y *)&a; +}