Module Name: src
Committed By: rillig
Date: Sun Jun 25 19:35:45 UTC 2023
Modified Files:
src/usr.bin/indent: indent.c lexi.c
Log Message:
indent: move cast detection from the lexer to the main processor
It is not the job of the lexer to modify the parser state.
To generate a diff of this commit:
cvs rdiff -u -r1.383 -r1.384 src/usr.bin/indent/indent.c
cvs rdiff -u -r1.235 -r1.236 src/usr.bin/indent/lexi.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.383 src/usr.bin/indent/indent.c:1.384
--- src/usr.bin/indent/indent.c:1.383 Sun Jun 25 19:19:42 2023
+++ src/usr.bin/indent/indent.c Sun Jun 25 19:35:45 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: indent.c,v 1.383 2023/06/25 19:19:42 rillig Exp $ */
+/* $NetBSD: indent.c,v 1.384 2023/06/25 19:35:45 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: indent.c,v 1.383 2023/06/25 19:19:42 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.384 2023/06/25 19:35:45 rillig Exp $");
#include <sys/param.h>
#include <err.h>
@@ -958,6 +958,10 @@ process_type_outside_parentheses(void)
static void
process_word(lexer_symbol lsym)
{
+ if (lsym == lsym_type /* in parentheses */
+ && ps.paren.item[ps.paren.len - 1].cast == cast_unknown)
+ ps.paren.item[ps.paren.len - 1].cast = cast_maybe;
+
if (ps.in_decl) {
if (lsym == lsym_funcname) {
ps.in_decl = false;
Index: src/usr.bin/indent/lexi.c
diff -u src/usr.bin/indent/lexi.c:1.235 src/usr.bin/indent/lexi.c:1.236
--- src/usr.bin/indent/lexi.c:1.235 Sun Jun 25 19:29:57 2023
+++ src/usr.bin/indent/lexi.c Sun Jun 25 19:35:45 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: lexi.c,v 1.235 2023/06/25 19:29:57 rillig Exp $ */
+/* $NetBSD: lexi.c,v 1.236 2023/06/25 19:35:45 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: lexi.c,v 1.235 2023/06/25 19:29:57 rillig Exp $");
+__RCSID("$NetBSD: lexi.c,v 1.236 2023/06/25 19:35:45 rillig Exp $");
#include <stdlib.h>
#include <string.h>
@@ -296,6 +296,8 @@ bsearch_typenames(const char *key)
static bool
is_typename(void)
{
+ if (ps.prev_lsym == lsym_tag)
+ return true;
if (opt.auto_typedefs &&
token.len >= 2 && memcmp(token.s + token.len - 2, "_t", 2) == 0)
return true;
@@ -335,6 +337,7 @@ cmp_keyword_by_name(const void *key, con
static bool
probably_function_definition(const char *p)
{
+ // TODO: Don't look at characters in comments, see lsym_funcname.c.
int paren_level = 0;
for (; *p != '\n'; p++) {
if (*p == '(')
@@ -409,29 +412,20 @@ lexi_alnum(void)
array_length(keywords), sizeof(keywords[0]), cmp_keyword_by_name);
lexer_symbol lsym = lsym_word;
if (kw != NULL) {
- if (kw->lsym == lsym_type)
- lsym = lsym_type;
+ lsym = kw->lsym;
ps.next_unary = true;
- if (kw->lsym == lsym_tag || kw->lsym == lsym_type)
+ if (lsym == lsym_tag || lsym == lsym_type)
goto found_typename;
- return kw->lsym;
+ return lsym;
}
if (is_typename()) {
lsym = lsym_type;
ps.next_unary = true;
found_typename:
- if (ps.paren.len > 0) {
- /* inside parentheses: cast, param list, offsetof or
- * sizeof */
- struct paren_level *paren_level =
- ps.paren.item + ps.paren.len - 1;
- if (paren_level->cast == cast_unknown)
- paren_level->cast = cast_maybe;
- }
if (ps.prev_lsym != lsym_period
&& ps.prev_lsym != lsym_unary_op) {
- if (kw != NULL && kw->lsym == lsym_tag)
+ if (lsym == lsym_tag)
return lsym_tag;
if (ps.paren.len == 0)
return lsym_type;