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: group lexer symbols by topic, sort pr...



details:   https://anonhg.NetBSD.org/src/rev/6f2d6629820f
branches:  trunk
changeset: 376295:6f2d6629820f
user:      rillig <rillig%NetBSD.org@localhost>
date:      Fri Jun 09 16:23:43 2023 +0000

description:
indent: group lexer symbols by topic, sort processing functions

No functional change.

diffstat:

 usr.bin/indent/indent.c |  682 ++++++++++++++++++++++++------------------------
 usr.bin/indent/indent.h |   28 +-
 2 files changed, 357 insertions(+), 353 deletions(-)

diffs (truncated from 858 to 300 lines):

diff -r a0ef19b5b380 -r 6f2d6629820f usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Fri Jun 09 15:36:31 2023 +0000
+++ b/usr.bin/indent/indent.c   Fri Jun 09 16:23:43 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.346 2023/06/09 11:22:31 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.347 2023/06/09 16:23:43 rillig Exp $      */
 
 /*-
  * SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.346 2023/06/09 11:22:31 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.347 2023/06/09 16:23:43 rillig Exp $");
 
 #include <sys/param.h>
 #include <err.h>
@@ -185,6 +185,23 @@ init_globals(void)
                backup_suffix = suffix;
 }
 
+static void
+load_profiles(int argc, char **argv)
+{
+       const char *profile_name = NULL;
+
+       for (int i = 1; i < argc; ++i) {
+               const char *arg = argv[i];
+
+               if (strcmp(arg, "-npro") == 0)
+                       return;
+               if (arg[0] == '-' && arg[1] == 'P' && arg[2] != '\0')
+                       profile_name = arg + 2;
+       }
+
+       load_profile_files(profile_name);
+}
+
 /*
  * Copy the input file to the backup file, then make the backup file the input
  * and the original input file the output.
@@ -227,23 +244,6 @@ bakcopy(void)
 }
 
 static void
-load_profiles(int argc, char **argv)
-{
-       const char *profile_name = NULL;
-
-       for (int i = 1; i < argc; ++i) {
-               const char *arg = argv[i];
-
-               if (strcmp(arg, "-npro") == 0)
-                       return;
-               if (arg[0] == '-' && arg[1] == 'P' && arg[2] != '\0')
-                       profile_name = arg + 2;
-       }
-
-       load_profile_files(profile_name);
-}
-
-static void
 parse_command_line(int argc, char **argv)
 {
        for (int i = 1; i < argc; ++i) {
@@ -308,58 +308,6 @@ set_initial_indentation(void)
 }
 
 static void
-indent_declarator(int decl_ind, bool tabs_to_var)
-{
-       int base = ps.ind_level * opt.indent_size;
-       int ind = base + (int)code.len;
-       int target = base + decl_ind;
-       size_t orig_code_len = code.len;
-
-       if (tabs_to_var)
-               for (int next; (next = next_tab(ind)) <= target; ind = next)
-                       buf_add_char(&code, '\t');
-
-       for (; ind < target; ind++)
-               buf_add_char(&code, ' ');
-
-       if (code.len == orig_code_len && ps.want_blank) {
-               buf_add_char(&code, ' ');
-               ps.want_blank = false;
-       }
-       ps.decl_indent_done = true;
-}
-
-static void
-update_ps_lbrace_kind(lexer_symbol lsym)
-{
-       if (lsym == lsym_tag) {
-               ps.lbrace_kind = token.s[0] == 's' ? psym_lbrace_struct :
-                   token.s[0] == 'u' ? psym_lbrace_union :
-                   psym_lbrace_enum;
-       } else if (lsym != lsym_type_outside_parentheses
-           && lsym != lsym_word
-           && lsym != lsym_lbrace)
-               ps.lbrace_kind = psym_lbrace_block;
-}
-
-static int
-process_eof(void)
-{
-       if (lab.len > 0 || code.len > 0 || com.len > 0)
-               output_line();
-       if (indent_enabled != indent_on) {
-               indent_enabled = indent_last_off_line;
-               output_line();
-       }
-
-       if (ps.psyms.top > 1)   /* check for balanced braces */
-               diag(1, "Stuff missing from end of file");
-
-       fflush(output);
-       return found_err ? EXIT_FAILURE : EXIT_SUCCESS;
-}
-
-static void
 maybe_break_line(lexer_symbol lsym)
 {
        if (!ps.force_nl)
@@ -385,6 +333,154 @@ move_com_to_code(lexer_symbol lsym)
 }
 
 static void
+update_ps_lbrace_kind(lexer_symbol lsym)
+{
+       if (lsym == lsym_tag) {
+               ps.lbrace_kind = token.s[0] == 's' ? psym_lbrace_struct :
+                   token.s[0] == 'u' ? psym_lbrace_union :
+                   psym_lbrace_enum;
+       } else if (lsym != lsym_type_outside_parentheses
+           && lsym != lsym_word
+           && lsym != lsym_lbrace)
+               ps.lbrace_kind = psym_lbrace_block;
+}
+
+static void
+indent_declarator(int decl_ind, bool tabs_to_var)
+{
+       int base = ps.ind_level * opt.indent_size;
+       int ind = base + (int)code.len;
+       int target = base + decl_ind;
+       size_t orig_code_len = code.len;
+
+       if (tabs_to_var)
+               for (int next; (next = next_tab(ind)) <= target; ind = next)
+                       buf_add_char(&code, '\t');
+
+       for (; ind < target; ind++)
+               buf_add_char(&code, ' ');
+
+       if (code.len == orig_code_len && ps.want_blank) {
+               buf_add_char(&code, ' ');
+               ps.want_blank = false;
+       }
+       ps.decl_indent_done = true;
+}
+
+static bool
+is_function_pointer_declaration(void)
+{
+       return ps.in_decl
+           && !ps.block_init
+           && !ps.decl_indent_done
+           && !ps.is_function_definition
+           && ps.line_start_nparen == 0;
+}
+
+static int
+process_eof(void)
+{
+       if (lab.len > 0 || code.len > 0 || com.len > 0)
+               output_line();
+       if (indent_enabled != indent_on) {
+               indent_enabled = indent_last_off_line;
+               output_line();
+       }
+
+       if (ps.psyms.top > 1)   /* check for balanced braces */
+               diag(1, "Stuff missing from end of file");
+
+       fflush(output);
+       return found_err ? EXIT_FAILURE : EXIT_SUCCESS;
+}
+
+/* move the whole line to the 'label' buffer */
+static void
+read_preprocessing_line(void)
+{
+       enum {
+               PLAIN, STR, CHR, COMM
+       } state = PLAIN;
+
+       buf_add_char(&lab, '#');
+
+       while (inp_p[0] != '\n' || (state == COMM && !had_eof)) {
+               buf_add_char(&lab, inp_next());
+               switch (lab.s[lab.len - 1]) {
+               case '\\':
+                       if (state != COMM)
+                               buf_add_char(&lab, inp_next());
+                       break;
+               case '/':
+                       if (inp_p[0] == '*' && state == PLAIN) {
+                               state = COMM;
+                               buf_add_char(&lab, *inp_p++);
+                       }
+                       break;
+               case '"':
+                       if (state == STR)
+                               state = PLAIN;
+                       else if (state == PLAIN)
+                               state = STR;
+                       break;
+               case '\'':
+                       if (state == CHR)
+                               state = PLAIN;
+                       else if (state == PLAIN)
+                               state = CHR;
+                       break;
+               case '*':
+                       if (inp_p[0] == '/' && state == COMM) {
+                               state = PLAIN;
+                               buf_add_char(&lab, *inp_p++);
+                       }
+                       break;
+               }
+       }
+
+       while (lab.len > 0 && ch_isblank(lab.s[lab.len - 1]))
+               lab.len--;
+}
+
+static void
+process_preprocessing(void)
+{
+       if (lab.len > 0 || code.len > 0 || com.len > 0)
+               output_line();
+
+       read_preprocessing_line();
+
+       const char *dir = lab.s + 1, *line_end = lab.s + lab.len;
+       while (dir < line_end && ch_isblank(*dir))
+               dir++;
+       size_t dir_len = 0;
+       while (dir + dir_len < line_end && ch_isalpha(dir[dir_len]))
+               dir_len++;
+
+       if (dir_len >= 2 && memcmp(dir, "if", 2) == 0) {
+               if ((size_t)ifdef_level < array_length(state_stack))
+                       state_stack[ifdef_level++] = ps;
+               else
+                       diag(1, "#if stack overflow");
+               out.line_kind = lk_if;
+
+       } else if (dir_len >= 2 && memcmp(dir, "el", 2) == 0) {
+               if (ifdef_level <= 0)
+                       diag(1, dir[2] == 'i'
+                           ? "Unmatched #elif" : "Unmatched #else");
+               else
+                       ps = state_stack[ifdef_level - 1];
+
+       } else if (dir_len == 5 && memcmp(dir, "endif", 5) == 0) {
+               if (ifdef_level <= 0)
+                       diag(1, "Unmatched #endif");
+               else
+                       ifdef_level--;
+               out.line_kind = lk_endif;
+       }
+}
+
+static void
 process_newline(void)
 {
        if (ps.prev_lsym == lsym_comma
@@ -406,16 +502,6 @@ stay_in_line:
 }
 
 static bool
-is_function_pointer_declaration(void)
-{
-       return ps.in_decl
-           && !ps.block_init
-           && !ps.decl_indent_done
-           && !ps.is_function_definition
-           && ps.line_start_nparen == 0;
-}
-
-static bool
 want_blank_before_lparen(void)
 {
        if (!ps.want_blank)
@@ -477,28 +563,6 @@ process_lparen(void)
 }
 



Home | Main Index | Thread Index | Old Index