indent: improve type guessing, fix formatting of declarations

This commit is contained in:
rillig 2023-05-15 18:22:40 +00:00
parent 3357d7dc84
commit 219ccf6233
3 changed files with 30 additions and 13 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: lsym_type_outside_parentheses.c,v 1.4 2023/05/15 17:51:49 rillig Exp $ */
/* $NetBSD: lsym_type_outside_parentheses.c,v 1.5 2023/05/15 18:22:40 rillig Exp $ */
/*
* Tests for the token lsym_type_outside_parentheses, which represents a type
@ -6,6 +6,7 @@
* function.
*
* See also:
* fmt_decl
* lex_ident
* lsym_type_in_parentheses
* lsym_word
@ -19,11 +20,12 @@
//indent input
t1 *no_init_ptr;
t2 *init_ptr = 0;
/* $ FIXME: Assume that an identifier after 'const' is a type name. */
const t3 * const_no_init_ptr;
const t3 *const_no_init_ptr;
static t4 *static_no_init_ptr;
/* $ FIXME: Assume that an identifier after 'typedef' is a type name. */
typedef t5 * typedef_no_init_ptr;
typedef t5 *typedef_no_init_ptr;
// $ XXX: There's no point aligning the word 'const' with the other names.
const char *const names[3];
//indent end
//indent run-equals-input -di24

View File

@ -1,4 +1,4 @@
/* $NetBSD: indent.c,v 1.279 2023/05/15 14:55:47 rillig Exp $ */
/* $NetBSD: indent.c,v 1.280 2023/05/15 18:22:40 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: indent.c,v 1.279 2023/05/15 14:55:47 rillig Exp $");
__RCSID("$NetBSD: indent.c,v 1.280 2023/05/15 18:22:40 rillig Exp $");
#include <sys/param.h>
#include <err.h>
@ -411,6 +411,18 @@ want_blank_before_lparen(void)
return true;
}
static bool
want_blank_before_lbracket(void)
{
if (code.len == 0)
return false;
if (ps.prev_token == lsym_comma)
return true;
if (ps.prev_token == lsym_binary_op)
return true;
return false;
}
static void
process_lparen_or_lbracket(void)
{
@ -423,7 +435,8 @@ process_lparen_or_lbracket(void)
if (is_function_pointer_declaration()) {
code_add_decl_indent(ps.decl_ind, ps.tabs_to_var);
ps.decl_indent_done = true;
} else if (want_blank_before_lparen())
} else if (token.st[0] == '('
? want_blank_before_lparen() : want_blank_before_lbracket())
buf_add_char(&code, ' ');
ps.want_blank = false;
buf_add_char(&code, token.st[0]);

View File

@ -1,4 +1,4 @@
/* $NetBSD: lexi.c,v 1.190 2023/05/15 17:28:14 rillig Exp $ */
/* $NetBSD: lexi.c,v 1.191 2023/05/15 18:22:40 rillig Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
@ -38,7 +38,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: lexi.c,v 1.190 2023/05/15 17:28:14 rillig Exp $");
__RCSID("$NetBSD: lexi.c,v 1.191 2023/05/15 18:22:40 rillig Exp $");
#include <stdlib.h>
#include <string.h>
@ -47,6 +47,7 @@ __RCSID("$NetBSD: lexi.c,v 1.190 2023/05/15 17:28:14 rillig Exp $");
/* In lexi_alnum, this constant marks a type, independent of parentheses. */
#define lsym_type lsym_type_outside_parentheses
#define lsym_type_modifier lsym_storage_class
/* must be sorted alphabetically, is used in binary search */
static const struct keyword {
@ -62,7 +63,7 @@ static const struct keyword {
{"case", lsym_case_label},
{"char", lsym_type},
{"complex", lsym_type},
{"const", lsym_type},
{"const", lsym_type_modifier},
{"continue", lsym_word},
{"default", lsym_case_label},
{"do", lsym_do},
@ -92,7 +93,7 @@ static const struct keyword {
{"union", lsym_tag},
{"unsigned", lsym_type},
{"void", lsym_type},
{"volatile", lsym_type},
{"volatile", lsym_type_modifier},
{"while", lsym_while}
};
@ -371,7 +372,8 @@ lexi_alnum(void)
while (ch_isblank(inp_peek()))
inp_skip();
ps.next_unary = ps.prev_token == lsym_tag; /* for 'struct s *' */
ps.next_unary = ps.prev_token == lsym_tag
|| ps.prev_token == lsym_typedef;
if (ps.prev_token == lsym_tag && ps.nparen == 0)
return lsym_type_outside_parentheses;