Module Name: src
Committed By: rillig
Date: Sat Jun 10 16:43:56 UTC 2023
Modified Files:
src/tests/usr.bin/indent: lsym_comment.c lsym_if.c lsym_switch.c
src/usr.bin/indent: args.c debug.c indent.c indent.h io.c lexi.c
parse.c pr_comment.c
Log Message:
indent: miscellaneous cleanups
To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/tests/usr.bin/indent/lsym_comment.c
cvs rdiff -u -r1.4 -r1.5 src/tests/usr.bin/indent/lsym_if.c
cvs rdiff -u -r1.3 -r1.4 src/tests/usr.bin/indent/lsym_switch.c
cvs rdiff -u -r1.82 -r1.83 src/usr.bin/indent/args.c
cvs rdiff -u -r1.49 -r1.50 src/usr.bin/indent/debug.c
cvs rdiff -u -r1.353 -r1.354 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.186 -r1.187 src/usr.bin/indent/indent.h
cvs rdiff -u -r1.215 -r1.216 src/usr.bin/indent/io.c
cvs rdiff -u -r1.224 -r1.225 src/usr.bin/indent/lexi.c
cvs rdiff -u -r1.70 -r1.71 src/usr.bin/indent/parse.c
cvs rdiff -u -r1.160 -r1.161 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.19 src/tests/usr.bin/indent/lsym_comment.c:1.20
--- src/tests/usr.bin/indent/lsym_comment.c:1.19 Sat Jun 10 06:38:21 2023
+++ src/tests/usr.bin/indent/lsym_comment.c Sat Jun 10 16:43:56 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_comment.c,v 1.19 2023/06/10 06:38:21 rillig Exp $ */
+/* $NetBSD: lsym_comment.c,v 1.20 2023/06/10 16:43:56 rillig Exp $ */
/*
* Tests for the token lsym_comment, which starts a comment.
@@ -32,7 +32,7 @@
* - block/end-of-line comment to the right of code
* - block/end-of-line comment to the right of label with code
*
- * - with/without opt.comment_delimiter_on_blankline (-cdb)
+ * - with/without opt.comment_delimiter_on_blank_line (-cdb)
* - with/without opt.star_comment_cont (-sc)
* - with/without opt.format_block_comments (-fbc)
* - with varying opt.max_line_length (32, 64, 80, 140)
@@ -1111,3 +1111,23 @@ a>b;
a > b;
}
//indent end
+
+
+/*
+ * Line comments are only related to a code snippet if they are on the same
+ * line; they cannot be continued in the next lines.
+ */
+//indent input
+int line; // comment line 1
+ // comment line 2
+int block; /* comment line 1
+ * comment line 2
+ */
+//indent end
+
+//indent run -di0
+int line; // comment line 1
+// $ XXX: This comment was probably intended to continue 'comment line 1'.
+// comment line 2
+int block; /* comment line 1 comment line 2 */
+//indent end
Index: src/tests/usr.bin/indent/lsym_if.c
diff -u src/tests/usr.bin/indent/lsym_if.c:1.4 src/tests/usr.bin/indent/lsym_if.c:1.5
--- src/tests/usr.bin/indent/lsym_if.c:1.4 Sun Apr 24 10:36:37 2022
+++ src/tests/usr.bin/indent/lsym_if.c Sat Jun 10 16:43:56 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_if.c,v 1.4 2022/04/24 10:36:37 rillig Exp $ */
+/* $NetBSD: lsym_if.c,v 1.5 2023/06/10 16:43:56 rillig Exp $ */
/*
* Tests for the token lsym_if, which represents the keyword 'if' that starts
@@ -21,3 +21,25 @@ function(void)
stmt();
}
//indent end
+
+
+/*
+ * After an 'if' statement without an 'else' branch, braces start a separate
+ * block.
+ */
+//indent input
+{
+ if(0)if(1)if(2)stmt();{}
+}
+//indent end
+
+//indent run
+{
+ if (0)
+ if (1)
+ if (2)
+// $ FIXME: The '{' must be on a separate line, with indentation 8.
+ stmt(); {
+ }
+}
+//indent end
Index: src/tests/usr.bin/indent/lsym_switch.c
diff -u src/tests/usr.bin/indent/lsym_switch.c:1.3 src/tests/usr.bin/indent/lsym_switch.c:1.4
--- src/tests/usr.bin/indent/lsym_switch.c:1.3 Sun Apr 24 09:04:12 2022
+++ src/tests/usr.bin/indent/lsym_switch.c Sat Jun 10 16:43:56 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: lsym_switch.c,v 1.3 2022/04/24 09:04:12 rillig Exp $ */
+/* $NetBSD: lsym_switch.c,v 1.4 2023/06/10 16:43:56 rillig Exp $ */
/*
* Tests for the token lsym_switch, which represents the keyword 'switch' that
@@ -9,8 +9,23 @@
* C11 6.8.4.2 "The 'switch' statement"
*/
+// TODO: Add systematic tests.
+
+/*
+ * Ensure that an unfinished 'switch' statement does not eat comments.
+ */
//indent input
-// TODO: add input
+{
+ switch (expr) // comment
+ {
+ }
+}
//indent end
-//indent run-equals-input
+//indent run
+{
+// $ FIXME: The '{' has moved to the comment.
+ switch (expr) // comment {
+ }
+}
+//indent end
Index: src/usr.bin/indent/args.c
diff -u src/usr.bin/indent/args.c:1.82 src/usr.bin/indent/args.c:1.83
--- src/usr.bin/indent/args.c:1.82 Mon Jun 5 10:12:21 2023
+++ src/usr.bin/indent/args.c Sat Jun 10 16:43:55 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: args.c,v 1.82 2023/06/05 10:12:21 rillig Exp $ */
+/* $NetBSD: args.c,v 1.83 2023/06/10 16:43:55 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: args.c,v 1.82 2023/06/05 10:12:21 rillig Exp $");
+__RCSID("$NetBSD: args.c,v 1.83 2023/06/10 16:43:55 rillig Exp $");
/* Read options from profile files and from the command line. */
@@ -73,18 +73,18 @@ static const struct pro {
short i_max;
void *p_var; /* the associated variable */
} pro[] = {
- bool_options("bacc", blanklines_around_conditional_compilation),
+ bool_options("bacc", blank_line_around_conditional_compilation),
bool_options("bad", blank_line_after_decl),
bool_options("badp", blank_line_after_decl_at_top),
- bool_options("bap", blanklines_after_procs),
- bool_options("bbb", blanklines_before_block_comments),
+ bool_options("bap", blank_line_after_proc),
+ bool_options("bbb", blank_line_before_block_comment),
bool_options("bc", break_after_comma),
bool_option("bl", false, brace_same_line),
bool_option("br", true, brace_same_line),
bool_options("bs", blank_after_sizeof),
int_option("c", comment_column, 1, 999),
int_option("cd", decl_comment_column, 1, 999),
- bool_options("cdb", comment_delimiter_on_blankline),
+ bool_options("cdb", comment_delimiter_on_blank_line),
bool_options("ce", cuddle_else),
int_option("ci", continuation_indent, 0, 999),
/* "cli" is special */
@@ -109,7 +109,7 @@ static const struct pro {
bool_options("pcs", proc_calls_space),
bool_options("psl", procnames_start_line),
bool_options("sc", star_comment_cont),
- bool_options("sob", swallow_optional_blanklines),
+ bool_options("sob", swallow_optional_blank_lines),
/* "st" is special */
bool_option("ta", true, auto_typedefs),
/* "T" is special */
Index: src/usr.bin/indent/debug.c
diff -u src/usr.bin/indent/debug.c:1.49 src/usr.bin/indent/debug.c:1.50
--- src/usr.bin/indent/debug.c:1.49 Sat Jun 10 13:03:17 2023
+++ src/usr.bin/indent/debug.c Sat Jun 10 16:43:55 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: debug.c,v 1.49 2023/06/10 13:03:17 rillig Exp $ */
+/* $NetBSD: debug.c,v 1.50 2023/06/10 16:43:55 rillig Exp $ */
/*-
* Copyright (c) 2023 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: debug.c,v 1.49 2023/06/10 13:03:17 rillig Exp $");
+__RCSID("$NetBSD: debug.c,v 1.50 2023/06/10 16:43:55 rillig Exp $");
#include <stdarg.h>
#include <string.h>
@@ -264,7 +264,7 @@ ps_paren_has_changed(void)
if (state.prev_ps.nparen != ps.nparen)
return true;
- const paren_level_props *prev = state.prev_ps.paren, *curr = ps.paren;
+ const struct paren_level *prev = state.prev_ps.paren, *curr = ps.paren;
for (int i = 0; i < ps.nparen; i++)
if (curr[i].indent != prev[i].indent
|| curr[i].cast != prev[i].cast)
@@ -356,7 +356,7 @@ debug_parser_state(void)
debug_ps_bool(tabs_to_var);
debug_ps_enum(extra_expr_indent, extra_expr_indent_name);
- // The parser symbol stack is printed in debug_parse_stack instead.
+ // The parser symbol stack is printed in debug_psyms_stack instead.
state.heading = "spacing inside a statement or declaration";
debug_ps_bool(next_unary);
@@ -388,7 +388,7 @@ debug_parser_state(void)
}
void
-debug_parse_stack(const char *situation)
+debug_psyms_stack(const char *situation)
{
debug_printf("parse stack %s:", situation);
const struct psym_stack *psyms = &ps.psyms;
Index: src/usr.bin/indent/indent.c
diff -u src/usr.bin/indent/indent.c:1.353 src/usr.bin/indent/indent.c:1.354
--- src/usr.bin/indent/indent.c:1.353 Sat Jun 10 12:59:31 2023
+++ src/usr.bin/indent/indent.c Sat Jun 10 16:43:55 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.c,v 1.353 2023/06/10 12:59:31 rillig Exp $ */
+/* $NetBSD: indent.c,v 1.354 2023/06/10 16:43:55 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,22 +38,20 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.353 2023/06/10 12:59:31 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.354 2023/06/10 16:43:55 rillig Exp $");
#include <sys/param.h>
#include <err.h>
-#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <unistd.h>
#include "indent.h"
struct options opt = {
.brace_same_line = true,
- .comment_delimiter_on_blankline = true,
+ .comment_delimiter_on_blank_line = true,
.cuddle_else = true,
.comment_column = 33,
.decl_indent = 16,
@@ -91,8 +89,8 @@ FILE *input;
FILE *output;
static const char *in_name = "Standard Input";
+static char backup_name[PATH_MAX];
static const char *backup_suffix = ".BAK";
-static char bakfile[MAXPATHLEN] = "";
void *
@@ -218,38 +216,36 @@ load_profiles(int argc, char **argv)
* and the original input file the output.
*/
static void
-bakcopy(void)
+copy_to_bak_file(void)
{
- ssize_t n;
- int bak_fd;
- char buff[8 * 1024];
+ size_t n;
+ char buff[BUFSIZ];
const char *last_slash = strrchr(in_name, '/');
- snprintf(bakfile, sizeof(bakfile), "%s%s",
- last_slash != NULL ? last_slash + 1 : in_name, backup_suffix);
+ const char *base = last_slash != NULL ? last_slash + 1 : in_name;
+ snprintf(backup_name, sizeof(backup_name), "%s%s", base, backup_suffix);
- /* copy in_name to backup file */
- bak_fd = creat(bakfile, 0600);
- if (bak_fd < 0)
- err(1, "%s", bakfile);
-
- while ((n = read(fileno(input), buff, sizeof(buff))) > 0)
- if (write(bak_fd, buff, (size_t)n) != n)
- err(1, "%s", bakfile);
- if (n < 0)
+ /* copy the input file to the backup file */
+ FILE *bak = fopen(backup_name, "w");
+ if (bak == NULL)
+ err(1, "%s", backup_name);
+
+ while ((n = fread(buff, 1, sizeof(buff), input)) > 0)
+ if (fwrite(buff, 1, n, bak) != n)
+ err(1, "%s", backup_name);
+ if (fclose(input) != 0)
err(1, "%s", in_name);
+ if (fclose(bak) != 0)
+ err(1, "%s", backup_name);
- close(bak_fd);
- (void)fclose(input);
-
- /* re-open backup file as the input file */
- input = fopen(bakfile, "r");
+ /* re-open the backup file as the input file */
+ input = fopen(backup_name, "r");
if (input == NULL)
- err(1, "%s", bakfile);
+ err(1, "%s", backup_name);
/* now the original input file will be the output */
output = fopen(in_name, "w");
if (output == NULL) {
- unlink(bakfile);
+ remove(backup_name);
err(1, "%s", in_name);
}
}
@@ -283,7 +279,7 @@ parse_command_line(int argc, char **argv
input = stdin;
output = stdout;
} else if (output == NULL)
- bakcopy();
+ copy_to_bak_file();
if (opt.comment_column <= 1)
opt.comment_column = 2; /* don't put normal comments in column
@@ -391,7 +387,7 @@ is_function_pointer_declaration(void)
static int
process_eof(void)
{
- output_finish();
+ finish_output();
if (ps.psyms.top > 1) /* check for balanced braces */
diag(1, "Stuff missing from end of file");
@@ -657,10 +653,10 @@ process_lbrace(void)
ps.in_stmt_or_decl = false; /* don't indent the {} */
- if (!ps.in_init)
- ps.force_nl = true;
- else
+ if (ps.in_init)
ps.init_level++;
+ else
+ ps.force_nl = true;
if (code.len > 0 && !ps.in_init) {
if (!opt.brace_same_line ||
@@ -814,7 +810,7 @@ process_comma(void)
}
static void
-process_colon_label(void)
+process_label_colon(void)
{
buf_add_buf(&lab, &code);
buf_add_char(&lab, ':');
@@ -829,7 +825,7 @@ process_colon_label(void)
}
static void
-process_colon_other(void)
+process_other_colon(void)
{
buf_add_char(&code, ':');
ps.want_blank = ps.decl_level == 0;
@@ -902,9 +898,9 @@ process_type_outside_parentheses(void)
ps.declaration = decl_begin;
int len = (int)token.len + 1;
- int ind = ps.ind_level == 0 || ps.decl_level > 0
- ? opt.decl_indent /* global variable or local member */
- : opt.local_decl_indent; /* local variable */
+ int ind = ps.ind_level > 0 && ps.decl_level == 0
+ ? opt.local_decl_indent /* local variable */
+ : opt.decl_indent; /* global variable, or member */
ps.decl_ind = ind > 0 ? ind : len;
ps.tabs_to_var = opt.use_tabs && ind > 0;
}
@@ -984,9 +980,9 @@ process_lsym(lexer_symbol lsym)
case lsym_postfix_op: process_postfix_op(); break;
case lsym_binary_op: goto copy_token;
case lsym_question: ps.quest_level++; goto copy_token;
- case lsym_colon_question: goto copy_token;
- case lsym_colon_label: process_colon_label(); break;
- case lsym_colon_other: process_colon_other(); break;
+ case lsym_question_colon: goto copy_token;
+ case lsym_label_colon: process_label_colon(); break;
+ case lsym_other_colon: process_other_colon(); break;
case lsym_comma: process_comma(); break;
case lsym_semicolon: process_semicolon(); break;
case lsym_typedef: goto copy_token;
@@ -1050,7 +1046,7 @@ indent(void)
&& opt.else_if_in_same_line)
ps.force_nl = false;
- if (lsym == lsym_newline || lsym == lsym_preprocessing)
+ if (lsym == lsym_preprocessing || lsym == lsym_newline)
ps.force_nl = false;
else if (lsym == lsym_comment) {
/* no special processing */
@@ -1066,8 +1062,9 @@ indent(void)
debug_parser_state();
- if (lsym != lsym_comment && lsym != lsym_newline &&
- lsym != lsym_preprocessing)
+ if (lsym != lsym_preprocessing
+ && lsym != lsym_newline
+ && lsym != lsym_comment)
ps.prev_lsym = lsym;
}
}
Index: src/usr.bin/indent/indent.h
diff -u src/usr.bin/indent/indent.h:1.186 src/usr.bin/indent/indent.h:1.187
--- src/usr.bin/indent/indent.h:1.186 Sat Jun 10 12:59:31 2023
+++ src/usr.bin/indent/indent.h Sat Jun 10 16:43:56 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.h,v 1.186 2023/06/10 12:59:31 rillig Exp $ */
+/* $NetBSD: indent.h,v 1.187 2023/06/10 16:43:56 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@@ -88,7 +88,7 @@ typedef enum lexer_symbol {
lsym_postfix_op, /* trailing '++' or '--' */
lsym_binary_op, /* e.g. '*', '&', '<<', '&&' or '/=' */
lsym_question, /* the '?' from a '?:' expression */
- lsym_colon_question, /* the ':' from a '?:' expression */
+ lsym_question_colon, /* the ':' from a '?:' expression */
lsym_comma,
lsym_typedef,
@@ -98,8 +98,8 @@ typedef enum lexer_symbol {
lsym_type_in_parentheses,
lsym_word, /* identifier, constant or string */
lsym_funcname, /* name of a function being defined */
- lsym_colon_label, /* the ':' after a label */
- lsym_colon_other, /* bit-fields, generic-association (C11),
+ lsym_label_colon, /* the ':' after a label */
+ lsym_other_colon, /* bit-fields, generic-association (C11),
* enum-type-specifier (C23),
* attribute-prefixed-token (C23),
* pp-prefixed-parameter (C23 6.10) */
@@ -174,7 +174,7 @@ extern struct buffer com; /* the trailin
* multi-line comment */
extern struct options {
- bool blanklines_around_conditional_compilation;
+ bool blank_line_around_conditional_compilation;
bool blank_line_after_decl_at_top; /* this is vaguely similar to
* blank_line_after_decl except
* that it only applies to the
@@ -185,21 +185,20 @@ extern struct options {
* even if there are no
* declarations */
bool blank_line_after_decl;
- bool blanklines_after_procs;
- bool blanklines_before_block_comments;
+ bool blank_line_after_proc;
+ bool blank_line_before_block_comment;
bool break_after_comma; /* whether to add a line break after each
* declarator */
- bool brace_same_line; /* whether brace should be on same line as if,
- * while, etc */
- bool blank_after_sizeof; /* whether a blank should always be
- * inserted after sizeof */
- bool comment_delimiter_on_blankline;
+ bool brace_same_line; /* whether a brace should be on same line as an
+ * if, while, etc. */
+ bool blank_after_sizeof;
+ bool comment_delimiter_on_blank_line;
int decl_comment_column; /* the column in which comments after
* declarations should be put */
bool cuddle_else; /* whether 'else' should cuddle up to '}' */
int continuation_indent; /* the indentation between the edge of
* code and continuation lines */
- float case_indent; /* The distance (measured in indentation
+ float case_indent; /* the distance (measured in indentation
* levels) to indent case labels from the
* switch statement */
int comment_column; /* the column in which comments to the right of
@@ -217,12 +216,12 @@ extern struct options {
bool else_if_in_same_line;
bool function_brace_split; /* split function declaration and brace
* onto separate lines */
- bool format_col1_comments; /* If comments which start in column 1
- * are to be reformatted (just like
- * comments that begin in later
+ bool format_col1_comments; /* whether comments that start in
+ * column 1 are to be reformatted (just
+ * like comments that begin in later
* columns) */
- bool format_block_comments; /* whether comments beginning with '/ *
- * \n' are to be reformatted */
+ bool format_block_comments; /* whether to reformat comments that
+ * begin with '/ * \n' */
bool indent_parameters;
int indent_size; /* the size of one indentation level */
int block_comment_max_line_length;
@@ -240,7 +239,7 @@ extern struct options {
bool space_after_cast; /* "b = (int) a" vs. "b = (int)a" */
bool star_comment_cont; /* whether comment continuation lines should
* have stars at the beginning of each line */
- bool swallow_optional_blanklines;
+ bool swallow_optional_blank_lines;
bool auto_typedefs; /* whether to recognize identifiers ending in
* "_t" like typedefs */
int tabsize; /* the size of a tab */
@@ -252,7 +251,7 @@ extern struct options {
extern bool found_err;
extern bool had_eof; /* whether input is exhausted */
-extern int line_no; /* the current line number. */
+extern int line_no; /* the current input line number */
extern enum indent_enabled {
indent_on,
indent_off,
@@ -262,7 +261,7 @@ extern enum indent_enabled {
#define STACKSIZE 256
/* Properties of each level of parentheses or brackets. */
-typedef struct paren_level_props {
+struct paren_level {
int indent; /* indentation of the operand/argument,
* relative to the enclosing statement; if
* negative, reflected at -1 */
@@ -271,7 +270,7 @@ typedef struct paren_level_props {
cast_maybe,
cast_no,
} cast; /* whether the parentheses form a type cast */
-} paren_level_props;
+};
struct psym_stack {
int top; /* pointer to top of stack */
@@ -377,7 +376,7 @@ extern struct parser_state {
* are currently open; used to indent the
* remaining lines of the statement,
* initializer or declaration */
- paren_level_props paren[20];
+ struct paren_level paren[20];
/* Horizontal spacing for comments */
@@ -445,7 +444,7 @@ void debug_println(const char *, ...) __
void debug_blank_line(void);
void debug_vis_range(const char *, const char *, size_t, const char *);
void debug_parser_state(void);
-void debug_parse_stack(const char *);
+void debug_psyms_stack(const char *);
void debug_print_buf(const char *, const struct buffer *);
void debug_buffers(void);
extern const char *const lsym_name[];
@@ -459,7 +458,7 @@ extern const char *const line_kind_name[
#define debug_blank_line() debug_noop()
#define debug_vis_range(prefix, s, e, suffix) debug_noop()
#define debug_parser_state() debug_noop()
-#define debug_parse_stack(situation) debug_noop()
+#define debug_psyms_stack(situation) debug_noop()
#define debug_print_buf(name, buf) debug_noop()
#define debug_buffers() debug_noop()
#endif
@@ -471,7 +470,7 @@ int ind_add(int, const char *, size_t);
void inp_skip(void);
char inp_next(void);
-void output_finish(void);
+void finish_output(void);
lexer_symbol lexi(void);
void diag(int, const char *, ...) __printflike(2, 3);
Index: src/usr.bin/indent/io.c
diff -u src/usr.bin/indent/io.c:1.215 src/usr.bin/indent/io.c:1.216
--- src/usr.bin/indent/io.c:1.215 Sat Jun 10 12:59:31 2023
+++ src/usr.bin/indent/io.c Sat Jun 10 16:43:56 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: io.c,v 1.215 2023/06/10 12:59:31 rillig Exp $ */
+/* $NetBSD: io.c,v 1.216 2023/06/10 16:43:56 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: io.c,v 1.215 2023/06/10 12:59:31 rillig Exp $");
+__RCSID("$NetBSD: io.c,v 1.216 2023/06/10 16:43:56 rillig Exp $");
#include <stdio.h>
@@ -110,7 +110,7 @@ inp_next(void)
static void
-buffer_newline(void)
+add_buffered_newline(void)
{
buffered_newlines++;
newlines++;
@@ -173,17 +173,17 @@ want_blank_line(void)
ps.blank_line_after_decl = false;
return true;
}
- if (opt.blanklines_around_conditional_compilation) {
+ if (opt.blank_line_around_conditional_compilation) {
if (out.prev_line_kind != lk_if && out.line_kind == lk_if)
return true;
if (out.prev_line_kind == lk_endif
&& out.line_kind != lk_endif)
return true;
}
- if (opt.blanklines_after_procs && out.prev_line_kind == lk_func_end
+ if (opt.blank_line_after_proc && out.prev_line_kind == lk_func_end
&& out.line_kind != lk_endif)
return true;
- if (opt.blanklines_before_block_comments
+ if (opt.blank_line_before_block_comment
&& out.line_kind == lk_block_comment)
return true;
return false;
@@ -227,7 +227,7 @@ output_line_label(void)
}
static int
-compute_code_indent_lineup(int base_ind)
+compute_lined_up_code_indent(int base_ind)
{
int ind = paren_indent;
int overflow = ind_add(ind, code.s, code.len) - opt.max_line_length;
@@ -261,7 +261,7 @@ compute_code_indent(void)
if (opt.lineup_to_parens) {
if (opt.lineup_to_parens_always)
return paren_indent;
- return compute_code_indent_lineup(base_ind);
+ return compute_lined_up_code_indent(base_ind);
}
int rel_ind = opt.continuation_indent * ps.line_start_nparen;
@@ -292,7 +292,7 @@ output_line_code(void)
}
static void
-output_line_comment(void)
+output_comment(void)
{
int target_ind = ps.com_ind + ps.comment_delta;
const char *p;
@@ -313,7 +313,7 @@ output_line_comment(void)
}
if (out_ind > target_ind)
- buffer_newline();
+ add_buffered_newline();
while (com.s + com.len > p && ch_isspace(com.s[com.len - 1]))
com.len--;
@@ -326,14 +326,14 @@ output_line_comment(void)
}
static void
-output_line_indented(void)
+output_indented_line(void)
{
if (lab.len == 0 && code.len == 0 && com.len == 0)
out.line_kind = lk_blank;
if (want_blank_line() && newlines < 2
&& out.line_kind != lk_blank)
- buffer_newline();
+ add_buffered_newline();
/* This kludge aligns function definitions correctly. */
if (ps.ind_level == 0)
@@ -345,7 +345,7 @@ output_line_indented(void)
ps.blank_line_after_decl = true;
}
- if (opt.swallow_optional_blanklines
+ if (opt.swallow_optional_blank_lines
&& out.line_kind == lk_blank
&& is_blank_line_optional())
return;
@@ -355,8 +355,8 @@ output_line_indented(void)
if (code.len > 0)
output_line_code();
if (com.len > 0)
- output_line_comment();
- buffer_newline();
+ output_comment();
+ add_buffered_newline();
if (out.line_kind != lk_blank)
write_buffered_newlines();
@@ -375,7 +375,7 @@ output_line(void)
debug_buffers();
if (indent_enabled == indent_on)
- output_line_indented();
+ output_indented_line();
else if (indent_enabled == indent_last_off_line) {
indent_enabled = indent_on;
write_range(out.indent_off_text.s, out.indent_off_text.len);
@@ -410,7 +410,7 @@ output_line(void)
}
void
-output_finish(void)
+finish_output(void)
{
output_line();
if (indent_enabled != indent_on) {
Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.224 src/usr.bin/indent/lexi.c:1.225
--- src/usr.bin/indent/lexi.c:1.224 Sat Jun 10 13:03:17 2023
+++ src/usr.bin/indent/lexi.c Sat Jun 10 16:43:56 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: lexi.c,v 1.224 2023/06/10 13:03:17 rillig Exp $ */
+/* $NetBSD: lexi.c,v 1.225 2023/06/10 16:43:56 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.224 2023/06/10 13:03:17 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.225 2023/06/10 16:43:56 rillig Exp $");
#include <stdlib.h>
#include <string.h>
@@ -167,6 +167,18 @@ static const unsigned char lex_number_ro
};
+static bool
+is_identifier_start(char ch)
+{
+ return ch_isalpha(ch) || ch == '_' || ch == '$';
+}
+
+static bool
+is_identifier_part(char ch)
+{
+ return ch_isalnum(ch) || ch == '_' || ch == '$';
+}
+
static void
token_add_char(char ch)
{
@@ -202,18 +214,6 @@ lex_number(void)
}
}
-static bool
-is_identifier_start(char ch)
-{
- return ch_isalpha(ch) || ch == '_' || ch == '$';
-}
-
-static bool
-is_identifier_part(char ch)
-{
- return ch_isalnum(ch) || ch == '_' || ch == '$';
-}
-
static void
lex_word(void)
{
@@ -276,20 +276,20 @@ static int
bsearch_typenames(const char *key)
{
const char **arr = typenames.items;
- int lo = 0;
- int hi = (int)typenames.len - 1;
+ unsigned lo = 0;
+ unsigned hi = typenames.len;
- while (lo <= hi) {
- int mid = (int)((unsigned)(lo + hi) >> 1);
+ while (lo < hi) {
+ unsigned mid = (lo + hi) / 2;
int cmp = strcmp(arr[mid], key);
if (cmp < 0)
lo = mid + 1;
else if (cmp > 0)
- hi = mid - 1;
+ hi = mid;
else
- return mid;
+ return (int)mid;
}
- return -(lo + 1);
+ return -1 - (int)lo;
}
static bool
@@ -302,6 +302,25 @@ is_typename(void)
return bsearch_typenames(token.s) >= 0;
}
+void
+register_typename(const char *name)
+{
+ if (typenames.len >= typenames.cap) {
+ typenames.cap = 16 + 2 * typenames.cap;
+ typenames.items = nonnull(realloc(typenames.items,
+ sizeof(typenames.items[0]) * typenames.cap));
+ }
+
+ int pos = bsearch_typenames(name);
+ if (pos >= 0)
+ return; /* already in the list */
+
+ pos = -1 - pos;
+ memmove(typenames.items + pos + 1, typenames.items + pos,
+ sizeof(typenames.items[0]) * (typenames.len++ - (unsigned)pos));
+ typenames.items[pos] = nonnull(strdup(name));
+}
+
static int
cmp_keyword_by_name(const void *key, const void *elem)
{
@@ -353,7 +372,6 @@ probably_function_definition(void)
return true;
}
-/* Read an alphanumeric token into 'token', or return lsym_eof. */
static lexer_symbol
lexi_alnum(void)
{
@@ -476,17 +494,12 @@ lex_asterisk_pointer(void)
ps.line_has_func_def = true;
}
-static void
-skip_blank(const char **pp)
-{
- while (ch_isblank(**pp))
- (*pp)++;
-}
-
static bool
-skip_string(const char **pp, const char *s)
+skip(const char **pp, const char *s)
{
size_t len = strlen(s);
+ while (ch_isblank(**pp))
+ (*pp)++;
if (strncmp(*pp, s, len) == 0) {
*pp += len;
return true;
@@ -498,31 +511,20 @@ static void
lex_indent_comment(void)
{
const char *p = inp.s;
-
- skip_blank(&p);
- if (!skip_string(&p, "/*"))
- return;
- skip_blank(&p);
- if (!skip_string(&p, "INDENT"))
- return;
-
- enum indent_enabled enabled;
- skip_blank(&p);
- if (*p == '*' || skip_string(&p, "ON"))
- enabled = indent_last_off_line;
- else if (skip_string(&p, "OFF"))
- enabled = indent_off;
- else
- return;
-
- skip_blank(&p);
- if (!skip_string(&p, "*/\n"))
- return;
-
- if (lab.len > 0 || code.len > 0 || com.len > 0)
- output_line();
-
- indent_enabled = enabled;
+ if (skip(&p, "/*") && skip(&p, "INDENT")) {
+ enum indent_enabled enabled;
+ if (skip(&p, "ON") || *p == '*')
+ enabled = indent_last_off_line;
+ else if (skip(&p, "OFF"))
+ enabled = indent_off;
+ else
+ return;
+ if (skip(&p, "*/\n")) {
+ if (lab.len > 0 || code.len > 0 || com.len > 0)
+ output_line();
+ indent_enabled = enabled;
+ }
+ }
}
/* Reads the next token, placing it in the global variable "token". */
@@ -612,10 +614,8 @@ lexi(void)
case ':':
lsym = ps.quest_level > 0
- ? (ps.quest_level--, lsym_colon_question)
- : ps.in_var_decl
- ? lsym_colon_other
- : lsym_colon_label;
+ ? (ps.quest_level--, lsym_question_colon)
+ : ps.in_var_decl ? lsym_other_colon : lsym_label_colon;
next_unary = true;
break;
@@ -642,7 +642,7 @@ lexi(void)
case '>':
case '<':
- case '!': /* ops like <, <<, <=, !=, etc */
+ case '!': /* ops like <, <<, <=, !=, etc. */
if (inp_p[0] == '>' || inp_p[0] == '<' || inp_p[0] == '=')
token_add_char(*inp_p++);
if (inp_p[0] == '=')
@@ -671,7 +671,7 @@ lexi(void)
break;
}
- /* things like '||', '&&', '<<=' */
+ /* punctuation like '%', '&&', '/', '^', '||', '~' */
lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
if (inp_p[0] == token.s[token.len - 1])
token_add_char(*inp_p++), lsym = lsym_binary_op;
@@ -685,22 +685,3 @@ lexi(void)
return lsym;
}
-
-void
-register_typename(const char *name)
-{
- if (typenames.len >= typenames.cap) {
- typenames.cap = 16 + 2 * typenames.cap;
- typenames.items = nonnull(realloc(typenames.items,
- sizeof(typenames.items[0]) * typenames.cap));
- }
-
- int pos = bsearch_typenames(name);
- if (pos >= 0)
- return; /* already in the list */
-
- pos = -(pos + 1);
- memmove(typenames.items + pos + 1, typenames.items + pos,
- sizeof(typenames.items[0]) * (typenames.len++ - (unsigned)pos));
- typenames.items[pos] = nonnull(strdup(name));
-}
Index: src/usr.bin/indent/parse.c
diff -u src/usr.bin/indent/parse.c:1.70 src/usr.bin/indent/parse.c:1.71
--- src/usr.bin/indent/parse.c:1.70 Fri Jun 9 07:20:30 2023
+++ src/usr.bin/indent/parse.c Sat Jun 10 16:43:56 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: parse.c,v 1.70 2023/06/09 07:20:30 rillig Exp $ */
+/* $NetBSD: parse.c,v 1.71 2023/06/10 16:43:56 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: parse.c,v 1.70 2023/06/09 07:20:30 rillig Exp $");
+__RCSID("$NetBSD: parse.c,v 1.71 2023/06/10 16:43:56 rillig Exp $");
#include <err.h>
@@ -141,7 +141,7 @@ is_lbrace(parser_symbol psym)
}
/*
- * Shift the token onto the parser stack, or reduce it by combining it with
+ * Shift the token onto the parser stack, then try to reduce it by combining it with
* previous tokens.
*/
void
@@ -160,6 +160,42 @@ parse(parser_symbol psym)
switch (psym) {
+ case psym_lbrace_block:
+ case psym_lbrace_struct:
+ case psym_lbrace_union:
+ case psym_lbrace_enum:
+ ps.break_after_comma = false;
+ if (psyms->sym[psyms->top] == psym_decl
+ || psyms->sym[psyms->top] == psym_stmt
+ || psyms->sym[psyms->top] == psym_stmt_list)
+ ++ps.ind_level_follow;
+ else if (code.len == 0) {
+ /* It is part of a while, for, etc. */
+ --ps.ind_level;
+
+ /* for a switch, brace should be two levels out from
+ * the code */
+ if (psyms->sym[psyms->top] == psym_switch_expr
+ && opt.case_indent >= 1.0F)
+ --ps.ind_level;
+ }
+
+ ps_push(psym);
+ ps_push_follow(psym_stmt);
+ break;
+
+ case psym_rbrace:
+ /* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
+ if (!(psyms->top > 0
+ && is_lbrace(psyms->sym[psyms->top - 1]))) {
+ diag(1, "Statement nesting error");
+ break;
+ }
+ ps.ind_level = ps.ind_level_follow =
+ psyms->ind_level[--psyms->top];
+ psyms->sym[psyms->top] = psym_stmt;
+ break;
+
case psym_decl:
if (psyms->sym[psyms->top] == psym_decl)
break; /* only put one declaration onto stack */
@@ -171,6 +207,11 @@ parse(parser_symbol psym)
ps.ind_level_follow = ps.ind_level = decl_level();
break;
+ case psym_stmt:
+ ps.break_after_comma = false;
+ ps_push(psym_stmt);
+ break;
+
case psym_if_expr:
if (psyms->sym[psyms->top] == psym_if_expr_stmt_else
&& opt.else_if_in_same_line)
@@ -182,47 +223,6 @@ parse(parser_symbol psym)
ps_push(psym);
break;
- case psym_lbrace_block:
- case psym_lbrace_struct:
- case psym_lbrace_union:
- case psym_lbrace_enum:
- ps.break_after_comma = false;
- if (psyms->sym[psyms->top] == psym_stmt
- || psyms->sym[psyms->top] == psym_decl
- || psyms->sym[psyms->top] == psym_stmt_list)
- ++ps.ind_level_follow; /* it is a random, isolated
- * stmt group or a declaration
- */
- else {
- if (code.len == 0) {
- /* it is a group as part of a while, for, etc.
- */
- --ps.ind_level;
-
- /* for a switch, brace should be two levels out
- * from the code */
- if (psyms->sym[psyms->top] == psym_switch_expr
- && opt.case_indent >= 1.0F)
- --ps.ind_level;
- }
- }
-
- ps_push(psym);
- ps_push_follow(psym_stmt);
- break;
-
- case psym_while_expr:
- if (psyms->sym[psyms->top] == psym_do_stmt) {
- ps.ind_level =
- ps.ind_level_follow = psyms->ind_level[psyms->top];
- ps_push(psym_while_expr);
- } else {
- ps_push_follow(psym_while_expr);
- ++ps.ind_level_follow;
- }
-
- break;
-
case psym_else:
if (psyms->sym[psyms->top] != psym_if_expr_stmt) {
diag(1, "Unmatched 'else'");
@@ -233,26 +233,20 @@ parse(parser_symbol psym)
psyms->sym[psyms->top] = psym_if_expr_stmt_else;
break;
- case psym_rbrace:
- /* stack should have <lbrace> <stmt> or <lbrace> <stmt_list> */
- if (!(psyms->top > 0
- && is_lbrace(psyms->sym[psyms->top - 1]))) {
- diag(1, "Statement nesting error");
- break;
- }
- ps.ind_level = ps.ind_level_follow =
- psyms->ind_level[--psyms->top];
- psyms->sym[psyms->top] = psym_stmt;
- break;
-
case psym_switch_expr:
ps_push_follow(psym_switch_expr);
ps.ind_level_follow += (int)opt.case_indent + 1;
break;
- case psym_stmt:
- ps.break_after_comma = false;
- ps_push(psym_stmt);
+ case psym_while_expr:
+ if (psyms->sym[psyms->top] == psym_do_stmt) {
+ ps.ind_level = ps.ind_level_follow =
+ psyms->ind_level[psyms->top];
+ ps_push(psym_while_expr);
+ } else {
+ ps_push_follow(psym_while_expr);
+ ++ps.ind_level_follow;
+ }
break;
default:
@@ -263,7 +257,7 @@ parse(parser_symbol psym)
if (psyms->top >= STACKSIZE - 1)
errx(1, "Parser stack overflow");
- debug_parse_stack("before reduction");
+ debug_psyms_stack("before reduction");
psyms_reduce(&ps.psyms);
- debug_parse_stack("after reduction");
+ debug_psyms_stack("after reduction");
}
Index: src/usr.bin/indent/pr_comment.c
diff -u src/usr.bin/indent/pr_comment.c:1.160 src/usr.bin/indent/pr_comment.c:1.161
--- src/usr.bin/indent/pr_comment.c:1.160 Sat Jun 10 12:59:31 2023
+++ src/usr.bin/indent/pr_comment.c Sat Jun 10 16:43:56 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: pr_comment.c,v 1.160 2023/06/10 12:59:31 rillig Exp $ */
+/* $NetBSD: pr_comment.c,v 1.161 2023/06/10 16:43:56 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: pr_comment.c,v 1.160 2023/06/10 12:59:31 rillig Exp $");
+__RCSID("$NetBSD: pr_comment.c,v 1.161 2023/06/10 16:43:56 rillig Exp $");
#include <string.h>
@@ -105,7 +105,7 @@ analyze_comment(bool *p_may_wrap, bool *
line_length = opt.block_comment_max_line_length;
if (may_wrap && inp_p[0] == '\n')
delim = true;
- if (may_wrap && opt.comment_delimiter_on_blankline)
+ if (may_wrap && opt.comment_delimiter_on_blank_line)
delim = true;
} else {
int target_ind = code.len > 0