Module Name: src Committed By: rillig Date: Fri Nov 19 15:32:13 UTC 2021
Modified Files: src/usr.bin/indent: indent.c indent.h pr_comment.c Log Message: indent: rename input buffer variables >From reading the names 'save_com' and 'sc_end', it was not obvious enough that these two variables are the limits of the same buffer, the names were just too unrelated. No functional change. To generate a diff of this commit: cvs rdiff -u -r1.219 -r1.220 src/usr.bin/indent/indent.c cvs rdiff -u -r1.87 -r1.88 src/usr.bin/indent/indent.h cvs rdiff -u -r1.117 -r1.118 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/usr.bin/indent/indent.c diff -u src/usr.bin/indent/indent.c:1.219 src/usr.bin/indent/indent.c:1.220 --- src/usr.bin/indent/indent.c:1.219 Fri Nov 19 15:28:32 2021 +++ src/usr.bin/indent/indent.c Fri Nov 19 15:32:13 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.c,v 1.219 2021/11/19 15:28:32 rillig Exp $ */ +/* $NetBSD: indent.c,v 1.220 2021/11/19 15:32:13 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)indent.c 5.1 #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: indent.c,v 1.219 2021/11/19 15:28:32 rillig Exp $"); +__RCSID("$NetBSD: indent.c,v 1.220 2021/11/19 15:32:13 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); #endif @@ -222,16 +222,17 @@ static void debug_save_com(const char *prefix) { debug_printf("%s: save_com is ", prefix); - debug_vis_range("\"", inbuf.save_com, inbuf.sc_end, "\"\n"); + debug_vis_range("\"", inbuf.save_com_s, inbuf.save_com_e, "\"\n"); } #else #define debug_save_com(prefix) do { } while (false) #endif static void -sc_check_size(size_t n) +save_com_check_size(size_t n) { - if ((size_t)(inbuf.sc_end - inbuf.sc_buf) + n <= sc_size) + if ((size_t)(inbuf.save_com_e - inbuf.save_com_buf) + n <= + array_length(inbuf.save_com_buf)) return; diag(1, "Internal buffer overflow - " @@ -241,31 +242,31 @@ sc_check_size(size_t n) } static void -sc_add_char(char ch) +save_com_add_char(char ch) { - sc_check_size(1); - *inbuf.sc_end++ = ch; + save_com_check_size(1); + *inbuf.save_com_e++ = ch; } static void -sc_add_range(const char *s, const char *e) +save_com_add_range(const char *s, const char *e) { size_t len = (size_t)(e - s); - sc_check_size(len); - memcpy(inbuf.sc_end, s, len); - inbuf.sc_end += len; + save_com_check_size(len); + memcpy(inbuf.save_com_e, s, len); + inbuf.save_com_e += len; } static void search_stmt_newline(bool *force_nl) { - if (inbuf.sc_end == NULL) { - inbuf.save_com = inbuf.sc_buf; - inbuf.save_com[0] = inbuf.save_com[1] = ' '; - inbuf.sc_end = &inbuf.save_com[2]; + if (inbuf.save_com_e == NULL) { + inbuf.save_com_s = inbuf.save_com_buf; + inbuf.save_com_s[0] = inbuf.save_com_s[1] = ' '; + inbuf.save_com_e = &inbuf.save_com_s[2]; debug_save_com("search_stmt_newline init"); } - sc_add_char('\n'); + save_com_add_char('\n'); debug_save_com(__func__); line_no++; @@ -284,7 +285,7 @@ search_stmt_newline(bool *force_nl) static void search_stmt_comment(void) { - if (inbuf.sc_end == NULL) { + if (inbuf.save_com_e == NULL) { /* * Copy everything from the start of the line, because * process_comment() will use that to calculate the original @@ -298,25 +299,25 @@ search_stmt_comment(void) */ assert((size_t)(inbuf.inp.s - inbuf.inp.buf) >= 4); size_t line_len = (size_t)(inbuf.inp.s - inbuf.inp.buf) - 4; - assert(line_len < array_length(inbuf.sc_buf)); - memcpy(inbuf.sc_buf, inbuf.inp.buf, line_len); - inbuf.save_com = inbuf.sc_buf + line_len; - inbuf.save_com[0] = inbuf.save_com[1] = ' '; - inbuf.sc_end = &inbuf.save_com[2]; + assert(line_len < array_length(inbuf.save_com_buf)); + memcpy(inbuf.save_com_buf, inbuf.inp.buf, line_len); + inbuf.save_com_s = inbuf.save_com_buf + line_len; + inbuf.save_com_s[0] = inbuf.save_com_s[1] = ' '; + inbuf.save_com_e = &inbuf.save_com_s[2]; debug_vis_range("search_stmt_comment: before save_com is \"", - inbuf.sc_buf, inbuf.save_com, "\"\n"); + inbuf.save_com_buf, inbuf.save_com_s, "\"\n"); debug_vis_range("search_stmt_comment: save_com is \"", - inbuf.save_com, inbuf.sc_end, "\"\n"); + inbuf.save_com_s, inbuf.save_com_e, "\"\n"); } - sc_add_range(token.s, token.e); + save_com_add_range(token.s, token.e); if (token.e[-1] == '/') { while (inbuf.inp.s[0] != '\n') - sc_add_char(inp_next()); + save_com_add_char(inp_next()); debug_save_com("search_stmt_comment end C99"); } else { - while (!(inbuf.sc_end[-2] == '*' && inbuf.sc_end[-1] == '/')) - sc_add_char(inp_next()); + while (!(inbuf.save_com_e[-2] == '*' && inbuf.save_com_e[-1] == '/')) + save_com_add_char(inp_next()); debug_save_com("search_stmt_comment end block"); } } @@ -328,9 +329,9 @@ search_stmt_lbrace(void) * Put KNF-style lbraces before the buffered up tokens and jump out of * this loop in order to avoid copying the token again. */ - if (inbuf.sc_end != NULL && opt.brace_same_line) { - assert(inbuf.save_com[0] == ' '); /* see search_stmt_comment */ - inbuf.save_com[0] = '{'; + if (inbuf.save_com_e != NULL && opt.brace_same_line) { + assert(inbuf.save_com_s[0] == ' '); /* see search_stmt_comment */ + inbuf.save_com_s[0] = '{'; /* * Originally the lbrace may have been alone on its own line, but it * will be moved into "the else's line", so if there was a newline @@ -361,21 +362,21 @@ search_stmt_other(lexer_symbol lsym, boo if (remove_newlines) *force_nl = false; - if (inbuf.sc_end == NULL) { /* ignore buffering if comment wasn't saved + if (inbuf.save_com_e == NULL) { /* ignore buffering if comment wasn't saved * up */ ps.search_stmt = false; return false; } debug_save_com(__func__); - while (inbuf.sc_end > inbuf.save_com && ch_isblank(inbuf.sc_end[-1])) - inbuf.sc_end--; + while (inbuf.save_com_e > inbuf.save_com_s && ch_isblank(inbuf.save_com_e[-1])) + inbuf.save_com_e--; if (opt.swallow_optional_blanklines || (!comment_buffered && remove_newlines)) { *force_nl = !remove_newlines; - while (inbuf.sc_end > inbuf.save_com && inbuf.sc_end[-1] == '\n') - inbuf.sc_end--; + while (inbuf.save_com_e > inbuf.save_com_s && inbuf.save_com_e[-1] == '\n') + inbuf.save_com_e--; } if (*force_nl) { /* if we should insert a nl here, put it into @@ -383,14 +384,14 @@ search_stmt_other(lexer_symbol lsym, boo *force_nl = false; --line_no; /* this will be re-increased when the newline * is read from the buffer */ - sc_add_char('\n'); - sc_add_char(' '); + save_com_add_char('\n'); + save_com_add_char(' '); if (opt.verbose) /* warn if the line was not already broken */ diag(0, "Line broken"); } for (const char *t_ptr = token.s; *t_ptr != '\0'; ++t_ptr) - sc_add_char(*t_ptr); + save_com_add_char(*t_ptr); debug_save_com("search_stmt_other end"); return true; } @@ -399,16 +400,16 @@ static void switch_buffer(void) { ps.search_stmt = false; - sc_add_char(' '); /* add trailing blank, just in case */ + save_com_add_char(' '); /* add trailing blank, just in case */ debug_save_com(__func__); inbuf.saved_inp_s = inbuf.inp.s; inbuf.saved_inp_e = inbuf.inp.e; - inbuf.inp.s = inbuf.save_com; /* redirect lexi input to save_com */ - inbuf.inp.e = inbuf.sc_end; - inbuf.sc_end = NULL; - debug_println("switched inp.s to save_com"); + inbuf.inp.s = inbuf.save_com_s; /* redirect lexi input to save_com_s */ + inbuf.inp.e = inbuf.save_com_e; + inbuf.save_com_e = NULL; + debug_println("switched inp.s to save_com_s"); } static void @@ -434,9 +435,9 @@ search_stmt_lookahead(lexer_symbol *lsym * Work around the latter problem by copying all whitespace characters * into the buffer so that the later lexi() call will read them. */ - if (inbuf.sc_end != NULL) { + if (inbuf.save_com_e != NULL) { while (ch_isblank(*inbuf.inp.s)) - sc_add_char(inp_next()); + save_com_add_char(inp_next()); debug_save_com(__func__); } @@ -1282,28 +1283,28 @@ read_preprocessing_line(void) lab.e--; if (lab.e - lab.s == com_end && inbuf.saved_inp_s == NULL) { /* comment on preprocessor line */ - if (inbuf.sc_end == NULL) { /* if this is the first comment, we must set + if (inbuf.save_com_e == NULL) { /* if this is the first comment, we must set * up the buffer */ - inbuf.save_com = inbuf.sc_buf; - inbuf.sc_end = inbuf.save_com; + inbuf.save_com_s = inbuf.save_com_buf; + inbuf.save_com_e = inbuf.save_com_s; } else { - sc_add_char('\n'); /* add newline between comments */ - sc_add_char(' '); + save_com_add_char('\n'); /* add newline between comments */ + save_com_add_char(' '); --line_no; } - sc_add_range(lab.s + com_start, lab.s + com_end); + save_com_add_range(lab.s + com_start, lab.s + com_end); lab.e = lab.s + com_start; while (lab.e > lab.s && ch_isblank(lab.e[-1])) lab.e--; inbuf.saved_inp_s = inbuf.inp.s; /* save current input buffer */ inbuf.saved_inp_e = inbuf.inp.e; - inbuf.inp.s = inbuf.save_com; /* fix so that subsequent calls to lexi will + inbuf.inp.s = inbuf.save_com_s; /* fix so that subsequent calls to lexi will * take tokens out of save_com */ - sc_add_char(' '); /* add trailing blank, just in case */ + save_com_add_char(' '); /* add trailing blank, just in case */ debug_save_com(__func__); - inbuf.inp.e = inbuf.sc_end; - inbuf.sc_end = NULL; - debug_println("switched inp.s to save_com"); + inbuf.inp.e = inbuf.save_com_e; + inbuf.save_com_e = NULL; + debug_println("switched inbuf to save_com"); } buf_terminate(&lab); } Index: src/usr.bin/indent/indent.h diff -u src/usr.bin/indent/indent.h:1.87 src/usr.bin/indent/indent.h:1.88 --- src/usr.bin/indent/indent.h:1.87 Fri Nov 19 15:28:32 2021 +++ src/usr.bin/indent/indent.h Fri Nov 19 15:32:13 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: indent.h,v 1.87 2021/11/19 15:28:32 rillig Exp $ */ +/* $NetBSD: indent.h,v 1.88 2021/11/19 15:32:13 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD @@ -135,9 +135,6 @@ typedef enum stmt_head { hd_while, } stmt_head; -#define sc_size 5000 /* size of save_com buffer */ - - /* A range of characters, in some cases null-terminated. */ struct buffer { char *s; /* start of the usable text */ @@ -149,11 +146,11 @@ struct buffer { extern struct input_buffer { struct buffer inp; /* one line of input, ready to be split into * tokens; occasionally this buffer switches - * to sc_buf */ - char sc_buf[sc_size]; /* input text is saved here when looking for + * to save_com_buf */ + char save_com_buf[5000]; /* input text is saved here when looking for * the brace after an if, while, etc */ - char *save_com; /* start of the comment stored in sc_buf */ - char *sc_end; /* pointer into save_com buffer */ + char *save_com_s; /* start of the comment in save_com_buf */ + char *save_com_e; /* end of the comment in save_com_buf */ char *saved_inp_s; /* saved value of inp.s when taking input from * save_com */ Index: src/usr.bin/indent/pr_comment.c diff -u src/usr.bin/indent/pr_comment.c:1.117 src/usr.bin/indent/pr_comment.c:1.118 --- src/usr.bin/indent/pr_comment.c:1.117 Fri Nov 19 15:28:32 2021 +++ src/usr.bin/indent/pr_comment.c Fri Nov 19 15:32:13 2021 @@ -1,4 +1,4 @@ -/* $NetBSD: pr_comment.c,v 1.117 2021/11/19 15:28:32 rillig Exp $ */ +/* $NetBSD: pr_comment.c,v 1.118 2021/11/19 15:32:13 rillig Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause @@ -43,7 +43,7 @@ static char sccsid[] = "@(#)pr_comment.c #include <sys/cdefs.h> #if defined(__NetBSD__) -__RCSID("$NetBSD: pr_comment.c,v 1.117 2021/11/19 15:28:32 rillig Exp $"); +__RCSID("$NetBSD: pr_comment.c,v 1.118 2021/11/19 15:32:13 rillig Exp $"); #elif defined(__FreeBSD__) __FBSDID("$FreeBSD: head/usr.bin/indent/pr_comment.c 334927 2018-06-10 16:44:18Z pstef $"); #endif @@ -159,9 +159,10 @@ analyze_comment(bool *p_may_wrap, bool * * XXX: ordered comparison between pointers from different objects * invokes undefined behavior (C99 6.5.8). */ - const char *start = inbuf.inp.s >= inbuf.sc_buf && - inbuf.inp.s < inbuf.sc_buf + sc_size - ? inbuf.sc_buf : inbuf.inp.buf; + const char *start = inbuf.inp.s >= inbuf.save_com_buf && + inbuf.inp.s < + inbuf.save_com_buf + array_length(inbuf.save_com_buf) + ? inbuf.save_com_buf : inbuf.inp.buf; ps.n_comment_delta = -ind_add(0, start, inbuf.inp.s - 2); } else { ps.n_comment_delta = 0;