From 8c329c871dd82b63f93a89031310a389955b9670 Mon Sep 17 00:00:00 2001 From: pstef Date: Sun, 23 Jul 2017 14:04:45 +0000 Subject: [PATCH] indent(1): don't produce unneeded space character in function declarators. --- usr.bin/indent/indent.c | 19 ++++--- usr.bin/indent/indent_codes.h | 1 + usr.bin/indent/indent_globs.h | 1 - usr.bin/indent/lexi.c | 5 +- usr.bin/indent/tests/binary.0.stdout | 2 +- usr.bin/indent/tests/comments.0.stdout | 2 +- usr.bin/indent/tests/declarations.0 | 57 ++++++++++++++++++++- usr.bin/indent/tests/declarations.0.stdout | 58 +++++++++++++++++++++- usr.bin/indent/tests/elsecomment.0.stdout | 2 +- usr.bin/indent/tests/float.0.stdout | 2 +- usr.bin/indent/tests/label.0.stdout | 2 +- usr.bin/indent/tests/nsac.0.stdout | 2 +- usr.bin/indent/tests/offsetof.0.stdout | 2 +- usr.bin/indent/tests/sac.0.stdout | 2 +- 14 files changed, 135 insertions(+), 22 deletions(-) diff --git a/usr.bin/indent/indent.c b/usr.bin/indent/indent.c index 66d558557b55..2b0b1a80b88d 100644 --- a/usr.bin/indent/indent.c +++ b/usr.bin/indent/indent.c @@ -535,11 +535,12 @@ check_type: ps.p_l_follow--; } if (ps.want_blank && *token != '[' && - (ps.last_token != ident || proc_calls_space || + ((ps.last_token != ident && ps.last_token != funcname) || + proc_calls_space || /* offsetof (1) is never allowed a space; sizeof (2) gets * one iff -bs; all other keywords (>2) always get a space * before lparen */ - (ps.keyword + Bill_Shannon > 2))) + ps.keyword + Bill_Shannon > 2)) *e_code++ = ' '; ps.want_blank = false; if (ps.in_decl && !ps.block_init && !ps.dumped_decl_indent && @@ -576,7 +577,6 @@ check_type: break; case rparen: /* got a ')' or ']' */ - rparen_count--; if (ps.cast_mask & (1 << ps.p_l_follow) & ~ps.not_cast_mask) { ps.last_u_d = true; ps.cast_mask &= (1 << ps.p_l_follow) - 1; @@ -738,7 +738,7 @@ check_type: * structure declaration */ scase = false; /* these will only need resetting in an error */ squest = 0; - if (ps.last_token == rparen && rparen_count == 0) + if (ps.last_token == rparen) ps.in_parameter_declaration = 0; ps.cast_mask = 0; ps.not_cast_mask = 0; @@ -838,6 +838,7 @@ check_type: && ps.in_parameter_declaration) postfix_blankline_requested = 1; ps.in_parameter_declaration = 0; + ps.in_decl = false; } dec_ind = 0; parse(lbrace); /* let parser know about this */ @@ -935,7 +936,6 @@ check_type: case decl: /* we have a declaration type (int, etc.) */ parse(decl); /* let parser worry about indentation */ if (ps.last_token == rparen && ps.tos <= 1) { - ps.in_parameter_declaration = 1; if (s_code != e_code) { dump_line(); ps.want_blank = 0; @@ -964,10 +964,11 @@ check_type: } goto copy_id; + case funcname: case ident: /* got an identifier or constant */ if (ps.in_decl) { /* if we are in a declaration, we must indent * identifier */ - if (is_procname == 0 || !procnames_start_line) { + if (type_code != funcname || !procnames_start_line) { if (!ps.block_init && !ps.dumped_decl_indent) { if (troff) { if (ps.want_blank) @@ -980,7 +981,8 @@ check_type: ps.want_blank = false; } } else { - if (ps.want_blank) + if (ps.want_blank && !(procnames_start_line && + type_code == funcname)) *e_code++ = ' '; ps.want_blank = false; if (dec_ind && s_code != e_code) { @@ -1014,7 +1016,8 @@ check_type: CHECK_SIZE_CODE; *e_code++ = *t_ptr; } - ps.want_blank = true; + if (type_code != funcname) + ps.want_blank = true; break; case strpfx: diff --git a/usr.bin/indent/indent_codes.h b/usr.bin/indent/indent_codes.h index 54fdc821b64f..7b23997b6e70 100644 --- a/usr.bin/indent/indent_codes.h +++ b/usr.bin/indent/indent_codes.h @@ -70,3 +70,4 @@ #define period 32 #define strpfx 33 #define storage 34 +#define funcname 35 diff --git a/usr.bin/indent/indent_globs.h b/usr.bin/indent/indent_globs.h index 1a688103dfa1..1c09208c0e5a 100644 --- a/usr.bin/indent/indent_globs.h +++ b/usr.bin/indent/indent_globs.h @@ -330,6 +330,5 @@ struct parser_state { } ps; int ifdef_level; -int rparen_count; struct parser_state state_stack[5]; struct parser_state match_state[5]; diff --git a/usr.bin/indent/lexi.c b/usr.bin/indent/lexi.c index 6deb55ae6853..8634591018a2 100644 --- a/usr.bin/indent/lexi.c +++ b/usr.bin/indent/lexi.c @@ -353,7 +353,8 @@ lexi(void) return (ident); } /* end of switch */ } /* end of if (found_it) */ - if (*buf_ptr == '(' && ps.tos <= 1 && ps.ind_level == 0) { + if (*buf_ptr == '(' && ps.tos <= 1 && ps.ind_level == 0 && + ps.in_parameter_declaration == 0 && ps.block_init == 0) { char *tp = buf_ptr; while (tp < buf_end) if (*tp++ == ')' && (*tp == ';' || *tp == ',')) @@ -361,7 +362,7 @@ lexi(void) strncpy(ps.procname, token, sizeof ps.procname - 1); if (ps.in_decl) ps.in_parameter_declaration = 1; - rparen_count = 1; + return (last_code = funcname); not_proc:; } /* diff --git a/usr.bin/indent/tests/binary.0.stdout b/usr.bin/indent/tests/binary.0.stdout index a883ed9fcd06..5bb9a31a3832 100644 --- a/usr.bin/indent/tests/binary.0.stdout +++ b/usr.bin/indent/tests/binary.0.stdout @@ -1,6 +1,6 @@ /* $FreeBSD$ */ #define b00101010 -1 -void +void t(void) { unsigned a[] = {0b00101010, 0x00005678, 02, 17U}; diff --git a/usr.bin/indent/tests/comments.0.stdout b/usr.bin/indent/tests/comments.0.stdout index 62417bd5e99f..69e119a6ba7c 100644 --- a/usr.bin/indent/tests/comments.0.stdout +++ b/usr.bin/indent/tests/comments.0.stdout @@ -1,6 +1,6 @@ /* $FreeBSD$ */ /* See r303597, r303598, r309219, and r309343 */ -void +void t(void) { /* diff --git a/usr.bin/indent/tests/declarations.0 b/usr.bin/indent/tests/declarations.0 index 31183ac255b7..a5482846ec02 100644 --- a/usr.bin/indent/tests/declarations.0 +++ b/usr.bin/indent/tests/declarations.0 @@ -1,6 +1,32 @@ /* $FreeBSD$ */ /* See r303570 */ -void t(void) { + +static const struct +{ + double x; + double y, z; +} n[m + 1] = +{ + { + .0, + .9, + 5 + } +}; + +typedef struct Complex +{ + double x; + double y; +} Complex; + +void +t1 (char *a, int b, + void (*fn)(void)) +{} + +void t2 (char *x, int y) +{ int a, b, c; @@ -19,3 +45,32 @@ void t(void) { ,o ; } + +const int int_minimum_size = +MAXALIGN(offsetof(int, test)) + MAXIMUM_ALIGNOF; + +int *int_create(void) +{ + +} + +static +_attribute_printf(1, 2) +void +print_error(const char *fmt,...) +{ + +} + +static LIST_HEAD(, alq) ald_active; +static int ald_shutingdown = 0; +struct thread *ald_thread; + +static int +do_execve(td, args, mac_p) + struct thread *td; + struct image_args *args; + struct mac *mac_p; +{ + +} diff --git a/usr.bin/indent/tests/declarations.0.stdout b/usr.bin/indent/tests/declarations.0.stdout index 972dd3806dc6..9c8384255d8b 100644 --- a/usr.bin/indent/tests/declarations.0.stdout +++ b/usr.bin/indent/tests/declarations.0.stdout @@ -1,7 +1,31 @@ /* $FreeBSD$ */ /* See r303570 */ -void -t(void) + +static const struct { + double x; + double y, z; +} n[m + 1] = +{ + { + .0, + .9, + 5 + } +}; + +typedef struct Complex { + double x; + double y; +} Complex; + +void +t1(char *a, int b, + void (*fn) (void)) +{ +} + +void +t2(char *x, int y) { int a, b, c; int @@ -13,3 +37,33 @@ t(void) ,o ; } + +const int int_minimum_size = +MAXALIGN(offsetof(int, test)) + MAXIMUM_ALIGNOF; + +int * +int_create(void) +{ + +} + +static +_attribute_printf(1, 2) +void +print_error(const char *fmt,...) +{ + +} + +static LIST_HEAD(, alq) ald_active; +static int ald_shutingdown = 0; +struct thread *ald_thread; + +static int +do_execve(td, args, mac_p) + struct thread *td; + struct image_args *args; + struct mac *mac_p; +{ + +} diff --git a/usr.bin/indent/tests/elsecomment.0.stdout b/usr.bin/indent/tests/elsecomment.0.stdout index ce78e1701e4c..5977ebaf0519 100644 --- a/usr.bin/indent/tests/elsecomment.0.stdout +++ b/usr.bin/indent/tests/elsecomment.0.stdout @@ -1,6 +1,6 @@ /* $FreeBSD$ */ /* See r303484 and r309342 */ -void +void t(void) { if (0) diff --git a/usr.bin/indent/tests/float.0.stdout b/usr.bin/indent/tests/float.0.stdout index 03d92c316dea..0f213182ff9b 100644 --- a/usr.bin/indent/tests/float.0.stdout +++ b/usr.bin/indent/tests/float.0.stdout @@ -1,6 +1,6 @@ /* $FreeBSD$ */ /* See r303499 */ -void +void t(void) { unsigned long x = 314UL; diff --git a/usr.bin/indent/tests/label.0.stdout b/usr.bin/indent/tests/label.0.stdout index 99314f2aeda4..22ec12272af3 100644 --- a/usr.bin/indent/tests/label.0.stdout +++ b/usr.bin/indent/tests/label.0.stdout @@ -1,6 +1,6 @@ /* $FreeBSD$ */ /* See r303489 */ -void +void t(void) { switch (1) { diff --git a/usr.bin/indent/tests/nsac.0.stdout b/usr.bin/indent/tests/nsac.0.stdout index 16209319c476..78f3b287574c 100644 --- a/usr.bin/indent/tests/nsac.0.stdout +++ b/usr.bin/indent/tests/nsac.0.stdout @@ -1,5 +1,5 @@ /* $FreeBSD$ */ -void +void t(void) { int a = (double)8; diff --git a/usr.bin/indent/tests/offsetof.0.stdout b/usr.bin/indent/tests/offsetof.0.stdout index d9f16d93e1d5..199bf0fa0755 100644 --- a/usr.bin/indent/tests/offsetof.0.stdout +++ b/usr.bin/indent/tests/offsetof.0.stdout @@ -1,6 +1,6 @@ /* $FreeBSD$ */ /* See r303718 */ -void +void t(void) { int n = malloc(offsetof(struct s, f) + 1); diff --git a/usr.bin/indent/tests/sac.0.stdout b/usr.bin/indent/tests/sac.0.stdout index a0e2e71362ce..1849b2833793 100644 --- a/usr.bin/indent/tests/sac.0.stdout +++ b/usr.bin/indent/tests/sac.0.stdout @@ -1,5 +1,5 @@ /* $FreeBSD$ */ -void +void t(void) { int a = (double) 8;