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: split 'main_loop' into several functions



details:   https://anonhg.NetBSD.org/src/rev/234b496af039
branches:  trunk
changeset: 1019521:234b496af039
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Mar 13 12:52:24 2021 +0000

description:
indent: split 'main_loop' into several functions

No functional change.

diffstat:

 usr.bin/indent/indent.c |  1356 +++++++++++++++++++++++++---------------------
 1 files changed, 748 insertions(+), 608 deletions(-)

diffs (truncated from 1469 to 300 lines):

diff -r 617ee20988f4 -r 234b496af039 usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Sat Mar 13 11:47:22 2021 +0000
+++ b/usr.bin/indent/indent.c   Sat Mar 13 12:52:24 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.53 2021/03/13 11:47:22 rillig Exp $       */
+/*     $NetBSD: indent.c,v 1.54 2021/03/13 12:52:24 rillig Exp $       */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -46,7 +46,7 @@
 #include <sys/cdefs.h>
 #ifndef lint
 #if defined(__NetBSD__)
-__RCSID("$NetBSD: indent.c,v 1.53 2021/03/13 11:47:22 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.54 2021/03/13 12:52:24 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -543,6 +543,719 @@
 }
 
 static void
+process_end_of_file(void)
+{
+    if (s_lab != e_lab || s_code != e_code || s_com != e_com)
+       dump_line();
+
+    if (ps.tos > 1)            /* check for balanced braces */
+       diag(1, "Stuff missing from end of file");
+
+    if (opt.verbose) {
+       printf("There were %d output lines and %d comments\n",
+              ps.out_lines, ps.out_coms);
+       printf("(Lines with comments)/(Lines with code): %6.3f\n",
+              (1.0 * ps.com_lines) / code_lines);
+    }
+
+    fflush(output);
+    exit(found_err);
+}
+
+static void
+process_comment_in_code(token_type type_code, int *inout_force_nl)
+{
+    if (*inout_force_nl &&
+       type_code != semicolon &&
+       (type_code != lbrace || !opt.btype_2)) {
+
+       /* we should force a broken line here */
+       if (opt.verbose)
+           diag(0, "Line broken");
+       dump_line();
+       ps.want_blank = false;  /* dont insert blank at line start */
+       *inout_force_nl = false;
+    }
+
+    ps.in_stmt = true;         /* turn on flag which causes an extra level of
+                                * indentation. this is turned off by a ; or
+                                * '}' */
+    if (s_com != e_com) {      /* the turkey has embedded a comment
+                                * in a line. fix it */
+       int len = e_com - s_com;
+
+       check_size_code(len + 3);
+       *e_code++ = ' ';
+       memcpy(e_code, s_com, len);
+       e_code += len;
+       *e_code++ = ' ';
+       *e_code = '\0';         /* null terminate code sect */
+       ps.want_blank = false;
+       e_com = s_com;
+    }
+}
+
+static void
+process_form_feed(void)
+{
+    ps.use_ff = true;          /* a form feed is treated much like a newline */
+    dump_line();
+    ps.want_blank = false;
+}
+
+static void
+process_newline(void)
+{
+    if (ps.last_token != comma || ps.p_l_follow > 0
+       || !opt.leave_comma || ps.block_init || !break_comma || s_com != e_com) {
+       dump_line();
+       ps.want_blank = false;
+    }
+    ++line_no;                 /* keep track of input line number */
+}
+
+static void
+process_lparen_or_lbracket(int dec_ind, int tabs_to_var, int sp_sw)
+{
+    /* count parens to make Healy happy */
+    if (++ps.p_l_follow == nitems(ps.paren_indents)) {
+       diag(0, "Reached internal limit of %zu unclosed parens",
+           nitems(ps.paren_indents));
+       ps.p_l_follow--;
+    }
+    if (*token == '[')
+       /* not a function pointer declaration or a function call */;
+    else if (ps.in_decl && !ps.block_init && !ps.dumped_decl_indent &&
+       ps.procname[0] == '\0' && ps.paren_level == 0) {
+       /* function pointer declarations */
+       indent_declaration(dec_ind, tabs_to_var);
+       ps.dumped_decl_indent = true;
+    } else if (ps.want_blank &&
+           ((ps.last_token != ident && ps.last_token != funcname) ||
+           opt.proc_calls_space ||
+           (ps.keyword == rw_sizeof ? opt.Bill_Shannon :
+           ps.keyword != rw_0 && ps.keyword != rw_offsetof)))
+       *e_code++ = ' ';
+    ps.want_blank = false;
+    *e_code++ = token[0];
+    ps.paren_indents[ps.p_l_follow - 1] =
+       indentation_after_range(0, s_code, e_code);
+    if (sp_sw && ps.p_l_follow == 1 && opt.extra_expression_indent
+           && ps.paren_indents[0] < 2 * opt.ind_size)
+       ps.paren_indents[0] = 2 * opt.ind_size;
+    if (ps.in_or_st && *token == '(' && ps.tos <= 2) {
+       /*
+        * this is a kluge to make sure that declarations will be
+        * aligned right if proc decl has an explicit type on it, i.e.
+        * "int a(x) {..."
+        */
+       parse(semicolon);       /* I said this was a kluge... */
+       ps.in_or_st = false;    /* turn off flag for structure decl or
+                                * initialization */
+    }
+    /* parenthesized type following sizeof or offsetof is not a cast */
+    if (ps.keyword == rw_offsetof || ps.keyword == rw_sizeof)
+       ps.not_cast_mask |= 1 << ps.p_l_follow;
+}
+
+static void
+process_rparen_or_rbracket(int *inout_sp_sw, int *inout_force_nl,
+                        token_type hd_type)
+{
+    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;
+       ps.want_blank = opt.space_after_cast;
+    } else
+       ps.want_blank = true;
+    ps.not_cast_mask &= (1 << ps.p_l_follow) - 1;
+
+    if (--ps.p_l_follow < 0) {
+       ps.p_l_follow = 0;
+       diag(0, "Extra %c", *token);
+    }
+
+    if (e_code == s_code)      /* if the paren starts the line */
+       ps.paren_level = ps.p_l_follow; /* then indent it */
+
+    *e_code++ = token[0];
+
+    if (*inout_sp_sw && (ps.p_l_follow == 0)) {        /* check for end of if
+                                * (...), or some such */
+       *inout_sp_sw = false;
+       *inout_force_nl = true; /* must force newline after if */
+       ps.last_u_d = true;     /* inform lexi that a following
+                                * operator is unary */
+       ps.in_stmt = false;     /* dont use stmt continuation indentation */
+
+       parse(hd_type);         /* let parser worry about if, or whatever */
+    }
+    ps.search_brace = opt.btype_2; /* this should ensure that constructs such
+                                * as main(){...} and int[]{...} have their
+                                * braces put in the right place */
+}
+
+static void
+process_unary_op(int dec_ind, int tabs_to_var)
+{
+    if (!ps.dumped_decl_indent && ps.in_decl && !ps.block_init &&
+       ps.procname[0] == '\0' && ps.paren_level == 0) {
+       /* pointer declarations */
+
+       /*
+        * if this is a unary op in a declaration, we should indent
+        * this token
+        */
+       int i;
+       for (i = 0; token[i]; ++i)
+           /* find length of token */;
+       indent_declaration(dec_ind - i, tabs_to_var);
+       ps.dumped_decl_indent = true;
+    } else if (ps.want_blank)
+       *e_code++ = ' ';
+
+    {
+       int len = e_token - s_token;
+
+       check_size_code(len);
+       memcpy(e_code, token, len);
+       e_code += len;
+    }
+    ps.want_blank = false;
+}
+
+static void
+process_binary_op(void)
+{
+    int len = e_token - s_token;
+
+    check_size_code(len + 1);
+    if (ps.want_blank)
+       *e_code++ = ' ';
+    memcpy(e_code, token, len);
+    e_code += len;
+
+    ps.want_blank = true;
+}
+
+static void
+process_postfix_op(void)
+{
+    *e_code++ = token[0];
+    *e_code++ = token[1];
+    ps.want_blank = true;
+}
+
+static void
+process_question(int *inout_squest)
+{
+    (*inout_squest)++;         /* this will be used when a later colon
+                                * appears so we can distinguish the
+                                * <c>?<n>:<n> construct */
+    if (ps.want_blank)
+       *e_code++ = ' ';
+    *e_code++ = '?';
+    ps.want_blank = true;
+}
+
+static void
+process_colon(int *inout_squest, int *inout_force_nl, int *inout_scase)
+{
+    if (*inout_squest > 0) {   /* it is part of the <c>?<n>: <n> construct */
+       --*inout_squest;
+       if (ps.want_blank)
+           *e_code++ = ' ';
+       *e_code++ = ':';
+       ps.want_blank = true;
+       return;
+    }
+    if (ps.in_or_st) {
+       *e_code++ = ':';
+       ps.want_blank = false;
+       return;
+    }
+    ps.in_stmt = false;                /* seeing a label does not imply we are in a
+                                * stmt */
+    /*
+     * turn everything so far into a label
+     */
+    {
+       int len = e_code - s_code;
+
+       check_size_label(len + 3);
+       memcpy(e_lab, s_code, len);
+       e_lab += len;
+       *e_lab++ = ':';
+       *e_lab = '\0';
+       e_code = s_code;
+    }
+    *inout_force_nl = ps.pcase = *inout_scase; /* ps.pcase will be used by
+                                                * dump_line to decide how to
+                                                * indent the label. force_nl
+                                                * will force a case n: to be
+                                                * on a line by itself */
+    *inout_scase = false;
+    ps.want_blank = false;
+}
+
+static void
+process_semicolon(int *inout_scase, int *inout_squest, int const dec_ind,
+                 int const tabs_to_var, int *inout_sp_sw,
+                 token_type const hd_type,
+                 int *inout_force_nl)
+{
+    if (ps.dec_nest == 0)
+       ps.in_or_st = false;    /* we are not in an initialization or
+                                * structure declaration */
+    *inout_scase = false; /* these will only need resetting in an error */
+    *inout_squest = 0;
+    if (ps.last_token == rparen)
+       ps.in_parameter_declaration = 0;
+    ps.cast_mask = 0;
+    ps.not_cast_mask = 0;
+    ps.block_init = 0;
+    ps.block_init_level = 0;
+    ps.just_saw_decl--;
+
+    if (ps.in_decl && s_code == e_code && !ps.block_init &&
+       !ps.dumped_decl_indent && ps.paren_level == 0) {
+       /* indent stray semicolons in declarations */
+       indent_declaration(dec_ind - 1, tabs_to_var);



Home | Main Index | Thread Index | Old Index