indent: remove read pointer from buffers that don't need it

The only buffer that needs a read pointer is the current input line in
'inp'.

No functional change.
This commit is contained in:
rillig 2023-06-04 20:51:19 +00:00
parent 3d214a88cc
commit c6e9afe1fe
6 changed files with 165 additions and 166 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: debug.c,v 1.33 2023/06/04 17:54:11 rillig Exp $ */ /* $NetBSD: debug.c,v 1.34 2023/06/04 20:51:19 rillig Exp $ */
/*- /*-
* Copyright (c) 2023 The NetBSD Foundation, Inc. * Copyright (c) 2023 The NetBSD Foundation, Inc.
@ -30,7 +30,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__RCSID("$NetBSD: debug.c,v 1.33 2023/06/04 17:54:11 rillig Exp $"); __RCSID("$NetBSD: debug.c,v 1.34 2023/06/04 20:51:19 rillig Exp $");
#include <stdarg.h> #include <stdarg.h>
@ -196,7 +196,7 @@ debug_print_buf(const char *name, const struct buffer *buf)
{ {
if (buf->len > 0) { if (buf->len > 0) {
debug_printf(" %s ", name); debug_printf(" %s ", name);
debug_vis_range("\"", buf->st, buf->len, "\""); debug_vis_range("\"", buf->s, buf->len, "\"");
} }
} }

View File

@ -1,4 +1,4 @@
/* $NetBSD: indent.c,v 1.326 2023/06/04 17:54:11 rillig Exp $ */ /* $NetBSD: indent.c,v 1.327 2023/06/04 20:51:19 rillig Exp $ */
/*- /*-
* SPDX-License-Identifier: BSD-4-Clause * SPDX-License-Identifier: BSD-4-Clause
@ -38,7 +38,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__RCSID("$NetBSD: indent.c,v 1.326 2023/06/04 17:54:11 rillig Exp $"); __RCSID("$NetBSD: indent.c,v 1.327 2023/06/04 20:51:19 rillig Exp $");
#include <sys/param.h> #include <sys/param.h>
#include <err.h> #include <err.h>
@ -110,8 +110,7 @@ static void
buf_expand(struct buffer *buf, size_t add_size) buf_expand(struct buffer *buf, size_t add_size)
{ {
buf->cap = buf->cap + add_size + 400; buf->cap = buf->cap + add_size + 400;
buf->mem = nonnull(realloc(buf->mem, buf->cap)); buf->s = nonnull(realloc(buf->s, buf->cap));
buf->st = buf->mem;
} }
void void
@ -119,7 +118,7 @@ buf_add_char(struct buffer *buf, char ch)
{ {
if (buf->len == buf->cap) if (buf->len == buf->cap)
buf_expand(buf, 1); buf_expand(buf, 1);
buf->mem[buf->len++] = ch; buf->s[buf->len++] = ch;
} }
void void
@ -129,14 +128,14 @@ buf_add_chars(struct buffer *buf, const char *s, size_t len)
return; return;
if (len > buf->cap - buf->len) if (len > buf->cap - buf->len)
buf_expand(buf, len); buf_expand(buf, len);
memcpy(buf->mem + buf->len, s, len); memcpy(buf->s + buf->len, s, len);
buf->len += len; buf->len += len;
} }
static void static void
buf_add_buf(struct buffer *buf, const struct buffer *add) buf_add_buf(struct buffer *buf, const struct buffer *add)
{ {
buf_add_chars(buf, add->st, add->len); buf_add_chars(buf, add->s, add->len);
} }
void void
@ -301,7 +300,7 @@ set_initial_indentation(void)
inp_read_line(); inp_read_line();
int ind = 0; int ind = 0;
for (const char *p = inp.st;; p++) { for (const char *p = inp_p;; p++) {
if (*p == ' ') if (*p == ' ')
ind++; ind++;
else if (*p == '\t') else if (*p == '\t')
@ -349,13 +348,13 @@ update_ps_decl_ptr(lexer_symbol lsym)
ps.decl_ptr = dp_other; ps.decl_ptr = dp_other;
break; break;
case dp_word: case dp_word:
if (lsym == lsym_unary_op && token.st[0] == '*') if (lsym == lsym_unary_op && token.s[0] == '*')
ps.decl_ptr = dp_word_asterisk; ps.decl_ptr = dp_word_asterisk;
else else
ps.decl_ptr = dp_other; ps.decl_ptr = dp_other;
break; break;
case dp_word_asterisk: case dp_word_asterisk:
if (lsym == lsym_unary_op && token.st[0] == '*') if (lsym == lsym_unary_op && token.s[0] == '*')
ps.decl_ptr = dp_word_asterisk; ps.decl_ptr = dp_word_asterisk;
else else
ps.decl_ptr = dp_other; ps.decl_ptr = dp_other;
@ -375,8 +374,8 @@ static void
update_ps_prev_tag(lexer_symbol lsym) update_ps_prev_tag(lexer_symbol lsym)
{ {
if (lsym == lsym_tag) { if (lsym == lsym_tag) {
ps.lbrace_kind = token.mem[0] == 's' ? psym_lbrace_struct : ps.lbrace_kind = token.s[0] == 's' ? psym_lbrace_struct :
token.mem[0] == 'u' ? psym_lbrace_union : token.s[0] == 'u' ? psym_lbrace_union :
psym_lbrace_enum; psym_lbrace_enum;
} else if (lsym != lsym_type_outside_parentheses } else if (lsym != lsym_type_outside_parentheses
&& lsym != lsym_word && lsym != lsym_word
@ -493,7 +492,7 @@ process_lparen(void)
} else if (want_blank_before_lparen()) } else if (want_blank_before_lparen())
buf_add_char(&code, ' '); buf_add_char(&code, ' ');
ps.want_blank = false; ps.want_blank = false;
buf_add_char(&code, token.st[0]); buf_add_char(&code, token.s[0]);
if (opt.extra_expr_indent && !opt.lineup_to_parens if (opt.extra_expr_indent && !opt.lineup_to_parens
&& ps.spaced_expr_psym != psym_0 && ps.nparen == 1 && ps.spaced_expr_psym != psym_0 && ps.nparen == 1
@ -508,7 +507,7 @@ process_lparen(void)
ps.init_or_struct = false; ps.init_or_struct = false;
} }
int indent = ind_add(0, code.st, code.len); int indent = ind_add(0, code.s, code.len);
if (opt.extra_expr_indent && ps.spaced_expr_psym != psym_0 if (opt.extra_expr_indent && ps.spaced_expr_psym != psym_0
&& ps.nparen == 1 && indent < 2 * opt.indent_size) && ps.nparen == 1 && indent < 2 * opt.indent_size)
indent = 2 * opt.indent_size; indent = 2 * opt.indent_size;
@ -548,9 +547,9 @@ process_lbracket(void)
if (want_blank_before_lbracket()) if (want_blank_before_lbracket())
buf_add_char(&code, ' '); buf_add_char(&code, ' ');
ps.want_blank = false; ps.want_blank = false;
buf_add_char(&code, token.st[0]); buf_add_char(&code, token.s[0]);
int indent = ind_add(0, code.st, code.len); int indent = ind_add(0, code.s, code.len);
ps.paren[ps.nparen - 1].indent = indent; ps.paren[ps.nparen - 1].indent = indent;
ps.paren[ps.nparen - 1].cast = cast_no; ps.paren[ps.nparen - 1].cast = cast_no;
@ -561,7 +560,7 @@ static void
process_rparen(void) process_rparen(void)
{ {
if (ps.nparen == 0) { if (ps.nparen == 0) {
diag(0, "Extra '%c'", *token.st); diag(0, "Extra '%c'", *token.s);
goto unbalanced; goto unbalanced;
} }
@ -579,7 +578,7 @@ process_rparen(void)
ps.line_start_nparen = ps.nparen; ps.line_start_nparen = ps.nparen;
unbalanced: unbalanced:
buf_add_char(&code, token.st[0]); buf_add_char(&code, token.s[0]);
if (ps.spaced_expr_psym != psym_0 && ps.nparen == 0) { if (ps.spaced_expr_psym != psym_0 && ps.nparen == 0) {
if (ps.extra_expr_indent == eei_yes) if (ps.extra_expr_indent == eei_yes)
@ -598,7 +597,7 @@ static void
process_rbracket(void) process_rbracket(void)
{ {
if (ps.nparen == 0) { if (ps.nparen == 0) {
diag(0, "Extra '%c'", *token.st); diag(0, "Extra '%c'", *token.s);
goto unbalanced; goto unbalanced;
} }
--ps.nparen; --ps.nparen;
@ -608,7 +607,7 @@ process_rbracket(void)
ps.line_start_nparen = ps.nparen; ps.line_start_nparen = ps.nparen;
unbalanced: unbalanced:
buf_add_char(&code, token.st[0]); buf_add_char(&code, token.s[0]);
} }
static bool static bool
@ -616,8 +615,8 @@ want_blank_before_unary_op(void)
{ {
if (ps.want_blank) if (ps.want_blank)
return true; return true;
if (token.st[0] == '+' || token.st[0] == '-') if (token.s[0] == '+' || token.s[0] == '-')
return code.len > 0 && code.mem[code.len - 1] == token.st[0]; return code.len > 0 && code.s[code.len - 1] == token.s[0];
return false; return false;
} }
@ -771,7 +770,7 @@ process_lbrace(void)
if (code.len > 0 && !ps.block_init) { if (code.len > 0 && !ps.block_init) {
if (!opt.brace_same_line || if (!opt.brace_same_line ||
(code.len > 0 && code.mem[code.len - 1] == '}')) (code.len > 0 && code.s[code.len - 1] == '}'))
output_line(); output_line();
else if (ps.in_func_def_params && !ps.init_or_struct) { else if (ps.in_func_def_params && !ps.init_or_struct) {
ps.ind_level_follow = 0; ps.ind_level_follow = 0;
@ -886,7 +885,7 @@ process_else(void)
ps.in_stmt_or_decl = false; ps.in_stmt_or_decl = false;
if (code.len > 0 if (code.len > 0
&& !(opt.cuddle_else && code.mem[code.len - 1] == '}')) { && !(opt.cuddle_else && code.s[code.len - 1] == '}')) {
if (opt.verbose) if (opt.verbose)
diag(0, "Line broken"); diag(0, "Line broken");
output_line(); output_line();
@ -940,9 +939,9 @@ process_ident(lexer_symbol lsym)
} else if (!ps.block_init && !ps.decl_indent_done && } else if (!ps.block_init && !ps.decl_indent_done &&
ps.line_start_nparen == 0) { ps.line_start_nparen == 0) {
if (opt.decl_indent == 0 if (opt.decl_indent == 0
&& code.len > 0 && code.mem[code.len - 1] == '}') && code.len > 0 && code.s[code.len - 1] == '}')
ps.decl_ind = ps.decl_ind =
ind_add(0, code.st, code.len) + 1; ind_add(0, code.s, code.len) + 1;
code_add_decl_indent(ps.decl_ind, ps.tabs_to_var); code_add_decl_indent(ps.decl_ind, ps.tabs_to_var);
ps.decl_indent_done = true; ps.decl_indent_done = true;
ps.want_blank = false; ps.want_blank = false;
@ -960,7 +959,7 @@ process_ident(lexer_symbol lsym)
static void static void
process_period(void) process_period(void)
{ {
if (code.len > 0 && code.mem[code.len - 1] == ',') if (code.len > 0 && code.s[code.len - 1] == ',')
buf_add_char(&code, ' '); buf_add_char(&code, ' ');
buf_add_char(&code, '.'); buf_add_char(&code, '.');
ps.want_blank = false; ps.want_blank = false;
@ -986,7 +985,7 @@ process_comma(void)
ps.block_init = false; ps.block_init = false;
int typical_varname_length = 8; int typical_varname_length = 8;
if (ps.break_after_comma && (opt.break_after_comma || if (ps.break_after_comma && (opt.break_after_comma ||
ind_add(compute_code_indent(), code.st, code.len) ind_add(compute_code_indent(), code.s, code.len)
>= opt.max_line_length - typical_varname_length)) >= opt.max_line_length - typical_varname_length))
ps.force_nl = true; ps.force_nl = true;
} }
@ -1002,20 +1001,20 @@ read_preprocessing_line(void)
buf_add_char(&lab, '#'); buf_add_char(&lab, '#');
while (ch_isblank(inp.st[0])) while (ch_isblank(inp_p[0]))
buf_add_char(&lab, *inp.st++); buf_add_char(&lab, *inp_p++);
while (inp.st[0] != '\n' || (state == COMM && !had_eof)) { while (inp_p[0] != '\n' || (state == COMM && !had_eof)) {
buf_add_char(&lab, inp_next()); buf_add_char(&lab, inp_next());
switch (lab.mem[lab.len - 1]) { switch (lab.s[lab.len - 1]) {
case '\\': case '\\':
if (state != COMM) if (state != COMM)
buf_add_char(&lab, inp_next()); buf_add_char(&lab, inp_next());
break; break;
case '/': case '/':
if (inp.st[0] == '*' && state == PLAIN) { if (inp_p[0] == '*' && state == PLAIN) {
state = COMM; state = COMM;
buf_add_char(&lab, *inp.st++); buf_add_char(&lab, *inp_p++);
} }
break; break;
case '"': case '"':
@ -1031,15 +1030,15 @@ read_preprocessing_line(void)
state = CHR; state = CHR;
break; break;
case '*': case '*':
if (inp.st[0] == '/' && state == COMM) { if (inp_p[0] == '/' && state == COMM) {
state = PLAIN; state = PLAIN;
buf_add_char(&lab, *inp.st++); buf_add_char(&lab, *inp_p++);
} }
break; break;
} }
} }
while (lab.len > 0 && ch_isblank(lab.mem[lab.len - 1])) while (lab.len > 0 && ch_isblank(lab.s[lab.len - 1]))
lab.len--; lab.len--;
} }
@ -1051,8 +1050,8 @@ process_preprocessing(void)
read_preprocessing_line(); read_preprocessing_line();
const char *end = lab.mem + lab.len; const char *end = lab.s + lab.len;
const char *dir = lab.st + 1; const char *dir = lab.s + 1;
while (dir < end && ch_isblank(*dir)) while (dir < end && ch_isblank(*dir))
dir++; dir++;
size_t dir_len = 0; size_t dir_len = 0;

View File

@ -1,4 +1,4 @@
/* $NetBSD: indent.h,v 1.170 2023/06/04 17:54:11 rillig Exp $ */ /* $NetBSD: indent.h,v 1.171 2023/06/04 20:51:19 rillig Exp $ */
/*- /*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
@ -139,9 +139,8 @@ typedef enum parser_symbol {
/* A range of characters, not null-terminated. */ /* A range of characters, not null-terminated. */
struct buffer { struct buffer {
const char *st; /* start of the usable text */ char *s;
char *mem; size_t len;
size_t len; /* length of the usable text, from 'mem' */
size_t cap; size_t cap;
}; };
@ -150,12 +149,13 @@ extern FILE *output;
/* /*
* The current line from the input file, used by the lexer to generate tokens. * The current line from the input file, used by the lexer to generate tokens.
* To read from the line, start at inp.st and continue up to and including the * To read from the line, start at inp_p and continue up to and including the
* next '\n'. To read beyond the '\n', call inp_skip or inp_next, which will * next '\n'. To read beyond the '\n', call inp_skip or inp_next, which will
* make the next line available, invalidating any pointers into the previous * make the next line available, invalidating any pointers into the previous
* line. * line.
*/ */
extern struct buffer inp; extern struct buffer inp;
extern const char *inp_p;
extern struct buffer token; /* the current token to be processed, is extern struct buffer token; /* the current token to be processed, is
* typically copied to the buffer 'code', or in * typically copied to the buffer 'code', or in

View File

@ -1,4 +1,4 @@
/* $NetBSD: io.c,v 1.192 2023/06/04 18:58:30 rillig Exp $ */ /* $NetBSD: io.c,v 1.193 2023/06/04 20:51:19 rillig Exp $ */
/*- /*-
* SPDX-License-Identifier: BSD-4-Clause * SPDX-License-Identifier: BSD-4-Clause
@ -38,13 +38,14 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__RCSID("$NetBSD: io.c,v 1.192 2023/06/04 18:58:30 rillig Exp $"); __RCSID("$NetBSD: io.c,v 1.193 2023/06/04 20:51:19 rillig Exp $");
#include <stdio.h> #include <stdio.h>
#include "indent.h" #include "indent.h"
struct buffer inp; struct buffer inp;
const char *inp_p;
struct output_state out; struct output_state out;
static unsigned wrote_newlines = 2; /* 0 in the middle of a line, 1 after a static unsigned wrote_newlines = 2; /* 0 in the middle of a line, 1 after a
* single '\n', > 1 means there were (n * single '\n', > 1 means there were (n
@ -55,15 +56,15 @@ static int paren_indent;
void void
inp_skip(void) inp_skip(void)
{ {
inp.st++; inp_p++;
if ((size_t)(inp.st - inp.mem) >= inp.len) if ((size_t)(inp_p - inp.s) >= inp.len)
inp_read_line(); inp_read_line();
} }
char char
inp_next(void) inp_next(void)
{ {
char ch = inp.st[0]; char ch = inp_p[0];
inp_skip(); inp_skip();
return ch; return ch;
} }
@ -71,7 +72,6 @@ inp_next(void)
static void static void
inp_read_next_line(FILE *f) inp_read_next_line(FILE *f)
{ {
inp.st = inp.mem;
inp.len = 0; inp.len = 0;
for (;;) { for (;;) {
@ -90,6 +90,7 @@ inp_read_next_line(FILE *f)
if (ch == '\n') if (ch == '\n')
break; break;
} }
inp_p = inp.s;
} }
static void static void
@ -175,12 +176,12 @@ static int
output_line_label(void) output_line_label(void)
{ {
while (lab.len > 0 && ch_isblank(lab.mem[lab.len - 1])) while (lab.len > 0 && ch_isblank(lab.s[lab.len - 1]))
lab.len--; lab.len--;
int ind = output_indent(0, compute_label_indent()); int ind = output_indent(0, compute_label_indent());
output_range(lab.st, lab.len); output_range(lab.s, lab.len);
return ind_add(ind, lab.st, lab.len); return ind_add(ind, lab.s, lab.len);
} }
static int static int
@ -202,15 +203,15 @@ output_line_code(int ind)
int code_ind = output_indent(ind, target_ind); int code_ind = output_indent(ind, target_ind);
if (ind > 0 && code_ind == ind) if (ind > 0 && code_ind == ind)
output_range(" ", 1), code_ind++; output_range(" ", 1), code_ind++;
output_range(code.st, code.len); output_range(code.s, code.len);
return ind_add(code_ind, code.st, code.len); return ind_add(code_ind, code.s, code.len);
} }
static void static void
output_line_comment(int ind) output_line_comment(int ind)
{ {
int target_ind = ps.com_ind; int target_ind = ps.com_ind;
const char *p = com.st; const char *p = com.s;
target_ind += ps.comment_delta; target_ind += ps.comment_delta;
@ -235,11 +236,11 @@ output_line_comment(int ind)
ind = 0; ind = 0;
} }
while (com.mem + com.len > p && ch_isspace(com.mem[com.len - 1])) while (com.s + com.len > p && ch_isspace(com.s[com.len - 1]))
com.len--; com.len--;
(void)output_indent(ind, target_ind); (void)output_indent(ind, target_ind);
output_range(p, com.len - (size_t)(p - com.mem)); output_range(p, com.len - (size_t)(p - com.s));
ps.comment_delta = ps.n_comment_delta; ps.comment_delta = ps.n_comment_delta;
} }
@ -296,7 +297,7 @@ output_line(void)
if (indent_enabled == indent_last_off_line) { if (indent_enabled == indent_last_off_line) {
indent_enabled = indent_on; indent_enabled = indent_on;
output_range(out.indent_off_text.st, out.indent_off_text.len); output_range(out.indent_off_text.s, out.indent_off_text.len);
out.indent_off_text.len = 0; out.indent_off_text.len = 0;
} }
@ -329,11 +330,11 @@ static int
compute_code_indent_lineup(int base_ind) compute_code_indent_lineup(int base_ind)
{ {
int ind = paren_indent; int ind = paren_indent;
int overflow = ind_add(ind, code.st, code.len) - opt.max_line_length; int overflow = ind_add(ind, code.s, code.len) - opt.max_line_length;
if (overflow < 0) if (overflow < 0)
return ind; return ind;
if (ind_add(base_ind, code.st, code.len) < opt.max_line_length) { if (ind_add(base_ind, code.s, code.len) < opt.max_line_length) {
ind -= overflow + 2; ind -= overflow + 2;
if (ind > base_ind) if (ind > base_ind)
return ind; return ind;
@ -377,7 +378,7 @@ compute_label_indent(void)
{ {
if (out.line_kind == lk_case_or_default) if (out.line_kind == lk_case_or_default)
return (int)(case_ind * (float)opt.indent_size); return (int)(case_ind * (float)opt.indent_size);
if (lab.st[0] == '#') if (lab.s[0] == '#')
return 0; return 0;
return opt.indent_size * (ps.ind_level - 2); return opt.indent_size * (ps.ind_level - 2);
} }
@ -387,6 +388,6 @@ inp_read_line(void)
{ {
if (indent_enabled == indent_on) if (indent_enabled == indent_on)
out.indent_off_text.len = 0; out.indent_off_text.len = 0;
buf_add_chars(&out.indent_off_text, inp.mem, inp.len); buf_add_chars(&out.indent_off_text, inp.s, inp.len);
inp_read_next_line(input); inp_read_next_line(input);
} }

View File

@ -1,4 +1,4 @@
/* $NetBSD: lexi.c,v 1.211 2023/06/04 14:20:00 rillig Exp $ */ /* $NetBSD: lexi.c,v 1.212 2023/06/04 20:51:19 rillig Exp $ */
/*- /*-
* SPDX-License-Identifier: BSD-4-Clause * SPDX-License-Identifier: BSD-4-Clause
@ -38,7 +38,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__RCSID("$NetBSD: lexi.c,v 1.211 2023/06/04 14:20:00 rillig Exp $"); __RCSID("$NetBSD: lexi.c,v 1.212 2023/06/04 20:51:19 rillig Exp $");
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -176,9 +176,9 @@ static void
lex_number(void) lex_number(void)
{ {
for (unsigned char s = 'A'; s != 'f' && s != 'i' && s != 'u';) { for (unsigned char s = 'A'; s != 'f' && s != 'i' && s != 'u';) {
unsigned char ch = (unsigned char)inp.st[0]; unsigned char ch = (unsigned char)inp_p[0];
if (ch == '\\' && inp.st[1] == '\n') { if (ch == '\\' && inp_p[1] == '\n') {
inp.st++; inp_p++;
inp_skip(); inp_skip();
line_no++; line_no++;
continue; continue;
@ -217,10 +217,10 @@ static void
lex_word(void) lex_word(void)
{ {
for (;;) { for (;;) {
if (is_identifier_part(inp.st[0])) if (is_identifier_part(inp_p[0]))
token_add_char(*inp.st++); token_add_char(*inp_p++);
else if (inp.st[0] == '\\' && inp.st[1] == '\n') { else if (inp_p[0] == '\\' && inp_p[1] == '\n') {
inp.st++; inp_p++;
inp_skip(); inp_skip();
line_no++; line_no++;
} else } else
@ -231,18 +231,18 @@ lex_word(void)
static void static void
lex_char_or_string(void) lex_char_or_string(void)
{ {
for (char delim = token.mem[token.len - 1];;) { for (char delim = token.s[token.len - 1];;) {
if (inp.st[0] == '\n') { if (inp_p[0] == '\n') {
diag(1, "Unterminated literal"); diag(1, "Unterminated literal");
return; return;
} }
token_add_char(*inp.st++); token_add_char(*inp_p++);
if (token.mem[token.len - 1] == delim) if (token.s[token.len - 1] == delim)
return; return;
if (token.mem[token.len - 1] == '\\') { if (token.s[token.len - 1] == '\\') {
if (inp.st[0] == '\n') if (inp_p[0] == '\n')
++line_no; ++line_no;
token_add_char(inp_next()); token_add_char(inp_next());
} }
@ -259,10 +259,10 @@ probably_typename(void)
return false; return false;
if (ps.in_stmt_or_decl) /* XXX: this condition looks incorrect */ if (ps.in_stmt_or_decl) /* XXX: this condition looks incorrect */
return false; return false;
if (inp.st[0] == '*' && inp.st[1] != '=') if (inp_p[0] == '*' && inp_p[1] != '=')
goto maybe; goto maybe;
/* XXX: is_identifier_start */ /* XXX: is_identifier_start */
if (ch_isalpha(inp.st[0])) if (ch_isalpha(inp_p[0]))
goto maybe; goto maybe;
return false; return false;
maybe: maybe:
@ -295,10 +295,10 @@ static bool
is_typename(void) is_typename(void)
{ {
if (opt.auto_typedefs && if (opt.auto_typedefs &&
token.len >= 2 && memcmp(token.mem + token.len - 2, "_t", 2) == 0) token.len >= 2 && memcmp(token.s + token.len - 2, "_t", 2) == 0)
return true; return true;
return bsearch_typenames(token.st) >= 0; return bsearch_typenames(token.s) >= 0;
} }
static int static int
@ -315,7 +315,7 @@ static bool
probably_looking_at_definition(void) probably_looking_at_definition(void)
{ {
int paren_level = 0; int paren_level = 0;
for (const char *p = inp.st; *p != '\n'; p++) { for (const char *p = inp_p; *p != '\n'; p++) {
if (*p == '(') if (*p == '(')
paren_level++; paren_level++;
if (*p == ')' && --paren_level == 0) { if (*p == ')' && --paren_level == 0) {
@ -353,15 +353,15 @@ probably_looking_at_definition(void)
static lexer_symbol static lexer_symbol
lexi_alnum(void) lexi_alnum(void)
{ {
if (ch_isdigit(inp.st[0]) || if (ch_isdigit(inp_p[0]) ||
(inp.st[0] == '.' && ch_isdigit(inp.st[1]))) { (inp_p[0] == '.' && ch_isdigit(inp_p[1]))) {
lex_number(); lex_number();
} else if (is_identifier_start(inp.st[0])) { } else if (is_identifier_start(inp_p[0])) {
lex_word(); lex_word();
if (token.len == 1 && token.st[0] == 'L' && if (token.len == 1 && token.s[0] == 'L' &&
(inp.st[0] == '"' || inp.st[0] == '\'')) { (inp_p[0] == '"' || inp_p[0] == '\'')) {
token_add_char(*inp.st++); token_add_char(*inp_p++);
lex_char_or_string(); lex_char_or_string();
ps.next_unary = false; ps.next_unary = false;
return lsym_word; return lsym_word;
@ -369,8 +369,8 @@ lexi_alnum(void)
} else } else
return lsym_eof; /* just as a placeholder */ return lsym_eof; /* just as a placeholder */
while (ch_isblank(inp.st[0])) while (ch_isblank(inp_p[0]))
inp.st++; inp_p++;
ps.next_unary = ps.prev_lsym == lsym_tag ps.next_unary = ps.prev_lsym == lsym_tag
|| ps.prev_lsym == lsym_typedef; || ps.prev_lsym == lsym_typedef;
@ -380,7 +380,7 @@ lexi_alnum(void)
token_add_char('\0'); token_add_char('\0');
token.len--; token.len--;
const struct keyword *kw = bsearch(token.st, keywords, const struct keyword *kw = bsearch(token.s, keywords,
array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name); array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
lexer_symbol lsym = lsym_word; lexer_symbol lsym = lsym_word;
if (kw != NULL) { if (kw != NULL) {
@ -411,7 +411,7 @@ found_typename:
} }
} }
if (inp.st[0] == '(' && ps.tos <= 1 && ps.ind_level == 0 && if (inp_p[0] == '(' && ps.tos <= 1 && ps.ind_level == 0 &&
!ps.in_func_def_params && !ps.block_init) { !ps.in_func_def_params && !ps.block_init) {
if (ps.nparen == 0 && probably_looking_at_definition()) { if (ps.nparen == 0 && probably_looking_at_definition()) {
@ -446,7 +446,7 @@ is_asterisk_unary(void)
static bool static bool
probably_in_function_definition(void) probably_in_function_definition(void)
{ {
for (const char *tp = inp.st; *tp != '\n';) { for (const char *tp = inp_p; *tp != '\n';) {
if (ch_isspace(*tp)) if (ch_isspace(*tp))
tp++; tp++;
else if (is_identifier_start(*tp)) { else if (is_identifier_start(*tp)) {
@ -462,8 +462,8 @@ probably_in_function_definition(void)
static void static void
lex_asterisk_unary(void) lex_asterisk_unary(void)
{ {
while (inp.st[0] == '*' || ch_isspace(inp.st[0])) { while (inp_p[0] == '*' || ch_isspace(inp_p[0])) {
if (inp.st[0] == '*') if (inp_p[0] == '*')
token_add_char('*'); token_add_char('*');
inp_skip(); inp_skip();
} }
@ -493,7 +493,7 @@ skip_string(const char **pp, const char *s)
static void static void
lex_indent_comment(void) lex_indent_comment(void)
{ {
const char *p = inp.mem; const char *p = inp.s;
skip_blank(&p); skip_blank(&p);
if (!skip_string(&p, "/*")) if (!skip_string(&p, "/*"))
@ -530,11 +530,11 @@ lexi(void)
ps.next_col_1 = false; ps.next_col_1 = false;
for (;;) { for (;;) {
if (ch_isblank(inp.st[0])) { if (ch_isblank(inp_p[0])) {
ps.curr_col_1 = false; ps.curr_col_1 = false;
inp.st++; inp_p++;
} else if (inp.st[0] == '\\' && inp.st[1] == '\n') { } else if (inp_p[0] == '\\' && inp_p[1] == '\n') {
inp.st++; inp_p++;
inp_skip(); inp_skip();
line_no++; line_no++;
} else } else
@ -552,7 +552,7 @@ lexi(void)
lexer_symbol lsym; lexer_symbol lsym;
bool next_unary; bool next_unary;
switch (token.mem[token.len - 1]) { switch (token.s[token.len - 1]) {
/* INDENT OFF */ /* INDENT OFF */
case '(': lsym = lsym_lparen; next_unary = true; break; case '(': lsym = lsym_lparen; next_unary = true; break;
@ -601,8 +601,8 @@ lexi(void)
next_unary = true; next_unary = true;
/* '++' or '--' */ /* '++' or '--' */
if (inp.st[0] == token.mem[token.len - 1]) { if (inp_p[0] == token.s[token.len - 1]) {
token_add_char(*inp.st++); token_add_char(*inp_p++);
if (ps.prev_lsym == lsym_word || if (ps.prev_lsym == lsym_word ||
ps.prev_lsym == lsym_rparen || ps.prev_lsym == lsym_rparen ||
ps.prev_lsym == lsym_rbracket) { ps.prev_lsym == lsym_rbracket) {
@ -611,11 +611,11 @@ lexi(void)
next_unary = false; next_unary = false;
} }
} else if (inp.st[0] == '=') { /* '+=' or '-=' */ } else if (inp_p[0] == '=') { /* '+=' or '-=' */
token_add_char(*inp.st++); token_add_char(*inp_p++);
} else if (inp.st[0] == '>') { /* '->' */ } else if (inp_p[0] == '>') { /* '->' */
token_add_char(*inp.st++); token_add_char(*inp_p++);
lsym = lsym_unary_op; lsym = lsym_unary_op;
next_unary = false; next_unary = false;
ps.want_blank = false; ps.want_blank = false;
@ -625,8 +625,8 @@ lexi(void)
case '=': case '=':
if (ps.init_or_struct) if (ps.init_or_struct)
ps.block_init = true; ps.block_init = true;
if (inp.st[0] == '=') if (inp_p[0] == '=')
token_add_char(*inp.st++); token_add_char(*inp_p++);
lsym = lsym_binary_op; lsym = lsym_binary_op;
next_unary = true; next_unary = true;
break; break;
@ -634,10 +634,10 @@ lexi(void)
case '>': case '>':
case '<': case '<':
case '!': /* ops like <, <<, <=, !=, etc */ case '!': /* ops like <, <<, <=, !=, etc */
if (inp.st[0] == '>' || inp.st[0] == '<' || inp.st[0] == '=') if (inp_p[0] == '>' || inp_p[0] == '<' || inp_p[0] == '=')
token_add_char(*inp.st++); token_add_char(*inp_p++);
if (inp.st[0] == '=') if (inp_p[0] == '=')
token_add_char(*inp.st++); token_add_char(*inp_p++);
lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op; lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
next_unary = true; next_unary = true;
break; break;
@ -648,30 +648,30 @@ lexi(void)
lsym = lsym_unary_op; lsym = lsym_unary_op;
next_unary = true; next_unary = true;
} else { } else {
if (inp.st[0] == '=') if (inp_p[0] == '=')
token_add_char(*inp.st++); token_add_char(*inp_p++);
lsym = lsym_binary_op; lsym = lsym_binary_op;
next_unary = true; next_unary = true;
} }
break; break;
default: default:
if (token.mem[token.len - 1] == '/' if (token.s[token.len - 1] == '/'
&& (inp.st[0] == '*' || inp.st[0] == '/')) { && (inp_p[0] == '*' || inp_p[0] == '/')) {
enum indent_enabled prev = indent_enabled; enum indent_enabled prev = indent_enabled;
lex_indent_comment(); lex_indent_comment();
if (prev == indent_on && indent_enabled == indent_off) if (prev == indent_on && indent_enabled == indent_off)
out.indent_off_text.len = 0; out.indent_off_text.len = 0;
token_add_char(*inp.st++); token_add_char(*inp_p++);
lsym = lsym_comment; lsym = lsym_comment;
next_unary = ps.next_unary; next_unary = ps.next_unary;
break; break;
} }
/* things like '||', '&&', '<<=', 'int *****i' */ /* things like '||', '&&', '<<=', 'int *****i' */
while (inp.st[0] == token.mem[token.len - 1] while (inp_p[0] == token.s[token.len - 1]
|| inp.st[0] == '=') || inp_p[0] == '=')
token_add_char(*inp.st++); token_add_char(*inp_p++);
lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op; lsym = ps.next_unary ? lsym_unary_op : lsym_binary_op;
next_unary = true; next_unary = true;

View File

@ -1,4 +1,4 @@
/* $NetBSD: pr_comment.c,v 1.150 2023/06/04 20:23:12 rillig Exp $ */ /* $NetBSD: pr_comment.c,v 1.151 2023/06/04 20:51:19 rillig Exp $ */
/*- /*-
* SPDX-License-Identifier: BSD-4-Clause * SPDX-License-Identifier: BSD-4-Clause
@ -38,7 +38,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__RCSID("$NetBSD: pr_comment.c,v 1.150 2023/06/04 20:23:12 rillig Exp $"); __RCSID("$NetBSD: pr_comment.c,v 1.151 2023/06/04 20:51:19 rillig Exp $");
#include <string.h> #include <string.h>
@ -61,7 +61,7 @@ com_add_delim(void)
static bool static bool
fits_in_one_line(int com_ind, int max_line_length) fits_in_one_line(int com_ind, int max_line_length)
{ {
for (const char *start = inp.st, *p = start; *p != '\n'; p++) { for (const char *start = inp_p, *p = start; *p != '\n'; p++) {
if (p[0] == '*' && p[1] == '/') { if (p[0] == '*' && p[1] == '/') {
int len = ind_add(com_ind + 3, int len = ind_add(com_ind + 3,
start, (size_t)(p - start)); start, (size_t)(p - start));
@ -86,13 +86,13 @@ analyze_comment(bool *p_may_wrap, bool *p_delim, int *p_line_length)
ind = 0; ind = 0;
} else { } else {
if (inp.st[0] == '-' || inp.st[0] == '*' || if (inp_p[0] == '-' || inp_p[0] == '*' ||
token.mem[token.len - 1] == '/' || token.s[token.len - 1] == '/' ||
(inp.st[0] == '\n' && !opt.format_block_comments)) { (inp_p[0] == '\n' && !opt.format_block_comments)) {
may_wrap = false; may_wrap = false;
delim = false; delim = false;
} }
if (code.len == 0 && inp.st[strspn(inp.st, "*")] == '\n') if (code.len == 0 && inp_p[strspn(inp_p, "*")] == '\n')
out.line_kind = lk_block_comment; out.line_kind = lk_block_comment;
if (com.len > 0) if (com.len > 0)
@ -107,8 +107,8 @@ analyze_comment(bool *p_may_wrap, bool *p_delim, int *p_line_length)
delim = false; delim = false;
int target_ind = code.len > 0 int target_ind = code.len > 0
? ind_add(compute_code_indent(), code.st, code.len) ? ind_add(compute_code_indent(), code.s, code.len)
: ind_add(compute_label_indent(), lab.st, lab.len); : ind_add(compute_label_indent(), lab.s, lab.len);
ind = ps.decl_on_line || ps.ind_level == 0 ind = ps.decl_on_line || ps.ind_level == 0
? opt.decl_comment_column - 1 ? opt.decl_comment_column - 1
@ -125,20 +125,20 @@ analyze_comment(bool *p_may_wrap, bool *p_delim, int *p_line_length)
if (!may_wrap) { if (!may_wrap) {
/* Find out how much indentation there was originally, because /* Find out how much indentation there was originally, because
* that much will have to be ignored by output_line. */ * that much will have to be ignored by output_line. */
size_t len = (size_t)(inp.st - 2 - inp.mem); size_t len = (size_t)(inp_p - 2 - inp.s);
ps.n_comment_delta = -ind_add(0, inp.mem, len); ps.n_comment_delta = -ind_add(0, inp.s, len);
} else { } else {
ps.n_comment_delta = 0; ps.n_comment_delta = 0;
if (!(inp.st[0] == '\t' && !ch_isblank(inp.st[1]))) if (!(inp_p[0] == '\t' && !ch_isblank(inp_p[1])))
while (ch_isblank(inp.st[0])) while (ch_isblank(inp_p[0]))
inp.st++; inp_p++;
} }
ps.comment_delta = 0; ps.comment_delta = 0;
com_add_char('/'); com_add_char('/');
com_add_char(token.mem[token.len - 1]); /* either '*' or '/' */ com_add_char(token.s[token.len - 1]); /* either '*' or '/' */
if (may_wrap && !ch_isblank(inp.st[0])) if (may_wrap && !ch_isblank(inp_p[0]))
com_add_char(' '); com_add_char(' ');
if (delim && fits_in_one_line(ind, line_length)) if (delim && fits_in_one_line(ind, line_length))
@ -163,11 +163,10 @@ analyze_comment(bool *p_may_wrap, bool *p_delim, int *p_line_length)
static void static void
copy_comment_wrap(int line_length, bool delim) copy_comment_wrap(int line_length, bool delim)
{ {
ssize_t last_blank = -1; /* index of the last blank in com.mem ssize_t last_blank = -1; /* index of the last blank in 'com' */
*/
for (;;) { for (;;) {
switch (inp.st[0]) { switch (inp_p[0]) {
case '\n': case '\n':
if (had_eof) { if (had_eof) {
diag(1, "Unterminated comment"); diag(1, "Unterminated comment");
@ -191,7 +190,7 @@ copy_comment_wrap(int line_length, bool delim)
} else { } else {
ps.next_col_1 = true; ps.next_col_1 = true;
if (!(com.len > 0 if (!(com.len > 0
&& ch_isblank(com.mem[com.len - 1]))) && ch_isblank(com.s[com.len - 1])))
com_add_char(' '); com_add_char(' ');
last_blank = (int)com.len - 1; last_blank = (int)com.len - 1;
} }
@ -201,21 +200,21 @@ copy_comment_wrap(int line_length, bool delim)
do { /* flush any blanks and/or tabs at start of do { /* flush any blanks and/or tabs at start of
* next line */ * next line */
inp_skip(); inp_skip();
if (inp.st[0] == '*' && skip_asterisk) { if (inp_p[0] == '*' && skip_asterisk) {
skip_asterisk = false; skip_asterisk = false;
inp.st++; inp_p++;
if (inp.st[0] == '/') if (inp_p[0] == '/')
goto end_of_comment; goto end_of_comment;
} }
} while (ch_isblank(inp.st[0])); } while (ch_isblank(inp_p[0]));
break; /* end of case for newline */ break; /* end of case for newline */
case '*': case '*':
inp.st++; inp_p++;
if (inp.st[0] == '/') { if (inp_p[0] == '/') {
end_of_comment: end_of_comment:
inp.st++; inp_p++;
if (delim) { if (delim) {
if (com.len > 3) if (com.len > 3)
@ -225,16 +224,16 @@ copy_comment_wrap(int line_length, bool delim)
com_add_char(' '); com_add_char(' ');
} else { } else {
size_t len = com.len; size_t len = com.len;
while (ch_isblank(com.mem[len - 1])) while (ch_isblank(com.s[len - 1]))
len--; len--;
int now_len = ind_add( int now_len = ind_add(
ps.com_ind, com.st, len); ps.com_ind, com.s, len);
if (now_len + 3 > line_length) if (now_len + 3 > line_length)
output_line(); output_line();
} }
if (!(com.len > 0 if (!(com.len > 0
&& ch_isblank(com.mem[com.len - 1]))) && ch_isblank(com.s[com.len - 1])))
com_add_char(' '); com_add_char(' ');
com_add_char('*'); com_add_char('*');
com_add_char('/'); com_add_char('/');
@ -246,14 +245,14 @@ copy_comment_wrap(int line_length, bool delim)
default: default:
; ;
int now_len = ind_add(ps.com_ind, com.st, com.len); int now_len = ind_add(ps.com_ind, com.s, com.len);
for (;;) { for (;;) {
char ch = inp_next(); char ch = inp_next();
if (ch_isblank(ch)) if (ch_isblank(ch))
last_blank = (ssize_t)com.len; last_blank = (ssize_t)com.len;
com_add_char(ch); com_add_char(ch);
now_len++; now_len++;
if (memchr("*\n\r\b\t", inp.st[0], 6) != NULL) if (memchr("*\n\r\b\t", inp_p[0], 6) != NULL)
break; break;
if (now_len >= line_length && last_blank != -1) if (now_len >= line_length && last_blank != -1)
break; break;
@ -263,7 +262,7 @@ copy_comment_wrap(int line_length, bool delim)
if (now_len <= line_length) if (now_len <= line_length)
break; break;
if (ch_isspace(com.mem[com.len - 1])) if (ch_isspace(com.s[com.len - 1]))
break; break;
if (last_blank == -1) { if (last_blank == -1) {
@ -273,7 +272,7 @@ copy_comment_wrap(int line_length, bool delim)
break; break;
} }
const char *last_word_s = com.mem + last_blank + 1; const char *last_word_s = com.s + last_blank + 1;
size_t last_word_len = com.len size_t last_word_len = com.len
- (size_t)(last_blank + 1); - (size_t)(last_blank + 1);
com.len = (size_t)last_blank; com.len = (size_t)last_blank;
@ -282,8 +281,8 @@ copy_comment_wrap(int line_length, bool delim)
/* Assume that output_line and com_add_delim don't /* Assume that output_line and com_add_delim don't
* invalidate the "unused" part of the buffer beyond * invalidate the "unused" part of the buffer beyond
* com.mem + com.len. */ * com.s + com.len. */
memmove(com.mem + com.len, last_word_s, last_word_len); memmove(com.s + com.len, last_word_s, last_word_len);
com.len += last_word_len; com.len += last_word_len;
last_blank = -1; last_blank = -1;
} }
@ -293,10 +292,10 @@ copy_comment_wrap(int line_length, bool delim)
static void static void
copy_comment_nowrap(void) copy_comment_nowrap(void)
{ {
char kind = token.mem[token.len - 1]; char kind = token.s[token.len - 1];
for (;;) { for (;;) {
if (inp.st[0] == '\n') { if (inp_p[0] == '\n') {
if (kind == '/') if (kind == '/')
return; return;
@ -315,10 +314,10 @@ copy_comment_nowrap(void)
continue; continue;
} }
com_add_char(*inp.st++); com_add_char(*inp_p++);
if (com.len >= 2 if (com.len >= 2
&& com.mem[com.len - 2] == '*' && com.s[com.len - 2] == '*'
&& com.mem[com.len - 1] == '/' && com.s[com.len - 1] == '/'
&& kind == '*') && kind == '*')
return; return;
} }