Module Name: src Committed By: rillig Date: Wed Jun 14 09:31:05 UTC 2023
Modified Files: src/tests/usr.bin/indent: lsym_comment.c psym_decl.c src/usr.bin/indent: debug.c indent.h io.c pr_comment.c Log Message: indent: clean up handling of comments One less moving part in the parser state. No functional change. To generate a diff of this commit: cvs rdiff -u -r1.20 -r1.21 src/tests/usr.bin/indent/lsym_comment.c cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/indent/psym_decl.c cvs rdiff -u -r1.56 -r1.57 src/usr.bin/indent/debug.c cvs rdiff -u -r1.191 -r1.192 src/usr.bin/indent/indent.h cvs rdiff -u -r1.217 -r1.218 src/usr.bin/indent/io.c cvs rdiff -u -r1.163 -r1.164 src/usr.bin/indent/pr_comment.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/usr.bin/indent/lsym_comment.c diff -u src/tests/usr.bin/indent/lsym_comment.c:1.20 src/tests/usr.bin/indent/lsym_comment.c:1.21 --- src/tests/usr.bin/indent/lsym_comment.c:1.20 Sat Jun 10 16:43:56 2023 +++ src/tests/usr.bin/indent/lsym_comment.c Wed Jun 14 09:31:05 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: lsym_comment.c,v 1.20 2023/06/10 16:43:56 rillig Exp $ */ +/* $NetBSD: lsym_comment.c,v 1.21 2023/06/14 09:31:05 rillig Exp $ */ /* * Tests for the token lsym_comment, which starts a comment. @@ -43,7 +43,6 @@ * - with varying opt.comment_column (-c0, -c1, -c33, -c80) * - with varying opt.decl_comment_column (-cd0, -cd1, -cd20, -cd33, -cd80) * - with/without ps.line_has_decl - * - with/without ps.next_col_1 * * - very long comments that overflow the buffer 'com' * - comments that come from save_com @@ -1020,8 +1019,8 @@ f(void) /* - * Test two completely empty lines in a wrap comment. The second empty line - * covers the condition ps.next_col_1 in copy_comment_wrap. + * In a comment that is wrapped, one or more empty lines separate paragraphs. + * All of these empty lines are preserved. */ //indent input /* line 1 @@ -1131,3 +1130,17 @@ int line; // comment line 1 // comment line 2 int block; /* comment line 1 comment line 2 */ //indent end + + +//indent input +/*/ comment? or:not; /* */ +//indent end + +//indent run +/* / comment? or:not; /* */ +//indent end + +//indent run -nfc1 +// $ FIXME: It's a comment, not code. +/*/ comment ? or : not; /* */ +//indent end Index: src/tests/usr.bin/indent/psym_decl.c diff -u src/tests/usr.bin/indent/psym_decl.c:1.4 src/tests/usr.bin/indent/psym_decl.c:1.5 --- src/tests/usr.bin/indent/psym_decl.c:1.4 Sun Apr 24 10:36:37 2022 +++ src/tests/usr.bin/indent/psym_decl.c Wed Jun 14 09:31:05 2023 @@ -1,20 +1,17 @@ -/* $NetBSD: psym_decl.c,v 1.4 2022/04/24 10:36:37 rillig Exp $ */ +/* $NetBSD: psym_decl.c,v 1.5 2023/06/14 09:31:05 rillig Exp $ */ /* * Tests for the parser symbol psym_decl, which represents a declaration. * * Since C99, declarations and statements can be mixed in blocks. * - * A label can be followed by a statement but not by a declaration. + * In C, a label can be followed by a statement but not by a declaration. * * Indent distinguishes global and local declarations. * * Declarations can be for functions or for variables. */ -// TODO: prove that psym_decl can only ever occur at the top of the stack. -// TODO: delete decl_level if the above is proven. - //indent input int global_var; int global_array = [1,2,3,4]; @@ -36,3 +33,23 @@ int global_array = [ 4, ]; //indent end + + +// Declarations can be nested. +//indent input +struct level_1 { + union level_2 { + enum level_3 { + level_3_c_1, + level_3_c_2, + } level_3; + } level_2; +} level_1; +//indent end + +// The outermost declarator 'level_1' is indented as a global variable. +// The inner declarators 'level_2' and 'level_3' are indented as local +// variables. +// XXX: This is inconsistent, as in practice, struct members are usually +// aligned, while local variables aren't. +//indent run-equals-input -ldi0 Index: src/usr.bin/indent/debug.c diff -u src/usr.bin/indent/debug.c:1.56 src/usr.bin/indent/debug.c:1.57 --- src/usr.bin/indent/debug.c:1.56 Wed Jun 14 08:36:51 2023 +++ src/usr.bin/indent/debug.c Wed Jun 14 09:31:05 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: debug.c,v 1.56 2023/06/14 08:36:51 rillig Exp $ */ +/* $NetBSD: debug.c,v 1.57 2023/06/14 09:31:05 rillig Exp $ */ /*- * Copyright (c) 2023 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: debug.c,v 1.56 2023/06/14 08:36:51 rillig Exp $"); +__RCSID("$NetBSD: debug.c,v 1.57 2023/06/14 09:31:05 rillig Exp $"); #include <stdarg.h> #include <string.h> @@ -363,10 +363,10 @@ debug_parser_state(void) debug_ps_int(nparen); debug_ps_paren(); - state.heading = "horizontal spacing for comments"; - debug_ps_int(comment_delta); - debug_ps_int(n_comment_delta); - debug_ps_int(com_ind); + state.heading = "indentation of comments"; + debug_ps_int(comment_ind); + debug_ps_int(comment_shift); + debug_ps_bool(comment_in_first_line); state.heading = "vertical spacing"; debug_ps_bool(break_after_comma); Index: src/usr.bin/indent/indent.h diff -u src/usr.bin/indent/indent.h:1.191 src/usr.bin/indent/indent.h:1.192 --- src/usr.bin/indent/indent.h:1.191 Wed Jun 14 08:36:51 2023 +++ src/usr.bin/indent/indent.h Wed Jun 14 09:31:05 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.h,v 1.191 2023/06/14 08:36:51 rillig Exp $ */ +/* $NetBSD: indent.h,v 1.192 2023/06/14 09:31:05 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD @@ -71,7 +71,7 @@ typedef enum lexer_symbol { lsym_eof, lsym_preprocessing, /* the initial '#' of a preprocessing line */ - lsym_newline, + lsym_newline, /* outside block comments */ lsym_comment, /* the initial '/ *' or '//' of a comment */ lsym_lparen, @@ -229,8 +229,8 @@ extern struct options { * lined-up code within the margin */ bool lineup_to_parens; /* whether continued code within parens will be * lined up to the open paren */ - bool proc_calls_space; /* whether function calls look like: foo (bar) - * rather than foo(bar) */ + bool proc_calls_space; /* whether function calls look like 'foo (bar)' + * rather than 'foo(bar)' */ bool procnames_start_line; /* whether the names of functions being * defined get placed in column 1 (i.e. * a newline is placed between the type @@ -376,15 +376,12 @@ extern struct parser_state { * initializer or declaration */ struct paren_level paren[20]; - /* Horizontal spacing for comments */ + /* Indentation of comments */ - int comment_delta; /* used to set up indentation for all lines of - * a boxed comment after the first one */ - int n_comment_delta; /* remembers how many columns there were before - * the start of a box comment so that - * forthcoming lines of the comment are - * indented properly */ - int com_ind; /* indentation of the current comment */ + int comment_ind; /* indentation of the current comment */ + int comment_shift; /* all but the first line of a boxed comment + * are shifted this much to the right */ + bool comment_in_first_line; /* Vertical spacing */ Index: src/usr.bin/indent/io.c diff -u src/usr.bin/indent/io.c:1.217 src/usr.bin/indent/io.c:1.218 --- src/usr.bin/indent/io.c:1.217 Sat Jun 10 21:36:38 2023 +++ src/usr.bin/indent/io.c Wed Jun 14 09:31:05 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: io.c,v 1.217 2023/06/10 21:36:38 rillig Exp $ */ +/* $NetBSD: io.c,v 1.218 2023/06/14 09:31:05 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: io.c,v 1.217 2023/06/10 21:36:38 rillig Exp $"); +__RCSID("$NetBSD: io.c,v 1.218 2023/06/14 09:31:05 rillig Exp $"); #include <stdio.h> @@ -294,10 +294,14 @@ output_line_code(void) static void output_comment(void) { - int target_ind = ps.com_ind + ps.comment_delta; + int target_ind = ps.comment_ind; const char *p; - /* consider original indentation in case this is a box comment */ + if (!ps.comment_in_first_line) + target_ind += ps.comment_shift; + ps.comment_in_first_line = false; + + /* consider the original indentation in case this is a box comment */ for (p = com.s; *p == '\t'; p++) target_ind += opt.tabsize; @@ -321,8 +325,6 @@ output_comment(void) write_indent(target_ind); write_range(p, com.len - (size_t)(p - com.s)); - - ps.comment_delta = ps.n_comment_delta; } static void Index: src/usr.bin/indent/pr_comment.c diff -u src/usr.bin/indent/pr_comment.c:1.163 src/usr.bin/indent/pr_comment.c:1.164 --- src/usr.bin/indent/pr_comment.c:1.163 Wed Jun 14 08:36:51 2023 +++ src/usr.bin/indent/pr_comment.c Wed Jun 14 09:31:05 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: pr_comment.c,v 1.163 2023/06/14 08:36:51 rillig Exp $ */ +/* $NetBSD: pr_comment.c,v 1.164 2023/06/14 09:31:05 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -38,7 +38,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: pr_comment.c,v 1.163 2023/06/14 08:36:51 rillig Exp $"); +__RCSID("$NetBSD: pr_comment.c,v 1.164 2023/06/14 09:31:05 rillig Exp $"); #include <string.h> @@ -58,7 +58,7 @@ com_add_delim(void) } static bool -fits_in_one_line(int com_ind, int max_line_length) +fits_in_one_line(int max_line_length) { for (const char *start = inp_p, *p = start; *p != '\n'; p++) { if (p[0] == '*' && p[1] == '/') { @@ -66,18 +66,17 @@ fits_in_one_line(int com_ind, int max_li && ch_isblank(p[-1]) && ch_isblank(p[-2])) p--; - int len = ind_add(com_ind + 3, + int ind = ind_add(ps.comment_ind + 3, start, (size_t)(p - start)); - len += p == start || ch_isblank(p[-1]) ? 2 : 3; - return len <= max_line_length; + ind += p == start || ch_isblank(p[-1]) ? 2 : 3; + return ind <= max_line_length; } } return false; } static void -analyze_comment(bool *p_may_wrap, bool *p_delim, - int *p_ind, int *p_line_length) +analyze_comment(bool *p_may_wrap, bool *p_delim, int *p_line_length) { bool may_wrap = true; bool delim = false; @@ -122,30 +121,28 @@ analyze_comment(bool *p_may_wrap, bool * } } - ps.com_ind = ind; - if (!may_wrap) { /* Find out how much indentation there was originally, because * that much will have to be ignored by output_line. */ size_t len = (size_t)(inp_p - 2 - inp.s); - ps.n_comment_delta = -ind_add(0, inp.s, len); + ps.comment_shift = -ind_add(0, inp.s, len); } else { - ps.n_comment_delta = 0; + ps.comment_shift = 0; if (!(inp_p[0] == '\t' && !ch_isblank(inp_p[1]))) while (ch_isblank(inp_p[0])) inp_p++; } + ps.comment_ind = ind; *p_may_wrap = may_wrap; *p_delim = delim; - *p_ind = ind; *p_line_length = line_length; } static void -copy_comment_start(bool may_wrap, bool *delim, int ind, int line_length) +copy_comment_start(bool may_wrap, bool *delim, int line_length) { - ps.comment_delta = 0; + ps.comment_in_first_line = true; com_add_char('/'); com_add_char(token.s[token.len - 1]); /* either '*' or '/' */ @@ -153,7 +150,7 @@ copy_comment_start(bool may_wrap, bool * if (!ch_isblank(inp_p[0])) com_add_char(' '); - if (*delim && fits_in_one_line(ind, line_length)) + if (*delim && fits_in_one_line(line_length)) *delim = false; if (*delim) { output_line(); @@ -165,7 +162,7 @@ copy_comment_start(bool may_wrap, bool * static void copy_comment_wrap_text(int line_length, ssize_t *last_blank) { - int now_len = ind_add(ps.com_ind, com.s, com.len); + int now_len = ind_add(ps.comment_ind, com.s, com.len); for (;;) { char ch = inp_next(); if (ch_isblank(ch)) @@ -204,10 +201,10 @@ copy_comment_wrap_text(int line_length, } static bool -copy_comment_wrap_newline(ssize_t *last_blank, bool *seen_newline) +copy_comment_wrap_newline(ssize_t *last_blank, bool seen_newline) { *last_blank = -1; - if (*seen_newline) { + if (seen_newline) { if (com.len == 0) com_add_char(' '); /* force empty output line */ if (com.len > 3) { @@ -217,7 +214,6 @@ copy_comment_wrap_newline(ssize_t *last_ output_line(); com_add_delim(); } else { - *seen_newline = true; if (!(com.len > 0 && ch_isblank(com.s[com.len - 1]))) com_add_char(' '); *last_blank = (int)com.len - 1; @@ -252,7 +248,7 @@ copy_comment_wrap_finish(int line_length size_t len = com.len; while (ch_isblank(com.s[len - 1])) len--; - int end_ind = ind_add(ps.com_ind, com.s, len); + int end_ind = ind_add(ps.comment_ind, com.s, len); if (end_ind + 3 > line_length) output_line(); } @@ -287,17 +283,17 @@ copy_comment_wrap(int line_length, bool if (had_eof) goto unterminated_comment; if (!copy_comment_wrap_newline(&last_blank, - &seen_newline)) - goto end_of_comment; + seen_newline)) + break; + seen_newline = true; } else if (inp_p[0] == '*' && inp_p[1] == '/') - goto end_of_comment; + break; else { copy_comment_wrap_text(line_length, &last_blank); seen_newline = false; } } -end_of_comment: copy_comment_wrap_finish(line_length, delim); return; @@ -348,10 +344,10 @@ void process_comment(void) { bool may_wrap, delim; - int ind, line_length; + int line_length; - analyze_comment(&may_wrap, &delim, &ind, &line_length); - copy_comment_start(may_wrap, &delim, ind, line_length); + analyze_comment(&may_wrap, &delim, &line_length); + copy_comment_start(may_wrap, &delim, line_length); if (may_wrap) copy_comment_wrap(line_length, delim); else